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 application name.
     34      */
     35     CharSequence title;
     36 
     37     /**
     38      * The intent used to start the application.
     39      */
     40     Intent intent;
     41 
     42     /**
     43      * Indicates whether the icon comes from an application's resource (if false)
     44      * or from a custom Bitmap (if true.)
     45      */
     46     boolean customIcon;
     47 
     48     /**
     49      * Indicates whether we're using the default fallback icon instead of something from the
     50      * app.
     51      */
     52     boolean usingFallbackIcon;
     53 
     54     /**
     55      * If isShortcut=true and customIcon=false, this contains a reference to the
     56      * shortcut icon as an application's resource.
     57      */
     58     Intent.ShortcutIconResource iconResource;
     59 
     60     /**
     61      * The application icon.
     62      */
     63     private Bitmap mIcon;
     64 
     65     ShortcutInfo() {
     66         itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
     67     }
     68 
     69     public ShortcutInfo(ShortcutInfo info) {
     70         super(info);
     71         title = info.title.toString();
     72         intent = new Intent(info.intent);
     73         if (info.iconResource != null) {
     74             iconResource = new Intent.ShortcutIconResource();
     75             iconResource.packageName = info.iconResource.packageName;
     76             iconResource.resourceName = info.iconResource.resourceName;
     77         }
     78         mIcon = info.mIcon; // TODO: should make a copy here.  maybe we don't need this ctor at all
     79         customIcon = info.customIcon;
     80     }
     81 
     82     /** TODO: Remove this.  It's only called by ApplicationInfo.makeShortcut. */
     83     public ShortcutInfo(ApplicationInfo info) {
     84         super(info);
     85         title = info.title.toString();
     86         intent = new Intent(info.intent);
     87         customIcon = false;
     88     }
     89 
     90     public void setIcon(Bitmap b) {
     91         mIcon = b;
     92     }
     93 
     94     public Bitmap getIcon(IconCache iconCache) {
     95         if (mIcon == null) {
     96             mIcon = iconCache.getIcon(this.intent);
     97             this.usingFallbackIcon = iconCache.isDefaultIcon(mIcon);
     98         }
     99         return mIcon;
    100     }
    101 
    102     /**
    103      * Creates the application intent based on a component name and various launch flags.
    104      * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
    105      *
    106      * @param className the class name of the component representing the intent
    107      * @param launchFlags the launch flags
    108      */
    109     final void setActivity(ComponentName className, int launchFlags) {
    110         intent = new Intent(Intent.ACTION_MAIN);
    111         intent.addCategory(Intent.CATEGORY_LAUNCHER);
    112         intent.setComponent(className);
    113         intent.setFlags(launchFlags);
    114         itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
    115     }
    116 
    117     @Override
    118     void onAddToDatabase(ContentValues values) {
    119         super.onAddToDatabase(values);
    120 
    121         String titleStr = title != null ? title.toString() : null;
    122         values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);
    123 
    124         String uri = intent != null ? intent.toUri(0) : null;
    125         values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);
    126 
    127         if (customIcon) {
    128             values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
    129                     LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
    130             writeBitmap(values, mIcon);
    131         } else {
    132             if (!usingFallbackIcon) {
    133                 writeBitmap(values, mIcon);
    134             }
    135             values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
    136                     LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);
    137             if (iconResource != null) {
    138                 values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
    139                         iconResource.packageName);
    140                 values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
    141                         iconResource.resourceName);
    142             }
    143         }
    144     }
    145 
    146     @Override
    147     public String toString() {
    148         return "ShortcutInfo(title=" + title.toString() + ")";
    149     }
    150 
    151     public static void dumpShortcutInfoList(String tag, String label,
    152             ArrayList<ShortcutInfo> list) {
    153         Log.d(tag, label + " size=" + list.size());
    154         for (ShortcutInfo info: list) {
    155             Log.d(tag, "   title=\"" + info.title + " icon=" + info.mIcon
    156                     + " customIcon=" + info.customIcon);
    157         }
    158     }
    159 }
    160 
    161