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.view; 17 18 import android.view.View; 19 import android.os.Bundle; 20 21 import androidx.annotation.NonNull; 22 import androidx.annotation.Nullable; 23 import androidx.fragment.app.Fragment; 24 import androidx.preference.PreferenceFragmentCompat; 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceManager; 27 28 import com.android.emergency.PreferenceKeys; 29 import com.android.emergency.R; 30 import com.android.emergency.ReloadablePreferenceInterface; 31 32 import java.util.ArrayList; 33 import java.util.List; 34 35 /** 36 * Fragment that displays personal and medical information. 37 */ 38 public class ViewEmergencyInfoFragment extends PreferenceFragmentCompat { 39 /** A list with all the preferences. */ 40 private final List<Preference> mPreferences = new ArrayList<Preference>(); 41 42 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)43 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 44 setPreferencesFromResource(R.xml.view_emergency_info, rootKey); 45 46 for (String preferenceKey : PreferenceKeys.KEYS_VIEW_EMERGENCY_INFO) { 47 Preference preference = findPreference(preferenceKey); 48 mPreferences.add(preference); 49 50 if (((ReloadablePreferenceInterface) preference).isNotSet()) { 51 getPreferenceScreen().removePreference(preference); 52 } 53 } 54 } 55 56 @Override onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)57 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 58 super.onViewCreated(view, savedInstanceState); 59 60 // Set a top padding for the EmergencyInfo View. 61 int topPadding = (int) getResources().getDimension(R.dimen.view_emergency_info_top_padding); 62 view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + topPadding, 63 view.getPaddingRight(), view.getPaddingBottom()); 64 } 65 66 @Override onResume()67 public void onResume() { 68 super.onResume(); 69 for (Preference preference : mPreferences) { 70 ReloadablePreferenceInterface reloadablePreference = 71 (ReloadablePreferenceInterface) preference; 72 reloadablePreference.reloadFromPreference(); 73 if (reloadablePreference.isNotSet()) { 74 getPreferenceScreen().removePreference(preference); 75 } else { 76 // Note: this preference won't be added it if it already exists. 77 getPreferenceScreen().addPreference(preference); 78 } 79 } 80 } 81 newInstance()82 public static Fragment newInstance() { 83 return new ViewEmergencyInfoFragment(); 84 } 85 } 86