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

Commit 69ddbe8d authored by Oscar Azucena's avatar Oscar Azucena
Browse files

Added API to set assistants UID.

Added API to set the UIDs of the services that can be used as assistants.
Also modified code that uses the previous APIs for setting the unique
assistant UID and its corresponding hotword service. The new API can set
multiple UIDs all which will be treated the same for the purposes of
proviving assistant services.

Bug: 189312611
Test: m -j, and run assistant
Change-Id: Ieb28b7141206b58ae1d5d68580c08f4c733560b6
parent e6c0ffb7
Loading
Loading
Loading
Loading
+26 −27
Original line number Diff line number Diff line
@@ -2476,37 +2476,37 @@ static jint android_media_AudioSystem_getMinSampleRate(JNIEnv *env, jobject thiz
    return 4000; // SAMPLE_RATE_HZ_MIN  (for API)
}

static jint
android_media_AudioSystem_setAssistantUid(JNIEnv *env, jobject thiz, jint uid)
{
    status_t status = AudioSystem::setAssistantUid(uid);
    return (jint)nativeToJavaStatus(status);
static std::vector<uid_t> convertJIntArrayToUidVector(JNIEnv *env, jintArray jArray) {
    std::vector<uid_t> nativeVector;
    if (jArray != nullptr) {
        jsize len = env->GetArrayLength(jArray);

        if (len > 0) {
            int *nativeArray = nullptr;
            nativeArray = env->GetIntArrayElements(jArray, 0);
            if (nativeArray != nullptr) {
                for (size_t i = 0; i < len; i++) {
                    nativeVector.push_back(nativeArray[i]);
                }
                env->ReleaseIntArrayElements(jArray, nativeArray, 0);
            }
        }
    }
    return nativeVector;
}

static jint android_media_AudioSystem_setHotwordDetectionServiceUid(JNIEnv *env, jobject thiz,
                                                                    jint uid) {
    status_t status = AudioSystem::setHotwordDetectionServiceUid(uid);
static jint android_media_AudioSystem_setAssistantServicesUids(JNIEnv *env, jobject thiz,
                                                               jintArray uids) {
    std::vector<uid_t> nativeUidsVector = convertJIntArrayToUidVector(env, uids);

    status_t status = AudioSystem::setAssistantServicesUids(nativeUidsVector);
    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);
    std::vector<uid_t> nativeUidsVector = convertJIntArrayToUidVector(env, 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);
}
@@ -3000,9 +3000,8 @@ static const JNINativeMethod gMethods[] =
          (void *)android_media_AudioSystem_getReportedSurroundFormats},
         {"setSurroundFormatEnabled", "(IZ)I",
          (void *)android_media_AudioSystem_setSurroundFormatEnabled},
         {"setAssistantUid", "(I)I", (void *)android_media_AudioSystem_setAssistantUid},
         {"setHotwordDetectionServiceUid", "(I)I",
          (void *)android_media_AudioSystem_setHotwordDetectionServiceUid},
         {"setAssistantServicesUids", "([I)I",
          (void *)android_media_AudioSystem_setAssistantServicesUids},
         {"setA11yServicesUids", "([I)I", (void *)android_media_AudioSystem_setA11yServicesUids},
         {"isHapticPlaybackSupported", "()Z",
          (void *)android_media_AudioSystem_isHapticPlaybackSupported},
