blob: 4a6799722fbd2a5cd07f8e7b0855cb21b2ece77b [file] [log] [blame]
-b master501eec92009-07-06 13:53:11 -07001/*
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
Amith Yamasani8823c0a82009-07-07 14:30:17 -070019import android.app.ActivityManagerNative;
20import android.app.IActivityManager;
Christopher Tate45281862010-03-05 15:46:30 -080021import android.app.backup.IBackupManager;
-b master501eec92009-07-06 13:53:11 -070022import android.content.Context;
Amith Yamasani8823c0a82009-07-07 14:30:17 -070023import android.content.res.Configuration;
Amith Yamasani70c874b2009-07-06 14:53:25 -070024import android.location.LocationManager;
-b master501eec92009-07-06 13:53:11 -070025import android.media.AudioManager;
Mike Lockwood237a2992009-09-15 14:42:16 -040026import android.os.IPowerManager;
-b master501eec92009-07-06 13:53:11 -070027import android.os.RemoteException;
28import android.os.ServiceManager;
Maggie Benthall67944582013-02-22 14:58:27 -050029import android.os.UserManager;
-b master501eec92009-07-06 13:53:11 -070030import android.provider.Settings;
Amith Yamasani8823c0a82009-07-07 14:30:17 -070031import android.text.TextUtils;
-b master501eec92009-07-06 13:53:11 -070032
Maggie Benthall67944582013-02-22 14:58:27 -050033import java.util.Locale;
34
-b master501eec92009-07-06 13:53:11 -070035public class SettingsHelper {
-b master501eec92009-07-06 13:53:11 -070036 private Context mContext;
37 private AudioManager mAudioManager;
Amith Yamasani70c874b2009-07-06 14:53:25 -070038
-b master501eec92009-07-06 13:53:11 -070039 public SettingsHelper(Context context) {
40 mContext = context;
41 mAudioManager = (AudioManager) context
42 .getSystemService(Context.AUDIO_SERVICE);
-b master501eec92009-07-06 13:53:11 -070043 }
44
Amith Yamasani70c874b2009-07-06 14:53:25 -070045 /**
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 master501eec92009-07-06 13:53:11 -070055 if (Settings.System.SCREEN_BRIGHTNESS.equals(name)) {
56 setBrightness(Integer.parseInt(value));
57 } else if (Settings.System.SOUND_EFFECTS_ENABLED.equals(name)) {
Amith Yamasani70c874b2009-07-06 14:53:25 -070058 setSoundEffects(Integer.parseInt(value) == 1);
59 } else if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
60 setGpsLocation(value);
61 return false;
Christopher Tate03b6d902010-02-26 14:12:03 -080062 } else if (Settings.Secure.BACKUP_AUTO_RESTORE.equals(name)) {
63 setAutoRestore(Integer.parseInt(value) == 1);
Svetoslav Ganov818d2042012-09-12 21:35:15 -070064 } else if (isAlreadyConfiguredCriticalAccessibilitySetting(name)) {
65 return false;
Amith Yamasani70c874b2009-07-06 14:53:25 -070066 }
67 return true;
68 }
69
Svetoslav Ganov818d2042012-09-12 21:35:15 -070070 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 Tate03b6d902010-02-26 14:12:03 -080089 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 Yamasani70c874b2009-07-06 14:53:25 -070099 private void setGpsLocation(String value) {
Maggie Benthall67944582013-02-22 14:58:27 -0500100 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
101 if (! um.isLocationSharingToggleAllowed()) {
102 return;
103 }
Amith Yamasani70c874b2009-07-06 14:53:25 -0700104 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 master501eec92009-07-06 13:53:11 -0700119 }
120 }
121
122 private void setBrightness(int brightness) {
123 try {
Mike Lockwood237a2992009-09-15 14:42:16 -0400124 IPowerManager power = IPowerManager.Stub.asInterface(
125 ServiceManager.getService("power"));
126 if (power != null) {
Jeff Brown96307042012-07-27 15:51:34 -0700127 power.setTemporaryScreenBrightnessSettingOverride(brightness);
-b master501eec92009-07-06 13:53:11 -0700128 }
129 } catch (RemoteException doe) {
130
131 }
132 }
133
Amith Yamasani8823c0a82009-07-07 14:30:17 -0700134 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 Tate75a99702011-05-18 16:28:19 -0700150 void setLocaleData(byte[] data, int size) {
Amith Yamasani8823c0a82009-07-07 14:30:17 -0700151 // Check if locale was set by the user:
152 Configuration conf = mContext.getResources().getConfiguration();
153 Locale loc = conf.locale;
Amith Yamasanid1582142009-07-08 20:04:55 -0700154 // 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 Yamasani8823c0a82009-07-07 14:30:17 -0700157 if (conf.userSetLocale) return; // Don't change if user set it in the SetupWizard
158
159 final String[] availableLocales = mContext.getAssets().getLocales();
Christopher Tate75a99702011-05-18 16:28:19 -0700160 String localeCode = new String(data, 0, size);
Amith Yamasani8823c0a82009-07-07 14:30:17 -0700161 String language = new String(data, 0, 2);
Christopher Tate75a99702011-05-18 16:28:19 -0700162 String country = size > 4 ? new String(data, 3, 2) : "";
Amith Yamasani8823c0a82009-07-07 14:30:17 -0700163 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 Yamasanid1582142009-07-08 20:04:55 -0700183 }
Amith Yamasani8823c0a82009-07-07 14:30:17 -0700184
Amith Yamasanid1582142009-07-08 20:04:55 -0700185 /**
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 Yamasani8823c0a82009-07-07 14:30:17 -0700192 }
-b master501eec92009-07-06 13:53:11 -0700193}