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