blob: 1bf212e329eaf364f64a68f7ec20d1b071ac5e54 [file] [log] [blame]
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001/*
2 * Copyright (C) 2018 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
17package com.android.phone;
18
Hongming Jin862ce042019-02-05 15:42:50 -080019import android.app.role.RoleManager;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080020import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.ApplicationInfo;
24import android.content.pm.PackageInfo;
25import android.content.pm.PackageManager;
26import android.content.pm.ResolveInfo;
Hongming Jin862ce042019-02-05 15:42:50 -080027import android.os.AsyncTask;
28import android.os.Binder;
29import android.os.Process;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080030import android.telephony.TelephonyManager;
31import android.text.TextUtils;
Hongming Jin862ce042019-02-05 15:42:50 -080032import android.util.Log;
33
34import com.android.internal.util.CollectionUtils;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080035
36import java.util.List;
37
38/**
39 * A helper to query activities of emergency assistance.
40 */
41public class EmergencyAssistanceHelper {
Hongming Jin862ce042019-02-05 15:42:50 -080042 private static final String TAG = EmergencyAssistanceHelper.class.getSimpleName();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080043 /**
yuanjiahsubc20ba82018-11-26 22:57:18 +080044 * Get intent action of target emergency app.
45 *
46 * @param context The context of the application.
47 * @return A string of intent action to launch target emergency app by feature flag, it will be
48 * used for team food.
49 */
50 public static String getIntentAction(Context context) {
yuanjiahsu8f3b1152019-03-13 15:33:23 +080051 String action = context.getResources().getString(R.string.config_emergency_app_intent);
52 if (!action.isEmpty()) {
yuanjiahsu8f3b1152019-03-13 15:33:23 +080053 return action;
yuanjiahsubc20ba82018-11-26 22:57:18 +080054 }
55
56 return TelephonyManager.ACTION_EMERGENCY_ASSISTANCE;
57 }
58
59 /**
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080060 * Query activities of emergency assistance.
61 *
62 * @param context The context of the application.
63 * @return A list of {@link ResolveInfo} which is queried from default assistance package,
64 * or null if there is no installed system application of emergency assistance.
65 */
66 public static List<ResolveInfo> resolveAssistPackageAndQueryActivities(Context context) {
Hongming Jin862ce042019-02-05 15:42:50 -080067 final String assistPackage = getDefaultEmergencyPackage(context);
68 List<ResolveInfo> infos = queryAssistActivities(context, assistPackage);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080069 if (infos == null || infos.isEmpty()) {
70 PackageManager packageManager = context.getPackageManager();
yuanjiahsubc20ba82018-11-26 22:57:18 +080071 Intent queryIntent = new Intent(getIntentAction(context));
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080072 infos = packageManager.queryIntentActivities(queryIntent, 0);
73
74 PackageInfo bestMatch = null;
75 for (int i = 0; i < infos.size(); i++) {
76 if (infos.get(i).activityInfo == null) continue;
77 String packageName = infos.get(i).activityInfo.packageName;
78 PackageInfo packageInfo;
79 try {
80 packageInfo = packageManager.getPackageInfo(packageName, 0);
81 } catch (PackageManager.NameNotFoundException e) {
82 continue;
83 }
84 // Get earliest installed system app.
85 if (isSystemApp(packageInfo) && (bestMatch == null
86 || bestMatch.firstInstallTime > packageInfo.firstInstallTime)) {
87 bestMatch = packageInfo;
88 }
89 }
90
91 if (bestMatch != null) {
Hongming Jin862ce042019-02-05 15:42:50 -080092 setDefaultEmergencyPackageAsync(context, bestMatch.packageName);
93 return queryAssistActivities(context, bestMatch.packageName);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080094 } else {
95 return null;
96 }
97 } else {
98 return infos;
99 }
100 }
101
102 /**
103 * Compose {@link ComponentName} from {@link ResolveInfo}.
104 */
105 public static ComponentName getComponentName(ResolveInfo resolveInfo) {
106 if (resolveInfo == null || resolveInfo.activityInfo == null) return null;
107 return new ComponentName(resolveInfo.activityInfo.packageName,
108 resolveInfo.activityInfo.name);
109 }
110
Hongming Jin862ce042019-02-05 15:42:50 -0800111 private static List<ResolveInfo> queryAssistActivities(Context context, String assistPackage) {
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800112 List<ResolveInfo> infos = null;
113
114 if (!TextUtils.isEmpty(assistPackage)) {
yuanjiahsubc20ba82018-11-26 22:57:18 +0800115 Intent queryIntent = new Intent(getIntentAction(context))
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800116 .setPackage(assistPackage);
117 infos = context.getPackageManager().queryIntentActivities(queryIntent, 0);
118 }
119 return infos;
120 }
121
122 private static boolean isSystemApp(PackageInfo info) {
123 return info.applicationInfo != null
124 && (info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
125 }
Hongming Jin862ce042019-02-05 15:42:50 -0800126
127 private static String getDefaultEmergencyPackage(Context context) {
128 long identity = Binder.clearCallingIdentity();
129 try {
130 return CollectionUtils.firstOrNull(context.getSystemService(RoleManager.class)
131 .getRoleHolders(RoleManager.ROLE_EMERGENCY));
132 } finally {
133 Binder.restoreCallingIdentity(identity);
134 }
135 }
136
137 private static boolean setDefaultEmergencyPackageAsync(Context context, String pkgName) {
138 long identity = Binder.clearCallingIdentity();
139 try {
140 context.getSystemService(RoleManager.class).addRoleHolderAsUser(
141 RoleManager.ROLE_EMERGENCY, pkgName, 0, Process.myUserHandle(),
Hai Zhang651617b2019-03-06 20:40:39 -0800142 AsyncTask.THREAD_POOL_EXECUTOR, successful -> {
143 if (!successful) {
Hongming Jin862ce042019-02-05 15:42:50 -0800144 Log.e(TAG, "Failed to set emergency default app.");
145 }
146 });
147 } finally {
148 Binder.restoreCallingIdentity(identity);
149 }
150 return true;
151 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800152}