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.PackageManager;
     24 import android.content.pm.PackageManager.NameNotFoundException;
     25 import android.content.pm.PackageInfo;
     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     ShortcutInfo() {
     68         itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
     69     }
     70 
     71     protected Intent getIntent() {
     72         return intent;
     73     }
     74 
     75     public ShortcutInfo(Context context, ShortcutInfo info) {
     76         super(info);
     77         title = info.title.toString();
     78         intent = new Intent(info.intent);
     79         if (info.iconResource != null) {
     80             iconResource = new Intent.ShortcutIconResource();
     81             iconResource.packageName = info.iconResource.packageName;
     82             iconResource.resourceName = info.iconResource.resourceName;
     83         }
     84         mIcon = info.mIcon; // TODO: should make a copy here.  maybe we don't need this ctor at all
     85         customIcon = info.customIcon;
     86         initFlagsAndFirstInstallTime(
     87                 getPackageInfo(context, intent.getComponent().getPackageName()));
     88     }
     89 
     90     /** TODO: Remove this.  It's only called by ApplicationInfo.makeShortcut. */
     91     public ShortcutInfo(AppInfo info) {
     92         super(info);
     93         title = info.title.toString();
     94         intent = new Intent(info.intent);
     95         customIcon = false;
     96         flags = info.flags;
     97         firstInstallTime = info.firstInstallTime;
     98     }
     99 
    100     public static PackageInfo getPackageInfo(Context context, String packageName) {
    101         PackageInfo pi = null;
    102         try {
    103             PackageManager pm = context.getPackageManager();
    104             pi = pm.getPackageInfo(packageName, 0);
    105         } catch (NameNotFoundException e) {
    106             Log.d("ShortcutInfo", "PackageManager.getPackageInfo failed for " + packageName);
    107         }
    108         return pi;
    109     }
    110 
    111     void initFlagsAndFirstInstallTime(PackageInfo pi) {
    112         flags = AppInfo.initFlags(pi);
    113         firstInstallTime = AppInfo.initFirstInstallTime(pi);
    114     }
    115 
    116     public void setIcon(Bitmap b) {
    117         mIcon = b;
    118     }
    119 
    120     public Bitmap getIcon(IconCache iconCache) {
    121         if (mIcon == null) {
    122             updateIcon(iconCache);
    123         }
    124         return mIcon;
    125     }
    126 
    127     public void updateIcon(IconCache iconCache) {
    128         mIcon = iconCache.getIcon(intent);
    129         usingFallbackIcon = iconCache.isDefaultIcon(mIcon);
    130     }
    131 
    132     /**
    133      * Creates the application intent based on a component name and various launch flags.
    134      * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
    135      *
    136      * @param className the class name of the component representing the intent
    137      * @param launchFlags the launch flags
    138      */
    139     final void setActivity(Context context, ComponentName className, int launchFlags) {
    140         intent = new Intent(Intent.ACTION_MAIN);
    141         intent.addCategory(Intent.CATEGORY_LAUNCHER);
    142         intent.setComponent(className);
    143         intent.setFlags(launchFlags);
    144         itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
    145         initFlagsAndFirstInstallTime(
    146                 getPackageInfo(context, intent.getComponent().getPackageName()));
    147     }
    148 
    149     @Override
    150     void onAddToDatabase(ContentValues values) {
    151         super.onAddToDatabase(values);
    152 
    153         String titleStr = title != null ? title.toString() : null;
    154         values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);
    155 
    156         String uri = intent != null ? intent.toUri(0) : null;
    157         values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);
    158 
    159         if (customIcon) {
    160             values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
    161                     LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
    162             writeBitmap(values, mIcon);
    163         } else {
    164             if (!usingFallbackIcon) {
    165                 writeBitmap(values, mIcon);
    166             }
    167             values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
    168                     LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);
    169             if (iconResource != null) {
    170                 values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
    171                         iconResource.packageName);
    172                 values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
    173                         iconResource.resourceName);
    174             }
    175         }
    176     }
    177 
    178     @Override
    179     public String toString() {
    180         return "ShortcutInfo(title=" + title.toString() + "intent=" + intent + "id=" + this.id
    181                 + " type=" + this.itemType + " container=" + this.container + " screen=" + screenId
    182                 + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY
    183                 + " dropPos=" + dropPos + ")";
    184     }
    185 
    186     public static void dumpShortcutInfoList(String tag, String label,
    187             ArrayList<ShortcutInfo> list) {
    188         Log.d(tag, label + " size=" + list.size());
    189         for (ShortcutInfo info: list) {
    190             Log.d(tag, "   title=\"" + info.title + " icon=" + info.mIcon
    191                     + " customIcon=" + info.customIcon);
    192         }
    193     }
    194 }
    195 
    196