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

Commit 940284bb authored by George Hodulik's avatar George Hodulik Committed by Android (Google) Code Review
Browse files

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

parents 2d9b3f3a b3aa6562
Loading
Loading
Loading
Loading
+28 −7
Original line number Original line Diff line number Diff line
@@ -20,6 +20,7 @@ import static com.android.internal.util.function.pooled.PooledLambda.obtainMessa
import android.annotation.CallSuper;
import android.annotation.CallSuper;
import android.annotation.MainThread;
import android.annotation.MainThread;
import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.annotation.TestApi;
import android.app.Service;
import android.app.Service;
@@ -100,7 +101,7 @@ public abstract class AppPredictionService extends Service {
            mHandler.sendMessage(
            mHandler.sendMessage(
                    obtainMessage(AppPredictionService::onSortAppTargets,
                    obtainMessage(AppPredictionService::onSortAppTargets,
                            AppPredictionService.this, sessionId, targets.getList(), null,
                            AppPredictionService.this, sessionId, targets.getList(), null,
                            new CallbackWrapper(callback)));
                            new CallbackWrapper(callback, null)));
        }
        }


        @Override
        @Override
@@ -196,7 +197,9 @@ public abstract class AppPredictionService extends Service {


        final CallbackWrapper wrapper = findCallbackWrapper(callbacks, callback);
        final CallbackWrapper wrapper = findCallbackWrapper(callbacks, callback);
        if (wrapper == null) {
        if (wrapper == null) {
            callbacks.add(new CallbackWrapper(callback));
            callbacks.add(new CallbackWrapper(callback,
                    callbackWrapper ->
                        mHandler.post(() -> removeCallbackWrapper(callbacks, callbackWrapper))));
            if (callbacks.size() == 1) {
            if (callbacks.size() == 1) {
                onStartPredictionUpdates();
                onStartPredictionUpdates();
            }
            }
@@ -219,12 +222,20 @@ public abstract class AppPredictionService extends Service {


        final CallbackWrapper wrapper = findCallbackWrapper(callbacks, callback);
        final CallbackWrapper wrapper = findCallbackWrapper(callbacks, callback);
        if (wrapper != null) {
        if (wrapper != null) {
            removeCallbackWrapper(callbacks, wrapper);
        }
    }

    private void removeCallbackWrapper(
                ArrayList<CallbackWrapper> callbacks, CallbackWrapper wrapper) {
        if (callbacks == null) {
            return;
        }
        callbacks.remove(wrapper);
        callbacks.remove(wrapper);
        if (callbacks.isEmpty()) {
        if (callbacks.isEmpty()) {
            onStopPredictionUpdates();
            onStopPredictionUpdates();
        }
        }
    }
    }
    }


    /**
    /**
     * Called when there are no longer any continuous prediction callbacks registered.
     * Called when there are no longer any continuous prediction callbacks registered.
@@ -295,9 +306,12 @@ public abstract class AppPredictionService extends Service {
            IBinder.DeathRecipient {
            IBinder.DeathRecipient {


        private IPredictionCallback mCallback;
        private IPredictionCallback mCallback;
        private final Consumer<CallbackWrapper> mOnBinderDied;


        CallbackWrapper(IPredictionCallback callback) {
        CallbackWrapper(IPredictionCallback callback,
                @Nullable Consumer<CallbackWrapper> onBinderDied) {
            mCallback = callback;
            mCallback = callback;
            mOnBinderDied = onBinderDied;
            try {
            try {
                mCallback.asBinder().linkToDeath(this, 0);
                mCallback.asBinder().linkToDeath(this, 0);
            } catch (RemoteException e) {
            } catch (RemoteException e) {
@@ -306,6 +320,10 @@ public abstract class AppPredictionService extends Service {
        }
        }


        public boolean isCallback(@NonNull IPredictionCallback callback) {
        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);
            return mCallback.equals(callback);
        }
        }


@@ -323,6 +341,9 @@ public abstract class AppPredictionService extends Service {
        @Override
        @Override
        public void binderDied() {
        public void binderDied() {
            mCallback = null;
            mCallback = null;
            if (mOnBinderDied != null) {
                mOnBinderDied.accept(this);
            }
        }
        }
    }
    }
}
}