1 /*
2  * 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 package com.android.launcher3.model.data;
17 
18 import static android.graphics.BitmapFactory.decodeByteArray;
19 
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.LauncherActivityInfo;
23 import android.text.TextUtils;
24 import android.util.Log;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 
29 import com.android.launcher3.LauncherSettings;
30 import com.android.launcher3.icons.BitmapInfo;
31 import com.android.launcher3.icons.LauncherIcons;
32 
33 /**
34  * Class representing one request for an icon to be queried in a sql database.
35  *
36  * @param <T> ItemInfoWithIcon subclass whose title and icon can be loaded and filled by an sql
37  *           query.
38  */
39 public class IconRequestInfo<T extends ItemInfoWithIcon> {
40 
41     private static final String TAG = "IconRequestInfo";
42 
43     @NonNull public final T itemInfo;
44     @Nullable public final LauncherActivityInfo launcherActivityInfo;
45     @Nullable public final String packageName;
46     @Nullable public final String resourceName;
47     @Nullable public final byte[] iconBlob;
48     public final boolean useLowResIcon;
49 
IconRequestInfo( @onNull T itemInfo, @Nullable LauncherActivityInfo launcherActivityInfo, boolean useLowResIcon)50     public IconRequestInfo(
51             @NonNull T itemInfo,
52             @Nullable LauncherActivityInfo launcherActivityInfo,
53             boolean useLowResIcon) {
54         this(
55                 itemInfo,
56                 launcherActivityInfo,
57                 /* packageName= */ null,
58                 /* resourceName= */ null,
59                 /* iconBlob= */ null,
60                 useLowResIcon);
61     }
62 
IconRequestInfo( @onNull T itemInfo, @Nullable LauncherActivityInfo launcherActivityInfo, @Nullable String packageName, @Nullable String resourceName, @Nullable byte[] iconBlob, boolean useLowResIcon)63     public IconRequestInfo(
64             @NonNull T itemInfo,
65             @Nullable LauncherActivityInfo launcherActivityInfo,
66             @Nullable String packageName,
67             @Nullable String resourceName,
68             @Nullable byte[] iconBlob,
69             boolean useLowResIcon) {
70         this.itemInfo = itemInfo;
71         this.launcherActivityInfo = launcherActivityInfo;
72         this.packageName = packageName;
73         this.resourceName = resourceName;
74         this.iconBlob = iconBlob;
75         this.useLowResIcon = useLowResIcon;
76     }
77 
78     /** Loads  */
loadWorkspaceIcon(Context context)79     public boolean loadWorkspaceIcon(Context context) {
80         if (!(itemInfo instanceof WorkspaceItemInfo)) {
81             throw new IllegalStateException(
82                     "loadWorkspaceIcon should only be use for a WorkspaceItemInfos: " + itemInfo);
83         }
84 
85         try (LauncherIcons li = LauncherIcons.obtain(context)) {
86             WorkspaceItemInfo info = (WorkspaceItemInfo) itemInfo;
87             if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
88                 if (!TextUtils.isEmpty(packageName) || !TextUtils.isEmpty(resourceName)) {
89                     info.iconResource = new Intent.ShortcutIconResource();
90                     info.iconResource.packageName = packageName;
91                     info.iconResource.resourceName = resourceName;
92                     BitmapInfo iconInfo = li.createIconBitmap(info.iconResource);
93                     if (iconInfo != null) {
94                         info.bitmap = iconInfo;
95                         return true;
96                     }
97                 }
98             }
99 
100             // Failed to load from resource, try loading from DB.
101             try {
102                 if (iconBlob == null) {
103                     return false;
104                 }
105                 info.bitmap = li.createIconBitmap(decodeByteArray(
106                         iconBlob, 0, iconBlob.length));
107                 return true;
108             } catch (Exception e) {
109                 Log.e(TAG, "Failed to decode byte array for info " + info, e);
110                 return false;
111             }
112         }
113     }
114 }
115