+11 −8
Original line number Diff line number Diff line
@@ -38,18 +38,21 @@ public abstract class AudioManagerInternal {

    public abstract void updateRingerModeAffectedStreamsInternal();

    public abstract void setAccessibilityServiceUids(IntArray uids);

    /**
     * Notify the UID of the currently active {@link android.service.voice.HotwordDetectionService}.
     *
     * <p>The caller is expected to take care of any performance implications, e.g. by using a
     * background thread to call this method.</p>
     * Add the UID for a new assistant service
     *
     * @param uid UID of the currently active service or {@link android.os.Process#INVALID_UID} if
     *            none.
     * @param uid UID of the newly available assistants
     */
    public abstract void setHotwordDetectionServiceUid(int uid);
    public abstract void addAssistantServiceUid(int uid);

    public abstract void setAccessibilityServiceUids(IntArray uids);
    /**
     * Remove the UID for an existing assistant service
     *
     * @param uid UID of the currently available assistant
     */
    public abstract void removeAssistantServiceUid(int uid);

    /**
     * Called by {@link com.android.server.inputmethod.InputMethodManagerService} to notify the UID
+2 −9
Original line number Diff line number Diff line
@@ -1856,16 +1856,9 @@ public class AudioSystem

    /**
     * @hide
     * Communicate UID of active assistant to audio policy service.
     * Communicate UIDs of assistant to audio policy service.
     */
    public static native int setAssistantUid(int uid);

    /**
     * Communicate UID of the current {@link android.service.voice.HotwordDetectionService} to audio
     * policy service.
     * @hide
     */
    public static native int setHotwordDetectionServiceUid(int uid);
    public static native int setAssistantServicesUids(int[] uids);

    /**
     * @hide
+103 −26
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import static android.media.AudioManager.RINGER_MODE_SILENT;
import static android.media.AudioManager.RINGER_MODE_VIBRATE;
import static android.media.AudioManager.STREAM_SYSTEM;
import static android.os.Process.FIRST_APPLICATION_UID;
import static android.os.Process.INVALID_UID;
import static android.provider.Settings.Secure.VOLUME_HUSH_MUTE;
import static android.provider.Settings.Secure.VOLUME_HUSH_OFF;
import static android.provider.Settings.Secure.VOLUME_HUSH_VIBRATE;
@@ -147,6 +148,7 @@ import android.service.notification.ZenModeConfig;
import android.telecom.TelecomManager;
import android.text.TextUtils;
import android.util.AndroidRuntimeException;
import android.util.ArraySet;
import android.util.IntArray;
import android.util.Log;
import android.util.MathUtils;
@@ -329,6 +331,8 @@ public class AudioService extends IAudioService.Stub
    private static final int MSG_ROUTING_UPDATED = 41;
    private static final int MSG_INIT_HEADTRACKING_SENSORS = 42;
    private static final int MSG_PERSIST_SPATIAL_AUDIO_ENABLED = 43;
    private static final int MSG_ADD_ASSISTANT_SERVICE_UID = 44;
    private static final int MSG_REMOVE_ASSISTANT_SERVICE_UID = 45;

    // start of messages handled under wakelock
    //   these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(),
@@ -756,10 +760,12 @@ public class AudioService extends IAudioService.Stub
    private VolumePolicy mVolumePolicy = VolumePolicy.DEFAULT;
    private long mLoweredFromNormalToVibrateTime;

    // Uid of the active hotword detection service to check if caller is the one or not.
    @GuardedBy("mHotwordDetectionServiceUidLock")
    private int mHotwordDetectionServiceUid = android.os.Process.INVALID_UID;
    private final Object mHotwordDetectionServiceUidLock = new Object();
    // Array of Uids of valid assistant services to check if caller is one of them
    @GuardedBy("mSettingsLock")
    private final ArraySet<Integer> mAssistantUids = new ArraySet<>();
    @GuardedBy("mSettingsLock")
    private int mPrimaryAssistantUid = INVALID_UID;


    // Array of Uids of valid accessibility services to check if caller is one of them
    private final Object mAccessibilityServiceUidsLock = new Object();
@@ -786,9 +792,6 @@ public class AudioService extends IAudioService.Stub
    private boolean mNavigationRepeatSoundEffectsEnabled;
    private boolean mHomeSoundEffectEnabled;

    @GuardedBy("mSettingsLock")
    private int mAssistantUid;

    @GuardedBy("mSettingsLock")
    private int mCurrentImeUid;

@@ -1395,12 +1398,10 @@ public class AudioService extends IAudioService.Stub
            mDeviceBroker.setForceUse_Async(AudioSystem.FOR_DOCK, forDock, "onAudioServerDied");
            sendEncodedSurroundMode(mContentResolver, "onAudioServerDied");
            sendEnabledSurroundFormats(mContentResolver, true);
            updateAssistantUId(true);
            AudioSystem.setRttEnabled(mRttEnabled);
            updateAssistantServicesUidsLocked();
        }
        synchronized (mHotwordDetectionServiceUidLock) {
            AudioSystem.setHotwordDetectionServiceUid(mHotwordDetectionServiceUid);
        }

        synchronized (mAccessibilityServiceUidsLock) {
            AudioSystem.setA11yServicesUids(mAccessibilityServiceUids);
        }
@@ -1478,6 +1479,60 @@ public class AudioService extends IAudioService.Stub
        updateVibratorInfos();
    }

    private void onRemoveAssistantServiceUids(int[] uids) {
        synchronized (mSettingsLock) {
            removeAssistantServiceUidsLocked(uids);
        }
    }

    @GuardedBy("mSettingsLock")
    private void removeAssistantServiceUidsLocked(int[] uids) {
        boolean changed = false;
        for (int index = 0; index < uids.length; index++) {
            if (!mAssistantUids.remove(uids[index])) {
                Slog.e(TAG, TextUtils.formatSimple(
                        "Cannot remove assistant service, uid(%d) not present", uids[index]));
                continue;
            }
            changed = true;
        }
        if (changed) {
            updateAssistantServicesUidsLocked();
        }
    }

    private void onAddAssistantServiceUids(int[] uids) {
        synchronized (mSettingsLock) {
            addAssistantServiceUidsLocked(uids);
        }
    }

    @GuardedBy("mSettingsLock")
    private void addAssistantServiceUidsLocked(int[] uids) {
        boolean changed = false;
        for (int index = 0; index < uids.length; index++) {
            if (uids[index] == INVALID_UID) {
                continue;
            }
            if (!mAssistantUids.add(uids[index])) {
                Slog.e(TAG, TextUtils.formatSimple(
                                "Cannot add assistant service, uid(%d) already present",
                                uids[index]));
                continue;
            }
            changed = true;
        }
        if (changed) {
            updateAssistantServicesUidsLocked();
        }
    }

    @GuardedBy("mSettingsLock")
    private void updateAssistantServicesUidsLocked() {
        int[] assistantUids = mAssistantUids.stream().mapToInt(Integer::intValue).toArray();
        AudioSystem.setAssistantServicesUids(assistantUids);
    }

    private void onReinitVolumes(@NonNull String caller) {
        final int numStreamTypes = AudioSystem.getNumStreamTypes();
        // keep track of any error during stream volume initialization
@@ -2243,8 +2298,7 @@ public class AudioService extends IAudioService.Stub

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

        int assistantUid = INVALID_UID;
        // Consider assistants in the following order of priority:
        // 1) apk in assistant role
        // 2) voice interaction service
@@ -2288,10 +2342,10 @@ public class AudioService extends IAudioService.Stub
                }
            }
        }

        if (assistantUid != mAssistantUid || forceUpdate) {
            AudioSystem.setAssistantUid(assistantUid);
            mAssistantUid = assistantUid;
        if ((mPrimaryAssistantUid != assistantUid) || forceUpdate) {
            mAssistantUids.remove(mPrimaryAssistantUid);
            mPrimaryAssistantUid = assistantUid;
            addAssistantServiceUidsLocked(new int[]{mPrimaryAssistantUid});
        }
    }

@@ -7843,6 +7897,14 @@ public class AudioService extends IAudioService.Stub
                case MSG_PERSIST_SPATIAL_AUDIO_ENABLED:
                    onPersistSpatialAudioEnabled(msg.arg1 == 1);
                    break;

                case MSG_ADD_ASSISTANT_SERVICE_UID:
                    onAddAssistantServiceUids(new int[]{msg.arg1});
                    break;

                case MSG_REMOVE_ASSISTANT_SERVICE_UID:
                    onRemoveAssistantServiceUids(new int[]{msg.arg1});
                    break;
            }
        }
    }
@@ -9351,7 +9413,7 @@ public class AudioService extends IAudioService.Stub
        pw.print("  mPendingVolumeCommand="); pw.println(mPendingVolumeCommand);
        pw.print("  mMusicActiveMs="); pw.println(mMusicActiveMs);
        pw.print("  mMcc="); pw.println(mMcc);
        pw.print("  mCameraSoundForced="); pw.println(mCameraSoundForced);
        pw.print("  mCameraSoundForced="); pw.println(isCameraSoundForced());
        pw.print("  mHasVibrator="); pw.println(mHasVibrator);
        pw.print("  mVolumePolicy="); pw.println(mVolumePolicy);
        pw.print("  mAvrcpAbsVolSupported="); pw.println(mAvrcpAbsVolSupported);
@@ -9371,9 +9433,9 @@ public class AudioService extends IAudioService.Stub
                        + " FromRestrictions=" + mMicMuteFromRestrictions
                        + " FromApi=" + mMicMuteFromApi
                        + " from system=" + mMicMuteFromSystemCached);
        pw.print("\n  mAssistantUid="); pw.println(mAssistantUid);
        pw.print("  mCurrentImeUid="); pw.println(mCurrentImeUid);
        dumpAccessibilityServiceUids(pw);
        dumpAssistantServicesUids(pw);

        dumpAudioPolicies(pw);
        mDynPolicyLogger.dump(pw);
@@ -9416,6 +9478,19 @@ public class AudioService extends IAudioService.Stub
        }
    }

    private void dumpAssistantServicesUids(PrintWriter pw) {
        synchronized (mSettingsLock) {
            if (mAssistantUids.size() > 0) {
                pw.println("  Assistant service UIDs:");
                for (int uid : mAssistantUids) {
                    pw.println("  - " + uid);
                }
            } else {
                pw.println("  No Assistant service Uids.");
            }
        }
    }

    private void dumpAccessibilityServiceUids(PrintWriter pw) {
        synchronized (mSupportedSystemUsagesLock) {
            if (mAccessibilityServiceUids != null && mAccessibilityServiceUids.length > 0) {
@@ -9714,13 +9789,15 @@ public class AudioService extends IAudioService.Stub
        }

        @Override
        public void setHotwordDetectionServiceUid(int uid) {
            synchronized (mHotwordDetectionServiceUidLock) {
                if (mHotwordDetectionServiceUid != uid) {
                    mHotwordDetectionServiceUid = uid;
                    AudioSystem.setHotwordDetectionServiceUid(mHotwordDetectionServiceUid);
                }
        public void addAssistantServiceUid(int uid) {
            sendMsg(mAudioHandler, MSG_ADD_ASSISTANT_SERVICE_UID, SENDMSG_QUEUE,
                    uid, 0, null, 0);
        }

        @Override
        public void removeAssistantServiceUid(int uid) {
            sendMsg(mAudioHandler, MSG_REMOVE_ASSISTANT_SERVICE_UID, SENDMSG_QUEUE,
                    uid, 0, null, 0);
        }

        @Override
+0 −8
Original line number Diff line number Diff line
@@ -386,14 +386,6 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback {
        return AudioSystem.muteMicrophone(on);
    }

    /**
     * Same as {@link AudioSystem#setHotwordDetectionServiceUid(int)}
     * Communicate UID of current HotwordDetectionService to audio policy service.
     */
    public int setHotwordDetectionServiceUid(int uid) {
        return AudioSystem.setHotwordDetectionServiceUid(uid);
    }

    /**
     * Same as {@link AudioSystem#setCurrentImeUid(int)}
     * Communicate UID of current InputMethodService to audio policy service.
Loading