blob: 3053125b4cdbaeff221366f184e8fba62e80565f [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;
29
30import java.util.List;
31
32/**
33 * A helper to query activities of emergency assistance.
34 */
35public class EmergencyAssistanceHelper {
36
37 /**
38 * Query activities of emergency assistance.
39 *
40 * @param context The context of the application.
41 * @return A list of {@link ResolveInfo} which is queried from default assistance package,
42 * or null if there is no installed system application of emergency assistance.
43 */
44 public static List<ResolveInfo> resolveAssistPackageAndQueryActivities(Context context) {
45 List<ResolveInfo> infos = queryAssistActivities(context);
46
47 if (infos == null || infos.isEmpty()) {
48 PackageManager packageManager = context.getPackageManager();
49 Intent queryIntent = new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE);
50 infos = packageManager.queryIntentActivities(queryIntent, 0);
51
52 PackageInfo bestMatch = null;
53 for (int i = 0; i < infos.size(); i++) {
54 if (infos.get(i).activityInfo == null) continue;
55 String packageName = infos.get(i).activityInfo.packageName;
56 PackageInfo packageInfo;
57 try {
58 packageInfo = packageManager.getPackageInfo(packageName, 0);
59 } catch (PackageManager.NameNotFoundException e) {
60 continue;
61 }
62 // Get earliest installed system app.
63 if (isSystemApp(packageInfo) && (bestMatch == null
64 || bestMatch.firstInstallTime > packageInfo.firstInstallTime)) {
65 bestMatch = packageInfo;
66 }
67 }
68
69 if (bestMatch != null) {
70 Settings.Secure.putString(context.getContentResolver(),
71 Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION, bestMatch.packageName);
72 return queryAssistActivities(context);
73 } else {
74 return null;
75 }
76 } else {
77 return infos;
78 }
79 }
80
81 /**
82 * Compose {@link ComponentName} from {@link ResolveInfo}.
83 */
84 public static ComponentName getComponentName(ResolveInfo resolveInfo) {
85 if (resolveInfo == null || resolveInfo.activityInfo == null) return null;
86 return new ComponentName(resolveInfo.activityInfo.packageName,
87 resolveInfo.activityInfo.name);
88 }
89
90 private static List<ResolveInfo> queryAssistActivities(Context context) {
91 final String assistPackage = Settings.Secure.getString(context.getContentResolver(),
92 Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION);
93 List<ResolveInfo> infos = null;
94
95 if (!TextUtils.isEmpty(assistPackage)) {
96 Intent queryIntent = new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE)
97 .setPackage(assistPackage);
98 infos = context.getPackageManager().queryIntentActivities(queryIntent, 0);
99 }
100 return infos;
101 }
102
103 private static boolean isSystemApp(PackageInfo info) {
104 return info.applicationInfo != null
105 && (info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
106 }
107}