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