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.graphics.Bitmap;
     24 import android.util.Log;
     25 
     26 import com.android.launcher3.LauncherSettings.Favorites;
     27 import com.android.launcher3.compat.LauncherActivityInfoCompat;
     28 import com.android.launcher3.compat.UserHandleCompat;
     29 import com.android.launcher3.compat.UserManagerCompat;
     30 
     31 import java.util.ArrayList;
     32 import java.util.Arrays;
     33 
     34 /**
     35  * Represents a launchable icon on the workspaces and in folders.
     36  */
     37 public class ShortcutInfo extends ItemInfo {
     38 
     39     public static final int DEFAULT = 0;
     40 
     41     /**
     42      * The shortcut was restored from a backup and it not ready to be used. This is automatically
     43      * set during backup/restore
     44      */
     45     public static final int FLAG_RESTORED_ICON = 1;
     46 
     47     /**
     48      * The icon was added as an auto-install app, and is not ready to be used. This flag can't
     49      * be present along with {@link #FLAG_RESTORED_ICON}, and is set during default layout
     50      * parsing.
     51      */
     52     public static final int FLAG_AUTOINTALL_ICON = 2; //0B10;
     53 
     54     /**
     55      * The icon is being installed. If {@link FLAG_RESTORED_ICON} or {@link FLAG_AUTOINTALL_ICON}
     56      * is set, then the icon is either being installed or is in a broken state.
     57      */
     58     public static final int FLAG_INSTALL_SESSION_ACTIVE = 4; // 0B100;
     59 
     60     /**
     61      * Indicates that the widget restore has started.
     62      */
     63     public static final int FLAG_RESTORE_STARTED = 8; //0B1000;
     64 
     65     /**
     66      * Indicates if it represents a common type mentioned in {@link CommonAppTypeParser}.
     67      * Upto 15 different types supported.
     68      */
     69     public static final int FLAG_RESTORED_APP_TYPE = 0B0011110000;
     70 
     71     /**
     72      * The intent used to start the application.
     73      */
     74     Intent intent;
     75 
     76     /**
     77      * Indicates whether the icon comes from an application's resource (if false)
     78      * or from a custom Bitmap (if true.)
     79      * TODO: remove this flag
     80      */
     81     public boolean customIcon;
     82 
     83     /**
     84      * Indicates whether we're using the default fallback icon instead of something from the
     85      * app.
     86      */
     87     boolean usingFallbackIcon;
     88 
     89     /**
     90      * Indicates whether we're using a low res icon
     91      */
     92     boolean usingLowResIcon;
     93 
     94     /**
     95      * If isShortcut=true and customIcon=false, this contains a reference to the
     96      * shortcut icon as an application's resource.
     97      */
     98     public Intent.ShortcutIconResource iconResource;
     99 
    100     /**
    101      * The application icon.
    102      */
    103     private Bitmap mIcon;
    104 
    105     /**
    106      * Indicates that the icon is disabled due to safe mode restrictions.
    107      */
    108     public static final int FLAG_DISABLED_SAFEMODE = 1;
    109 
    110     /**
    111      * Indicates that the icon is disabled as the app is not available.
    112      */
    113     public static final int FLAG_DISABLED_NOT_AVAILABLE = 2;
    114 
    115     /**
    116      * Could be disabled, if the the app is installed but unavailable (eg. in safe mode or when
    117      * sd-card is not available).
    118      */
    119     int isDisabled = DEFAULT;
    120 
    121     int status;
    122 
    123     /**
    124      * The installation progress [0-100] of the package that this shortcut represents.
    125      */
    126     private int mInstallProgress;
    127 
    128     /**
    129      * Refer {@link AppInfo#firstInstallTime}.
    130      */
    131     public long firstInstallTime;
    132 
    133     /**
    134      * TODO move this to {@link status}
    135      */
    136     int flags = 0;
    137 
    138     /**
    139      * If this shortcut is a placeholder, then intent will be a market intent for the package, and
    140      * this will hold the original intent from the database.  Otherwise, null.
    141      * Refer {@link #FLAG_RESTORE_PENDING}, {@link #FLAG_INSTALL_PENDING}
    142      */
    143     Intent promisedIntent;
    144 
    145     ShortcutInfo() {
    146         itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
    147     }
    148 
    149     public Intent getIntent() {
    150         return intent;
    151     }
    152 
    153     ShortcutInfo(Intent intent, CharSequence title, CharSequence contentDescription,
    154             Bitmap icon, UserHandleCompat user) {
    155         this();
    156         this.intent = intent;
    157         this.title = Utilities.trim(title);
    158         this.contentDescription = contentDescription;
    159         mIcon = icon;
    160         this.user = user;
    161     }
    162 
    163     public ShortcutInfo(Context context, ShortcutInfo info) {
    164         super(info);
    165         title = Utilities.trim(info.title);
    166         intent = new Intent(info.intent);
    167         if (info.iconResource != null) {
    168             iconResource = new Intent.ShortcutIconResource();
    169             iconResource.packageName = info.iconResource.packageName;
    170             iconResource.resourceName = info.iconResource.resourceName;
    171         }
    172         mIcon = info.mIcon; // TODO: should make a copy here.  maybe we don't need this ctor at all
    173         customIcon = info.customIcon;
    174         flags = info.flags;
    175         firstInstallTime = info.firstInstallTime;
    176         user = info.user;
    177         status = info.status;
    178     }
    179 
    180     /** TODO: Remove this.  It's only called by ApplicationInfo.makeShortcut. */
    181     public ShortcutInfo(AppInfo info) {
    182         super(info);
    183         title = Utilities.trim(info.title);
    184         intent = new Intent(info.intent);
    185         customIcon = false;
    186         flags = info.flags;
    187         firstInstallTime = info.firstInstallTime;
    188     }
    189 
    190     public void setIcon(Bitmap b) {
    191         mIcon = b;
    192     }
    193 
    194     public Bitmap getIcon(IconCache iconCache) {
    195         if (mIcon == null) {
    196             updateIcon(iconCache);
    197         }
    198         return mIcon;
    199     }
    200 
    201     public void updateIcon(IconCache iconCache) {
    202         if (itemType == Favorites.ITEM_TYPE_APPLICATION) {
    203             iconCache.getTitleAndIcon(this, promisedIntent != null ? promisedIntent : intent, user,
    204                     shouldUseLowResIcon());
    205         }
    206     }
    207 
    208     @Override
    209     void onAddToDatabase(Context context, ContentValues values) {
    210         super.onAddToDatabase(context, values);
    211 
    212         String titleStr = title != null ? title.toString() : null;
    213         values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);
    214 
    215         String uri = promisedIntent != null ? promisedIntent.toUri(0)
    216                 : (intent != null ? intent.toUri(0) : null);
    217         values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);
    218         values.put(LauncherSettings.Favorites.RESTORED, status);
    219 
    220         if (customIcon) {
    221             values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
    222                     LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
    223             writeBitmap(values, mIcon);
    224         } else {
    225             if (!usingFallbackIcon) {
    226                 writeBitmap(values, mIcon);
    227             }
    228             if (iconResource != null) {
    229                 values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
    230                         LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);
    231                 values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
    232                         iconResource.packageName);
    233                 values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
    234                         iconResource.resourceName);
    235             }
    236         }
    237     }
    238 
    239     @Override
    240     public String toString() {
    241         return "ShortcutInfo(title=" + title + "intent=" + intent + "id=" + this.id
    242                 + " type=" + this.itemType + " container=" + this.container + " screen=" + screenId
    243                 + " cellX=" + cellX + " cellY=" + cellY + " spanX=" + spanX + " spanY=" + spanY
    244                 + " dropPos=" + Arrays.toString(dropPos) + " user=" + user + ")";
    245     }
    246 
    247     public static void dumpShortcutInfoList(String tag, String label,
    248             ArrayList<ShortcutInfo> list) {
    249         Log.d(tag, label + " size=" + list.size());
    250         for (ShortcutInfo info: list) {
    251             Log.d(tag, "   title=\"" + info.title + " icon=" + info.mIcon
    252                     + " customIcon=" + info.customIcon);
    253         }
    254     }
    255 
    256     public ComponentName getTargetComponent() {
    257         return promisedIntent != null ? promisedIntent.getComponent() : intent.getComponent();
    258     }
    259 
    260     public boolean hasStatusFlag(int flag) {
    261         return (status & flag) != 0;
    262     }
    263 
    264 
    265     public final boolean isPromise() {
    266         return hasStatusFlag(FLAG_RESTORED_ICON | FLAG_AUTOINTALL_ICON);
    267     }
    268 
    269     public int getInstallProgress() {
    270         return mInstallProgress;
    271     }
    272 
    273     public void setInstallProgress(int progress) {
    274         mInstallProgress = progress;
    275         status |= FLAG_INSTALL_SESSION_ACTIVE;
    276     }
    277 
    278     public boolean shouldUseLowResIcon() {
    279         return usingLowResIcon && container >= 0 && rank >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
    280     }
    281 
    282     public static ShortcutInfo fromActivityInfo(LauncherActivityInfoCompat info, Context context) {
    283         final ShortcutInfo shortcut = new ShortcutInfo();
    284         shortcut.user = info.getUser();
    285         shortcut.title = Utilities.trim(info.getLabel());
    286         shortcut.contentDescription = UserManagerCompat.getInstance(context)
    287                 .getBadgedLabelForUser(info.getLabel(), info.getUser());
    288         shortcut.customIcon = false;
    289         shortcut.intent = AppInfo.makeLaunchIntent(context, info, info.getUser());
    290         shortcut.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
    291         shortcut.flags = AppInfo.initFlags(info);
    292         shortcut.firstInstallTime = info.getFirstInstallTime();
    293         return shortcut;
    294     }
    295 }
    296 
    297