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.pm.LauncherActivityInfo; 22 import android.util.Log; 23 24 import androidx.annotation.NonNull; 25 import androidx.annotation.Nullable; 26 27 import com.android.launcher3.icons.LauncherIcons; 28 29 /** 30 * Class representing one request for an icon to be queried in a sql database. 31 * 32 * @param <T> ItemInfoWithIcon subclass whose title and icon can be loaded and filled by an sql 33 * query. 34 */ 35 public class IconRequestInfo<T extends ItemInfoWithIcon> { 36 37 private static final String TAG = "IconRequestInfo"; 38 39 @NonNull public final T itemInfo; 40 @Nullable public final LauncherActivityInfo launcherActivityInfo; 41 @Nullable public final byte[] iconBlob; 42 public final boolean useLowResIcon; 43 IconRequestInfo( @onNull T itemInfo, @Nullable LauncherActivityInfo launcherActivityInfo, boolean useLowResIcon)44 public IconRequestInfo( 45 @NonNull T itemInfo, 46 @Nullable LauncherActivityInfo launcherActivityInfo, 47 boolean useLowResIcon) { 48 this( 49 itemInfo, 50 launcherActivityInfo, 51 /* iconBlob= */ null, 52 useLowResIcon); 53 } 54 IconRequestInfo( @onNull T itemInfo, @Nullable LauncherActivityInfo launcherActivityInfo, @Nullable byte[] iconBlob, boolean useLowResIcon)55 public IconRequestInfo( 56 @NonNull T itemInfo, 57 @Nullable LauncherActivityInfo launcherActivityInfo, 58 @Nullable byte[] iconBlob, 59 boolean useLowResIcon) { 60 this.itemInfo = itemInfo; 61 this.launcherActivityInfo = launcherActivityInfo; 62 this.iconBlob = iconBlob; 63 this.useLowResIcon = useLowResIcon; 64 } 65 66 /** 67 * Loads this request's item info's title. This method should only be used on IconRequestInfos 68 * for WorkspaceItemInfos. 69 */ loadWorkspaceIcon(Context context)70 public boolean loadWorkspaceIcon(Context context) { 71 if (!(itemInfo instanceof WorkspaceItemInfo)) { 72 throw new IllegalStateException( 73 "loadWorkspaceIcon should only be use for a WorkspaceItemInfos: " + itemInfo); 74 } 75 76 try (LauncherIcons li = LauncherIcons.obtain(context)) { 77 WorkspaceItemInfo info = (WorkspaceItemInfo) itemInfo; 78 // Failed to load from resource, try loading from DB. 79 if (iconBlob == null) { 80 return false; 81 } 82 info.bitmap = li.createIconBitmap(decodeByteArray( 83 iconBlob, 0, iconBlob.length)); 84 return true; 85 } catch (Exception e) { 86 Log.e(TAG, "Failed to decode byte array for info " + itemInfo, e); 87 return false; 88 } 89 } 90 } 91