Home | History | Annotate | Download | only in edit
      1 /*
      2  * Copyright (C) 2017 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.content.ActivityNotFoundException;
     20 import android.content.Intent;
     21 import android.content.SharedPreferences;
     22 import android.net.Uri;
     23 import android.os.Bundle;
     24 import android.provider.ContactsContract;
     25 import android.support.v14.preference.PreferenceFragment;
     26 import android.support.v7.preference.Preference;
     27 import android.support.v7.preference.PreferenceGroup;
     28 import android.support.v7.preference.PreferenceManager;
     29 import android.util.Log;
     30 import android.widget.Toast;
     31 
     32 import com.android.emergency.PreferenceKeys;
     33 import com.android.emergency.R;
     34 import com.android.emergency.ReloadablePreferenceInterface;
     35 import com.android.emergency.preferences.EmergencyContactsPreference;
     36 import com.android.internal.annotations.VisibleForTesting;
     37 
     38 import java.util.HashMap;
     39 import java.util.Map;
     40 
     41 /** Fragment for editing emergency info, including medical info and emergency contacts. */
     42 public class EditInfoFragment extends PreferenceFragment {
     43     private static final String TAG = "EditInfoFragment";
     44 
     45     /** Result code for contact picker */
     46     private static final int CONTACT_PICKER_RESULT = 1001;
     47 
     48     private final Map<String, Preference> mMedicalInfoPreferences =
     49             new HashMap<String, Preference>();
     50 
     51     /** The category that holds the emergency contacts. */
     52     private EmergencyContactsPreference mEmergencyContactsPreferenceCategory;
     53 
     54     @Override
     55     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
     56         setPreferencesFromResource(R.xml.edit_emergency_info, rootKey);
     57 
     58         for (String preferenceKey : PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO) {
     59             Preference preference = findPreference(preferenceKey);
     60             mMedicalInfoPreferences.put(preferenceKey, preference);
     61 
     62             if (((ReloadablePreferenceInterface) preference).isNotSet()) {
     63                 getMedicalInfoParent().removePreference(preference);
     64             }
     65         }
     66 
     67         // Fill in emergency contacts.
     68         mEmergencyContactsPreferenceCategory = (EmergencyContactsPreference)
     69                 findPreference(PreferenceKeys.KEY_EMERGENCY_CONTACTS);
     70 
     71         Preference addEmergencyContact = findPreference(PreferenceKeys.KEY_ADD_EMERGENCY_CONTACT);
     72         addEmergencyContact.setOnPreferenceClickListener(new Preference
     73                 .OnPreferenceClickListener() {
     74             @Override
     75             public boolean onPreferenceClick(Preference preference) {
     76                 // By using ContactsContract.CommonDataKinds.Phone.CONTENT_URI, the user is
     77                 // presented with a list of contacts, with one entry per phone number.
     78                 // The selected contact is guaranteed to have a name and phone number.
     79                 Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
     80                         ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
     81                 try {
     82                     startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
     83                     return true;
     84                 } catch (ActivityNotFoundException e) {
     85                     Log.w(TAG, "No contact app available to display the contacts", e);
     86                     Toast.makeText(getContext(),
     87                                    getContext().getString(R.string.fail_load_contact_picker),
     88                                    Toast.LENGTH_LONG).show();
     89                     return false;
     90                 }
     91             }
     92         });
     93     }
     94 
     95     @Override
     96     public void onResume() {
     97         super.onResume();
     98         reloadFromPreference();
     99     }
    100 
    101     /** Reloads the contacts by reading the value from the shared preferences. */
    102     public void reloadFromPreference() {
    103         for (Preference preference : mMedicalInfoPreferences.values()) {
    104             ReloadablePreferenceInterface reloadablePreference =
    105                     (ReloadablePreferenceInterface) preference;
    106             reloadablePreference.reloadFromPreference();
    107             if (reloadablePreference.isNotSet()) {
    108                 getMedicalInfoParent().removePreference(preference);
    109             } else {
    110                 // Note: this preference won't be added it if it already exists.
    111                 getMedicalInfoParent().addPreference(preference);
    112             }
    113         }
    114         mEmergencyContactsPreferenceCategory.reloadFromPreference();
    115     }
    116 
    117     @Override
    118     public void onActivityResult(int requestCode, int resultCode, Intent data) {
    119         if (requestCode == CONTACT_PICKER_RESULT && resultCode == Activity.RESULT_OK) {
    120             Uri phoneUri = data.getData();
    121             mEmergencyContactsPreferenceCategory.addNewEmergencyContact(phoneUri);
    122         }
    123     }
    124 
    125     @VisibleForTesting
    126     public PreferenceGroup getMedicalInfoParent() {
    127         return (PreferenceGroup) findPreference(PreferenceKeys.KEY_MEDICAL_INFO);
    128     }
    129 
    130     @VisibleForTesting
    131     public Preference getMedicalInfoPreference(String key) {
    132         return mMedicalInfoPreferences.get(key);
    133     }
    134 }
    135