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

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

Improve dumpsys printing of modules

This includes the module properties in the listing.

Bug: 154020971
Test: Manual verification of logging.
Change-Id: I13ed6e225714fe49f0b6e09c9c676594a886e6e3
parent c3cf2a85
Loading
Loading
Loading
Loading
+37 −26
Original line number Diff line number Diff line
@@ -32,9 +32,9 @@ import android.media.soundtrigger_middleware.RecognitionEvent;
import android.media.soundtrigger_middleware.RecognitionStatus;
import android.media.soundtrigger_middleware.SoundModel;
import android.media.soundtrigger_middleware.SoundTriggerModuleDescriptor;
import android.media.soundtrigger_middleware.SoundTriggerModuleProperties;
import android.media.soundtrigger_middleware.Status;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.os.ServiceSpecificException;
import android.util.Log;
@@ -108,17 +108,26 @@ import java.util.Set;
public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddlewareInternal, Dumpable {
    private static final String TAG = "SoundTriggerMiddlewareValidation";

    private enum ModuleState {
    private enum ModuleStatus {
        ALIVE,
        DETACHED,
        DEAD
    };

    private class ModuleState {
        final @NonNull SoundTriggerModuleProperties properties;
        Set<ModuleService> sessions = new HashSet<>();

        private ModuleState(@NonNull SoundTriggerModuleProperties properties) {
            this.properties = properties;
        }
    }

    private Boolean mCaptureState;

    private final @NonNull ISoundTriggerMiddlewareInternal mDelegate;
    private final @NonNull Context mContext;
    private Map<Integer, Set<ModuleService>> mModules;
    private Map<Integer, ModuleState> mModules;

    public SoundTriggerMiddlewareValidation(
            @NonNull ISoundTriggerMiddlewareInternal delegate, @NonNull Context context) {
@@ -168,7 +177,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
                SoundTriggerModuleDescriptor[] result = mDelegate.listModules();
                mModules = new HashMap<>(result.length);
                for (SoundTriggerModuleDescriptor desc : result) {
                    mModules.put(desc.handle, new HashSet<>());
                    mModules.put(desc.handle, new ModuleState(desc.properties));
                }
                return result;
            } catch (Exception e) {
@@ -278,18 +287,21 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
    @Override
    public void dump(PrintWriter pw) {
        synchronized (this) {
            pw.printf("Capture state is %s\n", mCaptureState == null ? "uninitialized"
            pw.printf("Capture state is %s\n\n", mCaptureState == null ? "uninitialized"
                    : (mCaptureState ? "active" : "inactive"));
            if (mModules != null) {
                for (int handle : mModules.keySet()) {
                    final ModuleState module = mModules.get(handle);
                    pw.println("=========================================");
                    pw.printf("Active sessions for module %d", handle);
                    pw.println();
                    pw.printf("Module %d\n%s\n", handle,
                            ObjectPrinter.print(module.properties, true, 16));
                    pw.println("=========================================");
                    for (ModuleService session : mModules.get(handle)) {
                    for (ModuleService session : module.sessions) {
                        session.dump(pw);
                    }
                }
            } else {
                pw.println("Modules have not yet been enumerated.");
            }
        }
        pw.println();
@@ -297,7 +309,6 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
        if (mDelegate instanceof Dumpable) {
            ((Dumpable) mDelegate).dump(pw);
        }

    }

    /** State of a sound model. */
@@ -375,7 +386,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
        private ISoundTriggerModule mDelegate;
        private @NonNull Map<Integer, ModelState> mLoadedModels = new HashMap<>();
        private final int mHandle;
        private ModuleState mState = ModuleState.ALIVE;
        private ModuleStatus mState = ModuleStatus.ALIVE;

        ModuleService(int handle, @NonNull ISoundTriggerCallback callback) {
            mCallback = callback;
@@ -389,7 +400,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware

        void attach(@NonNull ISoundTriggerModule delegate) {
            mDelegate = delegate;
            mModules.get(mHandle).add(this);
            mModules.get(mHandle).sessions.add(this);
        }

        @Override
@@ -401,7 +412,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware

            synchronized (SoundTriggerMiddlewareValidation.this) {
                // State validation.
                if (mState == ModuleState.DETACHED) {
                if (mState == ModuleStatus.DETACHED) {
                    throw new IllegalStateException("Module has been detached.");
                }

@@ -425,7 +436,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware

            synchronized (SoundTriggerMiddlewareValidation.this) {
                // State validation.
                if (mState == ModuleState.DETACHED) {
                if (mState == ModuleStatus.DETACHED) {
                    throw new IllegalStateException("Module has been detached.");
                }

@@ -448,7 +459,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware

            synchronized (SoundTriggerMiddlewareValidation.this) {
                // State validation.
                if (mState == ModuleState.DETACHED) {
                if (mState == ModuleStatus.DETACHED) {
                    throw new IllegalStateException("Module has been detached.");
                }
                ModelState modelState = mLoadedModels.get(
@@ -481,7 +492,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware

            synchronized (SoundTriggerMiddlewareValidation.this) {
                // State validation.
                if (mState == ModuleState.DETACHED) {
                if (mState == ModuleStatus.DETACHED) {
                    throw new IllegalStateException("Module has been detached.");
                }
                ModelState modelState = mLoadedModels.get(
@@ -515,7 +526,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware

            synchronized (SoundTriggerMiddlewareValidation.this) {
                // State validation.
                if (mState == ModuleState.DETACHED) {
                if (mState == ModuleStatus.DETACHED) {
                    throw new IllegalStateException("Module has been detached.");
                }
                ModelState modelState = mLoadedModels.get(
@@ -544,7 +555,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware

            synchronized (SoundTriggerMiddlewareValidation.this) {
                // State validation.
                if (mState == ModuleState.DETACHED) {
                if (mState == ModuleStatus.DETACHED) {
                    throw new IllegalStateException("Module has been detached.");
                }
                ModelState modelState = mLoadedModels.get(
@@ -572,7 +583,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware

            synchronized (SoundTriggerMiddlewareValidation.this) {
                // State validation.
                if (mState == ModuleState.DETACHED) {
                if (mState == ModuleStatus.DETACHED) {
                    throw new IllegalStateException("Module has been detached.");
                }
                ModelState modelState = mLoadedModels.get(
@@ -600,7 +611,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware

            synchronized (SoundTriggerMiddlewareValidation.this) {
                // State validation.
                if (mState == ModuleState.DETACHED) {
                if (mState == ModuleStatus.DETACHED) {
                    throw new IllegalStateException("Module has been detached.");
                }
                ModelState modelState = mLoadedModels.get(
@@ -629,7 +640,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware

            synchronized (SoundTriggerMiddlewareValidation.this) {
                // State validation.
                if (mState == ModuleState.DETACHED) {
                if (mState == ModuleStatus.DETACHED) {
                    throw new IllegalStateException("Module has been detached.");
                }
                ModelState modelState = mLoadedModels.get(
@@ -658,10 +669,10 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware

            synchronized (SoundTriggerMiddlewareValidation.this) {
                // State validation.
                if (mState == ModuleState.DETACHED) {
                if (mState == ModuleStatus.DETACHED) {
                    throw new IllegalStateException("Module has already been detached.");
                }
                if (mState == ModuleState.ALIVE && !mLoadedModels.isEmpty()) {
                if (mState == ModuleStatus.ALIVE && !mLoadedModels.isEmpty()) {
                    throw new IllegalStateException("Cannot detach while models are loaded.");
                }

@@ -683,16 +694,16 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
        private void detachInternal() {
            try {
                mDelegate.detach();
                mState = ModuleState.DETACHED;
                mState = ModuleStatus.DETACHED;
                mCallback.asBinder().unlinkToDeath(this, 0);
                mModules.get(mHandle).remove(this);
                mModules.get(mHandle).sessions.remove(this);
            } catch (RemoteException e) {
                throw e.rethrowAsRuntimeException();
            }
        }

        void dump(PrintWriter pw) {
            if (mState == ModuleState.ALIVE) {
            if (mState == ModuleStatus.ALIVE) {
                pw.printf("Loaded models for session %s (handle, active)", toString());
                pw.println();
                pw.println("-------------------------------");
@@ -762,7 +773,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
        public void onModuleDied() {
            synchronized (SoundTriggerMiddlewareValidation.this) {
                try {
                    mState = ModuleState.DEAD;
                    mState = ModuleStatus.DEAD;
                    mCallback.onModuleDied();
                } catch (RemoteException e) {
                    // Dead client will be handled by binderDied() - no need to handle here.