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;
234528186e0d65fc68ef0dd1941aa2ac8aefcd55a3Christopher Tateimport android.app.backup.IBackupManager;
24501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b masterimport android.content.Context;
258823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasaniimport android.content.res.Configuration;
2670c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasaniimport android.location.LocationManager;
27501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b masterimport android.media.AudioManager;
28237a29923a05663a2195bf93b392768dbaf31ebfMike Lockwoodimport android.os.IPowerManager;
29501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b masterimport android.os.RemoteException;
30501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b masterimport android.os.ServiceManager;
31501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b masterimport android.provider.Settings;
328823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasaniimport android.text.TextUtils;
33501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master
34501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b masterpublic class SettingsHelper {
35501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master    private Context mContext;
36501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master    private AudioManager mAudioManager;
3770c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani
38501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master    public SettingsHelper(Context context) {
39501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master        mContext = context;
40501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master        mAudioManager = (AudioManager) context
41501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master                .getSystemService(Context.AUDIO_SERVICE);
42501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master    }
43501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master
4470c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani    /**
4570c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani     * Sets the property via a call to the appropriate API, if any, and returns
4670c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani     * whether or not the setting should be saved to the database as well.
4770c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani     * @param name the name of the setting
4870c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani     * @param value the string value of the setting
4970c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani     * @return whether to continue with writing the value to the database. In
5070c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani     * some cases the data will be written by the call to the appropriate API,
5170c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani     * and in some cases the property value needs to be modified before setting.
5270c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani     */
5370c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani    public boolean restoreValue(String name, String value) {
54501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master        if (Settings.System.SCREEN_BRIGHTNESS.equals(name)) {
55501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master            setBrightness(Integer.parseInt(value));
56501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master        } else if (Settings.System.SOUND_EFFECTS_ENABLED.equals(name)) {
5770c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani            setSoundEffects(Integer.parseInt(value) == 1);
5870c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani        } else if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
5970c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani            setGpsLocation(value);
6070c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani            return false;
6103b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate        } else if (Settings.Secure.BACKUP_AUTO_RESTORE.equals(name)) {
6203b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate            setAutoRestore(Integer.parseInt(value) == 1);
63818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov        } else if (isAlreadyConfiguredCriticalAccessibilitySetting(name)) {
64818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov            return false;
6570c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani        }
6670c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani        return true;
6770c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani    }
6870c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani
69818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov    private boolean isAlreadyConfiguredCriticalAccessibilitySetting(String name) {
70818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov        // These are the critical accessibility settings that are required for a
71818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov        // blind user to be able to interact with the device. If these settings are
72818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov        // already configured, we will not overwrite them. If they are already set,
73818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov        // it means that the user has performed a global gesture to enable accessibility
74818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov        // and definitely needs these features working after the restore.
75818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov        if (Settings.Secure.ACCESSIBILITY_ENABLED.equals(name)
76818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov                || Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION.equals(name)
77818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov                || Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD.equals(name)
78818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov                || Settings.Secure.TOUCH_EXPLORATION_ENABLED.equals(name)) {
79818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov            return Settings.Secure.getInt(mContext.getContentResolver(), name, 0) != 0;
80818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov        } else if (Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES.equals(name)
81818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov                || Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES.equals(name)) {
82818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov            return !TextUtils.isEmpty(Settings.Secure.getString(
83818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov                    mContext.getContentResolver(), name));
84818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov        }
85818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov        return false;
86818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov    }
87818d20459099ea75e9b8d27c341af482653847a1Svetoslav Ganov
8803b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate    private void setAutoRestore(boolean enabled) {
8903b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate        try {
9003b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate            IBackupManager bm = IBackupManager.Stub.asInterface(
9103b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate                    ServiceManager.getService(Context.BACKUP_SERVICE));
9203b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate            if (bm != null) {
9303b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate                bm.setAutoRestore(enabled);
9403b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate            }
9503b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate        } catch (RemoteException e) {}
9603b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate    }
9703b6d90db9acc531a945c57795b903a3b74dd0b7Christopher Tate
9870c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani    private void setGpsLocation(String value) {
9970c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani        final String GPS = LocationManager.GPS_PROVIDER;
10070c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani        boolean enabled =
10170c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani                GPS.equals(value) ||
10270c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani                value.startsWith(GPS + ",") ||
10370c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani                value.endsWith("," + GPS) ||
10470c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani                value.contains("," + GPS + ",");
10570c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani        Settings.Secure.setLocationProviderEnabled(
10670c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani                mContext.getContentResolver(), GPS, enabled);
10770c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani    }
10870c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani
10970c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani    private void setSoundEffects(boolean enable) {
11070c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani        if (enable) {
11170c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani            mAudioManager.loadSoundEffects();
11270c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani        } else {
11370c874ba20b586712a7550b6c5efeb6dc0fdf9faAmith Yamasani            mAudioManager.unloadSoundEffects();
114501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master        }
115501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master    }
116501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master
117501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master    private void setBrightness(int brightness) {
118501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master        try {
119237a29923a05663a2195bf93b392768dbaf31ebfMike Lockwood            IPowerManager power = IPowerManager.Stub.asInterface(
120237a29923a05663a2195bf93b392768dbaf31ebfMike Lockwood                    ServiceManager.getService("power"));
121237a29923a05663a2195bf93b392768dbaf31ebfMike Lockwood            if (power != null) {
1229630704ed3b265f008a8f64ec60a33cf9dcd3345Jeff Brown                power.setTemporaryScreenBrightnessSettingOverride(brightness);
123501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master            }
124501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master        } catch (RemoteException doe) {
125501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master
126501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master        }
127501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master    }
128501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master
1298823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani    byte[] getLocaleData() {
1308823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        Configuration conf = mContext.getResources().getConfiguration();
1318823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        final Locale loc = conf.locale;
1328823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        String localeString = loc.getLanguage();
1338823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        String country = loc.getCountry();
1348823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        if (!TextUtils.isEmpty(country)) {
1358823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani            localeString += "_" + country;
1368823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        }
1378823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        return localeString.getBytes();
1388823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani    }
1398823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani
1408823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani    /**
1418823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani     * Sets the locale specified. Input data is the equivalent of "ll_cc".getBytes(), where
1428823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani     * "ll" is the language code and "cc" is the country code.
1438823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani     * @param data the locale string in bytes.
1448823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani     */
14575a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate    void setLocaleData(byte[] data, int size) {
1468823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        // Check if locale was set by the user:
1478823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        Configuration conf = mContext.getResources().getConfiguration();
1488823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        Locale loc = conf.locale;
149d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani        // TODO: The following is not working as intended because the network is forcing a locale
150d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani        // change after registering. Need to find some other way to detect if the user manually
151d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani        // changed the locale
1528823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        if (conf.userSetLocale) return; // Don't change if user set it in the SetupWizard
1538823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani
1548823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        final String[] availableLocales = mContext.getAssets().getLocales();
15575a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        String localeCode = new String(data, 0, size);
1568823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        String language = new String(data, 0, 2);
15775a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        String country = size > 4 ? new String(data, 3, 2) : "";
1588823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        loc = null;
1598823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        for (int i = 0; i < availableLocales.length; i++) {
1608823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani            if (availableLocales[i].equals(localeCode)) {
1618823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani                loc = new Locale(language, country);
1628823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani                break;
1638823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani            }
1648823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        }
1658823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        if (loc == null) return; // Couldn't find the saved locale in this version of the software
1668823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani
1678823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        try {
1688823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani            IActivityManager am = ActivityManagerNative.getDefault();
1698823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani            Configuration config = am.getConfiguration();
1708823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani            config.locale = loc;
1718823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani            // indicate this isn't some passing default - the user wants this remembered
1728823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani            config.userSetLocale = true;
1738823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani
1748823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani            am.updateConfiguration(config);
1758823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        } catch (RemoteException e) {
1768823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani            // Intentionally left blank
1778823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani        }
178d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani    }
1798823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani
180d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani    /**
181d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani     * Informs the audio service of changes to the settings so that
182d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani     * they can be re-read and applied.
183d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani     */
184d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani    void applyAudioSettings() {
185d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani        AudioManager am = new AudioManager(mContext);
186d158214511a3c04753de04fa6389e46d33135c38Amith Yamasani        am.reloadAudioSettings();
1878823c0a8c68fe669c21c539eef9fc6541f0c7494Amith Yamasani    }
188501eec92f9f4f206ad7972c63f2d0ef0285d8e34-b master}
189