1 /*
2  * Copyright (C) 2019 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.icons;
18 
19 import static com.android.launcher3.model.WidgetsModel.GO_DISABLE_WIDGETS;
20 
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.pm.LauncherApps;
24 import android.content.pm.PackageInfo;
25 import android.content.pm.ShortcutInfo;
26 import android.graphics.drawable.Drawable;
27 import android.os.UserHandle;
28 import android.text.TextUtils;
29 import android.util.Log;
30 
31 import androidx.annotation.NonNull;
32 
33 import com.android.launcher3.LauncherAppState;
34 import com.android.launcher3.config.FeatureFlags;
35 import com.android.launcher3.icons.cache.CachingLogic;
36 import com.android.launcher3.shortcuts.ShortcutKey;
37 import com.android.launcher3.util.Themes;
38 
39 /**
40  * Caching logic for shortcuts.
41  */
42 public class ShortcutCachingLogic implements CachingLogic<ShortcutInfo> {
43 
44     private static final String TAG = "ShortcutCachingLogic";
45 
46     @Override
getComponent(ShortcutInfo info)47     public ComponentName getComponent(ShortcutInfo info) {
48         return ShortcutKey.fromInfo(info).componentName;
49     }
50 
51     @Override
getUser(ShortcutInfo info)52     public UserHandle getUser(ShortcutInfo info) {
53         return info.getUserHandle();
54     }
55 
56     @Override
getLabel(ShortcutInfo info)57     public CharSequence getLabel(ShortcutInfo info) {
58         return info.getShortLabel();
59     }
60 
61     @Override
getDescription(ShortcutInfo object, CharSequence fallback)62     public CharSequence getDescription(ShortcutInfo object, CharSequence fallback) {
63         CharSequence label = object.getLongLabel();
64         return TextUtils.isEmpty(label) ? fallback : label;
65     }
66 
67     @NonNull
68     @Override
loadIcon(Context context, ShortcutInfo info)69     public BitmapInfo loadIcon(Context context, ShortcutInfo info) {
70         try (LauncherIcons li = LauncherIcons.obtain(context)) {
71             Drawable unbadgedDrawable = ShortcutCachingLogic.getIcon(
72                     context, info, LauncherAppState.getIDP(context).fillResIconDpi);
73             if (unbadgedDrawable == null) return BitmapInfo.LOW_RES_INFO;
74             return new BitmapInfo(li.createScaledBitmapWithoutShadow(
75                     unbadgedDrawable, 0), Themes.getColorAccent(context));
76         }
77     }
78 
79     @Override
getLastUpdatedTime(ShortcutInfo shortcutInfo, PackageInfo info)80     public long getLastUpdatedTime(ShortcutInfo shortcutInfo, PackageInfo info) {
81         if (shortcutInfo == null || !FeatureFlags.ENABLE_DEEP_SHORTCUT_ICON_CACHE.get()) {
82             return info.lastUpdateTime;
83         }
84         return Math.max(shortcutInfo.getLastChangedTimestamp(), info.lastUpdateTime);
85     }
86 
87     @Override
addToMemCache()88     public boolean addToMemCache() {
89         return false;
90     }
91 
92     /**
93      * Similar to {@link LauncherApps#getShortcutIconDrawable(ShortcutInfo, int)} with additional
94      * Launcher specific checks
95      */
getIcon(Context context, ShortcutInfo shortcutInfo, int density)96     public static Drawable getIcon(Context context, ShortcutInfo shortcutInfo, int density) {
97         if (GO_DISABLE_WIDGETS) {
98             return null;
99         }
100         try {
101             return context.getSystemService(LauncherApps.class)
102                     .getShortcutIconDrawable(shortcutInfo, density);
103         } catch (SecurityException | IllegalStateException e) {
104             Log.e(TAG, "Failed to get shortcut icon", e);
105             return null;
106         }
107     }
108 }
109