Bug 1317326 - Pause media playing when headset unplugged, r=alwu,sebastian
authormaliu <max@mxli.us>
Fri, 25 Nov 2016 21:23:05 +0800
changeset 325253 73cbeef4f94009a35a082b62d18196664df6965f
parent 325252 409240e746c8c54c85eb24c3ab13de5783d890aa
child 325254 fef5e39a4c3a0b9c4d3fbe5bd098fc17bf7bbb27
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewersalwu, sebastian
bugs1317326
milestone53.0a1
Bug 1317326 - Pause media playing when headset unplugged, r=alwu,sebastian MozReview-Commit-ID: GHdFWPgPum1
mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
--- a/mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
@@ -1,28 +1,32 @@
 package org.mozilla.gecko.media;
 
 import android.app.Notification;
 import android.app.PendingIntent;
 import android.app.Service;
+import android.content.BroadcastReceiver;
+import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
 import android.graphics.Paint;
 import android.graphics.Rect;
+import android.media.AudioManager;
 import android.media.session.MediaController;
 import android.media.session.MediaSession;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.IBinder;
+import android.support.annotation.CheckResult;
 import android.support.v4.app.NotificationManagerCompat;
 import android.util.Log;
 
-import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.BrowserApp;
 import org.mozilla.gecko.GeckoApp;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.PrefsHelper;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Tab;
 import org.mozilla.gecko.Tabs;
 import org.mozilla.gecko.util.ThreadUtils;
@@ -41,35 +45,38 @@ public class MediaControlService extends
 
     private static final int MEDIA_CONTROL_ID = 1;
     private static final String MEDIA_CONTROL_PREF = "dom.audiochannel.mediaControl";
 
     private String mActionState = ACTION_STOP;
 
     private MediaSession mSession;
     private MediaController mController;
+    private HeadSetStateReceiver mHeadSetStateReceiver;
 
     private PrefsHelper.PrefHandler mPrefsObserver;
     private final String[] mPrefs = { MEDIA_CONTROL_PREF };
 
     private boolean mInitialize = false;
     private boolean mIsMediaControlPrefOn = true;
 
     private static WeakReference<Tab> mTabReference = new WeakReference<>(null);
 
     private int minCoverSize;
     private int coverSize;
 
     @Override
     public void onCreate() {
         initialize();
+        mHeadSetStateReceiver = new HeadSetStateReceiver().registerReceiver(getApplicationContext());
     }
 
     @Override
     public void onDestroy() {
+        mHeadSetStateReceiver.unregisterReceiver(getApplicationContext());
         shutdown();
     }
 
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
         handleIntent(intent);
         return START_NOT_STICKY;
     }
@@ -272,16 +279,17 @@ public class MediaControlService extends
             public void onStop() {
                 Log.d(LOGTAG, "Controller, onStop");
                 super.onStop();
                 notifyControlInterfaceChanged(ACTION_STOP);
                 notifyObservers("MediaControl", "mediaControlStopped");
                 mTabReference = new WeakReference<>(null);
             }
         });
+
     }
 
     private void notifyObservers(String topic, String data) {
         GeckoAppShell.notifyObservers(topic, data);
     }
 
     private boolean isNeedToRemoveControlInterface(String action) {
         return action.equals(ACTION_STOP);
@@ -423,9 +431,34 @@ public class MediaControlService extends
 
         canvas.drawBitmap(favicon,
                 new Rect(0, 0, favicon.getWidth(), favicon.getHeight()),
                 new Rect(left, top, right, bottom),
                 paint);
 
         return coverArt;
     }
+
+    private class HeadSetStateReceiver extends BroadcastReceiver {
+
+        @CheckResult(suggest = "new HeadSetStateReceiver().registerReceiver(Context)")
+        HeadSetStateReceiver registerReceiver(Context context) {
+            IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
+            context.registerReceiver(this, intentFilter);
+            return this;
+        }
+
+        void unregisterReceiver(Context context) {
+            context.unregisterReceiver(HeadSetStateReceiver.this);
+        }
+
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            if (isMediaPlaying()) {
+                Intent pauseIntent = new Intent(getApplicationContext(), MediaControlService.class);
+                pauseIntent.setAction(ACTION_PAUSE);
+                handleIntent(pauseIntent);
+            }
+        }
+
+    }
+
 }
\ No newline at end of file