1 /*
2  * Copyright (C) 2016 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 package com.android.settings.accounts;
17 
18 import android.content.Context;
19 import android.content.Intent;
20 import android.content.pm.ResolveInfo;
21 import android.content.pm.UserInfo;
22 import android.content.res.Resources;
23 import android.os.UserHandle;
24 import android.os.UserManager;
25 import android.text.TextUtils;
26 
27 import androidx.annotation.VisibleForTesting;
28 import androidx.preference.Preference;
29 
30 import com.android.settings.R;
31 import com.android.settings.core.BasePreferenceController;
32 import com.android.settingslib.search.SearchIndexableRaw;
33 
34 import java.util.List;
35 
36 public class EmergencyInfoPreferenceController extends BasePreferenceController {
37 
38     @VisibleForTesting
39     Intent mIntent;
40 
EmergencyInfoPreferenceController(Context context, String preferenceKey)41     public EmergencyInfoPreferenceController(Context context, String preferenceKey) {
42         super(context, preferenceKey);
43     }
44 
45     @Override
updateRawDataToIndex(List<SearchIndexableRaw> rawData)46     public void updateRawDataToIndex(List<SearchIndexableRaw> rawData) {
47         if (isAvailable()) {
48             SearchIndexableRaw data = new SearchIndexableRaw(mContext);
49             final Resources res = mContext.getResources();
50             data.title = res.getString(com.android.settings.R.string.emergency_info_title);
51             data.screenTitle = res.getString(com.android.settings.R.string.emergency_info_title);
52             rawData.add(data);
53         }
54     }
55 
56     @Override
updateState(Preference preference)57     public void updateState(Preference preference) {
58         UserInfo info = mContext.getSystemService(UserManager.class).getUserInfo(
59                 UserHandle.myUserId());
60         preference.setSummary(mContext.getString(R.string.emergency_info_summary, info.name));
61     }
62 
63     @Override
handlePreferenceTreeClick(Preference preference)64     public boolean handlePreferenceTreeClick(Preference preference) {
65         if (TextUtils.equals(getPreferenceKey(), preference.getKey()) && mIntent != null) {
66             mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
67             mContext.startActivity(mIntent);
68             return true;
69         }
70         return false;
71     }
72 
73     @Override
getAvailabilityStatus()74     public int getAvailabilityStatus() {
75         if (!mContext.getResources().getBoolean(R.bool.config_show_emergency_info_in_device_info)) {
76             return UNSUPPORTED_ON_DEVICE;
77         }
78 
79         // If the variant of emergency info can not work, we should fallback to AOSP version.
80         if (isEmergencyInfoSupported()) {
81             return AVAILABLE;
82         } else if (isAOSPVersionSupported()) {
83             return AVAILABLE;
84         }
85         return UNSUPPORTED_ON_DEVICE;
86     }
87 
isEmergencyInfoSupported()88     private boolean isEmergencyInfoSupported() {
89         final String packageName = mContext.getResources().getString(
90                 R.string.config_emergency_package_name);
91         final String intentName = mContext.getResources().getString(
92                 R.string.config_emergency_intent_action);
93         mIntent = new Intent(intentName).setPackage(packageName);
94         final List<ResolveInfo> infos = mContext.getPackageManager().queryIntentActivities(mIntent,
95                 0);
96 
97         return infos != null && !infos.isEmpty();
98     }
99 
isAOSPVersionSupported()100     private boolean isAOSPVersionSupported() {
101         final String aospPackageName = mContext.getResources().getString(
102                 R.string.config_aosp_emergency_package_name);
103         final String aospIntentName = mContext.getResources().getString(
104                 R.string.config_aosp_emergency_intent_action);
105 
106         mIntent = new Intent(aospIntentName).setPackage(aospPackageName);
107         final List<ResolveInfo> infos = mContext.getPackageManager().queryIntentActivities(mIntent,
108                 0);
109 
110         return infos != null && !infos.isEmpty();
111     }
112 }
113