blob: c16e98f0c39a7804fe2397f591cd2062c2750a61 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080020import android.content.Intent;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010021import android.content.pm.PackageInfo;
Patrick Dubroy3d605d52010-07-29 13:59:29 -070022import android.content.pm.PackageManager;
Patrick Dubroy3d605d52010-07-29 13:59:29 -070023import android.content.pm.PackageManager.NameNotFoundException;
Winson Chungc3eecff2011-07-11 17:44:15 -070024import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.graphics.Bitmap;
Joe Onoratobe386092009-11-17 17:32:16 -080026import android.util.Log;
27
Patrick Dubroy3d605d52010-07-29 13:59:29 -070028import java.util.ArrayList;
Sameer Padalabe3e4102014-04-21 19:36:14 -070029import java.util.Arrays;
Winson Chungc3eecff2011-07-11 17:44:15 -070030import java.util.HashMap;
Patrick Dubroy3d605d52010-07-29 13:59:29 -070031
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032/**
Joe Onorato0589f0f2010-02-08 13:44:00 -080033 * Represents an app in AllAppsView.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034 */
Anjali Koppal7b168a12014-03-04 17:16:11 -080035public class AppInfo extends ItemInfo {
Michael Jurkaeadbfc52013-09-04 00:45:37 +020036 private static final String TAG = "Launcher3.AppInfo";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037
38 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039 * The intent used to start the application.
40 */
41 Intent intent;
42
43 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -040044 * A bitmap version of the application icon.
45 */
46 Bitmap iconBitmap;
47
Winson Chung78403fe2011-01-21 15:38:02 -080048 /**
49 * The time at which the app was first installed.
50 */
51 long firstInstallTime;
52
Joe Onorato0589f0f2010-02-08 13:44:00 -080053 ComponentName componentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054
Patrick Dubroycd953712011-02-28 15:16:42 -080055 static final int DOWNLOADED_FLAG = 1;
56 static final int UPDATED_SYSTEM_APP_FLAG = 2;
57
Patrick Dubroy3d605d52010-07-29 13:59:29 -070058 int flags = 0;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059
Michael Jurkaeadbfc52013-09-04 00:45:37 +020060 AppInfo() {
Romain Guy73b979d2009-06-09 12:57:21 -070061 itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062 }
Joe Onorato0589f0f2010-02-08 13:44:00 -080063
Anjali Koppale3e646e2014-03-17 09:34:39 -070064 public Intent getIntent() {
Winson Chung997a9232013-07-24 15:33:46 -070065 return intent;
66 }
67
Chris Wrenb6d4c282014-01-27 14:17:08 -050068 protected Intent getRestoredIntent() {
69 return null;
70 }
71
Joe Onorato0589f0f2010-02-08 13:44:00 -080072 /**
73 * Must not hold the Context.
74 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +020075 public AppInfo(PackageManager pm, ResolveInfo info, IconCache iconCache,
Michael Jurkac9d95c52011-08-29 14:03:34 -070076 HashMap<Object, CharSequence> labelCache) {
Patrick Dubroy3d605d52010-07-29 13:59:29 -070077 final String packageName = info.activityInfo.applicationInfo.packageName;
Joe Onorato0589f0f2010-02-08 13:44:00 -080078
Patrick Dubroy3d605d52010-07-29 13:59:29 -070079 this.componentName = new ComponentName(packageName, info.activityInfo.name);
Joe Onorato0589f0f2010-02-08 13:44:00 -080080 this.container = ItemInfo.NO_ID;
81 this.setActivity(componentName,
82 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
83
Patrick Dubroy3d605d52010-07-29 13:59:29 -070084 try {
Michael Jurka1e2f4652013-07-08 18:03:46 -070085 PackageInfo pi = pm.getPackageInfo(packageName, 0);
86 flags = initFlags(pi);
87 firstInstallTime = initFirstInstallTime(pi);
Patrick Dubroy3d605d52010-07-29 13:59:29 -070088 } catch (NameNotFoundException e) {
89 Log.d(TAG, "PackageManager.getApplicationInfo failed for " + packageName);
90 }
91
Winson Chungc3eecff2011-07-11 17:44:15 -070092 iconCache.getTitleAndIcon(this, info, labelCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -080093 }
Winson Chungc3eecff2011-07-11 17:44:15 -070094
Michael Jurka1e2f4652013-07-08 18:03:46 -070095 public static int initFlags(PackageInfo pi) {
96 int appFlags = pi.applicationInfo.flags;
97 int flags = 0;
98 if ((appFlags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) == 0) {
99 flags |= DOWNLOADED_FLAG;
100
101 if ((appFlags & android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
102 flags |= UPDATED_SYSTEM_APP_FLAG;
103 }
104 }
105 return flags;
106 }
107
108 public static long initFirstInstallTime(PackageInfo pi) {
109 return pi.firstInstallTime;
110 }
111
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200112 public AppInfo(AppInfo info) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700113 super(info);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800114 componentName = info.componentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 title = info.title.toString();
116 intent = new Intent(info.intent);
Patrick Dubroy430c53b2010-09-08 16:01:19 -0700117 flags = info.flags;
Winson Chung78403fe2011-01-21 15:38:02 -0800118 firstInstallTime = info.firstInstallTime;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 }
120
121 /**
122 * Creates the application intent based on a component name and various launch flags.
Romain Guy73b979d2009-06-09 12:57:21 -0700123 * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 *
125 * @param className the class name of the component representing the intent
126 * @param launchFlags the launch flags
127 */
128 final void setActivity(ComponentName className, int launchFlags) {
129 intent = new Intent(Intent.ACTION_MAIN);
130 intent.addCategory(Intent.CATEGORY_LAUNCHER);
131 intent.setComponent(className);
132 intent.setFlags(launchFlags);
Romain Guy73b979d2009-06-09 12:57:21 -0700133 itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 }
135
136 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 public String toString() {
Winson Chung64359a52013-07-08 17:17:08 -0700138 return "ApplicationInfo(title=" + title.toString() + " id=" + this.id
139 + " type=" + this.itemType + " container=" + this.container
140 + " screen=" + screenId + " cellX=" + cellX + " cellY=" + cellY
Sameer Padalabe3e4102014-04-21 19:36:14 -0700141 + " spanX=" + spanX + " spanY=" + spanY + " dropPos=" + Arrays.toString(dropPos)
142 + ")";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400144
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200145 public static void dumpApplicationInfoList(String tag, String label, ArrayList<AppInfo> list) {
Joe Onoratobe386092009-11-17 17:32:16 -0800146 Log.d(tag, label + " size=" + list.size());
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200147 for (AppInfo info: list) {
Winson Chungc85ea572011-07-11 16:02:31 -0700148 Log.d(tag, " title=\"" + info.title + "\" iconBitmap="
149 + info.iconBitmap + " firstInstallTime="
Winson Chung78403fe2011-01-21 15:38:02 -0800150 + info.firstInstallTime);
Joe Onoratobe386092009-11-17 17:32:16 -0800151 }
152 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800153
154 public ShortcutInfo makeShortcut() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700155 return new ShortcutInfo(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800156 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157}