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