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