14e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal/*
24e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal * Copyright (C) 2015 The Android Open Source Project
34e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal *
44e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal * Licensed under the Apache License, Version 2.0 (the "License");
54e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal * you may not use this file except in compliance with the License.
64e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal * You may obtain a copy of the License at
74e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal *
84e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal *      http://www.apache.org/licenses/LICENSE-2.0
94e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal *
104e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal * Unless required by applicable law or agreed to in writing, software
114e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal * distributed under the License is distributed on an "AS IS" BASIS,
124e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal * See the License for the specific language governing permissions and
144e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal * limitations under the License.
154e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal */
164e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal
174e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyalpackage com.android.launcher3.util;
184e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal
194e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyalimport android.content.Context;
204e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyalimport android.content.Intent.ShortcutIconResource;
214e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyalimport android.database.Cursor;
224e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyalimport android.graphics.Bitmap;
234e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyalimport android.text.TextUtils;
244e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal
254e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyalimport com.android.launcher3.LauncherSettings;
264e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyalimport com.android.launcher3.ShortcutInfo;
274e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyalimport com.android.launcher3.Utilities;
284e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal
294e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal/**
304e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal * Utility class to load icon from a cursor.
314e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal */
324e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyalpublic class CursorIconInfo {
334e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal    public final int iconPackageIndex;
344e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal    public final int iconResourceIndex;
354e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal    public final int iconIndex;
364e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal
37d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    public final int titleIndex;
38d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal
39d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    private final Context mContext;
40d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal
41d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    public CursorIconInfo(Context context, Cursor c) {
42d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal        mContext = context;
43d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal
444e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal        iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
454e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal        iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
464e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal        iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
47d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal
48d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal        titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
494e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal    }
504e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal
51d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    /**
52d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal     * Loads the icon from the cursor and updates the {@param info} if the icon is an app resource.
53d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal     */
54d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    public Bitmap loadIcon(Cursor c, ShortcutInfo info) {
554e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal        Bitmap icon = null;
56eb4b79935e9f75dda72e4953f45e616e252d7b03Sunny Goyal        String packageName = c.getString(iconPackageIndex);
57eb4b79935e9f75dda72e4953f45e616e252d7b03Sunny Goyal        String resourceName = c.getString(iconResourceIndex);
58eb4b79935e9f75dda72e4953f45e616e252d7b03Sunny Goyal        if (!TextUtils.isEmpty(packageName) || !TextUtils.isEmpty(resourceName)) {
59eb4b79935e9f75dda72e4953f45e616e252d7b03Sunny Goyal            info.iconResource = new ShortcutIconResource();
60eb4b79935e9f75dda72e4953f45e616e252d7b03Sunny Goyal            info.iconResource.packageName = packageName;
61eb4b79935e9f75dda72e4953f45e616e252d7b03Sunny Goyal            info.iconResource.resourceName = resourceName;
62d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal            icon = Utilities.createIconBitmap(packageName, resourceName, mContext);
63eb4b79935e9f75dda72e4953f45e616e252d7b03Sunny Goyal        }
64eb4b79935e9f75dda72e4953f45e616e252d7b03Sunny Goyal        if (icon == null) {
65eb4b79935e9f75dda72e4953f45e616e252d7b03Sunny Goyal            // Failed to load from resource, try loading from DB.
66d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal            icon = loadIcon(c);
674e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal        }
684e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal        return icon;
694e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal    }
70d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal
71d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    /**
72d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal     * Loads the fixed bitmap from the icon if available.
73d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal     */
74d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    public Bitmap loadIcon(Cursor c) {
75d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal        return Utilities.createIconBitmap(c, iconIndex, mContext);
76d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    }
77d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal
78d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    /**
79d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal     * Returns the title or empty string
80d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal     */
81d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    public String getTitle(Cursor c) {
82d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal        String title = c.getString(titleIndex);
83d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal        return TextUtils.isEmpty(title) ? "" : Utilities.trim(c.getString(titleIndex));
84d3b87ef1963fb96177ca85bcd6a25879e27e419cSunny Goyal    }
854e5cc64eaf1f63d866d51ce0a6bbafb3d4085c21Sunny Goyal}
86