ShortcutInfo.java revision aafa03cbb925c74be1c13f8bb99d928be429e62f
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 application name. 340589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */ 350589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato CharSequence title; 360589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato /** 380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * The intent used to start the application. 390589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */ 400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato Intent intent; 410589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 420589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato /** 430589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Indicates whether the icon comes from an application's resource (if false) 440589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * or from a custom Bitmap (if true.) 450589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */ 460589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato boolean customIcon; 470589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 480589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato /** 4956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato * Indicates whether we're using the default fallback icon instead of something from the 5056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato * app. 5156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato */ 5256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato boolean usingFallbackIcon; 5356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato 5456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato /** 5556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato * Indicates whether the shortcut is on external storage and may go away at any time. 5656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato */ 5756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato boolean onExternalStorage; 5856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato 5956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato /** 600589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * If isShortcut=true and customIcon=false, this contains a reference to the 610589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * shortcut icon as an application's resource. 620589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */ 630589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato Intent.ShortcutIconResource iconResource; 640589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 650589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato /** 660589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * The application icon. 670589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */ 680589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato private Bitmap mIcon; 690589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 700589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato ShortcutInfo() { 710589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT; 720589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 730589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 740589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato public ShortcutInfo(ShortcutInfo info) { 750589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato super(info); 760589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato title = info.title.toString(); 770589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato intent = new Intent(info.intent); 780589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato if (info.iconResource != null) { 790589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato iconResource = new Intent.ShortcutIconResource(); 800589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato iconResource.packageName = info.iconResource.packageName; 810589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato iconResource.resourceName = info.iconResource.resourceName; 820589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 830589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato mIcon = info.mIcon; // TODO: should make a copy here. maybe we don't need this ctor at all 840589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato customIcon = info.customIcon; 850589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 860589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 870589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato /** TODO: Remove this. It's only called by ApplicationInfo.makeShortcut. */ 880589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato public ShortcutInfo(ApplicationInfo info) { 890589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato super(info); 900589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato title = info.title.toString(); 910589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato intent = new Intent(info.intent); 920589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato customIcon = false; 930589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 940589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 950589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato public void setIcon(Bitmap b) { 960589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato mIcon = b; 970589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 980589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 990589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato public Bitmap getIcon(IconCache iconCache) { 1000589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato if (mIcon == null) { 1010589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato mIcon = iconCache.getIcon(this.intent); 1020589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 1030589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato return mIcon; 1040589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 1050589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 1060589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato /** 1070589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Creates the application intent based on a component name and various launch flags. 1080589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}. 1090589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * 1100589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * @param className the class name of the component representing the intent 1110589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato * @param launchFlags the launch flags 1120589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato */ 1130589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato final void setActivity(ComponentName className, int launchFlags) { 1140589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato intent = new Intent(Intent.ACTION_MAIN); 1150589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato intent.addCategory(Intent.CATEGORY_LAUNCHER); 1160589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato intent.setComponent(className); 1170589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato intent.setFlags(launchFlags); 1180589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION; 1190589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 1200589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 1210589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato @Override 1220589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato void onAddToDatabase(ContentValues values) { 1230589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato super.onAddToDatabase(values); 1240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 1250589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato String titleStr = title != null ? title.toString() : null; 1260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr); 1270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 1280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato String uri = intent != null ? intent.toUri(0) : null; 1290589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri); 1300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 1310589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato if (customIcon) { 1320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE, 1330589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP); 13456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato writeBitmap(values, mIcon); 1350589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } else { 13656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato if (onExternalStorage && !usingFallbackIcon) { 13756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato writeBitmap(values, mIcon); 13856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato } 1390589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE, 1400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE); 1410589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato if (iconResource != null) { 1420589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE, 1430589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato iconResource.packageName); 1440589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE, 1450589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato iconResource.resourceName); 1460589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 1470589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 1480589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 1490589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 1500589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato @Override 1510589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato public String toString() { 152f984e853f253c76a7a636a094680bb01c4b8ffb8Joe Onorato return "ShortcutInfo(title=" + title.toString() + ")"; 1530589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 1540589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 1550589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato @Override 1560589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato void unbind() { 1570589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato super.unbind(); 1580589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 1590589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 1600589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 1610589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato public static void dumpShortcutInfoList(String tag, String label, 1620589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato ArrayList<ShortcutInfo> list) { 1630589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato Log.d(tag, label + " size=" + list.size()); 1640589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato for (ShortcutInfo info: list) { 1650589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato Log.d(tag, " title=\"" + info.title + " icon=" + info.mIcon 1660589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato + " customIcon=" + info.customIcon); 1670589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 1680589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato } 1690589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato} 1700589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato 171