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