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; 18 19 import android.annotation.UserIdInt; 20 import android.app.prediction.AppPredictionContext; 21 import android.app.prediction.AppTarget; 22 import android.app.prediction.IPredictionCallback; 23 import android.content.Context; 24 import android.content.pm.ParceledListSlice; 25 import android.os.RemoteCallbackList; 26 import android.os.RemoteException; 27 import android.util.Slog; 28 29 import com.android.server.people.data.DataManager; 30 import com.android.server.people.prediction.AppTargetPredictor; 31 32 import java.util.List; 33 34 /** Manages the information and callbacks in an app prediction request session. */ 35 class SessionInfo { 36 37 private static final String TAG = "SessionInfo"; 38 39 private final AppTargetPredictor mAppTargetPredictor; 40 private final RemoteCallbackList<IPredictionCallback> mCallbacks = 41 new RemoteCallbackList<>(); 42 SessionInfo(AppPredictionContext predictionContext, DataManager dataManager, @UserIdInt int callingUserId, Context context)43 SessionInfo(AppPredictionContext predictionContext, DataManager dataManager, 44 @UserIdInt int callingUserId, Context context) { 45 mAppTargetPredictor = AppTargetPredictor.create(predictionContext, 46 this::updatePredictions, dataManager, callingUserId, context); 47 } 48 addCallback(IPredictionCallback callback)49 void addCallback(IPredictionCallback callback) { 50 mCallbacks.register(callback); 51 } 52 removeCallback(IPredictionCallback callback)53 void removeCallback(IPredictionCallback callback) { 54 mCallbacks.unregister(callback); 55 } 56 getPredictor()57 AppTargetPredictor getPredictor() { 58 return mAppTargetPredictor; 59 } 60 onDestroy()61 void onDestroy() { 62 mCallbacks.kill(); 63 } 64 updatePredictions(List<AppTarget> targets)65 private void updatePredictions(List<AppTarget> targets) { 66 int callbackCount = mCallbacks.beginBroadcast(); 67 for (int i = 0; i < callbackCount; i++) { 68 try { 69 mCallbacks.getBroadcastItem(i).onResult(new ParceledListSlice<>(targets)); 70 } catch (RemoteException e) { 71 Slog.e(TAG, "Failed to calling callback" + e); 72 } 73 } 74 mCallbacks.finishBroadcast(); 75 } 76 } 77