SettingsHelper.java revision 237a29923a05663a2195bf93b392768dbaf31ebf
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.backup.BackupDataInput; 24import android.content.ContentResolver; 25import android.content.Context; 26import android.content.IContentService; 27import android.content.res.Configuration; 28import android.location.LocationManager; 29import android.media.AudioManager; 30import android.os.IPowerManager; 31import android.os.RemoteException; 32import android.os.ServiceManager; 33import android.provider.Settings; 34import android.text.TextUtils; 35import android.util.Log; 36 37public class SettingsHelper { 38 private static final String TAG = "SettingsHelper"; 39 40 private Context mContext; 41 private AudioManager mAudioManager; 42 private IContentService mContentService; 43 private static final String[] PROVIDERS = { "gmail-ls", "calendar", "contacts" }; 44 45 private boolean mSilent; 46 private boolean mVibrate; 47 48 public SettingsHelper(Context context) { 49 mContext = context; 50 mAudioManager = (AudioManager) context 51 .getSystemService(Context.AUDIO_SERVICE); 52 mContentService = ContentResolver.getContentService(); 53 } 54 55 /** 56 * Sets the property via a call to the appropriate API, if any, and returns 57 * whether or not the setting should be saved to the database as well. 58 * @param name the name of the setting 59 * @param value the string value of the setting 60 * @return whether to continue with writing the value to the database. In 61 * some cases the data will be written by the call to the appropriate API, 62 * and in some cases the property value needs to be modified before setting. 63 */ 64 public boolean restoreValue(String name, String value) { 65 if (Settings.System.SCREEN_BRIGHTNESS.equals(name)) { 66 setBrightness(Integer.parseInt(value)); 67 } else if (Settings.System.SOUND_EFFECTS_ENABLED.equals(name)) { 68 setSoundEffects(Integer.parseInt(value) == 1); 69 } else if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) { 70 setGpsLocation(value); 71 return false; 72 } 73 return true; 74 } 75 76 private void setGpsLocation(String value) { 77 final String GPS = LocationManager.GPS_PROVIDER; 78 boolean enabled = 79 GPS.equals(value) || 80 value.startsWith(GPS + ",") || 81 value.endsWith("," + GPS) || 82 value.contains("," + GPS + ","); 83 Settings.Secure.setLocationProviderEnabled( 84 mContext.getContentResolver(), GPS, enabled); 85 } 86 87 private void setSoundEffects(boolean enable) { 88 if (enable) { 89 mAudioManager.loadSoundEffects(); 90 } else { 91 mAudioManager.unloadSoundEffects(); 92 } 93 } 94 95 private void setBrightness(int brightness) { 96 try { 97 IPowerManager power = IPowerManager.Stub.asInterface( 98 ServiceManager.getService("power")); 99 if (power != null) { 100 power.setBacklightBrightness(brightness); 101 } 102 } catch (RemoteException doe) { 103 104 } 105 } 106 107 private void setRingerMode() { 108 if (mSilent) { 109 mAudioManager.setRingerMode(mVibrate ? AudioManager.RINGER_MODE_VIBRATE : 110 AudioManager.RINGER_MODE_SILENT); 111 } else { 112 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); 113 mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, 114 mVibrate ? AudioManager.VIBRATE_SETTING_ON 115 : AudioManager.VIBRATE_SETTING_OFF); 116 } 117 } 118 119 byte[] getSyncProviders() { 120 byte[] sync = new byte[1 + PROVIDERS.length]; 121 // TODO: Sync backup needs to be moved to SystemBackupAgent 122 /* 123 try { 124 sync[0] = (byte) (mContentService.getListenForNetworkTickles() ? 1 : 0); 125 for (int i = 0; i < PROVIDERS.length; i++) { 126 sync[i + 1] = (byte) 127 (mContentService.getSyncAutomatically(PROVIDERS[i]) ? 1 : 0); 128 } 129 } catch (RemoteException re) { 130 Log.w(TAG, "Unable to backup sync providers"); 131 return sync; 132 } 133 */ 134 return sync; 135 } 136 137 void setSyncProviders(BackupDataInput backup) { 138 byte[] sync = new byte[backup.getDataSize()]; 139 140 try { 141 backup.readEntityData(sync, 0, sync.length); 142 // TODO: Sync backup needs to be moved to SystemBackupAgent 143 /* 144 mContentService.setListenForNetworkTickles(sync[0] == 1); 145 for (int i = 0; i < PROVIDERS.length; i++) { 146 mContentService.setSyncProviderAutomatically(PROVIDERS[i], sync[i + 1] > 0); 147 } 148 } catch (RemoteException re) { 149 Log.w(TAG, "Unable to restore sync providers"); 150 */ 151 } catch (java.io.IOException ioe) { 152 Log.w(TAG, "Unable to read sync settings"); 153 } 154 } 155 156 byte[] getLocaleData() { 157 Configuration conf = mContext.getResources().getConfiguration(); 158 final Locale loc = conf.locale; 159 String localeString = loc.getLanguage(); 160 String country = loc.getCountry(); 161 if (!TextUtils.isEmpty(country)) { 162 localeString += "_" + country; 163 } 164 return localeString.getBytes(); 165 } 166 167 /** 168 * Sets the locale specified. Input data is the equivalent of "ll_cc".getBytes(), where 169 * "ll" is the language code and "cc" is the country code. 170 * @param data the locale string in bytes. 171 */ 172 void setLocaleData(byte[] data) { 173 // Check if locale was set by the user: 174 Configuration conf = mContext.getResources().getConfiguration(); 175 Locale loc = conf.locale; 176 // TODO: The following is not working as intended because the network is forcing a locale 177 // change after registering. Need to find some other way to detect if the user manually 178 // changed the locale 179 if (conf.userSetLocale) return; // Don't change if user set it in the SetupWizard 180 181 final String[] availableLocales = mContext.getAssets().getLocales(); 182 String localeCode = new String(data); 183 String language = new String(data, 0, 2); 184 String country = data.length > 4 ? new String(data, 3, 2) : ""; 185 loc = null; 186 for (int i = 0; i < availableLocales.length; i++) { 187 if (availableLocales[i].equals(localeCode)) { 188 loc = new Locale(language, country); 189 break; 190 } 191 } 192 if (loc == null) return; // Couldn't find the saved locale in this version of the software 193 194 try { 195 IActivityManager am = ActivityManagerNative.getDefault(); 196 Configuration config = am.getConfiguration(); 197 config.locale = loc; 198 // indicate this isn't some passing default - the user wants this remembered 199 config.userSetLocale = true; 200 201 am.updateConfiguration(config); 202 } catch (RemoteException e) { 203 // Intentionally left blank 204 } 205 } 206 207 /** 208 * Informs the audio service of changes to the settings so that 209 * they can be re-read and applied. 210 */ 211 void applyAudioSettings() { 212 AudioManager am = new AudioManager(mContext); 213 am.reloadAudioSettings(); 214 } 215} 216