[tor-commits] [snowflake-mobile/master] Added bare service class and called it from MainActivity

cohosh at torproject.org cohosh at torproject.org
Fri Jun 5 17:49:16 UTC 2020


commit cfbf2d3f4863f591d451303820476a0d5fac6f4b
Author: Hashik Donthineni <HashikDonthineni at gmail.com>
Date:   Fri Jun 5 02:18:34 2020 +0530

    Added bare service class and called it from MainActivity
---
 .idea/misc.xml                                     |  2 +-
 app/build.gradle                                   |  4 ++
 .../org/torproject/snowflake/MainActivity.java     | 60 ++++++++++++++--------
 .../constants/ForegroundServiceConstants.java      | 10 ++++
 app/src/main/res/values/strings.xml                |  2 +
 5 files changed, 55 insertions(+), 23 deletions(-)

diff --git a/.idea/misc.xml b/.idea/misc.xml
index 37a7509..7bfef59 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
-  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
     <output url="file://$PROJECT_DIR$/build/classes" />
   </component>
   <component name="ProjectType">
diff --git a/app/build.gradle b/app/build.gradle
index 113f2a2..9580614 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -20,6 +20,10 @@ android {
             proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
         }
     }
+    compileOptions {
+        sourceCompatibility JavaVersion.VERSION_1_8
+        targetCompatibility JavaVersion.VERSION_1_8
+    }
 
 }
 
diff --git a/app/src/main/java/org/torproject/snowflake/MainActivity.java b/app/src/main/java/org/torproject/snowflake/MainActivity.java
index 240e285..188eb57 100644
--- a/app/src/main/java/org/torproject/snowflake/MainActivity.java
+++ b/app/src/main/java/org/torproject/snowflake/MainActivity.java
@@ -1,44 +1,60 @@
 package org.torproject.snowflake;
 
-import androidx.appcompat.app.AppCompatActivity;
-
-import android.app.ActivityManager;
-import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.os.Build;
 import android.os.Bundle;
 import android.widget.Button;
-import android.widget.Toast;
 
+import androidx.appcompat.app.AppCompatActivity;
+
+import org.torproject.snowflake.constants.ForegroundServiceConstants;
+
+/**
+ * MainActivity is the main UI of the application.
+ */
 public class MainActivity extends AppCompatActivity {
+    private static final String TAG = "MainActivity";
+    private SharedPreferences sharedPreferences;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
+        sharedPreferences = getSharedPreferences(getString(R.string.sharedpreference_file), MODE_PRIVATE);
 
         Button startButton = findViewById(R.id.start_button);
-
         startButton.setOnClickListener(v -> {
-            if (isServiceRunning()){ //Toggling the service.
-                //TODO: Start service
-            }
-            else{
-                //TODO: Stop service
-            }
+            if (isServiceRunning()) //Toggling the service.
+                serviceToggle(ForegroundServiceConstants.ACTION_STOP);
+            else
+                serviceToggle(ForegroundServiceConstants.ACTION_START);
         });
         if (BuildConfig.DEBUG)
             startButton.performClick(); //To perform an automatic click in testing environment.
     }
 
-    //Test to see if the service is already running.
-    private boolean isServiceRunning() {
-        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
-        if (manager != null) {
-            for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
-                if (MyPersistentService.class.getName().equals(service.service.getClassName())) {
-                    return true;
-                }
-            }
+    /**
+     * Turn service on/off.
+     *
+     * @param action An Action from ForegroundServiceConstants.
+     */
+    private void serviceToggle(String action) {
+        Intent serviceIntent = new Intent(MainActivity.this, MyPersistentService.class);
+        serviceIntent.setAction(action);
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            startForegroundService(serviceIntent);
+        } else {
+            startService(serviceIntent);
         }
-        return false;
+    }
+
+    /**
+     * Test to see if the MyPersistentService is running or not.
+     *
+     * @return boolean whether the service is running or not.
+     */
+    private boolean isServiceRunning() {
+        return sharedPreferences.getBoolean(getString(R.string.is_service_running_bool), false);
     }
 }
diff --git a/app/src/main/java/org/torproject/snowflake/constants/ForegroundServiceConstants.java b/app/src/main/java/org/torproject/snowflake/constants/ForegroundServiceConstants.java
new file mode 100644
index 0000000..b446f0e
--- /dev/null
+++ b/app/src/main/java/org/torproject/snowflake/constants/ForegroundServiceConstants.java
@@ -0,0 +1,10 @@
+package org.torproject.snowflake.constants;
+
+public class ForegroundServiceConstants {
+    public static final String ACTION_START = "1";
+    public static final String ACTION_STOP = "0";
+
+    //Shared Preferences State
+    public static final int SERVICE_RUNNING = 1;
+    public static final int SERVICE_STOPPED = 0;
+}
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index a10296e..85551fb 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -1,3 +1,5 @@
 <resources>
     <string name="app_name">Snowflake</string>
+    <string name="sharedpreference_file">org.torproject.snowflake.snowflake_preferences</string>
+    <string name="is_service_running_bool">is_service_running</string>
 </resources>





More information about the tor-commits mailing list