1 /* 2 * Copyright (C) 2019 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 17 package com.android.server.people.prediction; 18 19 import android.annotation.MainThread; 20 import android.annotation.NonNull; 21 import android.annotation.UserIdInt; 22 import android.annotation.WorkerThread; 23 import android.app.prediction.AppPredictionContext; 24 import android.app.prediction.AppTarget; 25 import android.app.prediction.AppTargetEvent; 26 import android.app.prediction.AppTargetId; 27 28 import com.android.internal.annotations.VisibleForTesting; 29 import com.android.server.people.data.DataManager; 30 31 import java.util.List; 32 import java.util.concurrent.ExecutorService; 33 import java.util.concurrent.Executors; 34 import java.util.function.Consumer; 35 36 /** 37 * Predictor that predicts the {@link AppTarget} the user is most likely to open. 38 */ 39 public class AppTargetPredictor { 40 41 private static final String UI_SURFACE_SHARE = "share"; 42 43 /** Creates a {@link AppTargetPredictor} instance based on the prediction context. */ create(@onNull AppPredictionContext predictionContext, @NonNull Consumer<List<AppTarget>> updatePredictionsMethod, @NonNull DataManager dataManager, @UserIdInt int callingUserId)44 public static AppTargetPredictor create(@NonNull AppPredictionContext predictionContext, 45 @NonNull Consumer<List<AppTarget>> updatePredictionsMethod, 46 @NonNull DataManager dataManager, @UserIdInt int callingUserId) { 47 if (UI_SURFACE_SHARE.equals(predictionContext.getUiSurface())) { 48 return new ShareTargetPredictor( 49 predictionContext, updatePredictionsMethod, dataManager, callingUserId); 50 } 51 return new AppTargetPredictor( 52 predictionContext, updatePredictionsMethod, dataManager, callingUserId); 53 } 54 55 private final AppPredictionContext mPredictionContext; 56 private final Consumer<List<AppTarget>> mUpdatePredictionsMethod; 57 private final DataManager mDataManager; 58 final int mCallingUserId; 59 private final ExecutorService mCallbackExecutor; 60 AppTargetPredictor(@onNull AppPredictionContext predictionContext, @NonNull Consumer<List<AppTarget>> updatePredictionsMethod, @NonNull DataManager dataManager, @UserIdInt int callingUserId)61 AppTargetPredictor(@NonNull AppPredictionContext predictionContext, 62 @NonNull Consumer<List<AppTarget>> updatePredictionsMethod, 63 @NonNull DataManager dataManager, @UserIdInt int callingUserId) { 64 mPredictionContext = predictionContext; 65 mUpdatePredictionsMethod = updatePredictionsMethod; 66 mDataManager = dataManager; 67 mCallingUserId = callingUserId; 68 mCallbackExecutor = Executors.newSingleThreadExecutor(); 69 } 70 71 /** 72 * Called by the client app to indicate a target launch. 73 */ 74 @MainThread onAppTargetEvent(AppTargetEvent event)75 public void onAppTargetEvent(AppTargetEvent event) { 76 mCallbackExecutor.execute(() -> reportAppTargetEvent(event)); 77 } 78 79 /** 80 * Called by the client app to indicate a particular location has been shown to the user. 81 */ 82 @MainThread onLaunchLocationShown(String launchLocation, List<AppTargetId> targetIds)83 public void onLaunchLocationShown(String launchLocation, List<AppTargetId> targetIds) { 84 } 85 86 /** 87 * Called by the client app to request sorting of the provided targets based on the prediction 88 * ranking. 89 */ 90 @MainThread onSortAppTargets(List<AppTarget> targets, Consumer<List<AppTarget>> callback)91 public void onSortAppTargets(List<AppTarget> targets, Consumer<List<AppTarget>> callback) { 92 mCallbackExecutor.execute(() -> sortTargets(targets, callback)); 93 } 94 95 /** 96 * Called by the client app to request target predictions. 97 */ 98 @MainThread onRequestPredictionUpdate()99 public void onRequestPredictionUpdate() { 100 mCallbackExecutor.execute(this::predictTargets); 101 } 102 103 @VisibleForTesting getUpdatePredictionsMethod()104 public Consumer<List<AppTarget>> getUpdatePredictionsMethod() { 105 return mUpdatePredictionsMethod; 106 } 107 108 /** To be overridden by the subclass to report app target event. */ 109 @WorkerThread reportAppTargetEvent(AppTargetEvent event)110 void reportAppTargetEvent(AppTargetEvent event) { 111 } 112 113 /** To be overridden by the subclass to predict the targets. */ 114 @WorkerThread predictTargets()115 void predictTargets() { 116 } 117 118 /** 119 * To be overridden by the subclass to sort the provided targets based on the prediction 120 * ranking. 121 */ 122 @WorkerThread sortTargets(List<AppTarget> targets, Consumer<List<AppTarget>> callback)123 void sortTargets(List<AppTarget> targets, Consumer<List<AppTarget>> callback) { 124 callback.accept(targets); 125 } 126 getPredictionContext()127 AppPredictionContext getPredictionContext() { 128 return mPredictionContext; 129 } 130 getDataManager()131 DataManager getDataManager() { 132 return mDataManager; 133 } 134 updatePredictions(List<AppTarget> targets)135 void updatePredictions(List<AppTarget> targets) { 136 mUpdatePredictionsMethod.accept(targets); 137 } 138 } 139