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