blob: 1b0e3a3574c46ffd142fe2d7ce48925fc3e26061 [file] [log] [blame]
mariagpuyol58748352016-03-09 15:36:28 -08001/*
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
Juan Lang2c0518a2017-05-10 17:00:32 -070018import android.app.DialogFragment;
mariagpuyol58748352016-03-09 15:36:28 -080019import android.app.Fragment;
20import android.os.Bundle;
Juan Lang2c0518a2017-05-10 17:00:32 -070021import android.support.v14.preference.PreferenceFragment;
22import android.support.v7.preference.Preference;
mariagpuyol4f9c4e62016-03-23 18:38:45 -070023import android.text.TextUtils;
mariagpuyol58748352016-03-09 15:36:28 -080024
25import com.android.emergency.PreferenceKeys;
26import com.android.emergency.R;
mariagpuyol88757782016-03-29 15:15:08 -070027import com.android.emergency.ReloadablePreferenceInterface;
Juan Lang2c0518a2017-05-10 17:00:32 -070028import com.android.emergency.preferences.AutoCompleteEditTextPreference;
Juan Langd6bbac52017-07-20 17:56:11 -070029import com.android.emergency.util.PreferenceUtils;
mariagpuyol58748352016-03-09 15:36:28 -080030import com.android.internal.logging.MetricsLogger;
Tamas Berghammer2fdeb9a2016-06-22 15:28:50 +010031import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Fan Zhangcbe03d92017-10-18 11:20:51 -070032import com.android.settingslib.CustomEditTextPreference;
mariagpuyol58748352016-03-09 15:36:28 -080033
mariagpuyol58748352016-03-09 15:36:28 -080034/**
35 * Fragment that displays personal and medical information.
36 */
Juan Lang432ad362017-05-04 16:20:22 -070037public class EditMedicalInfoFragment extends PreferenceFragment {
mariagpuyol58748352016-03-09 15:36:28 -080038 @Override
Juan Lang2c0518a2017-05-10 17:00:32 -070039 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
40 setPreferencesFromResource(R.xml.edit_medical_info, rootKey);
mariagpuyol58748352016-03-09 15:36:28 -080041
mariagpuyolc3f3dd22016-04-11 15:38:48 -070042 for (int i = 0; i < PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO.length; i++) {
43 final int index = i;
44 String preferenceKey = PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO[i];
Juan Langee4540b2017-07-11 10:11:36 -070045
mariagpuyol58748352016-03-09 15:36:28 -080046 Preference preference = findPreference(preferenceKey);
47 preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
48 @Override
49 public boolean onPreferenceChange(Preference preference, Object value) {
Juan Langee4540b2017-07-11 10:11:36 -070050 // Log settings changes.
mariagpuyol10324162016-06-01 18:21:30 -070051 boolean notSet = TextUtils.isEmpty((String) value);
mariagpuyolc3f3dd22016-04-11 15:38:48 -070052 // 0 is the default subtype. In DP1 and DP2 we had no explicit subtype.
mariagpuyol10324162016-06-01 18:21:30 -070053 // Start at 30 to differentiate between before and after.
mariagpuyolc3f3dd22016-04-11 15:38:48 -070054 MetricsLogger.action(
55 preference.getContext(),
mariagpuyol4f9c4e62016-03-23 18:38:45 -070056 MetricsEvent.ACTION_EDIT_EMERGENCY_INFO_FIELD,
mariagpuyol10324162016-06-01 18:21:30 -070057 30 + index * 2 + (notSet ? 0 : 1));
Juan Langd6bbac52017-07-20 17:56:11 -070058 // Enable or disable settings suggestion, as appropriate.
59 PreferenceUtils.updateSettingsSuggestionState(getActivity());
Juan Langee4540b2017-07-11 10:11:36 -070060 // If the preference implements OnPreferenceChangeListener, notify it of the
61 // change as well.
62 if (Preference.OnPreferenceChangeListener.class.isInstance(preference)) {
63 return ((Preference.OnPreferenceChangeListener) preference)
64 .onPreferenceChange(preference, value);
65 }
mariagpuyol58748352016-03-09 15:36:28 -080066 return true;
67 }
68 });
69 }
70 }
71
mariagpuyol88757782016-03-29 15:15:08 -070072 @Override
73 public void onResume() {
74 super.onResume();
75 reloadFromPreference();
76 }
77
Juan Lang2c0518a2017-05-10 17:00:32 -070078 @Override
79 public void onDisplayPreferenceDialog(Preference preference) {
Fan Zhangcbe03d92017-10-18 11:20:51 -070080 DialogFragment fragment = null;
81 if (preference instanceof CustomEditTextPreference) {
82 fragment = CustomEditTextPreference.CustomPreferenceDialogFragment
83 .newInstance(preference.getKey());
84 } else if (preference instanceof AutoCompleteEditTextPreference) {
85 fragment = AutoCompleteEditTextPreference.AutoCompleteEditTextPreferenceDialogFragment
86 .newInstance(preference.getKey());
Juan Lang2c0518a2017-05-10 17:00:32 -070087 }
Fan Zhangcbe03d92017-10-18 11:20:51 -070088 if (fragment != null) {
89 fragment.setTargetFragment(this, 0);
90 fragment.show(getFragmentManager(), "dialog_preference");
91 } else {
92 super.onDisplayPreferenceDialog(preference);
93 }
Juan Lang2c0518a2017-05-10 17:00:32 -070094 }
95
mariagpuyol88757782016-03-29 15:15:08 -070096 /** Reloads all the preferences by reading the value from the shared preferences. */
97 public void reloadFromPreference() {
98 for (String preferenceKey : PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO) {
99 ReloadablePreferenceInterface preference = (ReloadablePreferenceInterface)
100 findPreference(preferenceKey);
101 if (preference != null) {
102 preference.reloadFromPreference();
103 }
104 }
105 }
106
mariagpuyol58748352016-03-09 15:36:28 -0800107 public static Fragment newInstance() {
Juan Lang432ad362017-05-04 16:20:22 -0700108 return new EditMedicalInfoFragment();
mariagpuyol58748352016-03-09 15:36:28 -0800109 }
110}