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.Intent;
     21 import android.content.pm.PackageManager;
     22 import android.content.pm.PackageManager.NameNotFoundException;
     23 import android.content.pm.ResolveInfo;
     24 import android.graphics.Bitmap;
     25 import android.util.Log;
     26 
     27 import java.util.ArrayList;
     28 import java.util.HashMap;
     29 
     30 /**
     31  * Represents an app in AllAppsView.
     32  */
     33 class ApplicationInfo extends ItemInfo {
     34     private static final String TAG = "Launcher2.ApplicationInfo";
     35 
     36     /**
     37      * The application name.
     38      */
     39     CharSequence title;
     40 
     41     /**
     42      * The intent used to start the application.
     43      */
     44     Intent intent;
     45 
     46     /**
     47      * A bitmap version of the application icon.
     48      */
     49     Bitmap iconBitmap;
     50 
     51     /**
     52      * The time at which the app was first installed.
     53      */
     54     long firstInstallTime;
     55 
     56     ComponentName componentName;
     57 
     58     static final int DOWNLOADED_FLAG = 1;
     59     static final int UPDATED_SYSTEM_APP_FLAG = 2;
     60 
     61     int flags = 0;
     62 
     63     ApplicationInfo() {
     64         itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
     65     }
     66 
     67     /**
     68      * Must not hold the Context.
     69      */
     70     public ApplicationInfo(PackageManager pm, ResolveInfo info, IconCache iconCache,
     71             HashMap<Object, CharSequence> labelCache) {
     72         final String packageName = info.activityInfo.applicationInfo.packageName;
     73 
     74         this.componentName = new ComponentName(packageName, info.activityInfo.name);
     75         this.container = ItemInfo.NO_ID;
     76         this.setActivity(componentName,
     77                 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
     78 
     79         try {
     80             int appFlags = pm.getApplicationInfo(packageName, 0).flags;
     81             if ((appFlags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) == 0) {
     82                 flags |= DOWNLOADED_FLAG;
     83 
     84                 if ((appFlags & android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
     85                     flags |= UPDATED_SYSTEM_APP_FLAG;
     86                 }
     87             }
     88             firstInstallTime = pm.getPackageInfo(packageName, 0).firstInstallTime;
     89         } catch (NameNotFoundException e) {
     90             Log.d(TAG, "PackageManager.getApplicationInfo failed for " + packageName);
     91         }
     92 
     93         iconCache.getTitleAndIcon(this, info, labelCache);
     94     }
     95 
     96     public ApplicationInfo(ApplicationInfo info) {
     97         super(info);
     98         componentName = info.componentName;
     99         title = info.title.toString();
    100         intent = new Intent(info.intent);
    101         flags = info.flags;
    102         firstInstallTime = info.firstInstallTime;
    103     }
    104 
    105     /**
    106      * Creates the application intent based on a component name and various launch flags.
    107      * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
    108      *
    109      * @param className the class name of the component representing the intent
    110      * @param launchFlags the launch flags
    111      */
    112     final void setActivity(ComponentName className, int launchFlags) {
    113         intent = new Intent(Intent.ACTION_MAIN);
    114         intent.addCategory(Intent.CATEGORY_LAUNCHER);
    115         intent.setComponent(className);
    116         intent.setFlags(launchFlags);
    117         itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
    118     }
    119 
    120     @Override
    121     public String toString() {
    122         return "ApplicationInfo(title=" + title.toString() + ")";
    123     }
    124 
    125     public static void dumpApplicationInfoList(String tag, String label,
    126             ArrayList<ApplicationInfo> list) {
    127         Log.d(tag, label + " size=" + list.size());
    128         for (ApplicationInfo info: list) {
    129             Log.d(tag, "   title=\"" + info.title + "\" iconBitmap="
    130                     + info.iconBitmap + " firstInstallTime="
    131                     + info.firstInstallTime);
    132         }
    133     }
    134 
    135     public ShortcutInfo makeShortcut() {
    136         return new ShortcutInfo(this);
    137     }
    138 }
    139