Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2008 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 
     17 package com.android.providers.settings;
     18 
     19 import android.app.ActivityManagerNative;
     20 import android.app.IActivityManager;
     21 import android.app.backup.IBackupManager;
     22 import android.content.Context;
     23 import android.content.res.Configuration;
     24 import android.location.LocationManager;
     25 import android.media.AudioManager;
     26 import android.media.RingtoneManager;
     27 import android.net.Uri;
     28 import android.os.IPowerManager;
     29 import android.os.RemoteException;
     30 import android.os.ServiceManager;
     31 import android.os.UserManager;
     32 import android.provider.Settings;
     33 import android.text.TextUtils;
     34 
     35 import java.util.Locale;
     36 
     37 public class SettingsHelper {
     38     private static final String SILENT_RINGTONE = "_silent";
     39     private Context mContext;
     40     private AudioManager mAudioManager;
     41 
     42     public SettingsHelper(Context context) {
     43         mContext = context;
     44         mAudioManager = (AudioManager) context
     45                 .getSystemService(Context.AUDIO_SERVICE);
     46     }
     47 
     48     /**
     49      * Sets the property via a call to the appropriate API, if any, and returns
     50      * whether or not the setting should be saved to the database as well.
     51      * @param name the name of the setting
     52      * @param value the string value of the setting
     53      * @return whether to continue with writing the value to the database. In
     54      * some cases the data will be written by the call to the appropriate API,
     55      * and in some cases the property value needs to be modified before setting.
     56      */
     57     public boolean restoreValue(String name, String value) {
     58         if (Settings.System.SCREEN_BRIGHTNESS.equals(name)) {
     59             setBrightness(Integer.parseInt(value));
     60         } else if (Settings.System.SOUND_EFFECTS_ENABLED.equals(name)) {
     61             setSoundEffects(Integer.parseInt(value) == 1);
     62         } else if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
     63             setGpsLocation(value);
     64             return false;
     65         } else if (Settings.Secure.BACKUP_AUTO_RESTORE.equals(name)) {
     66             setAutoRestore(Integer.parseInt(value) == 1);
     67         } else if (isAlreadyConfiguredCriticalAccessibilitySetting(name)) {
     68             return false;
     69         } else if (Settings.System.RINGTONE.equals(name)
     70                 || Settings.System.NOTIFICATION_SOUND.equals(name)) {
     71             setRingtone(name, value);
     72             return false;
     73         }
     74         return true;
     75     }
     76 
     77     public String onBackupValue(String name, String value) {
     78         // Special processing for backing up ringtones
     79         if (Settings.System.RINGTONE.equals(name)
     80                 || Settings.System.NOTIFICATION_SOUND.equals(name)) {
     81             if (value == null) {
     82                 // Silent ringtone
     83                 return SILENT_RINGTONE;
     84             } else {
     85                 return getCanonicalRingtoneValue(value);
     86             }
     87         }
     88         // Return the original value
     89         return value;
     90     }
     91 
     92     /**
     93      * Sets the ringtone of type specified by the name.
     94      *
     95      * @param name should be Settings.System.RINGTONE or Settings.System.NOTIFICATION_SOUND.
     96      * @param value can be a canonicalized uri or "_silent" to indicate a silent (null) ringtone.
     97      */
     98     private void setRingtone(String name, String value) {
     99         // If it's null, don't change the default
    100         if (value == null) return;
    101         Uri ringtoneUri = null;
    102         if (SILENT_RINGTONE.equals(value)) {
    103             ringtoneUri = null;
    104         } else {
    105             Uri canonicalUri = Uri.parse(value);
    106             ringtoneUri = mContext.getContentResolver().uncanonicalize(canonicalUri);
    107             if (ringtoneUri == null) {
    108                 // Unrecognized or invalid Uri, don't restore
    109                 return;
    110             }
    111         }
    112         final int ringtoneType = Settings.System.RINGTONE.equals(name)
    113                 ? RingtoneManager.TYPE_RINGTONE : RingtoneManager.TYPE_NOTIFICATION;
    114         RingtoneManager.setActualDefaultRingtoneUri(mContext, ringtoneType, ringtoneUri);
    115     }
    116 
    117     private String getCanonicalRingtoneValue(String value) {
    118         final Uri ringtoneUri = Uri.parse(value);
    119         final Uri canonicalUri = mContext.getContentResolver().canonicalize(ringtoneUri);
    120         return canonicalUri == null ? null : canonicalUri.toString();
    121     }
    122 
    123     private boolean isAlreadyConfiguredCriticalAccessibilitySetting(String name) {
    124         // These are the critical accessibility settings that are required for a
    125         // blind user to be able to interact with the device. If these settings are
    126         // already configured, we will not overwrite them. If they are already set,
    127         // it means that the user has performed a global gesture to enable accessibility
    128         // and definitely needs these features working after the restore.
    129         if (Settings.Secure.ACCESSIBILITY_ENABLED.equals(name)
    130                 || Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION.equals(name)
    131                 || Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD.equals(name)
    132                 || Settings.Secure.TOUCH_EXPLORATION_ENABLED.equals(name)) {
    133             return Settings.Secure.getInt(mContext.getContentResolver(), name, 0) != 0;
    134         } else if (Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES.equals(name)
    135                 || Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES.equals(name)) {
    136             return !TextUtils.isEmpty(Settings.Secure.getString(
    137                     mContext.getContentResolver(), name));
    138         }
    139         return false;
    140     }
    141 
    142     private void setAutoRestore(boolean enabled) {
    143         try {
    144             IBackupManager bm = IBackupManager.Stub.asInterface(
    145                     ServiceManager.getService(Context.BACKUP_SERVICE));
    146             if (bm != null) {
    147                 bm.setAutoRestore(enabled);
    148             }
    149         } catch (RemoteException e) {}
    150     }
    151 
    152     private void setGpsLocation(String value) {
    153         UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
    154         if (um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) {
    155             return;
    156         }
    157         final String GPS = LocationManager.GPS_PROVIDER;
    158         boolean enabled =
    159                 GPS.equals(value) ||
    160                 value.startsWith(GPS + ",") ||
    161                 value.endsWith("," + GPS) ||
    162                 value.contains("," + GPS + ",");
    163         Settings.Secure.setLocationProviderEnabled(
    164                 mContext.getContentResolver(), GPS, enabled);
    165     }
    166 
    167     private void setSoundEffects(boolean enable) {
    168         if (enable) {
    169             mAudioManager.loadSoundEffects();
    170         } else {
    171             mAudioManager.unloadSoundEffects();
    172         }
    173     }
    174 
    175     private void setBrightness(int brightness) {
    176         try {
    177             IPowerManager power = IPowerManager.Stub.asInterface(
    178                     ServiceManager.getService("power"));
    179             if (power != null) {
    180                 power.setTemporaryScreenBrightnessSettingOverride(brightness);
    181             }
    182         } catch (RemoteException doe) {
    183 
    184         }
    185     }
    186 
    187     byte[] getLocaleData() {
    188         Configuration conf = mContext.getResources().getConfiguration();
    189         final Locale loc = conf.locale;
    190         String localeString = loc.getLanguage();
    191         String country = loc.getCountry();
    192         if (!TextUtils.isEmpty(country)) {
    193             localeString += "_" + country;
    194         }
    195         return localeString.getBytes();
    196     }
    197 
    198     /**
    199      * Sets the locale specified. Input data is the equivalent of "ll_cc".getBytes(), where
    200      * "ll" is the language code and "cc" is the country code.
    201      * @param data the locale string in bytes.
    202      */
    203     void setLocaleData(byte[] data, int size) {
    204         // Check if locale was set by the user:
    205         Configuration conf = mContext.getResources().getConfiguration();
    206         Locale loc = conf.locale;
    207         // TODO: The following is not working as intended because the network is forcing a locale
    208         // change after registering. Need to find some other way to detect if the user manually
    209         // changed the locale
    210         if (conf.userSetLocale) return; // Don't change if user set it in the SetupWizard
    211 
    212         final String[] availableLocales = mContext.getAssets().getLocales();
    213         String localeCode = new String(data, 0, size);
    214         String language = new String(data, 0, 2);
    215         String country = size > 4 ? new String(data, 3, 2) : "";
    216         loc = null;
    217         for (int i = 0; i < availableLocales.length; i++) {
    218             if (availableLocales[i].equals(localeCode)) {
    219                 loc = new Locale(language, country);
    220                 break;
    221             }
    222         }
    223         if (loc == null) return; // Couldn't find the saved locale in this version of the software
    224 
    225         try {
    226             IActivityManager am = ActivityManagerNative.getDefault();
    227             Configuration config = am.getConfiguration();
    228             config.locale = loc;
    229             // indicate this isn't some passing default - the user wants this remembered
    230             config.userSetLocale = true;
    231 
    232             am.updateConfiguration(config);
    233         } catch (RemoteException e) {
    234             // Intentionally left blank
    235         }
    236     }
    237 
    238     /**
    239      * Informs the audio service of changes to the settings so that
    240      * they can be re-read and applied.
    241      */
    242     void applyAudioSettings() {
    243         AudioManager am = new AudioManager(mContext);
    244         am.reloadAudioSettings();
    245     }
    246 }
    247