ShortcutInfo.java revision 487f7dd3059621527eb439d7d51d34e00293f9b1
10589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato/* 20589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Copyright (C) 2008 The Android Open Source Project 30589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * 40589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Licensed under the Apache License, Version 2.0 (the "License"); 50589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * you may not use this file except in compliance with the License. 60589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * You may obtain a copy of the License at 70589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * 80589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * http://www.apache.org/licenses/LICENSE-2.0 90589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * 100589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Unless required by applicable law or agreed to in writing, software 110589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * distributed under the License is distributed on an "AS IS" BASIS, 120589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 130589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * See the License for the specific language governing permissions and 140589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * limitations under the License. 150589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */ 160589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 170589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratopackage com.android.launcher2; 180589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 19aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.util.ArrayList; 20aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung 210589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.content.ComponentName; 220589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.content.ContentValues; 230589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.content.Intent; 240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.graphics.Bitmap; 250589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.util.Log; 260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato/** 280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Represents a launchable icon on the workspaces and in folders. 290589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */ 300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoclass ShortcutInfo extends ItemInfo { 310589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato /** 330589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * The intent used to start the application. 340589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */ 350589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato Intent intent; 360589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato /** 380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Indicates whether the icon comes from an application's resource (if false) 390589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * or from a custom Bitmap (if true.) 400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */ 410589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato boolean customIcon; 420589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 430589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato /** 4456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato * Indicates whether we're using the default fallback icon instead of something from the 4556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato * app. 4656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato */ 4756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato boolean usingFallbackIcon; 4856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato 4956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato /** 500589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * If isShortcut=true and customIcon=false, this contains a reference to the 510589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * shortcut icon as an application's resource. 520589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */ 530589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato Intent.ShortcutIconResource iconResource; 540589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 550589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato /** 560589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * The application icon. 570589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */ 580589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato private Bitmap mIcon; 590589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 60c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka ShortcutInfo() { 610589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT; 620589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 630589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 64c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka public ShortcutInfo(ShortcutInfo info) { 65c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka super(info); 660589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato title = info.title.toString(); 670589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato intent = new Intent(info.intent); 680589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato if (info.iconResource != null) { 690589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato iconResource = new Intent.ShortcutIconResource(); 700589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato iconResource.packageName = info.iconResource.packageName; 710589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato iconResource.resourceName = info.iconResource.resourceName; 720589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 730589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato mIcon = info.mIcon; // TODO: should make a copy here. maybe we don't need this ctor at all 740589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato customIcon = info.customIcon; 750589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 760589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 770589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato /** TODO: Remove this. It's only called by ApplicationInfo.makeShortcut. */ 78c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka public ShortcutInfo(ApplicationInfo info) { 79c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka super(info); 800589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato title = info.title.toString(); 810589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato intent = new Intent(info.intent); 820589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato customIcon = false; 830589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 840589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 850589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato public void setIcon(Bitmap b) { 860589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato mIcon = b; 870589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 880589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 890589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato public Bitmap getIcon(IconCache iconCache) { 900589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato if (mIcon == null) { 91e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka updateIcon(iconCache); 920589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 930589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato return mIcon; 940589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 950589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 96be365165ed00205265c1876c4829fa9ac630da2aWinson Chung /** Returns the package name that the shortcut's intent will resolve to, or an empty string if 97be365165ed00205265c1876c4829fa9ac630da2aWinson Chung * none exists. */ 98be365165ed00205265c1876c4829fa9ac630da2aWinson Chung String getPackageName() { 99be365165ed00205265c1876c4829fa9ac630da2aWinson Chung return super.getPackageName(intent); 100be365165ed00205265c1876c4829fa9ac630da2aWinson Chung } 101be365165ed00205265c1876c4829fa9ac630da2aWinson Chung 102e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka public void updateIcon(IconCache iconCache) { 103e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka mIcon = iconCache.getIcon(intent); 104e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka usingFallbackIcon = iconCache.isDefaultIcon(mIcon); 105e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka } 106e384affda684a48c61d99ebfe8be40fb7d46d761Michael Jurka 1070589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato /** 1080589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Creates the application intent based on a component name and various launch flags. 1090589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}. 1100589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * 1110589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * @param className the class name of the component representing the intent 1120589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * @param launchFlags the launch flags 1130589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */ 1140589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato final void setActivity(ComponentName className, int launchFlags) { 1150589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato intent = new Intent(Intent.ACTION_MAIN); 1160589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato intent.addCategory(Intent.CATEGORY_LAUNCHER); 1170589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato intent.setComponent(className); 1180589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato intent.setFlags(launchFlags); 1190589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION; 1200589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 1210589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 1220589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato @Override 1230589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato void onAddToDatabase(ContentValues values) { 1240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato super.onAddToDatabase(values); 1250589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 1260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato String titleStr = title != null ? title.toString() : null; 1270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr); 1280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 1290589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato String uri = intent != null ? intent.toUri(0) : null; 1300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri); 1310589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 1320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato if (customIcon) { 1330589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE, 1340589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP); 13556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato writeBitmap(values, mIcon); 1360589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } else { 137ddc9c1fb1ab426772add520d277ea9c2cd674094Joe Onorato if (!usingFallbackIcon) { 13856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato writeBitmap(values, mIcon); 13956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato } 1400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE, 1410589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE); 1420589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato if (iconResource != null) { 1430589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE, 1440589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato iconResource.packageName); 1450589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE, 1460589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato iconResource.resourceName); 1470589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 1480589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 1490589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 1500589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 1510589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato @Override 1520589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato public String toString() { 153db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung return "ShortcutInfo(title=" + title.toString() + "intent=" + intent + "id=" + this.id 154db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung + " type=" + this.itemType + " container=" + this.container + " screen=" + screen 155db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY 156487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen + " dropPos=" + dropPos + ")"; 1570589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 1580589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 1590589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato public static void dumpShortcutInfoList(String tag, String label, 1600589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato ArrayList<ShortcutInfo> list) { 1610589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato Log.d(tag, label + " size=" + list.size()); 1620589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato for (ShortcutInfo info: list) { 1630589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato Log.d(tag, " title=\"" + info.title + " icon=" + info.mIcon 1640589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato + " customIcon=" + info.customIcon); 1650589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 1660589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 1670589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato} 1680589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 169