diff options
author | Sungsoo Lim <sungsoo@google.com> | 2020-12-24 01:36:18 +0000 |
---|---|---|
committer | Sungsoo Lim <sungsoo@google.com> | 2021-01-11 10:04:08 +0900 |
commit | 96d134d75aa32f2345c478398de3d61dd90abacf (patch) | |
tree | 793a1cb08f82eeb86a6f489c6a8371f4da19b06e | |
parent | 928789a180ccac53d933b2aa5bb3246212095737 (diff) |
Cache stream volumes to prevent ANR
MediaRourter uses main thread for internal operations, and ANR could
happens if AudioService is not running when MediaRouter tried to get
stream volumes. This CL avoids such ANR by caching stream volumes.
Bug: 170327593
Test: manually
(cherry picked from internal master branch)
Change-Id: I7c00b26a3a25f17c877d3ec0e998905ab0b4af28
-rw-r--r-- | media/java/android/media/MediaRouter.java | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/media/java/android/media/MediaRouter.java b/media/java/android/media/MediaRouter.java index cf643dd113f5..b1139f8edfc4 100644 --- a/media/java/android/media/MediaRouter.java +++ b/media/java/android/media/MediaRouter.java @@ -43,6 +43,7 @@ import android.os.RemoteException; import android.os.ServiceManager; import android.os.UserHandle; import android.text.TextUtils; +import android.util.ArrayMap; import android.util.Log; import android.view.Display; @@ -51,6 +52,7 @@ import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.concurrent.CopyOnWriteArrayList; @@ -108,6 +110,8 @@ public class MediaRouter { IMediaRouterClient mClient; MediaRouterClientState mClientState; + Map<Integer, Integer> mStreamVolume = new ArrayMap<>(); + final IAudioRoutesObserver.Stub mAudioRoutesObserver = new IAudioRoutesObserver.Stub() { @Override public void dispatchAudioRoutesChanged(final AudioRoutesInfo newRoutes) { @@ -259,6 +263,17 @@ public class MediaRouter { mCurAudioRoutesInfo.bluetoothName = newRoutes.bluetoothName; } + int getStreamVolume(int streamType) { + if (!mStreamVolume.containsKey(streamType)) { + try { + mStreamVolume.put(streamType, mAudioService.getStreamVolume(streamType)); + } catch (RemoteException e) { + Log.e(TAG, "Error getting local stream volume", e); + } + } + return mStreamVolume.get(streamType); + } + boolean isBluetoothA2dpOn() { try { return mBluetoothA2dpRoute != null && mAudioService.isBluetoothA2dpOn(); @@ -1956,13 +1971,7 @@ public class MediaRouter { */ public int getVolume() { if (mPlaybackType == PLAYBACK_TYPE_LOCAL) { - int vol = 0; - try { - vol = sStatic.mAudioService.getStreamVolume(mPlaybackStream); - } catch (RemoteException e) { - Log.e(TAG, "Error getting local stream volume", e); - } - return vol; + return sStatic.getStreamVolume(mPlaybackStream); } else { return mVolume; } @@ -3077,11 +3086,12 @@ public class MediaRouter { if (intent.getAction().equals(AudioManager.VOLUME_CHANGED_ACTION)) { final int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1); + final int newVolume = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, 0); + sStatic.mStreamVolume.put(streamType, newVolume); if (streamType != AudioManager.STREAM_MUSIC) { return; } - final int newVolume = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, 0); final int oldVolume = intent.getIntExtra( AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, 0); if (newVolume != oldVolume) { |