Home | History | Annotate | Download | only in launcher3
      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.launcher3;
     18 
     19 import android.content.ComponentName;
     20 import android.content.ContentValues;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.pm.PackageInfo;
     24 import android.content.pm.PackageManager;
     25 import android.content.pm.PackageManager.NameNotFoundException;
     26 import android.graphics.Bitmap;
     27 import android.util.Log;
     28 
     29 import java.util.ArrayList;
     30 
     31 /**
     32  * Represents a launchable icon on the workspaces and in folders.
     33  */
     34 class ShortcutInfo extends ItemInfo {
     35 
     36     /**
     37      * The intent used to start the application.
     38      */
     39     Intent intent;
     40 
     41     /**
     42      * Indicates whether the icon comes from an application's resource (if false)
     43      * or from a custom Bitmap (if true.)
     44      */
     45     boolean customIcon;
     46 
     47     /**
     48      * Indicates whether we're using the default fallback icon instead of something from the
     49      * app.
     50      */
     51     boolean usingFallbackIcon;
     52 
     53     /**
     54      * If isShortcut=true and customIcon=false, this contains a reference to the
     55      * shortcut icon as an application's resource.
     56      */
     57     Intent.ShortcutIconResource iconResource;
     58 
     59     /**
     60      * The application icon.
     61      */
     62     private Bitmap mIcon;
     63 
     64     long firstInstallTime;
     65     int flags = 0;
     66 
     67     /**
     68      * If this shortcut is a placeholder, then intent will be a market intent for the package, and
     69      * this will hold the original intent from the database.  Otherwise, null.
     70      */
     71     Intent restoredIntent;
     72 
     73     ShortcutInfo() {
     74         itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
     75     }
     76 
     77     protected Intent getIntent() {
     78         return intent;
     79     }
     80 
     81     protected Intent getRestoredIntent() {
     82         return restoredIntent;
     83     }
     84 
     85     /**
     86      * Overwrite placeholder data with restored data, or do nothing if this is not a placeholder.
     87      */
     88     public void restore() {
     89         if (restoredIntent != null) {
     90             intent = restoredIntent;
     91             restoredIntent = null;
     92         }
     93     }
     94 
     95 
     96     ShortcutInfo(Intent intent, CharSequence title, Bitmap icon) {
     97         this();
     98         this.intent = intent;
     99         this.title = title;
    100         mIcon = icon;
    101     }
    102 
    103     public ShortcutInfo(Context context, ShortcutInfo info) {
    104         super(info);
    105         title = info.title.toString();
    106         intent = new Intent(info.intent);
    107         if (info.iconResource != null) {
    108             iconResource = new Intent.ShortcutIconResource();
    109             iconResource.packageName = info.iconResource.packageName;
    110             iconResource.resourceName = info.iconResource.resourceName;
    111         }
    112         mIcon = info.mIcon; // TODO: should make a copy here.  maybe we don't need this ctor at all
    113         customIcon = info.customIcon;
    114         initFlagsAndFirstInstallTime(
    115                 getPackageInfo(context, intent.getComponent().getPackageName()));
    116     }
    117 
    118     /** TODO: Remove this.  It's only called by ApplicationInfo.makeShortcut. */
    119     public ShortcutInfo(AppInfo info) {
    120         super(info);
    121         title = info.title.toString();
    122         intent = new Intent(info.intent);
    123         customIcon = false;
    124         flags = info.flags;
    125         firstInstallTime = info.firstInstallTime;
    126     }
    127 
    128     public static PackageInfo getPackageInfo(Context context, String packageName) {
    129         PackageInfo pi = null;
    130         try {
    131             PackageManager pm = context.getPackageManager();
    132             pi = pm.getPackageInfo(packageName, 0);
    133         } catch (NameNotFoundException e) {
    134             Log.d("ShortcutInfo", "PackageManager.getPackageInfo failed for " + packageName);
    135         }
    136         return pi;
    137     }
    138 
    139     void initFlagsAndFirstInstallTime(PackageInfo pi) {
    140         flags = AppInfo.initFlags(pi);
    141         firstInstallTime = AppInfo.initFirstInstallTime(pi);
    142     }
    143 
    144     public void setIcon(Bitmap b) {
    145         mIcon = b;
    146     }
    147 
    148     public Bitmap getIcon(IconCache iconCache) {
    149         if (mIcon == null) {
    150             updateIcon(iconCache);
    151         }
    152         return mIcon;
    153     }
    154 
    155     public void updateIcon(IconCache iconCache) {
    156         mIcon = iconCache.getIcon(intent);
    157         usingFallbackIcon = iconCache.isDefaultIcon(mIcon);
    158     }
    159 
    160     /**
    161      * Creates the application intent based on a component name and various launch flags.
    162      * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
    163      *
    164      * @param className the class name of the component representing the intent
    165      * @param launchFlags the launch flags
    166      */
    167     final void setActivity(Context context, ComponentName className, int launchFlags) {
    168         intent = new Intent(Intent.ACTION_MAIN);
    169         intent.addCategory(Intent.CATEGORY_LAUNCHER);
    170         intent.setComponent(className);
    171         intent.setFlags(launchFlags);
    172         itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
    173         initFlagsAndFirstInstallTime(
    174                 getPackageInfo(context, intent.getComponent().getPackageName()));
    175     }
    176 
    177     @Override
    178     void onAddToDatabase(ContentValues values) {
    179         super.onAddToDatabase(values);
    180 
    181         String titleStr = title != null ? title.toString() : null;
    182         values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);
    183 
    184         String uri = intent != null ? intent.toUri(0) : null;
    185         values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);
    186 
    187         if (customIcon) {
    188             values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
    189                     LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
    190             writeBitmap(values, mIcon);
    191         } else {
    192             if (!usingFallbackIcon) {
    193                 writeBitmap(values, mIcon);
    194             }
    195             values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
    196                     LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);
    197             if (iconResource != null) {
    198                 values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
    199                         iconResource.packageName);
    200                 values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
    201                         iconResource.resourceName);
    202             }
    203         }
    204     }
    205 
    206     @Override
    207     public String toString() {
    208         return "ShortcutInfo(title=" + title.toString() + "intent=" + intent + "id=" + this.id
    209                 + " type=" + this.itemType + " container=" + this.container + " screen=" + screenId
    210                 + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY
    211                 + " dropPos=" + dropPos + ")";
    212     }
    213 
    214     public static void dumpShortcutInfoList(String tag, String label,
    215             ArrayList<ShortcutInfo> list) {
    216         Log.d(tag, label + " size=" + list.size());
    217         for (ShortcutInfo info: list) {
    218             Log.d(tag, "   title=\"" + info.title + " icon=" + info.mIcon
    219                     + " customIcon=" + info.customIcon);
    220         }
    221     }
    222 }
    223 
    224