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.PreferenceFragment;
     23 import android.preference.PreferenceManager;
     24 import android.widget.ListView;
     25 
     26 import com.android.emergency.PreferenceKeys;
     27 import com.android.emergency.R;
     28 import com.android.emergency.preferences.EmergencyContactsPreference;
     29 
     30 import java.util.Collections;
     31 
     32 /**
     33  * Fragment that displays emergency contacts.
     34  */
     35 public class ViewEmergencyContactsFragment extends PreferenceFragment {
     36     /** The category that holds the emergency contacts. */
     37     private EmergencyContactsPreference mEmergencyContactsPreference;
     38 
     39     @Override
     40     public void onCreate(Bundle savedInstanceState) {
     41         super.onCreate(savedInstanceState);
     42         addPreferencesFromResource(R.xml.view_emergency_contacts);
     43         mEmergencyContactsPreference = (EmergencyContactsPreference)
     44                 findPreference(PreferenceKeys.KEY_EMERGENCY_CONTACTS);
     45     }
     46 
     47     @Override
     48     public void onActivityCreated(Bundle savedInstanceState) {
     49         super.onActivityCreated(savedInstanceState);
     50         // Set custom dividers
     51         ListView list = (ListView) getView().findViewById(android.R.id.list);
     52         list.setDivider(getResources().getDrawable(R.drawable.view_contact_divider));
     53     }
     54 
     55 
     56     @Override
     57     public void onResume() {
     58         super.onResume();
     59         mEmergencyContactsPreference.reloadFromPreference();
     60     }
     61 
     62     public static Fragment newInstance() {
     63         return new ViewEmergencyContactsFragment();
     64     }
     65 
     66     /** Returns true if there is at least one valid (still existing) emergency contact. */
     67     public static boolean hasAtLeastOneEmergencyContact(Context context) {
     68         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
     69         String emergencyContactsString = "";
     70         try {
     71             emergencyContactsString = prefs.getString(PreferenceKeys.KEY_EMERGENCY_CONTACTS, "");
     72         } catch (ClassCastException e) {
     73             // Protect against b/28194605: We used to store the contacts using a string set.
     74             // If it is a string set, ignore its value. If it is not a string set it will throw
     75             // a ClassCastException
     76             prefs.getStringSet(
     77                     PreferenceKeys.KEY_EMERGENCY_CONTACTS,
     78                     Collections.<String>emptySet());
     79         }
     80 
     81         return !EmergencyContactsPreference.deserializeAndFilter(
     82                 PreferenceKeys.KEY_EMERGENCY_CONTACTS,
     83                 context,
     84                 emergencyContactsString).isEmpty();
     85     }
     86 }
     87