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