blob: 5249fec020a81883ecf3b8af5f73b08c86aa5ae0 [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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Joe Onorato0589f0f2010-02-08 13:44:00 -080018
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 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -080033 * The intent used to start the application.
34 */
35 Intent intent;
36
37 /**
38 * Indicates whether the icon comes from an application's resource (if false)
39 * or from a custom Bitmap (if true.)
40 */
41 boolean customIcon;
42
43 /**
Joe Onorato56d82912010-03-07 14:32:10 -050044 * Indicates whether we're using the default fallback icon instead of something from the
45 * app.
46 */
47 boolean usingFallbackIcon;
48
49 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -080050 * If isShortcut=true and customIcon=false, this contains a reference to the
51 * shortcut icon as an application's resource.
52 */
53 Intent.ShortcutIconResource iconResource;
54
55 /**
56 * The application icon.
57 */
58 private Bitmap mIcon;
59
Michael Jurkac9d95c52011-08-29 14:03:34 -070060 ShortcutInfo() {
Joe Onorato0589f0f2010-02-08 13:44:00 -080061 itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
62 }
63
Michael Jurkac9d95c52011-08-29 14:03:34 -070064 public ShortcutInfo(ShortcutInfo info) {
65 super(info);
Joe Onorato0589f0f2010-02-08 13:44:00 -080066 title = info.title.toString();
67 intent = new Intent(info.intent);
68 if (info.iconResource != null) {
69 iconResource = new Intent.ShortcutIconResource();
70 iconResource.packageName = info.iconResource.packageName;
71 iconResource.resourceName = info.iconResource.resourceName;
72 }
73 mIcon = info.mIcon; // TODO: should make a copy here. maybe we don't need this ctor at all
74 customIcon = info.customIcon;
75 }
76
77 /** TODO: Remove this. It's only called by ApplicationInfo.makeShortcut. */
Michael Jurkac9d95c52011-08-29 14:03:34 -070078 public ShortcutInfo(ApplicationInfo info) {
79 super(info);
Joe Onorato0589f0f2010-02-08 13:44:00 -080080 title = info.title.toString();
81 intent = new Intent(info.intent);
82 customIcon = false;
83 }
84
85 public void setIcon(Bitmap b) {
86 mIcon = b;
87 }
88
89 public Bitmap getIcon(IconCache iconCache) {
90 if (mIcon == null) {
Michael Jurkae384aff2012-03-09 15:59:25 -080091 updateIcon(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -080092 }
93 return mIcon;
94 }
95
Michael Jurkae384aff2012-03-09 15:59:25 -080096 public void updateIcon(IconCache iconCache) {
97 mIcon = iconCache.getIcon(intent);
98 usingFallbackIcon = iconCache.isDefaultIcon(mIcon);
99 }
100
Joe Onorato0589f0f2010-02-08 13:44:00 -0800101 /**
102 * Creates the application intent based on a component name and various launch flags.
103 * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
104 *
105 * @param className the class name of the component representing the intent
106 * @param launchFlags the launch flags
107 */
108 final void setActivity(ComponentName className, int launchFlags) {
109 intent = new Intent(Intent.ACTION_MAIN);
110 intent.addCategory(Intent.CATEGORY_LAUNCHER);
111 intent.setComponent(className);
112 intent.setFlags(launchFlags);
113 itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
114 }
115
116 @Override
117 void onAddToDatabase(ContentValues values) {
118 super.onAddToDatabase(values);
119
120 String titleStr = title != null ? title.toString() : null;
121 values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);
122
123 String uri = intent != null ? intent.toUri(0) : null;
124 values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);
125
126 if (customIcon) {
127 values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
128 LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
Joe Onorato56d82912010-03-07 14:32:10 -0500129 writeBitmap(values, mIcon);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800130 } else {
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700131 if (!usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -0500132 writeBitmap(values, mIcon);
133 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800134 values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
135 LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);
136 if (iconResource != null) {
137 values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
138 iconResource.packageName);
139 values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
140 iconResource.resourceName);
141 }
142 }
143 }
144
145 @Override
146 public String toString() {
Winson Chungdb8a8942012-04-03 14:08:41 -0700147 return "ShortcutInfo(title=" + title.toString() + "intent=" + intent + "id=" + this.id
148 + " type=" + this.itemType + " container=" + this.container + " screen=" + screen
149 + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY
Adam Cohen487f7dd2012-06-28 18:12:10 -0700150 + " dropPos=" + dropPos + ")";
Joe Onorato0589f0f2010-02-08 13:44:00 -0800151 }
152
Joe Onorato0589f0f2010-02-08 13:44:00 -0800153 public static void dumpShortcutInfoList(String tag, String label,
154 ArrayList<ShortcutInfo> list) {
155 Log.d(tag, label + " size=" + list.size());
156 for (ShortcutInfo info: list) {
157 Log.d(tag, " title=\"" + info.title + " icon=" + info.mIcon
158 + " customIcon=" + info.customIcon);
159 }
160 }
161}
162