OSDN Git Service

Move isServiceRunning() to U.java
authorBraden Farmer <farmerbb@gmail.com>
Thu, 15 Dec 2016 00:13:05 +0000 (17:13 -0700)
committerBraden Farmer <farmerbb@gmail.com>
Thu, 15 Dec 2016 00:13:05 +0000 (17:13 -0700)
app/src/main/java/com/farmerbb/taskbar/MainActivity.java
app/src/main/java/com/farmerbb/taskbar/activity/IconPackApplyActivity.java
app/src/main/java/com/farmerbb/taskbar/activity/KeyboardShortcutActivity.java
app/src/main/java/com/farmerbb/taskbar/fragment/SettingsFragment.java
app/src/main/java/com/farmerbb/taskbar/receiver/BootReceiver.java
app/src/main/java/com/farmerbb/taskbar/util/U.java

index bca4b4f..616d0e4 100644 (file)
@@ -94,7 +94,7 @@ public class MainActivity extends AppCompatActivity {
                 break;
         }
 
-        if(pref.getBoolean("taskbar_active", false) && !isServiceRunning())
+        if(pref.getBoolean("taskbar_active", false) && !U.isServiceRunning(this, NotificationService.class))
             editor.putBoolean("taskbar_active", false);
 
         // Ensure that components that should be enabled are enabled properly
@@ -366,16 +366,6 @@ public class MainActivity extends AppCompatActivity {
         stopService(new Intent(this, NotificationService.class));
     }
 
-    private boolean isServiceRunning() {
-        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
-        for(ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
-            if(NotificationService.class.getName().equals(service.service.getClassName()))
-                return true;
-        }
-
-        return false;
-    }
-
     @TargetApi(Build.VERSION_CODES.M)
     private boolean canDrawOverlays() {
         return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.canDrawOverlays(this);
index c338eaf..1101dc6 100644 (file)
@@ -109,19 +109,9 @@ public class IconPackApplyActivity extends Activity {
 
             stopTaskbarService(true);
             startTaskbarService(true);
-        } else if(isServiceRunning()) {
+        } else if(U.isServiceRunning(this, StartMenuService.class)) {
             stopTaskbarService(false);
             startTaskbarService(false);
         }
     }
-
-    private boolean isServiceRunning() {
-        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
-        for(ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
-            if(StartMenuService.class.getName().equals(service.service.getClassName()))
-                return true;
-        }
-
-        return false;
-    }
 }
index 3a1d1da..3f61536 100644 (file)
@@ -62,7 +62,7 @@ public class KeyboardShortcutActivity extends Activity {
 
                 break;
             case Intent.ACTION_ASSIST:
-                if(getIntent().hasExtra(Intent.EXTRA_ASSIST_INPUT_HINT_KEYBOARD) && isServiceRunning()) {
+                if(getIntent().hasExtra(Intent.EXTRA_ASSIST_INPUT_HINT_KEYBOARD) && U.isServiceRunning(this, StartMenuService.class)) {
                     LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("com.farmerbb.taskbar.TOGGLE_START_MENU"));
                 } else {
                     Intent intent = new Intent("com.google.android.googlequicksearchbox.TEXT_ASSIST");
@@ -86,14 +86,4 @@ public class KeyboardShortcutActivity extends Activity {
 
         finish();
     }
-
-    private boolean isServiceRunning() {
-        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
-        for(ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
-            if(StartMenuService.class.getName().equals(service.service.getClassName()))
-                return true;
-        }
-
-        return false;
-    }
 }
index f4e5f7c..ebf5b87 100644 (file)
@@ -18,12 +18,10 @@ package com.farmerbb.taskbar.fragment;
 import android.annotation.SuppressLint;
 import android.annotation.TargetApi;
 import android.app.Activity;
-import android.app.ActivityManager;
 import android.app.AlertDialog;
 import android.app.FragmentTransaction;
 import android.content.ActivityNotFoundException;
 import android.content.ComponentName;
-import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.SharedPreferences;
@@ -412,7 +410,7 @@ public class SettingsFragment extends PreferenceFragment implements OnPreference
 
             stopTaskbarService(true);
             startTaskbarService(true);
-        } else if(isServiceRunning()) {
+        } else if(U.isServiceRunning(getActivity(), StartMenuService.class)) {
             stopTaskbarService(false);
             startTaskbarService(false);
         }
@@ -423,16 +421,6 @@ public class SettingsFragment extends PreferenceFragment implements OnPreference
         return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.canDrawOverlays(getActivity());
     }
 
-    private boolean isServiceRunning() {
-        ActivityManager manager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
-        for(ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
-            if(StartMenuService.class.getName().equals(service.service.getClassName()))
-                return true;
-        }
-
-        return false;
-    }
-
     @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
         if(requestCode == 123 && resultCode == Activity.RESULT_OK) {
index 00a03bb..fcbbbcf 100644 (file)
@@ -15,7 +15,6 @@
 
 package com.farmerbb.taskbar.receiver;
 
-import android.app.ActivityManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -50,18 +49,8 @@ public class BootReceiver extends BroadcastReceiver {
 
             context.startService(new Intent(context, NotificationService.class));
         } else {
-            editor.putBoolean("taskbar_active", isServiceRunning(context));
+            editor.putBoolean("taskbar_active", U.isServiceRunning(context, NotificationService.class));
             editor.apply();
         }
     }
-
-    private boolean isServiceRunning(Context context) {
-        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
-        for(ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
-            if(NotificationService.class.getName().equals(service.service.getClassName()))
-                return true;
-        }
-
-        return false;
-    }
 }
index b3e148e..e738f3e 100644 (file)
 package com.farmerbb.taskbar.util;
 
 import android.annotation.TargetApi;
+import android.app.ActivityManager;
 import android.app.ActivityOptions;
 import android.app.AlertDialog;
+import android.app.Service;
 import android.app.admin.DevicePolicyManager;
 import android.content.ActivityNotFoundException;
 import android.content.ComponentName;
@@ -777,4 +779,14 @@ public class U {
                 || Settings.Global.getInt(context.getContentResolver(), "enable_freeform_support", -1) == 1
                 || Settings.Global.getInt(context.getContentResolver(), "force_resizable_activities", -1) == 1);
     }
+
+    public static boolean isServiceRunning(Context context, Class<? extends Service> cls) {
+        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
+        for(ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
+            if(cls.getName().equals(service.service.getClassName()))
+                return true;
+        }
+
+        return false;
+    }
 }