1 /*
2  * Copyright (C) 2008 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 
17 package com.android.launcher2;
18 
19 import java.util.ArrayList;
20 
21 import android.content.ComponentName;
22 import android.content.ContentValues;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.graphics.Bitmap;
26 import android.util.Log;
27 
28 /**
29  * Represents a launchable icon on the workspaces and in folders.
30  */
31 class ShortcutInfo extends ItemInfo {
32 
33     /**
34      * The intent used to start the application.
35      */
36     Intent intent;
37 
38     /**
39      * Indicates whether the icon comes from an application's resource (if false)
40      * or from a custom Bitmap (if true.)
41      */
42     boolean customIcon;
43 
44     /**
45      * Indicates whether we're using the default fallback icon instead of something from the
46      * app.
47      */
48     boolean usingFallbackIcon;
49 
50     /**
51      * If isShortcut=true and customIcon=false, this contains a reference to the
52      * shortcut icon as an application's resource.
53      */
54     Intent.ShortcutIconResource iconResource;
55 
56     /**
57      * The application icon.
58      */
59     private Bitmap mIcon;
60 
ShortcutInfo()61     ShortcutInfo() {
62         itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
63     }
64 
ShortcutInfo(ShortcutInfo info)65     public ShortcutInfo(ShortcutInfo info) {
66         super(info);
67         title = info.title.toString();
68         intent = new Intent(info.intent);
69         if (info.iconResource != null) {
70             iconResource = new Intent.ShortcutIconResource();
71             iconResource.packageName = info.iconResource.packageName;
72             iconResource.resourceName = info.iconResource.resourceName;
73         }
74         mIcon = info.mIcon; // TODO: should make a copy here.  maybe we don't need this ctor at all
75         customIcon = info.customIcon;
76     }
77 
78     /** TODO: Remove this.  It's only called by ApplicationInfo.makeShortcut. */
ShortcutInfo(ApplicationInfo info)79     public ShortcutInfo(ApplicationInfo info) {
80         super(info);
81         title = info.title.toString();
82         intent = new Intent(info.intent);
83         customIcon = false;
84     }
85 
setIcon(Bitmap b)86     public void setIcon(Bitmap b) {
87         mIcon = b;
88     }
89 
getIcon(IconCache iconCache)90     public Bitmap getIcon(IconCache iconCache) {
91         if (mIcon == null) {
92             updateIcon(iconCache);
93         }
94         return mIcon;
95     }
96 
updateIcon(IconCache iconCache)97     public void updateIcon(IconCache iconCache) {
98         mIcon = iconCache.getIcon(intent, user);
99         usingFallbackIcon = iconCache.isDefaultIcon(mIcon);
100     }
101 
102     /**
103      * Creates the application intent based on a component name and various launch flags.
104      * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
105      *
106      * @param className the class name of the component representing the intent
107      * @param launchFlags the launch flags
108      */
setActivity(Intent intent)109     final void setActivity(Intent intent) {
110         int launchFlags = Intent.FLAG_ACTIVITY_NEW_TASK |
111                 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED;
112         this.intent = new Intent();
113         this.intent.setFlags(launchFlags);
114         this.intent.addCategory(Intent.CATEGORY_LAUNCHER);
115         this.intent.setComponent(intent.getComponent());
116         this.intent.putExtras(intent.getExtras());
117         itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
118         updateUser(this.intent);
119     }
120 
121     @Override
onAddToDatabase(Context context, ContentValues values)122     void onAddToDatabase(Context context, ContentValues values) {
123         super.onAddToDatabase(context, values);
124 
125         String titleStr = title != null ? title.toString() : null;
126         values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);
127 
128         String uri = intent != null ? intent.toUri(0) : null;
129         values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);
130 
131         if (customIcon) {
132             values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
133                     LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
134             writeBitmap(values, mIcon);
135         } else {
136             if (!usingFallbackIcon) {
137                 writeBitmap(values, mIcon);
138             }
139             values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
140                     LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);
141             if (iconResource != null) {
142                 values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
143                         iconResource.packageName);
144                 values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
145                         iconResource.resourceName);
146             }
147         }
148     }
149 
150     @Override
toString()151     public String toString() {
152         return "ShortcutInfo(title=" + title.toString() + "intent=" + intent + "id=" + this.id
153                 + " type=" + this.itemType + " container=" + this.container + " screen=" + screen
154                 + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY
155                 + " dropPos=" + dropPos + ")";
156     }
157 
dumpShortcutInfoList(String tag, String label, ArrayList<ShortcutInfo> list)158     public static void dumpShortcutInfoList(String tag, String label,
159             ArrayList<ShortcutInfo> list) {
160         Log.d(tag, label + " size=" + list.size());
161         for (ShortcutInfo info: list) {
162             Log.d(tag, "   title=\"" + info.title + " icon=" + info.mIcon
163                     + " customIcon=" + info.customIcon);
164         }
165     }
166 }
167 
168