blob: 533059f57808ae0f537fb1be83bd87e804e5570e [file] [log] [blame]
Joe Onorato0589f0f2010-02-08 13:44:00 -08001/*
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
17package com.android.launcher2;
18
Winson Chungaafa03c2010-06-11 17:34:16 -070019import java.util.ArrayList;
20
Joe Onorato0589f0f2010-02-08 13:44:00 -080021import android.content.ComponentName;
22import android.content.ContentValues;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.content.Intent;
24import android.graphics.Bitmap;
Joe Onorato0589f0f2010-02-08 13:44:00 -080025import android.util.Log;
26
Joe Onorato0589f0f2010-02-08 13:44:00 -080027/**
28 * Represents a launchable icon on the workspaces and in folders.
29 */
30class ShortcutInfo extends ItemInfo {
31
32 /**
33 * The application name.
34 */
35 CharSequence title;
36
37 /**
38 * The intent used to start the application.
39 */
40 Intent intent;
41
42 /**
43 * Indicates whether the icon comes from an application's resource (if false)
44 * or from a custom Bitmap (if true.)
45 */
46 boolean customIcon;
47
48 /**
Joe Onorato56d82912010-03-07 14:32:10 -050049 * Indicates whether we're using the default fallback icon instead of something from the
50 * app.
51 */
52 boolean usingFallbackIcon;
53
54 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -080055 * If isShortcut=true and customIcon=false, this contains a reference to the
56 * shortcut icon as an application's resource.
57 */
58 Intent.ShortcutIconResource iconResource;
59
60 /**
61 * The application icon.
62 */
63 private Bitmap mIcon;
64
Michael Jurkac9d95c52011-08-29 14:03:34 -070065 ShortcutInfo() {
Joe Onorato0589f0f2010-02-08 13:44:00 -080066 itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
67 }
68
Michael Jurkac9d95c52011-08-29 14:03:34 -070069 public ShortcutInfo(ShortcutInfo info) {
70 super(info);
Joe Onorato0589f0f2010-02-08 13:44:00 -080071 title = info.title.toString();
72 intent = new Intent(info.intent);
73 if (info.iconResource != null) {
74 iconResource = new Intent.ShortcutIconResource();
75 iconResource.packageName = info.iconResource.packageName;
76 iconResource.resourceName = info.iconResource.resourceName;
77 }
78 mIcon = info.mIcon; // TODO: should make a copy here. maybe we don't need this ctor at all
79 customIcon = info.customIcon;
80 }
81
82 /** TODO: Remove this. It's only called by ApplicationInfo.makeShortcut. */
Michael Jurkac9d95c52011-08-29 14:03:34 -070083 public ShortcutInfo(ApplicationInfo info) {
84 super(info);
Joe Onorato0589f0f2010-02-08 13:44:00 -080085 title = info.title.toString();
86 intent = new Intent(info.intent);
87 customIcon = false;
88 }
89
90 public void setIcon(Bitmap b) {
91 mIcon = b;
92 }
93
94 public Bitmap getIcon(IconCache iconCache) {
95 if (mIcon == null) {
Michael Jurkae384aff2012-03-09 15:59:25 -080096 updateIcon(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -080097 }
98 return mIcon;
99 }
100
Winson Chungbe365162012-05-07 10:34:12 -0700101 /** Returns the package name that the shortcut's intent will resolve to, or an empty string if
102 * none exists. */
103 String getPackageName() {
104 return super.getPackageName(intent);
105 }
106
Michael Jurkae384aff2012-03-09 15:59:25 -0800107 public void updateIcon(IconCache iconCache) {
108 mIcon = iconCache.getIcon(intent);
109 usingFallbackIcon = iconCache.isDefaultIcon(mIcon);
110 }
111
Joe Onorato0589f0f2010-02-08 13:44:00 -0800112 /**
113 * Creates the application intent based on a component name and various launch flags.
114 * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
115 *
116 * @param className the class name of the component representing the intent
117 * @param launchFlags the launch flags
118 */
119 final void setActivity(ComponentName className, int launchFlags) {
120 intent = new Intent(Intent.ACTION_MAIN);
121 intent.addCategory(Intent.CATEGORY_LAUNCHER);
122 intent.setComponent(className);
123 intent.setFlags(launchFlags);
124 itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
125 }
126
127 @Override
128 void onAddToDatabase(ContentValues values) {
129 super.onAddToDatabase(values);
130
131 String titleStr = title != null ? title.toString() : null;
132 values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);
133
134 String uri = intent != null ? intent.toUri(0) : null;
135 values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);
136
137 if (customIcon) {
138 values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
139 LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
Joe Onorato56d82912010-03-07 14:32:10 -0500140 writeBitmap(values, mIcon);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800141 } else {
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700142 if (!usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -0500143 writeBitmap(values, mIcon);
144 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145 values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
146 LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);
147 if (iconResource != null) {
148 values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
149 iconResource.packageName);
150 values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
151 iconResource.resourceName);
152 }
153 }
154 }
155
156 @Override
157 public String toString() {
Winson Chungdb8a8942012-04-03 14:08:41 -0700158 return "ShortcutInfo(title=" + title.toString() + "intent=" + intent + "id=" + this.id
159 + " type=" + this.itemType + " container=" + this.container + " screen=" + screen
160 + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY
161 + " isGesture=" + isGesture + " dropPos=" + dropPos + ")";
Joe Onorato0589f0f2010-02-08 13:44:00 -0800162 }
163
Joe Onorato0589f0f2010-02-08 13:44:00 -0800164 public static void dumpShortcutInfoList(String tag, String label,
165 ArrayList<ShortcutInfo> list) {
166 Log.d(tag, label + " size=" + list.size());
167 for (ShortcutInfo info: list) {
168 Log.d(tag, " title=\"" + info.title + " icon=" + info.mIcon
169 + " customIcon=" + info.customIcon);
170 }
171 }
172}
173