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