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 and icon from given iconBlob from Launcher.db. 68 * This method should only be used on {@link IconRequestInfo} for {@link WorkspaceItemInfo} 69 * or {@link AppInfo}. 70 */ loadIconFromDbBlob(Context context)71 public boolean loadIconFromDbBlob(Context context) { 72 if (!(itemInfo instanceof WorkspaceItemInfo) && !(itemInfo instanceof AppInfo)) { 73 throw new IllegalStateException( 74 "loadIconFromDb should only be used for either WorkspaceItemInfo or AppInfo: " 75 + itemInfo); 76 } 77 78 try (LauncherIcons li = LauncherIcons.obtain(context)) { 79 ItemInfoWithIcon info = itemInfo; 80 if (iconBlob == null) { 81 Log.d(TAG, "loadIconFromDb: icon blob null, returning. Component=" 82 + info.getTargetComponent()); 83 return false; 84 } 85 info.bitmap = li.createIconBitmap(decodeByteArray(iconBlob, 0, iconBlob.length)); 86 return true; 87 } catch (Exception e) { 88 Log.e(TAG, "Failed to decode byte array for info " + itemInfo, e); 89 return false; 90 } 91 } 92 } 93