Merge "Ensure some calls are available for testing"

This commit is contained in:
Mehdi Alizadeh
2019-01-30 18:37:17 +00:00
committed by Android (Google) Code Review
10 changed files with 225 additions and 5 deletions

View File

@@ -18,6 +18,7 @@ package android.app.prediction;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcel;
@@ -29,6 +30,7 @@ import android.os.Parcelable;
* @hide
*/
@SystemApi
@TestApi
public final class AppPredictionContext implements Parcelable {
private final int mPredictedTargetCount;
@@ -72,6 +74,17 @@ public final class AppPredictionContext implements Parcelable {
return mExtras;
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!getClass().equals(o != null ? o.getClass() : null)) return false;
AppPredictionContext other = (AppPredictionContext) o;
return mPredictedTargetCount == other.mPredictedTargetCount
&& mUiSurface.equals(other.mUiSurface)
&& mPackageName.equals(other.mPackageName);
}
@Override
public int describeContents() {
return 0;
@@ -104,6 +117,7 @@ public final class AppPredictionContext implements Parcelable {
* @hide
*/
@SystemApi
@TestApi
public static final class Builder {
@NonNull
@@ -116,8 +130,12 @@ public final class AppPredictionContext implements Parcelable {
private Bundle mExtras;
/**
* TODO(b/123591863): Add java docs
*
* @hide
*/
@SystemApi
@TestApi
public Builder(@NonNull Context context) {
mPackageName = context.getPackageName();
}

View File

@@ -17,6 +17,7 @@ package android.app.prediction;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.content.Context;
import com.android.internal.util.Preconditions;
@@ -26,6 +27,7 @@ import com.android.internal.util.Preconditions;
* @hide
*/
@SystemApi
@TestApi
public final class AppPredictionManager {
private final Context mContext;

View File

@@ -17,6 +17,7 @@ package android.app.prediction;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.os.Parcel;
import android.os.Parcelable;
@@ -26,6 +27,7 @@ import android.os.Parcelable;
* @hide
*/
@SystemApi
@TestApi
public final class AppPredictionSessionId implements Parcelable {
private final String mId;

View File

@@ -19,6 +19,7 @@ import android.annotation.CallbackExecutor;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.app.prediction.IPredictionCallback.Stub;
import android.content.Context;
import android.content.pm.ParceledListSlice;
@@ -63,6 +64,7 @@ import java.util.function.Consumer;
* @hide
*/
@SystemApi
@TestApi
public final class AppPredictor {
private static final String TAG = AppPredictor.class.getSimpleName();
@@ -102,6 +104,10 @@ public final class AppPredictor {
* Notifies the prediction service of an app target event.
*/
public void notifyAppTargetEvent(@NonNull AppTargetEvent event) {
if (mIsClosed.get()) {
throw new IllegalStateException("This client has already been destroyed.");
}
try {
mPredictionManager.notifyAppTargetEvent(mSessionId, event);
} catch (RemoteException e) {
@@ -114,6 +120,10 @@ public final class AppPredictor {
*/
public void notifyLocationShown(@NonNull String launchLocation,
@NonNull List<AppTargetId> targetIds) {
if (mIsClosed.get()) {
throw new IllegalStateException("This client has already been destroyed.");
}
try {
mPredictionManager.notifyLocationShown(mSessionId, launchLocation,
new ParceledListSlice<>(targetIds));
@@ -130,6 +140,10 @@ public final class AppPredictor {
*/
public void registerPredictionUpdates(@NonNull @CallbackExecutor Executor callbackExecutor,
@NonNull AppPredictor.Callback callback) {
if (mIsClosed.get()) {
throw new IllegalStateException("This client has already been destroyed.");
}
if (mRegisteredCallbacks.containsKey(callback)) {
// Skip if this callback is already registered
return;
@@ -149,6 +163,10 @@ public final class AppPredictor {
* callback until the callback is re-registered.
*/
public void unregisterPredictionUpdates(@NonNull AppPredictor.Callback callback) {
if (mIsClosed.get()) {
throw new IllegalStateException("This client has already been destroyed.");
}
if (!mRegisteredCallbacks.containsKey(callback)) {
// Skip if this callback was never registered
return;
@@ -168,6 +186,10 @@ public final class AppPredictor {
* @see Callback#onTargetsAvailable(List)
*/
public void requestPredictionUpdate() {
if (mIsClosed.get()) {
throw new IllegalStateException("This client has already been destroyed.");
}
try {
mPredictionManager.requestPredictionUpdate(mSessionId);
} catch (RemoteException e) {
@@ -182,6 +204,10 @@ public final class AppPredictor {
@Nullable
public void sortTargets(@NonNull List<AppTarget> targets,
@NonNull Executor callbackExecutor, @NonNull Consumer<List<AppTarget>> callback) {
if (mIsClosed.get()) {
throw new IllegalStateException("This client has already been destroyed.");
}
try {
mPredictionManager.sortAppTargets(mSessionId, new ParceledListSlice(targets),
new CallbackWrapper(callbackExecutor, callback));
@@ -206,6 +232,8 @@ public final class AppPredictor {
} catch (RemoteException e) {
Log.e(TAG, "Failed to notify app target event", e);
}
} else {
throw new IllegalStateException("This client has already been destroyed.");
}
}
@@ -221,6 +249,16 @@ public final class AppPredictor {
}
}
/**
* TODO(b/123591863): Add java docs
*
* @hide
*/
@TestApi
public AppPredictionSessionId getSessionId() {
return mSessionId;
}
/**
* Callback for receiving prediction updates.
*/

View File

@@ -18,6 +18,7 @@ package android.app.prediction;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.content.pm.ShortcutInfo;
import android.os.Parcel;
import android.os.Parcelable;
@@ -30,6 +31,7 @@ import com.android.internal.util.Preconditions;
* @hide
*/
@SystemApi
@TestApi
public final class AppTarget implements Parcelable {
private final AppTargetId mId;
@@ -42,8 +44,12 @@ public final class AppTarget implements Parcelable {
private int mRank;
/**
* TODO(b/123591863): Add java docs
*
* @hide
*/
@SystemApi
@TestApi
public AppTarget(@NonNull AppTargetId id, @NonNull String packageName,
@Nullable String className, @NonNull UserHandle user) {
mId = id;
@@ -55,15 +61,19 @@ public final class AppTarget implements Parcelable {
}
/**
* TODO(b/123591863): Add java docs
*
* @hide
*/
public AppTarget(@NonNull AppTargetId id, @NonNull ShortcutInfo shortcutInfo) {
@SystemApi
public AppTarget(@NonNull AppTargetId id, @NonNull ShortcutInfo shortcutInfo,
@Nullable String className) {
mId = id;
mShortcutInfo = Preconditions.checkNotNull(shortcutInfo);
mPackageName = mShortcutInfo.getPackage();
mUser = mShortcutInfo.getUserHandle();
mClassName = null;
mClassName = className;
}
private AppTarget(Parcel parcel) {
@@ -71,13 +81,12 @@ public final class AppTarget implements Parcelable {
mShortcutInfo = parcel.readTypedObject(ShortcutInfo.CREATOR);
if (mShortcutInfo == null) {
mPackageName = parcel.readString();
mClassName = parcel.readString();
mUser = UserHandle.of(parcel.readInt());
} else {
mPackageName = mShortcutInfo.getPackage();
mUser = mShortcutInfo.getUserHandle();
mClassName = null;
}
mClassName = parcel.readString();
mRank = parcel.readInt();
}
@@ -129,10 +138,31 @@ public final class AppTarget implements Parcelable {
mRank = rank;
}
/**
* Returns the rank for the target.
*/
public int getRank() {
return mRank;
}
@Override
public boolean equals(Object o) {
if (!getClass().equals(o != null ? o.getClass() : null)) return false;
AppTarget other = (AppTarget) o;
boolean sameClassName = (mClassName == null && other.mClassName == null)
|| (mClassName != null && mClassName.equals(other.mClassName));
boolean sameShortcutInfo = (mShortcutInfo == null && other.mShortcutInfo == null)
|| (mShortcutInfo != null && other.mShortcutInfo != null
&& mShortcutInfo.getId() == other.mShortcutInfo.getId());
return mId.equals(other.mId)
&& mPackageName.equals(other.mPackageName)
&& sameClassName
&& mUser.equals(other.mUser)
&& sameShortcutInfo
&& mRank == other.mRank;
}
@Override
public int describeContents() {
return 0;
@@ -144,9 +174,9 @@ public final class AppTarget implements Parcelable {
dest.writeTypedObject(mShortcutInfo, flags);
if (mShortcutInfo == null) {
dest.writeString(mPackageName);
dest.writeString(mClassName);
dest.writeInt(mUser.getIdentifier());
}
dest.writeString(mClassName);
dest.writeInt(mRank);
}

View File

@@ -19,6 +19,7 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.os.Parcel;
import android.os.Parcelable;
@@ -30,6 +31,7 @@ import java.lang.annotation.RetentionPolicy;
* @hide
*/
@SystemApi
@TestApi
public final class AppTargetEvent implements Parcelable {
/**
@@ -95,6 +97,16 @@ public final class AppTargetEvent implements Parcelable {
return mAction;
}
@Override
public boolean equals(Object o) {
if (!getClass().equals(o != null ? o.getClass() : null)) return false;
AppTargetEvent other = (AppTargetEvent) o;
return mTarget.equals(other.mTarget)
&& mLocation.equals(other.mLocation)
&& mAction == other.mAction;
}
@Override
public int describeContents() {
return 0;
@@ -126,6 +138,7 @@ public final class AppTargetEvent implements Parcelable {
* @hide
*/
@SystemApi
@TestApi
public static final class Builder {
private AppTarget mTarget;
private String mLocation;

View File

@@ -17,6 +17,7 @@ package android.app.prediction;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.os.Parcel;
import android.os.Parcelable;
@@ -25,14 +26,19 @@ import android.os.Parcelable;
* @hide
*/
@SystemApi
@TestApi
public final class AppTargetId implements Parcelable {
@NonNull
private final String mId;
/**
* TODO(b/123591863): Add java docs
*
* @hide
*/
@SystemApi
@TestApi
public AppTargetId(@NonNull String id) {
mId = id;
}

View File

@@ -21,6 +21,7 @@ import android.annotation.CallSuper;
import android.annotation.MainThread;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.app.Service;
import android.app.prediction.AppPredictionContext;
import android.app.prediction.AppPredictionSessionId;
@@ -49,6 +50,7 @@ import java.util.function.Consumer;
* @hide
*/
@SystemApi
@TestApi
public abstract class AppPredictionService extends Service {
private static final String TAG = "AppPredictionService";
@@ -140,6 +142,7 @@ public abstract class AppPredictionService extends Service {
@Override
public final IBinder onBind(Intent intent) {
// TODO(b/111701043): Verify that the action is valid
return mInterface.asBinder();
}
@@ -228,6 +231,7 @@ public abstract class AppPredictionService extends Service {
public void onStopPredictionUpdates() {}
private void doRequestPredictionUpdate(@NonNull AppPredictionSessionId sessionId) {
// Just an optimization, if there are no callbacks, then don't bother notifying the service
final ArrayList<CallbackWrapper> callbacks = mSessionCallbacks.get(sessionId);
if (callbacks != null && !callbacks.isEmpty()) {
onRequestPredictionUpdate(sessionId);