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.BuildConfig.WIDGETS_ENABLED;
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 import androidx.annotation.Nullable;
33 
34 import com.android.launcher3.LauncherAppState;
35 import com.android.launcher3.icons.BaseIconFactory.IconOptions;
36 import com.android.launcher3.icons.cache.CachingLogic;
37 import com.android.launcher3.shortcuts.ShortcutKey;
38 import com.android.launcher3.util.Themes;
39 
40 /**
41  * Caching logic for shortcuts.
42  */
43 public class ShortcutCachingLogic implements CachingLogic<ShortcutInfo> {
44 
45     private static final String TAG = "ShortcutCachingLogic";
46 
47     @Override
48     @NonNull
getComponent(@onNull ShortcutInfo info)49     public ComponentName getComponent(@NonNull ShortcutInfo info) {
50         return ShortcutKey.fromInfo(info).componentName;
51     }
52 
53     @NonNull
54     @Override
getUser(@onNull ShortcutInfo info)55     public UserHandle getUser(@NonNull ShortcutInfo info) {
56         return info.getUserHandle();
57     }
58 
59     @NonNull
60     @Override
getLabel(@onNull ShortcutInfo info)61     public CharSequence getLabel(@NonNull ShortcutInfo info) {
62         return info.getShortLabel();
63     }
64 
65     @Override
66     @NonNull
getDescription(@onNull ShortcutInfo object, @NonNull CharSequence fallback)67     public CharSequence getDescription(@NonNull ShortcutInfo object,
68             @NonNull CharSequence fallback) {
69         CharSequence label = object.getLongLabel();
70         return TextUtils.isEmpty(label) ? fallback : label;
71     }
72 
73     @NonNull
74     @Override
loadIcon(@onNull Context context, @NonNull ShortcutInfo info)75     public BitmapInfo loadIcon(@NonNull Context context, @NonNull ShortcutInfo info) {
76         try (LauncherIcons li = LauncherIcons.obtain(context)) {
77             Drawable unbadgedDrawable = ShortcutCachingLogic.getIcon(
78                     context, info, LauncherAppState.getIDP(context).fillResIconDpi);
79             if (unbadgedDrawable == null) return BitmapInfo.LOW_RES_INFO;
80             return li.createBadgedIconBitmap(unbadgedDrawable,
81                     new IconOptions().setExtractedColor(Themes.getColorAccent(context)));
82         }
83     }
84 
85     @Override
getLastUpdatedTime(@ullable ShortcutInfo shortcutInfo, @NonNull PackageInfo info)86     public long getLastUpdatedTime(@Nullable ShortcutInfo shortcutInfo,
87             @NonNull PackageInfo info) {
88         if (shortcutInfo == null) {
89             return info.lastUpdateTime;
90         }
91         return Math.max(shortcutInfo.getLastChangedTimestamp(), info.lastUpdateTime);
92     }
93 
94     @Override
addToMemCache()95     public boolean addToMemCache() {
96         return false;
97     }
98 
99     /**
100      * Similar to {@link LauncherApps#getShortcutIconDrawable(ShortcutInfo, int)} with additional
101      * Launcher specific checks
102      */
getIcon(Context context, ShortcutInfo shortcutInfo, int density)103     public static Drawable getIcon(Context context, ShortcutInfo shortcutInfo, int density) {
104         if (!WIDGETS_ENABLED) {
105             return null;
106         }
107         try {
108             return context.getSystemService(LauncherApps.class)
109                     .getShortcutIconDrawable(shortcutInfo, density);
110         } catch (SecurityException | IllegalStateException | NullPointerException e) {
111             Log.e(TAG, "Failed to get shortcut icon", e);
112             return null;
113         }
114     }
115 }
116