Specify user when setting MUTE for a call.

Bug: 24448841
Change-Id: Ic71fe287efc37c5a7b560fafe8cb4c33273ad6b8
diff --git a/src/com/android/server/telecom/CallAudioManager.java b/src/com/android/server/telecom/CallAudioManager.java
index 938e874..23284e3 100644
--- a/src/com/android/server/telecom/CallAudioManager.java
+++ b/src/com/android/server/telecom/CallAudioManager.java
@@ -16,11 +16,19 @@
 
 package com.android.server.telecom;
 
+import android.app.ActivityManagerNative;
 import android.content.Context;
+import android.content.pm.UserInfo;
 import android.media.AudioManager;
+import android.media.IAudioService;
+import android.os.Binder;
 import android.os.Handler;
+import android.os.IBinder;
 import android.os.Looper;
 import android.os.Message;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+import android.os.UserHandle;
 import android.telecom.CallAudioState;
 
 import com.android.internal.util.IndentingPrintWriter;
@@ -86,9 +94,27 @@
                 case MSG_AUDIO_MANAGER_SET_MICROPHONE_MUTE: {
                     boolean mute = (msg.arg1 != 0);
                     if (mute != mAudioManager.isMicrophoneMute()) {
-                        Log.i(this, "changing microphone mute state to: %b", mute);
-                        mAudioManager.setMicrophoneMute(mute);
+                        IAudioService audio = getAudioService();
+                        Log.i(this, "changing microphone mute state to: %b [serviceIsNull=%b]",
+                                mute, audio == null);
+                        if (audio != null) {
+                            try {
+                                // We use the audio service directly here so that we can specify
+                                // the current user. Telecom runs in the system_server process which
+                                // may run as a separate user from the foreground user. If we
+                                // used AudioManager directly, we would change mute for the system's
+                                // user and not the current foreground, which we want to avoid.
+                                audio.setMicrophoneMute(
+                                        mute, mContext.getOpPackageName(), getCurrentUserId());
+
+                            } catch (RemoteException e) {
+                                Log.e(this, e, "Remote exception while toggling mute.");
+                            }
+                            // TODO: Check microphone state after attempting to set to ensure that
+                            // our state corroborates AudioManager's state.
+                        }
                     }
+
                     break;
                 }
                 case MSG_AUDIO_MANAGER_REQUEST_AUDIO_FOCUS_FOR_CALL: {
@@ -701,6 +727,23 @@
         return mAudioFocusStreamType != STREAM_NONE;
     }
 
+    private IAudioService getAudioService() {
+        return IAudioService.Stub.asInterface(ServiceManager.getService(Context.AUDIO_SERVICE));
+    }
+
+    private int getCurrentUserId() {
+        final long ident = Binder.clearCallingIdentity();
+        try {
+            UserInfo currentUser = ActivityManagerNative.getDefault().getCurrentUser();
+            return currentUser.id;
+        } catch (RemoteException e) {
+            // Activity manager not running, nothing we can do assume user 0.
+        } finally {
+            Binder.restoreCallingIdentity(ident);
+        }
+        return UserHandle.USER_OWNER;
+    }
+
     /**
      * Translates an {@link AudioManager} stream type to a human-readable string description.
      *