| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.app.prediction; |
| 17 |
|
| 18 |
import android.annotation.CallbackExecutor; |
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.SystemApi; |
| 22 |
import android.annotation.TestApi; |
| 23 |
import android.app.prediction.IPredictionCallback.Stub; |
| 24 |
import android.content.Context; |
| 25 |
import android.content.pm.ParceledListSlice; |
| 26 |
import android.os.Binder; |
| 27 |
import android.os.IBinder; |
| 28 |
import android.os.RemoteException; |
| 29 |
import android.os.ServiceManager; |
| 30 |
import android.util.ArrayMap; |
| 31 |
import android.util.Log; |
| 32 |
|
| 33 |
import com.android.internal.annotations.GuardedBy; |
| 34 |
|
| 35 |
import dalvik.system.CloseGuard; |
| 36 |
|
| 37 |
import java.util.List; |
| 38 |
import java.util.UUID; |
| 39 |
import java.util.concurrent.Executor; |
| 40 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 41 |
import java.util.function.Consumer; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
@SystemApi |
| 75 |
public final class AppPredictor { |
| 76 |
|
| 77 |
private static final String TAG = AppPredictor.class.getSimpleName(); |
| 78 |
|
| 79 |
private final IPredictionManager mPredictionManager; |
| 80 |
private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 81 |
private final AtomicBoolean mIsClosed = new AtomicBoolean(false); |
| 82 |
|
| 83 |
private final AppPredictionSessionId mSessionId; |
| 84 |
@GuardedBy("itself") |
| 85 |
private final ArrayMap<Callback, CallbackWrapper> mRegisteredCallbacks = new ArrayMap<>(); |
| 86 |
|
| 87 |
private final IBinder mToken = new Binder(); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
AppPredictor(@NonNull Context context, @NonNull AppPredictionContext predictionContext) { |
| 99 |
IBinder b = ServiceManager.getService(Context.APP_PREDICTION_SERVICE); |
| 100 |
mPredictionManager = IPredictionManager.Stub.asInterface(b); |
| 101 |
mSessionId = new AppPredictionSessionId( |
| 102 |
context.getPackageName() + ":" + UUID.randomUUID(), context.getUserId()); |
| 103 |
try { |
| 104 |
mPredictionManager.createPredictionSession(predictionContext, mSessionId, mToken); |
| 105 |
} catch (RemoteException e) { |
| 106 |
Log.e(TAG, "Failed to create predictor", e); |
| 107 |
e.rethrowAsRuntimeException(); |
| 108 |
} |
| 109 |
|
| 110 |
mCloseGuard.open("AppPredictor.close"); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public void notifyAppTargetEvent(@NonNull AppTargetEvent event) { |
| 119 |
if (mIsClosed.get()) { |
| 120 |
throw new IllegalStateException("This client has already been destroyed."); |
| 121 |
} |
| 122 |
|
| 123 |
try { |
| 124 |
mPredictionManager.notifyAppTargetEvent(mSessionId, event); |
| 125 |
} catch (RemoteException e) { |
| 126 |
Log.e(TAG, "Failed to notify app target event", e); |
| 127 |
e.rethrowAsRuntimeException(); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public void notifyLaunchLocationShown(@NonNull String launchLocation, |
| 138 |
@NonNull List<AppTargetId> targetIds) { |
| 139 |
if (mIsClosed.get()) { |
| 140 |
throw new IllegalStateException("This client has already been destroyed."); |
| 141 |
} |
| 142 |
|
| 143 |
try { |
| 144 |
mPredictionManager.notifyLaunchLocationShown(mSessionId, launchLocation, |
| 145 |
new ParceledListSlice<>(targetIds)); |
| 146 |
} catch (RemoteException e) { |
| 147 |
Log.e(TAG, "Failed to notify location shown event", e); |
| 148 |
e.rethrowAsRuntimeException(); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public void registerPredictionUpdates(@NonNull @CallbackExecutor Executor callbackExecutor, |
| 162 |
@NonNull AppPredictor.Callback callback) { |
| 163 |
synchronized (mRegisteredCallbacks) { |
| 164 |
registerPredictionUpdatesLocked(callbackExecutor, callback); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
@GuardedBy("mRegisteredCallbacks") |
| 169 |
private void registerPredictionUpdatesLocked( |
| 170 |
@NonNull @CallbackExecutor Executor callbackExecutor, |
| 171 |
@NonNull AppPredictor.Callback callback) { |
| 172 |
if (mIsClosed.get()) { |
| 173 |
throw new IllegalStateException("This client has already been destroyed."); |
| 174 |
} |
| 175 |
|
| 176 |
if (mRegisteredCallbacks.containsKey(callback)) { |
| 177 |
|
| 178 |
return; |
| 179 |
} |
| 180 |
try { |
| 181 |
final CallbackWrapper callbackWrapper = new CallbackWrapper(callbackExecutor, |
| 182 |
callback::onTargetsAvailable); |
| 183 |
mPredictionManager.registerPredictionUpdates(mSessionId, callbackWrapper); |
| 184 |
mRegisteredCallbacks.put(callback, callbackWrapper); |
| 185 |
} catch (RemoteException e) { |
| 186 |
Log.e(TAG, "Failed to register for prediction updates", e); |
| 187 |
e.rethrowAsRuntimeException(); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public void unregisterPredictionUpdates(@NonNull AppPredictor.Callback callback) { |
| 200 |
synchronized (mRegisteredCallbacks) { |
| 201 |
unregisterPredictionUpdatesLocked(callback); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
@GuardedBy("mRegisteredCallbacks") |
| 206 |
private void unregisterPredictionUpdatesLocked(@NonNull AppPredictor.Callback callback) { |
| 207 |
if (mIsClosed.get()) { |
| 208 |
throw new IllegalStateException("This client has already been destroyed."); |
| 209 |
} |
| 210 |
|
| 211 |
if (!mRegisteredCallbacks.containsKey(callback)) { |
| 212 |
|
| 213 |
return; |
| 214 |
} |
| 215 |
try { |
| 216 |
final CallbackWrapper callbackWrapper = mRegisteredCallbacks.remove(callback); |
| 217 |
mPredictionManager.unregisterPredictionUpdates(mSessionId, callbackWrapper); |
| 218 |
} catch (RemoteException e) { |
| 219 |
Log.e(TAG, "Failed to unregister for prediction updates", e); |
| 220 |
e.rethrowAsRuntimeException(); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public void requestPredictionUpdate() { |
| 231 |
if (mIsClosed.get()) { |
| 232 |
throw new IllegalStateException("This client has already been destroyed."); |
| 233 |
} |
| 234 |
|
| 235 |
try { |
| 236 |
mPredictionManager.requestPredictionUpdate(mSessionId); |
| 237 |
} catch (RemoteException e) { |
| 238 |
Log.e(TAG, "Failed to request prediction update", e); |
| 239 |
e.rethrowAsRuntimeException(); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
@Nullable |
| 252 |
public void sortTargets(@NonNull List<AppTarget> targets, |
| 253 |
@NonNull Executor callbackExecutor, @NonNull Consumer<List<AppTarget>> callback) { |
| 254 |
if (mIsClosed.get()) { |
| 255 |
throw new IllegalStateException("This client has already been destroyed."); |
| 256 |
} |
| 257 |
|
| 258 |
try { |
| 259 |
mPredictionManager.sortAppTargets(mSessionId, new ParceledListSlice<>(targets), |
| 260 |
new CallbackWrapper(callbackExecutor, callback)); |
| 261 |
} catch (RemoteException e) { |
| 262 |
Log.e(TAG, "Failed to sort targets", e); |
| 263 |
e.rethrowAsRuntimeException(); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public void destroy() { |
| 272 |
if (!mIsClosed.getAndSet(true)) { |
| 273 |
mCloseGuard.close(); |
| 274 |
|
| 275 |
synchronized (mRegisteredCallbacks) { |
| 276 |
destroySessionLocked(); |
| 277 |
} |
| 278 |
} else { |
| 279 |
throw new IllegalStateException("This client has already been destroyed."); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
@GuardedBy("mRegisteredCallbacks") |
| 284 |
private void destroySessionLocked() { |
| 285 |
try { |
| 286 |
mPredictionManager.onDestroyPredictionSession(mSessionId); |
| 287 |
} catch (RemoteException e) { |
| 288 |
Log.e(TAG, "Failed to notify app target event", e); |
| 289 |
e.rethrowAsRuntimeException(); |
| 290 |
} |
| 291 |
mRegisteredCallbacks.clear(); |
| 292 |
} |
| 293 |
|
| 294 |
@Override |
| 295 |
protected void finalize() throws Throwable { |
| 296 |
try { |
| 297 |
if (mCloseGuard != null) { |
| 298 |
mCloseGuard.warnIfOpen(); |
| 299 |
} |
| 300 |
if (!mIsClosed.get()) { |
| 301 |
destroy(); |
| 302 |
} |
| 303 |
} finally { |
| 304 |
super.finalize(); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
@TestApi |
| 314 |
public AppPredictionSessionId getSessionId() { |
| 315 |
return mSessionId; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public interface Callback { |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
void onTargetsAvailable(@NonNull List<AppTarget> targets); |
| 328 |
} |
| 329 |
|
| 330 |
static class CallbackWrapper extends Stub { |
| 331 |
|
| 332 |
private final Consumer<List<AppTarget>> mCallback; |
| 333 |
private final Executor mExecutor; |
| 334 |
|
| 335 |
CallbackWrapper(@NonNull Executor callbackExecutor, |
| 336 |
@NonNull Consumer<List<AppTarget>> callback) { |
| 337 |
mCallback = callback; |
| 338 |
mExecutor = callbackExecutor; |
| 339 |
} |
| 340 |
|
| 341 |
@Override |
| 342 |
public void onResult(ParceledListSlice result) { |
| 343 |
final long identity = Binder.clearCallingIdentity(); |
| 344 |
try { |
| 345 |
mExecutor.execute(() -> mCallback.accept(result.getList())); |
| 346 |
} finally { |
| 347 |
Binder.restoreCallingIdentity(identity); |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
} |