Merge "Revert "enable dynamic binding in AppPredictionSession""
diff --git a/core/java/android/app/prediction/AppPredictionContext.java b/core/java/android/app/prediction/AppPredictionContext.java
index 8fc7e8d..d14238b 100644
--- a/core/java/android/app/prediction/AppPredictionContext.java
+++ b/core/java/android/app/prediction/AppPredictionContext.java
@@ -100,16 +100,6 @@
                 && mPackageName.equals(other.mPackageName);
     }
 
-    @NonNull
-    @Override
-    public String toString() {
-        return new StringBuilder(this.getClass().getSimpleName())
-                .append("[mUiSurface=").append(mUiSurface)
-                .append(",mPackageName=").append(mPackageName)
-                .append(",mPredictedTargetCount=").append(mPredictedTargetCount)
-                .append(",mExtras=").append(mExtras.toString()).append("]").toString();
-    }
-
     @Override
     public int describeContents() {
         return 0;
diff --git a/services/appprediction/java/com/android/server/appprediction/AppPredictionPerUserService.java b/services/appprediction/java/com/android/server/appprediction/AppPredictionPerUserService.java
index 48d976b..4f49fb7 100644
--- a/services/appprediction/java/com/android/server/appprediction/AppPredictionPerUserService.java
+++ b/services/appprediction/java/com/android/server/appprediction/AppPredictionPerUserService.java
@@ -32,14 +32,11 @@
 import android.os.RemoteException;
 import android.service.appprediction.AppPredictionService;
 import android.util.ArrayMap;
-import android.util.ArraySet;
 import android.util.Slog;
 
 import com.android.internal.annotations.GuardedBy;
 import com.android.server.infra.AbstractPerUserSystemService;
 
-import java.util.Map;
-import java.util.Set;
 import java.util.function.Consumer;
 
 /**
@@ -51,13 +48,9 @@
 
     private static final String TAG = AppPredictionPerUserService.class.getSimpleName();
 
-    /**
-     * A lookup of remote services in respect to their {@link ComponentName}.
-     */
-    @NonNull
+    @Nullable
     @GuardedBy("mLock")
-    private final ArrayMap<ComponentName, RemoteAppPredictionService> mRemoteServices =
-            new ArrayMap<>();
+    private RemoteAppPredictionService mRemoteService;
 
     /**
      * When {@code true}, remote service died but service state is kept so it's restored after
@@ -99,7 +92,7 @@
         if (enabledChanged) {
             if (!isEnabledLocked()) {
                 // Clear the remote service for the next call
-                mRemoteServices.clear();
+                mRemoteService = null;
             }
         }
         return enabledChanged;
@@ -111,14 +104,14 @@
     @GuardedBy("mLock")
     public void onCreatePredictionSessionLocked(@NonNull AppPredictionContext context,
             @NonNull AppPredictionSessionId sessionId) {
-        if (!mSessionInfos.containsKey(sessionId)) {
-            mSessionInfos.put(sessionId, new AppPredictionSessionInfo(sessionId, context,
-                    resolveComponentName(context), this::removeAppPredictionSessionInfo));
-        }
-        final RemoteAppPredictionService service = getRemoteServiceLocked(
-                getComponentName(sessionId));
+        final RemoteAppPredictionService service = getRemoteServiceLocked();
         if (service != null) {
             service.onCreatePredictionSession(context, sessionId);
+
+            if (!mSessionInfos.containsKey(sessionId)) {
+                mSessionInfos.put(sessionId, new AppPredictionSessionInfo(sessionId, context,
+                        this::removeAppPredictionSessionInfo));
+            }
         }
     }
 
@@ -128,8 +121,7 @@
     @GuardedBy("mLock")
     public void notifyAppTargetEventLocked(@NonNull AppPredictionSessionId sessionId,
             @NonNull AppTargetEvent event) {
-        final RemoteAppPredictionService service = getRemoteServiceLocked(
-                getComponentName(sessionId));
+        final RemoteAppPredictionService service = getRemoteServiceLocked();
         if (service != null) {
             service.notifyAppTargetEvent(sessionId, event);
         }
@@ -141,8 +133,7 @@
     @GuardedBy("mLock")
     public void notifyLaunchLocationShownLocked(@NonNull AppPredictionSessionId sessionId,
             @NonNull String launchLocation, @NonNull ParceledListSlice targetIds) {
-        final RemoteAppPredictionService service = getRemoteServiceLocked(
-                getComponentName(sessionId));
+        final RemoteAppPredictionService service = getRemoteServiceLocked();
         if (service != null) {
             service.notifyLaunchLocationShown(sessionId, launchLocation, targetIds);
         }
@@ -154,8 +145,7 @@
     @GuardedBy("mLock")
     public void sortAppTargetsLocked(@NonNull AppPredictionSessionId sessionId,
             @NonNull ParceledListSlice targets, @NonNull IPredictionCallback callback) {
-        final RemoteAppPredictionService service = getRemoteServiceLocked(
-                getComponentName(sessionId));
+        final RemoteAppPredictionService service = getRemoteServiceLocked();
         if (service != null) {
             service.sortAppTargets(sessionId, targets, callback);
         }
@@ -167,8 +157,7 @@
     @GuardedBy("mLock")
     public void registerPredictionUpdatesLocked(@NonNull AppPredictionSessionId sessionId,
             @NonNull IPredictionCallback callback) {
-        final RemoteAppPredictionService service = getRemoteServiceLocked(
-                getComponentName(sessionId));
+        final RemoteAppPredictionService service = getRemoteServiceLocked();
         if (service != null) {
             service.registerPredictionUpdates(sessionId, callback);
 
@@ -185,8 +174,7 @@
     @GuardedBy("mLock")
     public void unregisterPredictionUpdatesLocked(@NonNull AppPredictionSessionId sessionId,
             @NonNull IPredictionCallback callback) {
-        final RemoteAppPredictionService service = getRemoteServiceLocked(
-                getComponentName(sessionId));
+        final RemoteAppPredictionService service = getRemoteServiceLocked();
         if (service != null) {
             service.unregisterPredictionUpdates(sessionId, callback);
 
@@ -202,8 +190,7 @@
      */
     @GuardedBy("mLock")
     public void requestPredictionUpdateLocked(@NonNull AppPredictionSessionId sessionId) {
-        final RemoteAppPredictionService service = getRemoteServiceLocked(
-                getComponentName(sessionId));
+        final RemoteAppPredictionService service = getRemoteServiceLocked();
         if (service != null) {
             service.requestPredictionUpdate(sessionId);
         }
@@ -214,8 +201,7 @@
      */
     @GuardedBy("mLock")
     public void onDestroyPredictionSessionLocked(@NonNull AppPredictionSessionId sessionId) {
-        final RemoteAppPredictionService service = getRemoteServiceLocked(
-                getComponentName(sessionId));
+        final RemoteAppPredictionService service = getRemoteServiceLocked();
         if (service != null) {
             service.onDestroyPredictionSession(sessionId);
 
@@ -243,7 +229,7 @@
             synchronized (mLock) {
                 if (mZombie) {
                     // Sanity check - shouldn't happen
-                    if (mRemoteServices.isEmpty()) {
+                    if (mRemoteService == null) {
                         Slog.w(TAG, "Cannot resurrect sessions because remote service is null");
                         return;
                     }
@@ -280,30 +266,22 @@
     }
 
     private void destroyAndRebindRemoteService() {
-        if (mRemoteServices.isEmpty()) {
+        if (mRemoteService == null) {
             return;
         }
 
         if (isDebug()) {
             Slog.d(TAG, "Destroying the old remote service.");
         }
-        final Set<Map.Entry<ComponentName, RemoteAppPredictionService>> services =
-                new ArraySet<>(mRemoteServices.entrySet());
-        mRemoteServices.clear();
-        services.stream().forEach(entry -> destroyAndRebindRemoteService(
-                entry.getKey(), entry.getValue()));
-    }
+        mRemoteService.destroy();
+        mRemoteService = null;
 
-    private void destroyAndRebindRemoteService(
-            @NonNull final ComponentName component,
-            @NonNull final RemoteAppPredictionService service) {
-        service.destroy();
-        final RemoteAppPredictionService newService = getRemoteServiceLocked(component);
-        if (newService != null) {
+        mRemoteService = getRemoteServiceLocked();
+        if (mRemoteService != null) {
             if (isDebug()) {
                 Slog.d(TAG, "Rebinding to the new remote service.");
             }
-            newService.reconnect();
+            mRemoteService.reconnect();
         }
     }
 
@@ -314,7 +292,7 @@
     private void resurrectSessionsLocked() {
         final int numSessions = mSessionInfos.size();
         if (isDebug()) {
-            Slog.d(TAG, "Resurrecting remote service (" + mRemoteServices + ") on "
+            Slog.d(TAG, "Resurrecting remote service (" + mRemoteService + ") on "
                     + numSessions + " sessions.");
         }
 
@@ -332,49 +310,32 @@
         }
     }
 
-    @Nullable
-    private ComponentName resolveComponentName(@NonNull final AppPredictionContext context) {
-        // TODO: add logic to determine serviceName based on context
-        final String serviceName = getComponentNameLocked();
-        if (serviceName == null) {
-            if (mMaster.verbose) {
-                Slog.v(TAG, "getRemoteServiceLocked(): not set, context = " + context);
-            }
-            return null;
-        }
-        return ComponentName.unflattenFromString(serviceName);
-    }
-
-    @Nullable
-    private ComponentName getComponentName(@NonNull final AppPredictionSessionId sessionId) {
-        AppPredictionSessionInfo sessionInfo = mSessionInfos.get(sessionId);
-        return sessionInfo == null ? null : sessionInfo.mComponentName;
-    }
-
     @GuardedBy("mLock")
     @Nullable
-    private RemoteAppPredictionService getRemoteServiceLocked(
-            @Nullable final ComponentName serviceComponent) {
-        if (serviceComponent == null) return null;
-        if (!mRemoteServices.containsKey(serviceComponent)) {
-            mRemoteServices.put(serviceComponent, new RemoteAppPredictionService(getContext(),
+    private RemoteAppPredictionService getRemoteServiceLocked() {
+        if (mRemoteService == null) {
+            final String serviceName = getComponentNameLocked();
+            if (serviceName == null) {
+                if (mMaster.verbose) {
+                    Slog.v(TAG, "getRemoteServiceLocked(): not set");
+                }
+                return null;
+            }
+            ComponentName serviceComponent = ComponentName.unflattenFromString(serviceName);
+
+            mRemoteService = new RemoteAppPredictionService(getContext(),
                     AppPredictionService.SERVICE_INTERFACE, serviceComponent, mUserId, this,
-                    mMaster.isBindInstantServiceAllowed(), mMaster.verbose));
+                    mMaster.isBindInstantServiceAllowed(), mMaster.verbose);
         }
 
-        return mRemoteServices.get(serviceComponent);
+        return mRemoteService;
     }
 
     private static final class AppPredictionSessionInfo {
         private static final boolean DEBUG = false;  // Do not submit with true
 
-        @NonNull
         private final AppPredictionSessionId mSessionId;
-        @NonNull
         private final AppPredictionContext mPredictionContext;
-        @Nullable
-        private final ComponentName mComponentName;
-        @NonNull
         private final Consumer<AppPredictionSessionId> mRemoveSessionInfoAction;
 
         private final RemoteCallbackList<IPredictionCallback> mCallbacks =
@@ -391,17 +352,13 @@
                     }
                 };
 
-        AppPredictionSessionInfo(
-                @NonNull final AppPredictionSessionId id,
-                @NonNull final AppPredictionContext predictionContext,
-                @Nullable final ComponentName componentName,
-                @NonNull final Consumer<AppPredictionSessionId> removeSessionInfoAction) {
+        AppPredictionSessionInfo(AppPredictionSessionId id, AppPredictionContext predictionContext,
+                Consumer<AppPredictionSessionId> removeSessionInfoAction) {
             if (DEBUG) {
                 Slog.d(TAG, "Creating AppPredictionSessionInfo for session Id=" + id);
             }
             mSessionId = id;
             mPredictionContext = predictionContext;
-            mComponentName = componentName;
             mRemoveSessionInfoAction = removeSessionInfoAction;
         }
 
@@ -433,8 +390,8 @@
         void resurrectSessionLocked(AppPredictionPerUserService service) {
             int callbackCount = mCallbacks.getRegisteredCallbackCount();
             if (DEBUG) {
-                Slog.d(TAG, "Resurrecting remote service (" + service.getRemoteServiceLocked(
-                        mComponentName) + ") for session Id=" + mSessionId + " and "
+                Slog.d(TAG, "Resurrecting remote service (" + service.getRemoteServiceLocked()
+                        + ") for session Id=" + mSessionId + " and "
                         + callbackCount + " callbacks.");
             }
             service.onCreatePredictionSessionLocked(mPredictionContext, mSessionId);