1 /* 2 * Copyright (C) 2020 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 com.android.launcher3.hybridhotseat; 17 18 import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION; 19 import static com.android.launcher3.model.PredictionHelper.getAppTargetFromItemInfo; 20 import static com.android.launcher3.model.PredictionHelper.wrapAppTargetWithItemLocation; 21 22 import android.app.prediction.AppTarget; 23 import android.app.prediction.AppTargetEvent; 24 import android.content.Context; 25 import android.os.Bundle; 26 27 import com.android.launcher3.model.BgDataModel; 28 import com.android.launcher3.model.BgDataModel.FixedContainerItems; 29 import com.android.launcher3.model.PredictionHelper; 30 import com.android.launcher3.model.data.ItemInfo; 31 32 import java.util.ArrayList; 33 import java.util.Objects; 34 import java.util.stream.Collectors; 35 36 /** 37 * Model helper for app predictions in workspace 38 */ 39 public class HotseatPredictionModel { 40 private static final String BUNDLE_KEY_PIN_EVENTS = "pin_events"; 41 private static final String BUNDLE_KEY_CURRENT_ITEMS = "current_items"; 42 43 /** 44 * Creates and returns bundle using workspace items 45 */ convertDataModelToAppTargetBundle(Context context, BgDataModel dataModel)46 public static Bundle convertDataModelToAppTargetBundle(Context context, BgDataModel dataModel) { 47 Bundle bundle = new Bundle(); 48 ArrayList<AppTargetEvent> events = dataModel.itemsIdMap 49 .stream() 50 .filter(PredictionHelper::isTrackedForHotseatPrediction) 51 .map(item -> { 52 AppTarget target = getAppTargetFromItemInfo(context, item); 53 return target != null 54 ? wrapAppTargetWithItemLocation(target, AppTargetEvent.ACTION_PIN, item) 55 : null; 56 }) 57 .filter(Objects::nonNull) 58 .collect(Collectors.toCollection(ArrayList::new)); 59 60 ArrayList<AppTarget> currentTargets = new ArrayList<>(); 61 FixedContainerItems hotseatItems = dataModel.extraItems.get(CONTAINER_HOTSEAT_PREDICTION); 62 if (hotseatItems != null) { 63 for (ItemInfo itemInfo : hotseatItems.items) { 64 AppTarget target = getAppTargetFromItemInfo(context, itemInfo); 65 if (target != null) currentTargets.add(target); 66 } 67 } 68 bundle.putParcelableArrayList(BUNDLE_KEY_PIN_EVENTS, events); 69 bundle.putParcelableArrayList(BUNDLE_KEY_CURRENT_ITEMS, currentTargets); 70 return bundle; 71 } 72 } 73