• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     /**
79      * Loads this request's item info's title. This method should only be used on IconRequestInfos
80      * for WorkspaceItemInfos.
81      */
loadWorkspaceIcon(Context context)82     public boolean loadWorkspaceIcon(Context context) {
83         if (!(itemInfo instanceof WorkspaceItemInfo)) {
84             throw new IllegalStateException(
85                     "loadWorkspaceIcon should only be use for a WorkspaceItemInfos: " + itemInfo);
86         }
87 
88         try (LauncherIcons li = LauncherIcons.obtain(context)) {
89             WorkspaceItemInfo info = (WorkspaceItemInfo) itemInfo;
90             if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
91                 if (!TextUtils.isEmpty(packageName) || !TextUtils.isEmpty(resourceName)) {
92                     info.iconResource = new Intent.ShortcutIconResource();
93                     info.iconResource.packageName = packageName;
94                     info.iconResource.resourceName = resourceName;
95                     BitmapInfo iconInfo = li.createIconBitmap(info.iconResource);
96                     if (iconInfo != null) {
97                         info.bitmap = iconInfo;
98                         return true;
99                     }
100                 }
101             }
102 
103             // Failed to load from resource, try loading from DB.
104             try {
105                 if (iconBlob == null) {
106                     return false;
107                 }
108                 info.bitmap = li.createIconBitmap(decodeByteArray(
109                         iconBlob, 0, iconBlob.length));
110                 return true;
111             } catch (Exception e) {
112                 Log.e(TAG, "Failed to decode byte array for info " + info, e);
113                 return false;
114             }
115         }
116     }
117 }
118