OSDN Git Service

8abdc641d6750e9891c11f20193a70cced73f0d4
[android-x86/frameworks-base.git] / packages / SettingsProvider / src / com / android / providers / settings / SettingsHelper.java
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
19 import android.app.ActivityManager;
20 import android.app.IActivityManager;
21 import android.app.backup.IBackupManager;
22 import android.content.ContentResolver;
23 import android.content.ContentValues;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.res.Configuration;
27 import android.location.LocationManager;
28 import android.media.AudioManager;
29 import android.media.RingtoneManager;
30 import android.net.Uri;
31 import android.os.IPowerManager;
32 import android.os.RemoteException;
33 import android.os.ServiceManager;
34 import android.os.UserHandle;
35 import android.os.UserManager;
36 import android.provider.Settings;
37 import android.telephony.TelephonyManager;
38 import android.text.TextUtils;
39 import android.util.ArraySet;
40 import android.util.Slog;
41
42 import java.util.Locale;
43
44 public class SettingsHelper {
45     private static final String TAG = "SettingsHelper";
46     private static final String SILENT_RINGTONE = "_silent";
47     private Context mContext;
48     private AudioManager mAudioManager;
49     private TelephonyManager mTelephonyManager;
50
51     /**
52      * A few settings elements are special in that a restore of those values needs to
53      * be post-processed by relevant parts of the OS.  A restore of any settings element
54      * mentioned in this table will therefore cause the system to send a broadcast with
55      * the {@link Intent#ACTION_SETTING_RESTORED} action, with extras naming the
56      * affected setting and supplying its pre-restore value for comparison.
57      *
58      * @see Intent#ACTION_SETTING_RESTORED
59      * @see System#SETTINGS_TO_BACKUP
60      * @see Secure#SETTINGS_TO_BACKUP
61      * @see Global#SETTINGS_TO_BACKUP
62      *
63      * {@hide}
64      */
65     private static final ArraySet<String> sBroadcastOnRestore;
66     static {
67         sBroadcastOnRestore = new ArraySet<String>(5);
68         sBroadcastOnRestore.add(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
69         sBroadcastOnRestore.add(Settings.Secure.ENABLED_VR_LISTENERS);
70         sBroadcastOnRestore.add(Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
71         sBroadcastOnRestore.add(Settings.Secure.ENABLED_INPUT_METHODS);
72         sBroadcastOnRestore.add(Settings.Global.BLUETOOTH_ON);
73     }
74
75     private interface SettingsLookup {
76         public String lookup(ContentResolver resolver, String name, int userHandle);
77     }
78
79     private static SettingsLookup sSystemLookup = new SettingsLookup() {
80         public String lookup(ContentResolver resolver, String name, int userHandle) {
81             return Settings.System.getStringForUser(resolver, name, userHandle);
82         }
83     };
84
85     private static SettingsLookup sSecureLookup = new SettingsLookup() {
86         public String lookup(ContentResolver resolver, String name, int userHandle) {
87             return Settings.Secure.getStringForUser(resolver, name, userHandle);
88         }
89     };
90
91     private static SettingsLookup sGlobalLookup = new SettingsLookup() {
92         public String lookup(ContentResolver resolver, String name, int userHandle) {
93             return Settings.Global.getStringForUser(resolver, name, userHandle);
94         }
95     };
96
97     public SettingsHelper(Context context) {
98         mContext = context;
99         mAudioManager = (AudioManager) context
100                 .getSystemService(Context.AUDIO_SERVICE);
101         mTelephonyManager = (TelephonyManager) context
102                 .getSystemService(Context.TELEPHONY_SERVICE);
103     }
104
105     /**
106      * Sets the property via a call to the appropriate API, if any, and returns
107      * whether or not the setting should be saved to the database as well.
108      * @param name the name of the setting
109      * @param value the string value of the setting
110      * @return whether to continue with writing the value to the database. In
111      * some cases the data will be written by the call to the appropriate API,
112      * and in some cases the property value needs to be modified before setting.
113      */
114     public void restoreValue(Context context, ContentResolver cr, ContentValues contentValues,
115             Uri destination, String name, String value) {
116         // Will we need a post-restore broadcast for this element?
117         String oldValue = null;
118         boolean sendBroadcast = false;
119         final SettingsLookup table;
120
121         if (destination.equals(Settings.Secure.CONTENT_URI)) {
122             table = sSecureLookup;
123         } else if (destination.equals(Settings.System.CONTENT_URI)) {
124             table = sSystemLookup;
125         } else { /* must be GLOBAL; this was preflighted by the caller */
126             table = sGlobalLookup;
127         }
128
129         if (sBroadcastOnRestore.contains(name)) {
130             // TODO: http://b/22388012
131             oldValue = table.lookup(cr, name, UserHandle.USER_SYSTEM);
132             sendBroadcast = true;
133         }
134
135         try {
136             if (Settings.System.SCREEN_BRIGHTNESS.equals(name)) {
137                 setBrightness(Integer.parseInt(value));
138                 // fall through to the ordinary write to settings
139             } else if (Settings.System.SOUND_EFFECTS_ENABLED.equals(name)) {
140                 setSoundEffects(Integer.parseInt(value) == 1);
141                 // fall through to the ordinary write to settings
142             } else if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
143                 setGpsLocation(value);
144                 return;
145             } else if (Settings.Secure.BACKUP_AUTO_RESTORE.equals(name)) {
146                 setAutoRestore(Integer.parseInt(value) == 1);
147             } else if (isAlreadyConfiguredCriticalAccessibilitySetting(name)) {
148                 return;
149             } else if (Settings.System.RINGTONE.equals(name)
150                     || Settings.System.NOTIFICATION_SOUND.equals(name)) {
151                 setRingtone(name, value);
152                 return;
153             }
154
155             // Default case: write the restored value to settings
156             contentValues.clear();
157             contentValues.put(Settings.NameValueTable.NAME, name);
158             contentValues.put(Settings.NameValueTable.VALUE, value);
159             cr.insert(destination, contentValues);
160         } catch (Exception e) {
161             // If we fail to apply the setting, by definition nothing happened
162             sendBroadcast = false;
163         } finally {
164             // If this was an element of interest, send the "we just restored it"
165             // broadcast with the historical value now that the new value has
166             // been committed and observers kicked off.
167             if (sendBroadcast) {
168                 Intent intent = new Intent(Intent.ACTION_SETTING_RESTORED)
169                         .setPackage("android").addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY)
170                         .putExtra(Intent.EXTRA_SETTING_NAME, name)
171                         .putExtra(Intent.EXTRA_SETTING_NEW_VALUE, value)
172                         .putExtra(Intent.EXTRA_SETTING_PREVIOUS_VALUE, oldValue);
173                 context.sendBroadcastAsUser(intent, UserHandle.SYSTEM, null);
174             }
175         }
176     }
177
178     public String onBackupValue(String name, String value) {
179         // Special processing for backing up ringtones & notification sounds
180         if (Settings.System.RINGTONE.equals(name)
181                 || Settings.System.NOTIFICATION_SOUND.equals(name)) {
182             if (value == null) {
183                 if (Settings.System.RINGTONE.equals(name)) {
184                     // For ringtones, we need to distinguish between non-telephony vs telephony
185                     if (mTelephonyManager != null && mTelephonyManager.isVoiceCapable()) {
186                         // Backup a null ringtone as silent on voice-capable devices
187                         return SILENT_RINGTONE;
188                     } else {
189                         // Skip backup of ringtone on non-telephony devices.
190                         return null;
191                     }
192                 } else {
193                     // Backup a null notification sound as silent
194                     return SILENT_RINGTONE;
195                 }
196             } else {
197                 return getCanonicalRingtoneValue(value);
198             }
199         }
200         // Return the original value
201         return value;
202     }
203
204     /**
205      * Sets the ringtone of type specified by the name.
206      *
207      * @param name should be Settings.System.RINGTONE or Settings.System.NOTIFICATION_SOUND.
208      * @param value can be a canonicalized uri or "_silent" to indicate a silent (null) ringtone.
209      */
210     private void setRingtone(String name, String value) {
211         // If it's null, don't change the default
212         if (value == null) return;
213         Uri ringtoneUri = null;
214         if (SILENT_RINGTONE.equals(value)) {
215             ringtoneUri = null;
216         } else {
217             Uri canonicalUri = Uri.parse(value);
218             ringtoneUri = mContext.getContentResolver().uncanonicalize(canonicalUri);
219             if (ringtoneUri == null) {
220                 // Unrecognized or invalid Uri, don't restore
221                 return;
222             }
223         }
224         final int ringtoneType = Settings.System.RINGTONE.equals(name)
225                 ? RingtoneManager.TYPE_RINGTONE : RingtoneManager.TYPE_NOTIFICATION;
226         RingtoneManager.setActualDefaultRingtoneUri(mContext, ringtoneType, ringtoneUri);
227     }
228
229     private String getCanonicalRingtoneValue(String value) {
230         final Uri ringtoneUri = Uri.parse(value);
231         final Uri canonicalUri = mContext.getContentResolver().canonicalize(ringtoneUri);
232         return canonicalUri == null ? null : canonicalUri.toString();
233     }
234
235     private boolean isAlreadyConfiguredCriticalAccessibilitySetting(String name) {
236         // These are the critical accessibility settings that are required for users with
237         // accessibility needs to be able to interact with the device. If these settings are
238         // already configured, we will not overwrite them. If they are already set,
239         // it means that the user has performed a global gesture to enable accessibility or set
240         // these settings in the Accessibility portion of the Setup Wizard, and definitely needs
241         // these features working after the restore.
242         switch (name) {
243             case Settings.Secure.ACCESSIBILITY_ENABLED:
244             case Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD:
245             case Settings.Secure.TOUCH_EXPLORATION_ENABLED:
246             case Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED:
247             case Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED:
248             case Settings.Secure.UI_NIGHT_MODE:
249                 return Settings.Secure.getInt(mContext.getContentResolver(), name, 0) != 0;
250             case Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES:
251             case Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES:
252             case Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER:
253             case Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_SCALE:
254                 return !TextUtils.isEmpty(Settings.Secure.getString(
255                         mContext.getContentResolver(), name));
256             case Settings.System.FONT_SCALE:
257                 return Settings.System.getFloat(mContext.getContentResolver(), name, 1.0f) != 1.0f;
258             default:
259                 return false;
260         }
261     }
262
263     private void setAutoRestore(boolean enabled) {
264         try {
265             IBackupManager bm = IBackupManager.Stub.asInterface(
266                     ServiceManager.getService(Context.BACKUP_SERVICE));
267             if (bm != null) {
268                 bm.setAutoRestore(enabled);
269             }
270         } catch (RemoteException e) {}
271     }
272
273     private void setGpsLocation(String value) {
274         UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
275         if (um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) {
276             return;
277         }
278         final String GPS = LocationManager.GPS_PROVIDER;
279         boolean enabled =
280                 GPS.equals(value) ||
281                 value.startsWith(GPS + ",") ||
282                 value.endsWith("," + GPS) ||
283                 value.contains("," + GPS + ",");
284         Settings.Secure.setLocationProviderEnabled(
285                 mContext.getContentResolver(), GPS, enabled);
286     }
287
288     private void setSoundEffects(boolean enable) {
289         if (enable) {
290             mAudioManager.loadSoundEffects();
291         } else {
292             mAudioManager.unloadSoundEffects();
293         }
294     }
295
296     private void setBrightness(int brightness) {
297         try {
298             IPowerManager power = IPowerManager.Stub.asInterface(
299                     ServiceManager.getService("power"));
300             if (power != null) {
301                 power.setTemporaryScreenBrightnessSettingOverride(brightness);
302             }
303         } catch (RemoteException doe) {
304
305         }
306     }
307
308     byte[] getLocaleData() {
309         Configuration conf = mContext.getResources().getConfiguration();
310         final Locale loc = conf.locale;
311         String localeString = loc.getLanguage();
312         String country = loc.getCountry();
313         if (!TextUtils.isEmpty(country)) {
314             localeString += "-" + country;
315         }
316         return localeString.getBytes();
317     }
318
319     /**
320      * Sets the locale specified. Input data is the byte representation of a
321      * BCP-47 language tag. For backwards compatibility, strings of the form
322      * {@code ll_CC} are also accepted, where {@code ll} is a two letter language
323      * code and {@code CC} is a two letter country code.
324      *
325      * @param data the locale string in bytes.
326      */
327     void setLocaleData(byte[] data, int size) {
328         // Check if locale was set by the user:
329         final ContentResolver cr = mContext.getContentResolver();
330         final boolean userSetLocale = mContext.getResources().getConfiguration().userSetLocale;
331         final boolean provisioned = Settings.Global.getInt(cr,
332                 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
333         if (userSetLocale || provisioned) {
334             // Don't change if user set it in the SetupWizard, or if this is a post-setup
335             // deferred restore operation
336             Slog.i(TAG, "Not applying restored locale; "
337                     + (userSetLocale ? "user already specified" : "device already provisioned"));
338             return;
339         }
340
341         final String[] availableLocales = mContext.getAssets().getLocales();
342         // Replace "_" with "-" to deal with older backups.
343         String localeCode = new String(data, 0, size).replace('_', '-');
344         Locale loc = null;
345         for (int i = 0; i < availableLocales.length; i++) {
346             if (availableLocales[i].equals(localeCode)) {
347                 loc = Locale.forLanguageTag(localeCode);
348                 break;
349             }
350         }
351         if (loc == null) return; // Couldn't find the saved locale in this version of the software
352
353         try {
354             IActivityManager am = ActivityManager.getService();
355             Configuration config = am.getConfiguration();
356             config.locale = loc;
357             // indicate this isn't some passing default - the user wants this remembered
358             config.userSetLocale = true;
359
360             am.updateConfiguration(config);
361         } catch (RemoteException e) {
362             // Intentionally left blank
363         }
364     }
365
366     /**
367      * Informs the audio service of changes to the settings so that
368      * they can be re-read and applied.
369      */
370     void applyAudioSettings() {
371         AudioManager am = new AudioManager(mContext);
372         am.reloadAudioSettings();
373     }
374 }