The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Matt Pape | 25c940d | 2019-03-26 12:14:33 -0700 | [diff] [blame] | 19 | import static android.os.Process.INVALID_UID; |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 20 | import static android.os.Process.ROOT_UID; |
| 21 | import static android.os.Process.SHELL_UID; |
| 22 | import static android.os.Process.SYSTEM_UID; |
Winson Chung | d9f2fb3 | 2019-03-05 11:10:12 -0800 | [diff] [blame] | 23 | import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_2BUTTON_OVERLAY; |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 24 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 25 | import android.Manifest; |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame] | 26 | import android.annotation.NonNull; |
Kweku Adams | b0886f3 | 2017-10-31 15:32:09 -0700 | [diff] [blame] | 27 | import android.annotation.Nullable; |
Christopher Tate | d5fe147 | 2012-09-10 15:48:38 -0700 | [diff] [blame] | 28 | import android.app.ActivityManager; |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 29 | import android.app.AppGlobals; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 30 | import android.app.backup.BackupManager; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 31 | import android.content.BroadcastReceiver; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 32 | import android.content.ComponentName; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | import android.content.ContentProvider; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | import android.content.ContentValues; |
| 35 | import android.content.Context; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 36 | import android.content.Intent; |
| 37 | import android.content.IntentFilter; |
Winson Chung | d9f2fb3 | 2019-03-05 11:10:12 -0800 | [diff] [blame] | 38 | import android.content.om.IOverlayManager; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 39 | import android.content.pm.ApplicationInfo; |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 40 | import android.content.pm.IPackageManager; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 41 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 42 | import android.content.pm.PackageManager; |
Christopher Tate | 38e7a60 | 2013-09-03 16:57:34 -0700 | [diff] [blame] | 43 | import android.content.pm.UserInfo; |
Chad Brubaker | 20e0dc3 | 2017-04-28 18:24:55 -0700 | [diff] [blame] | 44 | import android.content.res.Resources; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 45 | import android.database.Cursor; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 46 | import android.database.MatrixCursor; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 47 | import android.database.sqlite.SQLiteDatabase; |
| 48 | import android.database.sqlite.SQLiteQueryBuilder; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 49 | import android.hardware.camera2.utils.ArrayUtils; |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 50 | import android.media.AudioManager; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 51 | import android.net.Uri; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 52 | import android.os.Binder; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 53 | import android.os.Build; |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 54 | import android.os.Bundle; |
Amith Yamasani | 5cdf7f5 | 2013-06-27 15:12:01 -0700 | [diff] [blame] | 55 | import android.os.DropBoxManager; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 56 | import android.os.Environment; |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 57 | import android.os.Handler; |
Svet Ganov | a8f9026 | 2016-05-10 08:44:48 -0700 | [diff] [blame] | 58 | import android.os.HandlerThread; |
Christopher Tate | 65fb2e4 | 2019-10-11 17:00:23 -0700 | [diff] [blame] | 59 | import android.os.IUserRestrictionsListener; |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 60 | import android.os.Looper; |
| 61 | import android.os.Message; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 62 | import android.os.ParcelFileDescriptor; |
Christopher Tate | 0da1357 | 2013-10-13 17:34:49 -0700 | [diff] [blame] | 63 | import android.os.Process; |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 64 | import android.os.RemoteException; |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 65 | import android.os.SELinux; |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 66 | import android.os.ServiceManager; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 67 | import android.os.UserHandle; |
| 68 | import android.os.UserManager; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 69 | import android.provider.DeviceConfig; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 70 | import android.provider.Settings; |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 71 | import android.provider.Settings.Global; |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 72 | import android.provider.Settings.Secure; |
Al Sutton | 91f89d0 | 2019-08-16 12:56:57 +0100 | [diff] [blame] | 73 | import android.provider.settings.validators.SystemSettingsValidators; |
Al Sutton | b0067fb | 2019-08-16 10:34:46 +0100 | [diff] [blame] | 74 | import android.provider.settings.validators.Validator; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 75 | import android.text.TextUtils; |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 76 | import android.util.ArrayMap; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 77 | import android.util.ArraySet; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 78 | import android.util.Slog; |
| 79 | import android.util.SparseArray; |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 80 | import android.util.SparseBooleanArray; |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame] | 81 | import android.util.proto.ProtoOutputStream; |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 82 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 83 | import com.android.internal.annotations.GuardedBy; |
| 84 | import com.android.internal.content.PackageMonitor; |
| 85 | import com.android.internal.os.BackgroundThread; |
Suprabh Shukla | 269c11e | 2015-12-02 16:51:16 -0800 | [diff] [blame] | 86 | import com.android.providers.settings.SettingsState.Setting; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 87 | import com.android.server.SystemConfig; |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 88 | |
Irina Dumitrescu | e369687 | 2019-01-09 16:07:59 +0000 | [diff] [blame] | 89 | import com.google.android.collect.Sets; |
| 90 | |
Neil Fuller | 3447fb4 | 2019-04-08 22:03:14 +0100 | [diff] [blame] | 91 | import libcore.util.HexEncoding; |
| 92 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 93 | import java.io.File; |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 94 | import java.io.FileDescriptor; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 95 | import java.io.FileNotFoundException; |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 96 | import java.io.PrintWriter; |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 97 | import java.nio.ByteBuffer; |
| 98 | import java.security.InvalidKeyException; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 99 | import java.security.NoSuchAlgorithmException; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 100 | import java.security.SecureRandom; |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 101 | import java.util.ArrayList; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 102 | import java.util.Arrays; |
Robin Lee | 7af9a74 | 2017-02-20 14:47:30 +0000 | [diff] [blame] | 103 | import java.util.Collection; |
Matt Pape | 4bd0a6a | 2019-11-19 12:17:45 -0800 | [diff] [blame^] | 104 | import java.util.Collections; |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 105 | import java.util.HashMap; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 106 | import java.util.HashSet; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 107 | import java.util.List; |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 108 | import java.util.Map; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 109 | import java.util.Set; |
| 110 | import java.util.regex.Pattern; |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 111 | |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 112 | import javax.crypto.Mac; |
| 113 | import javax.crypto.spec.SecretKeySpec; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 114 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 115 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 116 | /** |
| 117 | * <p> |
| 118 | * This class is a content provider that publishes the system settings. |
| 119 | * It can be accessed via the content provider APIs or via custom call |
| 120 | * commands. The latter is a bit faster and is the preferred way to access |
| 121 | * the platform settings. |
| 122 | * </p> |
| 123 | * <p> |
| 124 | * There are three settings types, global (with signature level protection |
| 125 | * and shared across users), secure (with signature permission level |
| 126 | * protection and per user), and system (with dangerous permission level |
| 127 | * protection and per user). Global settings are stored under the device owner. |
| 128 | * Each of these settings is represented by a {@link |
| 129 | * com.android.providers.settings.SettingsState} object mapped to an integer |
| 130 | * key derived from the setting type in the most significant bits and user |
| 131 | * id in the least significant bits. Settings are synchronously loaded on |
| 132 | * instantiation of a SettingsState and asynchronously persisted on mutation. |
| 133 | * Settings are stored in the user specific system directory. |
| 134 | * </p> |
| 135 | * <p> |
| 136 | * Apps targeting APIs Lollipop MR1 and lower can add custom settings entries |
| 137 | * and get a warning. Targeting higher API version prohibits this as the |
| 138 | * system settings are not a place for apps to save their state. When a package |
| 139 | * is removed the settings it added are deleted. Apps cannot delete system |
| 140 | * settings added by the platform. System settings values are validated to |
| 141 | * ensure the clients do not put bad values. Global and secure settings are |
| 142 | * changed only by trusted parties, therefore no validation is performed. Also |
| 143 | * there is a limit on the amount of app specific settings that can be added |
| 144 | * to prevent unlimited growth of the system process memory footprint. |
| 145 | * </p> |
| 146 | */ |
| 147 | @SuppressWarnings("deprecation") |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 148 | public class SettingsProvider extends ContentProvider { |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 149 | static final boolean DEBUG = false; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 150 | |
Svetoslav Ganov | 264c7a9 | 2016-08-24 17:31:14 -0700 | [diff] [blame] | 151 | private static final boolean DROP_DATABASE_ON_MIGRATION = true; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 152 | |
| 153 | private static final String LOG_TAG = "SettingsProvider"; |
Christopher Tate | 0da1357 | 2013-10-13 17:34:49 -0700 | [diff] [blame] | 154 | |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 155 | private static final String TABLE_SYSTEM = "system"; |
| 156 | private static final String TABLE_SECURE = "secure"; |
| 157 | private static final String TABLE_GLOBAL = "global"; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 158 | |
| 159 | // Old tables no longer exist. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | private static final String TABLE_FAVORITES = "favorites"; |
| 161 | private static final String TABLE_OLD_FAVORITES = "old_favorites"; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 162 | private static final String TABLE_BLUETOOTH_DEVICES = "bluetooth_devices"; |
| 163 | private static final String TABLE_BOOKMARKS = "bookmarks"; |
| 164 | private static final String TABLE_ANDROID_METADATA = "android_metadata"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 166 | // The set of removed legacy tables. |
| 167 | private static final Set<String> REMOVED_LEGACY_TABLES = new ArraySet<>(); |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 168 | static { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 169 | REMOVED_LEGACY_TABLES.add(TABLE_FAVORITES); |
| 170 | REMOVED_LEGACY_TABLES.add(TABLE_OLD_FAVORITES); |
| 171 | REMOVED_LEGACY_TABLES.add(TABLE_BLUETOOTH_DEVICES); |
| 172 | REMOVED_LEGACY_TABLES.add(TABLE_BOOKMARKS); |
| 173 | REMOVED_LEGACY_TABLES.add(TABLE_ANDROID_METADATA); |
| 174 | } |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 175 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 176 | private static final int MUTATION_OPERATION_INSERT = 1; |
| 177 | private static final int MUTATION_OPERATION_DELETE = 2; |
| 178 | private static final int MUTATION_OPERATION_UPDATE = 3; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 179 | private static final int MUTATION_OPERATION_RESET = 4; |
Julia Reynolds | 5e458dd | 2014-07-07 16:07:01 -0400 | [diff] [blame] | 180 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 181 | private static final String[] ALL_COLUMNS = new String[] { |
| 182 | Settings.NameValueTable._ID, |
| 183 | Settings.NameValueTable.NAME, |
| 184 | Settings.NameValueTable.VALUE |
| 185 | }; |
| 186 | |
Makoto Onuki | 53f0e02 | 2017-11-29 13:51:01 -0800 | [diff] [blame] | 187 | public static final int SETTINGS_TYPE_GLOBAL = SettingsState.SETTINGS_TYPE_GLOBAL; |
| 188 | public static final int SETTINGS_TYPE_SYSTEM = SettingsState.SETTINGS_TYPE_SYSTEM; |
| 189 | public static final int SETTINGS_TYPE_SECURE = SettingsState.SETTINGS_TYPE_SECURE; |
| 190 | public static final int SETTINGS_TYPE_SSAID = SettingsState.SETTINGS_TYPE_SSAID; |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 191 | public static final int SETTINGS_TYPE_CONFIG = SettingsState.SETTINGS_TYPE_CONFIG; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 192 | |
| 193 | private static final Bundle NULL_SETTING_BUNDLE = Bundle.forPair( |
| 194 | Settings.NameValueTable.VALUE, null); |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 195 | |
Nicholas Sauer | 3d87d1e | 2018-11-06 08:38:39 -0800 | [diff] [blame] | 196 | public static final String RESULT_ROWS_DELETED = "result_rows_deleted"; |
Nicholas Sauer | 7250053 | 2018-11-21 10:30:58 -0800 | [diff] [blame] | 197 | public static final String RESULT_SETTINGS_LIST = "result_settings_list"; |
Nicholas Sauer | 3d87d1e | 2018-11-06 08:38:39 -0800 | [diff] [blame] | 198 | |
Chad Brubaker | 20e0dc3 | 2017-04-28 18:24:55 -0700 | [diff] [blame] | 199 | // Overlay specified settings whitelisted for Instant Apps |
| 200 | private static final Set<String> OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS = new ArraySet<>(); |
| 201 | private static final Set<String> OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS = new ArraySet<>(); |
| 202 | private static final Set<String> OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS = new ArraySet<>(); |
| 203 | |
| 204 | static { |
| 205 | for (String name : Resources.getSystem().getStringArray( |
| 206 | com.android.internal.R.array.config_allowedGlobalInstantAppSettings)) { |
| 207 | OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS.add(name); |
| 208 | } |
| 209 | for (String name : Resources.getSystem().getStringArray( |
| 210 | com.android.internal.R.array.config_allowedSystemInstantAppSettings)) { |
| 211 | OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS.add(name); |
| 212 | } |
| 213 | for (String name : Resources.getSystem().getStringArray( |
| 214 | com.android.internal.R.array.config_allowedSecureInstantAppSettings)) { |
| 215 | OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS.add(name); |
| 216 | } |
| 217 | } |
| 218 | |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 219 | // Changes to these global settings are synchronously persisted |
| 220 | private static final Set<String> CRITICAL_GLOBAL_SETTINGS = new ArraySet<>(); |
| 221 | static { |
| 222 | CRITICAL_GLOBAL_SETTINGS.add(Settings.Global.DEVICE_PROVISIONED); |
| 223 | } |
| 224 | |
| 225 | // Changes to these secure settings are synchronously persisted |
| 226 | private static final Set<String> CRITICAL_SECURE_SETTINGS = new ArraySet<>(); |
| 227 | static { |
| 228 | CRITICAL_SECURE_SETTINGS.add(Settings.Secure.USER_SETUP_COMPLETE); |
| 229 | } |
| 230 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 231 | // Per user secure settings that moved to the for all users global settings. |
| 232 | static final Set<String> sSecureMovedToGlobalSettings = new ArraySet<>(); |
| 233 | static { |
| 234 | Settings.Secure.getMovedToGlobalSettings(sSecureMovedToGlobalSettings); |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 237 | // Per user system settings that moved to the for all users global settings. |
| 238 | static final Set<String> sSystemMovedToGlobalSettings = new ArraySet<>(); |
| 239 | static { |
| 240 | Settings.System.getMovedToGlobalSettings(sSystemMovedToGlobalSettings); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 243 | // Per user system settings that moved to the per user secure settings. |
| 244 | static final Set<String> sSystemMovedToSecureSettings = new ArraySet<>(); |
| 245 | static { |
| 246 | Settings.System.getMovedToSecureSettings(sSystemMovedToSecureSettings); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 249 | // Per all users global settings that moved to the per user secure settings. |
| 250 | static final Set<String> sGlobalMovedToSecureSettings = new ArraySet<>(); |
| 251 | static { |
| 252 | Settings.Global.getMovedToSecureSettings(sGlobalMovedToSecureSettings); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 255 | // Per user secure settings that are cloned for the managed profiles of the user. |
| 256 | private static final Set<String> sSecureCloneToManagedSettings = new ArraySet<>(); |
| 257 | static { |
| 258 | Settings.Secure.getCloneToManagedProfileSettings(sSecureCloneToManagedSettings); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 261 | // Per user system settings that are cloned for the managed profiles of the user. |
| 262 | private static final Set<String> sSystemCloneToManagedSettings = new ArraySet<>(); |
| 263 | static { |
| 264 | Settings.System.getCloneToManagedProfileSettings(sSystemCloneToManagedSettings); |
Julia Reynolds | 5e458dd | 2014-07-07 16:07:01 -0400 | [diff] [blame] | 265 | } |
| 266 | |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 267 | // Per user system settings that are cloned from the profile's parent when a dependency |
| 268 | // in {@link Settings.Secure} is set to "1". |
| 269 | public static final Map<String, String> sSystemCloneFromParentOnDependency = new ArrayMap<>(); |
| 270 | static { |
| 271 | Settings.System.getCloneFromParentOnValueSettings(sSystemCloneFromParentOnDependency); |
| 272 | } |
| 273 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 274 | private final Object mLock = new Object(); |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 275 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 276 | @GuardedBy("mLock") |
| 277 | private SettingsRegistry mSettingsRegistry; |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 278 | |
Svet Ganov | a8f9026 | 2016-05-10 08:44:48 -0700 | [diff] [blame] | 279 | @GuardedBy("mLock") |
| 280 | private HandlerThread mHandlerThread; |
| 281 | |
Makoto Onuki | 73360ab | 2017-03-17 11:50:13 -0700 | [diff] [blame] | 282 | @GuardedBy("mLock") |
| 283 | private Handler mHandler; |
| 284 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 285 | // We have to call in the user manager with no lock held, |
| 286 | private volatile UserManager mUserManager; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 287 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 288 | // We have to call in the package manager with no lock held, |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 289 | private volatile IPackageManager mPackageManager; |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 290 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 291 | public static int makeKey(int type, int userId) { |
Makoto Onuki | 53f0e02 | 2017-11-29 13:51:01 -0800 | [diff] [blame] | 292 | return SettingsState.makeKey(type, userId); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | public static int getTypeFromKey(int key) { |
Makoto Onuki | 53f0e02 | 2017-11-29 13:51:01 -0800 | [diff] [blame] | 296 | return SettingsState.getTypeFromKey(key); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | public static int getUserIdFromKey(int key) { |
Makoto Onuki | 53f0e02 | 2017-11-29 13:51:01 -0800 | [diff] [blame] | 300 | return SettingsState.getUserIdFromKey(key); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | public static String settingTypeToString(int type) { |
Makoto Onuki | 53f0e02 | 2017-11-29 13:51:01 -0800 | [diff] [blame] | 304 | return SettingsState.settingTypeToString(type); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | public static String keyToString(int key) { |
Makoto Onuki | 53f0e02 | 2017-11-29 13:51:01 -0800 | [diff] [blame] | 308 | return SettingsState.keyToString(key); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 309 | } |
| 310 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 311 | @Override |
| 312 | public boolean onCreate() { |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 313 | Settings.setInSystemServer(); |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 314 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 315 | synchronized (mLock) { |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 316 | mUserManager = UserManager.get(getContext()); |
| 317 | mPackageManager = AppGlobals.getPackageManager(); |
Svet Ganov | a8f9026 | 2016-05-10 08:44:48 -0700 | [diff] [blame] | 318 | mHandlerThread = new HandlerThread(LOG_TAG, |
| 319 | Process.THREAD_PRIORITY_BACKGROUND); |
| 320 | mHandlerThread.start(); |
Makoto Onuki | 73360ab | 2017-03-17 11:50:13 -0700 | [diff] [blame] | 321 | mHandler = new Handler(mHandlerThread.getLooper()); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 322 | mSettingsRegistry = new SettingsRegistry(); |
| 323 | } |
Makoto Onuki | 73360ab | 2017-03-17 11:50:13 -0700 | [diff] [blame] | 324 | mHandler.post(() -> { |
| 325 | registerBroadcastReceivers(); |
| 326 | startWatchingUserRestrictionChanges(); |
| 327 | }); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 328 | ServiceManager.addService("settings", new SettingsService(this)); |
Matt Pape | abb6789 | 2018-12-07 09:13:26 -0800 | [diff] [blame] | 329 | ServiceManager.addService("device_config", new DeviceConfigService(this)); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 330 | return true; |
| 331 | } |
| 332 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 333 | @Override |
| 334 | public Bundle call(String method, String name, Bundle args) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 335 | final int requestingUserId = getRequestingUserId(args); |
| 336 | switch (method) { |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 337 | case Settings.CALL_METHOD_GET_CONFIG: { |
| 338 | Setting setting = getConfigSetting(name); |
| 339 | return packageValueForCallResult(setting, isTrackingGeneration(args)); |
| 340 | } |
| 341 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 342 | case Settings.CALL_METHOD_GET_GLOBAL: { |
| 343 | Setting setting = getGlobalSetting(name); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 344 | return packageValueForCallResult(setting, isTrackingGeneration(args)); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 345 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 346 | |
| 347 | case Settings.CALL_METHOD_GET_SECURE: { |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 348 | Setting setting = getSecureSetting(name, requestingUserId, |
| 349 | /*enableOverride=*/ true); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 350 | return packageValueForCallResult(setting, isTrackingGeneration(args)); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | case Settings.CALL_METHOD_GET_SYSTEM: { |
| 354 | Setting setting = getSystemSetting(name, requestingUserId); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 355 | return packageValueForCallResult(setting, isTrackingGeneration(args)); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 358 | case Settings.CALL_METHOD_PUT_CONFIG: { |
| 359 | String value = getSettingValue(args); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 360 | final boolean makeDefault = getSettingMakeDefault(args); |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 361 | insertConfigSetting(name, value, makeDefault); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 362 | break; |
| 363 | } |
| 364 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 365 | case Settings.CALL_METHOD_PUT_GLOBAL: { |
| 366 | String value = getSettingValue(args); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 367 | String tag = getSettingTag(args); |
| 368 | final boolean makeDefault = getSettingMakeDefault(args); |
| 369 | insertGlobalSetting(name, value, tag, makeDefault, requestingUserId, false); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 370 | break; |
| 371 | } |
| 372 | |
| 373 | case Settings.CALL_METHOD_PUT_SECURE: { |
| 374 | String value = getSettingValue(args); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 375 | String tag = getSettingTag(args); |
| 376 | final boolean makeDefault = getSettingMakeDefault(args); |
| 377 | insertSecureSetting(name, value, tag, makeDefault, requestingUserId, false); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 378 | break; |
| 379 | } |
| 380 | |
| 381 | case Settings.CALL_METHOD_PUT_SYSTEM: { |
| 382 | String value = getSettingValue(args); |
| 383 | insertSystemSetting(name, value, requestingUserId); |
| 384 | break; |
| 385 | } |
| 386 | |
Matt Pape | 4bd0a6a | 2019-11-19 12:17:45 -0800 | [diff] [blame^] | 387 | case Settings.CALL_METHOD_SET_ALL_CONFIG: { |
| 388 | String prefix = getSettingPrefix(args); |
| 389 | Map<String, String> flags = getSettingFlags(args); |
| 390 | setAllConfigSettings(prefix, flags); |
| 391 | break; |
| 392 | } |
| 393 | |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 394 | case Settings.CALL_METHOD_RESET_CONFIG: { |
| 395 | final int mode = getResetModeEnforcingPermission(args); |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 396 | String prefix = getSettingPrefix(args); |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 397 | resetConfigSetting(mode, prefix); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 398 | break; |
| 399 | } |
| 400 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 401 | case Settings.CALL_METHOD_RESET_GLOBAL: { |
| 402 | final int mode = getResetModeEnforcingPermission(args); |
| 403 | String tag = getSettingTag(args); |
| 404 | resetGlobalSetting(requestingUserId, mode, tag); |
| 405 | break; |
| 406 | } |
| 407 | |
| 408 | case Settings.CALL_METHOD_RESET_SECURE: { |
| 409 | final int mode = getResetModeEnforcingPermission(args); |
| 410 | String tag = getSettingTag(args); |
| 411 | resetSecureSetting(requestingUserId, mode, tag); |
| 412 | break; |
| 413 | } |
| 414 | |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 415 | case Settings.CALL_METHOD_DELETE_CONFIG: { |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 416 | int rows = deleteConfigSetting(name) ? 1 : 0; |
Nicholas Sauer | 3d87d1e | 2018-11-06 08:38:39 -0800 | [diff] [blame] | 417 | Bundle result = new Bundle(); |
| 418 | result.putInt(RESULT_ROWS_DELETED, rows); |
| 419 | return result; |
| 420 | } |
| 421 | |
| 422 | case Settings.CALL_METHOD_DELETE_GLOBAL: { |
| 423 | int rows = deleteGlobalSetting(name, requestingUserId, false) ? 1 : 0; |
| 424 | Bundle result = new Bundle(); |
| 425 | result.putInt(RESULT_ROWS_DELETED, rows); |
| 426 | return result; |
| 427 | } |
| 428 | |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 429 | case Settings.CALL_METHOD_DELETE_SECURE: { |
| 430 | int rows = deleteSecureSetting(name, requestingUserId, false) ? 1 : 0; |
| 431 | Bundle result = new Bundle(); |
| 432 | result.putInt(RESULT_ROWS_DELETED, rows); |
| 433 | return result; |
| 434 | } |
| 435 | |
| 436 | case Settings.CALL_METHOD_DELETE_SYSTEM: { |
| 437 | int rows = deleteSystemSetting(name, requestingUserId) ? 1 : 0; |
| 438 | Bundle result = new Bundle(); |
| 439 | result.putInt(RESULT_ROWS_DELETED, rows); |
| 440 | return result; |
| 441 | } |
| 442 | |
| 443 | case Settings.CALL_METHOD_LIST_CONFIG: { |
| 444 | String prefix = getSettingPrefix(args); |
Matt Pape | 793b15c | 2019-10-07 13:17:20 -0700 | [diff] [blame] | 445 | return packageValuesForCallResult(getAllConfigFlags(prefix), |
| 446 | isTrackingGeneration(args)); |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | case Settings.CALL_METHOD_LIST_GLOBAL: { |
Nicholas Sauer | 7250053 | 2018-11-21 10:30:58 -0800 | [diff] [blame] | 450 | Bundle result = new Bundle(); |
| 451 | result.putStringArrayList(RESULT_SETTINGS_LIST, |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 452 | buildSettingsList(getAllGlobalSettings(null))); |
Nicholas Sauer | 7250053 | 2018-11-21 10:30:58 -0800 | [diff] [blame] | 453 | return result; |
| 454 | } |
| 455 | |
| 456 | case Settings.CALL_METHOD_LIST_SECURE: { |
| 457 | Bundle result = new Bundle(); |
| 458 | result.putStringArrayList(RESULT_SETTINGS_LIST, |
| 459 | buildSettingsList(getAllSecureSettings(requestingUserId, null))); |
| 460 | return result; |
| 461 | } |
| 462 | |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 463 | case Settings.CALL_METHOD_LIST_SYSTEM: { |
Nicholas Sauer | 7250053 | 2018-11-21 10:30:58 -0800 | [diff] [blame] | 464 | Bundle result = new Bundle(); |
| 465 | result.putStringArrayList(RESULT_SETTINGS_LIST, |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 466 | buildSettingsList(getAllSystemSettings(requestingUserId, null))); |
Nicholas Sauer | 7250053 | 2018-11-21 10:30:58 -0800 | [diff] [blame] | 467 | return result; |
| 468 | } |
| 469 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 470 | default: { |
| 471 | Slog.w(LOG_TAG, "call() with invalid method: " + method); |
| 472 | } break; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 473 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 474 | |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 475 | return null; |
| 476 | } |
| 477 | |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 478 | @Override |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 479 | public String getType(Uri uri) { |
| 480 | Arguments args = new Arguments(uri, null, null, true); |
| 481 | if (TextUtils.isEmpty(args.name)) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 482 | return "vnd.android.cursor.dir/" + args.table; |
| 483 | } else { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 484 | return "vnd.android.cursor.item/" + args.table; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
| 488 | @Override |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 489 | public Cursor query(Uri uri, String[] projection, String where, String[] whereArgs, |
| 490 | String order) { |
| 491 | if (DEBUG) { |
| 492 | Slog.v(LOG_TAG, "query() for user: " + UserHandle.getCallingUserId()); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 495 | Arguments args = new Arguments(uri, where, whereArgs, true); |
| 496 | String[] normalizedProjection = normalizeProjection(projection); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 497 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 498 | // If a legacy table that is gone, done. |
| 499 | if (REMOVED_LEGACY_TABLES.contains(args.table)) { |
| 500 | return new MatrixCursor(normalizedProjection, 0); |
| 501 | } |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 502 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 503 | switch (args.table) { |
| 504 | case TABLE_GLOBAL: { |
| 505 | if (args.name != null) { |
| 506 | Setting setting = getGlobalSetting(args.name); |
| 507 | return packageSettingForQuery(setting, normalizedProjection); |
| 508 | } else { |
| 509 | return getAllGlobalSettings(projection); |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 510 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 511 | } |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 512 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 513 | case TABLE_SECURE: { |
| 514 | final int userId = UserHandle.getCallingUserId(); |
| 515 | if (args.name != null) { |
| 516 | Setting setting = getSecureSetting(args.name, userId); |
| 517 | return packageSettingForQuery(setting, normalizedProjection); |
| 518 | } else { |
| 519 | return getAllSecureSettings(userId, projection); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 520 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 521 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 522 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 523 | case TABLE_SYSTEM: { |
| 524 | final int userId = UserHandle.getCallingUserId(); |
| 525 | if (args.name != null) { |
| 526 | Setting setting = getSystemSetting(args.name, userId); |
| 527 | return packageSettingForQuery(setting, normalizedProjection); |
| 528 | } else { |
| 529 | return getAllSystemSettings(userId, projection); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 530 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 531 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 532 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 533 | default: { |
| 534 | throw new IllegalArgumentException("Invalid Uri path:" + uri); |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Nicholas Sauer | 7250053 | 2018-11-21 10:30:58 -0800 | [diff] [blame] | 539 | private ArrayList<String> buildSettingsList(Cursor cursor) { |
| 540 | final ArrayList<String> lines = new ArrayList<String>(); |
| 541 | try { |
| 542 | while (cursor != null && cursor.moveToNext()) { |
| 543 | lines.add(cursor.getString(1) + "=" + cursor.getString(2)); |
| 544 | } |
| 545 | } finally { |
| 546 | if (cursor != null) { |
| 547 | cursor.close(); |
| 548 | } |
| 549 | } |
| 550 | return lines; |
| 551 | } |
| 552 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 553 | @Override |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 554 | public Uri insert(Uri uri, ContentValues values) { |
| 555 | if (DEBUG) { |
| 556 | Slog.v(LOG_TAG, "insert() for user: " + UserHandle.getCallingUserId()); |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 559 | String table = getValidTableOrThrow(uri); |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 560 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 561 | // If a legacy table that is gone, done. |
| 562 | if (REMOVED_LEGACY_TABLES.contains(table)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | return null; |
| 564 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 565 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 566 | String name = values.getAsString(Settings.Secure.NAME); |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 567 | if (!isKeyValid(name)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 568 | return null; |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 571 | String value = values.getAsString(Settings.Secure.VALUE); |
| 572 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 573 | switch (table) { |
| 574 | case TABLE_GLOBAL: { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 575 | if (insertGlobalSetting(name, value, null, false, |
| 576 | UserHandle.getCallingUserId(), false)) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 577 | return Uri.withAppendedPath(Settings.Global.CONTENT_URI, name); |
Christopher Tate | c221d2b | 2012-10-03 18:33:52 -0700 | [diff] [blame] | 578 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 579 | } break; |
| 580 | |
| 581 | case TABLE_SECURE: { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 582 | if (insertSecureSetting(name, value, null, false, |
| 583 | UserHandle.getCallingUserId(), false)) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 584 | return Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name); |
| 585 | } |
| 586 | } break; |
| 587 | |
| 588 | case TABLE_SYSTEM: { |
| 589 | if (insertSystemSetting(name, value, UserHandle.getCallingUserId())) { |
| 590 | return Uri.withAppendedPath(Settings.System.CONTENT_URI, name); |
| 591 | } |
| 592 | } break; |
| 593 | |
| 594 | default: { |
| 595 | throw new IllegalArgumentException("Bad Uri path:" + uri); |
Christopher Tate | c221d2b | 2012-10-03 18:33:52 -0700 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 599 | return null; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | @Override |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 603 | public int bulkInsert(Uri uri, ContentValues[] allValues) { |
| 604 | if (DEBUG) { |
| 605 | Slog.v(LOG_TAG, "bulkInsert() for user: " + UserHandle.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 607 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 608 | int insertionCount = 0; |
| 609 | final int valuesCount = allValues.length; |
| 610 | for (int i = 0; i < valuesCount; i++) { |
| 611 | ContentValues values = allValues[i]; |
| 612 | if (insert(uri, values) != null) { |
| 613 | insertionCount++; |
| 614 | } |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 615 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 616 | |
| 617 | return insertionCount; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | @Override |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 621 | public int delete(Uri uri, String where, String[] whereArgs) { |
| 622 | if (DEBUG) { |
| 623 | Slog.v(LOG_TAG, "delete() for user: " + UserHandle.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 625 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 626 | Arguments args = new Arguments(uri, where, whereArgs, false); |
| 627 | |
| 628 | // If a legacy table that is gone, done. |
| 629 | if (REMOVED_LEGACY_TABLES.contains(args.table)) { |
| 630 | return 0; |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 631 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 632 | |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 633 | if (!isKeyValid(args.name)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 634 | return 0; |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 635 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 636 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 637 | switch (args.table) { |
| 638 | case TABLE_GLOBAL: { |
| 639 | final int userId = UserHandle.getCallingUserId(); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 640 | return deleteGlobalSetting(args.name, userId, false) ? 1 : 0; |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 641 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 642 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 643 | case TABLE_SECURE: { |
| 644 | final int userId = UserHandle.getCallingUserId(); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 645 | return deleteSecureSetting(args.name, userId, false) ? 1 : 0; |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 646 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 647 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 648 | case TABLE_SYSTEM: { |
| 649 | final int userId = UserHandle.getCallingUserId(); |
| 650 | return deleteSystemSetting(args.name, userId) ? 1 : 0; |
| 651 | } |
| 652 | |
| 653 | default: { |
| 654 | throw new IllegalArgumentException("Bad Uri path:" + uri); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 655 | } |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 656 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | @Override |
| 660 | public int update(Uri uri, ContentValues values, String where, String[] whereArgs) { |
| 661 | if (DEBUG) { |
| 662 | Slog.v(LOG_TAG, "update() for user: " + UserHandle.getCallingUserId()); |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 663 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 664 | |
| 665 | Arguments args = new Arguments(uri, where, whereArgs, false); |
| 666 | |
| 667 | // If a legacy table that is gone, done. |
| 668 | if (REMOVED_LEGACY_TABLES.contains(args.table)) { |
| 669 | return 0; |
| 670 | } |
| 671 | |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 672 | String name = values.getAsString(Settings.Secure.NAME); |
| 673 | if (!isKeyValid(name)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 674 | return 0; |
| 675 | } |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 676 | String value = values.getAsString(Settings.Secure.VALUE); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 677 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 678 | switch (args.table) { |
| 679 | case TABLE_GLOBAL: { |
| 680 | final int userId = UserHandle.getCallingUserId(); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 681 | return updateGlobalSetting(args.name, value, null, false, |
| 682 | userId, false) ? 1 : 0; |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 683 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 684 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 685 | case TABLE_SECURE: { |
| 686 | final int userId = UserHandle.getCallingUserId(); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 687 | return updateSecureSetting(args.name, value, null, false, |
| 688 | userId, false) ? 1 : 0; |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 689 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 690 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 691 | case TABLE_SYSTEM: { |
| 692 | final int userId = UserHandle.getCallingUserId(); |
| 693 | return updateSystemSetting(args.name, value, userId) ? 1 : 0; |
| 694 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 695 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 696 | default: { |
| 697 | throw new IllegalArgumentException("Invalid Uri path:" + uri); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 698 | } |
| 699 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | @Override |
| 703 | public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 704 | final int userId = getUserIdFromUri(uri, UserHandle.getCallingUserId()); |
| 705 | if (userId != UserHandle.getCallingUserId()) { |
| 706 | getContext().enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS, |
| 707 | "Access files from the settings of another user"); |
| 708 | } |
| 709 | uri = ContentProvider.getUriWithoutUserId(uri); |
| 710 | |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 711 | final String cacheRingtoneSetting; |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 712 | final String cacheName; |
| 713 | if (Settings.System.RINGTONE_CACHE_URI.equals(uri)) { |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 714 | cacheRingtoneSetting = Settings.System.RINGTONE; |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 715 | cacheName = Settings.System.RINGTONE_CACHE; |
| 716 | } else if (Settings.System.NOTIFICATION_SOUND_CACHE_URI.equals(uri)) { |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 717 | cacheRingtoneSetting = Settings.System.NOTIFICATION_SOUND; |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 718 | cacheName = Settings.System.NOTIFICATION_SOUND_CACHE; |
| 719 | } else if (Settings.System.ALARM_ALERT_CACHE_URI.equals(uri)) { |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 720 | cacheRingtoneSetting = Settings.System.ALARM_ALERT; |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 721 | cacheName = Settings.System.ALARM_ALERT_CACHE; |
| 722 | } else { |
| 723 | throw new FileNotFoundException("Direct file access no longer supported; " |
| 724 | + "ringtone playback is available through android.media.Ringtone"); |
| 725 | } |
| 726 | |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 727 | int actualCacheOwner; |
| 728 | // Redirect cache to parent if ringtone setting is owned by profile parent |
| 729 | synchronized (mLock) { |
| 730 | actualCacheOwner = resolveOwningUserIdForSystemSettingLocked(userId, |
| 731 | cacheRingtoneSetting); |
| 732 | } |
| 733 | final File cacheFile = new File(getRingtoneCacheDir(actualCacheOwner), cacheName); |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 734 | return ParcelFileDescriptor.open(cacheFile, ParcelFileDescriptor.parseMode(mode)); |
| 735 | } |
| 736 | |
| 737 | private File getRingtoneCacheDir(int userId) { |
| 738 | final File cacheDir = new File(Environment.getDataSystemDeDirectory(userId), "ringtones"); |
| 739 | cacheDir.mkdir(); |
| 740 | SELinux.restorecon(cacheDir); |
| 741 | return cacheDir; |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 742 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 743 | |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame] | 744 | /** |
| 745 | * Dump all settings as a proto buf. |
| 746 | * |
| 747 | * @param fd The file to dump to |
| 748 | */ |
| 749 | void dumpProto(@NonNull FileDescriptor fd) { |
| 750 | ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 751 | |
| 752 | synchronized (mLock) { |
| 753 | SettingsProtoDumpUtil.dumpProtoLocked(mSettingsRegistry, proto); |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | proto.flush(); |
| 757 | } |
| 758 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 759 | public void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) { |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 760 | synchronized (mLock) { |
| 761 | final long identity = Binder.clearCallingIdentity(); |
| 762 | try { |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 763 | SparseBooleanArray users = mSettingsRegistry.getKnownUsersLocked(); |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 764 | final int userCount = users.size(); |
| 765 | for (int i = 0; i < userCount; i++) { |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 766 | dumpForUserLocked(users.keyAt(i), pw); |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 767 | } |
| 768 | } finally { |
| 769 | Binder.restoreCallingIdentity(identity); |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
Andreas Gampe | b5889307 | 2018-09-05 16:52:31 -0700 | [diff] [blame] | 774 | @GuardedBy("mLock") |
Svetoslav Ganov | a340bfd | 2016-08-02 18:24:49 -0700 | [diff] [blame] | 775 | private void dumpForUserLocked(int userId, PrintWriter pw) { |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 776 | if (userId == UserHandle.USER_SYSTEM) { |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 777 | pw.println("CONFIG SETTINGS (user " + userId + ")"); |
| 778 | SettingsState configSettings = mSettingsRegistry.getSettingsLocked( |
| 779 | SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM); |
| 780 | if (configSettings != null) { |
| 781 | dumpSettingsLocked(configSettings, pw); |
| 782 | pw.println(); |
| 783 | configSettings.dumpHistoricalOperations(pw); |
| 784 | } |
| 785 | |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 786 | pw.println("GLOBAL SETTINGS (user " + userId + ")"); |
Svetoslav Ganov | a340bfd | 2016-08-02 18:24:49 -0700 | [diff] [blame] | 787 | SettingsState globalSettings = mSettingsRegistry.getSettingsLocked( |
| 788 | SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 789 | if (globalSettings != null) { |
| 790 | dumpSettingsLocked(globalSettings, pw); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 791 | pw.println(); |
| 792 | globalSettings.dumpHistoricalOperations(pw); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 793 | } |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | pw.println("SECURE SETTINGS (user " + userId + ")"); |
Svetoslav Ganov | a340bfd | 2016-08-02 18:24:49 -0700 | [diff] [blame] | 797 | SettingsState secureSettings = mSettingsRegistry.getSettingsLocked( |
| 798 | SETTINGS_TYPE_SECURE, userId); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 799 | if (secureSettings != null) { |
| 800 | dumpSettingsLocked(secureSettings, pw); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 801 | pw.println(); |
| 802 | secureSettings.dumpHistoricalOperations(pw); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 803 | } |
Svetoslav Ganov | a340bfd | 2016-08-02 18:24:49 -0700 | [diff] [blame] | 804 | |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 805 | pw.println("SYSTEM SETTINGS (user " + userId + ")"); |
Svetoslav Ganov | a340bfd | 2016-08-02 18:24:49 -0700 | [diff] [blame] | 806 | SettingsState systemSettings = mSettingsRegistry.getSettingsLocked( |
| 807 | SETTINGS_TYPE_SYSTEM, userId); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 808 | if (systemSettings != null) { |
| 809 | dumpSettingsLocked(systemSettings, pw); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 810 | pw.println(); |
| 811 | systemSettings.dumpHistoricalOperations(pw); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 812 | } |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 813 | } |
| 814 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 815 | private void dumpSettingsLocked(SettingsState settingsState, PrintWriter pw) { |
| 816 | List<String> names = settingsState.getSettingNamesLocked(); |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 817 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 818 | final int nameCount = names.size(); |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 819 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 820 | for (int i = 0; i < nameCount; i++) { |
| 821 | String name = names.get(i); |
| 822 | Setting setting = settingsState.getSettingLocked(name); |
| 823 | pw.print("_id:"); pw.print(toDumpString(setting.getId())); |
| 824 | pw.print(" name:"); pw.print(toDumpString(name)); |
| 825 | if (setting.getPackageName() != null) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 826 | pw.print(" pkg:"); pw.print(setting.getPackageName()); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 827 | } |
| 828 | pw.print(" value:"); pw.print(toDumpString(setting.getValue())); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 829 | if (setting.getDefaultValue() != null) { |
| 830 | pw.print(" default:"); pw.print(setting.getDefaultValue()); |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame] | 831 | pw.print(" defaultSystemSet:"); pw.print(setting.isDefaultFromSystem()); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 832 | } |
| 833 | if (setting.getTag() != null) { |
| 834 | pw.print(" tag:"); pw.print(setting.getTag()); |
| 835 | } |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 836 | pw.println(); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 837 | } |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 838 | } |
| 839 | |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 840 | private static String toDumpString(String s) { |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 841 | if (s != null) { |
| 842 | return s; |
| 843 | } |
| 844 | return "{null}"; |
| 845 | } |
| 846 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 847 | private void registerBroadcastReceivers() { |
| 848 | IntentFilter userFilter = new IntentFilter(); |
| 849 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 850 | userFilter.addAction(Intent.ACTION_USER_STOPPED); |
| 851 | |
| 852 | getContext().registerReceiver(new BroadcastReceiver() { |
| 853 | @Override |
| 854 | public void onReceive(Context context, Intent intent) { |
| 855 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 856 | UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 857 | |
| 858 | switch (intent.getAction()) { |
| 859 | case Intent.ACTION_USER_REMOVED: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 860 | synchronized (mLock) { |
| 861 | mSettingsRegistry.removeUserStateLocked(userId, true); |
| 862 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 863 | } break; |
| 864 | |
| 865 | case Intent.ACTION_USER_STOPPED: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 866 | synchronized (mLock) { |
| 867 | mSettingsRegistry.removeUserStateLocked(userId, false); |
| 868 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 869 | } break; |
| 870 | } |
| 871 | } |
| 872 | }, userFilter); |
| 873 | |
| 874 | PackageMonitor monitor = new PackageMonitor() { |
| 875 | @Override |
| 876 | public void onPackageRemoved(String packageName, int uid) { |
| 877 | synchronized (mLock) { |
Zimuzo | c56192c | 2018-07-25 10:40:01 +0100 | [diff] [blame] | 878 | mSettingsRegistry.removeSettingsForPackageLocked(packageName, |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 879 | UserHandle.getUserId(uid)); |
| 880 | } |
| 881 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 882 | |
| 883 | @Override |
| 884 | public void onUidRemoved(int uid) { |
| 885 | synchronized (mLock) { |
| 886 | mSettingsRegistry.onUidRemovedLocked(uid); |
| 887 | } |
| 888 | } |
Zimuzo | c56192c | 2018-07-25 10:40:01 +0100 | [diff] [blame] | 889 | |
| 890 | @Override |
| 891 | public void onPackageDataCleared(String packageName, int uid) { |
| 892 | synchronized (mLock) { |
| 893 | mSettingsRegistry.removeSettingsForPackageLocked(packageName, |
| 894 | UserHandle.getUserId(uid)); |
| 895 | } |
| 896 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 897 | }; |
| 898 | |
| 899 | // package changes |
| 900 | monitor.register(getContext(), BackgroundThread.getHandler().getLooper(), |
| 901 | UserHandle.ALL, true); |
| 902 | } |
| 903 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 904 | private void startWatchingUserRestrictionChanges() { |
| 905 | // TODO: The current design of settings looking different based on user restrictions |
| 906 | // should be reworked to keep them separate and system code should check the setting |
| 907 | // first followed by checking the user restriction before performing an operation. |
Christopher Tate | 65fb2e4 | 2019-10-11 17:00:23 -0700 | [diff] [blame] | 908 | IUserRestrictionsListener listener = new IUserRestrictionsListener.Stub() { |
| 909 | @Override |
| 910 | public void onUserRestrictionsChanged(int userId, |
| 911 | Bundle newRestrictions, Bundle prevRestrictions) { |
| 912 | Set<String> changedRestrictions = |
| 913 | getRestrictionDiff(prevRestrictions, newRestrictions); |
| 914 | // We are changing the settings affected by restrictions to their current |
| 915 | // value with a forced update to ensure that all cross profile dependencies |
| 916 | // are taken into account. Also make sure the settings update to.. the same |
| 917 | // value passes the security checks, so clear binder calling id. |
| 918 | if (changedRestrictions.contains(UserManager.DISALLOW_SHARE_LOCATION)) { |
| 919 | final long identity = Binder.clearCallingIdentity(); |
| 920 | try { |
| 921 | synchronized (mLock) { |
| 922 | Setting setting = getSecureSetting( |
| 923 | Settings.Secure.LOCATION_MODE, userId); |
| 924 | updateSecureSetting(Settings.Secure.LOCATION_MODE, |
| 925 | setting != null ? setting.getValue() : null, null, |
| 926 | true, userId, true); |
| 927 | setting = getSecureSetting( |
| 928 | Settings.Secure.LOCATION_PROVIDERS_ALLOWED, userId); |
| 929 | updateSecureSetting(Settings.Secure.LOCATION_PROVIDERS_ALLOWED, |
| 930 | setting != null ? setting.getValue() : null, null, |
| 931 | true, userId, true); |
| 932 | } |
| 933 | } finally { |
| 934 | Binder.restoreCallingIdentity(identity); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 935 | } |
Christopher Tate | 65fb2e4 | 2019-10-11 17:00:23 -0700 | [diff] [blame] | 936 | } |
| 937 | if (changedRestrictions.contains(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES) |
| 938 | || changedRestrictions.contains( |
| 939 | UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY)) { |
| 940 | final long identity = Binder.clearCallingIdentity(); |
| 941 | try { |
| 942 | synchronized (mLock) { |
| 943 | Setting setting = getGlobalSetting( |
| 944 | Settings.Global.INSTALL_NON_MARKET_APPS); |
| 945 | String value = setting != null ? setting.getValue() : null; |
| 946 | updateGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS, |
| 947 | value, null, true, userId, true); |
| 948 | } |
| 949 | } finally { |
| 950 | Binder.restoreCallingIdentity(identity); |
| 951 | } |
| 952 | } |
| 953 | if (changedRestrictions.contains(UserManager.DISALLOW_DEBUGGING_FEATURES)) { |
| 954 | final long identity = Binder.clearCallingIdentity(); |
| 955 | try { |
| 956 | synchronized (mLock) { |
| 957 | Setting setting = getGlobalSetting(Settings.Global.ADB_ENABLED); |
| 958 | String value = setting != null ? setting.getValue() : null; |
| 959 | updateGlobalSetting(Settings.Global.ADB_ENABLED, |
| 960 | value, null, true, userId, true); |
| 961 | } |
| 962 | } finally { |
| 963 | Binder.restoreCallingIdentity(identity); |
| 964 | } |
| 965 | } |
| 966 | if (changedRestrictions.contains(UserManager.ENSURE_VERIFY_APPS)) { |
| 967 | final long identity = Binder.clearCallingIdentity(); |
| 968 | try { |
| 969 | synchronized (mLock) { |
| 970 | Setting enable = getGlobalSetting( |
| 971 | Settings.Global.PACKAGE_VERIFIER_ENABLE); |
| 972 | String enableValue = enable != null ? enable.getValue() : null; |
| 973 | updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_ENABLE, |
| 974 | enableValue, null, true, userId, true); |
| 975 | Setting include = getGlobalSetting( |
| 976 | Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB); |
| 977 | String includeValue = include != null ? include.getValue() : null; |
| 978 | updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, |
| 979 | includeValue, null, true, userId, true); |
| 980 | } |
| 981 | } finally { |
| 982 | Binder.restoreCallingIdentity(identity); |
| 983 | } |
| 984 | } |
| 985 | if (changedRestrictions.contains(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) { |
| 986 | final long identity = Binder.clearCallingIdentity(); |
| 987 | try { |
| 988 | synchronized (mLock) { |
| 989 | Setting setting = getGlobalSetting( |
| 990 | Settings.Global.PREFERRED_NETWORK_MODE); |
| 991 | String value = setting != null ? setting.getValue() : null; |
| 992 | updateGlobalSetting(Settings.Global.PREFERRED_NETWORK_MODE, |
| 993 | value, null, true, userId, true); |
| 994 | } |
| 995 | } finally { |
| 996 | Binder.restoreCallingIdentity(identity); |
| 997 | } |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 998 | } |
| 999 | } |
Christopher Tate | 65fb2e4 | 2019-10-11 17:00:23 -0700 | [diff] [blame] | 1000 | }; |
| 1001 | mUserManager.addUserRestrictionsListener(listener); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Irina Dumitrescu | e369687 | 2019-01-09 16:07:59 +0000 | [diff] [blame] | 1004 | private static Set<String> getRestrictionDiff(Bundle prevRestrictions, Bundle newRestrictions) { |
| 1005 | Set<String> restrictionNames = Sets.newArraySet(); |
| 1006 | restrictionNames.addAll(prevRestrictions.keySet()); |
| 1007 | restrictionNames.addAll(newRestrictions.keySet()); |
| 1008 | Set<String> diff = Sets.newArraySet(); |
| 1009 | for (String restrictionName : restrictionNames) { |
| 1010 | if (prevRestrictions.getBoolean(restrictionName) != newRestrictions.getBoolean( |
| 1011 | restrictionName)) { |
| 1012 | diff.add(restrictionName); |
| 1013 | } |
| 1014 | } |
| 1015 | return diff; |
| 1016 | } |
| 1017 | |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1018 | private Setting getConfigSetting(String name) { |
| 1019 | if (DEBUG) { |
| 1020 | Slog.v(LOG_TAG, "getConfigSetting(" + name + ")"); |
| 1021 | } |
| 1022 | |
Stanislav Zholnin | 5579950 | 2019-03-08 14:54:55 +0000 | [diff] [blame] | 1023 | DeviceConfig.enforceReadPermission(getContext(), /*namespace=*/name.split("/")[0]); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1024 | |
| 1025 | // Get the value. |
| 1026 | synchronized (mLock) { |
| 1027 | return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_CONFIG, |
| 1028 | UserHandle.USER_SYSTEM, name); |
| 1029 | } |
| 1030 | } |
| 1031 | |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1032 | private boolean insertConfigSetting(String name, String value, boolean makeDefault) { |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1033 | if (DEBUG) { |
| 1034 | Slog.v(LOG_TAG, "insertConfigSetting(" + name + ", " + value + ", " |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1035 | + makeDefault + ")"); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1036 | } |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1037 | return mutateConfigSetting(name, value, null, makeDefault, |
| 1038 | MUTATION_OPERATION_INSERT, 0); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
Matt Pape | 4bd0a6a | 2019-11-19 12:17:45 -0800 | [diff] [blame^] | 1041 | private boolean setAllConfigSettings(String prefix, Map<String, String> keyValues) { |
| 1042 | if (DEBUG) { |
| 1043 | Slog.v(LOG_TAG, "setAllConfigSettings for prefix: " + prefix); |
| 1044 | } |
| 1045 | |
| 1046 | enforceWritePermission(Manifest.permission.WRITE_DEVICE_CONFIG); |
| 1047 | |
| 1048 | synchronized (mLock) { |
| 1049 | return mSettingsRegistry.setSettingsLocked(SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM, |
| 1050 | prefix, keyValues, resolveCallingPackage()); |
| 1051 | } |
| 1052 | } |
| 1053 | |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1054 | private boolean deleteConfigSetting(String name) { |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 1055 | if (DEBUG) { |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1056 | Slog.v(LOG_TAG, "deleteConfigSetting(" + name + ")"); |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 1057 | } |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1058 | return mutateConfigSetting(name, null, null, false, |
| 1059 | MUTATION_OPERATION_DELETE, 0); |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 1060 | } |
| 1061 | |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1062 | private void resetConfigSetting(int mode, String prefix) { |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1063 | if (DEBUG) { |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1064 | Slog.v(LOG_TAG, "resetConfigSetting(" + mode + ", " + prefix + ")"); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1065 | } |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1066 | mutateConfigSetting(null, null, prefix, false, |
| 1067 | MUTATION_OPERATION_RESET, mode); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 1070 | private boolean mutateConfigSetting(String name, String value, String prefix, |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1071 | boolean makeDefault, int operation, int mode) { |
Stanislav Zholnin | 5a25a84 | 2019-03-13 14:43:26 +0000 | [diff] [blame] | 1072 | enforceWritePermission(Manifest.permission.WRITE_DEVICE_CONFIG); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1073 | |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1074 | // Perform the mutation. |
| 1075 | synchronized (mLock) { |
| 1076 | switch (operation) { |
| 1077 | case MUTATION_OPERATION_INSERT: { |
| 1078 | return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_CONFIG, |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1079 | UserHandle.USER_SYSTEM, name, value, null, makeDefault, true, |
Matt Pape | 7b1c6cd | 2019-01-04 08:10:41 -0800 | [diff] [blame] | 1080 | resolveCallingPackage(), false, null); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 1083 | case MUTATION_OPERATION_DELETE: { |
| 1084 | return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_CONFIG, |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1085 | UserHandle.USER_SYSTEM, name, false, null); |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 1086 | } |
| 1087 | |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1088 | case MUTATION_OPERATION_RESET: { |
| 1089 | mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_CONFIG, |
Matt Pape | 7b1c6cd | 2019-01-04 08:10:41 -0800 | [diff] [blame] | 1090 | UserHandle.USER_SYSTEM, resolveCallingPackage(), mode, null, prefix); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1091 | } return true; |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | return false; |
| 1096 | } |
| 1097 | |
Matt Pape | 793b15c | 2019-10-07 13:17:20 -0700 | [diff] [blame] | 1098 | private HashMap<String, String> getAllConfigFlags(@Nullable String prefix) { |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 1099 | if (DEBUG) { |
| 1100 | Slog.v(LOG_TAG, "getAllConfigFlags() for " + prefix); |
| 1101 | } |
| 1102 | |
Matt Pape | 91beb9c | 2019-10-17 15:20:34 -0700 | [diff] [blame] | 1103 | DeviceConfig.enforceReadPermission(getContext(), |
| 1104 | prefix != null ? prefix.split("/")[0] : null); |
| 1105 | |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 1106 | synchronized (mLock) { |
| 1107 | // Get the settings. |
| 1108 | SettingsState settingsState = mSettingsRegistry.getSettingsLocked( |
| 1109 | SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM); |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 1110 | List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_CONFIG, |
| 1111 | UserHandle.USER_SYSTEM); |
| 1112 | |
| 1113 | final int nameCount = names.size(); |
Matt Pape | 793b15c | 2019-10-07 13:17:20 -0700 | [diff] [blame] | 1114 | HashMap<String, String> flagsToValues = new HashMap<>(names.size()); |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 1115 | |
| 1116 | for (int i = 0; i < nameCount; i++) { |
| 1117 | String name = names.get(i); |
| 1118 | Setting setting = settingsState.getSettingLocked(name); |
| 1119 | if (prefix == null || setting.getName().startsWith(prefix)) { |
| 1120 | flagsToValues.put(setting.getName(), setting.getValue()); |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | return flagsToValues; |
| 1125 | } |
| 1126 | } |
| 1127 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1128 | private Cursor getAllGlobalSettings(String[] projection) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1129 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1130 | Slog.v(LOG_TAG, "getAllGlobalSettings()"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1131 | } |
| 1132 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1133 | synchronized (mLock) { |
| 1134 | // Get the settings. |
| 1135 | SettingsState settingsState = mSettingsRegistry.getSettingsLocked( |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1136 | SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1137 | |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1138 | List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_GLOBAL, |
| 1139 | UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1140 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1141 | final int nameCount = names.size(); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1142 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1143 | String[] normalizedProjection = normalizeProjection(projection); |
| 1144 | MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1145 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1146 | // Anyone can get the global settings, so no security checks. |
| 1147 | for (int i = 0; i < nameCount; i++) { |
| 1148 | String name = names.get(i); |
| 1149 | Setting setting = settingsState.getSettingLocked(name); |
| 1150 | appendSettingToCursor(result, setting); |
| 1151 | } |
| 1152 | |
| 1153 | return result; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1154 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1155 | } |
| 1156 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1157 | private Setting getGlobalSetting(String name) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1158 | if (DEBUG) { |
| 1159 | Slog.v(LOG_TAG, "getGlobalSetting(" + name + ")"); |
| 1160 | } |
| 1161 | |
Chad Brubaker | a6830e7 | 2017-04-28 17:34:36 -0700 | [diff] [blame] | 1162 | // Ensure the caller can access the setting. |
| 1163 | enforceSettingReadable(name, SETTINGS_TYPE_GLOBAL, UserHandle.getCallingUserId()); |
| 1164 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1165 | // Get the value. |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1166 | synchronized (mLock) { |
Chad Brubaker | a6830e7 | 2017-04-28 17:34:36 -0700 | [diff] [blame] | 1167 | return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_GLOBAL, |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1168 | UserHandle.USER_SYSTEM, name); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1169 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1172 | private boolean updateGlobalSetting(String name, String value, String tag, |
| 1173 | boolean makeDefault, int requestingUserId, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1174 | if (DEBUG) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1175 | Slog.v(LOG_TAG, "updateGlobalSetting(" + name + ", " + value + ", " |
| 1176 | + ", " + tag + ", " + makeDefault + ", " + requestingUserId |
| 1177 | + ", " + forceNotify + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1178 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1179 | return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId, |
| 1180 | MUTATION_OPERATION_UPDATE, forceNotify, 0); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1181 | } |
| 1182 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1183 | private boolean insertGlobalSetting(String name, String value, String tag, |
| 1184 | boolean makeDefault, int requestingUserId, boolean forceNotify) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1185 | if (DEBUG) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1186 | Slog.v(LOG_TAG, "insertGlobalSetting(" + name + ", " + value + ", " |
| 1187 | + ", " + tag + ", " + makeDefault + ", " + requestingUserId |
| 1188 | + ", " + forceNotify + ")"); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1189 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1190 | return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId, |
| 1191 | MUTATION_OPERATION_INSERT, forceNotify, 0); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1194 | private boolean deleteGlobalSetting(String name, int requestingUserId, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1195 | if (DEBUG) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1196 | Slog.v(LOG_TAG, "deleteGlobalSetting(" + name + ", " + requestingUserId |
| 1197 | + ", " + forceNotify + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1198 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1199 | return mutateGlobalSetting(name, null, null, false, requestingUserId, |
| 1200 | MUTATION_OPERATION_DELETE, forceNotify, 0); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1201 | } |
| 1202 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1203 | private void resetGlobalSetting(int requestingUserId, int mode, String tag) { |
| 1204 | if (DEBUG) { |
| 1205 | Slog.v(LOG_TAG, "resetGlobalSetting(" + requestingUserId + ", " |
| 1206 | + mode + ", " + tag + ")"); |
| 1207 | } |
| 1208 | mutateGlobalSetting(null, null, tag, false, requestingUserId, |
| 1209 | MUTATION_OPERATION_RESET, false, mode); |
| 1210 | } |
| 1211 | |
Christopher Tate | 65fb2e4 | 2019-10-11 17:00:23 -0700 | [diff] [blame] | 1212 | private boolean isSettingRestrictedForUser(String name, int userId, |
| 1213 | String value, int callerUid) { |
| 1214 | final long oldId = Binder.clearCallingIdentity(); |
| 1215 | try { |
| 1216 | return (name != null |
| 1217 | && mUserManager.isSettingRestrictedForUser(name, userId, value, callerUid)); |
| 1218 | } finally { |
| 1219 | Binder.restoreCallingIdentity(oldId); |
| 1220 | } |
| 1221 | } |
| 1222 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1223 | private boolean mutateGlobalSetting(String name, String value, String tag, |
| 1224 | boolean makeDefault, int requestingUserId, int operation, boolean forceNotify, |
| 1225 | int mode) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1226 | // Make sure the caller can change the settings - treated as secure. |
| 1227 | enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS); |
| 1228 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1229 | // Resolve the userId on whose behalf the call is made. |
| 1230 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId); |
| 1231 | |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 1232 | // If this is a setting that is currently restricted for this user, do not allow |
| 1233 | // unrestricting changes. |
Christopher Tate | 65fb2e4 | 2019-10-11 17:00:23 -0700 | [diff] [blame] | 1234 | if (isSettingRestrictedForUser(name, callingUserId, value, Binder.getCallingUid())) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1235 | return false; |
| 1236 | } |
| 1237 | |
| 1238 | // Perform the mutation. |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1239 | synchronized (mLock) { |
| 1240 | switch (operation) { |
| 1241 | case MUTATION_OPERATION_INSERT: { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1242 | return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_GLOBAL, |
| 1243 | UserHandle.USER_SYSTEM, name, value, tag, makeDefault, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1244 | getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1245 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1246 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1247 | case MUTATION_OPERATION_DELETE: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1248 | return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_GLOBAL, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1249 | UserHandle.USER_SYSTEM, name, forceNotify, CRITICAL_GLOBAL_SETTINGS); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1250 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1251 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1252 | case MUTATION_OPERATION_UPDATE: { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1253 | return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_GLOBAL, |
| 1254 | UserHandle.USER_SYSTEM, name, value, tag, makeDefault, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1255 | getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1256 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1257 | |
| 1258 | case MUTATION_OPERATION_RESET: { |
| 1259 | mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_GLOBAL, |
| 1260 | UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag); |
| 1261 | } return true; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | return false; |
| 1266 | } |
| 1267 | |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 1268 | private PackageInfo getCallingPackageInfo(int userId) { |
| 1269 | try { |
| 1270 | return mPackageManager.getPackageInfo(getCallingPackage(), |
| 1271 | PackageManager.GET_SIGNATURES, userId); |
| 1272 | } catch (RemoteException e) { |
| 1273 | throw new IllegalStateException("Package " + getCallingPackage() + " doesn't exist"); |
| 1274 | } |
| 1275 | } |
| 1276 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1277 | private Cursor getAllSecureSettings(int userId, String[] projection) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1278 | if (DEBUG) { |
| 1279 | Slog.v(LOG_TAG, "getAllSecureSettings(" + userId + ")"); |
| 1280 | } |
| 1281 | |
| 1282 | // Resolve the userId on whose behalf the call is made. |
| 1283 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId); |
| 1284 | |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 1285 | // The relevant "calling package" userId will be the owning userId for some |
| 1286 | // profiles, and we can't do the lookup inside our [lock held] loop, so work out |
| 1287 | // up front who the effective "new SSAID" user ID for that settings name will be. |
| 1288 | final int ssaidUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, |
| 1289 | Settings.Secure.ANDROID_ID); |
| 1290 | final PackageInfo ssaidCallingPkg = getCallingPackageInfo(ssaidUserId); |
| 1291 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1292 | synchronized (mLock) { |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1293 | List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SECURE, callingUserId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1294 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1295 | final int nameCount = names.size(); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1296 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1297 | String[] normalizedProjection = normalizeProjection(projection); |
| 1298 | MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1299 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1300 | for (int i = 0; i < nameCount; i++) { |
| 1301 | String name = names.get(i); |
| 1302 | // Determine the owning user as some profile settings are cloned from the parent. |
| 1303 | final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, |
| 1304 | name); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1305 | |
Alex Klyubin | 1991f57 | 2017-03-03 14:08:36 -0800 | [diff] [blame] | 1306 | if (!isSecureSettingAccessible(name, callingUserId, owningUserId)) { |
| 1307 | // This caller is not permitted to access this setting. Pretend the setting |
| 1308 | // doesn't exist. |
Svetoslav Ganov | 83a1f7f | 2016-04-27 13:50:49 -0700 | [diff] [blame] | 1309 | continue; |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1310 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1311 | |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 1312 | // As of Android O, the SSAID is read from an app-specific entry in table |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1313 | // SETTINGS_FILE_SSAID, unless accessed by a system process. |
| 1314 | final Setting setting; |
| 1315 | if (isNewSsaidSetting(name)) { |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 1316 | setting = getSsaidSettingLocked(ssaidCallingPkg, owningUserId); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1317 | } else { |
| 1318 | setting = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE, owningUserId, |
| 1319 | name); |
| 1320 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1321 | appendSettingToCursor(result, setting); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1322 | } |
| 1323 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1324 | return result; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1325 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1326 | } |
| 1327 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1328 | private Setting getSecureSetting(String name, int requestingUserId) { |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 1329 | return getSecureSetting(name, requestingUserId, /*enableOverride=*/ false); |
| 1330 | } |
| 1331 | |
| 1332 | private Setting getSecureSetting(String name, int requestingUserId, boolean enableOverride) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1333 | if (DEBUG) { |
| 1334 | Slog.v(LOG_TAG, "getSecureSetting(" + name + ", " + requestingUserId + ")"); |
| 1335 | } |
| 1336 | |
| 1337 | // Resolve the userId on whose behalf the call is made. |
| 1338 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId); |
| 1339 | |
Chad Brubaker | a6830e7 | 2017-04-28 17:34:36 -0700 | [diff] [blame] | 1340 | // Ensure the caller can access the setting. |
| 1341 | enforceSettingReadable(name, SETTINGS_TYPE_SECURE, UserHandle.getCallingUserId()); |
| 1342 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1343 | // Determine the owning user as some profile settings are cloned from the parent. |
| 1344 | final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name); |
| 1345 | |
Alex Klyubin | 1991f57 | 2017-03-03 14:08:36 -0800 | [diff] [blame] | 1346 | if (!isSecureSettingAccessible(name, callingUserId, owningUserId)) { |
| 1347 | // This caller is not permitted to access this setting. Pretend the setting doesn't |
| 1348 | // exist. |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 1349 | SettingsState settings = mSettingsRegistry.getSettingsLocked(SETTINGS_TYPE_SECURE, |
| 1350 | owningUserId); |
| 1351 | return settings != null ? settings.getNullSetting() : null; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1352 | } |
| 1353 | |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 1354 | // As of Android O, the SSAID is read from an app-specific entry in table |
| 1355 | // SETTINGS_FILE_SSAID, unless accessed by a system process. |
| 1356 | if (isNewSsaidSetting(name)) { |
| 1357 | PackageInfo callingPkg = getCallingPackageInfo(owningUserId); |
| 1358 | synchronized (mLock) { |
| 1359 | return getSsaidSettingLocked(callingPkg, owningUserId); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1360 | } |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 1361 | } |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 1362 | if (enableOverride) { |
Kweku Adams | 5e0052b | 2019-02-22 15:17:52 -0800 | [diff] [blame] | 1363 | if (Secure.LOCATION_MODE.equals(name)) { |
| 1364 | final Setting overridden = getLocationModeSetting(owningUserId); |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 1365 | if (overridden != null) { |
| 1366 | return overridden; |
| 1367 | } |
| 1368 | } |
| 1369 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1370 | |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 1371 | // Not the SSAID; do a straight lookup |
| 1372 | synchronized (mLock) { |
Chad Brubaker | a6830e7 | 2017-04-28 17:34:36 -0700 | [diff] [blame] | 1373 | return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE, |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1374 | owningUserId, name); |
| 1375 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1376 | } |
| 1377 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1378 | private boolean isNewSsaidSetting(String name) { |
| 1379 | return Settings.Secure.ANDROID_ID.equals(name) |
| 1380 | && UserHandle.getAppId(Binder.getCallingUid()) >= Process.FIRST_APPLICATION_UID; |
| 1381 | } |
| 1382 | |
Andreas Gampe | b5889307 | 2018-09-05 16:52:31 -0700 | [diff] [blame] | 1383 | @GuardedBy("mLock") |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 1384 | private Setting getSsaidSettingLocked(PackageInfo callingPkg, int owningUserId) { |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1385 | // Get uid of caller (key) used to store ssaid value |
| 1386 | String name = Integer.toString( |
| 1387 | UserHandle.getUid(owningUserId, UserHandle.getAppId(Binder.getCallingUid()))); |
| 1388 | |
| 1389 | if (DEBUG) { |
| 1390 | Slog.v(LOG_TAG, "getSsaidSettingLocked(" + name + "," + owningUserId + ")"); |
| 1391 | } |
| 1392 | |
| 1393 | // Retrieve the ssaid from the table if present. |
| 1394 | final Setting ssaid = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SSAID, owningUserId, |
| 1395 | name); |
Chad Brubaker | 0d277a7 | 2017-04-12 16:56:53 -0700 | [diff] [blame] | 1396 | // If the app is an Instant App use its stored SSAID instead of our own. |
| 1397 | final String instantSsaid; |
| 1398 | final long token = Binder.clearCallingIdentity(); |
| 1399 | try { |
| 1400 | instantSsaid = mPackageManager.getInstantAppAndroidId(callingPkg.packageName, |
| 1401 | owningUserId); |
| 1402 | } catch (RemoteException e) { |
| 1403 | Slog.e(LOG_TAG, "Failed to get Instant App Android ID", e); |
| 1404 | return null; |
| 1405 | } finally { |
| 1406 | Binder.restoreCallingIdentity(token); |
| 1407 | } |
Svet Ganov | 96c9946 | 2017-05-05 14:27:13 -0700 | [diff] [blame] | 1408 | |
| 1409 | final SettingsState ssaidSettings = mSettingsRegistry.getSettingsLocked( |
| 1410 | SETTINGS_TYPE_SSAID, owningUserId); |
| 1411 | |
Chad Brubaker | 0d277a7 | 2017-04-12 16:56:53 -0700 | [diff] [blame] | 1412 | if (instantSsaid != null) { |
| 1413 | // Use the stored value if it is still valid. |
| 1414 | if (ssaid != null && instantSsaid.equals(ssaid.getValue())) { |
Svet Ganov | 96c9946 | 2017-05-05 14:27:13 -0700 | [diff] [blame] | 1415 | return mascaradeSsaidSetting(ssaidSettings, ssaid); |
Chad Brubaker | 0d277a7 | 2017-04-12 16:56:53 -0700 | [diff] [blame] | 1416 | } |
| 1417 | // The value has changed, update the stored value. |
Chad Brubaker | 0d277a7 | 2017-04-12 16:56:53 -0700 | [diff] [blame] | 1418 | final boolean success = ssaidSettings.insertSettingLocked(name, instantSsaid, null, |
| 1419 | true, callingPkg.packageName); |
| 1420 | if (!success) { |
| 1421 | throw new IllegalStateException("Failed to update instant app android id"); |
| 1422 | } |
Svet Ganov | 96c9946 | 2017-05-05 14:27:13 -0700 | [diff] [blame] | 1423 | Setting setting = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SSAID, |
| 1424 | owningUserId, name); |
| 1425 | return mascaradeSsaidSetting(ssaidSettings, setting); |
Chad Brubaker | 0d277a7 | 2017-04-12 16:56:53 -0700 | [diff] [blame] | 1426 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1427 | |
| 1428 | // Lazy initialize ssaid if not yet present in ssaid table. |
Mark Rathjen | ea61759 | 2017-01-18 23:03:41 -0800 | [diff] [blame] | 1429 | if (ssaid == null || ssaid.isNull() || ssaid.getValue() == null) { |
Svet Ganov | 96c9946 | 2017-05-05 14:27:13 -0700 | [diff] [blame] | 1430 | Setting setting = mSettingsRegistry.generateSsaidLocked(callingPkg, owningUserId); |
| 1431 | return mascaradeSsaidSetting(ssaidSettings, setting); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Svet Ganov | 96c9946 | 2017-05-05 14:27:13 -0700 | [diff] [blame] | 1434 | return mascaradeSsaidSetting(ssaidSettings, ssaid); |
| 1435 | } |
| 1436 | |
| 1437 | private Setting mascaradeSsaidSetting(SettingsState settingsState, Setting ssaidSetting) { |
| 1438 | // SSAID settings are located in a dedicated table for internal bookkeeping |
| 1439 | // but for the world they reside in the secure table, so adjust the key here. |
| 1440 | // We have a special name when looking it up but want the world to see it as |
| 1441 | // "android_id". |
| 1442 | if (ssaidSetting != null) { |
| 1443 | return settingsState.new Setting(ssaidSetting) { |
| 1444 | @Override |
| 1445 | public int getKey() { |
| 1446 | final int userId = getUserIdFromKey(super.getKey()); |
| 1447 | return makeKey(SETTINGS_TYPE_SECURE, userId); |
| 1448 | } |
| 1449 | |
| 1450 | @Override |
| 1451 | public String getName() { |
| 1452 | return Settings.Secure.ANDROID_ID; |
| 1453 | } |
| 1454 | }; |
| 1455 | } |
| 1456 | return null; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Kweku Adams | 5e0052b | 2019-02-22 15:17:52 -0800 | [diff] [blame] | 1459 | private Setting getLocationModeSetting(int owningUserId) { |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 1460 | synchronized (mLock) { |
| 1461 | final Setting setting = getGlobalSetting( |
| 1462 | Global.LOCATION_GLOBAL_KILL_SWITCH); |
| 1463 | if (!"1".equals(setting.getValue())) { |
| 1464 | return null; |
| 1465 | } |
| 1466 | // Global kill-switch is enabled. Return an empty value. |
| 1467 | final SettingsState settingsState = mSettingsRegistry.getSettingsLocked( |
| 1468 | SETTINGS_TYPE_SECURE, owningUserId); |
| 1469 | return settingsState.new Setting( |
Kweku Adams | 5e0052b | 2019-02-22 15:17:52 -0800 | [diff] [blame] | 1470 | Secure.LOCATION_MODE, |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 1471 | "", // value |
| 1472 | "", // tag |
| 1473 | "", // default value |
| 1474 | "", // package name |
| 1475 | false, // from system |
| 1476 | "0" // id |
| 1477 | ) { |
| 1478 | @Override |
| 1479 | public boolean update(String value, boolean setDefault, String packageName, |
| 1480 | String tag, boolean forceNonSystemPackage) { |
Kweku Adams | 5e0052b | 2019-02-22 15:17:52 -0800 | [diff] [blame] | 1481 | Slog.wtf(LOG_TAG, "update shouldn't be called on this instance."); |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 1482 | return false; |
| 1483 | } |
| 1484 | }; |
| 1485 | } |
| 1486 | } |
| 1487 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1488 | private boolean insertSecureSetting(String name, String value, String tag, |
| 1489 | boolean makeDefault, int requestingUserId, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1490 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1491 | Slog.v(LOG_TAG, "insertSecureSetting(" + name + ", " + value + ", " |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1492 | + ", " + tag + ", " + makeDefault + ", " + requestingUserId |
| 1493 | + ", " + forceNotify + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1494 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1495 | return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId, |
| 1496 | MUTATION_OPERATION_INSERT, forceNotify, 0); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1497 | } |
| 1498 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1499 | private boolean deleteSecureSetting(String name, int requestingUserId, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1500 | if (DEBUG) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1501 | Slog.v(LOG_TAG, "deleteSecureSetting(" + name + ", " + requestingUserId |
| 1502 | + ", " + forceNotify + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1503 | } |
| 1504 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1505 | return mutateSecureSetting(name, null, null, false, requestingUserId, |
| 1506 | MUTATION_OPERATION_DELETE, forceNotify, 0); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1507 | } |
| 1508 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1509 | private boolean updateSecureSetting(String name, String value, String tag, |
| 1510 | boolean makeDefault, int requestingUserId, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1511 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1512 | Slog.v(LOG_TAG, "updateSecureSetting(" + name + ", " + value + ", " |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1513 | + ", " + tag + ", " + makeDefault + ", " + requestingUserId |
| 1514 | + ", " + forceNotify +")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1515 | } |
| 1516 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1517 | return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId, |
| 1518 | MUTATION_OPERATION_UPDATE, forceNotify, 0); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1519 | } |
| 1520 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1521 | private void resetSecureSetting(int requestingUserId, int mode, String tag) { |
| 1522 | if (DEBUG) { |
| 1523 | Slog.v(LOG_TAG, "resetSecureSetting(" + requestingUserId + ", " |
| 1524 | + mode + ", " + tag + ")"); |
| 1525 | } |
| 1526 | |
| 1527 | mutateSecureSetting(null, null, tag, false, requestingUserId, |
| 1528 | MUTATION_OPERATION_RESET, false, mode); |
| 1529 | } |
| 1530 | |
| 1531 | private boolean mutateSecureSetting(String name, String value, String tag, |
| 1532 | boolean makeDefault, int requestingUserId, int operation, boolean forceNotify, |
| 1533 | int mode) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1534 | // Make sure the caller can change the settings. |
| 1535 | enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS); |
| 1536 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1537 | // Resolve the userId on whose behalf the call is made. |
| 1538 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId); |
| 1539 | |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 1540 | // If this is a setting that is currently restricted for this user, do not allow |
| 1541 | // unrestricting changes. |
Christopher Tate | 65fb2e4 | 2019-10-11 17:00:23 -0700 | [diff] [blame] | 1542 | if (isSettingRestrictedForUser(name, callingUserId, value, Binder.getCallingUid())) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1543 | return false; |
| 1544 | } |
| 1545 | |
| 1546 | // Determine the owning user as some profile settings are cloned from the parent. |
| 1547 | final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name); |
| 1548 | |
| 1549 | // Only the owning user can change the setting. |
| 1550 | if (owningUserId != callingUserId) { |
| 1551 | return false; |
| 1552 | } |
| 1553 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1554 | // Mutate the value. |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1555 | synchronized (mLock) { |
Soonil Nagarkar | 10b1960 | 2019-09-10 15:46:32 -0700 | [diff] [blame] | 1556 | // Special cases for location providers (sigh). |
| 1557 | if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) { |
| 1558 | return updateLocationProvidersAllowedLocked(value, tag, owningUserId, makeDefault, |
| 1559 | forceNotify); |
| 1560 | } |
| 1561 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1562 | switch (operation) { |
| 1563 | case MUTATION_OPERATION_INSERT: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1564 | return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1565 | owningUserId, name, value, tag, makeDefault, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1566 | getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1567 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1568 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1569 | case MUTATION_OPERATION_DELETE: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1570 | return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SECURE, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1571 | owningUserId, name, forceNotify, CRITICAL_SECURE_SETTINGS); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1572 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1573 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1574 | case MUTATION_OPERATION_UPDATE: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1575 | return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SECURE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1576 | owningUserId, name, value, tag, makeDefault, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1577 | getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1578 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1579 | |
| 1580 | case MUTATION_OPERATION_RESET: { |
| 1581 | mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_SECURE, |
| 1582 | UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag); |
| 1583 | } return true; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | return false; |
| 1588 | } |
| 1589 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1590 | private Cursor getAllSystemSettings(int userId, String[] projection) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1591 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1592 | Slog.v(LOG_TAG, "getAllSecureSystem(" + userId + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | // Resolve the userId on whose behalf the call is made. |
| 1596 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId); |
| 1597 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1598 | synchronized (mLock) { |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1599 | List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SYSTEM, callingUserId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1600 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1601 | final int nameCount = names.size(); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1602 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1603 | String[] normalizedProjection = normalizeProjection(projection); |
| 1604 | MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1605 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1606 | for (int i = 0; i < nameCount; i++) { |
| 1607 | String name = names.get(i); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1608 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1609 | // Determine the owning user as some profile settings are cloned from the parent. |
| 1610 | final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, |
| 1611 | name); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1612 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1613 | Setting setting = mSettingsRegistry.getSettingLocked( |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1614 | SETTINGS_TYPE_SYSTEM, owningUserId, name); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1615 | appendSettingToCursor(result, setting); |
| 1616 | } |
| 1617 | |
| 1618 | return result; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1619 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1620 | } |
| 1621 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1622 | private Setting getSystemSetting(String name, int requestingUserId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1623 | if (DEBUG) { |
| 1624 | Slog.v(LOG_TAG, "getSystemSetting(" + name + ", " + requestingUserId + ")"); |
| 1625 | } |
| 1626 | |
| 1627 | // Resolve the userId on whose behalf the call is made. |
| 1628 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId); |
| 1629 | |
Chad Brubaker | a6830e7 | 2017-04-28 17:34:36 -0700 | [diff] [blame] | 1630 | // Ensure the caller can access the setting. |
| 1631 | enforceSettingReadable(name, SETTINGS_TYPE_SYSTEM, UserHandle.getCallingUserId()); |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1632 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1633 | // Determine the owning user as some profile settings are cloned from the parent. |
| 1634 | final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name); |
| 1635 | |
| 1636 | // Get the value. |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1637 | synchronized (mLock) { |
Chad Brubaker | a6830e7 | 2017-04-28 17:34:36 -0700 | [diff] [blame] | 1638 | return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SYSTEM, owningUserId, name); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1639 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1640 | } |
| 1641 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1642 | private boolean insertSystemSetting(String name, String value, int requestingUserId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1643 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1644 | Slog.v(LOG_TAG, "insertSystemSetting(" + name + ", " + value + ", " |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1645 | + requestingUserId + ")"); |
| 1646 | } |
| 1647 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1648 | return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1649 | } |
| 1650 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1651 | private boolean deleteSystemSetting(String name, int requestingUserId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1652 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1653 | Slog.v(LOG_TAG, "deleteSystemSetting(" + name + ", " + requestingUserId + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1654 | } |
| 1655 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1656 | return mutateSystemSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1657 | } |
| 1658 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1659 | private boolean updateSystemSetting(String name, String value, int requestingUserId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1660 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1661 | Slog.v(LOG_TAG, "updateSystemSetting(" + name + ", " + value + ", " |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1662 | + requestingUserId + ")"); |
| 1663 | } |
| 1664 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1665 | return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1666 | } |
| 1667 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1668 | private boolean mutateSystemSetting(String name, String value, int runAsUserId, |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1669 | int operation) { |
Billy Lau | 6ad2d66 | 2015-07-18 00:26:58 +0100 | [diff] [blame] | 1670 | if (!hasWriteSecureSettingsPermission()) { |
| 1671 | // If the caller doesn't hold WRITE_SECURE_SETTINGS, we verify whether this |
| 1672 | // operation is allowed for the calling package through appops. |
| 1673 | if (!Settings.checkAndNoteWriteSettingsOperation(getContext(), |
| 1674 | Binder.getCallingUid(), getCallingPackage(), true)) { |
| 1675 | return false; |
| 1676 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1677 | } |
| 1678 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1679 | // Resolve the userId on whose behalf the call is made. |
| 1680 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId); |
| 1681 | |
Christopher Tate | 65fb2e4 | 2019-10-11 17:00:23 -0700 | [diff] [blame] | 1682 | if (isSettingRestrictedForUser(name, callingUserId, value, Binder.getCallingUid())) { |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 1683 | return false; |
| 1684 | } |
| 1685 | |
Svetoslav | d8d25e0 | 2015-11-20 13:09:26 -0800 | [diff] [blame] | 1686 | // Enforce what the calling package can mutate the system settings. |
| 1687 | enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name, callingUserId); |
| 1688 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1689 | // Determine the owning user as some profile settings are cloned from the parent. |
| 1690 | final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name); |
| 1691 | |
| 1692 | // Only the owning user id can change the setting. |
| 1693 | if (owningUserId != callingUserId) { |
| 1694 | return false; |
| 1695 | } |
| 1696 | |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 1697 | // Invalidate any relevant cache files |
| 1698 | String cacheName = null; |
| 1699 | if (Settings.System.RINGTONE.equals(name)) { |
| 1700 | cacheName = Settings.System.RINGTONE_CACHE; |
| 1701 | } else if (Settings.System.NOTIFICATION_SOUND.equals(name)) { |
| 1702 | cacheName = Settings.System.NOTIFICATION_SOUND_CACHE; |
| 1703 | } else if (Settings.System.ALARM_ALERT.equals(name)) { |
| 1704 | cacheName = Settings.System.ALARM_ALERT_CACHE; |
| 1705 | } |
| 1706 | if (cacheName != null) { |
| 1707 | final File cacheFile = new File( |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 1708 | getRingtoneCacheDir(owningUserId), cacheName); |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 1709 | cacheFile.delete(); |
| 1710 | } |
| 1711 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1712 | // Mutate the value. |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1713 | synchronized (mLock) { |
| 1714 | switch (operation) { |
| 1715 | case MUTATION_OPERATION_INSERT: { |
| 1716 | validateSystemSettingValue(name, value); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1717 | return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SYSTEM, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1718 | owningUserId, name, value, null, false, getCallingPackage(), |
| 1719 | false, null); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | case MUTATION_OPERATION_DELETE: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1723 | return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SYSTEM, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1724 | owningUserId, name, false, null); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | case MUTATION_OPERATION_UPDATE: { |
| 1728 | validateSystemSettingValue(name, value); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1729 | return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SYSTEM, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1730 | owningUserId, name, value, null, false, getCallingPackage(), |
| 1731 | false, null); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1732 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1733 | } |
| 1734 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1735 | return false; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1736 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1737 | } |
| 1738 | |
Billy Lau | 6ad2d66 | 2015-07-18 00:26:58 +0100 | [diff] [blame] | 1739 | private boolean hasWriteSecureSettingsPermission() { |
Svetoslav | f41334b | 2015-06-23 12:06:03 -0700 | [diff] [blame] | 1740 | // Write secure settings is a more protected permission. If caller has it we are good. |
| 1741 | if (getContext().checkCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1742 | == PackageManager.PERMISSION_GRANTED) { |
| 1743 | return true; |
| 1744 | } |
| 1745 | |
Svetoslav | f41334b | 2015-06-23 12:06:03 -0700 | [diff] [blame] | 1746 | return false; |
| 1747 | } |
| 1748 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1749 | private void validateSystemSettingValue(String name, String value) { |
Al Sutton | 91f89d0 | 2019-08-16 12:56:57 +0100 | [diff] [blame] | 1750 | Validator validator = SystemSettingsValidators.VALIDATORS.get(name); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1751 | if (validator != null && !validator.validate(value)) { |
| 1752 | throw new IllegalArgumentException("Invalid value: " + value |
| 1753 | + " for setting: " + name); |
| 1754 | } |
| 1755 | } |
| 1756 | |
Alex Klyubin | 1991f57 | 2017-03-03 14:08:36 -0800 | [diff] [blame] | 1757 | /** |
| 1758 | * Returns {@code true} if the specified secure setting should be accessible to the caller. |
| 1759 | */ |
| 1760 | private boolean isSecureSettingAccessible(String name, int callingUserId, |
| 1761 | int owningUserId) { |
| 1762 | // Special case for location (sigh). |
| 1763 | // This check is not inside the name-based checks below because this method performs checks |
| 1764 | // only if the calling user ID is not the same as the owning user ID. |
| 1765 | if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) { |
| 1766 | return false; |
| 1767 | } |
| 1768 | |
| 1769 | switch (name) { |
| 1770 | case "bluetooth_address": |
| 1771 | // BluetoothManagerService for some reason stores the Android's Bluetooth MAC |
| 1772 | // address in this secure setting. Secure settings can normally be read by any app, |
| 1773 | // which thus enables them to bypass the recently introduced restrictions on access |
| 1774 | // to device identifiers. |
| 1775 | // To mitigate this we make this setting available only to callers privileged to see |
| 1776 | // this device's MAC addresses, same as through public API |
| 1777 | // BluetoothAdapter.getAddress() (see BluetoothManagerService for details). |
| 1778 | return getContext().checkCallingOrSelfPermission( |
| 1779 | Manifest.permission.LOCAL_MAC_ADDRESS) == PackageManager.PERMISSION_GRANTED; |
| 1780 | default: |
| 1781 | return true; |
| 1782 | } |
| 1783 | } |
| 1784 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1785 | private boolean isLocationProvidersAllowedRestricted(String name, int callingUserId, |
| 1786 | int owningUserId) { |
| 1787 | // Optimization - location providers are restricted only for managed profiles. |
| 1788 | if (callingUserId == owningUserId) { |
| 1789 | return false; |
| 1790 | } |
| 1791 | if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name) |
| 1792 | && mUserManager.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, |
| 1793 | new UserHandle(callingUserId))) { |
| 1794 | return true; |
| 1795 | } |
| 1796 | return false; |
| 1797 | } |
| 1798 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1799 | private int resolveOwningUserIdForSecureSettingLocked(int userId, String setting) { |
| 1800 | return resolveOwningUserIdLocked(userId, sSecureCloneToManagedSettings, setting); |
| 1801 | } |
| 1802 | |
| 1803 | private int resolveOwningUserIdForSystemSettingLocked(int userId, String setting) { |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 1804 | final int parentId; |
| 1805 | // Resolves dependency if setting has a dependency and the calling user has a parent |
| 1806 | if (sSystemCloneFromParentOnDependency.containsKey(setting) |
| 1807 | && (parentId = getGroupParentLocked(userId)) != userId) { |
| 1808 | // The setting has a dependency and the profile has a parent |
| 1809 | String dependency = sSystemCloneFromParentOnDependency.get(setting); |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1810 | // Lookup the dependency setting as ourselves, some callers may not have access to it. |
| 1811 | final long token = Binder.clearCallingIdentity(); |
| 1812 | try { |
| 1813 | Setting settingObj = getSecureSetting(dependency, userId); |
| 1814 | if (settingObj != null && settingObj.getValue().equals("1")) { |
| 1815 | return parentId; |
| 1816 | } |
| 1817 | } finally { |
| 1818 | Binder.restoreCallingIdentity(token); |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 1819 | } |
| 1820 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1821 | return resolveOwningUserIdLocked(userId, sSystemCloneToManagedSettings, setting); |
| 1822 | } |
| 1823 | |
| 1824 | private int resolveOwningUserIdLocked(int userId, Set<String> keys, String name) { |
| 1825 | final int parentId = getGroupParentLocked(userId); |
| 1826 | if (parentId != userId && keys.contains(name)) { |
| 1827 | return parentId; |
| 1828 | } |
| 1829 | return userId; |
| 1830 | } |
| 1831 | |
Svetoslav | f41334b | 2015-06-23 12:06:03 -0700 | [diff] [blame] | 1832 | private void enforceRestrictedSystemSettingsMutationForCallingPackage(int operation, |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1833 | String name, int userId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1834 | // System/root/shell can mutate whatever secure settings they want. |
| 1835 | final int callingUid = Binder.getCallingUid(); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1836 | final int appId = UserHandle.getAppId(callingUid); |
| 1837 | if (appId == android.os.Process.SYSTEM_UID |
| 1838 | || appId == Process.SHELL_UID |
| 1839 | || appId == Process.ROOT_UID) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1840 | return; |
| 1841 | } |
| 1842 | |
| 1843 | switch (operation) { |
| 1844 | case MUTATION_OPERATION_INSERT: |
| 1845 | // Insert updates. |
| 1846 | case MUTATION_OPERATION_UPDATE: { |
| 1847 | if (Settings.System.PUBLIC_SETTINGS.contains(name)) { |
| 1848 | return; |
| 1849 | } |
| 1850 | |
| 1851 | // The calling package is already verified. |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1852 | PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1853 | |
| 1854 | // Privileged apps can do whatever they want. |
| 1855 | if ((packageInfo.applicationInfo.privateFlags |
| 1856 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
| 1857 | return; |
| 1858 | } |
| 1859 | |
| 1860 | warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk( |
| 1861 | packageInfo.applicationInfo.targetSdkVersion, name); |
| 1862 | } break; |
| 1863 | |
| 1864 | case MUTATION_OPERATION_DELETE: { |
| 1865 | if (Settings.System.PUBLIC_SETTINGS.contains(name) |
| 1866 | || Settings.System.PRIVATE_SETTINGS.contains(name)) { |
| 1867 | throw new IllegalArgumentException("You cannot delete system defined" |
| 1868 | + " secure settings."); |
| 1869 | } |
| 1870 | |
| 1871 | // The calling package is already verified. |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1872 | PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1873 | |
| 1874 | // Privileged apps can do whatever they want. |
| 1875 | if ((packageInfo.applicationInfo.privateFlags & |
| 1876 | ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
| 1877 | return; |
| 1878 | } |
| 1879 | |
| 1880 | warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk( |
| 1881 | packageInfo.applicationInfo.targetSdkVersion, name); |
| 1882 | } break; |
| 1883 | } |
| 1884 | } |
| 1885 | |
Todd Kennedy | be0b889 | 2017-02-15 14:13:52 -0800 | [diff] [blame] | 1886 | private Set<String> getInstantAppAccessibleSettings(int settingsType) { |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1887 | switch (settingsType) { |
| 1888 | case SETTINGS_TYPE_GLOBAL: |
Todd Kennedy | be0b889 | 2017-02-15 14:13:52 -0800 | [diff] [blame] | 1889 | return Settings.Global.INSTANT_APP_SETTINGS; |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1890 | case SETTINGS_TYPE_SECURE: |
Todd Kennedy | be0b889 | 2017-02-15 14:13:52 -0800 | [diff] [blame] | 1891 | return Settings.Secure.INSTANT_APP_SETTINGS; |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1892 | case SETTINGS_TYPE_SYSTEM: |
Todd Kennedy | be0b889 | 2017-02-15 14:13:52 -0800 | [diff] [blame] | 1893 | return Settings.System.INSTANT_APP_SETTINGS; |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1894 | default: |
| 1895 | throw new IllegalArgumentException("Invalid settings type: " + settingsType); |
| 1896 | } |
| 1897 | } |
| 1898 | |
Chad Brubaker | 20e0dc3 | 2017-04-28 18:24:55 -0700 | [diff] [blame] | 1899 | private Set<String> getOverlayInstantAppAccessibleSettings(int settingsType) { |
| 1900 | switch (settingsType) { |
| 1901 | case SETTINGS_TYPE_GLOBAL: |
| 1902 | return OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS; |
| 1903 | case SETTINGS_TYPE_SYSTEM: |
| 1904 | return OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS; |
| 1905 | case SETTINGS_TYPE_SECURE: |
| 1906 | return OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS; |
| 1907 | default: |
| 1908 | throw new IllegalArgumentException("Invalid settings type: " + settingsType); |
| 1909 | } |
| 1910 | } |
| 1911 | |
Andreas Gampe | b5889307 | 2018-09-05 16:52:31 -0700 | [diff] [blame] | 1912 | @GuardedBy("mLock") |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1913 | private List<String> getSettingsNamesLocked(int settingsType, int userId) { |
Chad Brubaker | b6108d6 | 2017-12-23 20:06:44 -0800 | [diff] [blame] | 1914 | // Don't enforce the instant app whitelist for now -- its too prone to unintended breakage |
| 1915 | // in the current form. |
| 1916 | return mSettingsRegistry.getSettingsNamesLocked(settingsType, userId); |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1917 | } |
| 1918 | |
Chad Brubaker | a6830e7 | 2017-04-28 17:34:36 -0700 | [diff] [blame] | 1919 | private void enforceSettingReadable(String settingName, int settingsType, int userId) { |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1920 | if (UserHandle.getAppId(Binder.getCallingUid()) < Process.FIRST_APPLICATION_UID) { |
| 1921 | return; |
| 1922 | } |
Chad Brubaker | f0fa853 | 2017-02-23 10:45:20 -0800 | [diff] [blame] | 1923 | ApplicationInfo ai = getCallingApplicationInfoOrThrow(); |
Svetoslav Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 1924 | if (!ai.isInstantApp()) { |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1925 | return; |
| 1926 | } |
Chad Brubaker | 20e0dc3 | 2017-04-28 18:24:55 -0700 | [diff] [blame] | 1927 | if (!getInstantAppAccessibleSettings(settingsType).contains(settingName) |
| 1928 | && !getOverlayInstantAppAccessibleSettings(settingsType).contains(settingName)) { |
Chad Brubaker | b6108d6 | 2017-12-23 20:06:44 -0800 | [diff] [blame] | 1929 | // Don't enforce the instant app whitelist for now -- its too prone to unintended |
| 1930 | // breakage in the current form. |
| 1931 | Slog.w(LOG_TAG, "Instant App " + ai.packageName |
| 1932 | + " trying to access unexposed setting, this will be an error in the future."); |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1933 | } |
| 1934 | } |
| 1935 | |
Chad Brubaker | f0fa853 | 2017-02-23 10:45:20 -0800 | [diff] [blame] | 1936 | private ApplicationInfo getCallingApplicationInfoOrThrow() { |
| 1937 | // We always use the callingUid for this lookup. This means that if hypothetically an |
| 1938 | // app was installed in user A with cross user and in user B as an Instant App |
| 1939 | // the app in A would be able to see all the settings in user B. However since cross |
| 1940 | // user is a system permission and the app must be uninstalled in B and then installed as |
| 1941 | // an Instant App that situation is not realistic or supported. |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1942 | ApplicationInfo ai = null; |
| 1943 | try { |
Chad Brubaker | f0fa853 | 2017-02-23 10:45:20 -0800 | [diff] [blame] | 1944 | ai = mPackageManager.getApplicationInfo(getCallingPackage(), 0 |
| 1945 | , UserHandle.getCallingUserId()); |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1946 | } catch (RemoteException ignored) { |
| 1947 | } |
| 1948 | if (ai == null) { |
| 1949 | throw new IllegalStateException("Failed to lookup info for package " |
| 1950 | + getCallingPackage()); |
| 1951 | } |
| 1952 | return ai; |
| 1953 | } |
| 1954 | |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1955 | private PackageInfo getCallingPackageInfoOrThrow(int userId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1956 | try { |
Svetoslav Ganov | 67a8d35 | 2016-03-02 13:26:40 -0800 | [diff] [blame] | 1957 | PackageInfo packageInfo = mPackageManager.getPackageInfo( |
| 1958 | getCallingPackage(), 0, userId); |
| 1959 | if (packageInfo != null) { |
| 1960 | return packageInfo; |
| 1961 | } |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1962 | } catch (RemoteException e) { |
Svetoslav Ganov | 67a8d35 | 2016-03-02 13:26:40 -0800 | [diff] [blame] | 1963 | /* ignore */ |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1964 | } |
Svetoslav Ganov | 67a8d35 | 2016-03-02 13:26:40 -0800 | [diff] [blame] | 1965 | throw new IllegalStateException("Calling package doesn't exist"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | private int getGroupParentLocked(int userId) { |
| 1969 | // Most frequent use case. |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1970 | if (userId == UserHandle.USER_SYSTEM) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1971 | return userId; |
| 1972 | } |
| 1973 | // We are in the same process with the user manager and the returned |
| 1974 | // user info is a cached instance, so just look up instead of cache. |
| 1975 | final long identity = Binder.clearCallingIdentity(); |
| 1976 | try { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1977 | // Just a lookup and not reentrant, so holding a lock is fine. |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1978 | UserInfo userInfo = mUserManager.getProfileParent(userId); |
| 1979 | return (userInfo != null) ? userInfo.id : userId; |
| 1980 | } finally { |
| 1981 | Binder.restoreCallingIdentity(identity); |
| 1982 | } |
| 1983 | } |
| 1984 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1985 | private void enforceWritePermission(String permission) { |
| 1986 | if (getContext().checkCallingOrSelfPermission(permission) |
| 1987 | != PackageManager.PERMISSION_GRANTED) { |
| 1988 | throw new SecurityException("Permission denial: writing to settings requires:" |
| 1989 | + permission); |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | /* |
| 1994 | * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED. |
| 1995 | * This setting contains a list of the currently enabled location providers. |
| 1996 | * But helper functions in android.providers.Settings can enable or disable |
| 1997 | * a single provider by using a "+" or "-" prefix before the provider name. |
| 1998 | * |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 1999 | * <p>See also {@link com.android.server.pm.UserRestrictionsUtils#isSettingRestrictedForUser()}. |
| 2000 | * If DISALLOW_SHARE_LOCATION is set, the said method will only allow values with |
| 2001 | * the "-" prefix. |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 2002 | * |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2003 | * @returns whether the enabled location providers changed. |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2004 | */ |
Andreas Gampe | b5889307 | 2018-09-05 16:52:31 -0700 | [diff] [blame] | 2005 | @GuardedBy("mLock") |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2006 | private boolean updateLocationProvidersAllowedLocked(String value, String tag, |
| 2007 | int owningUserId, boolean makeDefault, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2008 | if (TextUtils.isEmpty(value)) { |
| 2009 | return false; |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 2010 | } |
Maggie | 83e03f5 | 2018-03-16 12:22:20 -0700 | [diff] [blame] | 2011 | Setting oldSetting = getSecureSetting( |
| 2012 | Settings.Secure.LOCATION_PROVIDERS_ALLOWED, owningUserId); |
| 2013 | if (oldSetting == null) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2014 | return false; |
| 2015 | } |
Maggie | 83e03f5 | 2018-03-16 12:22:20 -0700 | [diff] [blame] | 2016 | String oldProviders = oldSetting.getValue(); |
| 2017 | List<String> oldProvidersList = TextUtils.isEmpty(oldProviders) |
| 2018 | ? new ArrayList<>() : new ArrayList<>(Arrays.asList(oldProviders.split(","))); |
| 2019 | Set<String> newProvidersSet = new ArraySet<>(); |
| 2020 | newProvidersSet.addAll(oldProvidersList); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2021 | |
Maggie | 83e03f5 | 2018-03-16 12:22:20 -0700 | [diff] [blame] | 2022 | String[] providerUpdates = value.split(","); |
| 2023 | boolean inputError = false; |
| 2024 | for (String provider : providerUpdates) { |
| 2025 | // do not update location_providers_allowed when input is invalid |
| 2026 | if (TextUtils.isEmpty(provider)) { |
| 2027 | inputError = true; |
| 2028 | break; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2029 | } |
Maggie | 83e03f5 | 2018-03-16 12:22:20 -0700 | [diff] [blame] | 2030 | final char prefix = provider.charAt(0); |
| 2031 | // do not update location_providers_allowed when input is invalid |
| 2032 | if (prefix != '+' && prefix != '-') { |
| 2033 | inputError = true; |
| 2034 | break; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2035 | } |
Maggie | 83e03f5 | 2018-03-16 12:22:20 -0700 | [diff] [blame] | 2036 | // skip prefix |
| 2037 | provider = provider.substring(1); |
| 2038 | if (prefix == '+') { |
| 2039 | newProvidersSet.add(provider); |
| 2040 | } else if (prefix == '-') { |
| 2041 | newProvidersSet.remove(provider); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2042 | } |
Maggie | 83e03f5 | 2018-03-16 12:22:20 -0700 | [diff] [blame] | 2043 | } |
| 2044 | String newProviders = TextUtils.join(",", newProvidersSet.toArray()); |
| 2045 | if (inputError == true || newProviders.equals(oldProviders)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2046 | // nothing changed, so no need to update the database |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2047 | if (forceNotify) { |
Maggie | 83e03f5 | 2018-03-16 12:22:20 -0700 | [diff] [blame] | 2048 | mSettingsRegistry.notifyForSettingsChange( |
| 2049 | makeKey(SETTINGS_TYPE_SECURE, owningUserId), |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2050 | Settings.Secure.LOCATION_PROVIDERS_ALLOWED); |
| 2051 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2052 | return false; |
| 2053 | } |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2054 | return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE, |
Maggie | 83e03f5 | 2018-03-16 12:22:20 -0700 | [diff] [blame] | 2055 | owningUserId, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, newProviders, tag, |
| 2056 | makeDefault, getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2057 | } |
| 2058 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2059 | private static void warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk( |
| 2060 | int targetSdkVersion, String name) { |
| 2061 | // If the app targets Lollipop MR1 or older SDK we warn, otherwise crash. |
| 2062 | if (targetSdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) { |
| 2063 | if (Settings.System.PRIVATE_SETTINGS.contains(name)) { |
| 2064 | Slog.w(LOG_TAG, "You shouldn't not change private system settings." |
| 2065 | + " This will soon become an error."); |
| 2066 | } else { |
| 2067 | Slog.w(LOG_TAG, "You shouldn't keep your settings in the secure settings." |
| 2068 | + " This will soon become an error."); |
| 2069 | } |
| 2070 | } else { |
| 2071 | if (Settings.System.PRIVATE_SETTINGS.contains(name)) { |
| 2072 | throw new IllegalArgumentException("You cannot change private secure settings."); |
| 2073 | } else { |
| 2074 | throw new IllegalArgumentException("You cannot keep your settings in" |
| 2075 | + " the secure settings."); |
| 2076 | } |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | private static int resolveCallingUserIdEnforcingPermissionsLocked(int requestingUserId) { |
| 2081 | if (requestingUserId == UserHandle.getCallingUserId()) { |
| 2082 | return requestingUserId; |
| 2083 | } |
| 2084 | return ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2085 | Binder.getCallingUid(), requestingUserId, false, true, |
| 2086 | "get/set setting for user", null); |
| 2087 | } |
| 2088 | |
Matt Pape | 793b15c | 2019-10-07 13:17:20 -0700 | [diff] [blame] | 2089 | private Bundle packageValueForCallResult(Setting setting, boolean trackingGeneration) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2090 | if (!trackingGeneration) { |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2091 | if (setting == null || setting.isNull()) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2092 | return NULL_SETTING_BUNDLE; |
| 2093 | } |
| 2094 | return Bundle.forPair(Settings.NameValueTable.VALUE, setting.getValue()); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2095 | } |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2096 | Bundle result = new Bundle(); |
| 2097 | result.putString(Settings.NameValueTable.VALUE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2098 | !setting.isNull() ? setting.getValue() : null); |
Svet Ganov | 96c9946 | 2017-05-05 14:27:13 -0700 | [diff] [blame] | 2099 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2100 | mSettingsRegistry.mGenerationRegistry.addGenerationData(result, setting.getKey()); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2101 | return result; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2102 | } |
| 2103 | |
Matt Pape | 793b15c | 2019-10-07 13:17:20 -0700 | [diff] [blame] | 2104 | private Bundle packageValuesForCallResult(HashMap<String, String> keyValues, |
| 2105 | boolean trackingGeneration) { |
| 2106 | Bundle result = new Bundle(); |
| 2107 | result.putSerializable(Settings.NameValueTable.VALUE, keyValues); |
| 2108 | if (trackingGeneration) { |
| 2109 | synchronized (mLock) { |
| 2110 | mSettingsRegistry.mGenerationRegistry.addGenerationData(result, |
| 2111 | mSettingsRegistry.getSettingsLocked( |
| 2112 | SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM).mKey); |
| 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | return result; |
| 2117 | } |
| 2118 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2119 | private static int getRequestingUserId(Bundle args) { |
| 2120 | final int callingUserId = UserHandle.getCallingUserId(); |
| 2121 | return (args != null) ? args.getInt(Settings.CALL_METHOD_USER_KEY, callingUserId) |
| 2122 | : callingUserId; |
| 2123 | } |
| 2124 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2125 | private boolean isTrackingGeneration(Bundle args) { |
| 2126 | return args != null && args.containsKey(Settings.CALL_METHOD_TRACK_GENERATION_KEY); |
| 2127 | } |
| 2128 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2129 | private static String getSettingValue(Bundle args) { |
| 2130 | return (args != null) ? args.getString(Settings.NameValueTable.VALUE) : null; |
| 2131 | } |
| 2132 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2133 | private static String getSettingTag(Bundle args) { |
| 2134 | return (args != null) ? args.getString(Settings.CALL_METHOD_TAG_KEY) : null; |
| 2135 | } |
| 2136 | |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 2137 | private static String getSettingPrefix(Bundle args) { |
Matt Pape | c600153 | 2019-10-24 15:50:32 -0700 | [diff] [blame] | 2138 | return (args != null) ? args.getString(Settings.CALL_METHOD_PREFIX_KEY) : null; |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 2139 | } |
| 2140 | |
Matt Pape | 4bd0a6a | 2019-11-19 12:17:45 -0800 | [diff] [blame^] | 2141 | private static Map<String, String> getSettingFlags(Bundle args) { |
| 2142 | return (args != null) ? (HashMap) args.getSerializable(Settings.CALL_METHOD_FLAGS_KEY) |
| 2143 | : Collections.emptyMap(); |
| 2144 | } |
| 2145 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2146 | private static boolean getSettingMakeDefault(Bundle args) { |
| 2147 | return (args != null) && args.getBoolean(Settings.CALL_METHOD_MAKE_DEFAULT_KEY); |
| 2148 | } |
| 2149 | |
| 2150 | private static int getResetModeEnforcingPermission(Bundle args) { |
| 2151 | final int mode = (args != null) ? args.getInt(Settings.CALL_METHOD_RESET_MODE_KEY) : 0; |
| 2152 | switch (mode) { |
| 2153 | case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: { |
| 2154 | if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) { |
| 2155 | throw new SecurityException("Only system, shell/root on a " |
| 2156 | + "debuggable build can reset to untrusted defaults"); |
| 2157 | } |
| 2158 | return mode; |
| 2159 | } |
| 2160 | case Settings.RESET_MODE_UNTRUSTED_CHANGES: { |
| 2161 | if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) { |
| 2162 | throw new SecurityException("Only system, shell/root on a " |
| 2163 | + "debuggable build can reset untrusted changes"); |
| 2164 | } |
| 2165 | return mode; |
| 2166 | } |
| 2167 | case Settings.RESET_MODE_TRUSTED_DEFAULTS: { |
| 2168 | if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) { |
| 2169 | throw new SecurityException("Only system, shell/root on a " |
| 2170 | + "debuggable build can reset to trusted defaults"); |
| 2171 | } |
| 2172 | return mode; |
| 2173 | } |
| 2174 | case Settings.RESET_MODE_PACKAGE_DEFAULTS: { |
| 2175 | return mode; |
| 2176 | } |
| 2177 | } |
| 2178 | throw new IllegalArgumentException("Invalid reset mode: " + mode); |
| 2179 | } |
| 2180 | |
| 2181 | private static boolean isCallerSystemOrShellOrRootOnDebuggableBuild() { |
| 2182 | final int appId = UserHandle.getAppId(Binder.getCallingUid()); |
| 2183 | return appId == SYSTEM_UID || (Build.IS_DEBUGGABLE |
| 2184 | && (appId == SHELL_UID || appId == ROOT_UID)); |
| 2185 | } |
| 2186 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2187 | private static String getValidTableOrThrow(Uri uri) { |
| 2188 | if (uri.getPathSegments().size() > 0) { |
| 2189 | String table = uri.getPathSegments().get(0); |
| 2190 | if (DatabaseHelper.isValidTable(table)) { |
| 2191 | return table; |
| 2192 | } |
| 2193 | throw new IllegalArgumentException("Bad root path: " + table); |
| 2194 | } |
| 2195 | throw new IllegalArgumentException("Invalid URI:" + uri); |
| 2196 | } |
| 2197 | |
| 2198 | private static MatrixCursor packageSettingForQuery(Setting setting, String[] projection) { |
Svetoslav Ganov | fedb230 | 2016-04-26 18:36:42 -0700 | [diff] [blame] | 2199 | if (setting.isNull()) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2200 | return new MatrixCursor(projection, 0); |
| 2201 | } |
| 2202 | MatrixCursor cursor = new MatrixCursor(projection, 1); |
| 2203 | appendSettingToCursor(cursor, setting); |
| 2204 | return cursor; |
| 2205 | } |
| 2206 | |
| 2207 | private static String[] normalizeProjection(String[] projection) { |
| 2208 | if (projection == null) { |
| 2209 | return ALL_COLUMNS; |
| 2210 | } |
| 2211 | |
| 2212 | final int columnCount = projection.length; |
| 2213 | for (int i = 0; i < columnCount; i++) { |
| 2214 | String column = projection[i]; |
| 2215 | if (!ArrayUtils.contains(ALL_COLUMNS, column)) { |
| 2216 | throw new IllegalArgumentException("Invalid column: " + column); |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 2217 | } |
| 2218 | } |
| 2219 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2220 | return projection; |
| 2221 | } |
| 2222 | |
| 2223 | private static void appendSettingToCursor(MatrixCursor cursor, Setting setting) { |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2224 | if (setting == null || setting.isNull()) { |
Suprabh Shukla | c9d064a | 2016-04-12 18:45:34 -0700 | [diff] [blame] | 2225 | return; |
| 2226 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2227 | final int columnCount = cursor.getColumnCount(); |
| 2228 | |
| 2229 | String[] values = new String[columnCount]; |
| 2230 | |
| 2231 | for (int i = 0; i < columnCount; i++) { |
| 2232 | String column = cursor.getColumnName(i); |
| 2233 | |
| 2234 | switch (column) { |
| 2235 | case Settings.NameValueTable._ID: { |
| 2236 | values[i] = setting.getId(); |
| 2237 | } break; |
| 2238 | |
| 2239 | case Settings.NameValueTable.NAME: { |
| 2240 | values[i] = setting.getName(); |
| 2241 | } break; |
| 2242 | |
| 2243 | case Settings.NameValueTable.VALUE: { |
| 2244 | values[i] = setting.getValue(); |
| 2245 | } break; |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 2246 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2247 | } |
| 2248 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2249 | cursor.addRow(values); |
| 2250 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2251 | |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 2252 | private static boolean isKeyValid(String key) { |
| 2253 | return !(TextUtils.isEmpty(key) || SettingsState.isBinary(key)); |
| 2254 | } |
| 2255 | |
Matt Pape | 7b1c6cd | 2019-01-04 08:10:41 -0800 | [diff] [blame] | 2256 | private String resolveCallingPackage() { |
| 2257 | switch (Binder.getCallingUid()) { |
| 2258 | case Process.ROOT_UID: { |
| 2259 | return "root"; |
| 2260 | } |
| 2261 | |
| 2262 | case Process.SHELL_UID: { |
| 2263 | return "com.android.shell"; |
| 2264 | } |
| 2265 | |
| 2266 | default: { |
| 2267 | return getCallingPackage(); |
| 2268 | } |
| 2269 | } |
| 2270 | } |
| 2271 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2272 | private static final class Arguments { |
| 2273 | private static final Pattern WHERE_PATTERN_WITH_PARAM_NO_BRACKETS = |
| 2274 | Pattern.compile("[\\s]*name[\\s]*=[\\s]*\\?[\\s]*"); |
| 2275 | |
| 2276 | private static final Pattern WHERE_PATTERN_WITH_PARAM_IN_BRACKETS = |
| 2277 | Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*\\?[\\s]*\\)[\\s]*"); |
| 2278 | |
| 2279 | private static final Pattern WHERE_PATTERN_NO_PARAM_IN_BRACKETS = |
| 2280 | Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*\\)[\\s]*"); |
| 2281 | |
| 2282 | private static final Pattern WHERE_PATTERN_NO_PARAM_NO_BRACKETS = |
| 2283 | Pattern.compile("[\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*"); |
| 2284 | |
| 2285 | public final String table; |
| 2286 | public final String name; |
| 2287 | |
| 2288 | public Arguments(Uri uri, String where, String[] whereArgs, boolean supportAll) { |
| 2289 | final int segmentSize = uri.getPathSegments().size(); |
| 2290 | switch (segmentSize) { |
| 2291 | case 1: { |
| 2292 | if (where != null |
| 2293 | && (WHERE_PATTERN_WITH_PARAM_NO_BRACKETS.matcher(where).matches() |
| 2294 | || WHERE_PATTERN_WITH_PARAM_IN_BRACKETS.matcher(where).matches()) |
| 2295 | && whereArgs.length == 1) { |
| 2296 | name = whereArgs[0]; |
| 2297 | table = computeTableForSetting(uri, name); |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 2298 | return; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2299 | } else if (where != null |
| 2300 | && (WHERE_PATTERN_NO_PARAM_NO_BRACKETS.matcher(where).matches() |
| 2301 | || WHERE_PATTERN_NO_PARAM_IN_BRACKETS.matcher(where).matches())) { |
| 2302 | final int startIndex = Math.max(where.indexOf("'"), |
| 2303 | where.indexOf("\"")) + 1; |
| 2304 | final int endIndex = Math.max(where.lastIndexOf("'"), |
| 2305 | where.lastIndexOf("\"")); |
| 2306 | name = where.substring(startIndex, endIndex); |
| 2307 | table = computeTableForSetting(uri, name); |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 2308 | return; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2309 | } else if (supportAll && where == null && whereArgs == null) { |
| 2310 | name = null; |
| 2311 | table = computeTableForSetting(uri, null); |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 2312 | return; |
Brad Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame] | 2313 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2314 | } break; |
| 2315 | |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 2316 | case 2: { |
| 2317 | if (where == null && whereArgs == null) { |
| 2318 | name = uri.getPathSegments().get(1); |
| 2319 | table = computeTableForSetting(uri, name); |
| 2320 | return; |
| 2321 | } |
| 2322 | } break; |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2323 | } |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 2324 | |
| 2325 | EventLogTags.writeUnsupportedSettingsQuery( |
| 2326 | uri.toSafeString(), where, Arrays.toString(whereArgs)); |
| 2327 | String message = String.format( "Supported SQL:\n" |
| 2328 | + " uri content://some_table/some_property with null where and where args\n" |
| 2329 | + " uri content://some_table with query name=? and single name as arg\n" |
| 2330 | + " uri content://some_table with query name=some_name and null args\n" |
| 2331 | + " but got - uri:%1s, where:%2s whereArgs:%3s", uri, where, |
| 2332 | Arrays.toString(whereArgs)); |
| 2333 | throw new IllegalArgumentException(message); |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2334 | } |
| 2335 | |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 2336 | private static String computeTableForSetting(Uri uri, String name) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2337 | String table = getValidTableOrThrow(uri); |
| 2338 | |
| 2339 | if (name != null) { |
| 2340 | if (sSystemMovedToSecureSettings.contains(name)) { |
| 2341 | table = TABLE_SECURE; |
| 2342 | } |
| 2343 | |
| 2344 | if (sSystemMovedToGlobalSettings.contains(name)) { |
| 2345 | table = TABLE_GLOBAL; |
| 2346 | } |
| 2347 | |
| 2348 | if (sSecureMovedToGlobalSettings.contains(name)) { |
| 2349 | table = TABLE_GLOBAL; |
| 2350 | } |
| 2351 | |
| 2352 | if (sGlobalMovedToSecureSettings.contains(name)) { |
| 2353 | table = TABLE_SECURE; |
| 2354 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2355 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2356 | |
| 2357 | return table; |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | final class SettingsRegistry { |
| 2362 | private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid"; |
| 2363 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2364 | private static final String SETTINGS_FILE_GLOBAL = "settings_global.xml"; |
| 2365 | private static final String SETTINGS_FILE_SYSTEM = "settings_system.xml"; |
| 2366 | private static final String SETTINGS_FILE_SECURE = "settings_secure.xml"; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2367 | private static final String SETTINGS_FILE_SSAID = "settings_ssaid.xml"; |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 2368 | private static final String SETTINGS_FILE_CONFIG = "settings_config.xml"; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2369 | |
| 2370 | private static final String SSAID_USER_KEY = "userkey"; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2371 | |
| 2372 | private final SparseArray<SettingsState> mSettingsStates = new SparseArray<>(); |
| 2373 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2374 | private GenerationRegistry mGenerationRegistry; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2375 | |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 2376 | private final Handler mHandler; |
| 2377 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2378 | private final BackupManager mBackupManager; |
| 2379 | |
Amith Yamasani | 3945202 | 2017-03-21 15:23:47 -0700 | [diff] [blame] | 2380 | private String mSettingsCreationBuildId; |
| 2381 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2382 | public SettingsRegistry() { |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 2383 | mHandler = new MyHandler(getContext().getMainLooper()); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2384 | mGenerationRegistry = new GenerationRegistry(mLock); |
| 2385 | mBackupManager = new BackupManager(getContext()); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2386 | migrateAllLegacySettingsIfNeeded(); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2387 | syncSsaidTableOnStart(); |
| 2388 | } |
| 2389 | |
| 2390 | private void generateUserKeyLocked(int userId) { |
| 2391 | // Generate a random key for each user used for creating a new ssaid. |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 2392 | final byte[] keyBytes = new byte[32]; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2393 | final SecureRandom rand = new SecureRandom(); |
| 2394 | rand.nextBytes(keyBytes); |
| 2395 | |
| 2396 | // Convert to string for storage in settings table. |
Neil Fuller | 3447fb4 | 2019-04-08 22:03:14 +0100 | [diff] [blame] | 2397 | final String userKey = HexEncoding.encodeToString(keyBytes, true /* upperCase */); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2398 | |
| 2399 | // Store the key in the ssaid table. |
| 2400 | final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId); |
| 2401 | final boolean success = ssaidSettings.insertSettingLocked(SSAID_USER_KEY, userKey, null, |
| 2402 | true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 2403 | |
| 2404 | if (!success) { |
| 2405 | throw new IllegalStateException("Ssaid settings not accessible"); |
| 2406 | } |
| 2407 | } |
| 2408 | |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 2409 | private byte[] getLengthPrefix(byte[] data) { |
| 2410 | return ByteBuffer.allocate(4).putInt(data.length).array(); |
| 2411 | } |
| 2412 | |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 2413 | public Setting generateSsaidLocked(PackageInfo callingPkg, int userId) { |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2414 | // Read the user's key from the ssaid table. |
| 2415 | Setting userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY); |
Mark Rathjen | ea61759 | 2017-01-18 23:03:41 -0800 | [diff] [blame] | 2416 | if (userKeySetting == null || userKeySetting.isNull() |
| 2417 | || userKeySetting.getValue() == null) { |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2418 | // Lazy initialize and store the user key. |
| 2419 | generateUserKeyLocked(userId); |
| 2420 | userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY); |
Mark Rathjen | ea61759 | 2017-01-18 23:03:41 -0800 | [diff] [blame] | 2421 | if (userKeySetting == null || userKeySetting.isNull() |
| 2422 | || userKeySetting.getValue() == null) { |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2423 | throw new IllegalStateException("User key not accessible"); |
| 2424 | } |
| 2425 | } |
| 2426 | final String userKey = userKeySetting.getValue(); |
Neil Fuller | 3447fb4 | 2019-04-08 22:03:14 +0100 | [diff] [blame] | 2427 | if (userKey == null || userKey.length() % 2 != 0) { |
| 2428 | throw new IllegalStateException("User key invalid"); |
| 2429 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2430 | |
| 2431 | // Convert the user's key back to a byte array. |
Neil Fuller | 3447fb4 | 2019-04-08 22:03:14 +0100 | [diff] [blame] | 2432 | final byte[] keyBytes = HexEncoding.decode(userKey); |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 2433 | |
| 2434 | // Validate that the key is of expected length. |
| 2435 | // Keys are currently 32 bytes, but were once 16 bytes during Android O development. |
Neil Fuller | 3447fb4 | 2019-04-08 22:03:14 +0100 | [diff] [blame] | 2436 | if (keyBytes.length != 16 && keyBytes.length != 32) { |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2437 | throw new IllegalStateException("User key invalid"); |
| 2438 | } |
| 2439 | |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 2440 | final Mac m; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2441 | try { |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 2442 | m = Mac.getInstance("HmacSHA256"); |
| 2443 | m.init(new SecretKeySpec(keyBytes, m.getAlgorithm())); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2444 | } catch (NoSuchAlgorithmException e) { |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 2445 | throw new IllegalStateException("HmacSHA256 is not available", e); |
| 2446 | } catch (InvalidKeyException e) { |
| 2447 | throw new IllegalStateException("Key is corrupted", e); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2448 | } |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 2449 | |
Mark Rathjen | f42dd91 | 2017-06-05 19:04:34 -0700 | [diff] [blame] | 2450 | // Mac each of the developer signatures. |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 2451 | for (int i = 0; i < callingPkg.signatures.length; i++) { |
| 2452 | byte[] sig = callingPkg.signatures[i].toByteArray(); |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 2453 | m.update(getLengthPrefix(sig), 0, 4); |
| 2454 | m.update(sig); |
| 2455 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2456 | |
| 2457 | // Convert result to a string for storage in settings table. Only want first 64 bits. |
Neil Fuller | 3447fb4 | 2019-04-08 22:03:14 +0100 | [diff] [blame] | 2458 | final String ssaid = HexEncoding.encodeToString(m.doFinal(), false /* upperCase */) |
| 2459 | .substring(0, 16); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2460 | |
| 2461 | // Save the ssaid in the ssaid table. |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 2462 | final String uid = Integer.toString(callingPkg.applicationInfo.uid); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2463 | final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId); |
| 2464 | final boolean success = ssaidSettings.insertSettingLocked(uid, ssaid, null, true, |
Mark Rathjen | f42dd91 | 2017-06-05 19:04:34 -0700 | [diff] [blame] | 2465 | callingPkg.packageName); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2466 | |
| 2467 | if (!success) { |
| 2468 | throw new IllegalStateException("Ssaid settings not accessible"); |
| 2469 | } |
| 2470 | |
| 2471 | return getSettingLocked(SETTINGS_TYPE_SSAID, userId, uid); |
| 2472 | } |
| 2473 | |
| 2474 | public void syncSsaidTableOnStart() { |
| 2475 | synchronized (mLock) { |
| 2476 | // Verify that each user's packages and ssaid's are in sync. |
| 2477 | for (UserInfo user : mUserManager.getUsers(true)) { |
| 2478 | // Get all uids for the user's packages. |
| 2479 | final List<PackageInfo> packages; |
| 2480 | try { |
Tetsutoki Shiozawa | ebe0e5f | 2018-01-17 11:07:09 +0900 | [diff] [blame] | 2481 | packages = mPackageManager.getInstalledPackages( |
| 2482 | PackageManager.MATCH_UNINSTALLED_PACKAGES, |
| 2483 | user.id).getList(); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2484 | } catch (RemoteException e) { |
| 2485 | throw new IllegalStateException("Package manager not available"); |
| 2486 | } |
| 2487 | final Set<String> appUids = new HashSet<>(); |
| 2488 | for (PackageInfo info : packages) { |
| 2489 | appUids.add(Integer.toString(info.applicationInfo.uid)); |
| 2490 | } |
| 2491 | |
| 2492 | // Get all uids currently stored in the user's ssaid table. |
| 2493 | final Set<String> ssaidUids = new HashSet<>( |
| 2494 | getSettingsNamesLocked(SETTINGS_TYPE_SSAID, user.id)); |
| 2495 | ssaidUids.remove(SSAID_USER_KEY); |
| 2496 | |
| 2497 | // Perform a set difference for the appUids and ssaidUids. |
| 2498 | ssaidUids.removeAll(appUids); |
| 2499 | |
| 2500 | // If there are ssaidUids left over they need to be removed from the table. |
| 2501 | final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, |
| 2502 | user.id); |
| 2503 | for (String uid : ssaidUids) { |
| 2504 | ssaidSettings.deleteSettingLocked(uid); |
| 2505 | } |
| 2506 | } |
| 2507 | } |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 2508 | } |
| 2509 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2510 | public List<String> getSettingsNamesLocked(int type, int userId) { |
| 2511 | final int key = makeKey(type, userId); |
| 2512 | SettingsState settingsState = peekSettingsStateLocked(key); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2513 | if (settingsState == null) { |
Matt Pape | 4bd0a6a | 2019-11-19 12:17:45 -0800 | [diff] [blame^] | 2514 | return new ArrayList<>(); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2515 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2516 | return settingsState.getSettingNamesLocked(); |
| 2517 | } |
| 2518 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2519 | public SparseBooleanArray getKnownUsersLocked() { |
| 2520 | SparseBooleanArray users = new SparseBooleanArray(); |
| 2521 | for (int i = mSettingsStates.size()-1; i >= 0; i--) { |
| 2522 | users.put(getUserIdFromKey(mSettingsStates.keyAt(i)), true); |
| 2523 | } |
| 2524 | return users; |
| 2525 | } |
| 2526 | |
Kweku Adams | b0886f3 | 2017-10-31 15:32:09 -0700 | [diff] [blame] | 2527 | @Nullable |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2528 | public SettingsState getSettingsLocked(int type, int userId) { |
| 2529 | final int key = makeKey(type, userId); |
| 2530 | return peekSettingsStateLocked(key); |
| 2531 | } |
| 2532 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2533 | public boolean ensureSettingsForUserLocked(int userId) { |
| 2534 | // First make sure this user actually exists. |
| 2535 | if (mUserManager.getUserInfo(userId) == null) { |
| 2536 | Slog.wtf(LOG_TAG, "Requested user " + userId + " does not exist"); |
| 2537 | return false; |
| 2538 | } |
| 2539 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2540 | // Migrate the setting for this user if needed. |
| 2541 | migrateLegacySettingsForUserIfNeededLocked(userId); |
| 2542 | |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 2543 | // Ensure config settings loaded if owner. |
| 2544 | if (userId == UserHandle.USER_SYSTEM) { |
| 2545 | final int configKey |
| 2546 | = makeKey(SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM); |
| 2547 | ensureSettingsStateLocked(configKey); |
| 2548 | } |
| 2549 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2550 | // Ensure global settings loaded if owner. |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 2551 | if (userId == UserHandle.USER_SYSTEM) { |
| 2552 | final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2553 | ensureSettingsStateLocked(globalKey); |
| 2554 | } |
| 2555 | |
| 2556 | // Ensure secure settings loaded. |
| 2557 | final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId); |
| 2558 | ensureSettingsStateLocked(secureKey); |
| 2559 | |
| 2560 | // Make sure the secure settings have an Android id set. |
| 2561 | SettingsState secureSettings = getSettingsLocked(SETTINGS_TYPE_SECURE, userId); |
| 2562 | ensureSecureSettingAndroidIdSetLocked(secureSettings); |
| 2563 | |
| 2564 | // Ensure system settings loaded. |
| 2565 | final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId); |
| 2566 | ensureSettingsStateLocked(systemKey); |
| 2567 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2568 | // Ensure secure settings loaded. |
| 2569 | final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId); |
| 2570 | ensureSettingsStateLocked(ssaidKey); |
| 2571 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2572 | // Upgrade the settings to the latest version. |
| 2573 | UpgradeController upgrader = new UpgradeController(userId); |
| 2574 | upgrader.upgradeIfNeededLocked(); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2575 | return true; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | private void ensureSettingsStateLocked(int key) { |
| 2579 | if (mSettingsStates.get(key) == null) { |
| 2580 | final int maxBytesPerPackage = getMaxBytesPerPackageForType(getTypeFromKey(key)); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2581 | SettingsState settingsState = new SettingsState(getContext(), mLock, |
| 2582 | getSettingsFile(key), key, maxBytesPerPackage, mHandlerThread.getLooper()); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2583 | mSettingsStates.put(key, settingsState); |
| 2584 | } |
| 2585 | } |
| 2586 | |
| 2587 | public void removeUserStateLocked(int userId, boolean permanently) { |
| 2588 | // We always keep the global settings in memory. |
| 2589 | |
| 2590 | // Nuke system settings. |
| 2591 | final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId); |
| 2592 | final SettingsState systemSettingsState = mSettingsStates.get(systemKey); |
| 2593 | if (systemSettingsState != null) { |
| 2594 | if (permanently) { |
| 2595 | mSettingsStates.remove(systemKey); |
| 2596 | systemSettingsState.destroyLocked(null); |
Brad Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame] | 2597 | } else { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2598 | systemSettingsState.destroyLocked(new Runnable() { |
| 2599 | @Override |
| 2600 | public void run() { |
| 2601 | mSettingsStates.remove(systemKey); |
| 2602 | } |
| 2603 | }); |
| 2604 | } |
| 2605 | } |
| 2606 | |
| 2607 | // Nuke secure settings. |
| 2608 | final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId); |
| 2609 | final SettingsState secureSettingsState = mSettingsStates.get(secureKey); |
| 2610 | if (secureSettingsState != null) { |
| 2611 | if (permanently) { |
| 2612 | mSettingsStates.remove(secureKey); |
| 2613 | secureSettingsState.destroyLocked(null); |
| 2614 | } else { |
| 2615 | secureSettingsState.destroyLocked(new Runnable() { |
| 2616 | @Override |
| 2617 | public void run() { |
| 2618 | mSettingsStates.remove(secureKey); |
| 2619 | } |
| 2620 | }); |
Brad Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame] | 2621 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2622 | } |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2623 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2624 | // Nuke ssaid settings. |
| 2625 | final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId); |
| 2626 | final SettingsState ssaidSettingsState = mSettingsStates.get(ssaidKey); |
| 2627 | if (ssaidSettingsState != null) { |
| 2628 | if (permanently) { |
| 2629 | mSettingsStates.remove(ssaidKey); |
| 2630 | ssaidSettingsState.destroyLocked(null); |
| 2631 | } else { |
| 2632 | ssaidSettingsState.destroyLocked(new Runnable() { |
| 2633 | @Override |
| 2634 | public void run() { |
| 2635 | mSettingsStates.remove(ssaidKey); |
| 2636 | } |
| 2637 | }); |
| 2638 | } |
| 2639 | } |
| 2640 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2641 | // Nuke generation tracking data |
| 2642 | mGenerationRegistry.onUserRemoved(userId); |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2643 | } |
| 2644 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2645 | public boolean insertSettingLocked(int type, int userId, String name, String value, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2646 | String tag, boolean makeDefault, String packageName, boolean forceNotify, |
| 2647 | Set<String> criticalSettings) { |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 2648 | return insertSettingLocked(type, userId, name, value, tag, makeDefault, false, |
| 2649 | packageName, forceNotify, criticalSettings); |
| 2650 | } |
| 2651 | |
| 2652 | public boolean insertSettingLocked(int type, int userId, String name, String value, |
| 2653 | String tag, boolean makeDefault, boolean forceNonSystemPackage, String packageName, |
| 2654 | boolean forceNotify, Set<String> criticalSettings) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2655 | final int key = makeKey(type, userId); |
| 2656 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2657 | boolean success = false; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2658 | SettingsState settingsState = peekSettingsStateLocked(key); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2659 | if (settingsState != null) { |
| 2660 | success = settingsState.insertSettingLocked(name, value, |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 2661 | tag, makeDefault, forceNonSystemPackage, packageName); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2662 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2663 | |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2664 | if (success && criticalSettings != null && criticalSettings.contains(name)) { |
| 2665 | settingsState.persistSyncLocked(); |
| 2666 | } |
| 2667 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2668 | if (forceNotify || success) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2669 | notifyForSettingsChange(key, name); |
| 2670 | } |
| 2671 | return success; |
| 2672 | } |
| 2673 | |
Matt Pape | 4bd0a6a | 2019-11-19 12:17:45 -0800 | [diff] [blame^] | 2674 | public boolean setSettingsLocked(int type, int userId, String prefix, |
| 2675 | Map<String, String> keyValues, String packageName) { |
| 2676 | final int key = makeKey(type, userId); |
| 2677 | |
| 2678 | SettingsState settingsState = peekSettingsStateLocked(key); |
| 2679 | if (settingsState != null) { |
| 2680 | List<String> changedSettings = |
| 2681 | settingsState.setSettingsLocked(prefix, keyValues, packageName); |
| 2682 | if (!changedSettings.isEmpty()) { |
| 2683 | notifyForConfigSettingsChangeLocked(key, prefix, changedSettings); |
| 2684 | } |
| 2685 | } |
| 2686 | |
| 2687 | return settingsState != null; |
| 2688 | } |
| 2689 | |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2690 | public boolean deleteSettingLocked(int type, int userId, String name, boolean forceNotify, |
| 2691 | Set<String> criticalSettings) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2692 | final int key = makeKey(type, userId); |
| 2693 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2694 | boolean success = false; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2695 | SettingsState settingsState = peekSettingsStateLocked(key); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2696 | if (settingsState != null) { |
| 2697 | success = settingsState.deleteSettingLocked(name); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2698 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2699 | |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2700 | if (success && criticalSettings != null && criticalSettings.contains(name)) { |
| 2701 | settingsState.persistSyncLocked(); |
| 2702 | } |
| 2703 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2704 | if (forceNotify || success) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2705 | notifyForSettingsChange(key, name); |
| 2706 | } |
| 2707 | return success; |
| 2708 | } |
| 2709 | |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2710 | public boolean updateSettingLocked(int type, int userId, String name, String value, |
| 2711 | String tag, boolean makeDefault, String packageName, boolean forceNotify, |
| 2712 | Set<String> criticalSettings) { |
| 2713 | final int key = makeKey(type, userId); |
| 2714 | |
| 2715 | boolean success = false; |
| 2716 | SettingsState settingsState = peekSettingsStateLocked(key); |
| 2717 | if (settingsState != null) { |
| 2718 | success = settingsState.updateSettingLocked(name, value, tag, |
| 2719 | makeDefault, packageName); |
| 2720 | } |
| 2721 | |
| 2722 | if (success && criticalSettings != null && criticalSettings.contains(name)) { |
| 2723 | settingsState.persistSyncLocked(); |
| 2724 | } |
| 2725 | |
| 2726 | if (forceNotify || success) { |
| 2727 | notifyForSettingsChange(key, name); |
| 2728 | } |
| 2729 | |
| 2730 | return success; |
| 2731 | } |
| 2732 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2733 | public Setting getSettingLocked(int type, int userId, String name) { |
| 2734 | final int key = makeKey(type, userId); |
| 2735 | |
| 2736 | SettingsState settingsState = peekSettingsStateLocked(key); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2737 | if (settingsState == null) { |
Mark Rathjen | ea61759 | 2017-01-18 23:03:41 -0800 | [diff] [blame] | 2738 | return null; |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2739 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2740 | |
| 2741 | // getSettingLocked will return non-null result |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2742 | return settingsState.getSettingLocked(name); |
| 2743 | } |
| 2744 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2745 | public void resetSettingsLocked(int type, int userId, String packageName, int mode, |
| 2746 | String tag) { |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 2747 | resetSettingsLocked(type, userId, packageName, mode, tag, null); |
| 2748 | } |
| 2749 | |
| 2750 | public void resetSettingsLocked(int type, int userId, String packageName, int mode, |
| 2751 | String tag, @Nullable String prefix) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2752 | final int key = makeKey(type, userId); |
| 2753 | SettingsState settingsState = peekSettingsStateLocked(key); |
| 2754 | if (settingsState == null) { |
| 2755 | return; |
| 2756 | } |
| 2757 | |
| 2758 | switch (mode) { |
| 2759 | case Settings.RESET_MODE_PACKAGE_DEFAULTS: { |
| 2760 | for (String name : settingsState.getSettingNamesLocked()) { |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2761 | boolean someSettingChanged = false; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2762 | Setting setting = settingsState.getSettingLocked(name); |
| 2763 | if (packageName.equals(setting.getPackageName())) { |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 2764 | if ((tag != null && !tag.equals(setting.getTag())) |
| 2765 | || (prefix != null && !setting.getName().startsWith(prefix))) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2766 | continue; |
| 2767 | } |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2768 | if (settingsState.resetSettingLocked(name)) { |
| 2769 | someSettingChanged = true; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2770 | notifyForSettingsChange(key, name); |
| 2771 | } |
| 2772 | } |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2773 | if (someSettingChanged) { |
| 2774 | settingsState.persistSyncLocked(); |
| 2775 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2776 | } |
| 2777 | } break; |
| 2778 | |
| 2779 | case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: { |
| 2780 | for (String name : settingsState.getSettingNamesLocked()) { |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2781 | boolean someSettingChanged = false; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2782 | Setting setting = settingsState.getSettingLocked(name); |
| 2783 | if (!SettingsState.isSystemPackage(getContext(), |
Matt Pape | 25c940d | 2019-03-26 12:14:33 -0700 | [diff] [blame] | 2784 | setting.getPackageName(), INVALID_UID, userId)) { |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 2785 | if (prefix != null && !setting.getName().startsWith(prefix)) { |
| 2786 | continue; |
| 2787 | } |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2788 | if (settingsState.resetSettingLocked(name)) { |
| 2789 | someSettingChanged = true; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2790 | notifyForSettingsChange(key, name); |
| 2791 | } |
| 2792 | } |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2793 | if (someSettingChanged) { |
| 2794 | settingsState.persistSyncLocked(); |
| 2795 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2796 | } |
| 2797 | } break; |
| 2798 | |
| 2799 | case Settings.RESET_MODE_UNTRUSTED_CHANGES: { |
| 2800 | for (String name : settingsState.getSettingNamesLocked()) { |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2801 | boolean someSettingChanged = false; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2802 | Setting setting = settingsState.getSettingLocked(name); |
| 2803 | if (!SettingsState.isSystemPackage(getContext(), |
Matt Pape | 25c940d | 2019-03-26 12:14:33 -0700 | [diff] [blame] | 2804 | setting.getPackageName(), INVALID_UID, userId)) { |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 2805 | if (prefix != null && !setting.getName().startsWith(prefix)) { |
| 2806 | continue; |
| 2807 | } |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame] | 2808 | if (setting.isDefaultFromSystem()) { |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2809 | if (settingsState.resetSettingLocked(name)) { |
| 2810 | someSettingChanged = true; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2811 | notifyForSettingsChange(key, name); |
| 2812 | } |
| 2813 | } else if (settingsState.deleteSettingLocked(name)) { |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2814 | someSettingChanged = true; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2815 | notifyForSettingsChange(key, name); |
| 2816 | } |
| 2817 | } |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2818 | if (someSettingChanged) { |
| 2819 | settingsState.persistSyncLocked(); |
| 2820 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2821 | } |
| 2822 | } break; |
| 2823 | |
| 2824 | case Settings.RESET_MODE_TRUSTED_DEFAULTS: { |
| 2825 | for (String name : settingsState.getSettingNamesLocked()) { |
| 2826 | Setting setting = settingsState.getSettingLocked(name); |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2827 | boolean someSettingChanged = false; |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 2828 | if (prefix != null && !setting.getName().startsWith(prefix)) { |
| 2829 | continue; |
| 2830 | } |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame] | 2831 | if (setting.isDefaultFromSystem()) { |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2832 | if (settingsState.resetSettingLocked(name)) { |
| 2833 | someSettingChanged = true; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2834 | notifyForSettingsChange(key, name); |
| 2835 | } |
| 2836 | } else if (settingsState.deleteSettingLocked(name)) { |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2837 | someSettingChanged = true; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2838 | notifyForSettingsChange(key, name); |
| 2839 | } |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2840 | if (someSettingChanged) { |
| 2841 | settingsState.persistSyncLocked(); |
| 2842 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2843 | } |
| 2844 | } break; |
| 2845 | } |
| 2846 | } |
| 2847 | |
Zimuzo | c56192c | 2018-07-25 10:40:01 +0100 | [diff] [blame] | 2848 | public void removeSettingsForPackageLocked(String packageName, int userId) { |
Svet Ganov | 8de3480 | 2015-04-27 09:33:40 -0700 | [diff] [blame] | 2849 | // Global and secure settings are signature protected. Apps signed |
| 2850 | // by the platform certificate are generally not uninstalled and |
| 2851 | // the main exception is tests. We trust components signed |
| 2852 | // by the platform certificate and do not do a clean up after them. |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2853 | |
| 2854 | final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId); |
| 2855 | SettingsState systemSettings = mSettingsStates.get(systemKey); |
Svet Ganov | 8de3480 | 2015-04-27 09:33:40 -0700 | [diff] [blame] | 2856 | if (systemSettings != null) { |
Zimuzo | c56192c | 2018-07-25 10:40:01 +0100 | [diff] [blame] | 2857 | systemSettings.removeSettingsForPackageLocked(packageName); |
Svet Ganov | 8de3480 | 2015-04-27 09:33:40 -0700 | [diff] [blame] | 2858 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2859 | } |
| 2860 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2861 | public void onUidRemovedLocked(int uid) { |
| 2862 | final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, |
| 2863 | UserHandle.getUserId(uid)); |
Tony Mak | 24c6ab4 | 2018-01-08 14:35:34 +0000 | [diff] [blame] | 2864 | if (ssaidSettings != null) { |
| 2865 | ssaidSettings.deleteSettingLocked(Integer.toString(uid)); |
| 2866 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
Kweku Adams | b0886f3 | 2017-10-31 15:32:09 -0700 | [diff] [blame] | 2869 | @Nullable |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2870 | private SettingsState peekSettingsStateLocked(int key) { |
| 2871 | SettingsState settingsState = mSettingsStates.get(key); |
| 2872 | if (settingsState != null) { |
| 2873 | return settingsState; |
| 2874 | } |
| 2875 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2876 | if (!ensureSettingsForUserLocked(getUserIdFromKey(key))) { |
| 2877 | return null; |
| 2878 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2879 | return mSettingsStates.get(key); |
| 2880 | } |
| 2881 | |
| 2882 | private void migrateAllLegacySettingsIfNeeded() { |
| 2883 | synchronized (mLock) { |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 2884 | final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2885 | File globalFile = getSettingsFile(key); |
Amith Yamasani | 74bf71b | 2017-07-19 16:49:52 -0700 | [diff] [blame] | 2886 | if (SettingsState.stateFileExists(globalFile)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2887 | return; |
| 2888 | } |
| 2889 | |
Amith Yamasani | 3945202 | 2017-03-21 15:23:47 -0700 | [diff] [blame] | 2890 | mSettingsCreationBuildId = Build.ID; |
| 2891 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2892 | final long identity = Binder.clearCallingIdentity(); |
| 2893 | try { |
| 2894 | List<UserInfo> users = mUserManager.getUsers(true); |
| 2895 | |
| 2896 | final int userCount = users.size(); |
| 2897 | for (int i = 0; i < userCount; i++) { |
| 2898 | final int userId = users.get(i).id; |
| 2899 | |
| 2900 | DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId); |
| 2901 | SQLiteDatabase database = dbHelper.getWritableDatabase(); |
| 2902 | migrateLegacySettingsForUserLocked(dbHelper, database, userId); |
| 2903 | |
| 2904 | // Upgrade to the latest version. |
| 2905 | UpgradeController upgrader = new UpgradeController(userId); |
| 2906 | upgrader.upgradeIfNeededLocked(); |
| 2907 | |
| 2908 | // Drop from memory if not a running user. |
| 2909 | if (!mUserManager.isUserRunning(new UserHandle(userId))) { |
| 2910 | removeUserStateLocked(userId, false); |
| 2911 | } |
| 2912 | } |
| 2913 | } finally { |
| 2914 | Binder.restoreCallingIdentity(identity); |
| 2915 | } |
| 2916 | } |
| 2917 | } |
| 2918 | |
| 2919 | private void migrateLegacySettingsForUserIfNeededLocked(int userId) { |
| 2920 | // Every user has secure settings and if no file we need to migrate. |
| 2921 | final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId); |
| 2922 | File secureFile = getSettingsFile(secureKey); |
Amith Yamasani | 74bf71b | 2017-07-19 16:49:52 -0700 | [diff] [blame] | 2923 | if (SettingsState.stateFileExists(secureFile)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2924 | return; |
| 2925 | } |
| 2926 | |
| 2927 | DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId); |
| 2928 | SQLiteDatabase database = dbHelper.getWritableDatabase(); |
| 2929 | |
| 2930 | migrateLegacySettingsForUserLocked(dbHelper, database, userId); |
| 2931 | } |
| 2932 | |
| 2933 | private void migrateLegacySettingsForUserLocked(DatabaseHelper dbHelper, |
| 2934 | SQLiteDatabase database, int userId) { |
Amith Yamasani | bf2ef61 | 2016-03-07 16:37:18 -0800 | [diff] [blame] | 2935 | // Move over the system settings. |
| 2936 | final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId); |
| 2937 | ensureSettingsStateLocked(systemKey); |
| 2938 | SettingsState systemSettings = mSettingsStates.get(systemKey); |
| 2939 | migrateLegacySettingsLocked(systemSettings, database, TABLE_SYSTEM); |
| 2940 | systemSettings.persistSyncLocked(); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2941 | |
| 2942 | // Move over the secure settings. |
Amith Yamasani | bf2ef61 | 2016-03-07 16:37:18 -0800 | [diff] [blame] | 2943 | // Do this after System settings, since this is the first thing we check when deciding |
| 2944 | // to skip over migration from db to xml for a secondary user. |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2945 | final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId); |
| 2946 | ensureSettingsStateLocked(secureKey); |
| 2947 | SettingsState secureSettings = mSettingsStates.get(secureKey); |
| 2948 | migrateLegacySettingsLocked(secureSettings, database, TABLE_SECURE); |
| 2949 | ensureSecureSettingAndroidIdSetLocked(secureSettings); |
| 2950 | secureSettings.persistSyncLocked(); |
| 2951 | |
Amith Yamasani | bf2ef61 | 2016-03-07 16:37:18 -0800 | [diff] [blame] | 2952 | // Move over the global settings if owner. |
| 2953 | // Do this last, since this is the first thing we check when deciding |
| 2954 | // to skip over migration from db to xml for owner user. |
| 2955 | if (userId == UserHandle.USER_SYSTEM) { |
| 2956 | final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, userId); |
| 2957 | ensureSettingsStateLocked(globalKey); |
| 2958 | SettingsState globalSettings = mSettingsStates.get(globalKey); |
| 2959 | migrateLegacySettingsLocked(globalSettings, database, TABLE_GLOBAL); |
Amith Yamasani | 3945202 | 2017-03-21 15:23:47 -0700 | [diff] [blame] | 2960 | // If this was just created |
| 2961 | if (mSettingsCreationBuildId != null) { |
| 2962 | globalSettings.insertSettingLocked(Settings.Global.DATABASE_CREATION_BUILDID, |
| 2963 | mSettingsCreationBuildId, null, true, |
| 2964 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 2965 | } |
Amith Yamasani | bf2ef61 | 2016-03-07 16:37:18 -0800 | [diff] [blame] | 2966 | globalSettings.persistSyncLocked(); |
| 2967 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2968 | |
| 2969 | // Drop the database as now all is moved and persisted. |
| 2970 | if (DROP_DATABASE_ON_MIGRATION) { |
| 2971 | dbHelper.dropDatabase(); |
| 2972 | } else { |
| 2973 | dbHelper.backupDatabase(); |
| 2974 | } |
| 2975 | } |
| 2976 | |
| 2977 | private void migrateLegacySettingsLocked(SettingsState settingsState, |
| 2978 | SQLiteDatabase database, String table) { |
| 2979 | SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); |
| 2980 | queryBuilder.setTables(table); |
| 2981 | |
| 2982 | Cursor cursor = queryBuilder.query(database, ALL_COLUMNS, |
| 2983 | null, null, null, null, null); |
| 2984 | |
| 2985 | if (cursor == null) { |
| 2986 | return; |
| 2987 | } |
| 2988 | |
| 2989 | try { |
| 2990 | if (!cursor.moveToFirst()) { |
| 2991 | return; |
| 2992 | } |
| 2993 | |
| 2994 | final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME); |
| 2995 | final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE); |
| 2996 | |
| 2997 | settingsState.setVersionLocked(database.getVersion()); |
| 2998 | |
| 2999 | while (!cursor.isAfterLast()) { |
| 3000 | String name = cursor.getString(nameColumnIdx); |
| 3001 | String value = cursor.getString(valueColumnIdx); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3002 | settingsState.insertSettingLocked(name, value, null, true, |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3003 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 3004 | cursor.moveToNext(); |
| 3005 | } |
| 3006 | } finally { |
| 3007 | cursor.close(); |
| 3008 | } |
| 3009 | } |
| 3010 | |
Andreas Gampe | b5889307 | 2018-09-05 16:52:31 -0700 | [diff] [blame] | 3011 | @GuardedBy("secureSettings.mLock") |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3012 | private void ensureSecureSettingAndroidIdSetLocked(SettingsState secureSettings) { |
| 3013 | Setting value = secureSettings.getSettingLocked(Settings.Secure.ANDROID_ID); |
| 3014 | |
Seigo Nonaka | 6e5b602 | 2016-04-27 16:32:44 +0900 | [diff] [blame] | 3015 | if (!value.isNull()) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3016 | return; |
| 3017 | } |
| 3018 | |
| 3019 | final int userId = getUserIdFromKey(secureSettings.mKey); |
| 3020 | |
| 3021 | final UserInfo user; |
| 3022 | final long identity = Binder.clearCallingIdentity(); |
| 3023 | try { |
| 3024 | user = mUserManager.getUserInfo(userId); |
| 3025 | } finally { |
| 3026 | Binder.restoreCallingIdentity(identity); |
| 3027 | } |
| 3028 | if (user == null) { |
| 3029 | // Can happen due to races when deleting users - treat as benign. |
| 3030 | return; |
| 3031 | } |
| 3032 | |
| 3033 | String androidId = Long.toHexString(new SecureRandom().nextLong()); |
| 3034 | secureSettings.insertSettingLocked(Settings.Secure.ANDROID_ID, androidId, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3035 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3036 | |
| 3037 | Slog.d(LOG_TAG, "Generated and saved new ANDROID_ID [" + androidId |
| 3038 | + "] for user " + userId); |
| 3039 | |
| 3040 | // Write a drop box entry if it's a restricted profile |
| 3041 | if (user.isRestricted()) { |
| 3042 | DropBoxManager dbm = (DropBoxManager) getContext().getSystemService( |
| 3043 | Context.DROPBOX_SERVICE); |
| 3044 | if (dbm != null && dbm.isTagEnabled(DROPBOX_TAG_USERLOG)) { |
| 3045 | dbm.addText(DROPBOX_TAG_USERLOG, System.currentTimeMillis() |
| 3046 | + "," + DROPBOX_TAG_USERLOG + "," + androidId + "\n"); |
| 3047 | } |
| 3048 | } |
| 3049 | } |
| 3050 | |
| 3051 | private void notifyForSettingsChange(int key, String name) { |
Svet Ganov | 945864c | 2018-03-22 21:49:10 -0700 | [diff] [blame] | 3052 | // Increment the generation first, so observers always see the new value |
Phil Weaver | 83fec00 | 2016-05-11 10:55:29 -0700 | [diff] [blame] | 3053 | mGenerationRegistry.incrementGeneration(key); |
| 3054 | |
Linus Tufvesson | 93c3855 | 2019-05-30 12:47:39 +0100 | [diff] [blame] | 3055 | if (isGlobalSettingsKey(key) || isConfigSettingsKey(key)) { |
Jeff Sharkey | 308093f | 2018-03-25 22:53:29 -0600 | [diff] [blame] | 3056 | final long token = Binder.clearCallingIdentity(); |
| 3057 | try { |
Linus Tufvesson | 93c3855 | 2019-05-30 12:47:39 +0100 | [diff] [blame] | 3058 | if (Global.LOCATION_GLOBAL_KILL_SWITCH.equals(name) |
| 3059 | && isGlobalSettingsKey(key)) { |
Jeff Sharkey | 308093f | 2018-03-25 22:53:29 -0600 | [diff] [blame] | 3060 | // When the global kill switch is updated, send the |
| 3061 | // change notification for the location setting. |
| 3062 | notifyLocationChangeForRunningUsers(); |
| 3063 | } |
Linus Tufvesson | 93c3855 | 2019-05-30 12:47:39 +0100 | [diff] [blame] | 3064 | notifySettingChangeForRunningUsers(key, name); |
Jeff Sharkey | 308093f | 2018-03-25 22:53:29 -0600 | [diff] [blame] | 3065 | } finally { |
| 3066 | Binder.restoreCallingIdentity(token); |
Svet Ganov | 945864c | 2018-03-22 21:49:10 -0700 | [diff] [blame] | 3067 | } |
Svet Ganov | 945864c | 2018-03-22 21:49:10 -0700 | [diff] [blame] | 3068 | } else { |
| 3069 | final int userId = getUserIdFromKey(key); |
| 3070 | final Uri uri = getNotificationUriFor(key, name); |
| 3071 | mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED, |
| 3072 | userId, 0, uri).sendToTarget(); |
| 3073 | if (isSecureSettingsKey(key)) { |
| 3074 | maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name, |
| 3075 | sSecureCloneToManagedSettings); |
| 3076 | maybeNotifyProfiles(SETTINGS_TYPE_SYSTEM, userId, uri, name, |
| 3077 | sSystemCloneFromParentOnDependency.values()); |
| 3078 | } else if (isSystemSettingsKey(key)) { |
| 3079 | maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name, |
| 3080 | sSystemCloneToManagedSettings); |
| 3081 | } |
Nicolas Prevot | 310e1ee | 2015-07-02 14:03:06 +0100 | [diff] [blame] | 3082 | } |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 3083 | |
Svet Ganov | 945864c | 2018-03-22 21:49:10 -0700 | [diff] [blame] | 3084 | // Always notify that our data changed |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 3085 | mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget(); |
Nicolas Prevot | 310e1ee | 2015-07-02 14:03:06 +0100 | [diff] [blame] | 3086 | } |
| 3087 | |
Matt Pape | 4bd0a6a | 2019-11-19 12:17:45 -0800 | [diff] [blame^] | 3088 | private void notifyForConfigSettingsChangeLocked(int key, String prefix, |
| 3089 | List<String> changedSettings) { |
| 3090 | |
| 3091 | // Increment the generation first, so observers always see the new value |
| 3092 | mGenerationRegistry.incrementGeneration(key); |
| 3093 | |
| 3094 | StringBuilder stringBuilder = new StringBuilder(prefix); |
| 3095 | for (int i = 0; i < changedSettings.size(); ++i) { |
| 3096 | stringBuilder.append(changedSettings.get(i).split("/")[1]).append("/"); |
| 3097 | } |
| 3098 | |
| 3099 | final long token = Binder.clearCallingIdentity(); |
| 3100 | try { |
| 3101 | notifySettingChangeForRunningUsers(key, stringBuilder.toString()); |
| 3102 | } finally { |
| 3103 | Binder.restoreCallingIdentity(token); |
| 3104 | } |
| 3105 | |
| 3106 | // Always notify that our data changed |
| 3107 | mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget(); |
| 3108 | } |
| 3109 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 3110 | private void maybeNotifyProfiles(int type, int userId, Uri uri, String name, |
Robin Lee | 7af9a74 | 2017-02-20 14:47:30 +0000 | [diff] [blame] | 3111 | Collection<String> keysCloned) { |
Nicolas Prevot | 310e1ee | 2015-07-02 14:03:06 +0100 | [diff] [blame] | 3112 | if (keysCloned.contains(name)) { |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 3113 | for (int profileId : mUserManager.getProfileIdsWithDisabled(userId)) { |
Nicolas Prevot | 310e1ee | 2015-07-02 14:03:06 +0100 | [diff] [blame] | 3114 | // the notification for userId has already been sent. |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 3115 | if (profileId != userId) { |
Svet Ganov | 945864c | 2018-03-22 21:49:10 -0700 | [diff] [blame] | 3116 | final int key = makeKey(type, profileId); |
| 3117 | // Increment the generation first, so observers always see the new value |
| 3118 | mGenerationRegistry.incrementGeneration(key); |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 3119 | mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED, |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 3120 | profileId, 0, uri).sendToTarget(); |
Nicolas Prevot | 310e1ee | 2015-07-02 14:03:06 +0100 | [diff] [blame] | 3121 | } |
| 3122 | } |
| 3123 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3124 | } |
| 3125 | |
Linus Tufvesson | 93c3855 | 2019-05-30 12:47:39 +0100 | [diff] [blame] | 3126 | private void notifySettingChangeForRunningUsers(int key, String name) { |
Svet Ganov | 945864c | 2018-03-22 21:49:10 -0700 | [diff] [blame] | 3127 | // Important: No need to update generation for each user as there |
| 3128 | // is a singleton generation entry for the global settings which |
| 3129 | // is already incremented be the caller. |
| 3130 | final Uri uri = getNotificationUriFor(key, name); |
| 3131 | final List<UserInfo> users = mUserManager.getUsers(/*excludeDying*/ true); |
| 3132 | for (int i = 0; i < users.size(); i++) { |
| 3133 | final int userId = users.get(i).id; |
| 3134 | if (mUserManager.isUserRunning(UserHandle.of(userId))) { |
| 3135 | mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED, |
| 3136 | userId, 0, uri).sendToTarget(); |
| 3137 | } |
| 3138 | } |
| 3139 | } |
| 3140 | |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 3141 | private void notifyLocationChangeForRunningUsers() { |
| 3142 | final List<UserInfo> users = mUserManager.getUsers(/*excludeDying=*/ true); |
| 3143 | |
| 3144 | for (int i = 0; i < users.size(); i++) { |
| 3145 | final int userId = users.get(i).id; |
| 3146 | |
| 3147 | if (!mUserManager.isUserRunning(UserHandle.of(userId))) { |
| 3148 | continue; |
| 3149 | } |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 3150 | |
Svet Ganov | 945864c | 2018-03-22 21:49:10 -0700 | [diff] [blame] | 3151 | // Increment the generation first, so observers always see the new value |
| 3152 | final int key = makeKey(SETTINGS_TYPE_SECURE, userId); |
| 3153 | mGenerationRegistry.incrementGeneration(key); |
| 3154 | |
Kweku Adams | 5e0052b | 2019-02-22 15:17:52 -0800 | [diff] [blame] | 3155 | final Uri uri = getNotificationUriFor(key, Secure.LOCATION_MODE); |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 3156 | mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED, |
| 3157 | userId, 0, uri).sendToTarget(); |
| 3158 | } |
| 3159 | } |
| 3160 | |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 3161 | private boolean isConfigSettingsKey(int key) { |
| 3162 | return getTypeFromKey(key) == SETTINGS_TYPE_CONFIG; |
| 3163 | } |
| 3164 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3165 | private boolean isGlobalSettingsKey(int key) { |
| 3166 | return getTypeFromKey(key) == SETTINGS_TYPE_GLOBAL; |
| 3167 | } |
| 3168 | |
| 3169 | private boolean isSystemSettingsKey(int key) { |
| 3170 | return getTypeFromKey(key) == SETTINGS_TYPE_SYSTEM; |
| 3171 | } |
| 3172 | |
| 3173 | private boolean isSecureSettingsKey(int key) { |
| 3174 | return getTypeFromKey(key) == SETTINGS_TYPE_SECURE; |
| 3175 | } |
| 3176 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3177 | private boolean isSsaidSettingsKey(int key) { |
| 3178 | return getTypeFromKey(key) == SETTINGS_TYPE_SSAID; |
| 3179 | } |
| 3180 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3181 | private File getSettingsFile(int key) { |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 3182 | if (isConfigSettingsKey(key)) { |
| 3183 | final int userId = getUserIdFromKey(key); |
| 3184 | return new File(Environment.getUserSystemDirectory(userId), |
| 3185 | SETTINGS_FILE_CONFIG); |
| 3186 | } else if (isGlobalSettingsKey(key)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3187 | final int userId = getUserIdFromKey(key); |
| 3188 | return new File(Environment.getUserSystemDirectory(userId), |
| 3189 | SETTINGS_FILE_GLOBAL); |
| 3190 | } else if (isSystemSettingsKey(key)) { |
| 3191 | final int userId = getUserIdFromKey(key); |
| 3192 | return new File(Environment.getUserSystemDirectory(userId), |
| 3193 | SETTINGS_FILE_SYSTEM); |
| 3194 | } else if (isSecureSettingsKey(key)) { |
| 3195 | final int userId = getUserIdFromKey(key); |
| 3196 | return new File(Environment.getUserSystemDirectory(userId), |
| 3197 | SETTINGS_FILE_SECURE); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3198 | } else if (isSsaidSettingsKey(key)) { |
| 3199 | final int userId = getUserIdFromKey(key); |
| 3200 | return new File(Environment.getUserSystemDirectory(userId), |
| 3201 | SETTINGS_FILE_SSAID); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3202 | } else { |
| 3203 | throw new IllegalArgumentException("Invalid settings key:" + key); |
| 3204 | } |
| 3205 | } |
| 3206 | |
| 3207 | private Uri getNotificationUriFor(int key, String name) { |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 3208 | if (isConfigSettingsKey(key)) { |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 3209 | return (name != null) ? Uri.withAppendedPath(DeviceConfig.CONTENT_URI, name) |
| 3210 | : DeviceConfig.CONTENT_URI; |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 3211 | } else if (isGlobalSettingsKey(key)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3212 | return (name != null) ? Uri.withAppendedPath(Settings.Global.CONTENT_URI, name) |
| 3213 | : Settings.Global.CONTENT_URI; |
| 3214 | } else if (isSecureSettingsKey(key)) { |
| 3215 | return (name != null) ? Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name) |
| 3216 | : Settings.Secure.CONTENT_URI; |
| 3217 | } else if (isSystemSettingsKey(key)) { |
| 3218 | return (name != null) ? Uri.withAppendedPath(Settings.System.CONTENT_URI, name) |
| 3219 | : Settings.System.CONTENT_URI; |
| 3220 | } else { |
| 3221 | throw new IllegalArgumentException("Invalid settings key:" + key); |
| 3222 | } |
| 3223 | } |
| 3224 | |
| 3225 | private int getMaxBytesPerPackageForType(int type) { |
| 3226 | switch (type) { |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 3227 | case SETTINGS_TYPE_CONFIG: |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3228 | case SETTINGS_TYPE_GLOBAL: |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3229 | case SETTINGS_TYPE_SECURE: |
| 3230 | case SETTINGS_TYPE_SSAID: { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3231 | return SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED; |
| 3232 | } |
| 3233 | |
| 3234 | default: { |
| 3235 | return SettingsState.MAX_BYTES_PER_APP_PACKAGE_LIMITED; |
| 3236 | } |
| 3237 | } |
| 3238 | } |
| 3239 | |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 3240 | private final class MyHandler extends Handler { |
| 3241 | private static final int MSG_NOTIFY_URI_CHANGED = 1; |
| 3242 | private static final int MSG_NOTIFY_DATA_CHANGED = 2; |
| 3243 | |
| 3244 | public MyHandler(Looper looper) { |
| 3245 | super(looper); |
| 3246 | } |
| 3247 | |
| 3248 | @Override |
| 3249 | public void handleMessage(Message msg) { |
| 3250 | switch (msg.what) { |
| 3251 | case MSG_NOTIFY_URI_CHANGED: { |
| 3252 | final int userId = msg.arg1; |
| 3253 | Uri uri = (Uri) msg.obj; |
Jeff Sharkey | 88f9d0b | 2017-08-11 15:29:40 -0600 | [diff] [blame] | 3254 | try { |
| 3255 | getContext().getContentResolver().notifyChange(uri, null, true, userId); |
| 3256 | } catch (SecurityException e) { |
| 3257 | Slog.w(LOG_TAG, "Failed to notify for " + userId + ": " + uri, e); |
| 3258 | } |
Varun Shah | 2ecf7ad | 2019-10-16 12:30:57 -0700 | [diff] [blame] | 3259 | if (DEBUG) { |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 3260 | Slog.v(LOG_TAG, "Notifying for " + userId + ": " + uri); |
| 3261 | } |
| 3262 | } break; |
| 3263 | |
| 3264 | case MSG_NOTIFY_DATA_CHANGED: { |
| 3265 | mBackupManager.dataChanged(); |
| 3266 | } break; |
| 3267 | } |
| 3268 | } |
| 3269 | } |
| 3270 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3271 | private final class UpgradeController { |
Beverly | f364d7c | 2019-10-10 16:44:43 -0400 | [diff] [blame] | 3272 | private static final int SETTINGS_VERSION = 184; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3273 | |
| 3274 | private final int mUserId; |
| 3275 | |
| 3276 | public UpgradeController(int userId) { |
| 3277 | mUserId = userId; |
| 3278 | } |
| 3279 | |
| 3280 | public void upgradeIfNeededLocked() { |
| 3281 | // The version of all settings for a user is the same (all users have secure). |
| 3282 | SettingsState secureSettings = getSettingsLocked( |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 3283 | SETTINGS_TYPE_SECURE, mUserId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3284 | |
| 3285 | // Try an update from the current state. |
| 3286 | final int oldVersion = secureSettings.getVersionLocked(); |
| 3287 | final int newVersion = SETTINGS_VERSION; |
| 3288 | |
Svet Ganov | c9755bc | 2015-03-28 13:21:22 -0700 | [diff] [blame] | 3289 | // If up do date - done. |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3290 | if (oldVersion == newVersion) { |
| 3291 | return; |
| 3292 | } |
| 3293 | |
| 3294 | // Try to upgrade. |
| 3295 | final int curVersion = onUpgradeLocked(mUserId, oldVersion, newVersion); |
| 3296 | |
| 3297 | // If upgrade failed start from scratch and upgrade. |
| 3298 | if (curVersion != newVersion) { |
| 3299 | // Drop state we have for this user. |
| 3300 | removeUserStateLocked(mUserId, true); |
| 3301 | |
| 3302 | // Recreate the database. |
| 3303 | DatabaseHelper dbHelper = new DatabaseHelper(getContext(), mUserId); |
| 3304 | SQLiteDatabase database = dbHelper.getWritableDatabase(); |
| 3305 | dbHelper.recreateDatabase(database, newVersion, curVersion, oldVersion); |
| 3306 | |
| 3307 | // Migrate the settings for this user. |
| 3308 | migrateLegacySettingsForUserLocked(dbHelper, database, mUserId); |
| 3309 | |
| 3310 | // Now upgrade should work fine. |
| 3311 | onUpgradeLocked(mUserId, oldVersion, newVersion); |
Svetoslav Ganov | 264c7a9 | 2016-08-24 17:31:14 -0700 | [diff] [blame] | 3312 | |
| 3313 | // Make a note what happened, so we don't wonder why data was lost |
| 3314 | String reason = "Settings rebuilt! Current version: " |
| 3315 | + curVersion + " while expected: " + newVersion; |
| 3316 | getGlobalSettingsLocked().insertSettingLocked( |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3317 | Settings.Global.DATABASE_DOWNGRADE_REASON, |
| 3318 | reason, null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3319 | } |
| 3320 | |
| 3321 | // Set the global settings version if owner. |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 3322 | if (mUserId == UserHandle.USER_SYSTEM) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3323 | SettingsState globalSettings = getSettingsLocked( |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 3324 | SETTINGS_TYPE_GLOBAL, mUserId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3325 | globalSettings.setVersionLocked(newVersion); |
| 3326 | } |
| 3327 | |
| 3328 | // Set the secure settings version. |
| 3329 | secureSettings.setVersionLocked(newVersion); |
| 3330 | |
| 3331 | // Set the system settings version. |
| 3332 | SettingsState systemSettings = getSettingsLocked( |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 3333 | SETTINGS_TYPE_SYSTEM, mUserId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3334 | systemSettings.setVersionLocked(newVersion); |
| 3335 | } |
| 3336 | |
| 3337 | private SettingsState getGlobalSettingsLocked() { |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 3338 | return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3339 | } |
| 3340 | |
| 3341 | private SettingsState getSecureSettingsLocked(int userId) { |
| 3342 | return getSettingsLocked(SETTINGS_TYPE_SECURE, userId); |
| 3343 | } |
| 3344 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3345 | private SettingsState getSsaidSettingsLocked(int userId) { |
| 3346 | return getSettingsLocked(SETTINGS_TYPE_SSAID, userId); |
| 3347 | } |
| 3348 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3349 | private SettingsState getSystemSettingsLocked(int userId) { |
| 3350 | return getSettingsLocked(SETTINGS_TYPE_SYSTEM, userId); |
| 3351 | } |
| 3352 | |
Jeff Brown | 503cffc | 2015-03-26 18:08:51 -0700 | [diff] [blame] | 3353 | /** |
| 3354 | * You must perform all necessary mutations to bring the settings |
| 3355 | * for this user from the old to the new version. When you add a new |
| 3356 | * upgrade step you *must* update SETTINGS_VERSION. |
| 3357 | * |
| 3358 | * This is an example of moving a setting from secure to global. |
| 3359 | * |
| 3360 | * // v119: Example settings changes. |
| 3361 | * if (currentVersion == 118) { |
| 3362 | * if (userId == UserHandle.USER_OWNER) { |
| 3363 | * // Remove from the secure settings. |
| 3364 | * SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3365 | * String name = "example_setting_to_move"; |
| 3366 | * String value = secureSettings.getSetting(name); |
| 3367 | * secureSettings.deleteSetting(name); |
| 3368 | * |
| 3369 | * // Add to the global settings. |
| 3370 | * SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3371 | * globalSettings.insertSetting(name, value, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3372 | * } |
| 3373 | * |
| 3374 | * // Update the current version. |
| 3375 | * currentVersion = 119; |
| 3376 | * } |
| 3377 | */ |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3378 | private int onUpgradeLocked(int userId, int oldVersion, int newVersion) { |
| 3379 | if (DEBUG) { |
| 3380 | Slog.w(LOG_TAG, "Upgrading settings for user: " + userId + " from version: " |
| 3381 | + oldVersion + " to version: " + newVersion); |
| 3382 | } |
| 3383 | |
Jeff Brown | 503cffc | 2015-03-26 18:08:51 -0700 | [diff] [blame] | 3384 | int currentVersion = oldVersion; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3385 | |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 3386 | // v119: Reset zen + ringer mode. |
| 3387 | if (currentVersion == 118) { |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 3388 | if (userId == UserHandle.USER_SYSTEM) { |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 3389 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3390 | globalSettings.updateSettingLocked(Settings.Global.ZEN_MODE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3391 | Integer.toString(Settings.Global.ZEN_MODE_OFF), null, |
| 3392 | true, SettingsState.SYSTEM_PACKAGE_NAME); |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 3393 | globalSettings.updateSettingLocked(Settings.Global.MODE_RINGER, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3394 | Integer.toString(AudioManager.RINGER_MODE_NORMAL), null, |
| 3395 | true, SettingsState.SYSTEM_PACKAGE_NAME); |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 3396 | } |
| 3397 | currentVersion = 119; |
| 3398 | } |
| 3399 | |
Jason Monk | 27bbb2d | 2015-03-31 16:46:39 -0400 | [diff] [blame] | 3400 | // v120: Add double tap to wake setting. |
| 3401 | if (currentVersion == 119) { |
| 3402 | SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3403 | secureSettings.insertSettingLocked(Settings.Secure.DOUBLE_TAP_TO_WAKE, |
| 3404 | getContext().getResources().getBoolean( |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3405 | R.bool.def_double_tap_to_wake) ? "1" : "0", null, true, |
Jason Monk | 27bbb2d | 2015-03-31 16:46:39 -0400 | [diff] [blame] | 3406 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 3407 | |
| 3408 | currentVersion = 120; |
| 3409 | } |
| 3410 | |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 3411 | if (currentVersion == 120) { |
| 3412 | // Before 121, we used a different string encoding logic. We just bump the |
| 3413 | // version here; SettingsState knows how to handle pre-version 120 files. |
| 3414 | currentVersion = 121; |
| 3415 | } |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 3416 | |
Martijn Coenen | 7ab4b7f | 2015-07-27 15:58:32 +0200 | [diff] [blame] | 3417 | if (currentVersion == 121) { |
| 3418 | // Version 122: allow OEMs to set a default payment component in resources. |
| 3419 | // Note that we only write the default if no default has been set; |
| 3420 | // if there is, we just leave the default at whatever it currently is. |
| 3421 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3422 | String defaultComponent = (getContext().getResources().getString( |
| 3423 | R.string.def_nfc_payment_component)); |
| 3424 | Setting currentSetting = secureSettings.getSettingLocked( |
| 3425 | Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT); |
| 3426 | if (defaultComponent != null && !defaultComponent.isEmpty() && |
Svetoslav Ganov | fedb230 | 2016-04-26 18:36:42 -0700 | [diff] [blame] | 3427 | currentSetting.isNull()) { |
Martijn Coenen | 7ab4b7f | 2015-07-27 15:58:32 +0200 | [diff] [blame] | 3428 | secureSettings.insertSettingLocked( |
| 3429 | Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3430 | defaultComponent, null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Martijn Coenen | 7ab4b7f | 2015-07-27 15:58:32 +0200 | [diff] [blame] | 3431 | } |
| 3432 | currentVersion = 122; |
| 3433 | } |
Suprabh Shukla | 269c11e | 2015-12-02 16:51:16 -0800 | [diff] [blame] | 3434 | |
| 3435 | if (currentVersion == 122) { |
| 3436 | // Version 123: Adding a default value for the ability to add a user from |
| 3437 | // the lock screen. |
| 3438 | if (userId == UserHandle.USER_SYSTEM) { |
| 3439 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3440 | Setting currentSetting = globalSettings.getSettingLocked( |
| 3441 | Settings.Global.ADD_USERS_WHEN_LOCKED); |
Svetoslav Ganov | fedb230 | 2016-04-26 18:36:42 -0700 | [diff] [blame] | 3442 | if (currentSetting.isNull()) { |
Suprabh Shukla | 269c11e | 2015-12-02 16:51:16 -0800 | [diff] [blame] | 3443 | globalSettings.insertSettingLocked( |
| 3444 | Settings.Global.ADD_USERS_WHEN_LOCKED, |
| 3445 | getContext().getResources().getBoolean( |
| 3446 | R.bool.def_add_users_from_lockscreen) ? "1" : "0", |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3447 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Suprabh Shukla | 269c11e | 2015-12-02 16:51:16 -0800 | [diff] [blame] | 3448 | } |
| 3449 | } |
| 3450 | currentVersion = 123; |
| 3451 | } |
Bryce Lee | bd17928 | 2015-12-17 19:01:37 -0800 | [diff] [blame] | 3452 | |
| 3453 | if (currentVersion == 123) { |
Bryce Lee | ec85f34 | 2015-12-16 13:32:28 -0800 | [diff] [blame] | 3454 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3455 | String defaultDisabledProfiles = (getContext().getResources().getString( |
| 3456 | R.string.def_bluetooth_disabled_profiles)); |
| 3457 | globalSettings.insertSettingLocked(Settings.Global.BLUETOOTH_DISABLED_PROFILES, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3458 | defaultDisabledProfiles, null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Bryce Lee | bd17928 | 2015-12-17 19:01:37 -0800 | [diff] [blame] | 3459 | currentVersion = 124; |
Bryce Lee | ec85f34 | 2015-12-16 13:32:28 -0800 | [diff] [blame] | 3460 | } |
| 3461 | |
Prathmesh Prabhu | de16b86 | 2016-03-04 15:22:24 -0800 | [diff] [blame] | 3462 | if (currentVersion == 124) { |
| 3463 | // Version 124: allow OEMs to set a default value for whether IME should be |
| 3464 | // shown when a physical keyboard is connected. |
| 3465 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3466 | Setting currentSetting = secureSettings.getSettingLocked( |
| 3467 | Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD); |
Svetoslav Ganov | fedb230 | 2016-04-26 18:36:42 -0700 | [diff] [blame] | 3468 | if (currentSetting.isNull()) { |
Prathmesh Prabhu | de16b86 | 2016-03-04 15:22:24 -0800 | [diff] [blame] | 3469 | secureSettings.insertSettingLocked( |
| 3470 | Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD, |
| 3471 | getContext().getResources().getBoolean( |
| 3472 | R.bool.def_show_ime_with_hard_keyboard) ? "1" : "0", |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3473 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Prathmesh Prabhu | de16b86 | 2016-03-04 15:22:24 -0800 | [diff] [blame] | 3474 | } |
| 3475 | currentVersion = 125; |
| 3476 | } |
| 3477 | |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 3478 | if (currentVersion == 125) { |
| 3479 | // Version 125: Allow OEMs to set the default VR service. |
| 3480 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3481 | |
| 3482 | Setting currentSetting = secureSettings.getSettingLocked( |
| 3483 | Settings.Secure.ENABLED_VR_LISTENERS); |
Svetoslav Ganov | fedb230 | 2016-04-26 18:36:42 -0700 | [diff] [blame] | 3484 | if (currentSetting.isNull()) { |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 3485 | ArraySet<ComponentName> l = |
| 3486 | SystemConfig.getInstance().getDefaultVrComponents(); |
| 3487 | |
| 3488 | if (l != null && !l.isEmpty()) { |
| 3489 | StringBuilder b = new StringBuilder(); |
| 3490 | boolean start = true; |
| 3491 | for (ComponentName c : l) { |
| 3492 | if (!start) { |
| 3493 | b.append(':'); |
| 3494 | } |
| 3495 | b.append(c.flattenToString()); |
| 3496 | start = false; |
| 3497 | } |
| 3498 | secureSettings.insertSettingLocked( |
| 3499 | Settings.Secure.ENABLED_VR_LISTENERS, b.toString(), |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3500 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 3501 | } |
| 3502 | |
| 3503 | } |
| 3504 | currentVersion = 126; |
| 3505 | } |
| 3506 | |
Daniel U | 02ba612 | 2016-04-01 18:41:42 +0100 | [diff] [blame] | 3507 | if (currentVersion == 126) { |
| 3508 | // Version 126: copy the primary values of LOCK_SCREEN_SHOW_NOTIFICATIONS and |
| 3509 | // LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS into managed profile. |
| 3510 | if (mUserManager.isManagedProfile(userId)) { |
| 3511 | final SettingsState systemSecureSettings = |
| 3512 | getSecureSettingsLocked(UserHandle.USER_SYSTEM); |
| 3513 | |
| 3514 | final Setting showNotifications = systemSecureSettings.getSettingLocked( |
| 3515 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS); |
Seigo Nonaka | 6e5b602 | 2016-04-27 16:32:44 +0900 | [diff] [blame] | 3516 | if (!showNotifications.isNull()) { |
Daniel U | 02ba612 | 2016-04-01 18:41:42 +0100 | [diff] [blame] | 3517 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3518 | secureSettings.insertSettingLocked( |
| 3519 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3520 | showNotifications.getValue(), null, true, |
Daniel U | 02ba612 | 2016-04-01 18:41:42 +0100 | [diff] [blame] | 3521 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 3522 | } |
| 3523 | |
| 3524 | final Setting allowPrivate = systemSecureSettings.getSettingLocked( |
| 3525 | Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS); |
Seigo Nonaka | 6e5b602 | 2016-04-27 16:32:44 +0900 | [diff] [blame] | 3526 | if (!allowPrivate.isNull()) { |
Daniel U | 02ba612 | 2016-04-01 18:41:42 +0100 | [diff] [blame] | 3527 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3528 | secureSettings.insertSettingLocked( |
| 3529 | Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3530 | allowPrivate.getValue(), null, true, |
Daniel U | 02ba612 | 2016-04-01 18:41:42 +0100 | [diff] [blame] | 3531 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 3532 | } |
| 3533 | } |
| 3534 | currentVersion = 127; |
| 3535 | } |
| 3536 | |
Steven Ng | dc20ba6 | 2016-04-26 18:19:04 +0100 | [diff] [blame] | 3537 | if (currentVersion == 127) { |
Mahaver Chopra | 3d9805d | 2016-07-07 16:25:05 +0100 | [diff] [blame] | 3538 | // version 127 is no longer used. |
Steven Ng | dc20ba6 | 2016-04-26 18:19:04 +0100 | [diff] [blame] | 3539 | currentVersion = 128; |
| 3540 | } |
| 3541 | |
Julia Reynolds | 1f721e1 | 2016-07-11 08:50:58 -0400 | [diff] [blame] | 3542 | if (currentVersion == 128) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3543 | // Version 128: Removed |
Julia Reynolds | 1f721e1 | 2016-07-11 08:50:58 -0400 | [diff] [blame] | 3544 | currentVersion = 129; |
| 3545 | } |
| 3546 | |
Dan Sandler | 71f85e9 | 2016-07-20 13:46:05 -0400 | [diff] [blame] | 3547 | if (currentVersion == 129) { |
| 3548 | // default longpress timeout changed from 500 to 400. If unchanged from the old |
| 3549 | // default, update to the new default. |
| 3550 | final SettingsState systemSecureSettings = |
| 3551 | getSecureSettingsLocked(userId); |
| 3552 | final String oldValue = systemSecureSettings.getSettingLocked( |
| 3553 | Settings.Secure.LONG_PRESS_TIMEOUT).getValue(); |
| 3554 | if (TextUtils.equals("500", oldValue)) { |
| 3555 | systemSecureSettings.insertSettingLocked( |
| 3556 | Settings.Secure.LONG_PRESS_TIMEOUT, |
| 3557 | String.valueOf(getContext().getResources().getInteger( |
| 3558 | R.integer.def_long_press_timeout_millis)), |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3559 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Dan Sandler | 71f85e9 | 2016-07-20 13:46:05 -0400 | [diff] [blame] | 3560 | } |
| 3561 | currentVersion = 130; |
| 3562 | } |
| 3563 | |
Anthony Hugh | 96e9cc5 | 2016-07-12 15:17:24 -0700 | [diff] [blame] | 3564 | if (currentVersion == 130) { |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3565 | // Split Ambient settings |
| 3566 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3567 | boolean dozeExplicitlyDisabled = "0".equals(secureSettings. |
| 3568 | getSettingLocked(Settings.Secure.DOZE_ENABLED).getValue()); |
| 3569 | |
| 3570 | if (dozeExplicitlyDisabled) { |
Lucas Dupin | 4359b55 | 2018-08-09 15:07:54 -0700 | [diff] [blame] | 3571 | secureSettings.insertSettingLocked(Settings.Secure.DOZE_PICK_UP_GESTURE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3572 | "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Lucas Dupin | 4359b55 | 2018-08-09 15:07:54 -0700 | [diff] [blame] | 3573 | secureSettings.insertSettingLocked(Settings.Secure.DOZE_DOUBLE_TAP_GESTURE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3574 | "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3575 | } |
| 3576 | currentVersion = 131; |
| 3577 | } |
| 3578 | |
| 3579 | if (currentVersion == 131) { |
Anthony Hugh | 96e9cc5 | 2016-07-12 15:17:24 -0700 | [diff] [blame] | 3580 | // Initialize new multi-press timeout to default value |
| 3581 | final SettingsState systemSecureSettings = getSecureSettingsLocked(userId); |
| 3582 | final String oldValue = systemSecureSettings.getSettingLocked( |
| 3583 | Settings.Secure.MULTI_PRESS_TIMEOUT).getValue(); |
| 3584 | if (TextUtils.equals(null, oldValue)) { |
| 3585 | systemSecureSettings.insertSettingLocked( |
| 3586 | Settings.Secure.MULTI_PRESS_TIMEOUT, |
| 3587 | String.valueOf(getContext().getResources().getInteger( |
| 3588 | R.integer.def_multi_press_timeout_millis)), |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3589 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Anthony Hugh | 96e9cc5 | 2016-07-12 15:17:24 -0700 | [diff] [blame] | 3590 | } |
| 3591 | |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3592 | currentVersion = 132; |
Anthony Hugh | 96e9cc5 | 2016-07-12 15:17:24 -0700 | [diff] [blame] | 3593 | } |
| 3594 | |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3595 | if (currentVersion == 132) { |
| 3596 | // Version 132: Allow managed profile to optionally use the parent's ringtones |
Andre Lago | ea35e07 | 2016-08-04 13:41:13 +0100 | [diff] [blame] | 3597 | final SettingsState systemSecureSettings = getSecureSettingsLocked(userId); |
| 3598 | String defaultSyncParentSounds = (getContext().getResources() |
| 3599 | .getBoolean(R.bool.def_sync_parent_sounds) ? "1" : "0"); |
| 3600 | systemSecureSettings.insertSettingLocked( |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3601 | Settings.Secure.SYNC_PARENT_SOUNDS, defaultSyncParentSounds, |
| 3602 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3603 | currentVersion = 133; |
Andre Lago | ea35e07 | 2016-08-04 13:41:13 +0100 | [diff] [blame] | 3604 | } |
| 3605 | |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3606 | if (currentVersion == 133) { |
| 3607 | // Version 133: Add default end button behavior |
Keun-young Park | ec7a118 | 2016-10-18 11:52:38 -0700 | [diff] [blame] | 3608 | final SettingsState systemSettings = getSystemSettingsLocked(userId); |
davidln | edb31e1 | 2018-09-25 10:21:20 -0700 | [diff] [blame] | 3609 | if (systemSettings.getSettingLocked(Settings.System.END_BUTTON_BEHAVIOR) |
| 3610 | .isNull()) { |
Keun-young Park | ec7a118 | 2016-10-18 11:52:38 -0700 | [diff] [blame] | 3611 | String defaultEndButtonBehavior = Integer.toString(getContext() |
| 3612 | .getResources().getInteger(R.integer.def_end_button_behavior)); |
| 3613 | systemSettings.insertSettingLocked(Settings.System.END_BUTTON_BEHAVIOR, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3614 | defaultEndButtonBehavior, null, true, |
| 3615 | SettingsState.SYSTEM_PACKAGE_NAME); |
Keun-young Park | ec7a118 | 2016-10-18 11:52:38 -0700 | [diff] [blame] | 3616 | } |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3617 | currentVersion = 134; |
Keun-young Park | ec7a118 | 2016-10-18 11:52:38 -0700 | [diff] [blame] | 3618 | } |
| 3619 | |
Phil Weaver | 89e3ffc | 2016-09-19 13:51:10 -0700 | [diff] [blame] | 3620 | if (currentVersion == 134) { |
| 3621 | // Remove setting that specifies if magnification values should be preserved. |
| 3622 | // This setting defaulted to true and never has a UI. |
| 3623 | getSecureSettingsLocked(userId).deleteSettingLocked( |
| 3624 | Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_AUTO_UPDATE); |
| 3625 | currentVersion = 135; |
| 3626 | } |
| 3627 | |
Jeremy Joslin | 8bdad34 | 2016-12-14 11:46:47 -0800 | [diff] [blame] | 3628 | if (currentVersion == 135) { |
Jeremy Joslin | c9eb3c4 | 2017-02-08 10:45:30 -0800 | [diff] [blame] | 3629 | // Version 135 no longer used. |
Jeremy Joslin | 8bdad34 | 2016-12-14 11:46:47 -0800 | [diff] [blame] | 3630 | currentVersion = 136; |
| 3631 | } |
| 3632 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3633 | if (currentVersion == 136) { |
| 3634 | // Version 136: Store legacy SSAID for all apps currently installed on the |
| 3635 | // device as first step in migrating SSAID to be unique per application. |
| 3636 | |
| 3637 | final boolean isUpgrade; |
| 3638 | try { |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 3639 | isUpgrade = mPackageManager.isDeviceUpgrading(); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3640 | } catch (RemoteException e) { |
| 3641 | throw new IllegalStateException("Package manager not available"); |
| 3642 | } |
| 3643 | // Only retain legacy ssaid if the device is performing an OTA. After wiping |
| 3644 | // user data or first boot on a new device should use new ssaid generation. |
| 3645 | if (isUpgrade) { |
| 3646 | // Retrieve the legacy ssaid from the secure settings table. |
Mark Rathjen | ea61759 | 2017-01-18 23:03:41 -0800 | [diff] [blame] | 3647 | final Setting legacySsaidSetting = getSettingLocked(SETTINGS_TYPE_SECURE, |
| 3648 | userId, Settings.Secure.ANDROID_ID); |
| 3649 | if (legacySsaidSetting == null || legacySsaidSetting.isNull() |
| 3650 | || legacySsaidSetting.getValue() == null) { |
| 3651 | throw new IllegalStateException("Legacy ssaid not accessible"); |
| 3652 | } |
| 3653 | final String legacySsaid = legacySsaidSetting.getValue(); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3654 | |
| 3655 | // Fill each uid with the legacy ssaid to be backwards compatible. |
| 3656 | final List<PackageInfo> packages; |
| 3657 | try { |
Tetsutoki Shiozawa | ebe0e5f | 2018-01-17 11:07:09 +0900 | [diff] [blame] | 3658 | packages = mPackageManager.getInstalledPackages( |
| 3659 | PackageManager.MATCH_UNINSTALLED_PACKAGES, |
| 3660 | userId).getList(); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3661 | } catch (RemoteException e) { |
| 3662 | throw new IllegalStateException("Package manager not available"); |
| 3663 | } |
| 3664 | |
| 3665 | final SettingsState ssaidSettings = getSsaidSettingsLocked(userId); |
| 3666 | for (PackageInfo info : packages) { |
| 3667 | // Check if the UID already has an entry in the table. |
| 3668 | final String uid = Integer.toString(info.applicationInfo.uid); |
| 3669 | final Setting ssaid = ssaidSettings.getSettingLocked(uid); |
| 3670 | |
| 3671 | if (ssaid.isNull() || ssaid.getValue() == null) { |
| 3672 | // Android Id doesn't exist for this package so create it. |
| 3673 | ssaidSettings.insertSettingLocked(uid, legacySsaid, null, true, |
| 3674 | info.packageName); |
Tetsutoki Shiozawa | ebe0e5f | 2018-01-17 11:07:09 +0900 | [diff] [blame] | 3675 | if (DEBUG) { |
| 3676 | Slog.d(LOG_TAG, "Keep the legacy ssaid for uid=" + uid); |
| 3677 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3678 | } |
| 3679 | } |
| 3680 | } |
| 3681 | |
| 3682 | currentVersion = 137; |
| 3683 | } |
Suprabh Shukla | e3745ee | 2017-02-02 20:01:11 -0800 | [diff] [blame] | 3684 | if (currentVersion == 137) { |
| 3685 | // Version 138: Settings.Secure#INSTALL_NON_MARKET_APPS is deprecated and its |
| 3686 | // default value set to 1. The user can no longer change the value of this |
| 3687 | // setting through the UI. |
| 3688 | final SettingsState secureSetting = getSecureSettingsLocked(userId); |
| 3689 | if (!mUserManager.hasUserRestriction( |
Suprabh Shukla | 0b1356f | 2017-02-21 14:33:50 -0800 | [diff] [blame] | 3690 | UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, UserHandle.of(userId)) |
| 3691 | && secureSetting.getSettingLocked( |
| 3692 | Settings.Secure.INSTALL_NON_MARKET_APPS).getValue().equals("0")) { |
| 3693 | |
Suprabh Shukla | e3745ee | 2017-02-02 20:01:11 -0800 | [diff] [blame] | 3694 | secureSetting.insertSettingLocked(Settings.Secure.INSTALL_NON_MARKET_APPS, |
| 3695 | "1", null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Suprabh Shukla | 0b1356f | 2017-02-21 14:33:50 -0800 | [diff] [blame] | 3696 | // For managed profiles with profile owners, DevicePolicyManagerService |
| 3697 | // may want to set the user restriction in this case |
| 3698 | secureSetting.insertSettingLocked( |
| 3699 | Settings.Secure.UNKNOWN_SOURCES_DEFAULT_REVERSED, "1", null, true, |
| 3700 | SettingsState.SYSTEM_PACKAGE_NAME); |
Suprabh Shukla | e3745ee | 2017-02-02 20:01:11 -0800 | [diff] [blame] | 3701 | } |
| 3702 | currentVersion = 138; |
| 3703 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3704 | |
Jeremy Joslin | c9eb3c4 | 2017-02-08 10:45:30 -0800 | [diff] [blame] | 3705 | if (currentVersion == 138) { |
Jeremy Joslin | 27d14c4 | 2017-02-15 12:02:03 -0800 | [diff] [blame] | 3706 | // Version 139: Removed. |
Jeremy Joslin | c9eb3c4 | 2017-02-08 10:45:30 -0800 | [diff] [blame] | 3707 | currentVersion = 139; |
| 3708 | } |
| 3709 | |
Phil Weaver | 385912e | 2017-02-10 10:06:56 -0800 | [diff] [blame] | 3710 | if (currentVersion == 139) { |
| 3711 | // Version 140: Settings.Secure#ACCESSIBILITY_SPEAK_PASSWORD is deprecated and |
| 3712 | // the user can no longer change the value of this setting through the UI. |
| 3713 | // Force to true. |
| 3714 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3715 | secureSettings.updateSettingLocked(Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD, |
| 3716 | "1", null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3717 | currentVersion = 140; |
| 3718 | } |
| 3719 | |
Julia Reynolds | ad0d9e0 | 2017-02-15 08:41:48 -0500 | [diff] [blame] | 3720 | if (currentVersion == 140) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3721 | // Version 141: Removed |
Julia Reynolds | ad0d9e0 | 2017-02-15 08:41:48 -0500 | [diff] [blame] | 3722 | currentVersion = 141; |
| 3723 | } |
| 3724 | |
Svet Ganov | 1370155 | 2017-02-23 12:45:17 -0800 | [diff] [blame] | 3725 | if (currentVersion == 141) { |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 3726 | // This implementation was incorrectly setting the current value of |
| 3727 | // settings changed by non-system packages as the default which default |
| 3728 | // is set by the system. We add a new upgrade step at the end to properly |
| 3729 | // handle this case which would also fix incorrect changes made by the |
| 3730 | // old implementation of this step. |
Svet Ganov | 1370155 | 2017-02-23 12:45:17 -0800 | [diff] [blame] | 3731 | currentVersion = 142; |
| 3732 | } |
| 3733 | |
Stephen Chen | 5d0922f | 2017-03-27 10:28:04 -0700 | [diff] [blame] | 3734 | if (currentVersion == 142) { |
Felipe Leme | ff35509 | 2017-04-03 12:55:02 -0700 | [diff] [blame] | 3735 | // Version 143: Set a default value for Wi-Fi wakeup feature. |
Stephen Chen | 5d0922f | 2017-03-27 10:28:04 -0700 | [diff] [blame] | 3736 | if (userId == UserHandle.USER_SYSTEM) { |
| 3737 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3738 | Setting currentSetting = globalSettings.getSettingLocked( |
| 3739 | Settings.Global.WIFI_WAKEUP_ENABLED); |
| 3740 | if (currentSetting.isNull()) { |
| 3741 | globalSettings.insertSettingLocked( |
| 3742 | Settings.Global.WIFI_WAKEUP_ENABLED, |
| 3743 | getContext().getResources().getBoolean( |
| 3744 | R.bool.def_wifi_wakeup_enabled) ? "1" : "0", |
| 3745 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3746 | } |
| 3747 | } |
| 3748 | |
| 3749 | currentVersion = 143; |
| 3750 | } |
| 3751 | |
Felipe Leme | ff35509 | 2017-04-03 12:55:02 -0700 | [diff] [blame] | 3752 | if (currentVersion == 143) { |
| 3753 | // Version 144: Set a default value for Autofill service. |
| 3754 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3755 | final Setting currentSetting = secureSettings |
| 3756 | .getSettingLocked(Settings.Secure.AUTOFILL_SERVICE); |
| 3757 | if (currentSetting.isNull()) { |
| 3758 | final String defaultValue = getContext().getResources().getString( |
| 3759 | com.android.internal.R.string.config_defaultAutofillService); |
| 3760 | if (defaultValue != null) { |
| 3761 | Slog.d(LOG_TAG, "Setting [" + defaultValue + "] as Autofill Service " |
| 3762 | + "for user " + userId); |
| 3763 | secureSettings.insertSettingLocked(Settings.Secure.AUTOFILL_SERVICE, |
| 3764 | defaultValue, null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3765 | } |
| 3766 | } |
| 3767 | |
| 3768 | currentVersion = 144; |
| 3769 | } |
| 3770 | |
Jeremy Joslin | 45caa25 | 2017-05-04 11:22:46 -0700 | [diff] [blame] | 3771 | if (currentVersion == 144) { |
Amin Shaikh | 8636796 | 2017-06-07 08:58:22 -0700 | [diff] [blame] | 3772 | // Version 145: Removed |
| 3773 | currentVersion = 145; |
| 3774 | } |
| 3775 | |
| 3776 | if (currentVersion == 145) { |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 3777 | // Version 146: In step 142 we had a bug where incorrectly |
| 3778 | // some settings were considered system set and as a result |
| 3779 | // made the default and marked as the default being set by |
| 3780 | // the system. Here reevaluate the default and default system |
| 3781 | // set flags. This would both fix corruption by the old impl |
| 3782 | // of step 142 and also properly handle devices which never |
| 3783 | // run 142. |
Jeremy Joslin | 45caa25 | 2017-05-04 11:22:46 -0700 | [diff] [blame] | 3784 | if (userId == UserHandle.USER_SYSTEM) { |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 3785 | SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3786 | ensureLegacyDefaultValueAndSystemSetUpdatedLocked(globalSettings, userId); |
| 3787 | globalSettings.persistSyncLocked(); |
Jeremy Joslin | 45caa25 | 2017-05-04 11:22:46 -0700 | [diff] [blame] | 3788 | } |
| 3789 | |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 3790 | SettingsState secureSettings = getSecureSettingsLocked(mUserId); |
| 3791 | ensureLegacyDefaultValueAndSystemSetUpdatedLocked(secureSettings, userId); |
| 3792 | secureSettings.persistSyncLocked(); |
| 3793 | |
| 3794 | SettingsState systemSettings = getSystemSettingsLocked(mUserId); |
| 3795 | ensureLegacyDefaultValueAndSystemSetUpdatedLocked(systemSettings, userId); |
| 3796 | systemSettings.persistSyncLocked(); |
| 3797 | |
Amin Shaikh | 8636796 | 2017-06-07 08:58:22 -0700 | [diff] [blame] | 3798 | currentVersion = 146; |
Jeremy Joslin | 45caa25 | 2017-05-04 11:22:46 -0700 | [diff] [blame] | 3799 | } |
| 3800 | |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 3801 | if (currentVersion == 146) { |
Joe LaPenna | 250d784 | 2018-01-25 10:19:42 -0800 | [diff] [blame] | 3802 | // Version 147: Removed. (This version previously allowed showing the |
| 3803 | // "wifi_wakeup_available" setting). |
| 3804 | // The setting that was added here is deleted in 153. |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 3805 | currentVersion = 147; |
| 3806 | } |
| 3807 | |
| 3808 | if (currentVersion == 147) { |
| 3809 | // Version 148: Set the default value for DEFAULT_RESTRICT_BACKGROUND_DATA. |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 3810 | if (userId == UserHandle.USER_SYSTEM) { |
| 3811 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3812 | final Setting currentSetting = globalSettings.getSettingLocked( |
| 3813 | Global.DEFAULT_RESTRICT_BACKGROUND_DATA); |
| 3814 | if (currentSetting.isNull()) { |
| 3815 | globalSettings.insertSettingLocked( |
| 3816 | Global.DEFAULT_RESTRICT_BACKGROUND_DATA, |
| 3817 | getContext().getResources().getBoolean( |
| 3818 | R.bool.def_restrict_background_data) ? "1" : "0", |
| 3819 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3820 | } |
| 3821 | } |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 3822 | currentVersion = 148; |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 3823 | } |
| 3824 | |
Tim Zheng | cc1e76a | 2017-08-30 17:46:19 -0700 | [diff] [blame] | 3825 | if (currentVersion == 148) { |
| 3826 | // Version 149: Set the default value for BACKUP_MANAGER_CONSTANTS. |
| 3827 | final SettingsState systemSecureSettings = getSecureSettingsLocked(userId); |
| 3828 | final String oldValue = systemSecureSettings.getSettingLocked( |
| 3829 | Settings.Secure.BACKUP_MANAGER_CONSTANTS).getValue(); |
| 3830 | if (TextUtils.equals(null, oldValue)) { |
| 3831 | final String defaultValue = getContext().getResources().getString( |
| 3832 | R.string.def_backup_manager_constants); |
| 3833 | if (!TextUtils.isEmpty(defaultValue)) { |
| 3834 | systemSecureSettings.insertSettingLocked( |
| 3835 | Settings.Secure.BACKUP_MANAGER_CONSTANTS, defaultValue, null, |
| 3836 | true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3837 | } |
| 3838 | } |
Tim Zheng | cc1e76a | 2017-08-30 17:46:19 -0700 | [diff] [blame] | 3839 | currentVersion = 149; |
| 3840 | } |
Jacky Cheung | 7076a31 | 2017-10-02 10:40:48 -0700 | [diff] [blame] | 3841 | |
| 3842 | if (currentVersion == 149) { |
| 3843 | // Version 150: Set a default value for mobile data always on |
| 3844 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3845 | final Setting currentSetting = globalSettings.getSettingLocked( |
| 3846 | Settings.Global.MOBILE_DATA_ALWAYS_ON); |
| 3847 | if (currentSetting.isNull()) { |
| 3848 | globalSettings.insertSettingLocked( |
| 3849 | Settings.Global.MOBILE_DATA_ALWAYS_ON, |
| 3850 | getContext().getResources().getBoolean( |
| 3851 | R.bool.def_mobile_data_always_on) ? "1" : "0", |
| 3852 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3853 | } |
| 3854 | |
| 3855 | currentVersion = 150; |
| 3856 | } |
| 3857 | |
Mike Digman | 4af4a6f | 2018-01-16 14:49:38 -0800 | [diff] [blame] | 3858 | if (currentVersion == 150) { |
Mike Digman | 32e0331 | 2018-05-16 16:43:23 -0700 | [diff] [blame] | 3859 | // Version 151: Removed. |
Mike Digman | 4af4a6f | 2018-01-16 14:49:38 -0800 | [diff] [blame] | 3860 | currentVersion = 151; |
| 3861 | } |
| 3862 | |
Joe LaPenna | 4f50ed4 | 2018-01-22 14:54:45 -0800 | [diff] [blame] | 3863 | if (currentVersion == 151) { |
Joe LaPenna | 250d784 | 2018-01-25 10:19:42 -0800 | [diff] [blame] | 3864 | // Version 152: Removed. (This version made the setting for wifi_wakeup enabled |
| 3865 | // by default but it is now no longer configurable). |
| 3866 | // The setting updated here is deleted in 153. |
Joe LaPenna | 4f50ed4 | 2018-01-22 14:54:45 -0800 | [diff] [blame] | 3867 | currentVersion = 152; |
| 3868 | } |
| 3869 | |
Joe LaPenna | 250d784 | 2018-01-25 10:19:42 -0800 | [diff] [blame] | 3870 | if (currentVersion == 152) { |
| 3871 | getGlobalSettingsLocked().deleteSettingLocked("wifi_wakeup_available"); |
| 3872 | currentVersion = 153; |
| 3873 | } |
| 3874 | |
Ben Lin | b4df8bc | 2018-01-29 11:48:20 -0800 | [diff] [blame] | 3875 | if (currentVersion == 153) { |
| 3876 | // Version 154: Read notification badge configuration from config. |
| 3877 | // If user has already set the value, don't do anything. |
| 3878 | final SettingsState systemSecureSettings = getSecureSettingsLocked(userId); |
| 3879 | final Setting showNotificationBadges = systemSecureSettings.getSettingLocked( |
| 3880 | Settings.Secure.NOTIFICATION_BADGING); |
| 3881 | if (showNotificationBadges.isNull()) { |
| 3882 | final boolean defaultValue = getContext().getResources().getBoolean( |
| 3883 | com.android.internal.R.bool.config_notificationBadging); |
| 3884 | systemSecureSettings.insertSettingLocked( |
| 3885 | Secure.NOTIFICATION_BADGING, |
| 3886 | defaultValue ? "1" : "0", |
| 3887 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3888 | } |
| 3889 | currentVersion = 154; |
| 3890 | } |
| 3891 | |
Bernardo Rufino | eaa78b9 | 2018-01-26 11:25:37 +0000 | [diff] [blame] | 3892 | if (currentVersion == 154) { |
| 3893 | // Version 155: Set the default value for BACKUP_LOCAL_TRANSPORT_PARAMETERS. |
| 3894 | final SettingsState systemSecureSettings = getSecureSettingsLocked(userId); |
| 3895 | final String oldValue = systemSecureSettings.getSettingLocked( |
| 3896 | Settings.Secure.BACKUP_LOCAL_TRANSPORT_PARAMETERS).getValue(); |
| 3897 | if (TextUtils.equals(null, oldValue)) { |
| 3898 | final String defaultValue = getContext().getResources().getString( |
| 3899 | R.string.def_backup_local_transport_parameters); |
| 3900 | if (!TextUtils.isEmpty(defaultValue)) { |
| 3901 | systemSecureSettings.insertSettingLocked( |
| 3902 | Settings.Secure.BACKUP_LOCAL_TRANSPORT_PARAMETERS, defaultValue, |
| 3903 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3904 | } |
| 3905 | |
| 3906 | } |
| 3907 | currentVersion = 155; |
| 3908 | } |
| 3909 | |
Beverly | da90481 | 2018-03-02 09:55:30 -0500 | [diff] [blame] | 3910 | if (currentVersion == 155) { |
Beverly | f364d7c | 2019-10-10 16:44:43 -0400 | [diff] [blame] | 3911 | // Version 156: migrated to version 184 |
Beverly | da90481 | 2018-03-02 09:55:30 -0500 | [diff] [blame] | 3912 | currentVersion = 156; |
| 3913 | } |
| 3914 | |
Beverly | 09da25f | 2018-02-26 09:17:07 -0500 | [diff] [blame] | 3915 | if (currentVersion == 156) { |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 3916 | // Version 157: Set a default value for zen duration, |
| 3917 | // in version 169, zen duration is moved to secure settings |
Beverly | 09da25f | 2018-02-26 09:17:07 -0500 | [diff] [blame] | 3918 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3919 | final Setting currentSetting = globalSettings.getSettingLocked( |
| 3920 | Global.ZEN_DURATION); |
| 3921 | if (currentSetting.isNull()) { |
| 3922 | String defaultZenDuration = Integer.toString(getContext() |
| 3923 | .getResources().getInteger(R.integer.def_zen_duration)); |
| 3924 | globalSettings.insertSettingLocked( |
| 3925 | Global.ZEN_DURATION, defaultZenDuration, |
| 3926 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3927 | } |
Beverly | 09da25f | 2018-02-26 09:17:07 -0500 | [diff] [blame] | 3928 | currentVersion = 157; |
| 3929 | } |
Annie Meng | d069a88 | 2018-03-13 15:31:40 +0000 | [diff] [blame] | 3930 | |
| 3931 | if (currentVersion == 157) { |
| 3932 | // Version 158: Set default value for BACKUP_AGENT_TIMEOUT_PARAMETERS. |
| 3933 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3934 | final String oldValue = globalSettings.getSettingLocked( |
| 3935 | Settings.Global.BACKUP_AGENT_TIMEOUT_PARAMETERS).getValue(); |
| 3936 | if (TextUtils.equals(null, oldValue)) { |
| 3937 | final String defaultValue = getContext().getResources().getString( |
| 3938 | R.string.def_backup_agent_timeout_parameters); |
| 3939 | if (!TextUtils.isEmpty(defaultValue)) { |
| 3940 | globalSettings.insertSettingLocked( |
| 3941 | Settings.Global.BACKUP_AGENT_TIMEOUT_PARAMETERS, defaultValue, |
| 3942 | null, true, |
| 3943 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 3944 | } |
| 3945 | } |
| 3946 | currentVersion = 158; |
| 3947 | } |
Roshan Pius | 9c39667 | 2018-03-02 14:54:13 -0800 | [diff] [blame] | 3948 | |
| 3949 | if (currentVersion == 158) { |
| 3950 | // Remove setting that specifies wifi bgscan throttling params |
| 3951 | getGlobalSettingsLocked().deleteSettingLocked( |
| 3952 | "wifi_scan_background_throttle_interval_ms"); |
| 3953 | getGlobalSettingsLocked().deleteSettingLocked( |
| 3954 | "wifi_scan_background_throttle_package_whitelist"); |
| 3955 | currentVersion = 159; |
| 3956 | } |
| 3957 | |
Pavel Grafov | c5c9730 | 2018-03-19 13:37:15 +0000 | [diff] [blame] | 3958 | if (currentVersion == 159) { |
| 3959 | // Version 160: Hiding notifications from the lockscreen is only available as |
| 3960 | // primary user option, profiles can only make them redacted. If a profile was |
| 3961 | // configured to not show lockscreen notifications, ensure that at the very |
| 3962 | // least these will be come hidden. |
| 3963 | if (mUserManager.isManagedProfile(userId)) { |
| 3964 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3965 | Setting showNotifications = secureSettings.getSettingLocked( |
| 3966 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS); |
| 3967 | // The default value is "1", check if user has turned it off. |
| 3968 | if ("0".equals(showNotifications.getValue())) { |
| 3969 | secureSettings.insertSettingLocked( |
| 3970 | Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, "0", |
| 3971 | null /* tag */, false /* makeDefault */, |
| 3972 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 3973 | } |
| 3974 | // The setting is no longer valid for managed profiles, it should be |
| 3975 | // treated as if it was set to "1". |
| 3976 | secureSettings.deleteSettingLocked(Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS); |
| 3977 | } |
| 3978 | currentVersion = 160; |
| 3979 | } |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 3980 | |
| 3981 | if (currentVersion == 160) { |
| 3982 | // Version 161: Set the default value for |
Philip P. Moltmann | 7e25b3d | 2018-03-09 20:22:58 -0800 | [diff] [blame] | 3983 | // MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY and |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 3984 | // SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT |
| 3985 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3986 | |
| 3987 | String oldValue = globalSettings.getSettingLocked( |
Philip P. Moltmann | 7e25b3d | 2018-03-09 20:22:58 -0800 | [diff] [blame] | 3988 | Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY).getValue(); |
| 3989 | if (TextUtils.equals(null, oldValue)) { |
| 3990 | globalSettings.insertSettingLocked( |
| 3991 | Settings.Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY, |
| 3992 | Integer.toString(getContext().getResources().getInteger( |
| 3993 | R.integer.def_max_sound_trigger_detection_service_ops_per_day)), |
| 3994 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3995 | } |
| 3996 | |
| 3997 | oldValue = globalSettings.getSettingLocked( |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 3998 | Global.SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT).getValue(); |
| 3999 | if (TextUtils.equals(null, oldValue)) { |
| 4000 | globalSettings.insertSettingLocked( |
| 4001 | Settings.Global.SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT, |
| 4002 | Integer.toString(getContext().getResources().getInteger( |
| 4003 | R.integer.def_sound_trigger_detection_service_op_timeout)), |
| 4004 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4005 | } |
| 4006 | currentVersion = 161; |
| 4007 | } |
| 4008 | |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 4009 | if (currentVersion == 161) { |
| 4010 | // Version 161: Add a gesture for silencing phones |
| 4011 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4012 | final Setting currentSetting = secureSettings.getSettingLocked( |
| 4013 | Secure.VOLUME_HUSH_GESTURE); |
| 4014 | if (currentSetting.isNull()) { |
| 4015 | secureSettings.insertSettingLocked( |
| 4016 | Secure.VOLUME_HUSH_GESTURE, |
| 4017 | Integer.toString(Secure.VOLUME_HUSH_VIBRATE), |
| 4018 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4019 | } |
| 4020 | |
| 4021 | currentVersion = 162; |
| 4022 | } |
| 4023 | |
Julia Reynolds | c4e5ecf | 2018-04-11 11:33:32 -0400 | [diff] [blame] | 4024 | if (currentVersion == 162) { |
Julia Reynolds | 76bfa60 | 2018-04-23 09:38:47 -0400 | [diff] [blame] | 4025 | // Version 162: REMOVED: Add a gesture for silencing phones |
Julia Reynolds | c4e5ecf | 2018-04-11 11:33:32 -0400 | [diff] [blame] | 4026 | currentVersion = 163; |
| 4027 | } |
| 4028 | |
Philip P. Moltmann | cb58a83 | 2018-04-16 09:19:42 -0700 | [diff] [blame] | 4029 | if (currentVersion == 163) { |
| 4030 | // Version 163: Update default value of |
| 4031 | // MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY from old to new default |
| 4032 | final SettingsState settings = getGlobalSettingsLocked(); |
| 4033 | final Setting currentSetting = settings.getSettingLocked( |
| 4034 | Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY); |
| 4035 | if (currentSetting.isDefaultFromSystem()) { |
| 4036 | settings.insertSettingLocked( |
| 4037 | Settings.Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY, |
| 4038 | Integer.toString(getContext().getResources().getInteger( |
| 4039 | R.integer |
| 4040 | .def_max_sound_trigger_detection_service_ops_per_day)), |
| 4041 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4042 | } |
| 4043 | |
| 4044 | currentVersion = 164; |
| 4045 | } |
| 4046 | |
Julia Reynolds | 76bfa60 | 2018-04-23 09:38:47 -0400 | [diff] [blame] | 4047 | if (currentVersion == 164) { |
Julia Reynolds | 24836c5 | 2018-06-06 14:36:03 -0400 | [diff] [blame] | 4048 | // Version 164: REMOVED: show zen upgrade notification |
Julia Reynolds | 76bfa60 | 2018-04-23 09:38:47 -0400 | [diff] [blame] | 4049 | currentVersion = 165; |
| 4050 | } |
| 4051 | |
Beverly | 301e92a | 2018-04-27 09:43:05 -0400 | [diff] [blame] | 4052 | if (currentVersion == 165) { |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 4053 | // Version 165: MOVED: Show zen settings suggestion and zen updated settings |
| 4054 | // moved to secure settings and are set in version 169 |
Beverly | 301e92a | 2018-04-27 09:43:05 -0400 | [diff] [blame] | 4055 | currentVersion = 166; |
| 4056 | } |
| 4057 | |
Beverly | 38fcfd0 | 2018-05-18 17:33:40 -0400 | [diff] [blame] | 4058 | if (currentVersion == 166) { |
| 4059 | // Version 166: add default values for hush gesture used and manual ringer |
| 4060 | // toggle |
| 4061 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4062 | Setting currentHushUsedSetting = secureSettings.getSettingLocked( |
| 4063 | Secure.HUSH_GESTURE_USED); |
| 4064 | if (currentHushUsedSetting.isNull()) { |
| 4065 | secureSettings.insertSettingLocked( |
| 4066 | Settings.Secure.HUSH_GESTURE_USED, "0", null, true, |
| 4067 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 4068 | } |
| 4069 | |
| 4070 | Setting currentRingerToggleCountSetting = secureSettings.getSettingLocked( |
| 4071 | Secure.MANUAL_RINGER_TOGGLE_COUNT); |
| 4072 | if (currentRingerToggleCountSetting.isNull()) { |
| 4073 | secureSettings.insertSettingLocked( |
| 4074 | Settings.Secure.MANUAL_RINGER_TOGGLE_COUNT, "0", null, true, |
| 4075 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 4076 | } |
| 4077 | currentVersion = 167; |
| 4078 | } |
| 4079 | |
Beverly | 155c9d2 | 2018-05-23 18:03:23 -0400 | [diff] [blame] | 4080 | if (currentVersion == 167) { |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 4081 | // Version 167: MOVED - Settings.Global.CHARGING_VIBRATION_ENABLED moved to |
| 4082 | // Settings.Secure.CHARGING_VIBRATION_ENABLED, set in version 170 |
Beverly | 155c9d2 | 2018-05-23 18:03:23 -0400 | [diff] [blame] | 4083 | currentVersion = 168; |
| 4084 | } |
| 4085 | |
Michael Wright | 0e9eeee | 2018-05-26 00:31:20 +0100 | [diff] [blame] | 4086 | if (currentVersion == 168) { |
| 4087 | // Version 168: by default, vibrate for phone calls |
| 4088 | final SettingsState systemSettings = getSystemSettingsLocked(userId); |
| 4089 | final Setting currentSetting = systemSettings.getSettingLocked( |
| 4090 | Settings.System.VIBRATE_WHEN_RINGING); |
| 4091 | if (currentSetting.isNull()) { |
| 4092 | systemSettings.insertSettingLocked( |
| 4093 | Settings.System.VIBRATE_WHEN_RINGING, |
| 4094 | getContext().getResources().getBoolean( |
| 4095 | R.bool.def_vibrate_when_ringing) ? "1" : "0", |
| 4096 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4097 | } |
| 4098 | currentVersion = 169; |
| 4099 | } |
| 4100 | |
Nadav Bar | f9f115d | 2018-06-24 10:06:50 +0300 | [diff] [blame] | 4101 | if (currentVersion == 169) { |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 4102 | // Version 169: Set the default value for Secure Settings ZEN_DURATION, |
| 4103 | // SHOW_ZEN_SETTINGS_SUGGESTION, ZEN_SETTINGS_UPDATE and |
| 4104 | // ZEN_SETTINGS_SUGGESTION_VIEWED |
Nadav Bar | f9f115d | 2018-06-24 10:06:50 +0300 | [diff] [blame] | 4105 | |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 4106 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 4107 | final Setting globalZenDuration = globalSettings.getSettingLocked( |
| 4108 | Global.ZEN_DURATION); |
| 4109 | |
| 4110 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4111 | final Setting secureZenDuration = secureSettings.getSettingLocked( |
| 4112 | Secure.ZEN_DURATION); |
| 4113 | |
| 4114 | // ZEN_DURATION |
| 4115 | if (!globalZenDuration.isNull()) { |
| 4116 | secureSettings.insertSettingLocked( |
| 4117 | Secure.ZEN_DURATION, globalZenDuration.getValue(), null, false, |
| 4118 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 4119 | |
| 4120 | // set global zen duration setting to null since it's deprecated |
| 4121 | globalSettings.insertSettingLocked( |
| 4122 | Global.ZEN_DURATION, null, null, true, |
| 4123 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 4124 | } else if (secureZenDuration.isNull()) { |
| 4125 | String defaultZenDuration = Integer.toString(getContext() |
| 4126 | .getResources().getInteger(R.integer.def_zen_duration)); |
| 4127 | secureSettings.insertSettingLocked( |
| 4128 | Secure.ZEN_DURATION, defaultZenDuration, null, true, |
| 4129 | SettingsState.SYSTEM_PACKAGE_NAME); |
Nadav Bar | f9f115d | 2018-06-24 10:06:50 +0300 | [diff] [blame] | 4130 | } |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 4131 | |
| 4132 | // SHOW_ZEN_SETTINGS_SUGGESTION |
| 4133 | final Setting currentShowZenSettingSuggestion = secureSettings.getSettingLocked( |
| 4134 | Secure.SHOW_ZEN_SETTINGS_SUGGESTION); |
| 4135 | if (currentShowZenSettingSuggestion.isNull()) { |
| 4136 | secureSettings.insertSettingLocked( |
| 4137 | Secure.SHOW_ZEN_SETTINGS_SUGGESTION, "1", |
| 4138 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4139 | } |
| 4140 | |
| 4141 | // ZEN_SETTINGS_UPDATED |
| 4142 | final Setting currentUpdatedSetting = secureSettings.getSettingLocked( |
| 4143 | Secure.ZEN_SETTINGS_UPDATED); |
| 4144 | if (currentUpdatedSetting.isNull()) { |
| 4145 | secureSettings.insertSettingLocked( |
| 4146 | Secure.ZEN_SETTINGS_UPDATED, "0", |
| 4147 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4148 | } |
| 4149 | |
| 4150 | // ZEN_SETTINGS_SUGGESTION_VIEWED |
| 4151 | final Setting currentSettingSuggestionViewed = secureSettings.getSettingLocked( |
| 4152 | Secure.ZEN_SETTINGS_SUGGESTION_VIEWED); |
| 4153 | if (currentSettingSuggestionViewed.isNull()) { |
| 4154 | secureSettings.insertSettingLocked( |
| 4155 | Secure.ZEN_SETTINGS_SUGGESTION_VIEWED, "0", |
| 4156 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4157 | } |
| 4158 | |
Nadav Bar | f9f115d | 2018-06-24 10:06:50 +0300 | [diff] [blame] | 4159 | currentVersion = 170; |
| 4160 | } |
| 4161 | |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 4162 | if (currentVersion == 170) { |
| 4163 | // Version 170: Set the default value for Secure Settings: |
| 4164 | // CHARGING_SOUNDS_ENABLED and CHARGING_VIBRATION_ENABLED |
| 4165 | |
| 4166 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 4167 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4168 | |
| 4169 | // CHARGING_SOUNDS_ENABLED |
| 4170 | final Setting globalChargingSoundEnabled = globalSettings.getSettingLocked( |
| 4171 | Global.CHARGING_SOUNDS_ENABLED); |
| 4172 | final Setting secureChargingSoundsEnabled = secureSettings.getSettingLocked( |
| 4173 | Secure.CHARGING_SOUNDS_ENABLED); |
| 4174 | |
| 4175 | if (!globalChargingSoundEnabled.isNull()) { |
Beverly | ff9c587 | 2019-01-02 15:44:00 -0500 | [diff] [blame] | 4176 | if (secureChargingSoundsEnabled.isNull()) { |
| 4177 | secureSettings.insertSettingLocked( |
| 4178 | Secure.CHARGING_SOUNDS_ENABLED, |
| 4179 | globalChargingSoundEnabled.getValue(), null, false, |
| 4180 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 4181 | } |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 4182 | |
| 4183 | // set global charging_sounds_enabled setting to null since it's deprecated |
| 4184 | globalSettings.insertSettingLocked( |
| 4185 | Global.CHARGING_SOUNDS_ENABLED, null, null, true, |
| 4186 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 4187 | } else if (secureChargingSoundsEnabled.isNull()) { |
| 4188 | String defChargingSoundsEnabled = getContext().getResources() |
| 4189 | .getBoolean(R.bool.def_charging_sounds_enabled) ? "1" : "0"; |
| 4190 | secureSettings.insertSettingLocked( |
| 4191 | Secure.CHARGING_SOUNDS_ENABLED, defChargingSoundsEnabled, null, |
| 4192 | true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4193 | } |
| 4194 | |
| 4195 | // CHARGING_VIBRATION_ENABLED |
| 4196 | final Setting secureChargingVibrationEnabled = secureSettings.getSettingLocked( |
| 4197 | Secure.CHARGING_VIBRATION_ENABLED); |
| 4198 | |
| 4199 | if (secureChargingVibrationEnabled.isNull()) { |
| 4200 | String defChargingVibrationEnabled = getContext().getResources() |
| 4201 | .getBoolean(R.bool.def_charging_vibration_enabled) ? "1" : "0"; |
| 4202 | secureSettings.insertSettingLocked( |
| 4203 | Secure.CHARGING_VIBRATION_ENABLED, defChargingVibrationEnabled, |
| 4204 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4205 | } |
| 4206 | |
| 4207 | currentVersion = 171; |
| 4208 | } |
| 4209 | |
Nadav Bar | 8bc8c9e | 2018-09-12 15:41:51 +0300 | [diff] [blame] | 4210 | if (currentVersion == 171) { |
| 4211 | // Version 171: by default, add STREAM_VOICE_CALL to list of streams that can |
| 4212 | // be muted. |
| 4213 | final SettingsState systemSettings = getSystemSettingsLocked(userId); |
| 4214 | final Setting currentSetting = systemSettings.getSettingLocked( |
| 4215 | Settings.System.MUTE_STREAMS_AFFECTED); |
| 4216 | if (!currentSetting.isNull()) { |
| 4217 | try { |
| 4218 | int currentSettingIntegerValue = Integer.parseInt( |
| 4219 | currentSetting.getValue()); |
| 4220 | if ((currentSettingIntegerValue |
| 4221 | & (1 << AudioManager.STREAM_VOICE_CALL)) == 0) { |
| 4222 | systemSettings.insertSettingLocked( |
| 4223 | Settings.System.MUTE_STREAMS_AFFECTED, |
| 4224 | Integer.toString( |
| 4225 | currentSettingIntegerValue |
| 4226 | | (1 << AudioManager.STREAM_VOICE_CALL)), |
| 4227 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4228 | } |
| 4229 | } catch (NumberFormatException e) { |
| 4230 | // remove the setting in case it is not a valid integer |
| 4231 | Slog.w("Failed to parse integer value of MUTE_STREAMS_AFFECTED" |
| 4232 | + "setting, removing setting", e); |
| 4233 | systemSettings.deleteSettingLocked( |
| 4234 | Settings.System.MUTE_STREAMS_AFFECTED); |
| 4235 | } |
| 4236 | |
| 4237 | } |
| 4238 | currentVersion = 172; |
| 4239 | } |
| 4240 | |
Soonil Nagarkar | 42da1b1 | 2019-01-22 11:29:27 -0800 | [diff] [blame] | 4241 | if (currentVersion == 172) { |
| 4242 | // Version 172: Set the default value for Secure Settings: LOCATION_MODE |
| 4243 | |
| 4244 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4245 | |
| 4246 | final Setting locationMode = secureSettings.getSettingLocked( |
| 4247 | Secure.LOCATION_MODE); |
| 4248 | |
| 4249 | if (locationMode.isNull()) { |
| 4250 | final Setting locationProvidersAllowed = secureSettings.getSettingLocked( |
| 4251 | Secure.LOCATION_PROVIDERS_ALLOWED); |
| 4252 | |
Soonil Nagarkar | 4ee3ac2 | 2019-02-08 19:19:24 -0800 | [diff] [blame] | 4253 | final int defLocationMode; |
| 4254 | if (locationProvidersAllowed.isNull()) { |
| 4255 | defLocationMode = getContext().getResources().getInteger( |
| 4256 | R.integer.def_location_mode); |
| 4257 | } else { |
| 4258 | defLocationMode = |
| 4259 | !TextUtils.isEmpty(locationProvidersAllowed.getValue()) |
| 4260 | ? Secure.LOCATION_MODE_ON |
| 4261 | : Secure.LOCATION_MODE_OFF; |
| 4262 | } |
Soonil Nagarkar | 42da1b1 | 2019-01-22 11:29:27 -0800 | [diff] [blame] | 4263 | secureSettings.insertSettingLocked( |
Soonil Nagarkar | 4ee3ac2 | 2019-02-08 19:19:24 -0800 | [diff] [blame] | 4264 | Secure.LOCATION_MODE, Integer.toString(defLocationMode), |
Soonil Nagarkar | 42da1b1 | 2019-01-22 11:29:27 -0800 | [diff] [blame] | 4265 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4266 | } |
| 4267 | |
| 4268 | currentVersion = 173; |
| 4269 | } |
| 4270 | |
Beverly | 4179f99 | 2019-02-08 11:34:16 -0500 | [diff] [blame] | 4271 | if (currentVersion == 173) { |
| 4272 | // Version 173: Set the default value for Secure Settings: NOTIFICATION_BUBBLES |
Lyn Han | b739fa6 | 2019-06-27 18:41:11 -0700 | [diff] [blame] | 4273 | // Removed. Moved NOTIFICATION_BUBBLES to Global Settings. |
Beverly | 4179f99 | 2019-02-08 11:34:16 -0500 | [diff] [blame] | 4274 | currentVersion = 174; |
| 4275 | } |
| 4276 | |
Yiwen Chen | 0305b57 | 2019-03-05 11:26:59 -0800 | [diff] [blame] | 4277 | if (currentVersion == 174) { |
| 4278 | // Version 174: Set the default value for Global Settings: APPLY_RAMPING_RINGER |
| 4279 | |
| 4280 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 4281 | |
| 4282 | Setting currentRampingRingerSetting = globalSettings.getSettingLocked( |
| 4283 | Settings.Global.APPLY_RAMPING_RINGER); |
| 4284 | if (currentRampingRingerSetting.isNull()) { |
| 4285 | globalSettings.insertSettingLocked( |
| 4286 | Settings.Global.APPLY_RAMPING_RINGER, |
| 4287 | getContext().getResources().getBoolean( |
| 4288 | R.bool.def_apply_ramping_ringer) ? "1" : "0", null, |
| 4289 | true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4290 | } |
| 4291 | |
| 4292 | currentVersion = 175; |
| 4293 | } |
| 4294 | |
wilsonshih | 5d999e2 | 2019-03-20 11:50:42 +0800 | [diff] [blame] | 4295 | if (currentVersion == 175) { |
| 4296 | // Version 175: Set the default value for System Settings: |
| 4297 | // RING_VIBRATION_INTENSITY. If the notification vibration intensity has been |
| 4298 | // set and ring vibration intensity hasn't, the ring vibration intensity should |
| 4299 | // followed notification vibration intensity. |
| 4300 | |
| 4301 | final SettingsState systemSettings = getSystemSettingsLocked(userId); |
| 4302 | |
| 4303 | Setting notificationVibrationIntensity = systemSettings.getSettingLocked( |
| 4304 | Settings.System.NOTIFICATION_VIBRATION_INTENSITY); |
| 4305 | |
| 4306 | Setting ringVibrationIntensity = systemSettings.getSettingLocked( |
| 4307 | Settings.System.RING_VIBRATION_INTENSITY); |
| 4308 | |
| 4309 | if (!notificationVibrationIntensity.isNull() |
| 4310 | && ringVibrationIntensity.isNull()) { |
| 4311 | systemSettings.insertSettingLocked( |
| 4312 | Settings.System.RING_VIBRATION_INTENSITY, |
| 4313 | notificationVibrationIntensity.getValue(), |
| 4314 | null , true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4315 | } |
| 4316 | |
| 4317 | currentVersion = 176; |
| 4318 | } |
| 4319 | |
Winson Chung | d9f2fb3 | 2019-03-05 11:10:12 -0800 | [diff] [blame] | 4320 | if (currentVersion == 176) { |
| 4321 | // Version 176: Migrate the existing swipe up setting into the resource overlay |
Winson Chung | 3d5d3b3 | 2019-04-24 16:52:47 -0700 | [diff] [blame] | 4322 | // for the navigation bar interaction mode. We do so only if the |
| 4323 | // setting is set. |
Winson Chung | d9f2fb3 | 2019-03-05 11:10:12 -0800 | [diff] [blame] | 4324 | |
Winson Chung | d9f2fb3 | 2019-03-05 11:10:12 -0800 | [diff] [blame] | 4325 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4326 | final Setting swipeUpSetting = secureSettings.getSettingLocked( |
Winson Chung | 85e7459 | 2019-03-06 11:49:22 -0800 | [diff] [blame] | 4327 | "swipe_up_to_switch_apps_enabled"); |
Winson Chung | 3d5d3b3 | 2019-04-24 16:52:47 -0700 | [diff] [blame] | 4328 | if (swipeUpSetting != null && !swipeUpSetting.isNull() |
| 4329 | && swipeUpSetting.getValue().equals("1")) { |
| 4330 | final IOverlayManager overlayManager = IOverlayManager.Stub.asInterface( |
| 4331 | ServiceManager.getService(Context.OVERLAY_SERVICE)); |
Winson Chung | d9f2fb3 | 2019-03-05 11:10:12 -0800 | [diff] [blame] | 4332 | try { |
Winson Chung | 3d5d3b3 | 2019-04-24 16:52:47 -0700 | [diff] [blame] | 4333 | overlayManager.setEnabledExclusiveInCategory( |
| 4334 | NAV_BAR_MODE_2BUTTON_OVERLAY, UserHandle.USER_CURRENT); |
Winson Chung | d9f2fb3 | 2019-03-05 11:10:12 -0800 | [diff] [blame] | 4335 | } catch (RemoteException e) { |
| 4336 | throw new IllegalStateException( |
| 4337 | "Failed to set nav bar interaction mode overlay"); |
| 4338 | } |
| 4339 | } |
| 4340 | |
| 4341 | currentVersion = 177; |
| 4342 | } |
| 4343 | |
Edgar Wang | eb8bddb | 2019-04-19 17:29:49 +0800 | [diff] [blame] | 4344 | if (currentVersion == 177) { |
| 4345 | // Version 177: Set the default value for Secure Settings: AWARE_ENABLED |
| 4346 | |
| 4347 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4348 | |
| 4349 | final Setting awareEnabled = secureSettings.getSettingLocked( |
| 4350 | Secure.AWARE_ENABLED); |
| 4351 | |
| 4352 | if (awareEnabled.isNull()) { |
| 4353 | final boolean defAwareEnabled = getContext().getResources().getBoolean( |
| 4354 | R.bool.def_aware_enabled); |
| 4355 | secureSettings.insertSettingLocked( |
| 4356 | Secure.AWARE_ENABLED, defAwareEnabled ? "1" : "0", |
| 4357 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4358 | } |
| 4359 | |
| 4360 | currentVersion = 178; |
| 4361 | } |
| 4362 | |
Edgar Wang | dabc41e | 2019-05-07 18:53:31 +0800 | [diff] [blame] | 4363 | if (currentVersion == 178) { |
| 4364 | // Version 178: Set the default value for Secure Settings: |
| 4365 | // SKIP_GESTURE & SILENCE_GESTURE |
| 4366 | |
| 4367 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4368 | |
| 4369 | final Setting skipGesture = secureSettings.getSettingLocked( |
| 4370 | Secure.SKIP_GESTURE); |
| 4371 | |
| 4372 | if (skipGesture.isNull()) { |
| 4373 | final boolean defSkipGesture = getContext().getResources().getBoolean( |
| 4374 | R.bool.def_skip_gesture); |
| 4375 | secureSettings.insertSettingLocked( |
| 4376 | Secure.SKIP_GESTURE, defSkipGesture ? "1" : "0", |
| 4377 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4378 | } |
| 4379 | |
| 4380 | final Setting silenceGesture = secureSettings.getSettingLocked( |
| 4381 | Secure.SILENCE_GESTURE); |
| 4382 | |
| 4383 | if (silenceGesture.isNull()) { |
| 4384 | final boolean defSilenceGesture = getContext().getResources().getBoolean( |
| 4385 | R.bool.def_silence_gesture); |
| 4386 | secureSettings.insertSettingLocked( |
| 4387 | Secure.SILENCE_GESTURE, defSilenceGesture ? "1" : "0", |
| 4388 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4389 | } |
| 4390 | |
| 4391 | currentVersion = 179; |
| 4392 | } |
| 4393 | |
Mady Mellor | 95e879a | 2019-05-06 08:50:06 -0700 | [diff] [blame] | 4394 | if (currentVersion == 179) { |
| 4395 | // Version 178: Reset the default for Secure Settings: NOTIFICATION_BUBBLES |
| 4396 | // This is originally set in version 173, however, the default value changed |
Lyn Han | b739fa6 | 2019-06-27 18:41:11 -0700 | [diff] [blame] | 4397 | // so this step is to ensure the value is updated to the correct default. |
Mady Mellor | 95e879a | 2019-05-06 08:50:06 -0700 | [diff] [blame] | 4398 | |
Lyn Han | b739fa6 | 2019-06-27 18:41:11 -0700 | [diff] [blame] | 4399 | // Removed. Moved NOTIFICATION_BUBBLES to Global Settings. |
Mady Mellor | 95e879a | 2019-05-06 08:50:06 -0700 | [diff] [blame] | 4400 | currentVersion = 180; |
| 4401 | } |
Winson Chung | d9f2fb3 | 2019-03-05 11:10:12 -0800 | [diff] [blame] | 4402 | |
Edgar Wang | 4870e4b | 2019-05-15 19:14:43 +0800 | [diff] [blame] | 4403 | if (currentVersion == 180) { |
| 4404 | // Version 180: Set the default value for Secure Settings: AWARE_LOCK_ENABLED |
| 4405 | |
| 4406 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4407 | |
| 4408 | final Setting awareLockEnabled = secureSettings.getSettingLocked( |
| 4409 | Secure.AWARE_LOCK_ENABLED); |
| 4410 | |
| 4411 | if (awareLockEnabled.isNull()) { |
| 4412 | final boolean defAwareLockEnabled = getContext().getResources().getBoolean( |
| 4413 | R.bool.def_aware_lock_enabled); |
| 4414 | secureSettings.insertSettingLocked( |
| 4415 | Secure.AWARE_LOCK_ENABLED, defAwareLockEnabled ? "1" : "0", |
| 4416 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4417 | } |
| 4418 | |
| 4419 | currentVersion = 181; |
| 4420 | } |
| 4421 | |
Eric Laurent | 02153e6 | 2019-06-12 17:20:03 -0700 | [diff] [blame] | 4422 | if (currentVersion == 181) { |
| 4423 | // Version cd : by default, add STREAM_BLUETOOTH_SCO to list of streams that can |
| 4424 | // be muted. |
| 4425 | final SettingsState systemSettings = getSystemSettingsLocked(userId); |
| 4426 | final Setting currentSetting = systemSettings.getSettingLocked( |
| 4427 | Settings.System.MUTE_STREAMS_AFFECTED); |
| 4428 | if (!currentSetting.isNull()) { |
| 4429 | try { |
| 4430 | int currentSettingIntegerValue = Integer.parseInt( |
| 4431 | currentSetting.getValue()); |
| 4432 | if ((currentSettingIntegerValue |
| 4433 | & (1 << AudioManager.STREAM_BLUETOOTH_SCO)) == 0) { |
| 4434 | systemSettings.insertSettingLocked( |
| 4435 | Settings.System.MUTE_STREAMS_AFFECTED, |
| 4436 | Integer.toString( |
| 4437 | currentSettingIntegerValue |
| 4438 | | (1 << AudioManager.STREAM_BLUETOOTH_SCO)), |
| 4439 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4440 | } |
| 4441 | } catch (NumberFormatException e) { |
| 4442 | // remove the setting in case it is not a valid integer |
| 4443 | Slog.w("Failed to parse integer value of MUTE_STREAMS_AFFECTED" |
| 4444 | + "setting, removing setting", e); |
| 4445 | systemSettings.deleteSettingLocked( |
| 4446 | Settings.System.MUTE_STREAMS_AFFECTED); |
| 4447 | } |
| 4448 | |
| 4449 | } |
| 4450 | currentVersion = 182; |
| 4451 | } |
| 4452 | |
Lyn Han | b739fa6 | 2019-06-27 18:41:11 -0700 | [diff] [blame] | 4453 | if (currentVersion == 182) { |
| 4454 | // Remove secure bubble settings. |
Mady Mellor | fa9556a | 2019-09-19 11:10:31 -0700 | [diff] [blame] | 4455 | getSecureSettingsLocked(userId).deleteSettingLocked("notification_bubbles"); |
Lyn Han | b739fa6 | 2019-06-27 18:41:11 -0700 | [diff] [blame] | 4456 | |
| 4457 | // Add global bubble settings. |
| 4458 | getGlobalSettingsLocked().insertSettingLocked(Global.NOTIFICATION_BUBBLES, |
| 4459 | getContext().getResources().getBoolean( |
| 4460 | R.bool.def_notification_bubbles) ? "1" : "0", null /* tag */, |
| 4461 | true /* makeDefault */, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4462 | |
| 4463 | currentVersion = 183; |
| 4464 | } |
| 4465 | |
Beverly | f364d7c | 2019-10-10 16:44:43 -0400 | [diff] [blame] | 4466 | if (currentVersion == 183) { |
| 4467 | // Version 184: Set default values for WIRELESS_CHARGING_STARTED_SOUND |
| 4468 | // and CHARGING_STARTED_SOUND |
| 4469 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 4470 | |
| 4471 | final String oldValueWireless = globalSettings.getSettingLocked( |
| 4472 | Global.WIRELESS_CHARGING_STARTED_SOUND).getValue(); |
| 4473 | final String oldValueWired = globalSettings.getSettingLocked( |
| 4474 | Global.CHARGING_STARTED_SOUND).getValue(); |
| 4475 | |
| 4476 | final String defaultValueWireless = getContext().getResources().getString( |
| 4477 | R.string.def_wireless_charging_started_sound); |
| 4478 | final String defaultValueWired = getContext().getResources().getString( |
| 4479 | R.string.def_charging_started_sound); |
| 4480 | |
| 4481 | // wireless charging sound |
| 4482 | if (oldValueWireless == null |
| 4483 | || TextUtils.equals(oldValueWireless, defaultValueWired)) { |
| 4484 | if (!TextUtils.isEmpty(defaultValueWireless)) { |
| 4485 | globalSettings.insertSettingLocked( |
| 4486 | Global.WIRELESS_CHARGING_STARTED_SOUND, defaultValueWireless, |
| 4487 | null /* tag */, true /* makeDefault */, |
| 4488 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 4489 | } else if (!TextUtils.isEmpty(defaultValueWired)) { |
| 4490 | // if the wireless sound is empty, use the wired charging sound |
| 4491 | globalSettings.insertSettingLocked( |
| 4492 | Global.WIRELESS_CHARGING_STARTED_SOUND, defaultValueWired, |
| 4493 | null /* tag */, true /* makeDefault */, |
| 4494 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 4495 | } |
| 4496 | } |
| 4497 | |
| 4498 | // wired charging sound |
| 4499 | if (oldValueWired == null && !TextUtils.isEmpty(defaultValueWired)) { |
| 4500 | globalSettings.insertSettingLocked( |
| 4501 | Global.CHARGING_STARTED_SOUND, defaultValueWired, |
| 4502 | null /* tag */, true /* makeDefault */, |
| 4503 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 4504 | } |
| 4505 | currentVersion = 184; |
| 4506 | } |
| 4507 | |
Felipe Leme | ff35509 | 2017-04-03 12:55:02 -0700 | [diff] [blame] | 4508 | // vXXX: Add new settings above this point. |
| 4509 | |
Dan Sandler | 71f85e9 | 2016-07-20 13:46:05 -0400 | [diff] [blame] | 4510 | if (currentVersion != newVersion) { |
Svetoslav Ganov | 264c7a9 | 2016-08-24 17:31:14 -0700 | [diff] [blame] | 4511 | Slog.wtf("SettingsProvider", "warning: upgrading settings database to version " |
Dan Sandler | 71f85e9 | 2016-07-20 13:46:05 -0400 | [diff] [blame] | 4512 | + newVersion + " left it at " |
Stephen Chen | 5d0922f | 2017-03-27 10:28:04 -0700 | [diff] [blame] | 4513 | + currentVersion + |
| 4514 | " instead; this is probably a bug. Did you update SETTINGS_VERSION?", |
| 4515 | new Throwable()); |
Dan Sandler | 71f85e9 | 2016-07-20 13:46:05 -0400 | [diff] [blame] | 4516 | if (DEBUG) { |
| 4517 | throw new RuntimeException("db upgrade error"); |
| 4518 | } |
| 4519 | } |
| 4520 | |
Jeff Brown | 503cffc | 2015-03-26 18:08:51 -0700 | [diff] [blame] | 4521 | // Return the current version. |
| 4522 | return currentVersion; |
Brad Fitzpatrick | 547a96b | 2010-03-09 17:58:53 -0800 | [diff] [blame] | 4523 | } |
| 4524 | } |
Svet Ganov | 1370155 | 2017-02-23 12:45:17 -0800 | [diff] [blame] | 4525 | |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 4526 | private void ensureLegacyDefaultValueAndSystemSetUpdatedLocked(SettingsState settings, |
| 4527 | int userId) { |
Svet Ganov | 1370155 | 2017-02-23 12:45:17 -0800 | [diff] [blame] | 4528 | List<String> names = settings.getSettingNamesLocked(); |
| 4529 | final int nameCount = names.size(); |
| 4530 | for (int i = 0; i < nameCount; i++) { |
| 4531 | String name = names.get(i); |
| 4532 | Setting setting = settings.getSettingLocked(name); |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 4533 | |
| 4534 | // In the upgrade case we pretend the call is made from the app |
| 4535 | // that made the last change to the setting to properly determine |
| 4536 | // whether the call has been made by a system component. |
| 4537 | int callingUid = -1; |
| 4538 | try { |
| 4539 | callingUid = mPackageManager.getPackageUid(setting.getPackageName(), 0, userId); |
| 4540 | } catch (RemoteException e) { |
| 4541 | /* ignore - handled below */ |
| 4542 | } |
| 4543 | if (callingUid < 0) { |
| 4544 | Slog.e(LOG_TAG, "Unknown package: " + setting.getPackageName()); |
| 4545 | continue; |
| 4546 | } |
| 4547 | try { |
| 4548 | final boolean systemSet = SettingsState.isSystemPackage(getContext(), |
Matt Pape | 25c940d | 2019-03-26 12:14:33 -0700 | [diff] [blame] | 4549 | setting.getPackageName(), callingUid, userId); |
Svet Ganov | 1370155 | 2017-02-23 12:45:17 -0800 | [diff] [blame] | 4550 | if (systemSet) { |
| 4551 | settings.insertSettingLocked(name, setting.getValue(), |
| 4552 | setting.getTag(), true, setting.getPackageName()); |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 4553 | } else if (setting.getDefaultValue() != null && setting.isDefaultFromSystem()) { |
| 4554 | // We had a bug where changes by non-system packages were marked |
| 4555 | // as system made and as a result set as the default. Therefore, if |
| 4556 | // the package changed the setting last is not a system one but the |
| 4557 | // setting is marked as its default coming from the system we clear |
| 4558 | // the default and clear the system set flag. |
| 4559 | settings.resetSettingDefaultValueLocked(name); |
Svet Ganov | 1370155 | 2017-02-23 12:45:17 -0800 | [diff] [blame] | 4560 | } |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 4561 | } catch (IllegalStateException e) { |
| 4562 | // If the package goes over its quota during the upgrade, don't |
| 4563 | // crash but just log the error as the system does the upgrade. |
| 4564 | Slog.e(LOG_TAG, "Error upgrading setting: " + setting.getName(), e); |
| 4565 | |
Svet Ganov | 1370155 | 2017-02-23 12:45:17 -0800 | [diff] [blame] | 4566 | } |
| 4567 | } |
| 4568 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 4569 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4570 | } |