Home | History | Annotate | Download | only in allapps
      1 /*
      2  * Copyright (C) 2018 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.allapps;
     17 
     18 import android.view.View;
     19 import android.view.ViewGroup;
     20 
     21 import com.android.launcher3.AppInfo;
     22 import com.android.launcher3.BubbleTextView;
     23 import com.android.launcher3.ItemInfo;
     24 import com.android.launcher3.PromiseAppInfo;
     25 import com.android.launcher3.util.ComponentKey;
     26 import com.android.launcher3.util.PackageUserKey;
     27 
     28 import java.util.ArrayList;
     29 import java.util.Collection;
     30 import java.util.HashMap;
     31 import java.util.List;
     32 import java.util.Set;
     33 
     34 /**
     35  * A utility class to maintain the collection of all apps.
     36  */
     37 public class AllAppsStore {
     38 
     39     private PackageUserKey mTempKey = new PackageUserKey(null, null);
     40     private final HashMap<ComponentKey, AppInfo> mComponentToAppMap = new HashMap<>();
     41     private final List<OnUpdateListener> mUpdateListeners = new ArrayList<>();
     42     private final ArrayList<ViewGroup> mIconContainers = new ArrayList<>();
     43 
     44     private boolean mDeferUpdates = false;
     45     private boolean mUpdatePending = false;
     46 
     47     public Collection<AppInfo> getApps() {
     48         return mComponentToAppMap.values();
     49     }
     50 
     51     /**
     52      * Sets the current set of apps.
     53      */
     54     public void setApps(List<AppInfo> apps) {
     55         mComponentToAppMap.clear();
     56         addOrUpdateApps(apps);
     57     }
     58 
     59     public AppInfo getApp(ComponentKey key) {
     60         return mComponentToAppMap.get(key);
     61     }
     62 
     63     public void setDeferUpdates(boolean deferUpdates) {
     64         if (mDeferUpdates != deferUpdates) {
     65             mDeferUpdates = deferUpdates;
     66 
     67             if (!mDeferUpdates && mUpdatePending) {
     68                 notifyUpdate();
     69                 mUpdatePending = false;
     70             }
     71         }
     72     }
     73 
     74     /**
     75      * Adds or updates existing apps in the list
     76      */
     77     public void addOrUpdateApps(List<AppInfo> apps) {
     78         for (AppInfo app : apps) {
     79             mComponentToAppMap.put(app.toComponentKey(), app);
     80         }
     81         notifyUpdate();
     82     }
     83 
     84     /**
     85      * Removes some apps from the list.
     86      */
     87     public void removeApps(List<AppInfo> apps) {
     88         for (AppInfo app : apps) {
     89             mComponentToAppMap.remove(app.toComponentKey());
     90         }
     91         notifyUpdate();
     92     }
     93 
     94 
     95     private void notifyUpdate() {
     96         if (mDeferUpdates) {
     97             mUpdatePending = true;
     98             return;
     99         }
    100         int count = mUpdateListeners.size();
    101         for (int i = 0; i < count; i++) {
    102             mUpdateListeners.get(i).onAppsUpdated();
    103         }
    104     }
    105 
    106     public void addUpdateListener(OnUpdateListener listener) {
    107         mUpdateListeners.add(listener);
    108     }
    109 
    110     public void removeUpdateListener(OnUpdateListener listener) {
    111         mUpdateListeners.remove(listener);
    112     }
    113 
    114     public void registerIconContainer(ViewGroup container) {
    115         if (container != null) {
    116             mIconContainers.add(container);
    117         }
    118     }
    119 
    120     public void unregisterIconContainer(ViewGroup container) {
    121         mIconContainers.remove(container);
    122     }
    123 
    124     public void updateIconBadges(Set<PackageUserKey> updatedBadges) {
    125         updateAllIcons((child) -> {
    126             if (child.getTag() instanceof ItemInfo) {
    127                 ItemInfo info = (ItemInfo) child.getTag();
    128                 if (mTempKey.updateFromItemInfo(info) && updatedBadges.contains(mTempKey)) {
    129                     child.applyBadgeState(info, true /* animate */);
    130                 }
    131             }
    132         });
    133     }
    134 
    135     public void updatePromiseAppProgress(PromiseAppInfo app) {
    136         updateAllIcons((child) -> {
    137             if (child.getTag() == app) {
    138                 child.applyProgressLevel(app.level);
    139             }
    140         });
    141     }
    142 
    143     private void updateAllIcons(IconAction action) {
    144         for (int i = mIconContainers.size() - 1; i >= 0; i--) {
    145             ViewGroup parent = mIconContainers.get(i);
    146             int childCount = parent.getChildCount();
    147 
    148             for (int j = 0; j < childCount; j++) {
    149                 View child = parent.getChildAt(j);
    150                 if (child instanceof BubbleTextView) {
    151                     action.apply((BubbleTextView) child);
    152                 }
    153             }
    154         }
    155     }
    156 
    157     public interface OnUpdateListener {
    158         void onAppsUpdated();
    159     }
    160 
    161     public interface IconAction {
    162         void apply(BubbleTextView icon);
    163     }
    164 }
    165