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