blob: 5bed05a0115ad1d6235707612d804c1f77c86408 [file] [log] [blame]
Doris Lingc7e2ed82016-10-27 13:09:16 -07001/*
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 */
16package com.android.settings.accounts;
17
18import android.content.Context;
19import android.content.Intent;
20import android.content.pm.ResolveInfo;
Doris Ling829d3aa2017-02-03 15:06:56 -080021import android.content.pm.UserInfo;
Doris Lingc7e2ed82016-10-27 13:09:16 -070022import android.content.res.Resources;
Doris Ling829d3aa2017-02-03 15:06:56 -080023import android.os.UserHandle;
24import android.os.UserManager;
Fan Zhang5c3301b2019-01-28 13:10:32 -080025import android.text.TextUtils;
Doris Lingc7e2ed82016-10-27 13:09:16 -070026
Fan Zhang23f8d592018-08-28 15:11:40 -070027import androidx.preference.Preference;
28
Doris Ling829d3aa2017-02-03 15:06:56 -080029import com.android.settings.R;
Fan Zhang5c3301b2019-01-28 13:10:32 -080030import com.android.settings.core.BasePreferenceController;
Raff Tsai966fa012019-09-25 11:19:06 +080031import com.android.settingslib.search.SearchIndexableRaw;
Doris Lingc7e2ed82016-10-27 13:09:16 -070032
33import java.util.List;
34
Fan Zhang5c3301b2019-01-28 13:10:32 -080035public class EmergencyInfoPreferenceController extends BasePreferenceController {
Doris Lingc7e2ed82016-10-27 13:09:16 -070036
yuanjiahsu1da7fbb2019-04-30 18:44:15 +080037 public static String getIntentAction(Context context) {
38 return context.getResources().getString(R.string.config_emergency_intent_action);
39 }
Doris Lingc7e2ed82016-10-27 13:09:16 -070040
Fan Zhang5c3301b2019-01-28 13:10:32 -080041 public EmergencyInfoPreferenceController(Context context, String preferenceKey) {
42 super(context, preferenceKey);
Doris Lingc7e2ed82016-10-27 13:09:16 -070043 }
44
45 @Override
Doris Lingc7e2ed82016-10-27 13:09:16 -070046 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 Zhang5c3301b2019-01-28 13:10:32 -080056 @Override
Doris Ling829d3aa2017-02-03 15:06:56 -080057 public void updateState(Preference preference) {
58 UserInfo info = mContext.getSystemService(UserManager.class).getUserInfo(
Fan Zhangd7fa2fa2018-12-05 14:42:53 -080059 UserHandle.myUserId());
Doris Ling829d3aa2017-02-03 15:06:56 -080060 preference.setSummary(mContext.getString(R.string.emergency_info_summary, info.name));
61 }
62
Doris Lingc7e2ed82016-10-27 13:09:16 -070063 @Override
64 public boolean handlePreferenceTreeClick(Preference preference) {
Fan Zhang5c3301b2019-01-28 13:10:32 -080065 if (TextUtils.equals(getPreferenceKey(), preference.getKey())) {
yuanjiahsu432650d2018-11-07 13:33:12 +080066 Intent intent = new Intent(getIntentAction(mContext));
Doris Lingc7e2ed82016-10-27 13:09:16 -070067 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
68 mContext.startActivity(intent);
69 return true;
70 }
71 return false;
72 }
73
74 @Override
Fan Zhang5c3301b2019-01-28 13:10:32 -080075 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 Lingc7e2ed82016-10-27 13:09:16 -070085 }
86
Fan Zhang5c3301b2019-01-28 13:10:32 -080087 private static String getPackageName(Context context) {
yuanjiahsu1da7fbb2019-04-30 18:44:15 +080088 return context.getResources().getString(R.string.config_emergency_package_name);
yuanjiahsu432650d2018-11-07 13:33:12 +080089 }
Doris Lingc7e2ed82016-10-27 13:09:16 -070090}