blob: c149876cedaa8339b3d9c75789cfe56f1ff405dc [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001/*
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
17package com.android.providers.settings;
18
Svetoslav683914b2015-01-15 14:22:26 -080019import android.Manifest;
Christopher Tated5fe1472012-09-10 15:48:38 -070020import android.app.ActivityManager;
Xiaohui Chen43765b72015-08-31 10:57:33 -070021import android.app.AppGlobals;
Christopher Tate45281862010-03-05 15:46:30 -080022import android.app.backup.BackupManager;
Christopher Tate06efb532012-08-24 15:29:27 -070023import android.content.BroadcastReceiver;
Ruben Brunk98576cf2016-03-07 18:54:28 -080024import android.content.ComponentName;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070025import android.content.ContentProvider;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070026import android.content.ContentValues;
27import android.content.Context;
Christopher Tate06efb532012-08-24 15:29:27 -070028import android.content.Intent;
29import android.content.IntentFilter;
Svetoslav683914b2015-01-15 14:22:26 -080030import android.content.pm.ApplicationInfo;
Xiaohui Chen43765b72015-08-31 10:57:33 -070031import android.content.pm.IPackageManager;
Svetoslav683914b2015-01-15 14:22:26 -080032import android.content.pm.PackageInfo;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070033import android.content.pm.PackageManager;
Christopher Tate38e7a602013-09-03 16:57:34 -070034import android.content.pm.UserInfo;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070035import android.database.Cursor;
Svetoslav683914b2015-01-15 14:22:26 -080036import android.database.MatrixCursor;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070037import android.database.sqlite.SQLiteDatabase;
38import android.database.sqlite.SQLiteQueryBuilder;
Svetoslav683914b2015-01-15 14:22:26 -080039import android.hardware.camera2.utils.ArrayUtils;
John Spurlocke11ae112015-05-11 16:09:03 -040040import android.media.AudioManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070041import android.net.Uri;
Christopher Tate06efb532012-08-24 15:29:27 -070042import android.os.Binder;
Svetoslav683914b2015-01-15 14:22:26 -080043import android.os.Build;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080044import android.os.Bundle;
Amith Yamasani5cdf7f52013-06-27 15:12:01 -070045import android.os.DropBoxManager;
Svetoslav683914b2015-01-15 14:22:26 -080046import android.os.Environment;
Svetoslav7e0683b2015-08-03 16:02:52 -070047import android.os.Handler;
Svet Ganova8f90262016-05-10 08:44:48 -070048import android.os.HandlerThread;
Svetoslav7e0683b2015-08-03 16:02:52 -070049import android.os.Looper;
50import android.os.Message;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070051import android.os.ParcelFileDescriptor;
Christopher Tate0da13572013-10-13 17:34:49 -070052import android.os.Process;
Xiaohui Chen43765b72015-08-31 10:57:33 -070053import android.os.RemoteException;
Jeff Sharkey413573a2016-02-22 17:52:45 -070054import android.os.SELinux;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -070055import android.os.ServiceManager;
Christopher Tate06efb532012-08-24 15:29:27 -070056import android.os.UserHandle;
57import android.os.UserManager;
Svet Ganov53a441c2016-04-19 19:38:00 -070058import android.os.UserManagerInternal;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070059import android.provider.Settings;
Jeremy Joslin8bdad342016-12-14 11:46:47 -080060import android.provider.Settings.Global;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070061import android.text.TextUtils;
Andre Lago3fa139c2016-08-04 13:53:44 +010062import android.util.ArrayMap;
Svetoslav683914b2015-01-15 14:22:26 -080063import android.util.ArraySet;
Christopher Tate06efb532012-08-24 15:29:27 -070064import android.util.Slog;
65import android.util.SparseArray;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -070066import android.util.SparseBooleanArray;
John Spurlocke11ae112015-05-11 16:09:03 -040067
Svetoslav683914b2015-01-15 14:22:26 -080068import com.android.internal.annotations.GuardedBy;
69import com.android.internal.content.PackageMonitor;
70import com.android.internal.os.BackgroundThread;
Suprabh Shukla269c11e2015-12-02 16:51:16 -080071import com.android.providers.settings.SettingsState.Setting;
Svet Ganov53a441c2016-04-19 19:38:00 -070072import com.android.server.LocalServices;
Ruben Brunk98576cf2016-03-07 18:54:28 -080073import com.android.server.SystemConfig;
John Spurlocke11ae112015-05-11 16:09:03 -040074
Svetoslav683914b2015-01-15 14:22:26 -080075import java.io.File;
Svetoslavb505ccc2015-02-17 12:41:04 -080076import java.io.FileDescriptor;
Svetoslav683914b2015-01-15 14:22:26 -080077import java.io.FileNotFoundException;
Svetoslavb505ccc2015-02-17 12:41:04 -080078import java.io.PrintWriter;
Svetoslav683914b2015-01-15 14:22:26 -080079import java.security.SecureRandom;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -070080import java.util.ArrayList;
Svetoslav683914b2015-01-15 14:22:26 -080081import java.util.Arrays;
82import java.util.List;
Andre Lago3fa139c2016-08-04 13:53:44 +010083import java.util.Map;
Svetoslav683914b2015-01-15 14:22:26 -080084import java.util.Set;
85import java.util.regex.Pattern;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070086
Svetoslav683914b2015-01-15 14:22:26 -080087/**
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 Project54b6cfa2008-10-21 07:00:00 -0700119public class SettingsProvider extends ContentProvider {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700120 static final boolean DEBUG = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700121
Svetoslav Ganov264c7a92016-08-24 17:31:14 -0700122 private static final boolean DROP_DATABASE_ON_MIGRATION = true;
Svetoslav683914b2015-01-15 14:22:26 -0800123
124 private static final String LOG_TAG = "SettingsProvider";
Christopher Tate0da13572013-10-13 17:34:49 -0700125
Christopher Tate06efb532012-08-24 15:29:27 -0700126 private static final String TABLE_SYSTEM = "system";
127 private static final String TABLE_SECURE = "secure";
128 private static final String TABLE_GLOBAL = "global";
Svetoslav683914b2015-01-15 14:22:26 -0800129
130 // Old tables no longer exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 private static final String TABLE_FAVORITES = "favorites";
132 private static final String TABLE_OLD_FAVORITES = "old_favorites";
Svetoslav683914b2015-01-15 14:22:26 -0800133 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 Project9066cfe2009-03-03 19:31:44 -0800136
Svetoslav683914b2015-01-15 14:22:26 -0800137 // The set of removed legacy tables.
138 private static final Set<String> REMOVED_LEGACY_TABLES = new ArraySet<>();
Christopher Tate06efb532012-08-24 15:29:27 -0700139 static {
Svetoslav683914b2015-01-15 14:22:26 -0800140 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 Tate06efb532012-08-24 15:29:27 -0700146
Svetoslav683914b2015-01-15 14:22:26 -0800147 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 Reynolds5e458dd2014-07-07 16:07:01 -0400150
Svetoslav683914b2015-01-15 14:22:26 -0800151 private static final String[] ALL_COLUMNS = new String[] {
152 Settings.NameValueTable._ID,
153 Settings.NameValueTable.NAME,
154 Settings.NameValueTable.VALUE
155 };
156
Svet Ganov53a441c2016-04-19 19:38:00 -0700157 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;
Svetoslav683914b2015-01-15 14:22:26 -0800160
Svet Ganov53a441c2016-04-19 19:38:00 -0700161 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 Tate06efb532012-08-24 15:29:27 -0700166
Svetoslav683914b2015-01-15 14:22:26 -0800167 // 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 Tate06efb532012-08-24 15:29:27 -0700171 }
172
Svetoslav683914b2015-01-15 14:22:26 -0800173 // 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 Project54b6cfa2008-10-21 07:00:00 -0700177 }
178
Svetoslav683914b2015-01-15 14:22:26 -0800179 // 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 Project54b6cfa2008-10-21 07:00:00 -0700183 }
184
Svetoslav683914b2015-01-15 14:22:26 -0800185 // 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 Project54b6cfa2008-10-21 07:00:00 -0700189 }
190
Svetoslav683914b2015-01-15 14:22:26 -0800191 // 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 Project54b6cfa2008-10-21 07:00:00 -0700195 }
196
Svetoslav683914b2015-01-15 14:22:26 -0800197 // 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 Reynolds5e458dd2014-07-07 16:07:01 -0400201 }
202
Andre Lago3fa139c2016-08-04 13:53:44 +0100203 // 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
Svetoslav683914b2015-01-15 14:22:26 -0800210 private final Object mLock = new Object();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700211
Svetoslav683914b2015-01-15 14:22:26 -0800212 @GuardedBy("mLock")
213 private SettingsRegistry mSettingsRegistry;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700214
Svet Ganova8f90262016-05-10 08:44:48 -0700215 @GuardedBy("mLock")
216 private HandlerThread mHandlerThread;
217
Svetoslav7ec28e82015-05-20 17:01:10 -0700218 // We have to call in the user manager with no lock held,
219 private volatile UserManager mUserManager;
Svetoslav683914b2015-01-15 14:22:26 -0800220
Svetoslav7ec28e82015-05-20 17:01:10 -0700221 // We have to call in the package manager with no lock held,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700222 private volatile IPackageManager mPackageManager;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700223
Svet Ganov53a441c2016-04-19 19:38:00 -0700224 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 Project54b6cfa2008-10-21 07:00:00 -0700258 @Override
259 public boolean onCreate() {
Svetoslav683914b2015-01-15 14:22:26 -0800260 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700261 mUserManager = UserManager.get(getContext());
262 mPackageManager = AppGlobals.getPackageManager();
Svet Ganova8f90262016-05-10 08:44:48 -0700263 mHandlerThread = new HandlerThread(LOG_TAG,
264 Process.THREAD_PRIORITY_BACKGROUND);
265 mHandlerThread.start();
Svetoslav683914b2015-01-15 14:22:26 -0800266 mSettingsRegistry = new SettingsRegistry();
267 }
268 registerBroadcastReceivers();
Svet Ganov53a441c2016-04-19 19:38:00 -0700269 startWatchingUserRestrictionChanges();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700270 ServiceManager.addService("settings", new SettingsService(this));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700271 return true;
272 }
273
Svetoslav683914b2015-01-15 14:22:26 -0800274 @Override
275 public Bundle call(String method, String name, Bundle args) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700276 final int requestingUserId = getRequestingUserId(args);
277 switch (method) {
278 case Settings.CALL_METHOD_GET_GLOBAL: {
279 Setting setting = getGlobalSetting(name);
Svet Ganov53a441c2016-04-19 19:38:00 -0700280 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav683914b2015-01-15 14:22:26 -0800281 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700282
283 case Settings.CALL_METHOD_GET_SECURE: {
284 Setting setting = getSecureSetting(name, requestingUserId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700285 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700286 }
287
288 case Settings.CALL_METHOD_GET_SYSTEM: {
289 Setting setting = getSystemSetting(name, requestingUserId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700290 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700291 }
292
293 case Settings.CALL_METHOD_PUT_GLOBAL: {
294 String value = getSettingValue(args);
Svet Ganov53a441c2016-04-19 19:38:00 -0700295 insertGlobalSetting(name, value, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700296 break;
297 }
298
299 case Settings.CALL_METHOD_PUT_SECURE: {
300 String value = getSettingValue(args);
Svet Ganov53a441c2016-04-19 19:38:00 -0700301 insertSecureSetting(name, value, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700302 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 Tate06efb532012-08-24 15:29:27 -0700314 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700315
Christopher Tate06efb532012-08-24 15:29:27 -0700316 return null;
317 }
318
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800319 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800320 public String getType(Uri uri) {
321 Arguments args = new Arguments(uri, null, null, true);
322 if (TextUtils.isEmpty(args.name)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700323 return "vnd.android.cursor.dir/" + args.table;
324 } else {
Svetoslav7ec28e82015-05-20 17:01:10 -0700325 return "vnd.android.cursor.item/" + args.table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700326 }
327 }
328
329 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800330 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 Project54b6cfa2008-10-21 07:00:00 -0700334 }
335
Svetoslav683914b2015-01-15 14:22:26 -0800336 Arguments args = new Arguments(uri, where, whereArgs, true);
337 String[] normalizedProjection = normalizeProjection(projection);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700338
Svetoslav683914b2015-01-15 14:22:26 -0800339 // If a legacy table that is gone, done.
340 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
341 return new MatrixCursor(normalizedProjection, 0);
342 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700343
Svetoslav7ec28e82015-05-20 17:01:10 -0700344 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 Lockwoodbd2a7122009-04-02 23:41:33 -0700351 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700352 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700353
Svetoslav7ec28e82015-05-20 17:01:10 -0700354 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);
Svetoslav683914b2015-01-15 14:22:26 -0800361 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700362 }
Svetoslav683914b2015-01-15 14:22:26 -0800363
Svetoslav7ec28e82015-05-20 17:01:10 -0700364 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);
Svetoslav683914b2015-01-15 14:22:26 -0800371 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700372 }
Svetoslav683914b2015-01-15 14:22:26 -0800373
Svetoslav7ec28e82015-05-20 17:01:10 -0700374 default: {
375 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700376 }
377 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700378 }
379
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700380 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800381 public Uri insert(Uri uri, ContentValues values) {
382 if (DEBUG) {
383 Slog.v(LOG_TAG, "insert() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700384 }
385
Svetoslav683914b2015-01-15 14:22:26 -0800386 String table = getValidTableOrThrow(uri);
Christopher Tate06efb532012-08-24 15:29:27 -0700387
Svetoslav683914b2015-01-15 14:22:26 -0800388 // If a legacy table that is gone, done.
389 if (REMOVED_LEGACY_TABLES.contains(table)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 return null;
391 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700392
Svetoslav683914b2015-01-15 14:22:26 -0800393 String name = values.getAsString(Settings.Secure.NAME);
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700394 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800395 return null;
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700396 }
397
Svetoslav683914b2015-01-15 14:22:26 -0800398 String value = values.getAsString(Settings.Secure.VALUE);
399
Svetoslav7ec28e82015-05-20 17:01:10 -0700400 switch (table) {
401 case TABLE_GLOBAL: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700402 if (insertGlobalSetting(name, value, UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700403 return Uri.withAppendedPath(Settings.Global.CONTENT_URI, name);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700404 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700405 } break;
406
407 case TABLE_SECURE: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700408 if (insertSecureSetting(name, value, UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700409 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 Tatec221d2b2012-10-03 18:33:52 -0700421 }
422 }
423
Svetoslav683914b2015-01-15 14:22:26 -0800424 return null;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700425 }
426
427 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800428 public int bulkInsert(Uri uri, ContentValues[] allValues) {
429 if (DEBUG) {
430 Slog.v(LOG_TAG, "bulkInsert() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700432
Svetoslav683914b2015-01-15 14:22:26 -0800433 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 Hackborn8d051722014-10-01 14:59:58 -0700440 }
Svetoslav683914b2015-01-15 14:22:26 -0800441
442 return insertionCount;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700443 }
444
445 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800446 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 Project9066cfe2009-03-03 19:31:44 -0800449 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700450
Svetoslav683914b2015-01-15 14:22:26 -0800451 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 Hackborn8d051722014-10-01 14:59:58 -0700456 }
Svetoslav683914b2015-01-15 14:22:26 -0800457
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700458 if (!isKeyValid(args.name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800459 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700460 }
Svetoslav683914b2015-01-15 14:22:26 -0800461
Svetoslav7ec28e82015-05-20 17:01:10 -0700462 switch (args.table) {
463 case TABLE_GLOBAL: {
464 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700465 return deleteGlobalSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700466 }
Svetoslav683914b2015-01-15 14:22:26 -0800467
Svetoslav7ec28e82015-05-20 17:01:10 -0700468 case TABLE_SECURE: {
469 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700470 return deleteSecureSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700471 }
Svetoslav683914b2015-01-15 14:22:26 -0800472
Svetoslav7ec28e82015-05-20 17:01:10 -0700473 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);
Svetoslav683914b2015-01-15 14:22:26 -0800480 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700481 }
Svetoslav683914b2015-01-15 14:22:26 -0800482 }
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 Tate06efb532012-08-24 15:29:27 -0700488 }
Svetoslav683914b2015-01-15 14:22:26 -0800489
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 Onuki3a2c35782015-06-18 11:21:58 -0700497 String name = values.getAsString(Settings.Secure.NAME);
498 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800499 return 0;
500 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700501 String value = values.getAsString(Settings.Secure.VALUE);
Svetoslav683914b2015-01-15 14:22:26 -0800502
Svetoslav7ec28e82015-05-20 17:01:10 -0700503 switch (args.table) {
504 case TABLE_GLOBAL: {
505 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700506 return updateGlobalSetting(args.name, value, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700507 }
Svetoslav683914b2015-01-15 14:22:26 -0800508
Svetoslav7ec28e82015-05-20 17:01:10 -0700509 case TABLE_SECURE: {
510 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700511 return updateSecureSetting(args.name, value, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700512 }
Svetoslav683914b2015-01-15 14:22:26 -0800513
Svetoslav7ec28e82015-05-20 17:01:10 -0700514 case TABLE_SYSTEM: {
515 final int userId = UserHandle.getCallingUserId();
516 return updateSystemSetting(args.name, value, userId) ? 1 : 0;
517 }
Svetoslav683914b2015-01-15 14:22:26 -0800518
Svetoslav7ec28e82015-05-20 17:01:10 -0700519 default: {
520 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800521 }
522 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700523 }
524
525 @Override
526 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
Robin Lee2ab02e22016-07-28 18:41:23 +0100527 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 Lago3fa139c2016-08-04 13:53:44 +0100534 final String cacheRingtoneSetting;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700535 final String cacheName;
536 if (Settings.System.RINGTONE_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100537 cacheRingtoneSetting = Settings.System.RINGTONE;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700538 cacheName = Settings.System.RINGTONE_CACHE;
539 } else if (Settings.System.NOTIFICATION_SOUND_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100540 cacheRingtoneSetting = Settings.System.NOTIFICATION_SOUND;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700541 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
542 } else if (Settings.System.ALARM_ALERT_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100543 cacheRingtoneSetting = Settings.System.ALARM_ALERT;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700544 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 Lago3fa139c2016-08-04 13:53:44 +0100550 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 Sharkey413573a2016-02-22 17:52:45 -0700557 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 Nelissen69f593c2009-07-28 09:55:04 -0700565 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800566
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700567 public void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800568 synchronized (mLock) {
569 final long identity = Binder.clearCallingIdentity();
570 try {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700571 SparseBooleanArray users = mSettingsRegistry.getKnownUsersLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800572 final int userCount = users.size();
573 for (int i = 0; i < userCount; i++) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700574 dumpForUserLocked(users.keyAt(i), pw);
Svetoslavb505ccc2015-02-17 12:41:04 -0800575 }
576 } finally {
577 Binder.restoreCallingIdentity(identity);
578 }
579 }
580 }
581
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700582 private void dumpForUserLocked(int userId, PrintWriter pw) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700583 if (userId == UserHandle.USER_SYSTEM) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800584 pw.println("GLOBAL SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700585 SettingsState globalSettings = mSettingsRegistry.getSettingsLocked(
586 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700587 if (globalSettings != null) {
588 dumpSettingsLocked(globalSettings, pw);
589 }
590 pw.println();
591
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700592 globalSettings.dumpHistoricalOperations(pw);
Svetoslavb505ccc2015-02-17 12:41:04 -0800593 }
594
595 pw.println("SECURE SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700596 SettingsState secureSettings = mSettingsRegistry.getSettingsLocked(
597 SETTINGS_TYPE_SECURE, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700598 if (secureSettings != null) {
599 dumpSettingsLocked(secureSettings, pw);
600 }
601 pw.println();
602
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700603 secureSettings.dumpHistoricalOperations(pw);
604
Svetoslavb505ccc2015-02-17 12:41:04 -0800605 pw.println("SYSTEM SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700606 SettingsState systemSettings = mSettingsRegistry.getSettingsLocked(
607 SETTINGS_TYPE_SYSTEM, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700608 if (systemSettings != null) {
609 dumpSettingsLocked(systemSettings, pw);
610 }
611 pw.println();
612
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700613 systemSettings.dumpHistoricalOperations(pw);
Svetoslavb505ccc2015-02-17 12:41:04 -0800614 }
615
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700616 private void dumpSettingsLocked(SettingsState settingsState, PrintWriter pw) {
617 List<String> names = settingsState.getSettingNamesLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800618
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700619 final int nameCount = names.size();
Svetoslavb505ccc2015-02-17 12:41:04 -0800620
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700621 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()));
Svetoslavb505ccc2015-02-17 12:41:04 -0800630 pw.println();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700631 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800632 }
633
Svetoslav7e0683b2015-08-03 16:02:52 -0700634 private static String toDumpString(String s) {
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700635 if (s != null) {
636 return s;
637 }
638 return "{null}";
639 }
640
Svetoslav683914b2015-01-15 14:22:26 -0800641 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 Chen43765b72015-08-31 10:57:33 -0700650 UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800651
652 switch (intent.getAction()) {
653 case Intent.ACTION_USER_REMOVED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700654 synchronized (mLock) {
655 mSettingsRegistry.removeUserStateLocked(userId, true);
656 }
Svetoslav683914b2015-01-15 14:22:26 -0800657 } break;
658
659 case Intent.ACTION_USER_STOPPED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700660 synchronized (mLock) {
661 mSettingsRegistry.removeUserStateLocked(userId, false);
662 }
Svetoslav683914b2015-01-15 14:22:26 -0800663 } 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 Ganov53a441c2016-04-19 19:38:00 -0700683 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
Svetoslav7ec28e82015-05-20 17:01:10 -0700769 private Cursor getAllGlobalSettings(String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800770 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700771 Slog.v(LOG_TAG, "getAllGlobalSettings()");
Svetoslav683914b2015-01-15 14:22:26 -0800772 }
773
Svetoslav7ec28e82015-05-20 17:01:10 -0700774 synchronized (mLock) {
775 // Get the settings.
776 SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700777 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800778
Svetoslav7ec28e82015-05-20 17:01:10 -0700779 List<String> names = settingsState.getSettingNamesLocked();
Svetoslav683914b2015-01-15 14:22:26 -0800780
Svetoslav7ec28e82015-05-20 17:01:10 -0700781 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -0800782
Svetoslav7ec28e82015-05-20 17:01:10 -0700783 String[] normalizedProjection = normalizeProjection(projection);
784 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -0800785
Svetoslav7ec28e82015-05-20 17:01:10 -0700786 // 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;
Svetoslav683914b2015-01-15 14:22:26 -0800794 }
Svetoslav683914b2015-01-15 14:22:26 -0800795 }
796
Svetoslav7ec28e82015-05-20 17:01:10 -0700797 private Setting getGlobalSetting(String name) {
Svetoslav683914b2015-01-15 14:22:26 -0800798 if (DEBUG) {
799 Slog.v(LOG_TAG, "getGlobalSetting(" + name + ")");
800 }
801
802 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700803 synchronized (mLock) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700804 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_GLOBAL,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700805 UserHandle.USER_SYSTEM, name);
Svetoslav683914b2015-01-15 14:22:26 -0800806 }
Svetoslav683914b2015-01-15 14:22:26 -0800807 }
808
Svet Ganov53a441c2016-04-19 19:38:00 -0700809 private boolean updateGlobalSetting(String name, String value, int requestingUserId,
810 boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800811 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700812 Slog.v(LOG_TAG, "updateGlobalSetting(" + name + ", " + value + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800813 }
Svet Ganov53a441c2016-04-19 19:38:00 -0700814 return mutateGlobalSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE,
815 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800816 }
817
Svet Ganov53a441c2016-04-19 19:38:00 -0700818 private boolean insertGlobalSetting(String name, String value, int requestingUserId,
819 boolean forceNotify) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700820 if (DEBUG) {
821 Slog.v(LOG_TAG, "insertGlobalSetting(" + name + ", " + value + ")");
822 }
Svet Ganov53a441c2016-04-19 19:38:00 -0700823 return mutateGlobalSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT,
824 forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700825 }
826
Svet Ganov53a441c2016-04-19 19:38:00 -0700827 private boolean deleteGlobalSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800828 if (DEBUG) {
829 Slog.v(LOG_TAG, "deleteGlobalSettingLocked(" + name + ")");
830 }
Svet Ganov53a441c2016-04-19 19:38:00 -0700831 return mutateGlobalSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE,
832 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800833 }
834
Svetoslav7ec28e82015-05-20 17:01:10 -0700835 private boolean mutateGlobalSetting(String name, String value, int requestingUserId,
Svet Ganov53a441c2016-04-19 19:38:00 -0700836 int operation, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800837 // Make sure the caller can change the settings - treated as secure.
838 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
839
Svetoslav683914b2015-01-15 14:22:26 -0800840 // Resolve the userId on whose behalf the call is made.
841 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
842
Makoto Onuki28da2e32015-11-20 11:30:44 -0800843 // If this is a setting that is currently restricted for this user, do not allow
844 // unrestricting changes.
Victor Chang9c7b7062016-07-12 23:47:29 +0100845 if (isGlobalOrSecureSettingRestrictedForUser(name, callingUserId, value,
846 Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -0800847 return false;
848 }
849
850 // Perform the mutation.
Svetoslav7ec28e82015-05-20 17:01:10 -0700851 synchronized (mLock) {
852 switch (operation) {
853 case MUTATION_OPERATION_INSERT: {
854 return mSettingsRegistry
Svet Ganov53a441c2016-04-19 19:38:00 -0700855 .insertSettingLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM,
856 name, value, getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700857 }
Svetoslav683914b2015-01-15 14:22:26 -0800858
Svetoslav7ec28e82015-05-20 17:01:10 -0700859 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700860 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_GLOBAL,
861 UserHandle.USER_SYSTEM, name, forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700862 }
Svetoslav683914b2015-01-15 14:22:26 -0800863
Svetoslav7ec28e82015-05-20 17:01:10 -0700864 case MUTATION_OPERATION_UPDATE: {
865 return mSettingsRegistry
Svet Ganov53a441c2016-04-19 19:38:00 -0700866 .updateSettingLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM,
867 name, value, getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700868 }
Svetoslav683914b2015-01-15 14:22:26 -0800869 }
870 }
871
872 return false;
873 }
874
Svetoslav7ec28e82015-05-20 17:01:10 -0700875 private Cursor getAllSecureSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800876 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
Svetoslav7ec28e82015-05-20 17:01:10 -0700883 synchronized (mLock) {
884 List<String> names = mSettingsRegistry.getSettingsNamesLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700885 SETTINGS_TYPE_SECURE, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -0800886
Svetoslav7ec28e82015-05-20 17:01:10 -0700887 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -0800888
Svetoslav7ec28e82015-05-20 17:01:10 -0700889 String[] normalizedProjection = normalizeProjection(projection);
890 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -0800891
Svetoslav7ec28e82015-05-20 17:01:10 -0700892 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);
Svetoslav683914b2015-01-15 14:22:26 -0800897
Svetoslav7ec28e82015-05-20 17:01:10 -0700898 // Special case for location (sigh).
899 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
Svetoslav Ganov83a1f7f2016-04-27 13:50:49 -0700900 continue;
Svetoslav7ec28e82015-05-20 17:01:10 -0700901 }
Svetoslav683914b2015-01-15 14:22:26 -0800902
Svetoslav7ec28e82015-05-20 17:01:10 -0700903 Setting setting = mSettingsRegistry.getSettingLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700904 SETTINGS_TYPE_SECURE, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -0700905 appendSettingToCursor(result, setting);
Svetoslav683914b2015-01-15 14:22:26 -0800906 }
907
Svetoslav7ec28e82015-05-20 17:01:10 -0700908 return result;
Svetoslav683914b2015-01-15 14:22:26 -0800909 }
Svetoslav683914b2015-01-15 14:22:26 -0800910 }
911
Svetoslav7ec28e82015-05-20 17:01:10 -0700912 private Setting getSecureSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800913 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 Hackborn32f40ee2016-10-20 15:54:14 -0700925 SettingsState settings = mSettingsRegistry.getSettingsLocked(SETTINGS_TYPE_SECURE,
926 owningUserId);
927 return settings != null ? settings.getNullSetting() : null;
Svetoslav683914b2015-01-15 14:22:26 -0800928 }
929
930 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700931 synchronized (mLock) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700932 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav7ec28e82015-05-20 17:01:10 -0700933 owningUserId, name);
934 }
Svetoslav683914b2015-01-15 14:22:26 -0800935 }
936
Svet Ganov53a441c2016-04-19 19:38:00 -0700937 private boolean insertSecureSetting(String name, String value, int requestingUserId,
938 boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800939 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700940 Slog.v(LOG_TAG, "insertSecureSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -0800941 + requestingUserId + ")");
942 }
943
Svet Ganov53a441c2016-04-19 19:38:00 -0700944 return mutateSecureSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT,
945 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800946 }
947
Svet Ganov53a441c2016-04-19 19:38:00 -0700948 private boolean deleteSecureSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800949 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700950 Slog.v(LOG_TAG, "deleteSecureSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800951 }
952
Svet Ganov53a441c2016-04-19 19:38:00 -0700953 return mutateSecureSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE,
954 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800955 }
956
Svet Ganov53a441c2016-04-19 19:38:00 -0700957 private boolean updateSecureSetting(String name, String value, int requestingUserId,
958 boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800959 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700960 Slog.v(LOG_TAG, "updateSecureSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -0800961 + requestingUserId + ")");
962 }
963
Svet Ganov53a441c2016-04-19 19:38:00 -0700964 return mutateSecureSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE,
965 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800966 }
967
Svetoslav7ec28e82015-05-20 17:01:10 -0700968 private boolean mutateSecureSetting(String name, String value, int requestingUserId,
Svet Ganov53a441c2016-04-19 19:38:00 -0700969 int operation, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800970 // Make sure the caller can change the settings.
971 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
972
Svetoslav683914b2015-01-15 14:22:26 -0800973 // Resolve the userId on whose behalf the call is made.
974 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
975
Makoto Onuki28da2e32015-11-20 11:30:44 -0800976 // If this is a setting that is currently restricted for this user, do not allow
977 // unrestricting changes.
Victor Chang9c7b7062016-07-12 23:47:29 +0100978 if (isGlobalOrSecureSettingRestrictedForUser(name, callingUserId, value,
979 Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -0800980 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 Ganov53a441c2016-04-19 19:38:00 -0700993 return updateLocationProvidersAllowedLocked(value, owningUserId, forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800994 }
995
996 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700997 synchronized (mLock) {
998 switch (operation) {
999 case MUTATION_OPERATION_INSERT: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001000 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
1001 owningUserId, name, value, getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -07001002 }
Svetoslav683914b2015-01-15 14:22:26 -08001003
Svetoslav7ec28e82015-05-20 17:01:10 -07001004 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001005 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SECURE,
1006 owningUserId, name, forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -07001007 }
Svetoslav683914b2015-01-15 14:22:26 -08001008
Svetoslav7ec28e82015-05-20 17:01:10 -07001009 case MUTATION_OPERATION_UPDATE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001010 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SECURE,
1011 owningUserId, name, value, getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -07001012 }
Svetoslav683914b2015-01-15 14:22:26 -08001013 }
1014 }
1015
1016 return false;
1017 }
1018
Svetoslav7ec28e82015-05-20 17:01:10 -07001019 private Cursor getAllSystemSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001020 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001021 Slog.v(LOG_TAG, "getAllSecureSystem(" + userId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001022 }
1023
1024 // Resolve the userId on whose behalf the call is made.
1025 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
1026
Svetoslav7ec28e82015-05-20 17:01:10 -07001027 synchronized (mLock) {
1028 List<String> names = mSettingsRegistry.getSettingsNamesLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001029 SETTINGS_TYPE_SYSTEM, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -08001030
Svetoslav7ec28e82015-05-20 17:01:10 -07001031 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001032
Svetoslav7ec28e82015-05-20 17:01:10 -07001033 String[] normalizedProjection = normalizeProjection(projection);
1034 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001035
Svetoslav7ec28e82015-05-20 17:01:10 -07001036 for (int i = 0; i < nameCount; i++) {
1037 String name = names.get(i);
Svetoslav683914b2015-01-15 14:22:26 -08001038
Svetoslav7ec28e82015-05-20 17:01:10 -07001039 // Determine the owning user as some profile settings are cloned from the parent.
1040 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId,
1041 name);
Svetoslav683914b2015-01-15 14:22:26 -08001042
Svetoslav7ec28e82015-05-20 17:01:10 -07001043 Setting setting = mSettingsRegistry.getSettingLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001044 SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001045 appendSettingToCursor(result, setting);
1046 }
1047
1048 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001049 }
Svetoslav683914b2015-01-15 14:22:26 -08001050 }
1051
Svetoslav7ec28e82015-05-20 17:01:10 -07001052 private Setting getSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001053 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.
Svetoslav7ec28e82015-05-20 17:01:10 -07001064 synchronized (mLock) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001065 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001066 }
Svetoslav683914b2015-01-15 14:22:26 -08001067 }
1068
Svetoslav7ec28e82015-05-20 17:01:10 -07001069 private boolean insertSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001070 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001071 Slog.v(LOG_TAG, "insertSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001072 + requestingUserId + ")");
1073 }
1074
Svetoslav7ec28e82015-05-20 17:01:10 -07001075 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT);
Svetoslav683914b2015-01-15 14:22:26 -08001076 }
1077
Svetoslav7ec28e82015-05-20 17:01:10 -07001078 private boolean deleteSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001079 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001080 Slog.v(LOG_TAG, "deleteSystemSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001081 }
1082
Svetoslav7ec28e82015-05-20 17:01:10 -07001083 return mutateSystemSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE);
Svetoslav683914b2015-01-15 14:22:26 -08001084 }
1085
Svetoslav7ec28e82015-05-20 17:01:10 -07001086 private boolean updateSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001087 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001088 Slog.v(LOG_TAG, "updateSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001089 + requestingUserId + ")");
1090 }
1091
Svetoslav7ec28e82015-05-20 17:01:10 -07001092 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE);
Svetoslav683914b2015-01-15 14:22:26 -08001093 }
1094
Svetoslav7ec28e82015-05-20 17:01:10 -07001095 private boolean mutateSystemSetting(String name, String value, int runAsUserId,
Svetoslav683914b2015-01-15 14:22:26 -08001096 int operation) {
Billy Lau6ad2d662015-07-18 00:26:58 +01001097 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 }
Svetoslav683914b2015-01-15 14:22:26 -08001104 }
1105
Svetoslav683914b2015-01-15 14:22:26 -08001106 // Resolve the userId on whose behalf the call is made.
1107 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId);
1108
Svetoslavd8d25e02015-11-20 13:09:26 -08001109 // Enforce what the calling package can mutate the system settings.
1110 enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name, callingUserId);
1111
Svetoslav683914b2015-01-15 14:22:26 -08001112 // 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 Sharkey413573a2016-02-22 17:52:45 -07001120 // 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 Lago3fa139c2016-08-04 13:53:44 +01001131 getRingtoneCacheDir(owningUserId), cacheName);
Jeff Sharkey413573a2016-02-22 17:52:45 -07001132 cacheFile.delete();
1133 }
1134
Svetoslav683914b2015-01-15 14:22:26 -08001135 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001136 synchronized (mLock) {
1137 switch (operation) {
1138 case MUTATION_OPERATION_INSERT: {
1139 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001140 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SYSTEM,
1141 owningUserId, name, value, getCallingPackage(), false);
Svetoslav7ec28e82015-05-20 17:01:10 -07001142 }
1143
1144 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001145 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SYSTEM,
1146 owningUserId, name, false);
Svetoslav7ec28e82015-05-20 17:01:10 -07001147 }
1148
1149 case MUTATION_OPERATION_UPDATE: {
1150 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001151 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SYSTEM,
1152 owningUserId, name, value, getCallingPackage(), false);
Svetoslav7ec28e82015-05-20 17:01:10 -07001153 }
Svetoslav683914b2015-01-15 14:22:26 -08001154 }
1155
Svetoslav7ec28e82015-05-20 17:01:10 -07001156 return false;
Svetoslav683914b2015-01-15 14:22:26 -08001157 }
Svetoslav683914b2015-01-15 14:22:26 -08001158 }
1159
Billy Lau6ad2d662015-07-18 00:26:58 +01001160 private boolean hasWriteSecureSettingsPermission() {
Svetoslavf41334b2015-06-23 12:06:03 -07001161 // 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
Svetoslavf41334b2015-06-23 12:06:03 -07001167 return false;
1168 }
1169
Svetoslav683914b2015-01-15 14:22:26 -08001170 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 Onuki28da2e32015-11-20 11:30:44 -08001192 /**
1193 * Checks whether changing a setting to a value is prohibited by the corresponding user
1194 * restriction.
1195 *
Svet Ganov53a441c2016-04-19 19:38:00 -07001196 * <p>See also {@link com.android.server.pm.UserRestrictionsUtils#applyUserRestriction(
1197 * Context, int, String, boolean)}, which should be in sync with this method.
Makoto Onuki28da2e32015-11-20 11:30:44 -08001198 *
1199 * @return true if the change is prohibited, false if the change is allowed.
1200 */
1201 private boolean isGlobalOrSecureSettingRestrictedForUser(String setting, int userId,
Victor Chang9c7b7062016-07-12 23:47:29 +01001202 String value, int callingUid) {
Makoto Onuki28da2e32015-11-20 11:30:44 -08001203 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 Chang9c7b7062016-07-12 23:47:29 +01001240 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 Franz0ff13fc2016-07-12 13:42:21 +01001249 case Settings.Global.SAFE_BOOT_DISALLOWED:
1250 if ("1".equals(value)) return false;
1251 restriction = UserManager.DISALLOW_SAFE_BOOT;
1252 break;
1253
Makoto Onuki28da2e32015-11-20 11:30:44 -08001254 default:
Mahaver Chopra87648752016-01-08 19:23:57 +00001255 if (setting != null && setting.startsWith(Settings.Global.DATA_ROAMING)) {
Mahaver Chopradea471e2015-12-17 11:02:37 +00001256 if ("0".equals(value)) return false;
1257 restriction = UserManager.DISALLOW_DATA_ROAMING;
1258 break;
1259 }
Makoto Onuki28da2e32015-11-20 11:30:44 -08001260 return false;
Svetoslav683914b2015-01-15 14:22:26 -08001261 }
Makoto Onuki28da2e32015-11-20 11:30:44 -08001262
1263 return mUserManager.hasUserRestriction(restriction, UserHandle.of(userId));
Svetoslav683914b2015-01-15 14:22:26 -08001264 }
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 Lago3fa139c2016-08-04 13:53:44 +01001271 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 Hackborn32f40ee2016-10-20 15:54:14 -07001277 Setting settingObj = getSecureSetting(dependency, userId);
1278 if (settingObj != null && settingObj.getValue().equals("1")) {
Andre Lago3fa139c2016-08-04 13:53:44 +01001279 return parentId;
1280 }
1281 }
Svetoslav683914b2015-01-15 14:22:26 -08001282 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
Svetoslavf41334b2015-06-23 12:06:03 -07001293 private void enforceRestrictedSystemSettingsMutationForCallingPackage(int operation,
Xiaohui Chen43765b72015-08-31 10:57:33 -07001294 String name, int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001295 // 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 Chen43765b72015-08-31 10:57:33 -07001312 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001313
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 Chen43765b72015-08-31 10:57:33 -07001332 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001333
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 Chen43765b72015-08-31 10:57:33 -07001346 private PackageInfo getCallingPackageInfoOrThrow(int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001347 try {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001348 PackageInfo packageInfo = mPackageManager.getPackageInfo(
1349 getCallingPackage(), 0, userId);
1350 if (packageInfo != null) {
1351 return packageInfo;
1352 }
Xiaohui Chen43765b72015-08-31 10:57:33 -07001353 } catch (RemoteException e) {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001354 /* ignore */
Svetoslav683914b2015-01-15 14:22:26 -08001355 }
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001356 throw new IllegalStateException("Calling package doesn't exist");
Svetoslav683914b2015-01-15 14:22:26 -08001357 }
1358
1359 private int getGroupParentLocked(int userId) {
1360 // Most frequent use case.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001361 if (userId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08001362 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 {
Svetoslav7ec28e82015-05-20 17:01:10 -07001368 // Just a lookup and not reentrant, so holding a lock is fine.
Svetoslav683914b2015-01-15 14:22:26 -08001369 UserInfo userInfo = mUserManager.getProfileParent(userId);
1370 return (userInfo != null) ? userInfo.id : userId;
1371 } finally {
1372 Binder.restoreCallingIdentity(identity);
1373 }
1374 }
1375
Svetoslav683914b2015-01-15 14:22:26 -08001376 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 Onuki28da2e32015-11-20 11:30:44 -08001390 * <p>See also {@link #isGlobalOrSecureSettingRestrictedForUser()}. If DISALLOW_SHARE_LOCATION
1391 * is set, the said method will only allow values with the "-" prefix.
1392 *
Svetoslav683914b2015-01-15 14:22:26 -08001393 * @returns whether the enabled location providers changed.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001394 */
Svet Ganov53a441c2016-04-19 19:38:00 -07001395 private boolean updateLocationProvidersAllowedLocked(String value, int owningUserId,
1396 boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001397 if (TextUtils.isEmpty(value)) {
1398 return false;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001399 }
1400
Svetoslav683914b2015-01-15 14:22:26 -08001401 final char prefix = value.charAt(0);
1402 if (prefix != '+' && prefix != '-') {
Svet Ganov53a441c2016-04-19 19:38:00 -07001403 if (forceNotify) {
1404 final int key = makeKey(SETTINGS_TYPE_SECURE, owningUserId);
1405 mSettingsRegistry.notifyForSettingsChange(key,
1406 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
1407 }
Svetoslav683914b2015-01-15 14:22:26 -08001408 return false;
1409 }
1410
1411 // skip prefix
1412 value = value.substring(1);
1413
Svetoslav7ec28e82015-05-20 17:01:10 -07001414 Setting settingValue = getSecureSetting(
Svetoslav683914b2015-01-15 14:22:26 -08001415 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, owningUserId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001416 if (settingValue == null) {
1417 return false;
1418 }
Svetoslav683914b2015-01-15 14:22:26 -08001419
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 Ganov53a441c2016-04-19 19:38:00 -07001459 if (forceNotify) {
1460 final int key = makeKey(SETTINGS_TYPE_SECURE, owningUserId);
1461 mSettingsRegistry.notifyForSettingsChange(key,
1462 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
1463 }
Svetoslav683914b2015-01-15 14:22:26 -08001464 return false;
1465 }
1466
Svet Ganov53a441c2016-04-19 19:38:00 -07001467 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslavb596a2c2015-02-17 21:37:09 -08001468 owningUserId, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, newProviders,
Svet Ganov53a441c2016-04-19 19:38:00 -07001469 getCallingPackage(), forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -08001470 }
1471
Svetoslav683914b2015-01-15 14:22:26 -08001472 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 Ganov53a441c2016-04-19 19:38:00 -07001502 private Bundle packageValueForCallResult(Setting setting,
1503 boolean trackingGeneration) {
1504 if (!trackingGeneration) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001505 if (setting == null || setting.isNull()) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001506 return NULL_SETTING_BUNDLE;
1507 }
1508 return Bundle.forPair(Settings.NameValueTable.VALUE, setting.getValue());
Svetoslav683914b2015-01-15 14:22:26 -08001509 }
Svet Ganov53a441c2016-04-19 19:38:00 -07001510 Bundle result = new Bundle();
1511 result.putString(Settings.NameValueTable.VALUE,
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001512 setting != null && !setting.isNull() ? setting.getValue() : null);
Svet Ganov53a441c2016-04-19 19:38:00 -07001513 mSettingsRegistry.mGenerationRegistry.addGenerationData(result, setting.getkey());
1514 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001515 }
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 Ganov53a441c2016-04-19 19:38:00 -07001523 private boolean isTrackingGeneration(Bundle args) {
1524 return args != null && args.containsKey(Settings.CALL_METHOD_TRACK_GENERATION_KEY);
1525 }
1526
Svetoslav683914b2015-01-15 14:22:26 -08001527 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 Ganovfedb2302016-04-26 18:36:42 -07001543 if (setting.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08001544 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 Fitzpatrickf366a9b2010-08-24 16:14:07 -07001561 }
1562 }
1563
Svetoslav683914b2015-01-15 14:22:26 -08001564 return projection;
1565 }
1566
1567 private static void appendSettingToCursor(MatrixCursor cursor, Setting setting) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001568 if (setting == null || setting.isNull()) {
Suprabh Shuklac9d064a2016-04-12 18:45:34 -07001569 return;
1570 }
Svetoslav683914b2015-01-15 14:22:26 -08001571 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 Fitzpatrickf366a9b2010-08-24 16:14:07 -07001590 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001591 }
1592
Svetoslav683914b2015-01-15 14:22:26 -08001593 cursor.addRow(values);
1594 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001595
Makoto Onuki3a2c35782015-06-18 11:21:58 -07001596 private static boolean isKeyValid(String key) {
1597 return !(TextUtils.isEmpty(key) || SettingsState.isBinary(key));
1598 }
1599
Svetoslav683914b2015-01-15 14:22:26 -08001600 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);
Svetoslav28494652015-02-12 14:11:42 -08001626 return;
Svetoslav683914b2015-01-15 14:22:26 -08001627 } 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);
Svetoslav28494652015-02-12 14:11:42 -08001636 return;
Svetoslav683914b2015-01-15 14:22:26 -08001637 } else if (supportAll && where == null && whereArgs == null) {
1638 name = null;
1639 table = computeTableForSetting(uri, null);
Svetoslav28494652015-02-12 14:11:42 -08001640 return;
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001641 }
Svetoslav683914b2015-01-15 14:22:26 -08001642 } break;
1643
Svetoslav28494652015-02-12 14:11:42 -08001644 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 Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001651 }
Svetoslav28494652015-02-12 14:11:42 -08001652
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 Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001662 }
1663
Svetoslav28494652015-02-12 14:11:42 -08001664 private static String computeTableForSetting(Uri uri, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08001665 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 Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001683 }
Svetoslav683914b2015-01-15 14:22:26 -08001684
1685 return table;
1686 }
1687 }
1688
1689 final class SettingsRegistry {
1690 private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid";
1691
Svetoslav683914b2015-01-15 14:22:26 -08001692 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 Ganov53a441c2016-04-19 19:38:00 -07001698 private GenerationRegistry mGenerationRegistry;
Svetoslav683914b2015-01-15 14:22:26 -08001699
Svetoslav7e0683b2015-08-03 16:02:52 -07001700 private final Handler mHandler;
1701
Svet Ganov53a441c2016-04-19 19:38:00 -07001702 private final BackupManager mBackupManager;
1703
Svetoslav683914b2015-01-15 14:22:26 -08001704 public SettingsRegistry() {
Svetoslav7e0683b2015-08-03 16:02:52 -07001705 mHandler = new MyHandler(getContext().getMainLooper());
Svet Ganov53a441c2016-04-19 19:38:00 -07001706 mGenerationRegistry = new GenerationRegistry(mLock);
1707 mBackupManager = new BackupManager(getContext());
Svetoslav683914b2015-01-15 14:22:26 -08001708 migrateAllLegacySettingsIfNeeded();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001709 }
1710
Svetoslav683914b2015-01-15 14:22:26 -08001711 public List<String> getSettingsNamesLocked(int type, int userId) {
1712 final int key = makeKey(type, userId);
1713 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001714 if (settingsState == null) {
1715 return new ArrayList<String>();
1716 }
Svetoslav683914b2015-01-15 14:22:26 -08001717 return settingsState.getSettingNamesLocked();
1718 }
1719
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001720 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
Svetoslav683914b2015-01-15 14:22:26 -08001728 public SettingsState getSettingsLocked(int type, int userId) {
1729 final int key = makeKey(type, userId);
1730 return peekSettingsStateLocked(key);
1731 }
1732
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001733 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
Svetoslav683914b2015-01-15 14:22:26 -08001740 // Migrate the setting for this user if needed.
1741 migrateLegacySettingsForUserIfNeededLocked(userId);
1742
1743 // Ensure global settings loaded if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001744 if (userId == UserHandle.USER_SYSTEM) {
1745 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001746 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 Hackborn32f40ee2016-10-20 15:54:14 -07001764 return true;
Svetoslav683914b2015-01-15 14:22:26 -08001765 }
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 Ganov92057492016-05-16 12:36:43 -07001771 maxBytesPerPackage, mHandlerThread.getLooper());
Svetoslav683914b2015-01-15 14:22:26 -08001772 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 Fitzpatrick342984a2010-03-09 16:59:30 -08001786 } else {
Svetoslav683914b2015-01-15 14:22:26 -08001787 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 Fitzpatrick342984a2010-03-09 16:59:30 -08001810 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001811 }
Svet Ganov53a441c2016-04-19 19:38:00 -07001812
1813 // Nuke generation tracking data
1814 mGenerationRegistry.onUserRemoved(userId);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001815 }
1816
Svetoslav683914b2015-01-15 14:22:26 -08001817 public boolean insertSettingLocked(int type, int userId, String name, String value,
Svet Ganov53a441c2016-04-19 19:38:00 -07001818 String packageName, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001819 final int key = makeKey(type, userId);
1820
1821 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001822 final boolean success = settingsState != null
1823 && settingsState.insertSettingLocked(name, value, packageName);
Svetoslav683914b2015-01-15 14:22:26 -08001824
Svet Ganov53a441c2016-04-19 19:38:00 -07001825 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08001826 notifyForSettingsChange(key, name);
1827 }
1828 return success;
1829 }
1830
Svet Ganov53a441c2016-04-19 19:38:00 -07001831 public boolean deleteSettingLocked(int type, int userId, String name, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001832 final int key = makeKey(type, userId);
1833
1834 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001835 if (settingsState == null) {
1836 return false;
1837 }
Svetoslav683914b2015-01-15 14:22:26 -08001838 final boolean success = settingsState.deleteSettingLocked(name);
1839
Svet Ganov53a441c2016-04-19 19:38:00 -07001840 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08001841 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 Hackborn32f40ee2016-10-20 15:54:14 -07001850 if (settingsState == null) {
1851 return null;
1852 }
Svetoslav683914b2015-01-15 14:22:26 -08001853 return settingsState.getSettingLocked(name);
1854 }
1855
1856 public boolean updateSettingLocked(int type, int userId, String name, String value,
Svet Ganov53a441c2016-04-19 19:38:00 -07001857 String packageName, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001858 final int key = makeKey(type, userId);
1859
1860 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001861 final boolean success = settingsState != null
1862 && settingsState.updateSettingLocked(name, value, packageName);
Svetoslav683914b2015-01-15 14:22:26 -08001863
Svet Ganov53a441c2016-04-19 19:38:00 -07001864 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08001865 notifyForSettingsChange(key, name);
1866 }
1867
1868 return success;
1869 }
1870
1871 public void onPackageRemovedLocked(String packageName, int userId) {
Svet Ganov8de34802015-04-27 09:33:40 -07001872 // 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.
Svetoslav683914b2015-01-15 14:22:26 -08001876
1877 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1878 SettingsState systemSettings = mSettingsStates.get(systemKey);
Svet Ganov8de34802015-04-27 09:33:40 -07001879 if (systemSettings != null) {
1880 systemSettings.onPackageRemovedLocked(packageName);
1881 }
Svetoslav683914b2015-01-15 14:22:26 -08001882 }
1883
1884 private SettingsState peekSettingsStateLocked(int key) {
1885 SettingsState settingsState = mSettingsStates.get(key);
1886 if (settingsState != null) {
1887 return settingsState;
1888 }
1889
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001890 if (!ensureSettingsForUserLocked(getUserIdFromKey(key))) {
1891 return null;
1892 }
Svetoslav683914b2015-01-15 14:22:26 -08001893 return mSettingsStates.get(key);
1894 }
1895
1896 private void migrateAllLegacySettingsIfNeeded() {
1897 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07001898 final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001899 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 Yamasanibf2ef612016-03-07 16:37:18 -08001947 // 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();
Svetoslav683914b2015-01-15 14:22:26 -08001953
1954 // Move over the secure settings.
Amith Yamasanibf2ef612016-03-07 16:37:18 -08001955 // 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.
Svetoslav683914b2015-01-15 14:22:26 -08001957 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 Yamasanibf2ef612016-03-07 16:37:18 -08001964 // 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 }
Svetoslav683914b2015-01-15 14:22:26 -08001974
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 Nonaka6e5b6022016-04-27 16:32:44 +09002020 if (!value.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08002021 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) {
Svetoslav683914b2015-01-15 14:22:26 -08002057 final int userId = getUserIdFromKey(key);
2058 Uri uri = getNotificationUriFor(key, name);
2059
Phil Weaver83fec002016-05-11 10:55:29 -07002060 mGenerationRegistry.incrementGeneration(key);
2061
Svetoslav7e0683b2015-08-03 16:02:52 -07002062 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
2063 userId, 0, uri).sendToTarget();
2064
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002065 if (isSecureSettingsKey(key)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07002066 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
2067 sSecureCloneToManagedSettings);
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002068 } else if (isSystemSettingsKey(key)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07002069 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
2070 sSystemCloneToManagedSettings);
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002071 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002072
Svet Ganov53a441c2016-04-19 19:38:00 -07002073 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002074 }
2075
Svet Ganov53a441c2016-04-19 19:38:00 -07002076 private void maybeNotifyProfiles(int type, int userId, Uri uri, String name,
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002077 Set<String> keysCloned) {
2078 if (keysCloned.contains(name)) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002079 for (int profileId : mUserManager.getProfileIdsWithDisabled(userId)) {
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002080 // the notification for userId has already been sent.
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002081 if (profileId != userId) {
Svetoslav7e0683b2015-08-03 16:02:52 -07002082 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002083 profileId, 0, uri).sendToTarget();
Svet Ganov53a441c2016-04-19 19:38:00 -07002084 final int key = makeKey(type, profileId);
2085 mGenerationRegistry.incrementGeneration(key);
2086
2087 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002088 }
2089 }
2090 }
Svetoslav683914b2015-01-15 14:22:26 -08002091 }
2092
Svetoslav683914b2015-01-15 14:22:26 -08002093 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
Svetoslav7e0683b2015-08-03 16:02:52 -07002151 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
Svetoslav683914b2015-01-15 14:22:26 -08002178 private final class UpgradeController {
Jeremy Joslin8bdad342016-12-14 11:46:47 -08002179 private static final int SETTINGS_VERSION = 136;
Svetoslav683914b2015-01-15 14:22:26 -08002180
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 Ganov53a441c2016-04-19 19:38:00 -07002190 SETTINGS_TYPE_SECURE, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08002191
2192 // Try an update from the current state.
2193 final int oldVersion = secureSettings.getVersionLocked();
2194 final int newVersion = SETTINGS_VERSION;
2195
Svet Ganovc9755bc2015-03-28 13:21:22 -07002196 // If up do date - done.
Svetoslav683914b2015-01-15 14:22:26 -08002197 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 Ganov264c7a92016-08-24 17:31:14 -07002219
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");
Svetoslav683914b2015-01-15 14:22:26 -08002225 }
2226
2227 // Set the global settings version if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07002228 if (mUserId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08002229 SettingsState globalSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07002230 SETTINGS_TYPE_GLOBAL, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08002231 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 Ganov53a441c2016-04-19 19:38:00 -07002239 SETTINGS_TYPE_SYSTEM, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08002240 systemSettings.setVersionLocked(newVersion);
2241 }
2242
2243 private SettingsState getGlobalSettingsLocked() {
Xiaohui Chen43765b72015-08-31 10:57:33 -07002244 return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002245 }
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 Brown503cffc2015-03-26 18:08:51 -07002255 /**
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 */
Svetoslav683914b2015-01-15 14:22:26 -08002280 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 Brown503cffc2015-03-26 18:08:51 -07002286 int currentVersion = oldVersion;
Svetoslav683914b2015-01-15 14:22:26 -08002287
John Spurlocke11ae112015-05-11 16:09:03 -04002288 // v119: Reset zen + ringer mode.
2289 if (currentVersion == 118) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07002290 if (userId == UserHandle.USER_SYSTEM) {
John Spurlocke11ae112015-05-11 16:09:03 -04002291 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 Monk27bbb2d2015-03-31 16:46:39 -04002302 // 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
Svetoslav7e0683b2015-08-03 16:02:52 -07002313 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 Onuki3a2c35782015-06-18 11:21:58 -07002318
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02002319 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 Ganovfedb2302016-04-26 18:36:42 -07002329 currentSetting.isNull()) {
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02002330 secureSettings.insertSettingLocked(
2331 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT,
2332 defaultComponent,
2333 SettingsState.SYSTEM_PACKAGE_NAME);
2334 }
2335 currentVersion = 122;
2336 }
Suprabh Shukla269c11e2015-12-02 16:51:16 -08002337
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 Ganovfedb2302016-04-26 18:36:42 -07002345 if (currentSetting.isNull()) {
Suprabh Shukla269c11e2015-12-02 16:51:16 -08002346 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 Leebd179282015-12-17 19:01:37 -08002355
2356 if (currentVersion == 123) {
Bryce Leeec85f342015-12-16 13:32:28 -08002357 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 Leebd179282015-12-17 19:01:37 -08002362 currentVersion = 124;
Bryce Leeec85f342015-12-16 13:32:28 -08002363 }
2364
Prathmesh Prabhude16b862016-03-04 15:22:24 -08002365 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 Ganovfedb2302016-04-26 18:36:42 -07002371 if (currentSetting.isNull()) {
Prathmesh Prabhude16b862016-03-04 15:22:24 -08002372 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 Brunk98576cf2016-03-07 18:54:28 -08002381 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 Ganovfedb2302016-04-26 18:36:42 -07002387 if (currentSetting.isNull()) {
Ruben Brunk98576cf2016-03-07 18:54:28 -08002388 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 U02ba6122016-04-01 18:41:42 +01002410 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 Nonaka6e5b6022016-04-27 16:32:44 +09002419 if (!showNotifications.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01002420 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 Nonaka6e5b6022016-04-27 16:32:44 +09002429 if (!allowPrivate.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01002430 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 Ngdc20ba62016-04-26 18:19:04 +01002440 if (currentVersion == 127) {
Mahaver Chopra3d9805d2016-07-07 16:25:05 +01002441 // version 127 is no longer used.
Steven Ngdc20ba62016-04-26 18:19:04 +01002442 currentVersion = 128;
2443 }
2444
Julia Reynolds1f721e12016-07-11 08:50:58 -04002445 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 Sandler71f85e92016-07-20 13:46:05 -04002476 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 Hugh96e9cc52016-07-12 15:17:24 -07002493 if (currentVersion == 130) {
Adrian Roos69741a22016-10-21 14:49:17 -07002494 // 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 Hugh96e9cc52016-07-12 15:17:24 -07002509 // 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 Roos69741a22016-10-21 14:49:17 -07002521 currentVersion = 132;
Anthony Hugh96e9cc52016-07-12 15:17:24 -07002522 }
2523
Adrian Roos69741a22016-10-21 14:49:17 -07002524 if (currentVersion == 132) {
2525 // Version 132: Allow managed profile to optionally use the parent's ringtones
Andre Lagoea35e072016-08-04 13:41:13 +01002526 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 Roos69741a22016-10-21 14:49:17 -07002533 currentVersion = 133;
Andre Lagoea35e072016-08-04 13:41:13 +01002534 }
2535
Adrian Roos69741a22016-10-21 14:49:17 -07002536 if (currentVersion == 133) {
2537 // Version 133: Add default end button behavior
Keun-young Parkec7a1182016-10-18 11:52:38 -07002538 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 Roos69741a22016-10-21 14:49:17 -07002546 currentVersion = 134;
Keun-young Parkec7a1182016-10-18 11:52:38 -07002547 }
2548
Phil Weaver89e3ffc2016-09-19 13:51:10 -07002549 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 Joslin8bdad342016-12-14 11:46:47 -08002557 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 Sandler71f85e92016-07-20 13:46:05 -04002581 if (currentVersion != newVersion) {
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07002582 Slog.wtf("SettingsProvider", "warning: upgrading settings database to version "
Dan Sandler71f85e92016-07-20 13:46:05 -04002583 + 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 Brown503cffc2015-03-26 18:08:51 -07002590 // vXXX: Add new settings above this point.
Svetoslav683914b2015-01-15 14:22:26 -08002591
Jeff Brown503cffc2015-03-26 18:08:51 -07002592 // Return the current version.
2593 return currentVersion;
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -08002594 }
2595 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002596 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07002597}