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