SettingsHelper.java revision 03b6d90db9acc531a945c57795b903a3b74dd0b7
1501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master/* 2501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master * Copyright (C) 2008 The Android Open Source Project 3501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master * 4501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master * Licensed under the Apache License, Version 2.0 (the "License"); 5501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master * you may not use this file except in compliance with the License. 6501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master * You may obtain a copy of the License at 7501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master * 8501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master * http://www.apache.org/licenses/LICENSE-2.0 9501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master * 10501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master * Unless required by applicable law or agreed to in writing, software 11501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master * distributed under the License is distributed on an "AS IS" BASIS, 12501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master * See the License for the specific language governing permissions and 14501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master * limitations under the License. 15501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master */ 16501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master 17501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b masterpackage com.android.providers.settings; 18501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master 198823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasaniimport java.util.Locale; 208823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani 218823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasaniimport android.app.ActivityManagerNative; 228823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasaniimport android.app.IActivityManager; 23501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b masterimport android.backup.BackupDataInput; 2403b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tateimport android.backup.IBackupManager; 25501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b masterimport android.content.ContentResolver; 26501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b masterimport android.content.Context; 2770c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasaniimport android.content.IContentService; 288823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasaniimport android.content.res.Configuration; 2970c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasaniimport android.location.LocationManager; 30501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b masterimport android.media.AudioManager; 31237a29923a05663a2195bf93b392768dbaf31ebfMike Lockwoodimport android.os.IPowerManager; 32501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b masterimport android.os.RemoteException; 33501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b masterimport android.os.ServiceManager; 34501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b masterimport android.provider.Settings; 358823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasaniimport android.text.TextUtils; 36501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b masterimport android.util.Log; 37501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master 38501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b masterpublic class SettingsHelper { 39501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master private static final String TAG = "SettingsHelper"; 40501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master 41501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master private Context mContext; 42501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master private AudioManager mAudioManager; 43501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master private IContentService mContentService; 44362aca60453ec7f27abb4d83fc29dd82095dc882Christopher Tate private IPowerManager mPowerManager; 4570c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani 4670c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani private boolean mSilent; 4770c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani private boolean mVibrate; 48501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master 49501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master public SettingsHelper(Context context) { 50501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master mContext = context; 51501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master mAudioManager = (AudioManager) context 52501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master .getSystemService(Context.AUDIO_SERVICE); 53501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master mContentService = ContentResolver.getContentService(); 54362aca60453ec7f27abb4d83fc29dd82095dc882Christopher Tate mPowerManager = IPowerManager.Stub.asInterface( 55362aca60453ec7f27abb4d83fc29dd82095dc882Christopher Tate ServiceManager.getService("power")); 56501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master } 57501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master 5870c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani /** 5970c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani * Sets the property via a call to the appropriate API, if any, and returns 6070c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani * whether or not the setting should be saved to the database as well. 6170c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani * @param name the name of the setting 6270c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani * @param value the string value of the setting 6370c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani * @return whether to continue with writing the value to the database. In 6470c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani * some cases the data will be written by the call to the appropriate API, 6570c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani * and in some cases the property value needs to be modified before setting. 6670c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani */ 6770c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani public boolean restoreValue(String name, String value) { 68501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master if (Settings.System.SCREEN_BRIGHTNESS.equals(name)) { 69501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master setBrightness(Integer.parseInt(value)); 70501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master } else if (Settings.System.SOUND_EFFECTS_ENABLED.equals(name)) { 7170c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani setSoundEffects(Integer.parseInt(value) == 1); 7270c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani } else if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) { 7370c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani setGpsLocation(value); 7470c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani return false; 7503b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate } else if (Settings.Secure.BACKUP_AUTO_RESTORE.equals(name)) { 7603b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate setAutoRestore(Integer.parseInt(value) == 1); 7770c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani } 7870c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani return true; 7970c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani } 8070c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani 8103b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate private void setAutoRestore(boolean enabled) { 8203b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate try { 8303b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate IBackupManager bm = IBackupManager.Stub.asInterface( 8403b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate ServiceManager.getService(Context.BACKUP_SERVICE)); 8503b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate if (bm != null) { 8603b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate bm.setAutoRestore(enabled); 8703b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate } 8803b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate } catch (RemoteException e) {} 8903b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate } 9003b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate 9170c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani private void setGpsLocation(String value) { 9270c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani final String GPS = LocationManager.GPS_PROVIDER; 9370c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani boolean enabled = 9470c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani GPS.equals(value) || 9570c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani value.startsWith(GPS + ",") || 9670c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani value.endsWith("," + GPS) || 9770c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani value.contains("," + GPS + ","); 9870c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani Settings.Secure.setLocationProviderEnabled( 9970c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani mContext.getContentResolver(), GPS, enabled); 10070c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani } 10170c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani 10270c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani private void setSoundEffects(boolean enable) { 10370c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani if (enable) { 10470c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani mAudioManager.loadSoundEffects(); 10570c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani } else { 10670c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani mAudioManager.unloadSoundEffects(); 107501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master } 108501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master } 109501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master 110501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master private void setBrightness(int brightness) { 111501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master try { 112237a29923a05663a2195bf93b392768dbaf31ebfMike Lockwood IPowerManager power = IPowerManager.Stub.asInterface( 113237a29923a05663a2195bf93b392768dbaf31ebfMike Lockwood ServiceManager.getService("power")); 114237a29923a05663a2195bf93b392768dbaf31ebfMike Lockwood if (power != null) { 115237a29923a05663a2195bf93b392768dbaf31ebfMike Lockwood power.setBacklightBrightness(brightness); 116501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master } 117501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master } catch (RemoteException doe) { 118501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master 119501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master } 120501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master } 121501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master 12270c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani private void setRingerMode() { 12370c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani if (mSilent) { 12470c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani mAudioManager.setRingerMode(mVibrate ? AudioManager.RINGER_MODE_VIBRATE : 12570c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani AudioManager.RINGER_MODE_SILENT); 12670c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani } else { 12770c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); 12870c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, 12970c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani mVibrate ? AudioManager.VIBRATE_SETTING_ON 13070c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani : AudioManager.VIBRATE_SETTING_OFF); 13170c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani } 13270c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani } 13370c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani 1348823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani byte[] getLocaleData() { 1358823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani Configuration conf = mContext.getResources().getConfiguration(); 1368823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani final Locale loc = conf.locale; 1378823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani String localeString = loc.getLanguage(); 1388823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani String country = loc.getCountry(); 1398823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani if (!TextUtils.isEmpty(country)) { 1408823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani localeString += "_" + country; 1418823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani } 1428823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani return localeString.getBytes(); 1438823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani } 1448823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani 1458823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani /** 1468823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani * Sets the locale specified. Input data is the equivalent of "ll_cc".getBytes(), where 1478823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani * "ll" is the language code and "cc" is the country code. 1488823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani * @param data the locale string in bytes. 1498823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani */ 1508823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani void setLocaleData(byte[] data) { 1518823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani // Check if locale was set by the user: 1528823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani Configuration conf = mContext.getResources().getConfiguration(); 1538823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani Locale loc = conf.locale; 154d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani // TODO: The following is not working as intended because the network is forcing a locale 155d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani // change after registering. Need to find some other way to detect if the user manually 156d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani // changed the locale 1578823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani if (conf.userSetLocale) return; // Don't change if user set it in the SetupWizard 1588823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani 1598823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani final String[] availableLocales = mContext.getAssets().getLocales(); 1608823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani String localeCode = new String(data); 1618823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani String language = new String(data, 0, 2); 1628823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani String country = data.length > 4 ? new String(data, 3, 2) : ""; 1638823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani loc = null; 1648823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani for (int i = 0; i < availableLocales.length; i++) { 1658823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani if (availableLocales[i].equals(localeCode)) { 1668823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani loc = new Locale(language, country); 1678823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani break; 1688823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani } 1698823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani } 1708823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani if (loc == null) return; // Couldn't find the saved locale in this version of the software 1718823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani 1728823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani try { 1738823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani IActivityManager am = ActivityManagerNative.getDefault(); 1748823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani Configuration config = am.getConfiguration(); 1758823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani config.locale = loc; 1768823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani // indicate this isn't some passing default - the user wants this remembered 1778823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani config.userSetLocale = true; 1788823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani 1798823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani am.updateConfiguration(config); 1808823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani } catch (RemoteException e) { 1818823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani // Intentionally left blank 1828823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani } 183d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani } 1848823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani 185d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani /** 186d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani * Informs the audio service of changes to the settings so that 187d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani * they can be re-read and applied. 188d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani */ 189d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani void applyAudioSettings() { 190d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani AudioManager am = new AudioManager(mContext); 191d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani am.reloadAudioSettings(); 1928823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani } 193501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master} 194