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