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.DialogFragment;
19 import android.app.Fragment;
20 import android.os.Bundle;
21 
22 import androidx.preference.Preference;
23 import androidx.preference.PreferenceFragment;
24 
25 import com.android.emergency.PreferenceKeys;
26 import com.android.emergency.R;
27 import com.android.emergency.ReloadablePreferenceInterface;
28 import com.android.emergency.preferences.EmergencyNamePreference;
29 import com.android.emergency.util.PreferenceUtils;
30 import com.android.internal.logging.MetricsLogger;
31 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
32 import com.android.settingslib.CustomDialogPreference;
33 import com.android.settingslib.CustomEditTextPreference;
34 
35 import android.text.TextUtils;
36 
37 /**
38  * Fragment that displays personal and medical information.
39  */
40 public class EditMedicalInfoFragment extends PreferenceFragment {
41 
42     private EmergencyNamePreference mEmergencyNamePreference;
43 
44     private static final String DIALOG_PREFERENCE_TAG = "dialog_preference";
45 
46     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)47     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
48         setPreferencesFromResource(R.xml.edit_medical_info, rootKey);
49 
50         for (int i = 0; i < PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO.length; i++) {
51             final int index = i;
52             String preferenceKey = PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO[i];
53 
54             Preference preference = findPreference(preferenceKey);
55             preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
56                 @Override
57                 public boolean onPreferenceChange(Preference preference, Object value) {
58                     // Log settings changes.
59                     boolean notSet = TextUtils.isEmpty((String) value);
60                     // 0 is the default subtype. In DP1 and DP2 we had no explicit subtype.
61                     // Start at 30 to differentiate between before and after.
62                     MetricsLogger.action(
63                             preference.getContext(),
64                             MetricsEvent.ACTION_EDIT_EMERGENCY_INFO_FIELD,
65                             30 + index * 2 + (notSet ? 0 : 1));
66                     // Enable or disable settings suggestion, as appropriate.
67                     PreferenceUtils.updateSettingsSuggestionState(getActivity());
68                     // If the preference implements OnPreferenceChangeListener, notify it of the
69                     // change as well.
70                     if (Preference.OnPreferenceChangeListener.class.isInstance(preference)) {
71                         return ((Preference.OnPreferenceChangeListener) preference)
72                                 .onPreferenceChange(preference, value);
73                     }
74                     return true;
75                 }
76             });
77         }
78 
79         mEmergencyNamePreference = (EmergencyNamePreference) findPreference(
80                 PreferenceKeys.KEY_NAME);
81     }
82 
83     @Override
onResume()84     public void onResume() {
85         super.onResume();
86         reloadFromPreference();
87         mEmergencyNamePreference.reloadFromUserManager();
88     }
89 
90     @Override
onDisplayPreferenceDialog(Preference preference)91     public void onDisplayPreferenceDialog(Preference preference) {
92         DialogFragment fragment = null;
93         if (preference instanceof CustomEditTextPreference) {
94             fragment = CustomEditTextPreference.CustomPreferenceDialogFragment
95                     .newInstance(preference.getKey());
96         } else if (preference instanceof CustomDialogPreference) {
97             fragment = EmergencyNamePreference.EmergencyNamePreferenceDialogFragment
98                     .newInstance(preference.getKey());
99         }
100         if (fragment != null) {
101             fragment.setTargetFragment(this, 0);
102             fragment.show(getFragmentManager(), DIALOG_PREFERENCE_TAG);
103         } else {
104             super.onDisplayPreferenceDialog(preference);
105         }
106     }
107 
108     /** Reloads all the preferences by reading the value from the shared preferences. */
reloadFromPreference()109     public void reloadFromPreference() {
110         for (String preferenceKey : PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO) {
111             ReloadablePreferenceInterface preference = (ReloadablePreferenceInterface)
112                     findPreference(preferenceKey);
113             if (preference != null) {
114                 preference.reloadFromPreference();
115             }
116         }
117     }
118 
newInstance()119     public static Fragment newInstance() {
120         return new EditMedicalInfoFragment();
121     }
122 }
123