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