• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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 
17 package com.android.launcher3.util
18 
19 import android.view.View
20 import com.android.launcher3.BubbleTextView
21 import com.android.launcher3.apppairs.AppPairIcon
22 import com.android.launcher3.folder.Folder
23 import com.android.launcher3.folder.FolderIcon
24 import com.android.launcher3.model.data.AppPairInfo
25 import com.android.launcher3.model.data.FolderInfo
26 import com.android.launcher3.model.data.ItemInfo
27 import com.android.launcher3.model.data.WorkspaceItemInfo
28 import com.android.launcher3.util.LauncherBindableItemsContainer.ItemOperator
29 import com.android.launcher3.views.ActivityContext
30 import com.android.launcher3.widget.PendingAppWidgetHostView
31 import java.util.function.Predicate
32 
33 /** Interface representing a container which can bind Launcher items with some utility methods */
34 interface LauncherBindableItemsContainer {
35 
36     /**
37      * Called to update workspace items as a result of {@link
38      * com.android.launcher3.model.BgDataModel.Callbacks#bindItemsUpdated(Set)}
39      */
40     fun updateContainerItems(updates: Set<ItemInfo>, context: ActivityContext) {
41         val op = ItemOperator { info, v ->
42             when {
43                 v is BubbleTextView && info is WorkspaceItemInfo && updates.contains(info) ->
44                     v.applyFromWorkspaceItem(info)
45                 v is FolderIcon && info is FolderInfo -> v.updatePreviewItems(updates::contains)
46                 v is AppPairIcon && info is AppPairInfo ->
47                     v.maybeRedrawForWorkspaceUpdate(updates::contains)
48                 v is PendingAppWidgetHostView && updates.contains(info) -> {
49                     v.applyState()
50                     v.postProviderAvailabilityCheck()
51                 }
52             }
53 
54             // Iterate all items
55             false
56         }
57 
58         mapOverItems(op)
59         Folder.getOpen(context)?.mapOverItems(op)
60     }
61 
62     /** Returns the first view, matching the [op] */
63     @Deprecated("Use mapOverItems instead", ReplaceWith("mapOverItems(op)"))
64     fun getFirstMatch(op: ItemOperator): View? = mapOverItems(op)
65 
66     /** Finds the first icon to match one of the given matchers, from highest to lowest priority. */
67     fun getFirstMatch(vararg matchers: Predicate<ItemInfo>): View? =
68         matchers.firstNotNullOfOrNull { mapOverItems { info, _ -> info != null && it.test(info) } }
69 
70     fun getViewByItemId(id: Int): View? = mapOverItems { info, _ -> info != null && info.id == id }
71 
72     /**
73      * Map the [op] over the shortcuts and widgets. Once we found the first view which matches, we
74      * will stop the iteration and return that view.
75      */
76     fun mapOverItems(op: ItemOperator): View?
77 
78     fun interface ItemOperator {
79 
80         /**
81          * Process the next itemInfo, possibly with side-effect on the next item.
82          *
83          * @param info info for the shortcut
84          * @param view view for the shortcut
85          * @return true if done, false to continue the map
86          */
87         fun evaluate(info: ItemInfo?, view: View): Boolean
88     }
89 }
90