blob: 6e7775b5d5fb39104017860acaf62e23f3315195 [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
18import android.app.Fragment;
mariagpuyol76e90262016-03-29 11:29:54 -070019import android.content.Context;
20import android.content.SharedPreferences;
mariagpuyol58748352016-03-09 15:36:28 -080021import android.os.Bundle;
22import android.preference.Preference;
23import android.preference.PreferenceFragment;
mariagpuyol76e90262016-03-29 11:29:54 -070024import android.preference.PreferenceManager;
mariagpuyol4f9c4e62016-03-23 18:38:45 -070025import android.text.TextUtils;
mariagpuyol58748352016-03-09 15:36:28 -080026
27import com.android.emergency.PreferenceKeys;
28import com.android.emergency.R;
mariagpuyol88757782016-03-29 15:15:08 -070029import com.android.emergency.ReloadablePreferenceInterface;
mariagpuyol4f9c4e62016-03-23 18:38:45 -070030import com.android.emergency.preferences.DatePreference;
mariagpuyol58748352016-03-09 15:36:28 -080031import com.android.internal.logging.MetricsLogger;
32import com.android.internal.logging.MetricsProto.MetricsEvent;
33
mariagpuyol58748352016-03-09 15:36:28 -080034/**
35 * Fragment that displays personal and medical information.
36 */
37public class EditEmergencyInfoFragment extends PreferenceFragment {
38 @Override
39 public void onCreate(Bundle savedInstanceState) {
40 super.onCreate(savedInstanceState);
41
42 addPreferencesFromResource(R.xml.edit_emergency_info);
43
mariagpuyol232daaf2016-03-09 11:26:00 -080044 for (String preferenceKey : PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO) {
mariagpuyol58748352016-03-09 15:36:28 -080045 Preference preference = findPreference(preferenceKey);
46 preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
47 @Override
48 public boolean onPreferenceChange(Preference preference, Object value) {
mariagpuyol4f9c4e62016-03-23 18:38:45 -070049 boolean notSet;
50 if (!preference.getKey().equals(PreferenceKeys.KEY_DATE_OF_BIRTH)) {
51 notSet = TextUtils.isEmpty((String) value);
52 } else {
53 notSet = DatePreference.DEFAULT_UNSET_VALUE == ((Long) value);
54 }
mariagpuyol58748352016-03-09 15:36:28 -080055 MetricsLogger.action(preference.getContext(),
mariagpuyol4f9c4e62016-03-23 18:38:45 -070056 MetricsEvent.ACTION_EDIT_EMERGENCY_INFO_FIELD,
57 preference.getKey() + ":" + (notSet ? "0" : "1"));
mariagpuyol58748352016-03-09 15:36:28 -080058 return true;
59 }
60 });
61 }
62 }
63
mariagpuyol76e90262016-03-29 11:29:54 -070064 /** Returns true if there is at least one preference set. */
65 public static boolean hasAtLeastOnePreferenceSet(Context context) {
66 SharedPreferences sharedPreferences =
67 PreferenceManager.getDefaultSharedPreferences(context);
68 for (String key : PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO) {
69 if (key.equals(PreferenceKeys.KEY_DATE_OF_BIRTH)) {
70 if (sharedPreferences.getLong(key, DatePreference.DEFAULT_UNSET_VALUE)
71 != DatePreference.DEFAULT_UNSET_VALUE) {
72 return true;
73 }
74 } else if (!TextUtils.isEmpty(sharedPreferences.getString(key, ""))) {
75 return true;
76 }
77 }
78 return false;
79 }
80
mariagpuyol88757782016-03-29 15:15:08 -070081 @Override
82 public void onResume() {
83 super.onResume();
84 reloadFromPreference();
85 }
86
87 /** Reloads all the preferences by reading the value from the shared preferences. */
88 public void reloadFromPreference() {
89 for (String preferenceKey : PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO) {
90 ReloadablePreferenceInterface preference = (ReloadablePreferenceInterface)
91 findPreference(preferenceKey);
92 if (preference != null) {
93 preference.reloadFromPreference();
94 }
95 }
96 }
97
mariagpuyol58748352016-03-09 15:36:28 -080098 public static Fragment newInstance() {
99 return new EditEmergencyInfoFragment();
100 }
101}