Merge "Fix com.android.server.wm.TaskFpsCallbackController#unregisterListener method NEVER works"

This commit is contained in:
Hai Zhang
2023-04-15 03:31:06 +00:00
committed by Gerrit Code Review

View File

@@ -26,8 +26,8 @@ import java.util.HashMap;
final class TaskFpsCallbackController { final class TaskFpsCallbackController {
private final Context mContext; private final Context mContext;
private final HashMap<ITaskFpsCallback, Long> mTaskFpsCallbacks; private final HashMap<IBinder, Long> mTaskFpsCallbacks;
private final HashMap<ITaskFpsCallback, IBinder.DeathRecipient> mDeathRecipients; private final HashMap<IBinder, IBinder.DeathRecipient> mDeathRecipients;
TaskFpsCallbackController(Context context) { TaskFpsCallbackController(Context context) {
mContext = context; mContext = context;
@@ -36,32 +36,42 @@ final class TaskFpsCallbackController {
} }
void registerListener(int taskId, ITaskFpsCallback callback) { void registerListener(int taskId, ITaskFpsCallback callback) {
if (mTaskFpsCallbacks.containsKey(callback)) { if (callback == null) {
return;
}
IBinder binder = callback.asBinder();
if (mTaskFpsCallbacks.containsKey(binder)) {
return; return;
} }
final long nativeListener = nativeRegister(callback, taskId); final long nativeListener = nativeRegister(callback, taskId);
mTaskFpsCallbacks.put(callback, nativeListener); mTaskFpsCallbacks.put(binder, nativeListener);
final IBinder.DeathRecipient deathRecipient = () -> unregisterListener(callback); final IBinder.DeathRecipient deathRecipient = () -> unregisterListener(callback);
try { try {
callback.asBinder().linkToDeath(deathRecipient, 0); binder.linkToDeath(deathRecipient, 0);
mDeathRecipients.put(callback, deathRecipient); mDeathRecipients.put(binder, deathRecipient);
} catch (RemoteException e) { } catch (RemoteException e) {
// ignore // ignore
} }
} }
void unregisterListener(ITaskFpsCallback callback) { void unregisterListener(ITaskFpsCallback callback) {
if (!mTaskFpsCallbacks.containsKey(callback)) { if (callback == null) {
return; return;
} }
callback.asBinder().unlinkToDeath(mDeathRecipients.get(callback), 0); IBinder binder = callback.asBinder();
mDeathRecipients.remove(callback); if (!mTaskFpsCallbacks.containsKey(binder)) {
return;
}
nativeUnregister(mTaskFpsCallbacks.get(callback)); binder.unlinkToDeath(mDeathRecipients.get(binder), 0);
mTaskFpsCallbacks.remove(callback); mDeathRecipients.remove(binder);
nativeUnregister(mTaskFpsCallbacks.get(binder));
mTaskFpsCallbacks.remove(binder);
} }
private static native long nativeRegister(ITaskFpsCallback callback, int taskId); private static native long nativeRegister(ITaskFpsCallback callback, int taskId);