Merge "Gracefully handle module death in SoundTriggerMiddlewareValidation" into rvc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
db11409edc
@@ -108,6 +108,12 @@ import java.util.Set;
|
|||||||
public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddlewareService, Dumpable {
|
public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddlewareService, Dumpable {
|
||||||
private static final String TAG = "SoundTriggerMiddlewareValidation";
|
private static final String TAG = "SoundTriggerMiddlewareValidation";
|
||||||
|
|
||||||
|
private enum ModuleState {
|
||||||
|
ALIVE,
|
||||||
|
DETACHED,
|
||||||
|
DEAD
|
||||||
|
};
|
||||||
|
|
||||||
private final @NonNull ISoundTriggerMiddlewareService mDelegate;
|
private final @NonNull ISoundTriggerMiddlewareService mDelegate;
|
||||||
private final @NonNull Context mContext;
|
private final @NonNull Context mContext;
|
||||||
private Map<Integer, Set<ModuleService>> mModules;
|
private Map<Integer, Set<ModuleService>> mModules;
|
||||||
@@ -381,6 +387,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
|
|||||||
private ISoundTriggerModule mDelegate;
|
private ISoundTriggerModule mDelegate;
|
||||||
private @NonNull Map<Integer, ModelState> mLoadedModels = new HashMap<>();
|
private @NonNull Map<Integer, ModelState> mLoadedModels = new HashMap<>();
|
||||||
private final int mHandle;
|
private final int mHandle;
|
||||||
|
private ModuleState mState = ModuleState.ALIVE;
|
||||||
|
|
||||||
ModuleService(int handle, @NonNull ISoundTriggerCallback callback) {
|
ModuleService(int handle, @NonNull ISoundTriggerCallback callback) {
|
||||||
mCallback = callback;
|
mCallback = callback;
|
||||||
@@ -406,7 +413,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
|
|||||||
|
|
||||||
synchronized (SoundTriggerMiddlewareValidation.this) {
|
synchronized (SoundTriggerMiddlewareValidation.this) {
|
||||||
// State validation.
|
// State validation.
|
||||||
if (mDelegate == null) {
|
if (mState == ModuleState.DETACHED) {
|
||||||
throw new IllegalStateException("Module has been detached.");
|
throw new IllegalStateException("Module has been detached.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -430,7 +437,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
|
|||||||
|
|
||||||
synchronized (SoundTriggerMiddlewareValidation.this) {
|
synchronized (SoundTriggerMiddlewareValidation.this) {
|
||||||
// State validation.
|
// State validation.
|
||||||
if (mDelegate == null) {
|
if (mState == ModuleState.DETACHED) {
|
||||||
throw new IllegalStateException("Module has been detached.");
|
throw new IllegalStateException("Module has been detached.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -453,7 +460,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
|
|||||||
|
|
||||||
synchronized (SoundTriggerMiddlewareValidation.this) {
|
synchronized (SoundTriggerMiddlewareValidation.this) {
|
||||||
// State validation.
|
// State validation.
|
||||||
if (mDelegate == null) {
|
if (mState == ModuleState.DETACHED) {
|
||||||
throw new IllegalStateException("Module has been detached.");
|
throw new IllegalStateException("Module has been detached.");
|
||||||
}
|
}
|
||||||
ModelState modelState = mLoadedModels.get(
|
ModelState modelState = mLoadedModels.get(
|
||||||
@@ -486,7 +493,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
|
|||||||
|
|
||||||
synchronized (SoundTriggerMiddlewareValidation.this) {
|
synchronized (SoundTriggerMiddlewareValidation.this) {
|
||||||
// State validation.
|
// State validation.
|
||||||
if (mDelegate == null) {
|
if (mState == ModuleState.DETACHED) {
|
||||||
throw new IllegalStateException("Module has been detached.");
|
throw new IllegalStateException("Module has been detached.");
|
||||||
}
|
}
|
||||||
ModelState modelState = mLoadedModels.get(
|
ModelState modelState = mLoadedModels.get(
|
||||||
@@ -520,7 +527,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
|
|||||||
|
|
||||||
synchronized (SoundTriggerMiddlewareValidation.this) {
|
synchronized (SoundTriggerMiddlewareValidation.this) {
|
||||||
// State validation.
|
// State validation.
|
||||||
if (mDelegate == null) {
|
if (mState == ModuleState.DETACHED) {
|
||||||
throw new IllegalStateException("Module has been detached.");
|
throw new IllegalStateException("Module has been detached.");
|
||||||
}
|
}
|
||||||
ModelState modelState = mLoadedModels.get(
|
ModelState modelState = mLoadedModels.get(
|
||||||
@@ -549,7 +556,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
|
|||||||
|
|
||||||
synchronized (SoundTriggerMiddlewareValidation.this) {
|
synchronized (SoundTriggerMiddlewareValidation.this) {
|
||||||
// State validation.
|
// State validation.
|
||||||
if (mDelegate == null) {
|
if (mState == ModuleState.DETACHED) {
|
||||||
throw new IllegalStateException("Module has been detached.");
|
throw new IllegalStateException("Module has been detached.");
|
||||||
}
|
}
|
||||||
ModelState modelState = mLoadedModels.get(
|
ModelState modelState = mLoadedModels.get(
|
||||||
@@ -577,7 +584,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
|
|||||||
|
|
||||||
synchronized (SoundTriggerMiddlewareValidation.this) {
|
synchronized (SoundTriggerMiddlewareValidation.this) {
|
||||||
// State validation.
|
// State validation.
|
||||||
if (mDelegate == null) {
|
if (mState == ModuleState.DETACHED) {
|
||||||
throw new IllegalStateException("Module has been detached.");
|
throw new IllegalStateException("Module has been detached.");
|
||||||
}
|
}
|
||||||
ModelState modelState = mLoadedModels.get(
|
ModelState modelState = mLoadedModels.get(
|
||||||
@@ -605,7 +612,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
|
|||||||
|
|
||||||
synchronized (SoundTriggerMiddlewareValidation.this) {
|
synchronized (SoundTriggerMiddlewareValidation.this) {
|
||||||
// State validation.
|
// State validation.
|
||||||
if (mDelegate == null) {
|
if (mState == ModuleState.DETACHED) {
|
||||||
throw new IllegalStateException("Module has been detached.");
|
throw new IllegalStateException("Module has been detached.");
|
||||||
}
|
}
|
||||||
ModelState modelState = mLoadedModels.get(
|
ModelState modelState = mLoadedModels.get(
|
||||||
@@ -634,7 +641,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
|
|||||||
|
|
||||||
synchronized (SoundTriggerMiddlewareValidation.this) {
|
synchronized (SoundTriggerMiddlewareValidation.this) {
|
||||||
// State validation.
|
// State validation.
|
||||||
if (mDelegate == null) {
|
if (mState == ModuleState.DETACHED) {
|
||||||
throw new IllegalStateException("Module has been detached.");
|
throw new IllegalStateException("Module has been detached.");
|
||||||
}
|
}
|
||||||
ModelState modelState = mLoadedModels.get(
|
ModelState modelState = mLoadedModels.get(
|
||||||
@@ -663,10 +670,10 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
|
|||||||
|
|
||||||
synchronized (SoundTriggerMiddlewareValidation.this) {
|
synchronized (SoundTriggerMiddlewareValidation.this) {
|
||||||
// State validation.
|
// State validation.
|
||||||
if (mDelegate == null) {
|
if (mState == ModuleState.DETACHED) {
|
||||||
throw new IllegalStateException("Module has already been detached.");
|
throw new IllegalStateException("Module has already been detached.");
|
||||||
}
|
}
|
||||||
if (!mLoadedModels.isEmpty()) {
|
if (mState == ModuleState.ALIVE && !mLoadedModels.isEmpty()) {
|
||||||
throw new IllegalStateException("Cannot detach while models are loaded.");
|
throw new IllegalStateException("Cannot detach while models are loaded.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -682,13 +689,13 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
|
|||||||
// Override toString() in order to have the delegate's ID in it.
|
// Override toString() in order to have the delegate's ID in it.
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return mDelegate.toString();
|
return Objects.toString(mDelegate.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void detachInternal() {
|
private void detachInternal() {
|
||||||
try {
|
try {
|
||||||
mDelegate.detach();
|
mDelegate.detach();
|
||||||
mDelegate = null;
|
mState = ModuleState.DETACHED;
|
||||||
mCallback.asBinder().unlinkToDeath(this, 0);
|
mCallback.asBinder().unlinkToDeath(this, 0);
|
||||||
mModules.get(mHandle).remove(this);
|
mModules.get(mHandle).remove(this);
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
@@ -697,6 +704,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
|
|||||||
}
|
}
|
||||||
|
|
||||||
void dump(PrintWriter pw) {
|
void dump(PrintWriter pw) {
|
||||||
|
if (mState == ModuleState.ALIVE) {
|
||||||
pw.printf("Loaded models for session %s (handle, active)", toString());
|
pw.printf("Loaded models for session %s (handle, active)", toString());
|
||||||
pw.println();
|
pw.println();
|
||||||
pw.println("-------------------------------");
|
pw.println("-------------------------------");
|
||||||
@@ -706,6 +714,10 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
|
|||||||
pw.print(entry.getValue().activityState.name());
|
pw.print(entry.getValue().activityState.name());
|
||||||
pw.println();
|
pw.println();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
pw.printf("Session %s is dead", toString());
|
||||||
|
pw.println();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -762,6 +774,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
|
|||||||
public void onModuleDied() {
|
public void onModuleDied() {
|
||||||
synchronized (SoundTriggerMiddlewareValidation.this) {
|
synchronized (SoundTriggerMiddlewareValidation.this) {
|
||||||
try {
|
try {
|
||||||
|
mState = ModuleState.DEAD;
|
||||||
mCallback.onModuleDied();
|
mCallback.onModuleDied();
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
// Dead client will be handled by binderDied() - no need to handle here.
|
// Dead client will be handled by binderDied() - no need to handle here.
|
||||||
|
|||||||
Reference in New Issue
Block a user