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