1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package android.app.prediction; 17 18 import android.annotation.IntRange; 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SystemApi; 22 import android.content.Context; 23 import android.os.Bundle; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 27 import java.util.Objects; 28 29 /** 30 * Class that provides contextual information about the environment in which the app prediction is 31 * used, such as package name, UI in which the app targets are shown, and number of targets. 32 * 33 * @hide 34 */ 35 @SystemApi 36 public final class AppPredictionContext implements Parcelable { 37 38 private final int mPredictedTargetCount; 39 @NonNull 40 private final String mUiSurface; 41 @NonNull 42 private final String mPackageName; 43 @Nullable 44 private final Bundle mExtras; 45 AppPredictionContext(@onNull String uiSurface, int numPredictedTargets, @NonNull String packageName, @Nullable Bundle extras)46 private AppPredictionContext(@NonNull String uiSurface, int numPredictedTargets, 47 @NonNull String packageName, @Nullable Bundle extras) { 48 mUiSurface = uiSurface; 49 mPredictedTargetCount = numPredictedTargets; 50 mPackageName = packageName; 51 mExtras = extras; 52 } 53 AppPredictionContext(@onNull Parcel parcel)54 private AppPredictionContext(@NonNull Parcel parcel) { 55 mUiSurface = parcel.readString(); 56 mPredictedTargetCount = parcel.readInt(); 57 mPackageName = parcel.readString(); 58 mExtras = parcel.readBundle(); 59 } 60 61 /** 62 * Returns the UI surface of the prediction context. 63 */ 64 @NonNull getUiSurface()65 public String getUiSurface() { 66 return mUiSurface; 67 } 68 69 /** 70 * Returns the predicted target count 71 */ getPredictedTargetCount()72 public @IntRange(from = 0) int getPredictedTargetCount() { 73 return mPredictedTargetCount; 74 } 75 76 /** 77 * Returns the package name of the prediction context. 78 */ 79 @NonNull getPackageName()80 public String getPackageName() { 81 return mPackageName; 82 } 83 84 /** 85 * Returns the extras of the prediction context. 86 */ 87 @Nullable getExtras()88 public Bundle getExtras() { 89 return mExtras; 90 } 91 92 @Override equals(@ullable Object o)93 public boolean equals(@Nullable Object o) { 94 if (o == this) return true; 95 if (!getClass().equals(o != null ? o.getClass() : null)) return false; 96 97 AppPredictionContext other = (AppPredictionContext) o; 98 return mPredictedTargetCount == other.mPredictedTargetCount 99 && mUiSurface.equals(other.mUiSurface) 100 && mPackageName.equals(other.mPackageName); 101 } 102 103 @Override hashCode()104 public int hashCode() { 105 int hashCode = Objects.hash(mUiSurface, mPackageName); 106 hashCode = 31 * hashCode + mPredictedTargetCount; 107 return hashCode; 108 } 109 110 @Override describeContents()111 public int describeContents() { 112 return 0; 113 } 114 115 @Override writeToParcel(@onNull Parcel dest, int flags)116 public void writeToParcel(@NonNull Parcel dest, int flags) { 117 dest.writeString(mUiSurface); 118 dest.writeInt(mPredictedTargetCount); 119 dest.writeString(mPackageName); 120 dest.writeBundle(mExtras); 121 } 122 123 public static final @android.annotation.NonNull Parcelable.Creator<AppPredictionContext> CREATOR = 124 new Parcelable.Creator<AppPredictionContext>() { 125 public AppPredictionContext createFromParcel(Parcel parcel) { 126 return new AppPredictionContext(parcel); 127 } 128 129 public AppPredictionContext[] newArray(int size) { 130 return new AppPredictionContext[size]; 131 } 132 }; 133 134 /** 135 * A builder for app prediction contexts. 136 * @hide 137 */ 138 @SystemApi 139 public static final class Builder { 140 141 @NonNull 142 private final String mPackageName; 143 144 private int mPredictedTargetCount; 145 @Nullable 146 private String mUiSurface; 147 @Nullable 148 private Bundle mExtras; 149 150 /** 151 * @param context The {@link Context} of the prediction client. 152 * 153 * @hide 154 */ 155 @SystemApi Builder(@onNull Context context)156 public Builder(@NonNull Context context) { 157 mPackageName = context.getPackageName(); 158 } 159 160 161 /** 162 * Sets the number of prediction targets as a hint. 163 */ 164 @NonNull setPredictedTargetCount(@ntRangefrom = 0) int predictedTargetCount)165 public Builder setPredictedTargetCount(@IntRange(from = 0) int predictedTargetCount) { 166 mPredictedTargetCount = predictedTargetCount; 167 return this; 168 } 169 170 /** 171 * Sets the UI surface. 172 */ 173 @NonNull setUiSurface(@onNull String uiSurface)174 public Builder setUiSurface(@NonNull String uiSurface) { 175 mUiSurface = uiSurface; 176 return this; 177 } 178 179 /** 180 * Sets the extras. 181 */ 182 @NonNull setExtras(@ullable Bundle extras)183 public Builder setExtras(@Nullable Bundle extras) { 184 mExtras = extras; 185 return this; 186 } 187 188 /** 189 * Builds a new context instance. 190 */ 191 @NonNull build()192 public AppPredictionContext build() { 193 return new AppPredictionContext(mUiSurface, mPredictedTargetCount, mPackageName, 194 mExtras); 195 } 196 } 197 } 198