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