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.app.backup.BackupDataInput;
24import android.app.backup.IBackupManager;
25import android.content.ContentResolver;
26import android.content.Context;
27import android.content.IContentService;
28import android.content.res.Configuration;
29import android.location.LocationManager;
30import android.media.AudioManager;
31import android.os.IPowerManager;
32import android.os.RemoteException;
33import android.os.ServiceManager;
34import android.provider.Settings;
35import android.text.TextUtils;
36import android.util.Log;
37
38public class SettingsHelper {
39    private static final String TAG = "SettingsHelper";
40
41    private Context mContext;
42    private AudioManager mAudioManager;
43    private IContentService mContentService;
44    private IPowerManager mPowerManager;
45
46    public SettingsHelper(Context context) {
47        mContext = context;
48        mAudioManager = (AudioManager) context
49                .getSystemService(Context.AUDIO_SERVICE);
50        mContentService = ContentResolver.getContentService();
51        mPowerManager = IPowerManager.Stub.asInterface(
52                ServiceManager.getService("power"));
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        } else if (Settings.Secure.BACKUP_AUTO_RESTORE.equals(name)) {
73            setAutoRestore(Integer.parseInt(value) == 1);
74        }
75        return true;
76    }
77
78    private void setAutoRestore(boolean enabled) {
79        try {
80            IBackupManager bm = IBackupManager.Stub.asInterface(
81                    ServiceManager.getService(Context.BACKUP_SERVICE));
82            if (bm != null) {
83                bm.setAutoRestore(enabled);
84            }
85        } catch (RemoteException e) {}
86    }
87
88    private void setGpsLocation(String value) {
89        final String GPS = LocationManager.GPS_PROVIDER;
90        boolean enabled =
91                GPS.equals(value) ||
92                value.startsWith(GPS + ",") ||
93                value.endsWith("," + GPS) ||
94                value.contains("," + GPS + ",");
95        Settings.Secure.setLocationProviderEnabled(
96                mContext.getContentResolver(), GPS, enabled);
97    }
98
99    private void setSoundEffects(boolean enable) {
100        if (enable) {
101            mAudioManager.loadSoundEffects();
102        } else {
103            mAudioManager.unloadSoundEffects();
104        }
105    }
106
107    private void setBrightness(int brightness) {
108        try {
109            IPowerManager power = IPowerManager.Stub.asInterface(
110                    ServiceManager.getService("power"));
111            if (power != null) {
112                power.setBacklightBrightness(brightness);
113            }
114        } catch (RemoteException doe) {
115
116        }
117    }
118
119    byte[] getLocaleData() {
120        Configuration conf = mContext.getResources().getConfiguration();
121        final Locale loc = conf.locale;
122        String localeString = loc.getLanguage();
123        String country = loc.getCountry();
124        if (!TextUtils.isEmpty(country)) {
125            localeString += "_" + country;
126        }
127        return localeString.getBytes();
128    }
129
130    /**
131     * Sets the locale specified. Input data is the equivalent of "ll_cc".getBytes(), where
132     * "ll" is the language code and "cc" is the country code.
133     * @param data the locale string in bytes.
134     */
135    void setLocaleData(byte[] data, int size) {
136        // Check if locale was set by the user:
137        Configuration conf = mContext.getResources().getConfiguration();
138        Locale loc = conf.locale;
139        // TODO: The following is not working as intended because the network is forcing a locale
140        // change after registering. Need to find some other way to detect if the user manually
141        // changed the locale
142        if (conf.userSetLocale) return; // Don't change if user set it in the SetupWizard
143
144        final String[] availableLocales = mContext.getAssets().getLocales();
145        String localeCode = new String(data, 0, size);
146        String language = new String(data, 0, 2);
147        String country = size > 4 ? new String(data, 3, 2) : "";
148        loc = null;
149        for (int i = 0; i < availableLocales.length; i++) {
150            if (availableLocales[i].equals(localeCode)) {
151                loc = new Locale(language, country);
152                break;
153            }
154        }
155        if (loc == null) return; // Couldn't find the saved locale in this version of the software
156
157        try {
158            IActivityManager am = ActivityManagerNative.getDefault();
159            Configuration config = am.getConfiguration();
160            config.locale = loc;
161            // indicate this isn't some passing default - the user wants this remembered
162            config.userSetLocale = true;
163
164            am.updateConfiguration(config);
165        } catch (RemoteException e) {
166            // Intentionally left blank
167        }
168    }
169
170    /**
171     * Informs the audio service of changes to the settings so that
172     * they can be re-read and applied.
173     */
174    void applyAudioSettings() {
175        AudioManager am = new AudioManager(mContext);
176        am.reloadAudioSettings();
177    }
178}
179