DO NOT MERGE Check caller for sending media key to global priority session

Prevent sending media key events from the non-system app to the global
priority session through the MediaSessionManager.dispatchMediaKeyEvent().
Note that any app can use the API indirectly with
the public API AudioManager.dispatchMediaKeyEvent().

Bug: 29833954
Tested: Installed malicious apps and confirmed that they don't work.
Tested: Run CtsTelecomTestCases and CtsMediaTestCases
Change-Id: I2a9e78196ba7455324e485f098f095d03b47ee15
(cherry picked from commit 09c3df004bad9343092e377a7e74e11f9635734a)
diff --git a/services/core/java/com/android/server/media/ b/services/core/java/com/android/server/media/
index a4d2cd2..70e7b7e 100644
--- a/services/core/java/com/android/server/media/
+++ b/services/core/java/com/android/server/media/
@@ -47,6 +47,7 @@
 import android.os.IBinder;
 import android.os.Message;
 import android.os.PowerManager;
+import android.os.Process;
 import android.os.RemoteException;
 import android.os.ResultReceiver;
 import android.os.ServiceManager;
@@ -763,6 +764,13 @@
                             + "setup is in progress.");
+                if (isGlobalPriorityActive() && uid != Process.SYSTEM_UID) {
+                    // Prevent dispatching key event through reflection while the global priority
+                    // session is active.
+                    Slog.i(TAG, "Only the system can dispatch media key event "
+                            + "to the global priority session.");
+                    return;
+                }
                 synchronized (mLock) {
                     // If we don't have a media button receiver to fall back on