Home | History | Annotate | Download | only in shortcuts
      1 /*
      2  * Copyright (C) 2016 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.shortcuts;
     18 
     19 import android.annotation.TargetApi;
     20 import android.os.Build;
     21 import android.os.UserHandle;
     22 import android.util.ArrayMap;
     23 import android.util.LruCache;
     24 import java.util.List;
     25 
     26 /**
     27  * Loads {@link ShortcutInfoCompat}s on demand (e.g. when launcher
     28  * loads for pinned shortcuts and on long-press for dynamic shortcuts), and caches them
     29  * for handful of apps in an LruCache while launcher lives.
     30  */
     31 @TargetApi(Build.VERSION_CODES.N)
     32 public class ShortcutCache {
     33     private static final int CACHE_SIZE = 30; // Max number shortcuts we cache.
     34 
     35     private final LruCache<ShortcutKey, ShortcutInfoCompat> mCachedShortcuts;
     36     // We always keep pinned shortcuts in the cache.
     37     private final ArrayMap<ShortcutKey, ShortcutInfoCompat> mPinnedShortcuts;
     38 
     39     public ShortcutCache() {
     40         mCachedShortcuts = new LruCache<>(CACHE_SIZE);
     41         mPinnedShortcuts = new ArrayMap<>();
     42     }
     43 
     44     /**
     45      * Removes shortcuts from the cache when shortcuts change for a given package.
     46      *
     47      * Returns a map of ids to their evicted shortcuts.
     48      *
     49      * @see android.content.pm.LauncherApps.Callback#onShortcutsChanged(String, List, UserHandle).
     50      */
     51     public void removeShortcuts(List<ShortcutInfoCompat> shortcuts) {
     52         for (ShortcutInfoCompat shortcut : shortcuts) {
     53             ShortcutKey key = ShortcutKey.fromInfo(shortcut);
     54             mCachedShortcuts.remove(key);
     55             mPinnedShortcuts.remove(key);
     56         }
     57     }
     58 
     59     public ShortcutInfoCompat get(ShortcutKey key) {
     60         if (mPinnedShortcuts.containsKey(key)) {
     61             return mPinnedShortcuts.get(key);
     62         }
     63         return mCachedShortcuts.get(key);
     64     }
     65 
     66     public void put(ShortcutKey key, ShortcutInfoCompat shortcut) {
     67         if (shortcut.isPinned()) {
     68             mPinnedShortcuts.put(key, shortcut);
     69         } else {
     70             mCachedShortcuts.put(key, shortcut);
     71         }
     72     }
     73 }
     74