Merge "Allow DEFAULT_INPUT_METHOD to capture audio during RTT call" into rvc-dev
diff --git a/core/jni/android_media_AudioSystem.cpp b/core/jni/android_media_AudioSystem.cpp
index ab3cf30..c5a4588 100644
--- a/core/jni/android_media_AudioSystem.cpp
+++ b/core/jni/android_media_AudioSystem.cpp
@@ -2266,6 +2266,11 @@
     return (jint)nativeToJavaStatus(status);
 }
 
+static jint android_media_AudioSystem_setCurrentImeUid(JNIEnv *env, jobject thiz, jint uid) {
+    status_t status = AudioSystem::setCurrentImeUid(uid);
+    return (jint)nativeToJavaStatus(status);
+}
+
 static jboolean
 android_media_AudioSystem_isHapticPlaybackSupported(JNIEnv *env, jobject thiz)
 {
@@ -2527,7 +2532,8 @@
          {"setUserIdDeviceAffinities", "(I[I[Ljava/lang/String;)I",
           (void *)android_media_AudioSystem_setUserIdDeviceAffinities},
          {"removeUserIdDeviceAffinities", "(I)I",
-          (void *)android_media_AudioSystem_removeUserIdDeviceAffinities}};
+          (void *)android_media_AudioSystem_removeUserIdDeviceAffinities},
+         {"setCurrentImeUid", "(I)I", (void *)android_media_AudioSystem_setCurrentImeUid}};
 
 static const JNINativeMethod gEventHandlerMethods[] = {
     {"native_setup",
diff --git a/media/java/android/media/AudioSystem.java b/media/java/android/media/AudioSystem.java
index 0a56acc..155eb1c 100644
--- a/media/java/android/media/AudioSystem.java
+++ b/media/java/android/media/AudioSystem.java
@@ -1233,6 +1233,11 @@
      * Communicate UIDs of active accessibility services to audio policy service.
      */
     public static native int setA11yServicesUids(int[] uids);
+    /**
+     * Communicate UID of current InputMethodService to audio policy service.
+     */
+    public static native int setCurrentImeUid(int uid);
+
 
     /**
      * @see AudioManager#isHapticPlaybackSupported()
diff --git a/services/core/java/com/android/server/audio/AudioService.java b/services/core/java/com/android/server/audio/AudioService.java
index 8bce504..f6cdaeb 100644
--- a/services/core/java/com/android/server/audio/AudioService.java
+++ b/services/core/java/com/android/server/audio/AudioService.java
@@ -52,6 +52,7 @@
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
+import android.content.pm.PackageManagerInternal;
 import android.content.pm.ResolveInfo;
 import android.content.pm.UserInfo;
 import android.content.res.Configuration;
@@ -576,6 +577,9 @@
     @GuardedBy("mSettingsLock")
     private int mAssistantUid;
 
+    @GuardedBy("mSettingsLock")
+    private int mCurrentImeUid;
+
     private final Object mSupportedSystemUsagesLock = new Object();
     @GuardedBy("mSupportedSystemUsagesLock")
     private @AttributeSystemUsage int[] mSupportedSystemUsages =
@@ -1056,6 +1060,7 @@
             sendEncodedSurroundMode(mContentResolver, "onAudioServerDied");
             sendEnabledSurroundFormats(mContentResolver, true);
             updateAssistantUId(true);
+            updateCurrentImeUid(true);
             AudioSystem.setRttEnabled(mRttEnabled);
         }
         synchronized (mAccessibilityServiceUidsLock) {
@@ -1601,6 +1606,37 @@
         }
     }
 
+    @GuardedBy("mSettingsLock")
+    private void updateCurrentImeUid(boolean forceUpdate) {
+        String imeId = Settings.Secure.getStringForUser(
+                mContentResolver,
+                Settings.Secure.DEFAULT_INPUT_METHOD, UserHandle.USER_CURRENT);
+        if (TextUtils.isEmpty(imeId)) {
+            Log.e(TAG, "updateCurrentImeUid() could not find current IME");
+            return;
+        }
+        ComponentName componentName = ComponentName.unflattenFromString(imeId);
+        if (componentName == null) {
+            Log.e(TAG, "updateCurrentImeUid() got invalid service name for "
+                    + Settings.Secure.DEFAULT_INPUT_METHOD + ": " + imeId);
+            return;
+        }
+        String packageName = componentName.getPackageName();
+        int currentUserId = LocalServices.getService(ActivityManagerInternal.class)
+                .getCurrentUserId();
+        int currentImeUid = LocalServices.getService(PackageManagerInternal.class)
+                .getPackageUidInternal(packageName, 0 /* flags */, currentUserId);
+        if (currentImeUid < 0) {
+            Log.e(TAG, "updateCurrentImeUid() could not find UID for package: " + packageName);
+            return;
+        }
+
+        if (currentImeUid != mCurrentImeUid || forceUpdate) {
+            AudioSystem.setCurrentImeUid(currentImeUid);
+            mCurrentImeUid = currentImeUid;
+        }
+    }
+
     private void readPersistedSettings() {
         final ContentResolver cr = mContentResolver;
 
@@ -1645,6 +1681,7 @@
             sendEncodedSurroundMode(cr, "readPersistedSettings");
             sendEnabledSurroundFormats(cr, true);
             updateAssistantUId(true);
+            updateCurrentImeUid(true);
             AudioSystem.setRttEnabled(mRttEnabled);
         }
 
@@ -5843,6 +5880,8 @@
 
             mContentResolver.registerContentObserver(Settings.Secure.getUriFor(
                     Settings.Secure.VOICE_INTERACTION_SERVICE), false, this);
+            mContentResolver.registerContentObserver(Settings.Secure.getUriFor(
+                    Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
         }
 
         @Override
@@ -5866,6 +5905,7 @@
                 updateEncodedSurroundOutput();
                 sendEnabledSurroundFormats(mContentResolver, mSurroundModeChanged);
                 updateAssistantUId(false);
+                updateCurrentImeUid(false);
             }
         }