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