blob: 5e8cca8ecc60c745b3134406885b502ac2d797ba [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;
Winson Chungc3eecff2011-07-11 17:44:15 -070029import java.util.HashMap;
Patrick Dubroy3d605d52010-07-29 13:59:29 -070030
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031/**
Joe Onorato0589f0f2010-02-08 13:44:00 -080032 * Represents an app in AllAppsView.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033 */
Anjali Koppal7b168a12014-03-04 17:16:11 -080034public class AppInfo extends ItemInfo {
Michael Jurkaeadbfc52013-09-04 00:45:37 +020035 private static final String TAG = "Launcher3.AppInfo";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036
37 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038 * The intent used to start the application.
39 */
40 Intent intent;
41
42 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -040043 * A bitmap version of the application icon.
44 */
45 Bitmap iconBitmap;
46
Winson Chung78403fe2011-01-21 15:38:02 -080047 /**
48 * The time at which the app was first installed.
49 */
50 long firstInstallTime;
51
Joe Onorato0589f0f2010-02-08 13:44:00 -080052 ComponentName componentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
Patrick Dubroycd953712011-02-28 15:16:42 -080054 static final int DOWNLOADED_FLAG = 1;
55 static final int UPDATED_SYSTEM_APP_FLAG = 2;
56
Patrick Dubroy3d605d52010-07-29 13:59:29 -070057 int flags = 0;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058
Michael Jurkaeadbfc52013-09-04 00:45:37 +020059 AppInfo() {
Romain Guy73b979d2009-06-09 12:57:21 -070060 itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061 }
Joe Onorato0589f0f2010-02-08 13:44:00 -080062
Winson Chung997a9232013-07-24 15:33:46 -070063 protected Intent getIntent() {
64 return intent;
65 }
66
Chris Wrenb6d4c282014-01-27 14:17:08 -050067 protected Intent getRestoredIntent() {
68 return null;
69 }
70
Joe Onorato0589f0f2010-02-08 13:44:00 -080071 /**
72 * Must not hold the Context.
73 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +020074 public AppInfo(PackageManager pm, ResolveInfo info, IconCache iconCache,
Michael Jurkac9d95c52011-08-29 14:03:34 -070075 HashMap<Object, CharSequence> labelCache) {
Patrick Dubroy3d605d52010-07-29 13:59:29 -070076 final String packageName = info.activityInfo.applicationInfo.packageName;
Joe Onorato0589f0f2010-02-08 13:44:00 -080077
Patrick Dubroy3d605d52010-07-29 13:59:29 -070078 this.componentName = new ComponentName(packageName, info.activityInfo.name);
Joe Onorato0589f0f2010-02-08 13:44:00 -080079 this.container = ItemInfo.NO_ID;
80 this.setActivity(componentName,
81 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
82
Patrick Dubroy3d605d52010-07-29 13:59:29 -070083 try {
Michael Jurka1e2f4652013-07-08 18:03:46 -070084 PackageInfo pi = pm.getPackageInfo(packageName, 0);
85 flags = initFlags(pi);
86 firstInstallTime = initFirstInstallTime(pi);
Patrick Dubroy3d605d52010-07-29 13:59:29 -070087 } catch (NameNotFoundException e) {
88 Log.d(TAG, "PackageManager.getApplicationInfo failed for " + packageName);
89 }
90
Winson Chungc3eecff2011-07-11 17:44:15 -070091 iconCache.getTitleAndIcon(this, info, labelCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -080092 }
Winson Chungc3eecff2011-07-11 17:44:15 -070093
Michael Jurka1e2f4652013-07-08 18:03:46 -070094 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
107 public static long initFirstInstallTime(PackageInfo pi) {
108 return pi.firstInstallTime;
109 }
110
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200111 public AppInfo(AppInfo info) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700112 super(info);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800113 componentName = info.componentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 title = info.title.toString();
115 intent = new Intent(info.intent);
Patrick Dubroy430c53b2010-09-08 16:01:19 -0700116 flags = info.flags;
Winson Chung78403fe2011-01-21 15:38:02 -0800117 firstInstallTime = info.firstInstallTime;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 }
119
120 /**
121 * Creates the application intent based on a component name and various launch flags.
Romain Guy73b979d2009-06-09 12:57:21 -0700122 * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 *
124 * @param className the class name of the component representing the intent
125 * @param launchFlags the launch flags
126 */
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);
Romain Guy73b979d2009-06-09 12:57:21 -0700132 itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 }
134
135 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 public String toString() {
Winson Chung64359a52013-07-08 17:17:08 -0700137 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 + ")";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400142
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200143 public static void dumpApplicationInfoList(String tag, String label, ArrayList<AppInfo> list) {
Joe Onoratobe386092009-11-17 17:32:16 -0800144 Log.d(tag, label + " size=" + list.size());
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200145 for (AppInfo info: list) {
Winson Chungc85ea572011-07-11 16:02:31 -0700146 Log.d(tag, " title=\"" + info.title + "\" iconBitmap="
147 + info.iconBitmap + " firstInstallTime="
Winson Chung78403fe2011-01-21 15:38:02 -0800148 + info.firstInstallTime);
Joe Onoratobe386092009-11-17 17:32:16 -0800149 }
150 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800151
152 public ShortcutInfo makeShortcut() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700153 return new ShortcutInfo(this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800154 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155}