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