Home | History | Annotate | Download | only in view
      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.app.Fragment;
     19 import android.content.Context;
     20 import android.content.SharedPreferences;
     21 import android.os.Bundle;
     22 import android.preference.Preference;
     23 import android.preference.PreferenceFragment;
     24 import android.preference.PreferenceManager;
     25 import android.text.TextUtils;
     26 
     27 import com.android.emergency.PreferenceKeys;
     28 import com.android.emergency.R;
     29 import com.android.emergency.ReloadablePreferenceInterface;
     30 
     31 import java.util.ArrayList;
     32 import java.util.List;
     33 
     34 /**
     35  * Fragment that displays personal and medical information.
     36  */
     37 public class ViewEmergencyInfoFragment extends PreferenceFragment {
     38     /** A list with all the preferences. */
     39     private final List<Preference> mPreferences = new ArrayList<Preference>();
     40 
     41     @Override
     42     public void onCreate(Bundle savedInstanceState) {
     43         super.onCreate(savedInstanceState);
     44         addPreferencesFromResource(R.xml.view_emergency_info);
     45 
     46         for (String preferenceKey : PreferenceKeys.KEYS_VIEW_EMERGENCY_INFO) {
     47             Preference preference = findPreference(preferenceKey);
     48             mPreferences.add(preference);
     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     /** Returns true if there is at least one preference set. */
     73     public static boolean hasAtLeastOnePreferenceSet(Context context) {
     74         SharedPreferences sharedPreferences =
     75                 PreferenceManager.getDefaultSharedPreferences(context);
     76         for (String key : PreferenceKeys.KEYS_VIEW_EMERGENCY_INFO) {
     77             if (!TextUtils.isEmpty(sharedPreferences.getString(key, ""))) {
     78                 return true;
     79             }
     80         }
     81         return false;
     82     }
     83 }
     84