EditEmergencyContactsFragment.java revision 5874835f1edc1cac42eb4b3aa5fe437e8fd53c78
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 */
16package com.android.emergency.edit;
17
18import android.app.Activity;
19import android.app.Fragment;
20import android.content.Intent;
21import android.net.Uri;
22import android.os.Bundle;
23import android.preference.Preference;
24import android.preference.PreferenceFragment;
25import android.provider.ContactsContract;
26
27import com.android.emergency.PreferenceKeys;
28import com.android.emergency.R;
29import com.android.emergency.preferences.EmergencyContactsPreference;
30
31/**
32 * Fragment that displays emergency contacts. These contacts can be added or removed.
33 */
34public 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
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
68    public void onResume() {
69        super.onResume();
70        mEmergencyContactsPreferenceCategory.reloadFromPreference();
71    }
72
73    @Override
74    public void onActivityResult(int requestCode, int resultCode, Intent data) {
75        if (requestCode == CONTACT_PICKER_RESULT && resultCode == Activity.RESULT_OK) {
76            Uri uri = data.getData();
77            mEmergencyContactsPreferenceCategory.addNewEmergencyContact(uri);
78        }
79    }
80
81    public static Fragment newInstance() {
82        return new EditEmergencyContactsFragment();
83    }
84}
85