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