[Android] Sleep: move the receiver to the activity which must react

Sébastien Toque git at videolan.org
Sun Dec 30 19:30:25 CET 2012


vlc-ports/android | branch: master | Sébastien Toque <xilasz at gmail.com> | Fri Dec 28 22:07:13 2012 +0100| [35d373a24bbc5c6b308a481f5c0fafbb8a51af82] | committer: Sébastien Toque

Sleep: move the receiver to the activity which must react

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=35d373a24bbc5c6b308a481f5c0fafbb8a51af82
---

 .../vlc/gui/video/VideoOverflowDialog.java         |   22 ------------
 .../vlc/gui/video/VideoPlayerActivity.java         |   37 +++++++++++++-------
 2 files changed, 24 insertions(+), 35 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoOverflowDialog.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoOverflowDialog.java
index 94e5288..ce51ff6 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoOverflowDialog.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoOverflowDialog.java
@@ -22,17 +22,12 @@ package org.videolan.vlc.gui.video;
 import java.util.Calendar;
 
 import org.videolan.vlc.R;
-import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.gui.SpeedSelectorDialog;
 import org.videolan.vlc.gui.TimeSleepDialog;
 
 import android.app.Activity;
 import android.app.Dialog;
-import android.content.BroadcastReceiver;
-import android.content.Context;
 import android.content.DialogInterface;
-import android.content.Intent;
-import android.content.IntentFilter;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -68,9 +63,6 @@ public class VideoOverflowDialog extends Dialog {
         // Init Sleep function
         mSleep = (ImageButton) mAdvFuncView.findViewById(R.id.adv_func_sleep);
         mSleep.setOnClickListener(mSleepListener);
-        IntentFilter filter = new IntentFilter();
-        filter.addAction(VLCApplication.SLEEP_INTENT);
-        getOwnerActivity().registerReceiver(mSleepReceiver, filter);
 
         // Init Speed function
         mSpeed = (ImageButton) mAdvFuncView.findViewById(R.id.adv_func_speed);
@@ -78,17 +70,6 @@ public class VideoOverflowDialog extends Dialog {
         mSpeedInfo = (TextView) mAdvFuncView.findViewById(R.id.adv_func_speed_info);
     }
 
-    private final BroadcastReceiver mSleepReceiver = new BroadcastReceiver()
-    {
-        @Override
-        public void onReceive(Context context, Intent intent)
-        {
-            String action = intent.getAction();
-            if (action.equalsIgnoreCase(VLCApplication.SLEEP_INTENT))
-                getOwnerActivity().finish();
-        }
-    };
-
     private final View.OnClickListener mSleepListener = new View.OnClickListener() {
 
         @Override
@@ -129,9 +110,6 @@ public class VideoOverflowDialog extends Dialog {
             }
         }
 
-        // Unregister receiver
-        getOwnerActivity().unregisterReceiver(mSleepReceiver);
-
         // Dismiss main window
         if(isShowing())
             dismiss();
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index a89c78a..5f31cb5 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -26,6 +26,7 @@ import java.lang.reflect.Method;
 import java.net.URLDecoder;
 import java.util.ArrayList;
 import java.util.Arrays;
+
 import org.videolan.vlc.AudioServiceController;
 import org.videolan.vlc.DatabaseManager;
 import org.videolan.vlc.EventManager;
@@ -34,6 +35,7 @@ import org.videolan.vlc.LibVlcException;
 import org.videolan.vlc.Media;
 import org.videolan.vlc.R;
 import org.videolan.vlc.Util;
+import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.WeakHandler;
 import org.videolan.vlc.gui.PreferencesActivity;
 import org.videolan.vlc.gui.audio.AudioPlayerActivity;
@@ -267,7 +269,10 @@ public class VideoPlayerActivity extends Activity {
         mSwitchingView = false;
         mEndReached = false;
 
-        registerReceiver(mBatteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
+        IntentFilter filter = new IntentFilter();
+        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
+        filter.addAction(VLCApplication.SLEEP_INTENT);
+        registerReceiver(mReceiver, filter);
 
         try {
             mLibVLC = LibVLC.getInstance();
@@ -285,8 +290,6 @@ public class VideoPlayerActivity extends Activity {
         setRequestedOrientation(mScreenOrientation != 100
                 ? mScreenOrientation
                 : getScreenOrientation());
-
-        mOverflowDialog = new VideoOverflowDialog(VideoPlayerActivity.this);
     }
 
     @Override
@@ -359,7 +362,7 @@ public class VideoPlayerActivity extends Activity {
     @Override
     protected void onDestroy() {
         super.onDestroy();
-        unregisterReceiver(mBatteryReceiver);
+        unregisterReceiver(mReceiver);
         if (mLibVLC != null && !mSwitchingView) {
             mLibVLC.stop();
         }
@@ -426,19 +429,25 @@ public class VideoPlayerActivity extends Activity {
         context.startActivity(intent);
     }
 
-    private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver()
+    private final BroadcastReceiver mReceiver = new BroadcastReceiver()
     {
         @Override
         public void onReceive(Context context, Intent intent)
         {
-            int batteryLevel = intent.getIntExtra("level", 0);
-            if (batteryLevel >= 50)
-                mBattery.setTextColor(Color.GREEN);
-            else if  (batteryLevel >= 30)
-                mBattery.setTextColor(Color.YELLOW);
-            else
-                mBattery.setTextColor(Color.RED);
-            mBattery.setText(String.format("%d%%", batteryLevel));
+            String action = intent.getAction();
+            if (action.equalsIgnoreCase(Intent.ACTION_BATTERY_CHANGED)) {
+                int batteryLevel = intent.getIntExtra("level", 0);
+                if (batteryLevel >= 50)
+                    mBattery.setTextColor(Color.GREEN);
+                else if (batteryLevel >= 30)
+                    mBattery.setTextColor(Color.YELLOW);
+                else
+                    mBattery.setTextColor(Color.RED);
+                mBattery.setText(String.format("%d%%", batteryLevel));
+            }
+            else if (action.equalsIgnoreCase(VLCApplication.SLEEP_INTENT)) {
+                finish();
+            }
         }
     };
 
@@ -1320,6 +1329,8 @@ public class VideoPlayerActivity extends Activity {
     }
 
     public void showAdvanceFunction(View v) {
+        if (mOverflowDialog == null)
+            mOverflowDialog = new VideoOverflowDialog(this);
         mOverflowDialog.show();
     }
 }



More information about the Android mailing list