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