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