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.ApplicationInfo;
23 import android.content.pm.LauncherActivityInfo;
24 import android.os.Build;
25 import android.os.Process;
26 import android.os.UserHandle;
27 
28 import com.android.launcher3.compat.UserManagerCompat;
29 import com.android.launcher3.util.ComponentKey;
30 import com.android.launcher3.util.PackageManagerHelper;
31 
32 import java.util.Comparator;
33 
34 /**
35  * Represents an app in AllAppsView.
36  */
37 public class AppInfo extends ItemInfoWithIcon {
38 
39     public static AppInfo[] EMPTY_ARRAY = new AppInfo[0];
40     public static Comparator<AppInfo> COMPONENT_KEY_COMPARATOR = (a, b) -> {
41         int uc = a.user.hashCode() - b.user.hashCode();
42         return uc != 0 ? uc : a.componentName.compareTo(b.componentName);
43     };
44 
45     /**
46      * The intent used to start the application.
47      */
48     public Intent intent;
49 
50     public ComponentName componentName;
51 
52     // Section name used for indexing.
53     public String sectionName = "";
54 
AppInfo()55     public AppInfo() {
56         itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
57     }
58 
59     @Override
getIntent()60     public Intent getIntent() {
61         return intent;
62     }
63 
64     /**
65      * Must not hold the Context.
66      */
AppInfo(Context context, LauncherActivityInfo info, UserHandle user)67     public AppInfo(Context context, LauncherActivityInfo info, UserHandle user) {
68         this(info, user, UserManagerCompat.getInstance(context).isQuietModeEnabled(user));
69     }
70 
AppInfo(LauncherActivityInfo info, UserHandle user, boolean quietModeEnabled)71     public AppInfo(LauncherActivityInfo info, UserHandle user, boolean quietModeEnabled) {
72         this.componentName = info.getComponentName();
73         this.container = ItemInfo.NO_ID;
74         this.user = user;
75         intent = makeLaunchIntent(info);
76 
77         if (quietModeEnabled) {
78             runtimeStatusFlags |= FLAG_DISABLED_QUIET_USER;
79         }
80         updateRuntimeFlagsForActivityTarget(this, info);
81     }
82 
AppInfo(AppInfo info)83     public AppInfo(AppInfo info) {
84         super(info);
85         componentName = info.componentName;
86         title = Utilities.trim(info.title);
87         intent = new Intent(info.intent);
88         user = info.user;
89         runtimeStatusFlags = info.runtimeStatusFlags;
90     }
91 
92     @Override
dumpProperties()93     protected String dumpProperties() {
94         return super.dumpProperties() + " componentName=" + componentName;
95     }
96 
makeWorkspaceItem()97     public WorkspaceItemInfo makeWorkspaceItem() {
98         return new WorkspaceItemInfo(this);
99     }
100 
toComponentKey()101     public ComponentKey toComponentKey() {
102         return new ComponentKey(componentName, user);
103     }
104 
makeLaunchIntent(LauncherActivityInfo info)105     public static Intent makeLaunchIntent(LauncherActivityInfo info) {
106         return makeLaunchIntent(info.getComponentName());
107     }
108 
makeLaunchIntent(ComponentName cn)109     public static Intent makeLaunchIntent(ComponentName cn) {
110         return new Intent(Intent.ACTION_MAIN)
111                 .addCategory(Intent.CATEGORY_LAUNCHER)
112                 .setComponent(cn)
113                 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
114     }
115 
updateRuntimeFlagsForActivityTarget( ItemInfoWithIcon info, LauncherActivityInfo lai)116     public static void updateRuntimeFlagsForActivityTarget(
117             ItemInfoWithIcon info, LauncherActivityInfo lai) {
118         ApplicationInfo appInfo = lai.getApplicationInfo();
119         if (PackageManagerHelper.isAppSuspended(appInfo)) {
120             info.runtimeStatusFlags |= FLAG_DISABLED_SUSPENDED;
121         }
122         info.runtimeStatusFlags |= (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0
123                 ? FLAG_SYSTEM_NO : FLAG_SYSTEM_YES;
124 
125         if (Utilities.ATLEAST_OREO
126                 && appInfo.targetSdkVersion >= Build.VERSION_CODES.O
127                 && Process.myUserHandle().equals(lai.getUser())) {
128             // The icon for a non-primary user is badged, hence it's not exactly an adaptive icon.
129             info.runtimeStatusFlags |= FLAG_ADAPTIVE_ICON;
130         }
131     }
132 
133     @Override
clone()134     public AppInfo clone() {
135         return new AppInfo(this);
136     }
137 }
138