Index: media/base/android/java/src/org/chromium/media/AudioManagerAndroid.java |
diff --git a/media/base/android/java/src/org/chromium/media/AudioManagerAndroid.java b/media/base/android/java/src/org/chromium/media/AudioManagerAndroid.java |
index db674f52d3b9f2a0a43978447f7078cdb5ef1671..93171a76975b7398ecef3e276e5b0eba9f98b5ca 100644 |
--- a/media/base/android/java/src/org/chromium/media/AudioManagerAndroid.java |
+++ b/media/base/android/java/src/org/chromium/media/AudioManagerAndroid.java |
@@ -7,16 +7,23 @@ package org.chromium.media; |
import android.bluetooth.BluetoothAdapter; |
import android.bluetooth.BluetoothManager; |
import android.content.BroadcastReceiver; |
+import android.content.ContentResolver; |
import android.content.Context; |
import android.content.Intent; |
import android.content.IntentFilter; |
import android.content.pm.PackageManager; |
+import android.database.ContentObserver; |
import android.media.AudioFormat; |
import android.media.AudioManager; |
import android.media.AudioRecord; |
import android.media.AudioTrack; |
+import android.net.Uri; |
import android.os.Build; |
+import android.os.Handler; |
+import android.os.Looper; |
import android.os.Process; |
+import android.provider.Settings; |
+import android.provider.Settings.System; |
import android.util.Log; |
import java.util.Arrays; |
@@ -106,6 +113,7 @@ class AudioManagerAndroid { |
private final AudioManager mAudioManager; |
private final Context mContext; |
+ private final long mNativeAudioManagerAndroid; |
private boolean mHasBluetoothPermission = false; |
private boolean mIsInitialized = false; |
@@ -117,18 +125,28 @@ class AudioManagerAndroid { |
// Contains a list of currently available audio devices. |
private boolean[] mAudioDevices = new boolean[DEVICE_COUNT]; |
+ private final ContentResolver mContentResolver; |
+ private SettingsObserver mSettingsObserver = null; |
+ private SettingsObserverThread mSettingsObserverThread = null; |
+ private int mCurrentVolume; |
+ private final Object mSettingsObserverLock = new Object(); |
+ |
// Broadcast receiver for wired headset intent broadcasts. |
private BroadcastReceiver mWiredHeadsetReceiver; |
/** Construction */ |
@CalledByNative |
- private static AudioManagerAndroid createAudioManagerAndroid(Context context) { |
- return new AudioManagerAndroid(context); |
+ private static AudioManagerAndroid createAudioManagerAndroid( |
+ Context context, |
+ long nativeAudioManagerAndroid) { |
+ return new AudioManagerAndroid(context, nativeAudioManagerAndroid); |
} |
- private AudioManagerAndroid(Context context) { |
+ private AudioManagerAndroid(Context context, long nativeAudioManagerAndroid) { |
mContext = context; |
+ mNativeAudioManagerAndroid = nativeAudioManagerAndroid; |
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE); |
+ mContentResolver = mContext.getContentResolver(); |
} |
/** |
@@ -174,6 +192,16 @@ class AudioManagerAndroid { |
initBluetooth(); |
mIsInitialized = true; |
+ |
+ mSettingsObserverThread = new SettingsObserverThread(); |
+ mSettingsObserverThread.start(); |
+ synchronized(mSettingsObserverLock) { |
+ try { |
+ mSettingsObserverLock.wait(); |
+ } catch (InterruptedException e) { |
+ Log.e(TAG, "unregisterHeadsetReceiver exception: " + e.getMessage()); |
+ } |
+ } |
} |
/** |
@@ -185,6 +213,14 @@ class AudioManagerAndroid { |
if (!mIsInitialized) |
return; |
+ if (mSettingsObserverThread != null ) { |
+ mSettingsObserverThread = null; |
+ } |
+ if (mSettingsObserver != null) { |
+ mContentResolver.unregisterContentObserver(mSettingsObserver); |
+ mSettingsObserver = null; |
+ } |
+ |
unregisterForWiredHeadsetIntentBroadcast(); |
// Restore previously stored audio states. |
@@ -553,4 +589,40 @@ class AudioManagerAndroid { |
private void loge(String msg) { |
Log.e(TAG, msg); |
} |
+ |
+ private class SettingsObserver extends ContentObserver { |
+ SettingsObserver() { |
+ super(new Handler()); |
+ mContentResolver.registerContentObserver(Settings.System.CONTENT_URI, true, this); |
+ } |
+ |
+ @Override |
+ public void onChange(boolean selfChange) { |
+ super.onChange(selfChange); |
+ int volume = mAudioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL); |
+ nativeSetMute(mNativeAudioManagerAndroid, (volume == 0)); |
+ } |
+ } |
+ |
+ private native void nativeSetMute(long nativeAudioManagerAndroid, boolean muted); |
+ |
+ private class SettingsObserverThread extends Thread { |
+ SettingsObserverThread() { |
+ super("SettinsObserver"); |
+ } |
+ |
+ @Override |
+ public void run() { |
+ // Set this thread up so the handler will work on it. |
+ Looper.prepare(); |
+ |
+ synchronized(mSettingsObserverLock) { |
+ mSettingsObserver = new SettingsObserver(); |
+ mSettingsObserverLock.notify(); |
+ } |
+ |
+ // Listen for volume change. |
+ Looper.loop(); |
+ } |
+ } |
} |