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