blob: 7e6c28239ea2a3e27c5fa1df2402cb31fab26037 [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;
yuanjiahsubc20ba82018-11-26 22:57:18 +080032import android.util.FeatureFlagUtils;
Hongming Jin862ce042019-02-05 15:42:50 -080033import android.util.Log;
34
35import com.android.internal.util.CollectionUtils;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080036
37import java.util.List;
38
39/**
40 * A helper to query activities of emergency assistance.
41 */
42public class EmergencyAssistanceHelper {
Hongming Jin862ce042019-02-05 15:42:50 -080043 private static final String TAG = EmergencyAssistanceHelper.class.getSimpleName();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080044 /**
yuanjiahsubc20ba82018-11-26 22:57:18 +080045 * Get intent action of target emergency app.
46 *
47 * @param context The context of the application.
48 * @return A string of intent action to launch target emergency app by feature flag, it will be
49 * used for team food.
50 */
51 public static String getIntentAction(Context context) {
52 if (FeatureFlagUtils.isEnabled(context, FeatureFlagUtils.SAFETY_HUB)) {
53 String action = context.getResources().getString(R.string.config_emergency_app_intent);
54 if (!action.isEmpty()) {
55 return action;
56 }
57 }
58
59 return TelephonyManager.ACTION_EMERGENCY_ASSISTANCE;
60 }
61
62 /**
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080063 * Query activities of emergency assistance.
64 *
65 * @param context The context of the application.
66 * @return A list of {@link ResolveInfo} which is queried from default assistance package,
67 * or null if there is no installed system application of emergency assistance.
68 */
69 public static List<ResolveInfo> resolveAssistPackageAndQueryActivities(Context context) {
Hongming Jin862ce042019-02-05 15:42:50 -080070 final String assistPackage = getDefaultEmergencyPackage(context);
71 List<ResolveInfo> infos = queryAssistActivities(context, assistPackage);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080072 if (infos == null || infos.isEmpty()) {
73 PackageManager packageManager = context.getPackageManager();
yuanjiahsubc20ba82018-11-26 22:57:18 +080074 Intent queryIntent = new Intent(getIntentAction(context));
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080075 infos = packageManager.queryIntentActivities(queryIntent, 0);
76
77 PackageInfo bestMatch = null;
78 for (int i = 0; i < infos.size(); i++) {
79 if (infos.get(i).activityInfo == null) continue;
80 String packageName = infos.get(i).activityInfo.packageName;
81 PackageInfo packageInfo;
82 try {
83 packageInfo = packageManager.getPackageInfo(packageName, 0);
84 } catch (PackageManager.NameNotFoundException e) {
85 continue;
86 }
87 // Get earliest installed system app.
88 if (isSystemApp(packageInfo) && (bestMatch == null
89 || bestMatch.firstInstallTime > packageInfo.firstInstallTime)) {
90 bestMatch = packageInfo;
91 }
92 }
93
94 if (bestMatch != null) {
Hongming Jin862ce042019-02-05 15:42:50 -080095 setDefaultEmergencyPackageAsync(context, bestMatch.packageName);
96 return queryAssistActivities(context, bestMatch.packageName);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080097 } else {
98 return null;
99 }
100 } else {
101 return infos;
102 }
103 }
104
105 /**
106 * Compose {@link ComponentName} from {@link ResolveInfo}.
107 */
108 public static ComponentName getComponentName(ResolveInfo resolveInfo) {
109 if (resolveInfo == null || resolveInfo.activityInfo == null) return null;
110 return new ComponentName(resolveInfo.activityInfo.packageName,
111 resolveInfo.activityInfo.name);
112 }
113
Hongming Jin862ce042019-02-05 15:42:50 -0800114 private static List<ResolveInfo> queryAssistActivities(Context context, String assistPackage) {
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800115 List<ResolveInfo> infos = null;
116
117 if (!TextUtils.isEmpty(assistPackage)) {
yuanjiahsubc20ba82018-11-26 22:57:18 +0800118 Intent queryIntent = new Intent(getIntentAction(context))
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800119 .setPackage(assistPackage);
120 infos = context.getPackageManager().queryIntentActivities(queryIntent, 0);
121 }
122 return infos;
123 }
124
125 private static boolean isSystemApp(PackageInfo info) {
126 return info.applicationInfo != null
127 && (info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
128 }
Hongming Jin862ce042019-02-05 15:42:50 -0800129
130 private static String getDefaultEmergencyPackage(Context context) {
131 long identity = Binder.clearCallingIdentity();
132 try {
133 return CollectionUtils.firstOrNull(context.getSystemService(RoleManager.class)
134 .getRoleHolders(RoleManager.ROLE_EMERGENCY));
135 } finally {
136 Binder.restoreCallingIdentity(identity);
137 }
138 }
139
140 private static boolean setDefaultEmergencyPackageAsync(Context context, String pkgName) {
141 long identity = Binder.clearCallingIdentity();
142 try {
143 context.getSystemService(RoleManager.class).addRoleHolderAsUser(
144 RoleManager.ROLE_EMERGENCY, pkgName, 0, Process.myUserHandle(),
Hai Zhang651617b2019-03-06 20:40:39 -0800145 AsyncTask.THREAD_POOL_EXECUTOR, successful -> {
146 if (!successful) {
Hongming Jin862ce042019-02-05 15:42:50 -0800147 Log.e(TAG, "Failed to set emergency default app.");
148 }
149 });
150 } finally {
151 Binder.restoreCallingIdentity(identity);
152 }
153 return true;
154 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800155}