mariagpuyol | 5874835 | 2016-03-09 15:36:28 -0800 | [diff] [blame] | 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; |
mariagpuyol | 76e9026 | 2016-03-29 11:29:54 -0700 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.SharedPreferences; |
mariagpuyol | 5874835 | 2016-03-09 15:36:28 -0800 | [diff] [blame] | 21 | import android.os.Bundle; |
| 22 | import android.preference.Preference; |
| 23 | import android.preference.PreferenceFragment; |
mariagpuyol | 76e9026 | 2016-03-29 11:29:54 -0700 | [diff] [blame] | 24 | import android.preference.PreferenceManager; |
mariagpuyol | 4f9c4e6 | 2016-03-23 18:38:45 -0700 | [diff] [blame] | 25 | import android.text.TextUtils; |
mariagpuyol | 5874835 | 2016-03-09 15:36:28 -0800 | [diff] [blame] | 26 | |
| 27 | import com.android.emergency.PreferenceKeys; |
| 28 | import com.android.emergency.R; |
mariagpuyol | 8875778 | 2016-03-29 15:15:08 -0700 | [diff] [blame^] | 29 | import com.android.emergency.ReloadablePreferenceInterface; |
mariagpuyol | 4f9c4e6 | 2016-03-23 18:38:45 -0700 | [diff] [blame] | 30 | import com.android.emergency.preferences.DatePreference; |
mariagpuyol | 5874835 | 2016-03-09 15:36:28 -0800 | [diff] [blame] | 31 | import com.android.internal.logging.MetricsLogger; |
| 32 | import com.android.internal.logging.MetricsProto.MetricsEvent; |
| 33 | |
mariagpuyol | 5874835 | 2016-03-09 15:36:28 -0800 | [diff] [blame] | 34 | /** |
| 35 | * Fragment that displays personal and medical information. |
| 36 | */ |
| 37 | public 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 | |
mariagpuyol | 232daaf | 2016-03-09 11:26:00 -0800 | [diff] [blame] | 44 | for (String preferenceKey : PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO) { |
mariagpuyol | 5874835 | 2016-03-09 15:36:28 -0800 | [diff] [blame] | 45 | Preference preference = findPreference(preferenceKey); |
| 46 | preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { |
| 47 | @Override |
| 48 | public boolean onPreferenceChange(Preference preference, Object value) { |
mariagpuyol | 4f9c4e6 | 2016-03-23 18:38:45 -0700 | [diff] [blame] | 49 | 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 | } |
mariagpuyol | 5874835 | 2016-03-09 15:36:28 -0800 | [diff] [blame] | 55 | MetricsLogger.action(preference.getContext(), |
mariagpuyol | 4f9c4e6 | 2016-03-23 18:38:45 -0700 | [diff] [blame] | 56 | MetricsEvent.ACTION_EDIT_EMERGENCY_INFO_FIELD, |
| 57 | preference.getKey() + ":" + (notSet ? "0" : "1")); |
mariagpuyol | 5874835 | 2016-03-09 15:36:28 -0800 | [diff] [blame] | 58 | return true; |
| 59 | } |
| 60 | }); |
| 61 | } |
| 62 | } |
| 63 | |
mariagpuyol | 76e9026 | 2016-03-29 11:29:54 -0700 | [diff] [blame] | 64 | /** 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 | |
mariagpuyol | 8875778 | 2016-03-29 15:15:08 -0700 | [diff] [blame^] | 81 | @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 | |
mariagpuyol | 5874835 | 2016-03-09 15:36:28 -0800 | [diff] [blame] | 98 | public static Fragment newInstance() { |
| 99 | return new EditEmergencyInfoFragment(); |
| 100 | } |
| 101 | } |