Merge "Add null check in AppPredictionService callback wrapper." into qt-dev

This commit is contained in:
George Hodulik
2019-05-06 17:58:43 +00:00
committed by Android (Google) Code Review

View File

@@ -20,6 +20,7 @@ import static com.android.internal.util.function.pooled.PooledLambda.obtainMessa
import android.annotation.CallSuper;
import android.annotation.MainThread;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.app.Service;
@@ -100,7 +101,7 @@ public abstract class AppPredictionService extends Service {
mHandler.sendMessage(
obtainMessage(AppPredictionService::onSortAppTargets,
AppPredictionService.this, sessionId, targets.getList(), null,
new CallbackWrapper(callback)));
new CallbackWrapper(callback, null)));
}
@Override
@@ -196,7 +197,9 @@ public abstract class AppPredictionService extends Service {
final CallbackWrapper wrapper = findCallbackWrapper(callbacks, callback);
if (wrapper == null) {
callbacks.add(new CallbackWrapper(callback));
callbacks.add(new CallbackWrapper(callback,
callbackWrapper ->
mHandler.post(() -> removeCallbackWrapper(callbacks, callbackWrapper))));
if (callbacks.size() == 1) {
onStartPredictionUpdates();
}
@@ -219,10 +222,18 @@ public abstract class AppPredictionService extends Service {
final CallbackWrapper wrapper = findCallbackWrapper(callbacks, callback);
if (wrapper != null) {
callbacks.remove(wrapper);
if (callbacks.isEmpty()) {
onStopPredictionUpdates();
}
removeCallbackWrapper(callbacks, wrapper);
}
}
private void removeCallbackWrapper(
ArrayList<CallbackWrapper> callbacks, CallbackWrapper wrapper) {
if (callbacks == null) {
return;
}
callbacks.remove(wrapper);
if (callbacks.isEmpty()) {
onStopPredictionUpdates();
}
}
@@ -295,9 +306,12 @@ public abstract class AppPredictionService extends Service {
IBinder.DeathRecipient {
private IPredictionCallback mCallback;
private final Consumer<CallbackWrapper> mOnBinderDied;
CallbackWrapper(IPredictionCallback callback) {
CallbackWrapper(IPredictionCallback callback,
@Nullable Consumer<CallbackWrapper> onBinderDied) {
mCallback = callback;
mOnBinderDied = onBinderDied;
try {
mCallback.asBinder().linkToDeath(this, 0);
} catch (RemoteException e) {
@@ -306,6 +320,10 @@ public abstract class AppPredictionService extends Service {
}
public boolean isCallback(@NonNull IPredictionCallback callback) {
if (mCallback == null) {
Slog.e(TAG, "Callback is null, likely the binder has died.");
return false;
}
return mCallback.equals(callback);
}
@@ -323,6 +341,9 @@ public abstract class AppPredictionService extends Service {
@Override
public void binderDied() {
mCallback = null;
if (mOnBinderDied != null) {
mOnBinderDied.accept(this);
}
}
}
}