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