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