blob: 9e1ac21805d697b968578a481f6e44501f6e6000 [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
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.ApplicationInfo;
23import android.content.pm.PackageInfo;
24import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
26import android.provider.Settings;
27import android.telephony.TelephonyManager;
28import android.text.TextUtils;
yuanjiahsubc20ba82018-11-26 22:57:18 +080029import android.util.FeatureFlagUtils;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080030
31import java.util.List;
32
33/**
34 * A helper to query activities of emergency assistance.
35 */
36public class EmergencyAssistanceHelper {
37
38 /**
yuanjiahsubc20ba82018-11-26 22:57:18 +080039 * Get intent action of target emergency app.
40 *
41 * @param context The context of the application.
42 * @return A string of intent action to launch target emergency app by feature flag, it will be
43 * used for team food.
44 */
45 public static String getIntentAction(Context context) {
46 if (FeatureFlagUtils.isEnabled(context, FeatureFlagUtils.SAFETY_HUB)) {
47 String action = context.getResources().getString(R.string.config_emergency_app_intent);
48 if (!action.isEmpty()) {
49 return action;
50 }
51 }
52
53 return TelephonyManager.ACTION_EMERGENCY_ASSISTANCE;
54 }
55
56 /**
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080057 * Query activities of emergency assistance.
58 *
59 * @param context The context of the application.
60 * @return A list of {@link ResolveInfo} which is queried from default assistance package,
61 * or null if there is no installed system application of emergency assistance.
62 */
63 public static List<ResolveInfo> resolveAssistPackageAndQueryActivities(Context context) {
64 List<ResolveInfo> infos = queryAssistActivities(context);
65
66 if (infos == null || infos.isEmpty()) {
67 PackageManager packageManager = context.getPackageManager();
yuanjiahsubc20ba82018-11-26 22:57:18 +080068 Intent queryIntent = new Intent(getIntentAction(context));
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080069 infos = packageManager.queryIntentActivities(queryIntent, 0);
70
71 PackageInfo bestMatch = null;
72 for (int i = 0; i < infos.size(); i++) {
73 if (infos.get(i).activityInfo == null) continue;
74 String packageName = infos.get(i).activityInfo.packageName;
75 PackageInfo packageInfo;
76 try {
77 packageInfo = packageManager.getPackageInfo(packageName, 0);
78 } catch (PackageManager.NameNotFoundException e) {
79 continue;
80 }
81 // Get earliest installed system app.
82 if (isSystemApp(packageInfo) && (bestMatch == null
83 || bestMatch.firstInstallTime > packageInfo.firstInstallTime)) {
84 bestMatch = packageInfo;
85 }
86 }
87
88 if (bestMatch != null) {
89 Settings.Secure.putString(context.getContentResolver(),
90 Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION, bestMatch.packageName);
91 return queryAssistActivities(context);
92 } else {
93 return null;
94 }
95 } else {
96 return infos;
97 }
98 }
99
100 /**
101 * Compose {@link ComponentName} from {@link ResolveInfo}.
102 */
103 public static ComponentName getComponentName(ResolveInfo resolveInfo) {
104 if (resolveInfo == null || resolveInfo.activityInfo == null) return null;
105 return new ComponentName(resolveInfo.activityInfo.packageName,
106 resolveInfo.activityInfo.name);
107 }
108
109 private static List<ResolveInfo> queryAssistActivities(Context context) {
110 final String assistPackage = Settings.Secure.getString(context.getContentResolver(),
111 Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION);
112 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 }
126}