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.support.v7.preference.Preference; 26 import android.support.v7.preference.PreferenceScreen; 27 28 import com.android.settings.R; 29 import com.android.settings.core.PreferenceController; 30 import com.android.settings.search.SearchIndexableRaw; 31 32 import java.util.List; 33 34 public class EmergencyInfoPreferenceController extends PreferenceController { 35 36 private static final String KEY_EMERGENCY_INFO = "emergency_info"; 37 private static final String ACTION_EDIT_EMERGENCY_INFO = "android.settings.EDIT_EMERGENGY_INFO"; 38 private static final String PACKAGE_NAME_EMERGENCY = "com.android.emergency"; 39 40 public EmergencyInfoPreferenceController(Context context) { 41 super(context); 42 } 43 44 @Override 45 public void updateRawDataToIndex(List<SearchIndexableRaw> rawData) { 46 if (isAvailable()) { 47 SearchIndexableRaw data = new SearchIndexableRaw(mContext); 48 final Resources res = mContext.getResources(); 49 data.title = res.getString(com.android.settings.R.string.emergency_info_title); 50 data.screenTitle = res.getString(com.android.settings.R.string.emergency_info_title); 51 rawData.add(data); 52 } 53 } 54 55 public void updateState(Preference preference) { 56 UserInfo info = mContext.getSystemService(UserManager.class).getUserInfo( 57 UserHandle.myUserId()); 58 preference.setSummary(mContext.getString(R.string.emergency_info_summary, info.name)); 59 } 60 61 @Override 62 public boolean handlePreferenceTreeClick(Preference preference) { 63 if (KEY_EMERGENCY_INFO.equals(preference.getKey())) { 64 Intent intent = new Intent(ACTION_EDIT_EMERGENCY_INFO); 65 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 66 mContext.startActivity(intent); 67 return true; 68 } 69 return false; 70 } 71 72 @Override 73 public boolean isAvailable() { 74 Intent intent = new Intent(ACTION_EDIT_EMERGENCY_INFO).setPackage(PACKAGE_NAME_EMERGENCY); 75 List<ResolveInfo> infos = mContext.getPackageManager().queryIntentActivities(intent, 0); 76 return infos != null && !infos.isEmpty(); 77 } 78 79 @Override 80 public String getPreferenceKey() { 81 return KEY_EMERGENCY_INFO; 82 } 83 } 84