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.app.DialogFragment;
20 import android.content.ActivityNotFoundException;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.net.Uri;
24 import android.os.Bundle;
25 import android.provider.ContactsContract;
26 import androidx.preference.Preference.OnPreferenceChangeListener;
27 import androidx.preference.PreferenceFragment;
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceGroup;
30 import android.util.Log;
31 import android.widget.Toast;
32 
33 import com.android.emergency.PreferenceKeys;
34 import com.android.emergency.R;
35 import com.android.emergency.ReloadablePreferenceInterface;
36 import com.android.emergency.preferences.EmergencyContactsPreference;
37 import com.android.emergency.preferences.EmergencyNamePreference;
38 import com.android.emergency.util.PreferenceUtils;
39 import com.android.internal.annotations.VisibleForTesting;
40 import com.android.settingslib.CustomDialogPreference;
41 import com.android.settingslib.CustomEditTextPreference;
42 
43 import java.util.HashMap;
44 import java.util.Map;
45 
46 /** Fragment for editing emergency info, including medical info and emergency contacts. */
47 public class EditInfoFragment extends PreferenceFragment {
48     private static final String TAG = "EditInfoFragment";
49 
50     /** Result code for contact picker */
51     private static final int CONTACT_PICKER_RESULT = 1001;
52 
53     private static final String DIALOG_PREFERENCE_TAG = "dialog_preference";
54 
55     private final Map<String, Preference> mMedicalInfoPreferences =
56             new HashMap<String, Preference>();
57 
58     /** The category that holds the emergency contacts. */
59     private EmergencyContactsPreference mEmergencyContactsPreferenceCategory;
60 
61     private EmergencyNamePreference mEmergencyNamePreference;
62 
63     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)64     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
65         setPreferencesFromResource(R.xml.edit_emergency_info, rootKey);
66 
67         for (String preferenceKey : PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO) {
68             Preference preference = findPreference(preferenceKey);
69             mMedicalInfoPreferences.put(preferenceKey, preference);
70 
71             preference.setOnPreferenceChangeListener(new PreferenceChangeListener(getActivity()));
72 
73             if (((ReloadablePreferenceInterface) preference).isNotSet()) {
74                 getMedicalInfoParent().removePreference(preference);
75             }
76         }
77 
78         mEmergencyNamePreference = (EmergencyNamePreference) findPreference(
79                 PreferenceKeys.KEY_NAME);
80 
81         // Fill in emergency contacts.
82         mEmergencyContactsPreferenceCategory = (EmergencyContactsPreference)
83                 findPreference(PreferenceKeys.KEY_EMERGENCY_CONTACTS);
84 
85         Preference addEmergencyContact = findPreference(PreferenceKeys.KEY_ADD_EMERGENCY_CONTACT);
86         addEmergencyContact.setOnPreferenceClickListener(new Preference
87                 .OnPreferenceClickListener() {
88             @Override
89             public boolean onPreferenceClick(Preference preference) {
90                 // By using ContactsContract.CommonDataKinds.Phone.CONTENT_URI, the user is
91                 // presented with a list of contacts, with one entry per phone number.
92                 // The selected contact is guaranteed to have a name and phone number.
93                 Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
94                         ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
95                 try {
96                     startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
97                     return true;
98                 } catch (ActivityNotFoundException e) {
99                     Log.w(TAG, "No contact app available to display the contacts", e);
100                     Toast.makeText(getContext(),
101                                    getContext().getString(R.string.fail_load_contact_picker),
102                                    Toast.LENGTH_LONG).show();
103                     return false;
104                 }
105             }
106         });
107     }
108 
109     @Override
onResume()110     public void onResume() {
111         super.onResume();
112         reloadFromPreference();
113         mEmergencyNamePreference.reloadFromUserManager();
114     }
115 
116     /** Reloads the contacts by reading the value from the shared preferences. */
reloadFromPreference()117     public void reloadFromPreference() {
118         for (Preference preference : mMedicalInfoPreferences.values()) {
119             ReloadablePreferenceInterface reloadablePreference =
120                     (ReloadablePreferenceInterface) preference;
121             reloadablePreference.reloadFromPreference();
122             if (reloadablePreference.isNotSet()) {
123                 getMedicalInfoParent().removePreference(preference);
124             } else {
125                 // Note: this preference won't be added it if it already exists.
126                 getMedicalInfoParent().addPreference(preference);
127             }
128         }
129         mEmergencyContactsPreferenceCategory.reloadFromPreference();
130     }
131 
132     @Override
onDisplayPreferenceDialog(Preference preference)133     public void onDisplayPreferenceDialog(Preference preference) {
134         DialogFragment fragment = null;
135         if (preference instanceof CustomEditTextPreference) {
136             fragment = CustomEditTextPreference.CustomPreferenceDialogFragment
137                     .newInstance(preference.getKey());
138         } else if (preference instanceof CustomDialogPreference) {
139             fragment = EmergencyNamePreference.EmergencyNamePreferenceDialogFragment
140                     .newInstance(preference.getKey());
141         }
142         if (fragment != null) {
143             fragment.setTargetFragment(this, 0);
144             fragment.show(getFragmentManager(), DIALOG_PREFERENCE_TAG);
145         } else {
146             super.onDisplayPreferenceDialog(preference);
147         }
148     }
149 
150     @Override
onActivityResult(int requestCode, int resultCode, Intent data)151     public void onActivityResult(int requestCode, int resultCode, Intent data) {
152         if (requestCode == CONTACT_PICKER_RESULT && resultCode == Activity.RESULT_OK) {
153             Uri phoneUri = data.getData();
154             mEmergencyContactsPreferenceCategory.addNewEmergencyContact(phoneUri);
155         }
156     }
157 
158     @VisibleForTesting
getMedicalInfoParent()159     public PreferenceGroup getMedicalInfoParent() {
160         return (PreferenceGroup) findPreference(PreferenceKeys.KEY_MEDICAL_INFO);
161     }
162 
163     @VisibleForTesting
getMedicalInfoPreference(String key)164     public Preference getMedicalInfoPreference(String key) {
165         return mMedicalInfoPreferences.get(key);
166     }
167 
168     @VisibleForTesting
169     static class PreferenceChangeListener implements OnPreferenceChangeListener {
170         private final Context mContext;
171 
PreferenceChangeListener(Context context)172         public PreferenceChangeListener(Context context) {
173             mContext = context;
174         }
175 
176         @Override
onPreferenceChange(Preference preferenceItem, Object value)177         public boolean onPreferenceChange(Preference preferenceItem, Object value) {
178             // Enable or disable settings suggestion, as appropriate.
179             PreferenceUtils.updateSettingsSuggestionState(mContext);
180             // If the preference implements OnPreferenceChangeListener, notify it of the
181             // change as well.
182             if (Preference.OnPreferenceChangeListener.class.isInstance(preferenceItem)) {
183                 return ((Preference.OnPreferenceChangeListener) preferenceItem)
184                     .onPreferenceChange(preferenceItem, value);
185             }
186             return true;
187         }
188     }
189 }
190