Home | History | Annotate | Download | only in launcher2
      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.launcher2;
     18 
     19 import java.util.ArrayList;
     20 
     21 import android.content.ComponentName;
     22 import android.content.ContentValues;
     23 import android.content.Intent;
     24 import android.graphics.Bitmap;
     25 import android.util.Log;
     26 
     27 /**
     28  * Represents a launchable icon on the workspaces and in folders.
     29  */
     30 class ShortcutInfo extends ItemInfo {
     31 
     32     /**
     33      * 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     /**
     44      * Indicates whether we're using the default fallback icon instead of something from the
     45      * app.
     46      */
     47     boolean usingFallbackIcon;
     48 
     49     /**
     50      * 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 
     60     ShortcutInfo() {
     61         itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
     62     }
     63 
     64     public ShortcutInfo(ShortcutInfo info) {
     65         super(info);
     66         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. */
     78     public ShortcutInfo(ApplicationInfo info) {
     79         super(info);
     80         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) {
     91             updateIcon(iconCache);
     92         }
     93         return mIcon;
     94     }
     95 
     96     public void updateIcon(IconCache iconCache) {
     97         mIcon = iconCache.getIcon(intent);
     98         usingFallbackIcon = iconCache.isDefaultIcon(mIcon);
     99     }
    100 
    101     /**
    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);
    129             writeBitmap(values, mIcon);
    130         } else {
    131             if (!usingFallbackIcon) {
    132                 writeBitmap(values, mIcon);
    133             }
    134             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() {
    147         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
    150                 + " dropPos=" + dropPos + ")";
    151     }
    152 
    153     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 
    163