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