Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.phone; |
| 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.PackageInfo; |
| 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.pm.ResolveInfo; |
| 26 | import android.provider.Settings; |
| 27 | import android.telephony.TelephonyManager; |
| 28 | import android.text.TextUtils; |
yuanjiahsu | bc20ba8 | 2018-11-26 22:57:18 +0800 | [diff] [blame^] | 29 | import android.util.FeatureFlagUtils; |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 30 | |
| 31 | import java.util.List; |
| 32 | |
| 33 | /** |
| 34 | * A helper to query activities of emergency assistance. |
| 35 | */ |
| 36 | public class EmergencyAssistanceHelper { |
| 37 | |
| 38 | /** |
yuanjiahsu | bc20ba8 | 2018-11-26 22:57:18 +0800 | [diff] [blame^] | 39 | * 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 Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 57 | * 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(); |
yuanjiahsu | bc20ba8 | 2018-11-26 22:57:18 +0800 | [diff] [blame^] | 68 | Intent queryIntent = new Intent(getIntentAction(context)); |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 69 | 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)) { |
yuanjiahsu | bc20ba8 | 2018-11-26 22:57:18 +0800 | [diff] [blame^] | 115 | Intent queryIntent = new Intent(getIntentAction(context)) |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 116 | .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 | } |