-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 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 | |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 19 | import android.os.Process; |
Seigo Nonaka | 0f19cc7 | 2017-06-26 16:06:17 -0700 | [diff] [blame] | 20 | import com.android.internal.R; |
| 21 | import com.android.internal.app.LocalePicker; |
| 22 | import com.android.internal.annotations.VisibleForTesting; |
| 23 | |
| 24 | import android.annotation.NonNull; |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 25 | import android.app.ActivityManager; |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 26 | import android.app.IActivityManager; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 27 | import android.app.backup.IBackupManager; |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 28 | import android.content.ContentResolver; |
| 29 | import android.content.ContentValues; |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 30 | import android.content.Context; |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 31 | import android.content.Intent; |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 32 | import android.content.res.Configuration; |
Seigo Nonaka | 0f19cc7 | 2017-06-26 16:06:17 -0700 | [diff] [blame] | 33 | import android.icu.util.ULocale; |
Amith Yamasani | 70c874b | 2009-07-06 14:53:25 -0700 | [diff] [blame] | 34 | import android.location.LocationManager; |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 35 | import android.media.AudioManager; |
Amith Yamasani | 622bf222 | 2013-09-06 13:54:28 -0700 | [diff] [blame] | 36 | import android.media.RingtoneManager; |
| 37 | import android.net.Uri; |
Mike Lockwood | 237a299 | 2009-09-15 14:42:16 -0400 | [diff] [blame] | 38 | import android.os.IPowerManager; |
Seigo Nonaka | 0f19cc7 | 2017-06-26 16:06:17 -0700 | [diff] [blame] | 39 | import android.os.LocaleList; |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 40 | import android.os.RemoteException; |
| 41 | import android.os.ServiceManager; |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 42 | import android.os.UserHandle; |
Maggie Benthall | 6794458 | 2013-02-22 14:58:27 -0500 | [diff] [blame] | 43 | import android.os.UserManager; |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 44 | import android.provider.Settings; |
Marvin Paul | 8fc5072 | 2014-12-23 11:46:33 -0800 | [diff] [blame] | 45 | import android.telephony.TelephonyManager; |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 46 | import android.text.TextUtils; |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 47 | import android.util.ArraySet; |
Christopher Tate | 45d1758 | 2017-06-26 17:35:26 -0700 | [diff] [blame] | 48 | import android.util.Slog; |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 49 | |
Seigo Nonaka | 0f19cc7 | 2017-06-26 16:06:17 -0700 | [diff] [blame] | 50 | import java.util.ArrayList; |
| 51 | import java.util.HashMap; |
| 52 | import java.util.List; |
Maggie Benthall | 6794458 | 2013-02-22 14:58:27 -0500 | [diff] [blame] | 53 | import java.util.Locale; |
| 54 | |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 55 | public class SettingsHelper { |
Christopher Tate | 45d1758 | 2017-06-26 17:35:26 -0700 | [diff] [blame] | 56 | private static final String TAG = "SettingsHelper"; |
Amith Yamasani | 622bf222 | 2013-09-06 13:54:28 -0700 | [diff] [blame] | 57 | private static final String SILENT_RINGTONE = "_silent"; |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 58 | private Context mContext; |
| 59 | private AudioManager mAudioManager; |
Marvin Paul | 8fc5072 | 2014-12-23 11:46:33 -0800 | [diff] [blame] | 60 | private TelephonyManager mTelephonyManager; |
Amith Yamasani | 70c874b | 2009-07-06 14:53:25 -0700 | [diff] [blame] | 61 | |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 62 | /** |
| 63 | * A few settings elements are special in that a restore of those values needs to |
| 64 | * be post-processed by relevant parts of the OS. A restore of any settings element |
| 65 | * mentioned in this table will therefore cause the system to send a broadcast with |
| 66 | * the {@link Intent#ACTION_SETTING_RESTORED} action, with extras naming the |
| 67 | * affected setting and supplying its pre-restore value for comparison. |
| 68 | * |
| 69 | * @see Intent#ACTION_SETTING_RESTORED |
| 70 | * @see System#SETTINGS_TO_BACKUP |
| 71 | * @see Secure#SETTINGS_TO_BACKUP |
| 72 | * @see Global#SETTINGS_TO_BACKUP |
| 73 | * |
| 74 | * {@hide} |
| 75 | */ |
| 76 | private static final ArraySet<String> sBroadcastOnRestore; |
| 77 | static { |
Stanley Tng | 767f05f | 2017-05-01 21:27:31 -0700 | [diff] [blame] | 78 | sBroadcastOnRestore = new ArraySet<String>(5); |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 79 | sBroadcastOnRestore.add(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS); |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 80 | sBroadcastOnRestore.add(Settings.Secure.ENABLED_VR_LISTENERS); |
Christopher Tate | 2d4aadc | 2015-03-16 16:55:14 -0700 | [diff] [blame] | 81 | sBroadcastOnRestore.add(Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES); |
Christopher Tate | 7b9a28c | 2015-03-18 13:06:16 -0700 | [diff] [blame] | 82 | sBroadcastOnRestore.add(Settings.Secure.ENABLED_INPUT_METHODS); |
Stanley Tng | 767f05f | 2017-05-01 21:27:31 -0700 | [diff] [blame] | 83 | sBroadcastOnRestore.add(Settings.Global.BLUETOOTH_ON); |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private interface SettingsLookup { |
| 87 | public String lookup(ContentResolver resolver, String name, int userHandle); |
| 88 | } |
| 89 | |
| 90 | private static SettingsLookup sSystemLookup = new SettingsLookup() { |
| 91 | public String lookup(ContentResolver resolver, String name, int userHandle) { |
| 92 | return Settings.System.getStringForUser(resolver, name, userHandle); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | private static SettingsLookup sSecureLookup = new SettingsLookup() { |
| 97 | public String lookup(ContentResolver resolver, String name, int userHandle) { |
| 98 | return Settings.Secure.getStringForUser(resolver, name, userHandle); |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | private static SettingsLookup sGlobalLookup = new SettingsLookup() { |
| 103 | public String lookup(ContentResolver resolver, String name, int userHandle) { |
| 104 | return Settings.Global.getStringForUser(resolver, name, userHandle); |
| 105 | } |
| 106 | }; |
| 107 | |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 108 | public SettingsHelper(Context context) { |
| 109 | mContext = context; |
| 110 | mAudioManager = (AudioManager) context |
| 111 | .getSystemService(Context.AUDIO_SERVICE); |
Marvin Paul | 8fc5072 | 2014-12-23 11:46:33 -0800 | [diff] [blame] | 112 | mTelephonyManager = (TelephonyManager) context |
| 113 | .getSystemService(Context.TELEPHONY_SERVICE); |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Amith Yamasani | 70c874b | 2009-07-06 14:53:25 -0700 | [diff] [blame] | 116 | /** |
| 117 | * Sets the property via a call to the appropriate API, if any, and returns |
| 118 | * whether or not the setting should be saved to the database as well. |
| 119 | * @param name the name of the setting |
| 120 | * @param value the string value of the setting |
| 121 | * @return whether to continue with writing the value to the database. In |
| 122 | * some cases the data will be written by the call to the appropriate API, |
| 123 | * and in some cases the property value needs to be modified before setting. |
| 124 | */ |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 125 | public void restoreValue(Context context, ContentResolver cr, ContentValues contentValues, |
Michal Karpinski | 6135a26 | 2017-08-11 10:45:58 +0100 | [diff] [blame] | 126 | Uri destination, String name, String value, int restoredFromSdkInt) { |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 127 | // Will we need a post-restore broadcast for this element? |
| 128 | String oldValue = null; |
| 129 | boolean sendBroadcast = false; |
| 130 | final SettingsLookup table; |
| 131 | |
| 132 | if (destination.equals(Settings.Secure.CONTENT_URI)) { |
| 133 | table = sSecureLookup; |
| 134 | } else if (destination.equals(Settings.System.CONTENT_URI)) { |
| 135 | table = sSystemLookup; |
| 136 | } else { /* must be GLOBAL; this was preflighted by the caller */ |
| 137 | table = sGlobalLookup; |
Amith Yamasani | 70c874b | 2009-07-06 14:53:25 -0700 | [diff] [blame] | 138 | } |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 139 | |
| 140 | if (sBroadcastOnRestore.contains(name)) { |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 141 | // TODO: http://b/22388012 |
| 142 | oldValue = table.lookup(cr, name, UserHandle.USER_SYSTEM); |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 143 | sendBroadcast = true; |
| 144 | } |
| 145 | |
| 146 | try { |
| 147 | if (Settings.System.SCREEN_BRIGHTNESS.equals(name)) { |
| 148 | setBrightness(Integer.parseInt(value)); |
| 149 | // fall through to the ordinary write to settings |
| 150 | } else if (Settings.System.SOUND_EFFECTS_ENABLED.equals(name)) { |
| 151 | setSoundEffects(Integer.parseInt(value) == 1); |
| 152 | // fall through to the ordinary write to settings |
| 153 | } else if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) { |
| 154 | setGpsLocation(value); |
| 155 | return; |
| 156 | } else if (Settings.Secure.BACKUP_AUTO_RESTORE.equals(name)) { |
| 157 | setAutoRestore(Integer.parseInt(value) == 1); |
| 158 | } else if (isAlreadyConfiguredCriticalAccessibilitySetting(name)) { |
| 159 | return; |
| 160 | } else if (Settings.System.RINGTONE.equals(name) |
| 161 | || Settings.System.NOTIFICATION_SOUND.equals(name)) { |
| 162 | setRingtone(name, value); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | // Default case: write the restored value to settings |
| 167 | contentValues.clear(); |
| 168 | contentValues.put(Settings.NameValueTable.NAME, name); |
| 169 | contentValues.put(Settings.NameValueTable.VALUE, value); |
| 170 | cr.insert(destination, contentValues); |
| 171 | } catch (Exception e) { |
| 172 | // If we fail to apply the setting, by definition nothing happened |
| 173 | sendBroadcast = false; |
| 174 | } finally { |
| 175 | // If this was an element of interest, send the "we just restored it" |
| 176 | // broadcast with the historical value now that the new value has |
| 177 | // been committed and observers kicked off. |
| 178 | if (sendBroadcast) { |
| 179 | Intent intent = new Intent(Intent.ACTION_SETTING_RESTORED) |
| 180 | .setPackage("android").addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY) |
| 181 | .putExtra(Intent.EXTRA_SETTING_NAME, name) |
| 182 | .putExtra(Intent.EXTRA_SETTING_NEW_VALUE, value) |
Michal Karpinski | 6135a26 | 2017-08-11 10:45:58 +0100 | [diff] [blame] | 183 | .putExtra(Intent.EXTRA_SETTING_PREVIOUS_VALUE, oldValue) |
| 184 | .putExtra(Intent.EXTRA_SETTING_RESTORED_FROM_SDK_INT, restoredFromSdkInt); |
Xiaohui Chen | e4de5a0 | 2015-09-22 15:33:31 -0700 | [diff] [blame] | 185 | context.sendBroadcastAsUser(intent, UserHandle.SYSTEM, null); |
Christopher Tate | 6597e34 | 2015-02-17 12:15:25 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
Amith Yamasani | 70c874b | 2009-07-06 14:53:25 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Amith Yamasani | 622bf222 | 2013-09-06 13:54:28 -0700 | [diff] [blame] | 190 | public String onBackupValue(String name, String value) { |
Marvin Paul | 8fc5072 | 2014-12-23 11:46:33 -0800 | [diff] [blame] | 191 | // Special processing for backing up ringtones & notification sounds |
Amith Yamasani | 622bf222 | 2013-09-06 13:54:28 -0700 | [diff] [blame] | 192 | if (Settings.System.RINGTONE.equals(name) |
| 193 | || Settings.System.NOTIFICATION_SOUND.equals(name)) { |
| 194 | if (value == null) { |
Marvin Paul | 8fc5072 | 2014-12-23 11:46:33 -0800 | [diff] [blame] | 195 | if (Settings.System.RINGTONE.equals(name)) { |
| 196 | // For ringtones, we need to distinguish between non-telephony vs telephony |
| 197 | if (mTelephonyManager != null && mTelephonyManager.isVoiceCapable()) { |
| 198 | // Backup a null ringtone as silent on voice-capable devices |
| 199 | return SILENT_RINGTONE; |
| 200 | } else { |
| 201 | // Skip backup of ringtone on non-telephony devices. |
| 202 | return null; |
| 203 | } |
| 204 | } else { |
| 205 | // Backup a null notification sound as silent |
| 206 | return SILENT_RINGTONE; |
| 207 | } |
Amith Yamasani | 622bf222 | 2013-09-06 13:54:28 -0700 | [diff] [blame] | 208 | } else { |
| 209 | return getCanonicalRingtoneValue(value); |
| 210 | } |
| 211 | } |
| 212 | // Return the original value |
| 213 | return value; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Sets the ringtone of type specified by the name. |
| 218 | * |
| 219 | * @param name should be Settings.System.RINGTONE or Settings.System.NOTIFICATION_SOUND. |
| 220 | * @param value can be a canonicalized uri or "_silent" to indicate a silent (null) ringtone. |
| 221 | */ |
| 222 | private void setRingtone(String name, String value) { |
| 223 | // If it's null, don't change the default |
| 224 | if (value == null) return; |
| 225 | Uri ringtoneUri = null; |
| 226 | if (SILENT_RINGTONE.equals(value)) { |
| 227 | ringtoneUri = null; |
| 228 | } else { |
| 229 | Uri canonicalUri = Uri.parse(value); |
| 230 | ringtoneUri = mContext.getContentResolver().uncanonicalize(canonicalUri); |
Amith Yamasani | 2e80444 | 2013-09-11 11:11:19 -0700 | [diff] [blame] | 231 | if (ringtoneUri == null) { |
| 232 | // Unrecognized or invalid Uri, don't restore |
| 233 | return; |
| 234 | } |
Amith Yamasani | 622bf222 | 2013-09-06 13:54:28 -0700 | [diff] [blame] | 235 | } |
| 236 | final int ringtoneType = Settings.System.RINGTONE.equals(name) |
| 237 | ? RingtoneManager.TYPE_RINGTONE : RingtoneManager.TYPE_NOTIFICATION; |
| 238 | RingtoneManager.setActualDefaultRingtoneUri(mContext, ringtoneType, ringtoneUri); |
| 239 | } |
| 240 | |
| 241 | private String getCanonicalRingtoneValue(String value) { |
| 242 | final Uri ringtoneUri = Uri.parse(value); |
| 243 | final Uri canonicalUri = mContext.getContentResolver().canonicalize(ringtoneUri); |
| 244 | return canonicalUri == null ? null : canonicalUri.toString(); |
| 245 | } |
| 246 | |
Svetoslav Ganov | 818d204 | 2012-09-12 21:35:15 -0700 | [diff] [blame] | 247 | private boolean isAlreadyConfiguredCriticalAccessibilitySetting(String name) { |
Anna Galusza | fa7786c | 2015-12-22 10:53:44 -0800 | [diff] [blame] | 248 | // These are the critical accessibility settings that are required for users with |
| 249 | // accessibility needs to be able to interact with the device. If these settings are |
Svetoslav Ganov | 818d204 | 2012-09-12 21:35:15 -0700 | [diff] [blame] | 250 | // already configured, we will not overwrite them. If they are already set, |
Anna Galusza | fa7786c | 2015-12-22 10:53:44 -0800 | [diff] [blame] | 251 | // it means that the user has performed a global gesture to enable accessibility or set |
| 252 | // these settings in the Accessibility portion of the Setup Wizard, and definitely needs |
| 253 | // these features working after the restore. |
Casey Burkhardt | 86b867f | 2015-12-29 18:03:36 -0800 | [diff] [blame] | 254 | switch (name) { |
| 255 | case Settings.Secure.ACCESSIBILITY_ENABLED: |
Casey Burkhardt | 86b867f | 2015-12-29 18:03:36 -0800 | [diff] [blame] | 256 | case Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD: |
| 257 | case Settings.Secure.TOUCH_EXPLORATION_ENABLED: |
| 258 | case Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED: |
| 259 | case Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED: |
| 260 | case Settings.Secure.UI_NIGHT_MODE: |
| 261 | return Settings.Secure.getInt(mContext.getContentResolver(), name, 0) != 0; |
| 262 | case Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES: |
| 263 | case Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES: |
Casey Burkhardt | 86b867f | 2015-12-29 18:03:36 -0800 | [diff] [blame] | 264 | case Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER: |
| 265 | case Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_SCALE: |
| 266 | return !TextUtils.isEmpty(Settings.Secure.getString( |
| 267 | mContext.getContentResolver(), name)); |
| 268 | case Settings.System.FONT_SCALE: |
| 269 | return Settings.System.getFloat(mContext.getContentResolver(), name, 1.0f) != 1.0f; |
| 270 | default: |
| 271 | return false; |
Svetoslav Ganov | 818d204 | 2012-09-12 21:35:15 -0700 | [diff] [blame] | 272 | } |
Svetoslav Ganov | 818d204 | 2012-09-12 21:35:15 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Christopher Tate | 03b6d90 | 2010-02-26 14:12:03 -0800 | [diff] [blame] | 275 | private void setAutoRestore(boolean enabled) { |
| 276 | try { |
| 277 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 278 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 279 | if (bm != null) { |
| 280 | bm.setAutoRestore(enabled); |
| 281 | } |
| 282 | } catch (RemoteException e) {} |
| 283 | } |
| 284 | |
Amith Yamasani | 70c874b | 2009-07-06 14:53:25 -0700 | [diff] [blame] | 285 | private void setGpsLocation(String value) { |
Maggie Benthall | 6794458 | 2013-02-22 14:58:27 -0500 | [diff] [blame] | 286 | UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 287 | if (um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) { |
Maggie Benthall | 6794458 | 2013-02-22 14:58:27 -0500 | [diff] [blame] | 288 | return; |
| 289 | } |
Amith Yamasani | 70c874b | 2009-07-06 14:53:25 -0700 | [diff] [blame] | 290 | final String GPS = LocationManager.GPS_PROVIDER; |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 291 | boolean enabled = |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 292 | GPS.equals(value) || |
Amith Yamasani | 70c874b | 2009-07-06 14:53:25 -0700 | [diff] [blame] | 293 | value.startsWith(GPS + ",") || |
| 294 | value.endsWith("," + GPS) || |
| 295 | value.contains("," + GPS + ","); |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 296 | LocationManager lm = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); |
| 297 | lm.setProviderEnabledForUser(GPS, enabled, Process.myUserHandle()); |
Amith Yamasani | 70c874b | 2009-07-06 14:53:25 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | private void setSoundEffects(boolean enable) { |
| 301 | if (enable) { |
| 302 | mAudioManager.loadSoundEffects(); |
| 303 | } else { |
| 304 | mAudioManager.unloadSoundEffects(); |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | private void setBrightness(int brightness) { |
| 309 | try { |
Mike Lockwood | 237a299 | 2009-09-15 14:42:16 -0400 | [diff] [blame] | 310 | IPowerManager power = IPowerManager.Stub.asInterface( |
| 311 | ServiceManager.getService("power")); |
| 312 | if (power != null) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 313 | power.setTemporaryScreenBrightnessSettingOverride(brightness); |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 314 | } |
| 315 | } catch (RemoteException doe) { |
| 316 | |
| 317 | } |
| 318 | } |
| 319 | |
Seigo Nonaka | 0f19cc7 | 2017-06-26 16:06:17 -0700 | [diff] [blame] | 320 | /* package */ byte[] getLocaleData() { |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 321 | Configuration conf = mContext.getResources().getConfiguration(); |
Seigo Nonaka | 0f19cc7 | 2017-06-26 16:06:17 -0700 | [diff] [blame] | 322 | return conf.getLocales().toLanguageTags().getBytes(); |
| 323 | } |
| 324 | |
| 325 | private static Locale toFullLocale(@NonNull Locale locale) { |
| 326 | if (locale.getScript().isEmpty() || locale.getCountry().isEmpty()) { |
| 327 | return ULocale.addLikelySubtags(ULocale.forLocale(locale)).toLocale(); |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 328 | } |
Seigo Nonaka | 0f19cc7 | 2017-06-26 16:06:17 -0700 | [diff] [blame] | 329 | return locale; |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** |
Seigo Nonaka | 0f19cc7 | 2017-06-26 16:06:17 -0700 | [diff] [blame] | 333 | * Merging the locale came from backup server and current device locale. |
| 334 | * |
| 335 | * Merge works with following rules. |
| 336 | * - The backup locales are appended to the current locale with keeping order. |
| 337 | * e.g. current locale "en-US,zh-CN" and backup locale "ja-JP,ko-KR" are merged to |
| 338 | * "en-US,zh-CH,ja-JP,ko-KR". |
| 339 | * |
| 340 | * - Duplicated locales are dropped. |
| 341 | * e.g. current locale "en-US,zh-CN" and backup locale "ja-JP,zh-Hans-CN,en-US" are merged to |
| 342 | * "en-US,zh-CN,ja-JP". |
| 343 | * |
| 344 | * - Unsupported locales are dropped. |
| 345 | * e.g. current locale "en-US" and backup locale "ja-JP,zh-CN" but the supported locales |
| 346 | * are "en-US,zh-CN", the merged locale list is "en-US,zh-CN". |
| 347 | * |
| 348 | * - The final result locale list only contains the supported locales. |
| 349 | * e.g. current locale "en-US" and backup locale "zh-Hans-CN" and supported locales are |
| 350 | * "en-US,zh-CN", the merged locale list is "en-US,zh-CN". |
| 351 | * |
| 352 | * @param restore The locale list that came from backup server. |
| 353 | * @param current The device's locale setting. |
| 354 | * @param supportedLocales The list of language tags supported by this device. |
| 355 | */ |
| 356 | @VisibleForTesting |
| 357 | public static LocaleList resolveLocales(LocaleList restore, LocaleList current, |
| 358 | String[] supportedLocales) { |
| 359 | final HashMap<Locale, Locale> allLocales = new HashMap<>(supportedLocales.length); |
| 360 | for (String supportedLocaleStr : supportedLocales) { |
| 361 | final Locale locale = Locale.forLanguageTag(supportedLocaleStr); |
| 362 | allLocales.put(toFullLocale(locale), locale); |
| 363 | } |
| 364 | |
| 365 | final ArrayList<Locale> filtered = new ArrayList<>(current.size()); |
| 366 | for (int i = 0; i < current.size(); i++) { |
| 367 | final Locale locale = current.get(i); |
| 368 | allLocales.remove(toFullLocale(locale)); |
| 369 | filtered.add(locale); |
| 370 | } |
| 371 | |
| 372 | for (int i = 0; i < restore.size(); i++) { |
| 373 | final Locale locale = allLocales.remove(toFullLocale(restore.get(i))); |
| 374 | if (locale != null) { |
| 375 | filtered.add(locale); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | if (filtered.size() == current.size()) { |
| 380 | return current; // Nothing added to current locale list. |
| 381 | } |
| 382 | |
| 383 | return new LocaleList(filtered.toArray(new Locale[filtered.size()])); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Sets the locale specified. Input data is the byte representation of comma separated |
| 388 | * multiple BCP-47 language tags. For backwards compatibility, strings of the form |
Narayan Kamath | 11bfc22 | 2014-07-30 15:42:25 +0100 | [diff] [blame] | 389 | * {@code ll_CC} are also accepted, where {@code ll} is a two letter language |
| 390 | * code and {@code CC} is a two letter country code. |
| 391 | * |
Seigo Nonaka | 0f19cc7 | 2017-06-26 16:06:17 -0700 | [diff] [blame] | 392 | * @param data the comma separated BCP-47 language tags in bytes. |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 393 | */ |
Seigo Nonaka | 0f19cc7 | 2017-06-26 16:06:17 -0700 | [diff] [blame] | 394 | /* package */ void setLocaleData(byte[] data, int size) { |
| 395 | final Configuration conf = mContext.getResources().getConfiguration(); |
| 396 | |
| 397 | // Replace "_" with "-" to deal with older backups. |
| 398 | final String localeCodes = new String(data, 0, size).replace('_', '-'); |
| 399 | final LocaleList localeList = LocaleList.forLanguageTags(localeCodes); |
| 400 | if (localeList.isEmpty()) { |
Christopher Tate | 45d1758 | 2017-06-26 17:35:26 -0700 | [diff] [blame] | 401 | return; |
| 402 | } |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 403 | |
Seigo Nonaka | 0f19cc7 | 2017-06-26 16:06:17 -0700 | [diff] [blame] | 404 | final String[] supportedLocales = LocalePicker.getSupportedLocales(mContext); |
| 405 | final LocaleList currentLocales = conf.getLocales(); |
| 406 | |
| 407 | final LocaleList merged = resolveLocales(localeList, currentLocales, supportedLocales); |
| 408 | if (merged.equals(currentLocales)) { |
| 409 | return; |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 410 | } |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 411 | |
| 412 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 413 | IActivityManager am = ActivityManager.getService(); |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 414 | Configuration config = am.getConfiguration(); |
Seigo Nonaka | 0f19cc7 | 2017-06-26 16:06:17 -0700 | [diff] [blame] | 415 | config.setLocales(merged); |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 416 | // indicate this isn't some passing default - the user wants this remembered |
| 417 | config.userSetLocale = true; |
| 418 | |
Seigo Nonaka | 0f19cc7 | 2017-06-26 16:06:17 -0700 | [diff] [blame] | 419 | am.updatePersistentConfiguration(config); |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 420 | } catch (RemoteException e) { |
| 421 | // Intentionally left blank |
| 422 | } |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 423 | } |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 424 | |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 425 | /** |
| 426 | * Informs the audio service of changes to the settings so that |
| 427 | * they can be re-read and applied. |
| 428 | */ |
| 429 | void applyAudioSettings() { |
| 430 | AudioManager am = new AudioManager(mContext); |
| 431 | am.reloadAudioSettings(); |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 432 | } |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 433 | } |