Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2017 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.util;
     17 
     18 import android.content.ComponentName;
     19 import android.content.Context;
     20 import android.content.SharedPreferences;
     21 import android.content.pm.PackageManager;
     22 import android.support.v7.preference.PreferenceManager;
     23 import android.text.TextUtils;
     24 
     25 import com.android.emergency.PreferenceKeys;
     26 import com.android.emergency.edit.EditInfoActivity;
     27 import com.android.emergency.preferences.EmergencyContactsPreference;
     28 import com.android.internal.annotations.VisibleForTesting;
     29 
     30 import java.util.Collections;
     31 
     32 /** Utility methods for dealing with preferences. */
     33 public class PreferenceUtils {
     34     @VisibleForTesting
     35     public static final String SETTINGS_SUGGESTION_ACTIVITY_ALIAS = ".edit.EditInfoSuggestion";
     36 
     37     /** Returns true if there is at least one preference set. */
     38     public static boolean hasAtLeastOnePreferenceSet(Context context) {
     39         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
     40         for (String key : PreferenceKeys.KEYS_VIEW_EMERGENCY_INFO) {
     41             if (!TextUtils.isEmpty(prefs.getString(key, ""))) {
     42                 return true;
     43             }
     44         }
     45         return false;
     46     }
     47 
     48     /** Returns true if there is at least one valid (still existing) emergency contact. */
     49     public static boolean hasAtLeastOneEmergencyContact(Context context) {
     50         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
     51         String emergencyContactsString = "";
     52         try {
     53             emergencyContactsString = prefs.getString(PreferenceKeys.KEY_EMERGENCY_CONTACTS, "");
     54         } catch (ClassCastException e) {
     55             // Protect against b/28194605: We used to store the contacts using a string set.
     56             // If it is a string set, ignore its value. If it is not a string set it will throw
     57             // a ClassCastException
     58             prefs.getStringSet(
     59                     PreferenceKeys.KEY_EMERGENCY_CONTACTS,
     60                     Collections.<String>emptySet());
     61         }
     62 
     63         return !EmergencyContactsPreference.deserializeAndFilter(
     64                 PreferenceKeys.KEY_EMERGENCY_CONTACTS,
     65                 context,
     66                 emergencyContactsString).isEmpty();
     67     }
     68 
     69     /**
     70      * Enables or disables the settings suggestion for this application, depending on whether any
     71      * emergency settings exist.
     72      */
     73     public static void updateSettingsSuggestionState(Context context) {
     74         int state = hasAtLeastOnePreferenceOrContactSet(context) ?
     75             PackageManager.COMPONENT_ENABLED_STATE_DISABLED :
     76             PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
     77         setSettingsSuggestionState(context, state);
     78     }
     79 
     80     /** Enables the settings suggestion for this application. */
     81     public static void enableSettingsSuggestion(Context context) {
     82         setSettingsSuggestionState(context, PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
     83     }
     84 
     85     private static boolean hasAtLeastOnePreferenceOrContactSet(Context context) {
     86         return hasAtLeastOnePreferenceSet(context) || hasAtLeastOneEmergencyContact(context);
     87     }
     88 
     89     private static void setSettingsSuggestionState(Context context, int state) {
     90         String packageName = context.getPackageName();
     91         String targetClass = packageName + SETTINGS_SUGGESTION_ACTIVITY_ALIAS;
     92         ComponentName name = new ComponentName(packageName, targetClass);
     93         PackageManager pm = context.getPackageManager();
     94         pm.setComponentEnabledSetting(name, state, PackageManager.DONT_KILL_APP);
     95     }
     96 }
     97