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.preference.Preference; 28 29 import com.android.settings.R; 30 import com.android.settings.core.BasePreferenceController; 31 import com.android.settings.search.SearchIndexableRaw; 32 33 import java.util.List; 34 35 public class EmergencyInfoPreferenceController extends BasePreferenceController { 36 getIntentAction(Context context)37 public static String getIntentAction(Context context) { 38 return context.getResources().getString(R.string.config_emergency_intent_action); 39 } 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())) { 66 Intent intent = new Intent(getIntentAction(mContext)); 67 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 68 mContext.startActivity(intent); 69 return true; 70 } 71 return false; 72 } 73 74 @Override getAvailabilityStatus()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; 85 } 86 getPackageName(Context context)87 private static String getPackageName(Context context) { 88 return context.getResources().getString(R.string.config_emergency_package_name); 89 } 90 } 91