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