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.Fragment;
19 import android.os.Bundle;
20 import android.preference.Preference;
21 import android.preference.PreferenceFragment;
22 import android.text.TextUtils;
23 
24 import com.android.emergency.PreferenceKeys;
25 import com.android.emergency.R;
26 import com.android.emergency.ReloadablePreferenceInterface;
27 import com.android.internal.logging.MetricsLogger;
28 import com.android.internal.logging.MetricsProto.MetricsEvent;
29 
30 /**
31  * Fragment that displays personal and medical information.
32  */
33 public class EditEmergencyInfoFragment extends PreferenceFragment {
34     @Override
onCreate(Bundle savedInstanceState)35     public void onCreate(Bundle savedInstanceState) {
36         super.onCreate(savedInstanceState);
37 
38         addPreferencesFromResource(R.xml.edit_emergency_info);
39 
40         for (int i = 0; i < PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO.length; i++) {
41             final int index = i;
42             String preferenceKey = PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO[i];
43             Preference preference = findPreference(preferenceKey);
44             preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
45                 @Override
46                 public boolean onPreferenceChange(Preference preference, Object value) {
47                     boolean notSet = TextUtils.isEmpty((String) value);
48                     // 0 is the default subtype. In DP1 and DP2 we had no explicit subtype.
49                     // Start at 30 to differentiate between before and after.
50                     MetricsLogger.action(
51                             preference.getContext(),
52                             MetricsEvent.ACTION_EDIT_EMERGENCY_INFO_FIELD,
53                             30 + index * 2 + (notSet ? 0 : 1));
54                     return true;
55                 }
56             });
57         }
58     }
59 
60     @Override
onResume()61     public void onResume() {
62         super.onResume();
63         reloadFromPreference();
64     }
65 
66     /** Reloads all the preferences by reading the value from the shared preferences. */
reloadFromPreference()67     public void reloadFromPreference() {
68         for (String preferenceKey : PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO) {
69             ReloadablePreferenceInterface preference = (ReloadablePreferenceInterface)
70                     findPreference(preferenceKey);
71             if (preference != null) {
72                 preference.reloadFromPreference();
73             }
74         }
75     }
76 
newInstance()77     public static Fragment newInstance() {
78         return new EditEmergencyInfoFragment();
79     }
80 }
81