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 */
16package com.android.emergency.view;
17
18import android.app.Fragment;
19import android.os.Bundle;
20import android.support.v14.preference.PreferenceFragment;
21import android.support.v7.preference.Preference;
22import android.support.v7.preference.PreferenceManager;
23
24import com.android.emergency.PreferenceKeys;
25import com.android.emergency.R;
26import com.android.emergency.ReloadablePreferenceInterface;
27
28import java.util.ArrayList;
29import java.util.List;
30
31/**
32 * Fragment that displays personal and medical information.
33 */
34public class ViewEmergencyInfoFragment extends PreferenceFragment {
35    /** A list with all the preferences. */
36    private final List<Preference> mPreferences = new ArrayList<Preference>();
37
38    @Override
39    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
40        setPreferencesFromResource(R.xml.view_emergency_info, rootKey);
41
42        for (String preferenceKey : PreferenceKeys.KEYS_VIEW_EMERGENCY_INFO) {
43            Preference preference = findPreference(preferenceKey);
44            mPreferences.add(preference);
45
46            if (((ReloadablePreferenceInterface) preference).isNotSet()) {
47                getPreferenceScreen().removePreference(preference);
48            }
49        }
50    }
51
52    @Override
53    public void onResume() {
54        super.onResume();
55        for (Preference preference : mPreferences) {
56            ReloadablePreferenceInterface reloadablePreference =
57                    (ReloadablePreferenceInterface) preference;
58            reloadablePreference.reloadFromPreference();
59            if (reloadablePreference.isNotSet()) {
60                getPreferenceScreen().removePreference(preference);
61            } else {
62                // Note: this preference won't be added it if it already exists.
63                getPreferenceScreen().addPreference(preference);
64            }
65        }
66    }
67
68    public static Fragment newInstance() {
69        return new ViewEmergencyInfoFragment();
70    }
71}
72