• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.util;
17 
18 import android.graphics.drawable.Drawable;
19 import android.view.View;
20 
21 import com.android.launcher3.BubbleTextView;
22 import com.android.launcher3.folder.Folder;
23 import com.android.launcher3.folder.FolderIcon;
24 import com.android.launcher3.graphics.PreloadIconDrawable;
25 import com.android.launcher3.model.data.FolderInfo;
26 import com.android.launcher3.model.data.ItemInfo;
27 import com.android.launcher3.model.data.LauncherAppWidgetInfo;
28 import com.android.launcher3.model.data.WorkspaceItemInfo;
29 import com.android.launcher3.views.ActivityContext;
30 import com.android.launcher3.widget.PendingAppWidgetHostView;
31 
32 import java.util.HashSet;
33 import java.util.List;
34 
35 /**
36  * Interface representing a container which can bind Launcher items with some utility methods
37  */
38 public interface LauncherBindableItemsContainer {
39 
40     /**
41      * Called to update workspace items as a result of
42      * {@link com.android.launcher3.model.BgDataModel.Callbacks#bindWorkspaceItemsChanged(List)}
43      */
updateWorkspaceItems(List<WorkspaceItemInfo> shortcuts, ActivityContext context)44     default void updateWorkspaceItems(List<WorkspaceItemInfo> shortcuts, ActivityContext context) {
45         final HashSet<WorkspaceItemInfo> updates = new HashSet<>(shortcuts);
46         ItemOperator op = (info, v) -> {
47             if (v instanceof BubbleTextView && updates.contains(info)) {
48                 WorkspaceItemInfo si = (WorkspaceItemInfo) info;
49                 BubbleTextView shortcut = (BubbleTextView) v;
50                 Drawable oldIcon = shortcut.getIcon();
51                 boolean oldPromiseState = (oldIcon instanceof PreloadIconDrawable)
52                         && ((PreloadIconDrawable) oldIcon).hasNotCompleted();
53                 shortcut.applyFromWorkspaceItem(
54                         si,
55                         si.isPromise() != oldPromiseState
56                                 && oldIcon instanceof PreloadIconDrawable
57                                 ? (PreloadIconDrawable) oldIcon
58                                 : null);
59             } else if (info instanceof FolderInfo && v instanceof FolderIcon) {
60                 ((FolderIcon) v).updatePreviewItems(updates::contains);
61             }
62 
63             // Iterate all items
64             return false;
65         };
66 
67         mapOverItems(op);
68         Folder openFolder = Folder.getOpen(context);
69         if (openFolder != null) {
70             openFolder.iterateOverItems(op);
71         }
72     }
73 
74     /**
75      * Called to update restored items as a result of
76      * {@link com.android.launcher3.model.BgDataModel.Callbacks#bindRestoreItemsChange(HashSet)}}
77      */
updateRestoreItems(final HashSet<ItemInfo> updates, ActivityContext context)78     default void updateRestoreItems(final HashSet<ItemInfo> updates, ActivityContext context) {
79         ItemOperator op = (info, v) -> {
80             if (info instanceof WorkspaceItemInfo && v instanceof BubbleTextView
81                     && updates.contains(info)) {
82                 ((BubbleTextView) v).applyLoadingState(null);
83             } else if (v instanceof PendingAppWidgetHostView
84                     && info instanceof LauncherAppWidgetInfo
85                     && updates.contains(info)) {
86                 ((PendingAppWidgetHostView) v).applyState();
87             } else if (v instanceof FolderIcon && info instanceof FolderInfo) {
88                 ((FolderIcon) v).updatePreviewItems(updates::contains);
89             }
90             // process all the shortcuts
91             return false;
92         };
93 
94         mapOverItems(op);
95         Folder folder = Folder.getOpen(context);
96         if (folder != null) {
97             folder.iterateOverItems(op);
98         }
99     }
100 
101     /**
102      * Map the operator over the shortcuts and widgets.
103      *
104      * @param op the operator to map over the shortcuts
105      */
mapOverItems(ItemOperator op)106     void mapOverItems(ItemOperator op);
107 
108     interface ItemOperator {
109         /**
110          * Process the next itemInfo, possibly with side-effect on the next item.
111          *
112          * @param info info for the shortcut
113          * @param view view for the shortcut
114          * @return true if done, false to continue the map
115          */
evaluate(ItemInfo info, View view)116         boolean evaluate(ItemInfo info, View view);
117     }
118 }
119