[Android] PlaybackService: use Phone State intent only for froyo

Thomas Guillem git at videolan.org
Wed Jul 1 15:14:32 CEST 2015


vlc-ports/android | branch: master | Thomas Guillem <thomas at gllm.fr> | Wed Jul  1 10:13:56 2015 +0200| [81fc2b37bfd76ec34acf8e4629d860d0a5730072] | committer: Thomas Guillem

PlaybackService: use Phone State intent only for froyo

After froyo, we can handle it with audio focus

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

 vlc-android/AndroidManifest.xml                    |    3 +-
 .../src/org/videolan/vlc/PlaybackService.java      |   42 ++++++++++++--------
 2 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/vlc-android/AndroidManifest.xml b/vlc-android/AndroidManifest.xml
index 3b93657..68d4ee2 100644
--- a/vlc-android/AndroidManifest.xml
+++ b/vlc-android/AndroidManifest.xml
@@ -29,7 +29,8 @@
     <!-- /!\ PHONE_CALLS
          See org.videolan.vlc.PhoneStateReceiver.java
          -->
-    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
+    <uses-permission android:name="android.permission.READ_PHONE_STATE"
+       android:maxSdkVersion="10" />
 
     <!-- STORAGE -->
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.java b/vlc-android/src/org/videolan/vlc/PlaybackService.java
index 44f084b..fcafc30 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.java
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.java
@@ -136,7 +136,7 @@ public class PlaybackService extends Service {
     private PowerManager.WakeLock mWakeLock;
     private final AtomicBoolean mExpanding = new AtomicBoolean(false);
 
-    private static boolean mWasPlayingAudio = false;
+    private static boolean mWasPlayingAudio = false; // used only if readPhoneState returns true
 
     // Index management
     /**
@@ -184,6 +184,10 @@ public class PlaybackService extends Service {
         All
     }
 
+    private static boolean readPhoneState() {
+        return !AndroidUtil.isFroyoOrLater();
+    }
+
     @Override
     public void onCreate() {
         super.onCreate();
@@ -224,8 +228,10 @@ public class PlaybackService extends Service {
         filter.addAction(Intent.ACTION_HEADSET_PLUG);
         filter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
         filter.addAction(VLCApplication.SLEEP_INTENT);
-        filter.addAction(VLCApplication.INCOMING_CALL_INTENT);
-        filter.addAction(VLCApplication.CALL_ENDED_INTENT);
+        if (readPhoneState()) {
+            filter.addAction(VLCApplication.INCOMING_CALL_INTENT);
+            filter.addAction(VLCApplication.CALL_ENDED_INTENT);
+        }
         registerReceiver(serviceReceiver, filter);
 
         final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
@@ -440,21 +446,23 @@ public class PlaybackService extends Service {
                 return;
             }
 
-            /*
-             * Incoming Call : Pause if VLC is playing audio or video. 
-             */
-            if (action.equalsIgnoreCase(VLCApplication.INCOMING_CALL_INTENT)) {
-                mWasPlayingAudio = mMediaPlayer.isPlaying() && hasCurrentMedia();
-                if (mWasPlayingAudio)
-                    pause();
-            }
+            if (readPhoneState()) {
+                /*
+                 * Incoming Call : Pause if VLC is playing audio or video.
+                 */
+                if (action.equalsIgnoreCase(VLCApplication.INCOMING_CALL_INTENT)) {
+                    mWasPlayingAudio = mMediaPlayer.isPlaying() && hasCurrentMedia();
+                    if (mWasPlayingAudio)
+                        pause();
+                }
 
-            /*
-             * Call ended : Play only if VLC was playing audio.
-             */
-            if (action.equalsIgnoreCase(VLCApplication.CALL_ENDED_INTENT)
-                    && mWasPlayingAudio) {
-                play();
+                /*
+                 * Call ended : Play only if VLC was playing audio.
+                 */
+                if (action.equalsIgnoreCase(VLCApplication.CALL_ENDED_INTENT)
+                        && mWasPlayingAudio) {
+                    play();
+                }
             }
 
             // skip all headsets events if there is a call



More information about the Android mailing list