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

Commit c79176dc authored by Ytai Ben-Tsvi's avatar Ytai Ben-Tsvi
Browse files

Require identity information in SoundTrigger.java

Deprecate the way to attach to SoundTrigger that doesn't include
identity information. Plumb that up to the SoundTriggerService layer,
where it is temporarily provided in a backward-compatible way instead
of with the actual identity.

Change-Id: Icc2bf3b80300bd2b75c81d253986b4e1582737ca
Bug: 163865561
parent 1a619f03
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -2116,7 +2116,7 @@ public class SoundTrigger {
     * @hide
     */
    @UnsupportedAppUsage
    public static SoundTriggerModule attachModule(int moduleId,
    private static SoundTriggerModule attachModule(int moduleId,
            @NonNull StatusListener listener,
            @Nullable Handler handler) {
        // TODO(ytai): This is a temporary hack to retain prior behavior, which makes
+9 −2
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import java.util.function.BiFunction;

/**
 * Helper for {@link SoundTrigger} APIs. Supports two types of models:
@@ -116,6 +117,9 @@ public class SoundTriggerHelper implements SoundTrigger.StatusListener {

    private PowerSaveModeListener mPowerSaveModeListener;

    private final BiFunction<Integer, SoundTrigger.StatusListener, SoundTriggerModule>
            mModuleProvider;

    // Handler to process call state changes will delay to allow time for the audio
    // and sound trigger HALs to process the end of call notifications
    // before we re enable pending recognition requests.
@@ -123,8 +127,11 @@ public class SoundTriggerHelper implements SoundTrigger.StatusListener {
    private static final int MSG_CALL_STATE_CHANGED = 0;
    private static final int CALL_INACTIVE_MSG_DELAY_MS = 1000;

    SoundTriggerHelper(Context context) {
    SoundTriggerHelper(Context context,
            @NonNull BiFunction<Integer, SoundTrigger.StatusListener,
                    SoundTriggerModule> moduleProvider) {
        ArrayList <ModuleProperties> modules = new ArrayList<>();
        mModuleProvider = moduleProvider;
        int status = SoundTrigger.listModules(modules);
        mContext = context;
        mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
@@ -264,7 +271,7 @@ public class SoundTriggerHelper implements SoundTrigger.StatusListener {

    private int prepareForRecognition(ModelData modelData) {
        if (mModule == null) {
            mModule = SoundTrigger.attachModule(mModuleProperties.getId(), this, null);
            mModule = mModuleProvider.apply(mModuleProperties.getId(), this);
            if (mModule == null) {
                Slog.w(TAG, "prepareForRecognition: cannot attach to sound trigger module");
                return STATUS_ERROR;
+17 −1
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import static com.android.internal.util.function.pooled.PooledLambda.obtainMessa
import android.Manifest;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.ActivityThread;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
@@ -57,6 +58,7 @@ import android.media.MediaRecorder;
import android.media.soundtrigger.ISoundTriggerDetectionService;
import android.media.soundtrigger.ISoundTriggerDetectionServiceClient;
import android.media.soundtrigger.SoundTriggerDetectionService;
import android.media.soundtrigger_middleware.Identity;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
@@ -223,7 +225,21 @@ public class SoundTriggerService extends SystemService {

    private synchronized void initSoundTriggerHelper() {
        if (mSoundTriggerHelper == null) {
            mSoundTriggerHelper = new SoundTriggerHelper(mContext);
            Identity middlemanIdentity = new Identity();
            middlemanIdentity.packageName = ActivityThread.currentOpPackageName();

            mSoundTriggerHelper = new SoundTriggerHelper(mContext, (moduleId,
                    listener) -> {
                // TODO(ytai): This is a temporary hack to retain prior behavior, which
                //  makes
                //  assumptions about process affinity and Binder context.
                Identity originatorIdentity = new Identity();
                originatorIdentity.uid = Binder.getCallingUid();
                originatorIdentity.pid = Binder.getCallingUid();

                return SoundTrigger.attachModuleAsMiddleman(moduleId, listener, null,
                        middlemanIdentity, originatorIdentity);
            });
        }
    }