1/* 2 * Copyright (C) 2015 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 17package com.android.launcher3.util; 18 19import android.content.Context; 20import android.content.Intent.ShortcutIconResource; 21import android.database.Cursor; 22import android.graphics.Bitmap; 23import android.text.TextUtils; 24 25import com.android.launcher3.LauncherSettings; 26import com.android.launcher3.ShortcutInfo; 27import com.android.launcher3.Utilities; 28 29/** 30 * Utility class to load icon from a cursor. 31 */ 32public class CursorIconInfo { 33 public final int iconPackageIndex; 34 public final int iconResourceIndex; 35 public final int iconIndex; 36 37 public final int titleIndex; 38 39 private final Context mContext; 40 41 public CursorIconInfo(Context context, Cursor c) { 42 mContext = context; 43 44 iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON); 45 iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE); 46 iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE); 47 48 titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE); 49 } 50 51 /** 52 * Loads the icon from the cursor and updates the {@param info} if the icon is an app resource. 53 */ 54 public Bitmap loadIcon(Cursor c, ShortcutInfo info) { 55 Bitmap icon = null; 56 String packageName = c.getString(iconPackageIndex); 57 String resourceName = c.getString(iconResourceIndex); 58 if (!TextUtils.isEmpty(packageName) || !TextUtils.isEmpty(resourceName)) { 59 info.iconResource = new ShortcutIconResource(); 60 info.iconResource.packageName = packageName; 61 info.iconResource.resourceName = resourceName; 62 icon = Utilities.createIconBitmap(packageName, resourceName, mContext); 63 } 64 if (icon == null) { 65 // Failed to load from resource, try loading from DB. 66 icon = loadIcon(c); 67 } 68 return icon; 69 } 70 71 /** 72 * Loads the fixed bitmap from the icon if available. 73 */ 74 public Bitmap loadIcon(Cursor c) { 75 return Utilities.createIconBitmap(c, iconIndex, mContext); 76 } 77 78 /** 79 * Returns the title or empty string 80 */ 81 public String getTitle(Cursor c) { 82 String title = c.getString(titleIndex); 83 return TextUtils.isEmpty(title) ? "" : Utilities.trim(c.getString(titleIndex)); 84 } 85} 86