Bug 1531833 - Add a Java method that enables and disable all that is needed to do audio communication with an earpiece. r=snorp
authorPaul Adenot <paul@paul.cx>
Tue, 16 Apr 2019 15:42:28 +0000
changeset 469691 cd74a5191ca1
parent 469690 ffe609cec2fe
child 469692 45f499ef4b62
push id35879
push usernerli@mozilla.com
push dateTue, 16 Apr 2019 22:01:48 +0000
treeherdermozilla-central@12a60898fdc1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1531833
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1531833 - Add a Java method that enables and disable all that is needed to do audio communication with an earpiece. r=snorp Differential Revision: https://phabricator.services.mozilla.com/D21735
mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoAppShell.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoAppShell.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoAppShell.java
@@ -1991,16 +1991,51 @@ public class GeckoAppShell {
         }
         final String prop = am.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
         if (prop == null) {
             return DEFAULT;
         }
         return Integer.parseInt(prop);
     }
 
+    static private int sPreviousAudioMode = -2;
+
+    @WrapForJNI(calledFrom = "any")
+    public static void setCommunicationAudioModeOn(final boolean on) {
+        final AudioManager am = (AudioManager)getApplicationContext()
+                                .getSystemService(Context.AUDIO_SERVICE);
+        if (am == null) {
+            return;
+        }
+
+        if (sPreviousAudioMode == AudioManager.MODE_INVALID) {
+            sPreviousAudioMode = am.getMode();
+        }
+
+        try {
+            if (on)  {
+                Log.e(LOGTAG, "Setting communication mode ON");
+                sPreviousAudioMode = am.getMode();
+                am.startBluetoothSco();
+                am.setBluetoothScoOn(true);
+                am.setMode(AudioManager.MODE_IN_COMMUNICATION);
+            } else {
+                Log.e(LOGTAG, "Setting communication mode OFF");
+                am.setMode(sPreviousAudioMode);
+                sPreviousAudioMode = AudioManager.MODE_INVALID;
+                am.stopBluetoothSco();
+                am.setBluetoothScoOn(false);
+            }
+        } catch (SecurityException e) {
+            Log.e(LOGTAG, "could not set communication mode", e);
+        }
+
+        am.setSpeakerphoneOn(!on);
+    }
+
     private static String getLanguageTag(final Locale locale) {
         final StringBuilder out = new StringBuilder(locale.getLanguage());
         final String country = locale.getCountry();
         final String variant = locale.getVariant();
         if (!TextUtils.isEmpty(country)) {
             out.append('-').append(country);
         }
         if (!TextUtils.isEmpty(variant)) {