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.emergency.edit; 17 18 import android.app.Activity; 19 import android.app.Fragment; 20 import android.content.Intent; 21 import android.net.Uri; 22 import android.os.Bundle; 23 import android.preference.Preference; 24 import android.preference.PreferenceFragment; 25 import android.provider.ContactsContract; 26 27 import com.android.emergency.PreferenceKeys; 28 import com.android.emergency.R; 29 import com.android.emergency.preferences.EmergencyContactsPreference; 30 31 /** 32 * Fragment that displays emergency contacts. These contacts can be added or removed. 33 */ 34 public class EditEmergencyContactsFragment extends PreferenceFragment { 35 36 /** Result code for contact picker */ 37 private static final int CONTACT_PICKER_RESULT = 1001; 38 39 /** The category that holds the emergency contacts. */ 40 private EmergencyContactsPreference mEmergencyContactsPreferenceCategory; 41 42 @Override onCreate(Bundle savedInstanceState)43 public void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 addPreferencesFromResource(R.xml.edit_emergency_contacts); 46 mEmergencyContactsPreferenceCategory = (EmergencyContactsPreference) 47 findPreference(PreferenceKeys.KEY_EMERGENCY_CONTACTS); 48 49 Preference addEmergencyContact = findPreference(PreferenceKeys.KEY_ADD_CONTACT); 50 addEmergencyContact.setOnPreferenceClickListener(new Preference 51 .OnPreferenceClickListener() { 52 @Override 53 public boolean onPreferenceClick(Preference preference) { 54 // By using ContactsContract.CommonDataKinds.Phone.CONTENT_URI, the user is 55 // presented with a list of contacts, with one entry per phone number. 56 // The selected contact is guaranteed to have a name and phone number. 57 Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, 58 ContactsContract.CommonDataKinds.Phone.CONTENT_URI); 59 startActivityForResult(contactPickerIntent, 60 CONTACT_PICKER_RESULT); 61 return true; 62 } 63 }); 64 65 } 66 67 @Override onResume()68 public void onResume() { 69 super.onResume(); 70 reloadFromPreference(); 71 } 72 73 /** Reloads the contacts by reading the value from the shared preferences. */ reloadFromPreference()74 public void reloadFromPreference() { 75 if (mEmergencyContactsPreferenceCategory != null) { 76 mEmergencyContactsPreferenceCategory.reloadFromPreference(); 77 } 78 } 79 80 @Override onActivityResult(int requestCode, int resultCode, Intent data)81 public void onActivityResult(int requestCode, int resultCode, Intent data) { 82 if (requestCode == CONTACT_PICKER_RESULT && resultCode == Activity.RESULT_OK) { 83 Uri uri = data.getData(); 84 mEmergencyContactsPreferenceCategory.addNewEmergencyContact(uri); 85 } 86 } 87 newInstance()88 public static Fragment newInstance() { 89 return new EditEmergencyContactsFragment(); 90 } 91 } 92