Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1c9c1d53 authored by Eric Laurent's avatar Eric Laurent
Browse files

AudioService: monitor assistant and accessibility services

Add monitoring of voice interaction service, assistant and
accessibility services changes and communicate corresponding UIDs
to audio policy service.
Those are needed to implement the concurrent audio capture policy.

Test: enable and disable Google Assistant and Voice Access.
Bug: 111438757

Change-Id: Ia20b54a70c0445113bd53b03b04a325dcf7940fb
parent de6fe070
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
@@ -2032,6 +2032,35 @@ static jint android_media_AudioSystem_get_FCC_8(JNIEnv *env, jobject thiz) {
    return FCC_8;
}

static jint
android_media_AudioSystem_setAssistantUid(JNIEnv *env, jobject thiz, jint uid)
{
    status_t status = AudioSystem::setAssistantUid(uid);
    return (jint)nativeToJavaStatus(status);
}

static jint
android_media_AudioSystem_setA11yServicesUids(JNIEnv *env, jobject thiz, jintArray uids) {
    std::vector<uid_t> nativeUidsVector;

    if (uids != nullptr) {
       jsize len = env->GetArrayLength(uids);

       if (len > 0) {
           int *nativeUids = nullptr;
           nativeUids = env->GetIntArrayElements(uids, 0);
           if (nativeUids != nullptr) {
               for (size_t i = 0; i < len; i++) {
                   nativeUidsVector.push_back(nativeUids[i]);
               }
               env->ReleaseIntArrayElements(uids, nativeUids, 0);
           }
       }
    }
    status_t status = AudioSystem::setA11yServicesUids(nativeUidsVector);
    return (jint)nativeToJavaStatus(status);
}

// ----------------------------------------------------------------------------

static const JNINativeMethod gMethods[] = {
@@ -2092,6 +2121,8 @@ static const JNINativeMethod gMethods[] = {
    {"getMicrophones", "(Ljava/util/ArrayList;)I", (void *)android_media_AudioSystem_getMicrophones},
    {"getSurroundFormats", "(Ljava/util/Map;Z)I", (void *)android_media_AudioSystem_getSurroundFormats},
    {"setSurroundFormatEnabled", "(IZ)I", (void *)android_media_AudioSystem_setSurroundFormatEnabled},
    {"setAssistantUid", "(I)I", (void *)android_media_AudioSystem_setAssistantUid},
    {"setA11yServicesUids", "([I)I", (void *)android_media_AudioSystem_setA11yServicesUids},
};

static const JNINativeMethod gEventHandlerMethods[] = {
+9 −0
Original line number Diff line number Diff line
@@ -924,6 +924,15 @@ public class AudioSystem

    public static native int setSurroundFormatEnabled(int audioFormat, boolean enabled);

    /**
     * Communicate UID of active assistant to audio policy service.
     */
    public static native int setAssistantUid(int uid);
    /**
     * Communicate UIDs of active accessibility services to audio policy service.
     */
    public static native int setA11yServicesUids(int[] uids);

    // Items shared with audio service

    /**
+46 −0
Original line number Diff line number Diff line
@@ -649,6 +649,9 @@ public class AudioService extends IAudioService.Stub
    private String mEnabledSurroundFormats;
    private boolean mSurroundModeChanged;

    @GuardedBy("mSettingsLock")
    private int mAssistantUid;

    // Intent "extra" data keys.
    public static final String CONNECT_INTENT_KEY_PORT_NAME = "portName";
    public static final String CONNECT_INTENT_KEY_STATE = "state";
@@ -1079,6 +1082,10 @@ public class AudioService extends IAudioService.Stub
            AudioSystem.setForceUse(AudioSystem.FOR_DOCK, forDock);
            sendEncodedSurroundMode(mContentResolver, "onAudioServerDied");
            sendEnabledSurroundFormats(mContentResolver, true);
            updateAssistantUId(true);
        }
        synchronized (mAccessibilityServiceUidsLock) {
            AudioSystem.setA11yServicesUids(mAccessibilityServiceUids);
        }
        synchronized (mHdmiClientLock) {
            if (mHdmiManager != null && mHdmiTvClient != null) {
@@ -1404,6 +1411,39 @@ public class AudioService extends IAudioService.Stub
        }
    }

    @GuardedBy("mSettingsLock")
    private void updateAssistantUId(boolean forceUpdate) {
        int assistantUid = 0;

        // Consider assistants in the following order of priority:
        // 1) voice interaction service
        // 2) assistant
        String assistantName = Settings.Secure.getStringForUser(
                        mContentResolver,
                        Settings.Secure.VOICE_INTERACTION_SERVICE, UserHandle.USER_CURRENT);
        if (TextUtils.isEmpty(assistantName)) {
            assistantName = Settings.Secure.getStringForUser(
                    mContentResolver,
                    Settings.Secure.ASSISTANT, UserHandle.USER_CURRENT);
        }
        if (!TextUtils.isEmpty(assistantName)) {
            String packageName = ComponentName.unflattenFromString(assistantName).getPackageName();
            if (!TextUtils.isEmpty(packageName)) {
                try {
                    assistantUid = mContext.getPackageManager().getPackageUid(packageName, 0);
                } catch (PackageManager.NameNotFoundException e) {
                    Log.e(TAG,
                            "updateAssistantUId() could not find UID for package: " + packageName);
                }
            }
        }

        if (assistantUid != mAssistantUid || forceUpdate) {
            AudioSystem.setAssistantUid(assistantUid);
            mAssistantUid = assistantUid;
        }
    }

    private void readPersistedSettings() {
        final ContentResolver cr = mContentResolver;

@@ -1447,6 +1487,7 @@ public class AudioService extends IAudioService.Stub
            readDockAudioSettings(cr);
            sendEncodedSurroundMode(cr, "readPersistedSettings");
            sendEnabledSurroundFormats(cr, true);
            updateAssistantUId(true);
        }

        mMuteAffectedStreams = System.getIntForUser(cr,
@@ -5811,6 +5852,9 @@ public class AudioService extends IAudioService.Stub
                    mContentResolver, Settings.Global.ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS);
            mContentResolver.registerContentObserver(Settings.Global.getUriFor(
                    Settings.Global.ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS), false, this);

            mContentResolver.registerContentObserver(Settings.Secure.getUriFor(
                    Settings.Secure.VOICE_INTERACTION_SERVICE), false, this);
        }

        @Override
@@ -5832,6 +5876,7 @@ public class AudioService extends IAudioService.Stub
                updateMasterMono(mContentResolver);
                updateEncodedSurroundOutput();
                sendEnabledSurroundFormats(mContentResolver, mSurroundModeChanged);
                updateAssistantUId(false);
            }
        }

@@ -7658,6 +7703,7 @@ public class AudioService extends IAudioService.Stub
                        mAccessibilityServiceUids = uids.toArray();
                    }
                }
                AudioSystem.setA11yServicesUids(mAccessibilityServiceUids);
            }
        }
    }