diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2020-02-12 08:05:03 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2020-02-12 08:05:03 +0000 |
commit | 5f212d00102d07d7f5c4867d8ac7c4913ca3fbb3 (patch) | |
tree | a73ae57d61a075cc346728865e023a3a4871e50d /services/appprediction | |
parent | 1d069de94580fbb24cdf5d7c3640b7efafede70e (diff) | |
parent | 5052796bf098273d1799267d5fb1eeaf7b89148d (diff) |
Merge "Revert "enable dynamic binding in AppPredictionSession""
Diffstat (limited to 'services/appprediction')
-rw-r--r-- | services/appprediction/java/com/android/server/appprediction/AppPredictionPerUserService.java | 125 |
1 files changed, 41 insertions, 84 deletions
diff --git a/services/appprediction/java/com/android/server/appprediction/AppPredictionPerUserService.java b/services/appprediction/java/com/android/server/appprediction/AppPredictionPerUserService.java index 48d976b1504d..4f49fb7578a1 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.RemoteCallbackList; 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 @@ public class AppPredictionPerUserService extends 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 @@ public class AppPredictionPerUserService extends if (enabledChanged) { if (!isEnabledLocked()) { // Clear the remote service for the next call - mRemoteServices.clear(); + mRemoteService = null; } } return enabledChanged; @@ -111,14 +104,14 @@ public class AppPredictionPerUserService extends @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 @@ public class AppPredictionPerUserService extends @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 @@ public class AppPredictionPerUserService extends @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 @@ public class AppPredictionPerUserService extends @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 @@ public class AppPredictionPerUserService extends @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 @@ public class AppPredictionPerUserService extends @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 @@ public class AppPredictionPerUserService extends */ @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 @@ public class AppPredictionPerUserService extends */ @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 @@ public class AppPredictionPerUserService extends 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 @@ public class AppPredictionPerUserService extends } 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 @@ public class AppPredictionPerUserService extends 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 @@ public class AppPredictionPerUserService extends } } + @GuardedBy("mLock") @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); + private RemoteAppPredictionService getRemoteServiceLocked() { + if (mRemoteService == null) { + final String serviceName = getComponentNameLocked(); + if (serviceName == null) { + if (mMaster.verbose) { + Slog.v(TAG, "getRemoteServiceLocked(): not set"); + } + return null; } - 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; - } + ComponentName serviceComponent = ComponentName.unflattenFromString(serviceName); - @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(), + 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 @@ public class AppPredictionPerUserService extends } }; - 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 @@ public class AppPredictionPerUserService extends 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); |