[Android] Remove activities counter, AppCompat can do it

Geoffrey Métais git at videolan.org
Wed Apr 4 18:35:04 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Mar  9 15:08:29 2018 +0100| [d64036b7f47355fcedd99a01413e288e036858c2] | committer: Geoffrey Métais

Remove activities counter, AppCompat can do it

> https://code.videolan.org/videolan/vlc-android/commit/d64036b7f47355fcedd99a01413e288e036858c2
---

 .../src/org/videolan/vlc/ExternalMonitor.java      | 21 ++++++++++---
 .../src/org/videolan/vlc/VLCApplication.java       | 36 ++--------------------
 2 files changed, 19 insertions(+), 38 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/ExternalMonitor.java b/vlc-android/src/org/videolan/vlc/ExternalMonitor.java
index c5853e5e8..652d2e7be 100644
--- a/vlc-android/src/org/videolan/vlc/ExternalMonitor.java
+++ b/vlc-android/src/org/videolan/vlc/ExternalMonitor.java
@@ -24,6 +24,10 @@
 package org.videolan.vlc;
 
 import android.app.Activity;
+import android.arch.lifecycle.Lifecycle;
+import android.arch.lifecycle.LifecycleObserver;
+import android.arch.lifecycle.OnLifecycleEvent;
+import android.arch.lifecycle.ProcessLifecycleOwner;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -54,7 +58,7 @@ import java.util.Enumeration;
 import java.util.LinkedList;
 import java.util.List;
 
-public class ExternalMonitor extends BroadcastReceiver {
+public class ExternalMonitor extends BroadcastReceiver implements LifecycleObserver {
     public final static String TAG = "VLC/ExternalMonitor";
     private static volatile boolean connected = false;
     private static volatile boolean mobile = true;
@@ -63,11 +67,17 @@ public class ExternalMonitor extends BroadcastReceiver {
     private static final List<NetworkObserver> networkObservers = new LinkedList<>();
     private static WeakReference<Activity> storageObserver = null;
 
+    public ExternalMonitor() {
+        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
+    }
+
     public interface NetworkObserver {
         void onNetworkConnectionChanged(boolean connected);
     }
 
-    static void register(Context ctx) {
+    @OnLifecycleEvent(Lifecycle.Event.ON_START)
+    static void register() {
+        final Context ctx = VLCApplication.getAppContext();
         final IntentFilter networkFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
         final IntentFilter storageFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
         storageFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
@@ -88,7 +98,9 @@ public class ExternalMonitor extends BroadcastReceiver {
             });
     }
 
-    static void unregister(Context ctx) {
+    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
+    static void unregister() {
+        final Context ctx = VLCApplication.getAppContext();
         ctx.unregisterReceiver(instance);
     }
 
@@ -102,8 +114,7 @@ public class ExternalMonitor extends BroadcastReceiver {
         final String action = intent.getAction();
         switch (action) {
             case ConnectivityManager.CONNECTIVITY_ACTION:
-                if (cm == null)
-                    cm = (ConnectivityManager) VLCApplication.getAppContext().getSystemService(
+                if (cm == null) cm = (ConnectivityManager) VLCApplication.getAppContext().getSystemService(
                             Context.CONNECTIVITY_SERVICE);
                 final NetworkInfo networkInfo = cm.getActiveNetworkInfo();
                 final boolean isConnected = networkInfo != null && networkInfo.isConnected();
diff --git a/vlc-android/src/org/videolan/vlc/VLCApplication.java b/vlc-android/src/org/videolan/vlc/VLCApplication.java
index 4527e9432..f4b650f2e 100644
--- a/vlc-android/src/org/videolan/vlc/VLCApplication.java
+++ b/vlc-android/src/org/videolan/vlc/VLCApplication.java
@@ -19,14 +19,14 @@
  *****************************************************************************/
 package org.videolan.vlc;
 
-import android.app.Activity;
 import android.app.Application;
+import android.arch.lifecycle.Lifecycle;
+import android.arch.lifecycle.ProcessLifecycleOwner;
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.res.Configuration;
 import android.content.res.Resources;
-import android.os.Bundle;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.Process;
@@ -114,8 +114,6 @@ public class VLCApplication extends Application {
                 Dialog.setCallbacks(VLCInstance.get(), mDialogCallbacks);
             }
         });
-
-        registerActivityLifecycleCallbacks(sActivityCbListener);
     }
 
     @Override
@@ -283,34 +281,6 @@ public class VLCApplication extends Application {
      * @return true if an activity is displayed, false if app is in background.
      */
     public static boolean isForeground() {
-        return sActivitiesCount > 0;
+        return ProcessLifecycleOwner.get().getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED);
     }
-
-    private static int sActivitiesCount = 0;
-    private static ActivityLifecycleCallbacks sActivityCbListener = new ActivityLifecycleCallbacks() {
-        @Override
-        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
-
-        @Override
-        public void onActivityStarted(Activity activity) {
-            if (++sActivitiesCount == 1) ExternalMonitor.register(instance);
-        }
-
-        @Override
-        public void onActivityResumed(Activity activity) {}
-
-        @Override
-        public void onActivityPaused(Activity activity) {}
-
-        @Override
-        public void onActivityStopped(Activity activity) {
-            if (--sActivitiesCount == 0)  ExternalMonitor.unregister(instance);
-        }
-
-        @Override
-        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
-
-        @Override
-        public void onActivityDestroyed(Activity activity) {}
-    };
 }



More information about the Android mailing list