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