diff options
| author | Shashwat Razdan <srazdan@google.com> | 2021-04-20 16:01:42 +0000 |
|---|---|---|
| committer | Android (Google) Code Review <android-gerrit@google.com> | 2021-04-20 16:01:42 +0000 |
| commit | e7f4889bebb201522ce52c1ca8a9e2baebd2d757 (patch) | |
| tree | 81ccf9511d9fbdb7360e81064f9cd3d6b52e1f5b /core/java/android | |
| parent | 692496d964491e16dea0b01004f11653eecd0b98 (diff) | |
| parent | 8c63ca65814af72f660dcf84075aba68148c883e (diff) | |
Merge "Addressing the feedback received in the API review." into sc-dev
Diffstat (limited to 'core/java/android')
5 files changed, 91 insertions, 82 deletions
diff --git a/core/java/android/app/smartspace/SmartspaceConfig.java b/core/java/android/app/smartspace/SmartspaceConfig.java index 07d7bf0e33af..0897b5fb81e7 100644 --- a/core/java/android/app/smartspace/SmartspaceConfig.java +++ b/core/java/android/app/smartspace/SmartspaceConfig.java @@ -15,6 +15,7 @@ */ package android.app.smartspace; +import android.annotation.IntRange; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.SuppressLint; @@ -41,6 +42,7 @@ public final class SmartspaceConfig implements Parcelable { * The least number of smartspace targets expected to be predicted by the backend. The backend * will always try to satisfy this threshold but it is not guaranteed to always meet it. */ + @IntRange(from = 0, to = 50) private final int mSmartspaceTargetCount; /** @@ -54,13 +56,15 @@ public final class SmartspaceConfig implements Parcelable { @NonNull private String mPackageName; - /** Send other client UI configurations in extras. + /** + * Send other client UI configurations in extras. * * This can include: * - * - Desired maximum update frequency - * - Request to get periodic updates - * - Request to support multiple clients for the same UISurface. + * - Desired maximum update frequency (For example 1 minute update frequency for AoD, 1 second + * update frequency for home screen etc). + * - Request to get periodic updates + * - Request to support multiple clients for the same UISurface. */ @Nullable private final Bundle mExtras; @@ -165,7 +169,7 @@ public final class SmartspaceConfig implements Parcelable { private Bundle mExtras = Bundle.EMPTY; /** - * @param context The {@link Context} which is used to fetch the package name. + * @param context The {@link Context} which is used to fetch the package name. * @param uiSurface the UI Surface name associated with this context. * @hide */ @@ -179,7 +183,8 @@ public final class SmartspaceConfig implements Parcelable { * Used to set the expected number of cards for this context. */ @NonNull - public Builder setSmartspaceTargetCount(int smartspaceTargetCount) { + public Builder setSmartspaceTargetCount( + @IntRange(from = 0, to = 50) int smartspaceTargetCount) { this.mSmartspaceTargetCount = smartspaceTargetCount; return this; } diff --git a/core/java/android/app/smartspace/SmartspaceSession.java b/core/java/android/app/smartspace/SmartspaceSession.java index 16def61239cf..9199581c3149 100644 --- a/core/java/android/app/smartspace/SmartspaceSession.java +++ b/core/java/android/app/smartspace/SmartspaceSession.java @@ -63,7 +63,7 @@ import java.util.function.Consumer; * * void onDestroy() { * mSmartspaceSession.unregisterPredictionUpdates() - * mSmartspaceSession.destroy(); + * mSmartspaceSession.close(); * } * * }</pre> @@ -81,7 +81,8 @@ public final class SmartspaceSession implements AutoCloseable { private final AtomicBoolean mIsClosed = new AtomicBoolean(false); private final SmartspaceSessionId mSessionId; - private final ArrayMap<Callback, CallbackWrapper> mRegisteredCallbacks = new ArrayMap<>(); + private final ArrayMap<OnTargetsAvailableListener, CallbackWrapper> mRegisteredCallbacks = + new ArrayMap<>(); private final IBinder mToken = new Binder(); /** @@ -98,11 +99,11 @@ public final class SmartspaceSession implements AutoCloseable { IBinder b = ServiceManager.getService(Context.SMARTSPACE_SERVICE); mInterface = android.app.smartspace.ISmartspaceManager.Stub.asInterface(b); mSessionId = new SmartspaceSessionId( - context.getPackageName() + ":" + UUID.randomUUID().toString(), context.getUserId()); + context.getPackageName() + ":" + UUID.randomUUID().toString(), context.getUser()); try { mInterface.createSmartspaceSession(smartspaceConfig, mSessionId, mToken); } catch (RemoteException e) { - Log.e(TAG, "Failed to cerate Smartspace session", e); + Log.e(TAG, "Failed to create Smartspace session", e); e.rethrowFromSystemServer(); } @@ -145,24 +146,24 @@ public final class SmartspaceSession implements AutoCloseable { * Requests the smartspace service provide continuous updates of smartspace cards via the * provided callback, until the given callback is unregistered. * - * @param callbackExecutor The callback executor to use when calling the callback. - * @param callback The Callback to be called when updates of Smartspace targets are + * @param listenerExecutor The listener executor to use when firing the listener. + * @param listener The listener to be called when updates of Smartspace targets are * available. */ - public void registerSmartspaceUpdates(@NonNull @CallbackExecutor Executor callbackExecutor, - @NonNull Callback callback) { + public void addOnTargetsAvailableListener(@NonNull @CallbackExecutor Executor listenerExecutor, + @NonNull OnTargetsAvailableListener listener) { if (mIsClosed.get()) { throw new IllegalStateException("This client has already been destroyed."); } - if (mRegisteredCallbacks.containsKey(callback)) { + if (mRegisteredCallbacks.containsKey(listener)) { // Skip if this callback is already registered return; } try { - final CallbackWrapper callbackWrapper = new CallbackWrapper(callbackExecutor, - callback::onTargetsAvailable); - mRegisteredCallbacks.put(callback, callbackWrapper); + final CallbackWrapper callbackWrapper = new CallbackWrapper(listenerExecutor, + listener::onTargetsAvailable); + mRegisteredCallbacks.put(listener, callbackWrapper); mInterface.registerSmartspaceUpdates(mSessionId, callbackWrapper); mInterface.requestSmartspaceUpdate(mSessionId); } catch (RemoteException e) { @@ -175,21 +176,21 @@ public final class SmartspaceSession implements AutoCloseable { * Requests the smartspace service to stop providing continuous updates to the provided * callback until the callback is re-registered. * - * @see {@link SmartspaceSession#registerSmartspaceUpdates(Executor, Callback)}. - * - * @param callback The callback to be unregistered. + * @param listener The callback to be unregistered. + * @see {@link SmartspaceSession#addOnTargetsAvailableListener(Executor, + * OnTargetsAvailableListener)}. */ - public void unregisterSmartspaceUpdates(@NonNull Callback callback) { + public void removeOnTargetsAvailableListener(@NonNull OnTargetsAvailableListener listener) { if (mIsClosed.get()) { throw new IllegalStateException("This client has already been destroyed."); } - if (!mRegisteredCallbacks.containsKey(callback)) { + if (!mRegisteredCallbacks.containsKey(listener)) { // Skip if this callback was never registered return; } try { - final CallbackWrapper callbackWrapper = mRegisteredCallbacks.remove(callback); + final CallbackWrapper callbackWrapper = mRegisteredCallbacks.remove(listener); mInterface.unregisterSmartspaceUpdates(mSessionId, callbackWrapper); } catch (RemoteException e) { Log.e(TAG, "Failed to unregister for smartspace updates", e); @@ -201,7 +202,7 @@ public final class SmartspaceSession implements AutoCloseable { * Destroys the client and unregisters the callback. Any method on this class after this call * will throw {@link IllegalStateException}. */ - public void destroy() { + private void destroy() { if (!mIsClosed.getAndSet(true)) { mCloseGuard.close(); @@ -238,6 +239,7 @@ public final class SmartspaceSession implements AutoCloseable { @Override public void close() { try { + destroy(); finalize(); } catch (Throwable throwable) { throwable.printStackTrace(); @@ -245,14 +247,14 @@ public final class SmartspaceSession implements AutoCloseable { } /** - * Callback for receiving smartspace updates. + * Listener to receive smartspace targets from the service. */ - public interface Callback { + public interface OnTargetsAvailableListener { /** * Called when a new set of smartspace targets are available. * - * @param targets Sorted list of smartspace targets. + * @param targets Ranked list of smartspace targets. */ void onTargetsAvailable(@NonNull List<SmartspaceTarget> targets); } diff --git a/core/java/android/app/smartspace/SmartspaceSessionId.java b/core/java/android/app/smartspace/SmartspaceSessionId.java index 5220c35d7064..4040cb37b99b 100644 --- a/core/java/android/app/smartspace/SmartspaceSessionId.java +++ b/core/java/android/app/smartspace/SmartspaceSessionId.java @@ -21,6 +21,7 @@ import android.annotation.Nullable; import android.annotation.SystemApi; import android.os.Parcel; import android.os.Parcelable; +import android.os.UserHandle; import java.util.Objects; @@ -36,21 +37,21 @@ public final class SmartspaceSessionId implements Parcelable { private final String mId; @NonNull - private final int mUserId; + private final UserHandle mUserHandle; /** * Creates a new id for a Smartspace session. * * @hide */ - public SmartspaceSessionId(@NonNull final String id, @NonNull final int userId) { + public SmartspaceSessionId(@NonNull final String id, @NonNull final UserHandle userHandle) { mId = id; - mUserId = userId; + mUserHandle = userHandle; } private SmartspaceSessionId(Parcel p) { mId = p.readString(); - mUserId = p.readInt(); + mUserHandle = p.readTypedObject(UserHandle.CREATOR); } /** @@ -65,8 +66,8 @@ public final class SmartspaceSessionId implements Parcelable { * Returns the userId associated with this sessionId. */ @NonNull - public int getUserId() { - return mUserId; + public UserHandle getUserHandle() { + return mUserHandle; } @Override @@ -74,20 +75,20 @@ public final class SmartspaceSessionId implements Parcelable { if (!getClass().equals(o != null ? o.getClass() : null)) return false; SmartspaceSessionId other = (SmartspaceSessionId) o; - return mId.equals(other.mId) && mUserId == other.mUserId; + return mId.equals(other.mId) && mUserHandle == other.mUserHandle; } @Override public String toString() { return "SmartspaceSessionId{" + "mId='" + mId + '\'' - + ", mUserId=" + mUserId + + ", mUserId=" + mUserHandle.getIdentifier() + '}'; } @Override public int hashCode() { - return Objects.hash(mId, mUserId); + return Objects.hash(mId, mUserHandle); } @Override @@ -98,7 +99,7 @@ public final class SmartspaceSessionId implements Parcelable { @Override public void writeToParcel(@NonNull Parcel dest, int flags) { dest.writeString(mId); - dest.writeInt(mUserId); + dest.writeTypedObject(this.mUserHandle, flags); } public static final @NonNull Creator<SmartspaceSessionId> CREATOR = diff --git a/core/java/android/app/smartspace/SmartspaceTarget.java b/core/java/android/app/smartspace/SmartspaceTarget.java index ce5040eb0a3e..8e9853575c31 100644 --- a/core/java/android/app/smartspace/SmartspaceTarget.java +++ b/core/java/android/app/smartspace/SmartspaceTarget.java @@ -15,6 +15,7 @@ */ package android.app.smartspace; +import android.annotation.CurrentTimeMillisLong; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.Nullable; @@ -39,7 +40,7 @@ import java.util.Objects; * {@link SmartspaceAction} as their type because they can have associated actions. * * <p><b>NOTE: </b> - * If {@link mWidgetId} is set, it should be preferred over all other properties. + * If {@link mWidget} is set, it should be preferred over all other properties. * Else, if {@link mSliceUri} is set, it should be preferred over all other data properties. * Otherwise, the instance should be treated as a data object. * @@ -61,18 +62,17 @@ public final class SmartspaceTarget implements Parcelable { private final SmartspaceAction mBaseAction; /** A timestamp indicating when the card was created. */ - @NonNull + @CurrentTimeMillisLong private final long mCreationTimeMillis; /** * A timestamp indicating when the card should be removed from view, in case the service * disconnects or restarts. */ - @NonNull + @CurrentTimeMillisLong private final long mExpiryTimeMillis; /** A score assigned to a target. */ - @NonNull private final float mScore; /** A {@link List<SmartspaceAction>} containing all action chips. */ @@ -89,18 +89,15 @@ public final class SmartspaceTarget implements Parcelable { * @see FeatureType */ @FeatureType - @NonNull private final int mFeatureType; /** * Indicates whether the content is sensitive. Certain UI surfaces may choose to skip rendering * real content until the device is unlocked. */ - @NonNull private final boolean mSensitive; /** Indicating if the UI should show this target in its expanded state. */ - @NonNull private final boolean mShouldShowExpanded; /** A Notification key if the target was generated using a notification. */ @@ -115,7 +112,14 @@ public final class SmartspaceTarget implements Parcelable { @NonNull private final UserHandle mUserHandle; - /** Target Ids of other {@link SmartspaceTarget}s if they are associated with this target. */ + /** + * Target Id of other {@link SmartspaceTarget}s if it is associated with this target. This + * association is added to tell the UI that a card would be more useful if displayed with the + * associated smartspace target. This field is supposed to be taken as a suggestion and the + * association can be ignored based on the situation in the UI. It is possible to have a one way + * card association. In other words, Card B can be associated with Card A but not the other way + * around. + */ @Nullable private final String mAssociatedSmartspaceTargetId; @@ -125,7 +129,7 @@ public final class SmartspaceTarget implements Parcelable { /** {@link AppWidgetProviderInfo} if this target is a widget. */ @Nullable - private final AppWidgetProviderInfo mWidgetId; + private final AppWidgetProviderInfo mWidget; public static final int FEATURE_UNDEFINED = 0; public static final int FEATURE_WEATHER = 1; @@ -202,7 +206,7 @@ public final class SmartspaceTarget implements Parcelable { this.mUserHandle = in.readTypedObject(UserHandle.CREATOR); this.mAssociatedSmartspaceTargetId = in.readString(); this.mSliceUri = in.readTypedObject(Uri.CREATOR); - this.mWidgetId = in.readTypedObject(AppWidgetProviderInfo.CREATOR); + this.mWidget = in.readTypedObject(AppWidgetProviderInfo.CREATOR); } private SmartspaceTarget(String smartspaceTargetId, @@ -213,7 +217,7 @@ public final class SmartspaceTarget implements Parcelable { boolean shouldShowExpanded, String sourceNotificationKey, ComponentName componentName, UserHandle userHandle, String associatedSmartspaceTargetId, Uri sliceUri, - AppWidgetProviderInfo widgetId) { + AppWidgetProviderInfo widget) { mSmartspaceTargetId = smartspaceTargetId; mHeaderAction = headerAction; mBaseAction = baseAction; @@ -230,7 +234,7 @@ public final class SmartspaceTarget implements Parcelable { mUserHandle = userHandle; mAssociatedSmartspaceTargetId = associatedSmartspaceTargetId; mSliceUri = sliceUri; - mWidgetId = widgetId; + mWidget = widget; } /** @@ -260,7 +264,7 @@ public final class SmartspaceTarget implements Parcelable { /** * Returns the creation time of the target. */ - @NonNull + @CurrentTimeMillisLong public long getCreationTimeMillis() { return mCreationTimeMillis; } @@ -268,7 +272,7 @@ public final class SmartspaceTarget implements Parcelable { /** * Returns the expiry time of the target. */ - @NonNull + @CurrentTimeMillisLong public long getExpiryTimeMillis() { return mExpiryTimeMillis; } @@ -276,7 +280,6 @@ public final class SmartspaceTarget implements Parcelable { /** * Returns the score of the target. */ - @NonNull public float getScore() { return mScore; } @@ -300,7 +303,7 @@ public final class SmartspaceTarget implements Parcelable { /** * Returns the feature type of the target. */ - @NonNull + @FeatureType public int getFeatureType() { return mFeatureType; } @@ -308,7 +311,6 @@ public final class SmartspaceTarget implements Parcelable { /** * Returns whether the target is sensitive or not. */ - @NonNull public boolean isSensitive() { return mSensitive; } @@ -316,7 +318,6 @@ public final class SmartspaceTarget implements Parcelable { /** * Returns whether the target should be shown in expanded state. */ - @NonNull public boolean shouldShowExpanded() { return mShouldShowExpanded; } @@ -365,8 +366,8 @@ public final class SmartspaceTarget implements Parcelable { * Returns the AppWidgetProviderInfo, if the target is a widget. */ @Nullable - public AppWidgetProviderInfo getWidgetId() { - return mWidgetId; + public AppWidgetProviderInfo getWidget() { + return mWidget; } /** @@ -403,7 +404,7 @@ public final class SmartspaceTarget implements Parcelable { dest.writeTypedObject(this.mUserHandle, flags); dest.writeString(this.mAssociatedSmartspaceTargetId); dest.writeTypedObject(this.mSliceUri, flags); - dest.writeTypedObject(this.mWidgetId, flags); + dest.writeTypedObject(this.mWidget, flags); } @Override @@ -430,7 +431,7 @@ public final class SmartspaceTarget implements Parcelable { + ", mUserHandle=" + mUserHandle + ", mAssociatedSmartspaceTargetId='" + mAssociatedSmartspaceTargetId + '\'' + ", mSliceUri=" + mSliceUri - + ", mWidgetId=" + mWidgetId + + ", mWidget=" + mWidget + '}'; } @@ -456,7 +457,7 @@ public final class SmartspaceTarget implements Parcelable { && Objects.equals(mAssociatedSmartspaceTargetId, that.mAssociatedSmartspaceTargetId) && Objects.equals(mSliceUri, that.mSliceUri) - && Objects.equals(mWidgetId, that.mWidgetId); + && Objects.equals(mWidget, that.mWidget); } @Override @@ -464,7 +465,7 @@ public final class SmartspaceTarget implements Parcelable { return Objects.hash(mSmartspaceTargetId, mHeaderAction, mBaseAction, mCreationTimeMillis, mExpiryTimeMillis, mScore, mActionChips, mIconGrid, mFeatureType, mSensitive, mShouldShowExpanded, mSourceNotificationKey, mComponentName, mUserHandle, - mAssociatedSmartspaceTargetId, mSliceUri, mWidgetId); + mAssociatedSmartspaceTargetId, mSliceUri, mWidget); } /** @@ -490,7 +491,7 @@ public final class SmartspaceTarget implements Parcelable { private final UserHandle mUserHandle; private String mAssociatedSmartspaceTargetId; private Uri mSliceUri; - private AppWidgetProviderInfo mWidgetId; + private AppWidgetProviderInfo mWidget; /** * A builder for {@link SmartspaceTarget}. @@ -528,7 +529,7 @@ public final class SmartspaceTarget implements Parcelable { * Sets the creation time. */ @NonNull - public Builder setCreationTimeMillis(@NonNull long creationTimeMillis) { + public Builder setCreationTimeMillis(@CurrentTimeMillisLong long creationTimeMillis) { this.mCreationTimeMillis = creationTimeMillis; return this; } @@ -537,7 +538,7 @@ public final class SmartspaceTarget implements Parcelable { * Sets the expiration time. */ @NonNull - public Builder setExpiryTimeMillis(@NonNull long expiryTimeMillis) { + public Builder setExpiryTimeMillis(@CurrentTimeMillisLong long expiryTimeMillis) { this.mExpiryTimeMillis = expiryTimeMillis; return this; } @@ -546,7 +547,7 @@ public final class SmartspaceTarget implements Parcelable { * Sets the score. */ @NonNull - public Builder setScore(@NonNull float score) { + public Builder setScore(float score) { this.mScore = score; return this; } @@ -573,7 +574,7 @@ public final class SmartspaceTarget implements Parcelable { * Sets the feature type. */ @NonNull - public Builder setFeatureType(@NonNull int featureType) { + public Builder setFeatureType(int featureType) { this.mFeatureType = featureType; return this; } @@ -582,7 +583,7 @@ public final class SmartspaceTarget implements Parcelable { * Sets whether the contents are sensitive. */ @NonNull - public Builder setSensitive(@NonNull boolean sensitive) { + public Builder setSensitive(boolean sensitive) { this.mSensitive = sensitive; return this; } @@ -591,7 +592,7 @@ public final class SmartspaceTarget implements Parcelable { * Sets whether to show the card as expanded. */ @NonNull - public Builder setShouldShowExpanded(@NonNull boolean shouldShowExpanded) { + public Builder setShouldShowExpanded(boolean shouldShowExpanded) { this.mShouldShowExpanded = shouldShowExpanded; return this; } @@ -618,7 +619,7 @@ public final class SmartspaceTarget implements Parcelable { /** * Sets the slice uri. * - * <p><b>NOTE: </b> If {@link mWidgetId} is also set, {@link mSliceUri} should be ignored. + * <p><b>NOTE: </b> If {@link mWidget} is also set, {@link mSliceUri} should be ignored. */ @NonNull public Builder setSliceUri(@NonNull Uri sliceUri) { @@ -629,12 +630,12 @@ public final class SmartspaceTarget implements Parcelable { /** * Sets the widget id. * - * <p><b>NOTE: </b> If {@link mWidgetId} is set, all other @Nullable params should be + * <p><b>NOTE: </b> If {@link mWidget} is set, all other @Nullable params should be * ignored. */ @NonNull - public Builder setWidgetId(@NonNull AppWidgetProviderInfo widgetId) { - this.mWidgetId = widgetId; + public Builder setWidget(@NonNull AppWidgetProviderInfo widget) { + this.mWidget = widget; return this; } @@ -654,7 +655,7 @@ public final class SmartspaceTarget implements Parcelable { mHeaderAction, mBaseAction, mCreationTimeMillis, mExpiryTimeMillis, mScore, mActionChips, mIconGrid, mFeatureType, mSensitive, mShouldShowExpanded, mSourceNotificationKey, mComponentName, mUserHandle, - mAssociatedSmartspaceTargetId, mSliceUri, mWidgetId); + mAssociatedSmartspaceTargetId, mSliceUri, mWidget); } } } diff --git a/core/java/android/app/smartspace/SmartspaceTargetEvent.java b/core/java/android/app/smartspace/SmartspaceTargetEvent.java index 920b9fe6a34f..61f8723ca393 100644 --- a/core/java/android/app/smartspace/SmartspaceTargetEvent.java +++ b/core/java/android/app/smartspace/SmartspaceTargetEvent.java @@ -41,11 +41,11 @@ public final class SmartspaceTargetEvent implements Parcelable { /** * Smartspace target was brought into view. */ - public static final int EVENT_TARGET_IN_VIEW = 2; + public static final int EVENT_TARGET_SHOWN = 2; /** * Smartspace target went out of view. */ - public static final int EVENT_TARGET_OUT_OF_VIEW = 3; + public static final int EVENT_TARGET_HIDDEN = 3; /** * A dismiss action was issued by the user. */ @@ -57,11 +57,11 @@ public final class SmartspaceTargetEvent implements Parcelable { /** * The Ui surface came into view. */ - public static final int EVENT_UI_SURFACE_IN_VIEW = 6; + public static final int EVENT_UI_SURFACE_SHOWN = 6; /** * The Ui surface went out of view. */ - public static final int EVENT_UI_SURFACE_OUT_OF_VIEW = 7; + public static final int EVENT_UI_SURFACE_HIDDEN = 7; /** * @see Parcelable.Creator @@ -152,12 +152,12 @@ public final class SmartspaceTargetEvent implements Parcelable { */ @IntDef(prefix = {"EVENT_"}, value = { EVENT_TARGET_INTERACTION, - EVENT_TARGET_IN_VIEW, - EVENT_TARGET_OUT_OF_VIEW, + EVENT_TARGET_SHOWN, + EVENT_TARGET_HIDDEN, EVENT_TARGET_DISMISS, EVENT_TARGET_BLOCK, - EVENT_UI_SURFACE_IN_VIEW, - EVENT_UI_SURFACE_OUT_OF_VIEW + EVENT_UI_SURFACE_SHOWN, + EVENT_UI_SURFACE_HIDDEN }) @Retention(RetentionPolicy.SOURCE) public @interface EventType { |
