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