MediaSessionManager: Prevent volume keys from being ignored
When the volume key is pressed and the foreground activity doesn't
handle the event, the foreground activity tells the MediaSessionService
for adjusting system volume with its package name returned by
Context#getPackageName().
However, if the foreground process runs on another application's process
by specifying android:process in the AndroidManifest.xml. (e.g.
ApnSettingsActivity in the Settings), its package name wouldn't match
with the UID returned by the Binder.getCallingUid(). Because of the
mismatch, the app ops check in the AudioService will fail and
SecurityException will be thrown.
This fixes the issue by two ways
1. Use Context#getOpPackageName() for sending package name.
2. Use system service's package name and UID when adjusting volume
with the hardware volume key presses.
Bug: 110525559
Test: Verified manually that the issue doesn't happen.
And run all media CTS
Change-Id: I8bba691a36cbe442212aab3f0b23c97533009052
diff --git a/services/core/java/com/android/server/media/MediaSessionService.java b/services/core/java/com/android/server/media/MediaSessionService.java
index 68b2a58..dc4405f 100644
--- a/services/core/java/com/android/server/media/MediaSessionService.java
+++ b/services/core/java/com/android/server/media/MediaSessionService.java
@@ -1844,7 +1844,7 @@
String packageName = getContext().getOpPackageName();
mAudioService.adjustSuggestedStreamVolume(direction, suggestedStream,
flags, packageName, TAG);
- } catch (RemoteException e) {
+ } catch (RemoteException|SecurityException e) {
Log.e(TAG, "Error adjusting default volume.", e);
} catch (IllegalArgumentException e) {
Log.e(TAG, "Cannot adjust volume: direction=" + direction