SettingsHelper.java revision 622bf2220cf7fb9bb526afa39921ee2aa93e32ca
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 android.app.ActivityManagerNative;
20import android.app.IActivityManager;
21import android.app.backup.IBackupManager;
22import android.content.Context;
23import android.content.res.Configuration;
24import android.location.LocationManager;
25import android.media.AudioManager;
26import android.media.RingtoneManager;
27import android.net.Uri;
28import android.os.IPowerManager;
29import android.os.RemoteException;
30import android.os.ServiceManager;
31import android.os.UserManager;
32import android.provider.Settings;
33import android.text.TextUtils;
34
35import java.util.Locale;
36
37public class SettingsHelper {
38    private static final String SILENT_RINGTONE = "_silent";
39    private Context mContext;
40    private AudioManager mAudioManager;
41
42    public SettingsHelper(Context context) {
43        mContext = context;
44        mAudioManager = (AudioManager) context
45                .getSystemService(Context.AUDIO_SERVICE);
46    }
47
48    /**
49     * Sets the property via a call to the appropriate API, if any, and returns
50     * whether or not the setting should be saved to the database as well.
51     * @param name the name of the setting
52     * @param value the string value of the setting
53     * @return whether to continue with writing the value to the database. In
54     * some cases the data will be written by the call to the appropriate API,
55     * and in some cases the property value needs to be modified before setting.
56     */
57    public boolean restoreValue(String name, String value) {
58        if (Settings.System.SCREEN_BRIGHTNESS.equals(name)) {
59            setBrightness(Integer.parseInt(value));
60        } else if (Settings.System.SOUND_EFFECTS_ENABLED.equals(name)) {
61            setSoundEffects(Integer.parseInt(value) == 1);
62        } else if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
63            setGpsLocation(value);
64            return false;
65        } else if (Settings.Secure.BACKUP_AUTO_RESTORE.equals(name)) {
66            setAutoRestore(Integer.parseInt(value) == 1);
67        } else if (isAlreadyConfiguredCriticalAccessibilitySetting(name)) {
68            return false;
69        } else if (Settings.System.RINGTONE.equals(name)
70                || Settings.System.NOTIFICATION_SOUND.equals(name)) {
71            setRingtone(name, value);
72            return false;
73        }
74        return true;
75    }
76
77    public String onBackupValue(String name, String value) {
78        // Special processing for backing up ringtones
79        if (Settings.System.RINGTONE.equals(name)
80                || Settings.System.NOTIFICATION_SOUND.equals(name)) {
81            if (value == null) {
82                // Silent ringtone
83                return SILENT_RINGTONE;
84            } else {
85                return getCanonicalRingtoneValue(value);
86            }
87        }
88        // Return the original value
89        return value;
90    }
91
92    /**
93     * Sets the ringtone of type specified by the name.
94     *
95     * @param name should be Settings.System.RINGTONE or Settings.System.NOTIFICATION_SOUND.
96     * @param value can be a canonicalized uri or "_silent" to indicate a silent (null) ringtone.
97     */
98    private void setRingtone(String name, String value) {
99        // If it's null, don't change the default
100        if (value == null) return;
101        Uri ringtoneUri = null;
102        if (SILENT_RINGTONE.equals(value)) {
103            ringtoneUri = null;
104        } else {
105            Uri canonicalUri = Uri.parse(value);
106            ringtoneUri = mContext.getContentResolver().uncanonicalize(canonicalUri);
107        }
108        final int ringtoneType = Settings.System.RINGTONE.equals(name)
109                ? RingtoneManager.TYPE_RINGTONE : RingtoneManager.TYPE_NOTIFICATION;
110        RingtoneManager.setActualDefaultRingtoneUri(mContext, ringtoneType, ringtoneUri);
111    }
112
113    private String getCanonicalRingtoneValue(String value) {
114        final Uri ringtoneUri = Uri.parse(value);
115        final Uri canonicalUri = mContext.getContentResolver().canonicalize(ringtoneUri);
116        return canonicalUri == null ? null : canonicalUri.toString();
117    }
118
119    private boolean isAlreadyConfiguredCriticalAccessibilitySetting(String name) {
120        // These are the critical accessibility settings that are required for a
121        // blind user to be able to interact with the device. If these settings are
122        // already configured, we will not overwrite them. If they are already set,
123        // it means that the user has performed a global gesture to enable accessibility
124        // and definitely needs these features working after the restore.
125        if (Settings.Secure.ACCESSIBILITY_ENABLED.equals(name)
126                || Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION.equals(name)
127                || Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD.equals(name)
128                || Settings.Secure.TOUCH_EXPLORATION_ENABLED.equals(name)) {
129            return Settings.Secure.getInt(mContext.getContentResolver(), name, 0) != 0;
130        } else if (Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES.equals(name)
131                || Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES.equals(name)) {
132            return !TextUtils.isEmpty(Settings.Secure.getString(
133                    mContext.getContentResolver(), name));
134        }
135        return false;
136    }
137
138    private void setAutoRestore(boolean enabled) {
139        try {
140            IBackupManager bm = IBackupManager.Stub.asInterface(
141                    ServiceManager.getService(Context.BACKUP_SERVICE));
142            if (bm != null) {
143                bm.setAutoRestore(enabled);
144            }
145        } catch (RemoteException e) {}
146    }
147
148    private void setGpsLocation(String value) {
149        UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
150        if (um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) {
151            return;
152        }
153        final String GPS = LocationManager.GPS_PROVIDER;
154        boolean enabled =
155                GPS.equals(value) ||
156                value.startsWith(GPS + ",") ||
157                value.endsWith("," + GPS) ||
158                value.contains("," + GPS + ",");
159        Settings.Secure.setLocationProviderEnabled(
160                mContext.getContentResolver(), GPS, enabled);
161    }
162
163    private void setSoundEffects(boolean enable) {
164        if (enable) {
165            mAudioManager.loadSoundEffects();
166        } else {
167            mAudioManager.unloadSoundEffects();
168        }
169    }
170
171    private void setBrightness(int brightness) {
172        try {
173            IPowerManager power = IPowerManager.Stub.asInterface(
174                    ServiceManager.getService("power"));
175            if (power != null) {
176                power.setTemporaryScreenBrightnessSettingOverride(brightness);
177            }
178        } catch (RemoteException doe) {
179
180        }
181    }
182
183    byte[] getLocaleData() {
184        Configuration conf = mContext.getResources().getConfiguration();
185        final Locale loc = conf.locale;
186        String localeString = loc.getLanguage();
187        String country = loc.getCountry();
188        if (!TextUtils.isEmpty(country)) {
189            localeString += "_" + country;
190        }
191        return localeString.getBytes();
192    }
193
194    /**
195     * Sets the locale specified. Input data is the equivalent of "ll_cc".getBytes(), where
196     * "ll" is the language code and "cc" is the country code.
197     * @param data the locale string in bytes.
198     */
199    void setLocaleData(byte[] data, int size) {
200        // Check if locale was set by the user:
201        Configuration conf = mContext.getResources().getConfiguration();
202        Locale loc = conf.locale;
203        // TODO: The following is not working as intended because the network is forcing a locale
204        // change after registering. Need to find some other way to detect if the user manually
205        // changed the locale
206        if (conf.userSetLocale) return; // Don't change if user set it in the SetupWizard
207
208        final String[] availableLocales = mContext.getAssets().getLocales();
209        String localeCode = new String(data, 0, size);
210        String language = new String(data, 0, 2);
211        String country = size > 4 ? new String(data, 3, 2) : "";
212        loc = null;
213        for (int i = 0; i < availableLocales.length; i++) {
214            if (availableLocales[i].equals(localeCode)) {
215                loc = new Locale(language, country);
216                break;
217            }
218        }
219        if (loc == null) return; // Couldn't find the saved locale in this version of the software
220
221        try {
222            IActivityManager am = ActivityManagerNative.getDefault();
223            Configuration config = am.getConfiguration();
224            config.locale = loc;
225            // indicate this isn't some passing default - the user wants this remembered
226            config.userSetLocale = true;
227
228            am.updateConfiguration(config);
229        } catch (RemoteException e) {
230            // Intentionally left blank
231        }
232    }
233
234    /**
235     * Informs the audio service of changes to the settings so that
236     * they can be re-read and applied.
237     */
238    void applyAudioSettings() {
239        AudioManager am = new AudioManager(mContext);
240        am.reloadAudioSettings();
241    }
242}
243