Doris Ling | c7e2ed8 | 2016-10-27 13:09:16 -0700 | [diff] [blame] | 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; |
Doris Ling | 829d3aa | 2017-02-03 15:06:56 -0800 | [diff] [blame] | 21 | import android.content.pm.UserInfo; |
Doris Ling | c7e2ed8 | 2016-10-27 13:09:16 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
Doris Ling | 829d3aa | 2017-02-03 15:06:56 -0800 | [diff] [blame] | 23 | import android.os.UserHandle; |
| 24 | import android.os.UserManager; |
Fan Zhang | 5c3301b | 2019-01-28 13:10:32 -0800 | [diff] [blame] | 25 | import android.text.TextUtils; |
Doris Ling | c7e2ed8 | 2016-10-27 13:09:16 -0700 | [diff] [blame] | 26 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 27 | import androidx.preference.Preference; |
| 28 | |
Doris Ling | 829d3aa | 2017-02-03 15:06:56 -0800 | [diff] [blame] | 29 | import com.android.settings.R; |
Fan Zhang | 5c3301b | 2019-01-28 13:10:32 -0800 | [diff] [blame] | 30 | import com.android.settings.core.BasePreferenceController; |
Raff Tsai | 966fa01 | 2019-09-25 11:19:06 +0800 | [diff] [blame] | 31 | import com.android.settingslib.search.SearchIndexableRaw; |
Doris Ling | c7e2ed8 | 2016-10-27 13:09:16 -0700 | [diff] [blame] | 32 | |
| 33 | import java.util.List; |
| 34 | |
Fan Zhang | 5c3301b | 2019-01-28 13:10:32 -0800 | [diff] [blame] | 35 | public class EmergencyInfoPreferenceController extends BasePreferenceController { |
Doris Ling | c7e2ed8 | 2016-10-27 13:09:16 -0700 | [diff] [blame] | 36 | |
yuanjiahsu | 1da7fbb | 2019-04-30 18:44:15 +0800 | [diff] [blame] | 37 | public static String getIntentAction(Context context) { |
| 38 | return context.getResources().getString(R.string.config_emergency_intent_action); |
| 39 | } |
Doris Ling | c7e2ed8 | 2016-10-27 13:09:16 -0700 | [diff] [blame] | 40 | |
Fan Zhang | 5c3301b | 2019-01-28 13:10:32 -0800 | [diff] [blame] | 41 | public EmergencyInfoPreferenceController(Context context, String preferenceKey) { |
| 42 | super(context, preferenceKey); |
Doris Ling | c7e2ed8 | 2016-10-27 13:09:16 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | @Override |
Doris Ling | c7e2ed8 | 2016-10-27 13:09:16 -0700 | [diff] [blame] | 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 | |
Fan Zhang | 5c3301b | 2019-01-28 13:10:32 -0800 | [diff] [blame] | 56 | @Override |
Doris Ling | 829d3aa | 2017-02-03 15:06:56 -0800 | [diff] [blame] | 57 | public void updateState(Preference preference) { |
| 58 | UserInfo info = mContext.getSystemService(UserManager.class).getUserInfo( |
Fan Zhang | d7fa2fa | 2018-12-05 14:42:53 -0800 | [diff] [blame] | 59 | UserHandle.myUserId()); |
Doris Ling | 829d3aa | 2017-02-03 15:06:56 -0800 | [diff] [blame] | 60 | preference.setSummary(mContext.getString(R.string.emergency_info_summary, info.name)); |
| 61 | } |
| 62 | |
Doris Ling | c7e2ed8 | 2016-10-27 13:09:16 -0700 | [diff] [blame] | 63 | @Override |
| 64 | public boolean handlePreferenceTreeClick(Preference preference) { |
Fan Zhang | 5c3301b | 2019-01-28 13:10:32 -0800 | [diff] [blame] | 65 | if (TextUtils.equals(getPreferenceKey(), preference.getKey())) { |
yuanjiahsu | 432650d | 2018-11-07 13:33:12 +0800 | [diff] [blame] | 66 | Intent intent = new Intent(getIntentAction(mContext)); |
Doris Ling | c7e2ed8 | 2016-10-27 13:09:16 -0700 | [diff] [blame] | 67 | intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 68 | mContext.startActivity(intent); |
| 69 | return true; |
| 70 | } |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | @Override |
Fan Zhang | 5c3301b | 2019-01-28 13:10:32 -0800 | [diff] [blame] | 75 | public int getAvailabilityStatus() { |
| 76 | if (!mContext.getResources().getBoolean(R.bool.config_show_emergency_info_in_device_info)) { |
| 77 | return UNSUPPORTED_ON_DEVICE; |
| 78 | } |
| 79 | final Intent intent = new Intent(getIntentAction(mContext)).setPackage( |
| 80 | getPackageName(mContext)); |
| 81 | final List<ResolveInfo> infos = mContext.getPackageManager().queryIntentActivities(intent, |
| 82 | 0); |
| 83 | return infos != null && !infos.isEmpty() |
| 84 | ? AVAILABLE : UNSUPPORTED_ON_DEVICE; |
Doris Ling | c7e2ed8 | 2016-10-27 13:09:16 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Fan Zhang | 5c3301b | 2019-01-28 13:10:32 -0800 | [diff] [blame] | 87 | private static String getPackageName(Context context) { |
yuanjiahsu | 1da7fbb | 2019-04-30 18:44:15 +0800 | [diff] [blame] | 88 | return context.getResources().getString(R.string.config_emergency_package_name); |
yuanjiahsu | 432650d | 2018-11-07 13:33:12 +0800 | [diff] [blame] | 89 | } |
Doris Ling | c7e2ed8 | 2016-10-27 13:09:16 -0700 | [diff] [blame] | 90 | } |