blob: 3e62158d591cdc5c53872357b5fbf67e00a7e3e0 [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
Svetoslav Ganove080da92016-12-21 17:10:35 -080087import static android.os.Process.ROOT_UID;
88import static android.os.Process.SYSTEM_UID;
89import static android.os.Process.SHELL_UID;
90
Svetoslav683914b2015-01-15 14:22:26 -080091/**
92 * <p>
93 * This class is a content provider that publishes the system settings.
94 * It can be accessed via the content provider APIs or via custom call
95 * commands. The latter is a bit faster and is the preferred way to access
96 * the platform settings.
97 * </p>
98 * <p>
99 * There are three settings types, global (with signature level protection
100 * and shared across users), secure (with signature permission level
101 * protection and per user), and system (with dangerous permission level
102 * protection and per user). Global settings are stored under the device owner.
103 * Each of these settings is represented by a {@link
104 * com.android.providers.settings.SettingsState} object mapped to an integer
105 * key derived from the setting type in the most significant bits and user
106 * id in the least significant bits. Settings are synchronously loaded on
107 * instantiation of a SettingsState and asynchronously persisted on mutation.
108 * Settings are stored in the user specific system directory.
109 * </p>
110 * <p>
111 * Apps targeting APIs Lollipop MR1 and lower can add custom settings entries
112 * and get a warning. Targeting higher API version prohibits this as the
113 * system settings are not a place for apps to save their state. When a package
114 * is removed the settings it added are deleted. Apps cannot delete system
115 * settings added by the platform. System settings values are validated to
116 * ensure the clients do not put bad values. Global and secure settings are
117 * changed only by trusted parties, therefore no validation is performed. Also
118 * there is a limit on the amount of app specific settings that can be added
119 * to prevent unlimited growth of the system process memory footprint.
120 * </p>
121 */
122@SuppressWarnings("deprecation")
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700123public class SettingsProvider extends ContentProvider {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700124 static final boolean DEBUG = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700125
Svetoslav Ganov264c7a92016-08-24 17:31:14 -0700126 private static final boolean DROP_DATABASE_ON_MIGRATION = true;
Svetoslav683914b2015-01-15 14:22:26 -0800127
128 private static final String LOG_TAG = "SettingsProvider";
Christopher Tate0da13572013-10-13 17:34:49 -0700129
Christopher Tate06efb532012-08-24 15:29:27 -0700130 private static final String TABLE_SYSTEM = "system";
131 private static final String TABLE_SECURE = "secure";
132 private static final String TABLE_GLOBAL = "global";
Svetoslav683914b2015-01-15 14:22:26 -0800133
134 // Old tables no longer exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 private static final String TABLE_FAVORITES = "favorites";
136 private static final String TABLE_OLD_FAVORITES = "old_favorites";
Svetoslav683914b2015-01-15 14:22:26 -0800137 private static final String TABLE_BLUETOOTH_DEVICES = "bluetooth_devices";
138 private static final String TABLE_BOOKMARKS = "bookmarks";
139 private static final String TABLE_ANDROID_METADATA = "android_metadata";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
Svetoslav683914b2015-01-15 14:22:26 -0800141 // The set of removed legacy tables.
142 private static final Set<String> REMOVED_LEGACY_TABLES = new ArraySet<>();
Christopher Tate06efb532012-08-24 15:29:27 -0700143 static {
Svetoslav683914b2015-01-15 14:22:26 -0800144 REMOVED_LEGACY_TABLES.add(TABLE_FAVORITES);
145 REMOVED_LEGACY_TABLES.add(TABLE_OLD_FAVORITES);
146 REMOVED_LEGACY_TABLES.add(TABLE_BLUETOOTH_DEVICES);
147 REMOVED_LEGACY_TABLES.add(TABLE_BOOKMARKS);
148 REMOVED_LEGACY_TABLES.add(TABLE_ANDROID_METADATA);
149 }
Christopher Tate06efb532012-08-24 15:29:27 -0700150
Svetoslav683914b2015-01-15 14:22:26 -0800151 private static final int MUTATION_OPERATION_INSERT = 1;
152 private static final int MUTATION_OPERATION_DELETE = 2;
153 private static final int MUTATION_OPERATION_UPDATE = 3;
Svetoslav Ganove080da92016-12-21 17:10:35 -0800154 private static final int MUTATION_OPERATION_RESET = 4;
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400155
Svetoslav683914b2015-01-15 14:22:26 -0800156 private static final String[] ALL_COLUMNS = new String[] {
157 Settings.NameValueTable._ID,
158 Settings.NameValueTable.NAME,
159 Settings.NameValueTable.VALUE
160 };
161
Svet Ganov53a441c2016-04-19 19:38:00 -0700162 public static final int SETTINGS_TYPE_GLOBAL = 0;
163 public static final int SETTINGS_TYPE_SYSTEM = 1;
164 public static final int SETTINGS_TYPE_SECURE = 2;
Svetoslav683914b2015-01-15 14:22:26 -0800165
Svet Ganov53a441c2016-04-19 19:38:00 -0700166 public static final int SETTINGS_TYPE_MASK = 0xF0000000;
167 public static final int SETTINGS_TYPE_SHIFT = 28;
168
169 private static final Bundle NULL_SETTING_BUNDLE = Bundle.forPair(
170 Settings.NameValueTable.VALUE, null);
Christopher Tate06efb532012-08-24 15:29:27 -0700171
Svetoslav683914b2015-01-15 14:22:26 -0800172 // Per user secure settings that moved to the for all users global settings.
173 static final Set<String> sSecureMovedToGlobalSettings = new ArraySet<>();
174 static {
175 Settings.Secure.getMovedToGlobalSettings(sSecureMovedToGlobalSettings);
Christopher Tate06efb532012-08-24 15:29:27 -0700176 }
177
Svetoslav683914b2015-01-15 14:22:26 -0800178 // Per user system settings that moved to the for all users global settings.
179 static final Set<String> sSystemMovedToGlobalSettings = new ArraySet<>();
180 static {
181 Settings.System.getMovedToGlobalSettings(sSystemMovedToGlobalSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700182 }
183
Svetoslav683914b2015-01-15 14:22:26 -0800184 // Per user system settings that moved to the per user secure settings.
185 static final Set<String> sSystemMovedToSecureSettings = new ArraySet<>();
186 static {
187 Settings.System.getMovedToSecureSettings(sSystemMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700188 }
189
Svetoslav683914b2015-01-15 14:22:26 -0800190 // Per all users global settings that moved to the per user secure settings.
191 static final Set<String> sGlobalMovedToSecureSettings = new ArraySet<>();
192 static {
193 Settings.Global.getMovedToSecureSettings(sGlobalMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700194 }
195
Svetoslav683914b2015-01-15 14:22:26 -0800196 // Per user secure settings that are cloned for the managed profiles of the user.
197 private static final Set<String> sSecureCloneToManagedSettings = new ArraySet<>();
198 static {
199 Settings.Secure.getCloneToManagedProfileSettings(sSecureCloneToManagedSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700200 }
201
Svetoslav683914b2015-01-15 14:22:26 -0800202 // Per user system settings that are cloned for the managed profiles of the user.
203 private static final Set<String> sSystemCloneToManagedSettings = new ArraySet<>();
204 static {
205 Settings.System.getCloneToManagedProfileSettings(sSystemCloneToManagedSettings);
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400206 }
207
Andre Lago3fa139c2016-08-04 13:53:44 +0100208 // Per user system settings that are cloned from the profile's parent when a dependency
209 // in {@link Settings.Secure} is set to "1".
210 public static final Map<String, String> sSystemCloneFromParentOnDependency = new ArrayMap<>();
211 static {
212 Settings.System.getCloneFromParentOnValueSettings(sSystemCloneFromParentOnDependency);
213 }
214
Svetoslav683914b2015-01-15 14:22:26 -0800215 private final Object mLock = new Object();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700216
Svetoslav683914b2015-01-15 14:22:26 -0800217 @GuardedBy("mLock")
218 private SettingsRegistry mSettingsRegistry;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700219
Svet Ganova8f90262016-05-10 08:44:48 -0700220 @GuardedBy("mLock")
221 private HandlerThread mHandlerThread;
222
Svetoslav7ec28e82015-05-20 17:01:10 -0700223 // We have to call in the user manager with no lock held,
224 private volatile UserManager mUserManager;
Svetoslav683914b2015-01-15 14:22:26 -0800225
Svetoslav7ec28e82015-05-20 17:01:10 -0700226 // We have to call in the package manager with no lock held,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700227 private volatile IPackageManager mPackageManager;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700228
Svet Ganov53a441c2016-04-19 19:38:00 -0700229 public static int makeKey(int type, int userId) {
230 return (type << SETTINGS_TYPE_SHIFT) | userId;
231 }
232
233 public static int getTypeFromKey(int key) {
234 return key >>> SETTINGS_TYPE_SHIFT;
235 }
236
237 public static int getUserIdFromKey(int key) {
238 return key & ~SETTINGS_TYPE_MASK;
239 }
240
241 public static String settingTypeToString(int type) {
242 switch (type) {
243 case SETTINGS_TYPE_GLOBAL: {
244 return "SETTINGS_GLOBAL";
245 }
246 case SETTINGS_TYPE_SECURE: {
247 return "SETTINGS_SECURE";
248 }
249 case SETTINGS_TYPE_SYSTEM: {
250 return "SETTINGS_SYSTEM";
251 }
252 default: {
253 return "UNKNOWN";
254 }
255 }
256 }
257
258 public static String keyToString(int key) {
259 return "Key[user=" + getUserIdFromKey(key) + ";type="
260 + settingTypeToString(getTypeFromKey(key)) + "]";
261 }
262
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700263 @Override
264 public boolean onCreate() {
Svetoslav683914b2015-01-15 14:22:26 -0800265 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700266 mUserManager = UserManager.get(getContext());
267 mPackageManager = AppGlobals.getPackageManager();
Svet Ganova8f90262016-05-10 08:44:48 -0700268 mHandlerThread = new HandlerThread(LOG_TAG,
269 Process.THREAD_PRIORITY_BACKGROUND);
270 mHandlerThread.start();
Svetoslav683914b2015-01-15 14:22:26 -0800271 mSettingsRegistry = new SettingsRegistry();
272 }
273 registerBroadcastReceivers();
Svet Ganov53a441c2016-04-19 19:38:00 -0700274 startWatchingUserRestrictionChanges();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700275 ServiceManager.addService("settings", new SettingsService(this));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700276 return true;
277 }
278
Svetoslav683914b2015-01-15 14:22:26 -0800279 @Override
280 public Bundle call(String method, String name, Bundle args) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700281 final int requestingUserId = getRequestingUserId(args);
282 switch (method) {
283 case Settings.CALL_METHOD_GET_GLOBAL: {
284 Setting setting = getGlobalSetting(name);
Svet Ganov53a441c2016-04-19 19:38:00 -0700285 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav683914b2015-01-15 14:22:26 -0800286 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700287
288 case Settings.CALL_METHOD_GET_SECURE: {
289 Setting setting = getSecureSetting(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_GET_SYSTEM: {
294 Setting setting = getSystemSetting(name, requestingUserId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700295 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700296 }
297
298 case Settings.CALL_METHOD_PUT_GLOBAL: {
299 String value = getSettingValue(args);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800300 String tag = getSettingTag(args);
301 final boolean makeDefault = getSettingMakeDefault(args);
302 insertGlobalSetting(name, value, tag, makeDefault, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700303 break;
304 }
305
306 case Settings.CALL_METHOD_PUT_SECURE: {
307 String value = getSettingValue(args);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800308 String tag = getSettingTag(args);
309 final boolean makeDefault = getSettingMakeDefault(args);
310 insertSecureSetting(name, value, tag, makeDefault, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700311 break;
312 }
313
314 case Settings.CALL_METHOD_PUT_SYSTEM: {
315 String value = getSettingValue(args);
316 insertSystemSetting(name, value, requestingUserId);
317 break;
318 }
319
Svetoslav Ganove080da92016-12-21 17:10:35 -0800320 case Settings.CALL_METHOD_RESET_GLOBAL: {
321 final int mode = getResetModeEnforcingPermission(args);
322 String tag = getSettingTag(args);
323 resetGlobalSetting(requestingUserId, mode, tag);
324 break;
325 }
326
327 case Settings.CALL_METHOD_RESET_SECURE: {
328 final int mode = getResetModeEnforcingPermission(args);
329 String tag = getSettingTag(args);
330 resetSecureSetting(requestingUserId, mode, tag);
331 break;
332 }
333
Svetoslav7ec28e82015-05-20 17:01:10 -0700334 default: {
335 Slog.w(LOG_TAG, "call() with invalid method: " + method);
336 } break;
Christopher Tate06efb532012-08-24 15:29:27 -0700337 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700338
Christopher Tate06efb532012-08-24 15:29:27 -0700339 return null;
340 }
341
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800342 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800343 public String getType(Uri uri) {
344 Arguments args = new Arguments(uri, null, null, true);
345 if (TextUtils.isEmpty(args.name)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700346 return "vnd.android.cursor.dir/" + args.table;
347 } else {
Svetoslav7ec28e82015-05-20 17:01:10 -0700348 return "vnd.android.cursor.item/" + args.table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700349 }
350 }
351
352 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800353 public Cursor query(Uri uri, String[] projection, String where, String[] whereArgs,
354 String order) {
355 if (DEBUG) {
356 Slog.v(LOG_TAG, "query() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700357 }
358
Svetoslav683914b2015-01-15 14:22:26 -0800359 Arguments args = new Arguments(uri, where, whereArgs, true);
360 String[] normalizedProjection = normalizeProjection(projection);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700361
Svetoslav683914b2015-01-15 14:22:26 -0800362 // If a legacy table that is gone, done.
363 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
364 return new MatrixCursor(normalizedProjection, 0);
365 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700366
Svetoslav7ec28e82015-05-20 17:01:10 -0700367 switch (args.table) {
368 case TABLE_GLOBAL: {
369 if (args.name != null) {
370 Setting setting = getGlobalSetting(args.name);
371 return packageSettingForQuery(setting, normalizedProjection);
372 } else {
373 return getAllGlobalSettings(projection);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700374 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700375 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700376
Svetoslav7ec28e82015-05-20 17:01:10 -0700377 case TABLE_SECURE: {
378 final int userId = UserHandle.getCallingUserId();
379 if (args.name != null) {
380 Setting setting = getSecureSetting(args.name, userId);
381 return packageSettingForQuery(setting, normalizedProjection);
382 } else {
383 return getAllSecureSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800384 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700385 }
Svetoslav683914b2015-01-15 14:22:26 -0800386
Svetoslav7ec28e82015-05-20 17:01:10 -0700387 case TABLE_SYSTEM: {
388 final int userId = UserHandle.getCallingUserId();
389 if (args.name != null) {
390 Setting setting = getSystemSetting(args.name, userId);
391 return packageSettingForQuery(setting, normalizedProjection);
392 } else {
393 return getAllSystemSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800394 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700395 }
Svetoslav683914b2015-01-15 14:22:26 -0800396
Svetoslav7ec28e82015-05-20 17:01:10 -0700397 default: {
398 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700399 }
400 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700401 }
402
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700403 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800404 public Uri insert(Uri uri, ContentValues values) {
405 if (DEBUG) {
406 Slog.v(LOG_TAG, "insert() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700407 }
408
Svetoslav683914b2015-01-15 14:22:26 -0800409 String table = getValidTableOrThrow(uri);
Christopher Tate06efb532012-08-24 15:29:27 -0700410
Svetoslav683914b2015-01-15 14:22:26 -0800411 // If a legacy table that is gone, done.
412 if (REMOVED_LEGACY_TABLES.contains(table)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 return null;
414 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700415
Svetoslav683914b2015-01-15 14:22:26 -0800416 String name = values.getAsString(Settings.Secure.NAME);
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700417 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800418 return null;
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700419 }
420
Svetoslav683914b2015-01-15 14:22:26 -0800421 String value = values.getAsString(Settings.Secure.VALUE);
422
Svetoslav7ec28e82015-05-20 17:01:10 -0700423 switch (table) {
424 case TABLE_GLOBAL: {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800425 if (insertGlobalSetting(name, value, null, false,
426 UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700427 return Uri.withAppendedPath(Settings.Global.CONTENT_URI, name);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700428 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700429 } break;
430
431 case TABLE_SECURE: {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800432 if (insertSecureSetting(name, value, null, false,
433 UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700434 return Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name);
435 }
436 } break;
437
438 case TABLE_SYSTEM: {
439 if (insertSystemSetting(name, value, UserHandle.getCallingUserId())) {
440 return Uri.withAppendedPath(Settings.System.CONTENT_URI, name);
441 }
442 } break;
443
444 default: {
445 throw new IllegalArgumentException("Bad Uri path:" + uri);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700446 }
447 }
448
Svetoslav683914b2015-01-15 14:22:26 -0800449 return null;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700450 }
451
452 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800453 public int bulkInsert(Uri uri, ContentValues[] allValues) {
454 if (DEBUG) {
455 Slog.v(LOG_TAG, "bulkInsert() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700457
Svetoslav683914b2015-01-15 14:22:26 -0800458 int insertionCount = 0;
459 final int valuesCount = allValues.length;
460 for (int i = 0; i < valuesCount; i++) {
461 ContentValues values = allValues[i];
462 if (insert(uri, values) != null) {
463 insertionCount++;
464 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700465 }
Svetoslav683914b2015-01-15 14:22:26 -0800466
467 return insertionCount;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700468 }
469
470 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800471 public int delete(Uri uri, String where, String[] whereArgs) {
472 if (DEBUG) {
473 Slog.v(LOG_TAG, "delete() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700475
Svetoslav683914b2015-01-15 14:22:26 -0800476 Arguments args = new Arguments(uri, where, whereArgs, false);
477
478 // If a legacy table that is gone, done.
479 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
480 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700481 }
Svetoslav683914b2015-01-15 14:22:26 -0800482
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700483 if (!isKeyValid(args.name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800484 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700485 }
Svetoslav683914b2015-01-15 14:22:26 -0800486
Svetoslav7ec28e82015-05-20 17:01:10 -0700487 switch (args.table) {
488 case TABLE_GLOBAL: {
489 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700490 return deleteGlobalSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700491 }
Svetoslav683914b2015-01-15 14:22:26 -0800492
Svetoslav7ec28e82015-05-20 17:01:10 -0700493 case TABLE_SECURE: {
494 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700495 return deleteSecureSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700496 }
Svetoslav683914b2015-01-15 14:22:26 -0800497
Svetoslav7ec28e82015-05-20 17:01:10 -0700498 case TABLE_SYSTEM: {
499 final int userId = UserHandle.getCallingUserId();
500 return deleteSystemSetting(args.name, userId) ? 1 : 0;
501 }
502
503 default: {
504 throw new IllegalArgumentException("Bad Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800505 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700506 }
Svetoslav683914b2015-01-15 14:22:26 -0800507 }
508
509 @Override
510 public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
511 if (DEBUG) {
512 Slog.v(LOG_TAG, "update() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700513 }
Svetoslav683914b2015-01-15 14:22:26 -0800514
515 Arguments args = new Arguments(uri, where, whereArgs, false);
516
517 // If a legacy table that is gone, done.
518 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
519 return 0;
520 }
521
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700522 String name = values.getAsString(Settings.Secure.NAME);
523 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800524 return 0;
525 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700526 String value = values.getAsString(Settings.Secure.VALUE);
Svetoslav683914b2015-01-15 14:22:26 -0800527
Svetoslav7ec28e82015-05-20 17:01:10 -0700528 switch (args.table) {
529 case TABLE_GLOBAL: {
530 final int userId = UserHandle.getCallingUserId();
Svetoslav Ganove080da92016-12-21 17:10:35 -0800531 return updateGlobalSetting(args.name, value, null, false,
532 userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700533 }
Svetoslav683914b2015-01-15 14:22:26 -0800534
Svetoslav7ec28e82015-05-20 17:01:10 -0700535 case TABLE_SECURE: {
536 final int userId = UserHandle.getCallingUserId();
Svetoslav Ganove080da92016-12-21 17:10:35 -0800537 return updateSecureSetting(args.name, value, null, false,
538 userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700539 }
Svetoslav683914b2015-01-15 14:22:26 -0800540
Svetoslav7ec28e82015-05-20 17:01:10 -0700541 case TABLE_SYSTEM: {
542 final int userId = UserHandle.getCallingUserId();
543 return updateSystemSetting(args.name, value, userId) ? 1 : 0;
544 }
Svetoslav683914b2015-01-15 14:22:26 -0800545
Svetoslav7ec28e82015-05-20 17:01:10 -0700546 default: {
547 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800548 }
549 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700550 }
551
552 @Override
553 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
Robin Lee2ab02e22016-07-28 18:41:23 +0100554 final int userId = getUserIdFromUri(uri, UserHandle.getCallingUserId());
555 if (userId != UserHandle.getCallingUserId()) {
556 getContext().enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS,
557 "Access files from the settings of another user");
558 }
559 uri = ContentProvider.getUriWithoutUserId(uri);
560
Andre Lago3fa139c2016-08-04 13:53:44 +0100561 final String cacheRingtoneSetting;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700562 final String cacheName;
563 if (Settings.System.RINGTONE_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100564 cacheRingtoneSetting = Settings.System.RINGTONE;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700565 cacheName = Settings.System.RINGTONE_CACHE;
566 } else if (Settings.System.NOTIFICATION_SOUND_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100567 cacheRingtoneSetting = Settings.System.NOTIFICATION_SOUND;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700568 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
569 } else if (Settings.System.ALARM_ALERT_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100570 cacheRingtoneSetting = Settings.System.ALARM_ALERT;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700571 cacheName = Settings.System.ALARM_ALERT_CACHE;
572 } else {
573 throw new FileNotFoundException("Direct file access no longer supported; "
574 + "ringtone playback is available through android.media.Ringtone");
575 }
576
Andre Lago3fa139c2016-08-04 13:53:44 +0100577 int actualCacheOwner;
578 // Redirect cache to parent if ringtone setting is owned by profile parent
579 synchronized (mLock) {
580 actualCacheOwner = resolveOwningUserIdForSystemSettingLocked(userId,
581 cacheRingtoneSetting);
582 }
583 final File cacheFile = new File(getRingtoneCacheDir(actualCacheOwner), cacheName);
Jeff Sharkey413573a2016-02-22 17:52:45 -0700584 return ParcelFileDescriptor.open(cacheFile, ParcelFileDescriptor.parseMode(mode));
585 }
586
587 private File getRingtoneCacheDir(int userId) {
588 final File cacheDir = new File(Environment.getDataSystemDeDirectory(userId), "ringtones");
589 cacheDir.mkdir();
590 SELinux.restorecon(cacheDir);
591 return cacheDir;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700592 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800593
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700594 public void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800595 synchronized (mLock) {
596 final long identity = Binder.clearCallingIdentity();
597 try {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700598 SparseBooleanArray users = mSettingsRegistry.getKnownUsersLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800599 final int userCount = users.size();
600 for (int i = 0; i < userCount; i++) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700601 dumpForUserLocked(users.keyAt(i), pw);
Svetoslavb505ccc2015-02-17 12:41:04 -0800602 }
603 } finally {
604 Binder.restoreCallingIdentity(identity);
605 }
606 }
607 }
608
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700609 private void dumpForUserLocked(int userId, PrintWriter pw) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700610 if (userId == UserHandle.USER_SYSTEM) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800611 pw.println("GLOBAL SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700612 SettingsState globalSettings = mSettingsRegistry.getSettingsLocked(
613 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700614 if (globalSettings != null) {
615 dumpSettingsLocked(globalSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800616 pw.println();
617 globalSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700618 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800619 }
620
621 pw.println("SECURE SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700622 SettingsState secureSettings = mSettingsRegistry.getSettingsLocked(
623 SETTINGS_TYPE_SECURE, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700624 if (secureSettings != null) {
625 dumpSettingsLocked(secureSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800626 pw.println();
627 secureSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700628 }
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700629
Svetoslavb505ccc2015-02-17 12:41:04 -0800630 pw.println("SYSTEM SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700631 SettingsState systemSettings = mSettingsRegistry.getSettingsLocked(
632 SETTINGS_TYPE_SYSTEM, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700633 if (systemSettings != null) {
634 dumpSettingsLocked(systemSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800635 pw.println();
636 systemSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700637 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800638 }
639
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700640 private void dumpSettingsLocked(SettingsState settingsState, PrintWriter pw) {
641 List<String> names = settingsState.getSettingNamesLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800642
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700643 final int nameCount = names.size();
Svetoslavb505ccc2015-02-17 12:41:04 -0800644
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700645 for (int i = 0; i < nameCount; i++) {
646 String name = names.get(i);
647 Setting setting = settingsState.getSettingLocked(name);
648 pw.print("_id:"); pw.print(toDumpString(setting.getId()));
649 pw.print(" name:"); pw.print(toDumpString(name));
650 if (setting.getPackageName() != null) {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800651 pw.print(" pkg:"); pw.print(setting.getPackageName());
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700652 }
653 pw.print(" value:"); pw.print(toDumpString(setting.getValue()));
Svetoslav Ganove080da92016-12-21 17:10:35 -0800654 if (setting.getDefaultValue() != null) {
655 pw.print(" default:"); pw.print(setting.getDefaultValue());
656 pw.print(" defaultSystemSet:"); pw.print(setting.isDefaultSystemSet());
657 }
658 if (setting.getTag() != null) {
659 pw.print(" tag:"); pw.print(setting.getTag());
660 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800661 pw.println();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700662 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800663 }
664
Svetoslav7e0683b2015-08-03 16:02:52 -0700665 private static String toDumpString(String s) {
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700666 if (s != null) {
667 return s;
668 }
669 return "{null}";
670 }
671
Svetoslav683914b2015-01-15 14:22:26 -0800672 private void registerBroadcastReceivers() {
673 IntentFilter userFilter = new IntentFilter();
674 userFilter.addAction(Intent.ACTION_USER_REMOVED);
675 userFilter.addAction(Intent.ACTION_USER_STOPPED);
676
677 getContext().registerReceiver(new BroadcastReceiver() {
678 @Override
679 public void onReceive(Context context, Intent intent) {
680 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700681 UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800682
683 switch (intent.getAction()) {
684 case Intent.ACTION_USER_REMOVED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700685 synchronized (mLock) {
686 mSettingsRegistry.removeUserStateLocked(userId, true);
687 }
Svetoslav683914b2015-01-15 14:22:26 -0800688 } break;
689
690 case Intent.ACTION_USER_STOPPED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700691 synchronized (mLock) {
692 mSettingsRegistry.removeUserStateLocked(userId, false);
693 }
Svetoslav683914b2015-01-15 14:22:26 -0800694 } break;
695 }
696 }
697 }, userFilter);
698
699 PackageMonitor monitor = new PackageMonitor() {
700 @Override
701 public void onPackageRemoved(String packageName, int uid) {
702 synchronized (mLock) {
703 mSettingsRegistry.onPackageRemovedLocked(packageName,
704 UserHandle.getUserId(uid));
705 }
706 }
707 };
708
709 // package changes
710 monitor.register(getContext(), BackgroundThread.getHandler().getLooper(),
711 UserHandle.ALL, true);
712 }
713
Svet Ganov53a441c2016-04-19 19:38:00 -0700714 private void startWatchingUserRestrictionChanges() {
715 // TODO: The current design of settings looking different based on user restrictions
716 // should be reworked to keep them separate and system code should check the setting
717 // first followed by checking the user restriction before performing an operation.
718 UserManagerInternal userManager = LocalServices.getService(UserManagerInternal.class);
719 userManager.addUserRestrictionsListener((int userId, Bundle newRestrictions,
720 Bundle prevRestrictions) -> {
721 // We are changing the settings affected by restrictions to their current
722 // value with a forced update to ensure that all cross profile dependencies
723 // are taken into account. Also make sure the settings update to.. the same
724 // value passes the security checks, so clear binder calling id.
Svetoslav Ganove080da92016-12-21 17:10:35 -0800725 if (newRestrictions.getBoolean(UserManager.DISALLOW_SHARE_LOCATION)
726 != prevRestrictions.getBoolean(UserManager.DISALLOW_SHARE_LOCATION)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700727 final long identity = Binder.clearCallingIdentity();
728 try {
729 synchronized (mLock) {
730 Setting setting = getSecureSetting(
731 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, userId);
732 updateSecureSetting(Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800733 setting != null ? setting.getValue() : null, null,
734 true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700735 }
736 } finally {
737 Binder.restoreCallingIdentity(identity);
738 }
739 }
Svetoslav Ganove080da92016-12-21 17:10:35 -0800740 if (newRestrictions.getBoolean(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)
741 != prevRestrictions.getBoolean(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700742 final long identity = Binder.clearCallingIdentity();
743 try {
744 synchronized (mLock) {
745 Setting setting = getGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800746 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700747 updateGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800748 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700749 }
750 } finally {
751 Binder.restoreCallingIdentity(identity);
752 }
753 }
Svetoslav Ganove080da92016-12-21 17:10:35 -0800754 if (newRestrictions.getBoolean(UserManager.DISALLOW_DEBUGGING_FEATURES)
755 != prevRestrictions.getBoolean(UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700756 final long identity = Binder.clearCallingIdentity();
757 try {
758 synchronized (mLock) {
759 Setting setting = getGlobalSetting(Settings.Global.ADB_ENABLED);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800760 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700761 updateGlobalSetting(Settings.Global.ADB_ENABLED,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800762 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700763 }
764 } finally {
765 Binder.restoreCallingIdentity(identity);
766 }
767 }
Svetoslav Ganove080da92016-12-21 17:10:35 -0800768 if (newRestrictions.getBoolean(UserManager.ENSURE_VERIFY_APPS)
769 != prevRestrictions.getBoolean(UserManager.ENSURE_VERIFY_APPS)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700770 final long identity = Binder.clearCallingIdentity();
771 try {
772 synchronized (mLock) {
773 Setting enable = getGlobalSetting(
774 Settings.Global.PACKAGE_VERIFIER_ENABLE);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800775 String enableValue = enable != null ? enable.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700776 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_ENABLE,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800777 enableValue, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700778 Setting include = getGlobalSetting(
779 Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800780 String includeValue = include != null ? include.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700781 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800782 includeValue, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700783 }
784 } finally {
785 Binder.restoreCallingIdentity(identity);
786 }
787 }
Svetoslav Ganove080da92016-12-21 17:10:35 -0800788 if (newRestrictions.getBoolean(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)
789 != prevRestrictions.getBoolean(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700790 final long identity = Binder.clearCallingIdentity();
791 try {
792 synchronized (mLock) {
793 Setting setting = getGlobalSetting(
794 Settings.Global.PREFERRED_NETWORK_MODE);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800795 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700796 updateGlobalSetting(Settings.Global.PREFERRED_NETWORK_MODE,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800797 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700798 }
799 } finally {
800 Binder.restoreCallingIdentity(identity);
801 }
802 }
803 });
804 }
805
Svetoslav7ec28e82015-05-20 17:01:10 -0700806 private Cursor getAllGlobalSettings(String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800807 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700808 Slog.v(LOG_TAG, "getAllGlobalSettings()");
Svetoslav683914b2015-01-15 14:22:26 -0800809 }
810
Svetoslav7ec28e82015-05-20 17:01:10 -0700811 synchronized (mLock) {
812 // Get the settings.
813 SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700814 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800815
Svetoslav7ec28e82015-05-20 17:01:10 -0700816 List<String> names = settingsState.getSettingNamesLocked();
Svetoslav683914b2015-01-15 14:22:26 -0800817
Svetoslav7ec28e82015-05-20 17:01:10 -0700818 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -0800819
Svetoslav7ec28e82015-05-20 17:01:10 -0700820 String[] normalizedProjection = normalizeProjection(projection);
821 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -0800822
Svetoslav7ec28e82015-05-20 17:01:10 -0700823 // Anyone can get the global settings, so no security checks.
824 for (int i = 0; i < nameCount; i++) {
825 String name = names.get(i);
826 Setting setting = settingsState.getSettingLocked(name);
827 appendSettingToCursor(result, setting);
828 }
829
830 return result;
Svetoslav683914b2015-01-15 14:22:26 -0800831 }
Svetoslav683914b2015-01-15 14:22:26 -0800832 }
833
Svetoslav7ec28e82015-05-20 17:01:10 -0700834 private Setting getGlobalSetting(String name) {
Svetoslav683914b2015-01-15 14:22:26 -0800835 if (DEBUG) {
836 Slog.v(LOG_TAG, "getGlobalSetting(" + name + ")");
837 }
838
839 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700840 synchronized (mLock) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700841 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_GLOBAL,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700842 UserHandle.USER_SYSTEM, name);
Svetoslav683914b2015-01-15 14:22:26 -0800843 }
Svetoslav683914b2015-01-15 14:22:26 -0800844 }
845
Svetoslav Ganove080da92016-12-21 17:10:35 -0800846 private boolean updateGlobalSetting(String name, String value, String tag,
847 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800848 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800849 Slog.v(LOG_TAG, "updateGlobalSetting(" + name + ", " + value + ", "
850 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
851 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800852 }
Svetoslav Ganove080da92016-12-21 17:10:35 -0800853 return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId,
854 MUTATION_OPERATION_UPDATE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -0800855 }
856
Svetoslav Ganove080da92016-12-21 17:10:35 -0800857 private boolean insertGlobalSetting(String name, String value, String tag,
858 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700859 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800860 Slog.v(LOG_TAG, "insertGlobalSetting(" + name + ", " + value + ", "
861 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
862 + ", " + forceNotify + ")");
Svetoslav7ec28e82015-05-20 17:01:10 -0700863 }
Svetoslav Ganove080da92016-12-21 17:10:35 -0800864 return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId,
865 MUTATION_OPERATION_INSERT, forceNotify, 0);
Svetoslav7ec28e82015-05-20 17:01:10 -0700866 }
867
Svet Ganov53a441c2016-04-19 19:38:00 -0700868 private boolean deleteGlobalSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800869 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800870 Slog.v(LOG_TAG, "deleteGlobalSetting(" + name + ", " + requestingUserId
871 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800872 }
Svetoslav Ganove080da92016-12-21 17:10:35 -0800873 return mutateGlobalSetting(name, null, null, false, requestingUserId,
874 MUTATION_OPERATION_DELETE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -0800875 }
876
Svetoslav Ganove080da92016-12-21 17:10:35 -0800877 private void resetGlobalSetting(int requestingUserId, int mode, String tag) {
878 if (DEBUG) {
879 Slog.v(LOG_TAG, "resetGlobalSetting(" + requestingUserId + ", "
880 + mode + ", " + tag + ")");
881 }
882 mutateGlobalSetting(null, null, tag, false, requestingUserId,
883 MUTATION_OPERATION_RESET, false, mode);
884 }
885
886 private boolean mutateGlobalSetting(String name, String value, String tag,
887 boolean makeDefault, int requestingUserId, int operation, boolean forceNotify,
888 int mode) {
Svetoslav683914b2015-01-15 14:22:26 -0800889 // Make sure the caller can change the settings - treated as secure.
890 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
891
Svetoslav683914b2015-01-15 14:22:26 -0800892 // Resolve the userId on whose behalf the call is made.
893 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
894
Makoto Onuki28da2e32015-11-20 11:30:44 -0800895 // If this is a setting that is currently restricted for this user, do not allow
896 // unrestricting changes.
Svetoslav Ganove080da92016-12-21 17:10:35 -0800897 if (name != null && isGlobalOrSecureSettingRestrictedForUser(name, callingUserId, value,
Victor Chang9c7b7062016-07-12 23:47:29 +0100898 Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -0800899 return false;
900 }
901
902 // Perform the mutation.
Svetoslav7ec28e82015-05-20 17:01:10 -0700903 synchronized (mLock) {
904 switch (operation) {
905 case MUTATION_OPERATION_INSERT: {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800906 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_GLOBAL,
907 UserHandle.USER_SYSTEM, name, value, tag, makeDefault,
908 getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700909 }
Svetoslav683914b2015-01-15 14:22:26 -0800910
Svetoslav7ec28e82015-05-20 17:01:10 -0700911 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700912 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_GLOBAL,
913 UserHandle.USER_SYSTEM, name, forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700914 }
Svetoslav683914b2015-01-15 14:22:26 -0800915
Svetoslav7ec28e82015-05-20 17:01:10 -0700916 case MUTATION_OPERATION_UPDATE: {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800917 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_GLOBAL,
918 UserHandle.USER_SYSTEM, name, value, tag, makeDefault,
919 getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700920 }
Svetoslav Ganove080da92016-12-21 17:10:35 -0800921
922 case MUTATION_OPERATION_RESET: {
923 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_GLOBAL,
924 UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag);
925 } return true;
Svetoslav683914b2015-01-15 14:22:26 -0800926 }
927 }
928
929 return false;
930 }
931
Svetoslav7ec28e82015-05-20 17:01:10 -0700932 private Cursor getAllSecureSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800933 if (DEBUG) {
934 Slog.v(LOG_TAG, "getAllSecureSettings(" + userId + ")");
935 }
936
937 // Resolve the userId on whose behalf the call is made.
938 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
939
Svetoslav7ec28e82015-05-20 17:01:10 -0700940 synchronized (mLock) {
941 List<String> names = mSettingsRegistry.getSettingsNamesLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700942 SETTINGS_TYPE_SECURE, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -0800943
Svetoslav7ec28e82015-05-20 17:01:10 -0700944 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -0800945
Svetoslav7ec28e82015-05-20 17:01:10 -0700946 String[] normalizedProjection = normalizeProjection(projection);
947 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -0800948
Svetoslav7ec28e82015-05-20 17:01:10 -0700949 for (int i = 0; i < nameCount; i++) {
950 String name = names.get(i);
951 // Determine the owning user as some profile settings are cloned from the parent.
952 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
953 name);
Svetoslav683914b2015-01-15 14:22:26 -0800954
Svetoslav7ec28e82015-05-20 17:01:10 -0700955 // Special case for location (sigh).
956 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
Svetoslav Ganov83a1f7f2016-04-27 13:50:49 -0700957 continue;
Svetoslav7ec28e82015-05-20 17:01:10 -0700958 }
Svetoslav683914b2015-01-15 14:22:26 -0800959
Svetoslav7ec28e82015-05-20 17:01:10 -0700960 Setting setting = mSettingsRegistry.getSettingLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700961 SETTINGS_TYPE_SECURE, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -0700962 appendSettingToCursor(result, setting);
Svetoslav683914b2015-01-15 14:22:26 -0800963 }
964
Svetoslav7ec28e82015-05-20 17:01:10 -0700965 return result;
Svetoslav683914b2015-01-15 14:22:26 -0800966 }
Svetoslav683914b2015-01-15 14:22:26 -0800967 }
968
Svetoslav7ec28e82015-05-20 17:01:10 -0700969 private Setting getSecureSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800970 if (DEBUG) {
971 Slog.v(LOG_TAG, "getSecureSetting(" + name + ", " + requestingUserId + ")");
972 }
973
974 // Resolve the userId on whose behalf the call is made.
975 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
976
977 // Determine the owning user as some profile settings are cloned from the parent.
978 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
979
980 // Special case for location (sigh).
981 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700982 SettingsState settings = mSettingsRegistry.getSettingsLocked(SETTINGS_TYPE_SECURE,
983 owningUserId);
984 return settings != null ? settings.getNullSetting() : null;
Svetoslav683914b2015-01-15 14:22:26 -0800985 }
986
987 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700988 synchronized (mLock) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700989 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav7ec28e82015-05-20 17:01:10 -0700990 owningUserId, name);
991 }
Svetoslav683914b2015-01-15 14:22:26 -0800992 }
993
Svetoslav Ganove080da92016-12-21 17:10:35 -0800994 private boolean insertSecureSetting(String name, String value, String tag,
995 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800996 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700997 Slog.v(LOG_TAG, "insertSecureSetting(" + name + ", " + value + ", "
Svetoslav Ganove080da92016-12-21 17:10:35 -0800998 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
999 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001000 }
1001
Svetoslav Ganove080da92016-12-21 17:10:35 -08001002 return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId,
1003 MUTATION_OPERATION_INSERT, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001004 }
1005
Svet Ganov53a441c2016-04-19 19:38:00 -07001006 private boolean deleteSecureSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001007 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001008 Slog.v(LOG_TAG, "deleteSecureSetting(" + name + ", " + requestingUserId
1009 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001010 }
1011
Svetoslav Ganove080da92016-12-21 17:10:35 -08001012 return mutateSecureSetting(name, null, null, false, requestingUserId,
1013 MUTATION_OPERATION_DELETE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001014 }
1015
Svetoslav Ganove080da92016-12-21 17:10:35 -08001016 private boolean updateSecureSetting(String name, String value, String tag,
1017 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001018 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001019 Slog.v(LOG_TAG, "updateSecureSetting(" + name + ", " + value + ", "
Svetoslav Ganove080da92016-12-21 17:10:35 -08001020 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1021 + ", " + forceNotify +")");
Svetoslav683914b2015-01-15 14:22:26 -08001022 }
1023
Svetoslav Ganove080da92016-12-21 17:10:35 -08001024 return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId,
1025 MUTATION_OPERATION_UPDATE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001026 }
1027
Svetoslav Ganove080da92016-12-21 17:10:35 -08001028 private void resetSecureSetting(int requestingUserId, int mode, String tag) {
1029 if (DEBUG) {
1030 Slog.v(LOG_TAG, "resetSecureSetting(" + requestingUserId + ", "
1031 + mode + ", " + tag + ")");
1032 }
1033
1034 mutateSecureSetting(null, null, tag, false, requestingUserId,
1035 MUTATION_OPERATION_RESET, false, mode);
1036 }
1037
1038 private boolean mutateSecureSetting(String name, String value, String tag,
1039 boolean makeDefault, int requestingUserId, int operation, boolean forceNotify,
1040 int mode) {
Svetoslav683914b2015-01-15 14:22:26 -08001041 // Make sure the caller can change the settings.
1042 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
1043
Svetoslav683914b2015-01-15 14:22:26 -08001044 // Resolve the userId on whose behalf the call is made.
1045 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1046
Makoto Onuki28da2e32015-11-20 11:30:44 -08001047 // If this is a setting that is currently restricted for this user, do not allow
1048 // unrestricting changes.
Svetoslav Ganove080da92016-12-21 17:10:35 -08001049 if (name != null && isGlobalOrSecureSettingRestrictedForUser(name, callingUserId, value,
Victor Chang9c7b7062016-07-12 23:47:29 +01001050 Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -08001051 return false;
1052 }
1053
1054 // Determine the owning user as some profile settings are cloned from the parent.
1055 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
1056
1057 // Only the owning user can change the setting.
1058 if (owningUserId != callingUserId) {
1059 return false;
1060 }
1061
1062 // Special cases for location providers (sigh).
1063 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001064 return updateLocationProvidersAllowedLocked(value, tag, owningUserId, makeDefault,
1065 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -08001066 }
1067
1068 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001069 synchronized (mLock) {
1070 switch (operation) {
1071 case MUTATION_OPERATION_INSERT: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001072 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001073 owningUserId, name, value, tag, makeDefault,
1074 getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -07001075 }
Svetoslav683914b2015-01-15 14:22:26 -08001076
Svetoslav7ec28e82015-05-20 17:01:10 -07001077 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001078 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SECURE,
1079 owningUserId, name, forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -07001080 }
Svetoslav683914b2015-01-15 14:22:26 -08001081
Svetoslav7ec28e82015-05-20 17:01:10 -07001082 case MUTATION_OPERATION_UPDATE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001083 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001084 owningUserId, name, value, tag, makeDefault,
1085 getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -07001086 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001087
1088 case MUTATION_OPERATION_RESET: {
1089 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_SECURE,
1090 UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag);
1091 } return true;
Svetoslav683914b2015-01-15 14:22:26 -08001092 }
1093 }
1094
1095 return false;
1096 }
1097
Svetoslav7ec28e82015-05-20 17:01:10 -07001098 private Cursor getAllSystemSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001099 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001100 Slog.v(LOG_TAG, "getAllSecureSystem(" + userId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001101 }
1102
1103 // Resolve the userId on whose behalf the call is made.
1104 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
1105
Svetoslav7ec28e82015-05-20 17:01:10 -07001106 synchronized (mLock) {
1107 List<String> names = mSettingsRegistry.getSettingsNamesLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001108 SETTINGS_TYPE_SYSTEM, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -08001109
Svetoslav7ec28e82015-05-20 17:01:10 -07001110 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001111
Svetoslav7ec28e82015-05-20 17:01:10 -07001112 String[] normalizedProjection = normalizeProjection(projection);
1113 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001114
Svetoslav7ec28e82015-05-20 17:01:10 -07001115 for (int i = 0; i < nameCount; i++) {
1116 String name = names.get(i);
Svetoslav683914b2015-01-15 14:22:26 -08001117
Svetoslav7ec28e82015-05-20 17:01:10 -07001118 // Determine the owning user as some profile settings are cloned from the parent.
1119 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId,
1120 name);
Svetoslav683914b2015-01-15 14:22:26 -08001121
Svetoslav7ec28e82015-05-20 17:01:10 -07001122 Setting setting = mSettingsRegistry.getSettingLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001123 SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001124 appendSettingToCursor(result, setting);
1125 }
1126
1127 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001128 }
Svetoslav683914b2015-01-15 14:22:26 -08001129 }
1130
Svetoslav7ec28e82015-05-20 17:01:10 -07001131 private Setting getSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001132 if (DEBUG) {
1133 Slog.v(LOG_TAG, "getSystemSetting(" + name + ", " + requestingUserId + ")");
1134 }
1135
1136 // Resolve the userId on whose behalf the call is made.
1137 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1138
1139 // Determine the owning user as some profile settings are cloned from the parent.
1140 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1141
1142 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001143 synchronized (mLock) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001144 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001145 }
Svetoslav683914b2015-01-15 14:22:26 -08001146 }
1147
Svetoslav7ec28e82015-05-20 17:01:10 -07001148 private boolean insertSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001149 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001150 Slog.v(LOG_TAG, "insertSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001151 + requestingUserId + ")");
1152 }
1153
Svetoslav7ec28e82015-05-20 17:01:10 -07001154 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT);
Svetoslav683914b2015-01-15 14:22:26 -08001155 }
1156
Svetoslav7ec28e82015-05-20 17:01:10 -07001157 private boolean deleteSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001158 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001159 Slog.v(LOG_TAG, "deleteSystemSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001160 }
1161
Svetoslav7ec28e82015-05-20 17:01:10 -07001162 return mutateSystemSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE);
Svetoslav683914b2015-01-15 14:22:26 -08001163 }
1164
Svetoslav7ec28e82015-05-20 17:01:10 -07001165 private boolean updateSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001166 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001167 Slog.v(LOG_TAG, "updateSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001168 + requestingUserId + ")");
1169 }
1170
Svetoslav7ec28e82015-05-20 17:01:10 -07001171 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE);
Svetoslav683914b2015-01-15 14:22:26 -08001172 }
1173
Svetoslav7ec28e82015-05-20 17:01:10 -07001174 private boolean mutateSystemSetting(String name, String value, int runAsUserId,
Svetoslav683914b2015-01-15 14:22:26 -08001175 int operation) {
Billy Lau6ad2d662015-07-18 00:26:58 +01001176 if (!hasWriteSecureSettingsPermission()) {
1177 // If the caller doesn't hold WRITE_SECURE_SETTINGS, we verify whether this
1178 // operation is allowed for the calling package through appops.
1179 if (!Settings.checkAndNoteWriteSettingsOperation(getContext(),
1180 Binder.getCallingUid(), getCallingPackage(), true)) {
1181 return false;
1182 }
Svetoslav683914b2015-01-15 14:22:26 -08001183 }
1184
Svetoslav683914b2015-01-15 14:22:26 -08001185 // Resolve the userId on whose behalf the call is made.
1186 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId);
1187
Svetoslavd8d25e02015-11-20 13:09:26 -08001188 // Enforce what the calling package can mutate the system settings.
1189 enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name, callingUserId);
1190
Svetoslav683914b2015-01-15 14:22:26 -08001191 // Determine the owning user as some profile settings are cloned from the parent.
1192 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1193
1194 // Only the owning user id can change the setting.
1195 if (owningUserId != callingUserId) {
1196 return false;
1197 }
1198
Jeff Sharkey413573a2016-02-22 17:52:45 -07001199 // Invalidate any relevant cache files
1200 String cacheName = null;
1201 if (Settings.System.RINGTONE.equals(name)) {
1202 cacheName = Settings.System.RINGTONE_CACHE;
1203 } else if (Settings.System.NOTIFICATION_SOUND.equals(name)) {
1204 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
1205 } else if (Settings.System.ALARM_ALERT.equals(name)) {
1206 cacheName = Settings.System.ALARM_ALERT_CACHE;
1207 }
1208 if (cacheName != null) {
1209 final File cacheFile = new File(
Andre Lago3fa139c2016-08-04 13:53:44 +01001210 getRingtoneCacheDir(owningUserId), cacheName);
Jeff Sharkey413573a2016-02-22 17:52:45 -07001211 cacheFile.delete();
1212 }
1213
Svetoslav683914b2015-01-15 14:22:26 -08001214 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001215 synchronized (mLock) {
1216 switch (operation) {
1217 case MUTATION_OPERATION_INSERT: {
1218 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001219 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001220 owningUserId, name, value, null, false, getCallingPackage(), false);
Svetoslav7ec28e82015-05-20 17:01:10 -07001221 }
1222
1223 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001224 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SYSTEM,
1225 owningUserId, name, false);
Svetoslav7ec28e82015-05-20 17:01:10 -07001226 }
1227
1228 case MUTATION_OPERATION_UPDATE: {
1229 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001230 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001231 owningUserId, name, value, null, false, getCallingPackage(), false);
Svetoslav7ec28e82015-05-20 17:01:10 -07001232 }
Svetoslav683914b2015-01-15 14:22:26 -08001233 }
1234
Svetoslav7ec28e82015-05-20 17:01:10 -07001235 return false;
Svetoslav683914b2015-01-15 14:22:26 -08001236 }
Svetoslav683914b2015-01-15 14:22:26 -08001237 }
1238
Billy Lau6ad2d662015-07-18 00:26:58 +01001239 private boolean hasWriteSecureSettingsPermission() {
Svetoslavf41334b2015-06-23 12:06:03 -07001240 // Write secure settings is a more protected permission. If caller has it we are good.
1241 if (getContext().checkCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS)
1242 == PackageManager.PERMISSION_GRANTED) {
1243 return true;
1244 }
1245
Svetoslavf41334b2015-06-23 12:06:03 -07001246 return false;
1247 }
1248
Svetoslav683914b2015-01-15 14:22:26 -08001249 private void validateSystemSettingValue(String name, String value) {
1250 Settings.System.Validator validator = Settings.System.VALIDATORS.get(name);
1251 if (validator != null && !validator.validate(value)) {
1252 throw new IllegalArgumentException("Invalid value: " + value
1253 + " for setting: " + name);
1254 }
1255 }
1256
1257 private boolean isLocationProvidersAllowedRestricted(String name, int callingUserId,
1258 int owningUserId) {
1259 // Optimization - location providers are restricted only for managed profiles.
1260 if (callingUserId == owningUserId) {
1261 return false;
1262 }
1263 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)
1264 && mUserManager.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION,
1265 new UserHandle(callingUserId))) {
1266 return true;
1267 }
1268 return false;
1269 }
1270
Makoto Onuki28da2e32015-11-20 11:30:44 -08001271 /**
1272 * Checks whether changing a setting to a value is prohibited by the corresponding user
1273 * restriction.
1274 *
Svet Ganov53a441c2016-04-19 19:38:00 -07001275 * <p>See also {@link com.android.server.pm.UserRestrictionsUtils#applyUserRestriction(
1276 * Context, int, String, boolean)}, which should be in sync with this method.
Makoto Onuki28da2e32015-11-20 11:30:44 -08001277 *
1278 * @return true if the change is prohibited, false if the change is allowed.
1279 */
1280 private boolean isGlobalOrSecureSettingRestrictedForUser(String setting, int userId,
Victor Chang9c7b7062016-07-12 23:47:29 +01001281 String value, int callingUid) {
Makoto Onuki28da2e32015-11-20 11:30:44 -08001282 String restriction;
1283 switch (setting) {
1284 case Settings.Secure.LOCATION_MODE:
1285 // Note LOCATION_MODE will be converted into LOCATION_PROVIDERS_ALLOWED
1286 // in android.provider.Settings.Secure.putStringForUser(), so we shouldn't come
1287 // here normally, but we still protect it here from a direct provider write.
1288 if (String.valueOf(Settings.Secure.LOCATION_MODE_OFF).equals(value)) return false;
1289 restriction = UserManager.DISALLOW_SHARE_LOCATION;
1290 break;
1291
1292 case Settings.Secure.LOCATION_PROVIDERS_ALLOWED:
1293 // See SettingsProvider.updateLocationProvidersAllowedLocked. "-" is to disable
1294 // a provider, which should be allowed even if the user restriction is set.
1295 if (value != null && value.startsWith("-")) return false;
1296 restriction = UserManager.DISALLOW_SHARE_LOCATION;
1297 break;
1298
1299 case Settings.Secure.INSTALL_NON_MARKET_APPS:
1300 if ("0".equals(value)) return false;
1301 restriction = UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES;
1302 break;
1303
1304 case Settings.Global.ADB_ENABLED:
1305 if ("0".equals(value)) return false;
1306 restriction = UserManager.DISALLOW_DEBUGGING_FEATURES;
1307 break;
1308
1309 case Settings.Global.PACKAGE_VERIFIER_ENABLE:
1310 case Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB:
1311 if ("1".equals(value)) return false;
1312 restriction = UserManager.ENSURE_VERIFY_APPS;
1313 break;
1314
1315 case Settings.Global.PREFERRED_NETWORK_MODE:
1316 restriction = UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS;
1317 break;
1318
Victor Chang9c7b7062016-07-12 23:47:29 +01001319 case Settings.Secure.ALWAYS_ON_VPN_APP:
1320 case Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN:
1321 // Whitelist system uid (ConnectivityService) and root uid to change always-on vpn
Svetoslav Ganove080da92016-12-21 17:10:35 -08001322 final int appId = UserHandle.getAppId(callingUid);
1323 if (appId == Process.SYSTEM_UID || appId == Process.ROOT_UID) {
Victor Chang9c7b7062016-07-12 23:47:29 +01001324 return false;
1325 }
1326 restriction = UserManager.DISALLOW_CONFIG_VPN;
1327 break;
1328
Benjamin Franz0ff13fc2016-07-12 13:42:21 +01001329 case Settings.Global.SAFE_BOOT_DISALLOWED:
1330 if ("1".equals(value)) return false;
1331 restriction = UserManager.DISALLOW_SAFE_BOOT;
1332 break;
1333
Makoto Onuki28da2e32015-11-20 11:30:44 -08001334 default:
Mahaver Chopra87648752016-01-08 19:23:57 +00001335 if (setting != null && setting.startsWith(Settings.Global.DATA_ROAMING)) {
Mahaver Chopradea471e2015-12-17 11:02:37 +00001336 if ("0".equals(value)) return false;
1337 restriction = UserManager.DISALLOW_DATA_ROAMING;
1338 break;
1339 }
Makoto Onuki28da2e32015-11-20 11:30:44 -08001340 return false;
Svetoslav683914b2015-01-15 14:22:26 -08001341 }
Makoto Onuki28da2e32015-11-20 11:30:44 -08001342
1343 return mUserManager.hasUserRestriction(restriction, UserHandle.of(userId));
Svetoslav683914b2015-01-15 14:22:26 -08001344 }
1345
1346 private int resolveOwningUserIdForSecureSettingLocked(int userId, String setting) {
1347 return resolveOwningUserIdLocked(userId, sSecureCloneToManagedSettings, setting);
1348 }
1349
1350 private int resolveOwningUserIdForSystemSettingLocked(int userId, String setting) {
Andre Lago3fa139c2016-08-04 13:53:44 +01001351 final int parentId;
1352 // Resolves dependency if setting has a dependency and the calling user has a parent
1353 if (sSystemCloneFromParentOnDependency.containsKey(setting)
1354 && (parentId = getGroupParentLocked(userId)) != userId) {
1355 // The setting has a dependency and the profile has a parent
1356 String dependency = sSystemCloneFromParentOnDependency.get(setting);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001357 Setting settingObj = getSecureSetting(dependency, userId);
1358 if (settingObj != null && settingObj.getValue().equals("1")) {
Andre Lago3fa139c2016-08-04 13:53:44 +01001359 return parentId;
1360 }
1361 }
Svetoslav683914b2015-01-15 14:22:26 -08001362 return resolveOwningUserIdLocked(userId, sSystemCloneToManagedSettings, setting);
1363 }
1364
1365 private int resolveOwningUserIdLocked(int userId, Set<String> keys, String name) {
1366 final int parentId = getGroupParentLocked(userId);
1367 if (parentId != userId && keys.contains(name)) {
1368 return parentId;
1369 }
1370 return userId;
1371 }
1372
Svetoslavf41334b2015-06-23 12:06:03 -07001373 private void enforceRestrictedSystemSettingsMutationForCallingPackage(int operation,
Xiaohui Chen43765b72015-08-31 10:57:33 -07001374 String name, int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001375 // System/root/shell can mutate whatever secure settings they want.
1376 final int callingUid = Binder.getCallingUid();
Svetoslav Ganove080da92016-12-21 17:10:35 -08001377 final int appId = UserHandle.getAppId(callingUid);
1378 if (appId == android.os.Process.SYSTEM_UID
1379 || appId == Process.SHELL_UID
1380 || appId == Process.ROOT_UID) {
Svetoslav683914b2015-01-15 14:22:26 -08001381 return;
1382 }
1383
1384 switch (operation) {
1385 case MUTATION_OPERATION_INSERT:
1386 // Insert updates.
1387 case MUTATION_OPERATION_UPDATE: {
1388 if (Settings.System.PUBLIC_SETTINGS.contains(name)) {
1389 return;
1390 }
1391
1392 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001393 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001394
1395 // Privileged apps can do whatever they want.
1396 if ((packageInfo.applicationInfo.privateFlags
1397 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1398 return;
1399 }
1400
1401 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1402 packageInfo.applicationInfo.targetSdkVersion, name);
1403 } break;
1404
1405 case MUTATION_OPERATION_DELETE: {
1406 if (Settings.System.PUBLIC_SETTINGS.contains(name)
1407 || Settings.System.PRIVATE_SETTINGS.contains(name)) {
1408 throw new IllegalArgumentException("You cannot delete system defined"
1409 + " secure settings.");
1410 }
1411
1412 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001413 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001414
1415 // Privileged apps can do whatever they want.
1416 if ((packageInfo.applicationInfo.privateFlags &
1417 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1418 return;
1419 }
1420
1421 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1422 packageInfo.applicationInfo.targetSdkVersion, name);
1423 } break;
1424 }
1425 }
1426
Xiaohui Chen43765b72015-08-31 10:57:33 -07001427 private PackageInfo getCallingPackageInfoOrThrow(int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001428 try {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001429 PackageInfo packageInfo = mPackageManager.getPackageInfo(
1430 getCallingPackage(), 0, userId);
1431 if (packageInfo != null) {
1432 return packageInfo;
1433 }
Xiaohui Chen43765b72015-08-31 10:57:33 -07001434 } catch (RemoteException e) {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001435 /* ignore */
Svetoslav683914b2015-01-15 14:22:26 -08001436 }
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001437 throw new IllegalStateException("Calling package doesn't exist");
Svetoslav683914b2015-01-15 14:22:26 -08001438 }
1439
1440 private int getGroupParentLocked(int userId) {
1441 // Most frequent use case.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001442 if (userId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08001443 return userId;
1444 }
1445 // We are in the same process with the user manager and the returned
1446 // user info is a cached instance, so just look up instead of cache.
1447 final long identity = Binder.clearCallingIdentity();
1448 try {
Svetoslav7ec28e82015-05-20 17:01:10 -07001449 // Just a lookup and not reentrant, so holding a lock is fine.
Svetoslav683914b2015-01-15 14:22:26 -08001450 UserInfo userInfo = mUserManager.getProfileParent(userId);
1451 return (userInfo != null) ? userInfo.id : userId;
1452 } finally {
1453 Binder.restoreCallingIdentity(identity);
1454 }
1455 }
1456
Svetoslav683914b2015-01-15 14:22:26 -08001457 private void enforceWritePermission(String permission) {
1458 if (getContext().checkCallingOrSelfPermission(permission)
1459 != PackageManager.PERMISSION_GRANTED) {
1460 throw new SecurityException("Permission denial: writing to settings requires:"
1461 + permission);
1462 }
1463 }
1464
1465 /*
1466 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
1467 * This setting contains a list of the currently enabled location providers.
1468 * But helper functions in android.providers.Settings can enable or disable
1469 * a single provider by using a "+" or "-" prefix before the provider name.
1470 *
Makoto Onuki28da2e32015-11-20 11:30:44 -08001471 * <p>See also {@link #isGlobalOrSecureSettingRestrictedForUser()}. If DISALLOW_SHARE_LOCATION
1472 * is set, the said method will only allow values with the "-" prefix.
1473 *
Svetoslav683914b2015-01-15 14:22:26 -08001474 * @returns whether the enabled location providers changed.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001475 */
Svetoslav Ganove080da92016-12-21 17:10:35 -08001476 private boolean updateLocationProvidersAllowedLocked(String value, String tag,
1477 int owningUserId, boolean makeDefault, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001478 if (TextUtils.isEmpty(value)) {
1479 return false;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001480 }
1481
Svetoslav683914b2015-01-15 14:22:26 -08001482 final char prefix = value.charAt(0);
1483 if (prefix != '+' && prefix != '-') {
Svet Ganov53a441c2016-04-19 19:38:00 -07001484 if (forceNotify) {
1485 final int key = makeKey(SETTINGS_TYPE_SECURE, owningUserId);
1486 mSettingsRegistry.notifyForSettingsChange(key,
1487 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
1488 }
Svetoslav683914b2015-01-15 14:22:26 -08001489 return false;
1490 }
1491
1492 // skip prefix
1493 value = value.substring(1);
1494
Svetoslav7ec28e82015-05-20 17:01:10 -07001495 Setting settingValue = getSecureSetting(
Svetoslav683914b2015-01-15 14:22:26 -08001496 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, owningUserId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001497 if (settingValue == null) {
1498 return false;
1499 }
Svetoslav683914b2015-01-15 14:22:26 -08001500
1501 String oldProviders = (settingValue != null) ? settingValue.getValue() : "";
1502
1503 int index = oldProviders.indexOf(value);
1504 int end = index + value.length();
1505
1506 // check for commas to avoid matching on partial string
1507 if (index > 0 && oldProviders.charAt(index - 1) != ',') {
1508 index = -1;
1509 }
1510
1511 // check for commas to avoid matching on partial string
1512 if (end < oldProviders.length() && oldProviders.charAt(end) != ',') {
1513 index = -1;
1514 }
1515
1516 String newProviders;
1517
1518 if (prefix == '+' && index < 0) {
1519 // append the provider to the list if not present
1520 if (oldProviders.length() == 0) {
1521 newProviders = value;
1522 } else {
1523 newProviders = oldProviders + ',' + value;
1524 }
1525 } else if (prefix == '-' && index >= 0) {
1526 // remove the provider from the list if present
1527 // remove leading or trailing comma
1528 if (index > 0) {
1529 index--;
1530 } else if (end < oldProviders.length()) {
1531 end++;
1532 }
1533
1534 newProviders = oldProviders.substring(0, index);
1535 if (end < oldProviders.length()) {
1536 newProviders += oldProviders.substring(end);
1537 }
1538 } else {
1539 // nothing changed, so no need to update the database
Svet Ganov53a441c2016-04-19 19:38:00 -07001540 if (forceNotify) {
1541 final int key = makeKey(SETTINGS_TYPE_SECURE, owningUserId);
1542 mSettingsRegistry.notifyForSettingsChange(key,
1543 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
1544 }
Svetoslav683914b2015-01-15 14:22:26 -08001545 return false;
1546 }
1547
Svet Ganov53a441c2016-04-19 19:38:00 -07001548 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslavb596a2c2015-02-17 21:37:09 -08001549 owningUserId, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, newProviders,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001550 tag, makeDefault, getCallingPackage(), forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -08001551 }
1552
Svetoslav683914b2015-01-15 14:22:26 -08001553 private static void warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1554 int targetSdkVersion, String name) {
1555 // If the app targets Lollipop MR1 or older SDK we warn, otherwise crash.
1556 if (targetSdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) {
1557 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
1558 Slog.w(LOG_TAG, "You shouldn't not change private system settings."
1559 + " This will soon become an error.");
1560 } else {
1561 Slog.w(LOG_TAG, "You shouldn't keep your settings in the secure settings."
1562 + " This will soon become an error.");
1563 }
1564 } else {
1565 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
1566 throw new IllegalArgumentException("You cannot change private secure settings.");
1567 } else {
1568 throw new IllegalArgumentException("You cannot keep your settings in"
1569 + " the secure settings.");
1570 }
1571 }
1572 }
1573
1574 private static int resolveCallingUserIdEnforcingPermissionsLocked(int requestingUserId) {
1575 if (requestingUserId == UserHandle.getCallingUserId()) {
1576 return requestingUserId;
1577 }
1578 return ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1579 Binder.getCallingUid(), requestingUserId, false, true,
1580 "get/set setting for user", null);
1581 }
1582
Svet Ganov53a441c2016-04-19 19:38:00 -07001583 private Bundle packageValueForCallResult(Setting setting,
1584 boolean trackingGeneration) {
1585 if (!trackingGeneration) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001586 if (setting == null || setting.isNull()) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001587 return NULL_SETTING_BUNDLE;
1588 }
1589 return Bundle.forPair(Settings.NameValueTable.VALUE, setting.getValue());
Svetoslav683914b2015-01-15 14:22:26 -08001590 }
Svet Ganov53a441c2016-04-19 19:38:00 -07001591 Bundle result = new Bundle();
1592 result.putString(Settings.NameValueTable.VALUE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001593 !setting.isNull() ? setting.getValue() : null);
1594 mSettingsRegistry.mGenerationRegistry.addGenerationData(result, setting.getKey());
Svet Ganov53a441c2016-04-19 19:38:00 -07001595 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001596 }
1597
1598 private static int getRequestingUserId(Bundle args) {
1599 final int callingUserId = UserHandle.getCallingUserId();
1600 return (args != null) ? args.getInt(Settings.CALL_METHOD_USER_KEY, callingUserId)
1601 : callingUserId;
1602 }
1603
Svet Ganov53a441c2016-04-19 19:38:00 -07001604 private boolean isTrackingGeneration(Bundle args) {
1605 return args != null && args.containsKey(Settings.CALL_METHOD_TRACK_GENERATION_KEY);
1606 }
1607
Svetoslav683914b2015-01-15 14:22:26 -08001608 private static String getSettingValue(Bundle args) {
1609 return (args != null) ? args.getString(Settings.NameValueTable.VALUE) : null;
1610 }
1611
Svetoslav Ganove080da92016-12-21 17:10:35 -08001612 private static String getSettingTag(Bundle args) {
1613 return (args != null) ? args.getString(Settings.CALL_METHOD_TAG_KEY) : null;
1614 }
1615
1616 private static boolean getSettingMakeDefault(Bundle args) {
1617 return (args != null) && args.getBoolean(Settings.CALL_METHOD_MAKE_DEFAULT_KEY);
1618 }
1619
1620 private static int getResetModeEnforcingPermission(Bundle args) {
1621 final int mode = (args != null) ? args.getInt(Settings.CALL_METHOD_RESET_MODE_KEY) : 0;
1622 switch (mode) {
1623 case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: {
1624 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
1625 throw new SecurityException("Only system, shell/root on a "
1626 + "debuggable build can reset to untrusted defaults");
1627 }
1628 return mode;
1629 }
1630 case Settings.RESET_MODE_UNTRUSTED_CHANGES: {
1631 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
1632 throw new SecurityException("Only system, shell/root on a "
1633 + "debuggable build can reset untrusted changes");
1634 }
1635 return mode;
1636 }
1637 case Settings.RESET_MODE_TRUSTED_DEFAULTS: {
1638 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
1639 throw new SecurityException("Only system, shell/root on a "
1640 + "debuggable build can reset to trusted defaults");
1641 }
1642 return mode;
1643 }
1644 case Settings.RESET_MODE_PACKAGE_DEFAULTS: {
1645 return mode;
1646 }
1647 }
1648 throw new IllegalArgumentException("Invalid reset mode: " + mode);
1649 }
1650
1651 private static boolean isCallerSystemOrShellOrRootOnDebuggableBuild() {
1652 final int appId = UserHandle.getAppId(Binder.getCallingUid());
1653 return appId == SYSTEM_UID || (Build.IS_DEBUGGABLE
1654 && (appId == SHELL_UID || appId == ROOT_UID));
1655 }
1656
Svetoslav683914b2015-01-15 14:22:26 -08001657 private static String getValidTableOrThrow(Uri uri) {
1658 if (uri.getPathSegments().size() > 0) {
1659 String table = uri.getPathSegments().get(0);
1660 if (DatabaseHelper.isValidTable(table)) {
1661 return table;
1662 }
1663 throw new IllegalArgumentException("Bad root path: " + table);
1664 }
1665 throw new IllegalArgumentException("Invalid URI:" + uri);
1666 }
1667
1668 private static MatrixCursor packageSettingForQuery(Setting setting, String[] projection) {
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07001669 if (setting.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08001670 return new MatrixCursor(projection, 0);
1671 }
1672 MatrixCursor cursor = new MatrixCursor(projection, 1);
1673 appendSettingToCursor(cursor, setting);
1674 return cursor;
1675 }
1676
1677 private static String[] normalizeProjection(String[] projection) {
1678 if (projection == null) {
1679 return ALL_COLUMNS;
1680 }
1681
1682 final int columnCount = projection.length;
1683 for (int i = 0; i < columnCount; i++) {
1684 String column = projection[i];
1685 if (!ArrayUtils.contains(ALL_COLUMNS, column)) {
1686 throw new IllegalArgumentException("Invalid column: " + column);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001687 }
1688 }
1689
Svetoslav683914b2015-01-15 14:22:26 -08001690 return projection;
1691 }
1692
1693 private static void appendSettingToCursor(MatrixCursor cursor, Setting setting) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001694 if (setting == null || setting.isNull()) {
Suprabh Shuklac9d064a2016-04-12 18:45:34 -07001695 return;
1696 }
Svetoslav683914b2015-01-15 14:22:26 -08001697 final int columnCount = cursor.getColumnCount();
1698
1699 String[] values = new String[columnCount];
1700
1701 for (int i = 0; i < columnCount; i++) {
1702 String column = cursor.getColumnName(i);
1703
1704 switch (column) {
1705 case Settings.NameValueTable._ID: {
1706 values[i] = setting.getId();
1707 } break;
1708
1709 case Settings.NameValueTable.NAME: {
1710 values[i] = setting.getName();
1711 } break;
1712
1713 case Settings.NameValueTable.VALUE: {
1714 values[i] = setting.getValue();
1715 } break;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001716 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001717 }
1718
Svetoslav683914b2015-01-15 14:22:26 -08001719 cursor.addRow(values);
1720 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001721
Makoto Onuki3a2c35782015-06-18 11:21:58 -07001722 private static boolean isKeyValid(String key) {
1723 return !(TextUtils.isEmpty(key) || SettingsState.isBinary(key));
1724 }
1725
Svetoslav683914b2015-01-15 14:22:26 -08001726 private static final class Arguments {
1727 private static final Pattern WHERE_PATTERN_WITH_PARAM_NO_BRACKETS =
1728 Pattern.compile("[\\s]*name[\\s]*=[\\s]*\\?[\\s]*");
1729
1730 private static final Pattern WHERE_PATTERN_WITH_PARAM_IN_BRACKETS =
1731 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*\\?[\\s]*\\)[\\s]*");
1732
1733 private static final Pattern WHERE_PATTERN_NO_PARAM_IN_BRACKETS =
1734 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*\\)[\\s]*");
1735
1736 private static final Pattern WHERE_PATTERN_NO_PARAM_NO_BRACKETS =
1737 Pattern.compile("[\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*");
1738
1739 public final String table;
1740 public final String name;
1741
1742 public Arguments(Uri uri, String where, String[] whereArgs, boolean supportAll) {
1743 final int segmentSize = uri.getPathSegments().size();
1744 switch (segmentSize) {
1745 case 1: {
1746 if (where != null
1747 && (WHERE_PATTERN_WITH_PARAM_NO_BRACKETS.matcher(where).matches()
1748 || WHERE_PATTERN_WITH_PARAM_IN_BRACKETS.matcher(where).matches())
1749 && whereArgs.length == 1) {
1750 name = whereArgs[0];
1751 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08001752 return;
Svetoslav683914b2015-01-15 14:22:26 -08001753 } else if (where != null
1754 && (WHERE_PATTERN_NO_PARAM_NO_BRACKETS.matcher(where).matches()
1755 || WHERE_PATTERN_NO_PARAM_IN_BRACKETS.matcher(where).matches())) {
1756 final int startIndex = Math.max(where.indexOf("'"),
1757 where.indexOf("\"")) + 1;
1758 final int endIndex = Math.max(where.lastIndexOf("'"),
1759 where.lastIndexOf("\""));
1760 name = where.substring(startIndex, endIndex);
1761 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08001762 return;
Svetoslav683914b2015-01-15 14:22:26 -08001763 } else if (supportAll && where == null && whereArgs == null) {
1764 name = null;
1765 table = computeTableForSetting(uri, null);
Svetoslav28494652015-02-12 14:11:42 -08001766 return;
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001767 }
Svetoslav683914b2015-01-15 14:22:26 -08001768 } break;
1769
Svetoslav28494652015-02-12 14:11:42 -08001770 case 2: {
1771 if (where == null && whereArgs == null) {
1772 name = uri.getPathSegments().get(1);
1773 table = computeTableForSetting(uri, name);
1774 return;
1775 }
1776 } break;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001777 }
Svetoslav28494652015-02-12 14:11:42 -08001778
1779 EventLogTags.writeUnsupportedSettingsQuery(
1780 uri.toSafeString(), where, Arrays.toString(whereArgs));
1781 String message = String.format( "Supported SQL:\n"
1782 + " uri content://some_table/some_property with null where and where args\n"
1783 + " uri content://some_table with query name=? and single name as arg\n"
1784 + " uri content://some_table with query name=some_name and null args\n"
1785 + " but got - uri:%1s, where:%2s whereArgs:%3s", uri, where,
1786 Arrays.toString(whereArgs));
1787 throw new IllegalArgumentException(message);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001788 }
1789
Svetoslav28494652015-02-12 14:11:42 -08001790 private static String computeTableForSetting(Uri uri, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08001791 String table = getValidTableOrThrow(uri);
1792
1793 if (name != null) {
1794 if (sSystemMovedToSecureSettings.contains(name)) {
1795 table = TABLE_SECURE;
1796 }
1797
1798 if (sSystemMovedToGlobalSettings.contains(name)) {
1799 table = TABLE_GLOBAL;
1800 }
1801
1802 if (sSecureMovedToGlobalSettings.contains(name)) {
1803 table = TABLE_GLOBAL;
1804 }
1805
1806 if (sGlobalMovedToSecureSettings.contains(name)) {
1807 table = TABLE_SECURE;
1808 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001809 }
Svetoslav683914b2015-01-15 14:22:26 -08001810
1811 return table;
1812 }
1813 }
1814
1815 final class SettingsRegistry {
1816 private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid";
1817
Svetoslav683914b2015-01-15 14:22:26 -08001818 private static final String SETTINGS_FILE_GLOBAL = "settings_global.xml";
1819 private static final String SETTINGS_FILE_SYSTEM = "settings_system.xml";
1820 private static final String SETTINGS_FILE_SECURE = "settings_secure.xml";
1821
1822 private final SparseArray<SettingsState> mSettingsStates = new SparseArray<>();
1823
Svet Ganov53a441c2016-04-19 19:38:00 -07001824 private GenerationRegistry mGenerationRegistry;
Svetoslav683914b2015-01-15 14:22:26 -08001825
Svetoslav7e0683b2015-08-03 16:02:52 -07001826 private final Handler mHandler;
1827
Svet Ganov53a441c2016-04-19 19:38:00 -07001828 private final BackupManager mBackupManager;
1829
Svetoslav683914b2015-01-15 14:22:26 -08001830 public SettingsRegistry() {
Svetoslav7e0683b2015-08-03 16:02:52 -07001831 mHandler = new MyHandler(getContext().getMainLooper());
Svet Ganov53a441c2016-04-19 19:38:00 -07001832 mGenerationRegistry = new GenerationRegistry(mLock);
1833 mBackupManager = new BackupManager(getContext());
Svetoslav683914b2015-01-15 14:22:26 -08001834 migrateAllLegacySettingsIfNeeded();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001835 }
1836
Svetoslav683914b2015-01-15 14:22:26 -08001837 public List<String> getSettingsNamesLocked(int type, int userId) {
1838 final int key = makeKey(type, userId);
1839 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001840 if (settingsState == null) {
1841 return new ArrayList<String>();
1842 }
Svetoslav683914b2015-01-15 14:22:26 -08001843 return settingsState.getSettingNamesLocked();
1844 }
1845
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001846 public SparseBooleanArray getKnownUsersLocked() {
1847 SparseBooleanArray users = new SparseBooleanArray();
1848 for (int i = mSettingsStates.size()-1; i >= 0; i--) {
1849 users.put(getUserIdFromKey(mSettingsStates.keyAt(i)), true);
1850 }
1851 return users;
1852 }
1853
Svetoslav683914b2015-01-15 14:22:26 -08001854 public SettingsState getSettingsLocked(int type, int userId) {
1855 final int key = makeKey(type, userId);
1856 return peekSettingsStateLocked(key);
1857 }
1858
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001859 public boolean ensureSettingsForUserLocked(int userId) {
1860 // First make sure this user actually exists.
1861 if (mUserManager.getUserInfo(userId) == null) {
1862 Slog.wtf(LOG_TAG, "Requested user " + userId + " does not exist");
1863 return false;
1864 }
1865
Svetoslav683914b2015-01-15 14:22:26 -08001866 // Migrate the setting for this user if needed.
1867 migrateLegacySettingsForUserIfNeededLocked(userId);
1868
1869 // Ensure global settings loaded if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001870 if (userId == UserHandle.USER_SYSTEM) {
1871 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001872 ensureSettingsStateLocked(globalKey);
1873 }
1874
1875 // Ensure secure settings loaded.
1876 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1877 ensureSettingsStateLocked(secureKey);
1878
1879 // Make sure the secure settings have an Android id set.
1880 SettingsState secureSettings = getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
1881 ensureSecureSettingAndroidIdSetLocked(secureSettings);
1882
1883 // Ensure system settings loaded.
1884 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1885 ensureSettingsStateLocked(systemKey);
1886
1887 // Upgrade the settings to the latest version.
1888 UpgradeController upgrader = new UpgradeController(userId);
1889 upgrader.upgradeIfNeededLocked();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001890 return true;
Svetoslav683914b2015-01-15 14:22:26 -08001891 }
1892
1893 private void ensureSettingsStateLocked(int key) {
1894 if (mSettingsStates.get(key) == null) {
1895 final int maxBytesPerPackage = getMaxBytesPerPackageForType(getTypeFromKey(key));
Svetoslav Ganove080da92016-12-21 17:10:35 -08001896 SettingsState settingsState = new SettingsState(getContext(), mLock,
1897 getSettingsFile(key), key, maxBytesPerPackage, mHandlerThread.getLooper());
Svetoslav683914b2015-01-15 14:22:26 -08001898 mSettingsStates.put(key, settingsState);
1899 }
1900 }
1901
1902 public void removeUserStateLocked(int userId, boolean permanently) {
1903 // We always keep the global settings in memory.
1904
1905 // Nuke system settings.
1906 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1907 final SettingsState systemSettingsState = mSettingsStates.get(systemKey);
1908 if (systemSettingsState != null) {
1909 if (permanently) {
1910 mSettingsStates.remove(systemKey);
1911 systemSettingsState.destroyLocked(null);
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001912 } else {
Svetoslav683914b2015-01-15 14:22:26 -08001913 systemSettingsState.destroyLocked(new Runnable() {
1914 @Override
1915 public void run() {
1916 mSettingsStates.remove(systemKey);
1917 }
1918 });
1919 }
1920 }
1921
1922 // Nuke secure settings.
1923 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1924 final SettingsState secureSettingsState = mSettingsStates.get(secureKey);
1925 if (secureSettingsState != null) {
1926 if (permanently) {
1927 mSettingsStates.remove(secureKey);
1928 secureSettingsState.destroyLocked(null);
1929 } else {
1930 secureSettingsState.destroyLocked(new Runnable() {
1931 @Override
1932 public void run() {
1933 mSettingsStates.remove(secureKey);
1934 }
1935 });
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001936 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001937 }
Svet Ganov53a441c2016-04-19 19:38:00 -07001938
1939 // Nuke generation tracking data
1940 mGenerationRegistry.onUserRemoved(userId);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001941 }
1942
Svetoslav683914b2015-01-15 14:22:26 -08001943 public boolean insertSettingLocked(int type, int userId, String name, String value,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001944 String tag, boolean makeDefault, String packageName, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001945 final int key = makeKey(type, userId);
1946
Svetoslav Ganove080da92016-12-21 17:10:35 -08001947 boolean success = false;
Svetoslav683914b2015-01-15 14:22:26 -08001948 SettingsState settingsState = peekSettingsStateLocked(key);
Svetoslav Ganove080da92016-12-21 17:10:35 -08001949 if (settingsState != null) {
1950 success = settingsState.insertSettingLocked(name, value,
1951 tag, makeDefault, packageName);
1952 }
Svetoslav683914b2015-01-15 14:22:26 -08001953
Svet Ganov53a441c2016-04-19 19:38:00 -07001954 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08001955 notifyForSettingsChange(key, name);
1956 }
1957 return success;
1958 }
1959
Svet Ganov53a441c2016-04-19 19:38:00 -07001960 public boolean deleteSettingLocked(int type, int userId, String name, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001961 final int key = makeKey(type, userId);
1962
Svetoslav Ganove080da92016-12-21 17:10:35 -08001963 boolean success = false;
Svetoslav683914b2015-01-15 14:22:26 -08001964 SettingsState settingsState = peekSettingsStateLocked(key);
Svetoslav Ganove080da92016-12-21 17:10:35 -08001965 if (settingsState != null) {
1966 success = settingsState.deleteSettingLocked(name);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001967 }
Svetoslav683914b2015-01-15 14:22:26 -08001968
Svet Ganov53a441c2016-04-19 19:38:00 -07001969 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08001970 notifyForSettingsChange(key, name);
1971 }
1972 return success;
1973 }
1974
1975 public Setting getSettingLocked(int type, int userId, String name) {
1976 final int key = makeKey(type, userId);
1977
1978 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001979 if (settingsState == null) {
1980 return null;
1981 }
Svetoslav683914b2015-01-15 14:22:26 -08001982 return settingsState.getSettingLocked(name);
1983 }
1984
1985 public boolean updateSettingLocked(int type, int userId, String name, String value,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001986 String tag, boolean makeDefault, String packageName, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001987 final int key = makeKey(type, userId);
1988
Svetoslav Ganove080da92016-12-21 17:10:35 -08001989 boolean success = false;
Svetoslav683914b2015-01-15 14:22:26 -08001990 SettingsState settingsState = peekSettingsStateLocked(key);
Svetoslav Ganove080da92016-12-21 17:10:35 -08001991 if (settingsState != null) {
1992 success = settingsState.updateSettingLocked(name, value, tag,
1993 makeDefault, packageName);
1994 }
Svetoslav683914b2015-01-15 14:22:26 -08001995
Svet Ganov53a441c2016-04-19 19:38:00 -07001996 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08001997 notifyForSettingsChange(key, name);
1998 }
1999
2000 return success;
2001 }
2002
Svetoslav Ganove080da92016-12-21 17:10:35 -08002003 public void resetSettingsLocked(int type, int userId, String packageName, int mode,
2004 String tag) {
2005 final int key = makeKey(type, userId);
2006 SettingsState settingsState = peekSettingsStateLocked(key);
2007 if (settingsState == null) {
2008 return;
2009 }
2010
2011 switch (mode) {
2012 case Settings.RESET_MODE_PACKAGE_DEFAULTS: {
2013 for (String name : settingsState.getSettingNamesLocked()) {
2014 Setting setting = settingsState.getSettingLocked(name);
2015 if (packageName.equals(setting.getPackageName())) {
2016 if (tag != null && !tag.equals(setting.getTag())) {
2017 continue;
2018 }
2019 if (settingsState.resetSettingLocked(name, packageName)) {
2020 notifyForSettingsChange(key, name);
2021 }
2022 }
2023 }
2024 } break;
2025
2026 case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: {
2027 for (String name : settingsState.getSettingNamesLocked()) {
2028 Setting setting = settingsState.getSettingLocked(name);
2029 if (!SettingsState.isSystemPackage(getContext(),
2030 setting.getPackageName())) {
2031 if (settingsState.resetSettingLocked(name, packageName)) {
2032 notifyForSettingsChange(key, name);
2033 }
2034 }
2035 }
2036 } break;
2037
2038 case Settings.RESET_MODE_UNTRUSTED_CHANGES: {
2039 for (String name : settingsState.getSettingNamesLocked()) {
2040 Setting setting = settingsState.getSettingLocked(name);
2041 if (!SettingsState.isSystemPackage(getContext(),
2042 setting.getPackageName())) {
2043 if (setting.isDefaultSystemSet()) {
2044 if (settingsState.resetSettingLocked(name, packageName)) {
2045 notifyForSettingsChange(key, name);
2046 }
2047 } else if (settingsState.deleteSettingLocked(name)) {
2048 notifyForSettingsChange(key, name);
2049 }
2050 }
2051 }
2052 } break;
2053
2054 case Settings.RESET_MODE_TRUSTED_DEFAULTS: {
2055 for (String name : settingsState.getSettingNamesLocked()) {
2056 Setting setting = settingsState.getSettingLocked(name);
2057 if (setting.isDefaultSystemSet()) {
2058 if (settingsState.resetSettingLocked(name, packageName)) {
2059 notifyForSettingsChange(key, name);
2060 }
2061 } else if (settingsState.deleteSettingLocked(name)) {
2062 notifyForSettingsChange(key, name);
2063 }
2064 }
2065 } break;
2066 }
2067 }
2068
Svetoslav683914b2015-01-15 14:22:26 -08002069 public void onPackageRemovedLocked(String packageName, int userId) {
Svet Ganov8de34802015-04-27 09:33:40 -07002070 // Global and secure settings are signature protected. Apps signed
2071 // by the platform certificate are generally not uninstalled and
2072 // the main exception is tests. We trust components signed
2073 // by the platform certificate and do not do a clean up after them.
Svetoslav683914b2015-01-15 14:22:26 -08002074
2075 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2076 SettingsState systemSettings = mSettingsStates.get(systemKey);
Svet Ganov8de34802015-04-27 09:33:40 -07002077 if (systemSettings != null) {
2078 systemSettings.onPackageRemovedLocked(packageName);
2079 }
Svetoslav683914b2015-01-15 14:22:26 -08002080 }
2081
2082 private SettingsState peekSettingsStateLocked(int key) {
2083 SettingsState settingsState = mSettingsStates.get(key);
2084 if (settingsState != null) {
2085 return settingsState;
2086 }
2087
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002088 if (!ensureSettingsForUserLocked(getUserIdFromKey(key))) {
2089 return null;
2090 }
Svetoslav683914b2015-01-15 14:22:26 -08002091 return mSettingsStates.get(key);
2092 }
2093
2094 private void migrateAllLegacySettingsIfNeeded() {
2095 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07002096 final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002097 File globalFile = getSettingsFile(key);
2098 if (globalFile.exists()) {
2099 return;
2100 }
2101
2102 final long identity = Binder.clearCallingIdentity();
2103 try {
2104 List<UserInfo> users = mUserManager.getUsers(true);
2105
2106 final int userCount = users.size();
2107 for (int i = 0; i < userCount; i++) {
2108 final int userId = users.get(i).id;
2109
2110 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
2111 SQLiteDatabase database = dbHelper.getWritableDatabase();
2112 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
2113
2114 // Upgrade to the latest version.
2115 UpgradeController upgrader = new UpgradeController(userId);
2116 upgrader.upgradeIfNeededLocked();
2117
2118 // Drop from memory if not a running user.
2119 if (!mUserManager.isUserRunning(new UserHandle(userId))) {
2120 removeUserStateLocked(userId, false);
2121 }
2122 }
2123 } finally {
2124 Binder.restoreCallingIdentity(identity);
2125 }
2126 }
2127 }
2128
2129 private void migrateLegacySettingsForUserIfNeededLocked(int userId) {
2130 // Every user has secure settings and if no file we need to migrate.
2131 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2132 File secureFile = getSettingsFile(secureKey);
2133 if (secureFile.exists()) {
2134 return;
2135 }
2136
2137 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
2138 SQLiteDatabase database = dbHelper.getWritableDatabase();
2139
2140 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
2141 }
2142
2143 private void migrateLegacySettingsForUserLocked(DatabaseHelper dbHelper,
2144 SQLiteDatabase database, int userId) {
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002145 // Move over the system settings.
2146 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2147 ensureSettingsStateLocked(systemKey);
2148 SettingsState systemSettings = mSettingsStates.get(systemKey);
2149 migrateLegacySettingsLocked(systemSettings, database, TABLE_SYSTEM);
2150 systemSettings.persistSyncLocked();
Svetoslav683914b2015-01-15 14:22:26 -08002151
2152 // Move over the secure settings.
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002153 // Do this after System settings, since this is the first thing we check when deciding
2154 // to skip over migration from db to xml for a secondary user.
Svetoslav683914b2015-01-15 14:22:26 -08002155 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2156 ensureSettingsStateLocked(secureKey);
2157 SettingsState secureSettings = mSettingsStates.get(secureKey);
2158 migrateLegacySettingsLocked(secureSettings, database, TABLE_SECURE);
2159 ensureSecureSettingAndroidIdSetLocked(secureSettings);
2160 secureSettings.persistSyncLocked();
2161
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002162 // Move over the global settings if owner.
2163 // Do this last, since this is the first thing we check when deciding
2164 // to skip over migration from db to xml for owner user.
2165 if (userId == UserHandle.USER_SYSTEM) {
2166 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, userId);
2167 ensureSettingsStateLocked(globalKey);
2168 SettingsState globalSettings = mSettingsStates.get(globalKey);
2169 migrateLegacySettingsLocked(globalSettings, database, TABLE_GLOBAL);
2170 globalSettings.persistSyncLocked();
2171 }
Svetoslav683914b2015-01-15 14:22:26 -08002172
2173 // Drop the database as now all is moved and persisted.
2174 if (DROP_DATABASE_ON_MIGRATION) {
2175 dbHelper.dropDatabase();
2176 } else {
2177 dbHelper.backupDatabase();
2178 }
2179 }
2180
2181 private void migrateLegacySettingsLocked(SettingsState settingsState,
2182 SQLiteDatabase database, String table) {
2183 SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
2184 queryBuilder.setTables(table);
2185
2186 Cursor cursor = queryBuilder.query(database, ALL_COLUMNS,
2187 null, null, null, null, null);
2188
2189 if (cursor == null) {
2190 return;
2191 }
2192
2193 try {
2194 if (!cursor.moveToFirst()) {
2195 return;
2196 }
2197
2198 final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME);
2199 final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE);
2200
2201 settingsState.setVersionLocked(database.getVersion());
2202
2203 while (!cursor.isAfterLast()) {
2204 String name = cursor.getString(nameColumnIdx);
2205 String value = cursor.getString(valueColumnIdx);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002206 settingsState.insertSettingLocked(name, value, null, true,
Svetoslav683914b2015-01-15 14:22:26 -08002207 SettingsState.SYSTEM_PACKAGE_NAME);
2208 cursor.moveToNext();
2209 }
2210 } finally {
2211 cursor.close();
2212 }
2213 }
2214
2215 private void ensureSecureSettingAndroidIdSetLocked(SettingsState secureSettings) {
2216 Setting value = secureSettings.getSettingLocked(Settings.Secure.ANDROID_ID);
2217
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09002218 if (!value.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08002219 return;
2220 }
2221
2222 final int userId = getUserIdFromKey(secureSettings.mKey);
2223
2224 final UserInfo user;
2225 final long identity = Binder.clearCallingIdentity();
2226 try {
2227 user = mUserManager.getUserInfo(userId);
2228 } finally {
2229 Binder.restoreCallingIdentity(identity);
2230 }
2231 if (user == null) {
2232 // Can happen due to races when deleting users - treat as benign.
2233 return;
2234 }
2235
2236 String androidId = Long.toHexString(new SecureRandom().nextLong());
2237 secureSettings.insertSettingLocked(Settings.Secure.ANDROID_ID, androidId,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002238 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Svetoslav683914b2015-01-15 14:22:26 -08002239
2240 Slog.d(LOG_TAG, "Generated and saved new ANDROID_ID [" + androidId
2241 + "] for user " + userId);
2242
2243 // Write a drop box entry if it's a restricted profile
2244 if (user.isRestricted()) {
2245 DropBoxManager dbm = (DropBoxManager) getContext().getSystemService(
2246 Context.DROPBOX_SERVICE);
2247 if (dbm != null && dbm.isTagEnabled(DROPBOX_TAG_USERLOG)) {
2248 dbm.addText(DROPBOX_TAG_USERLOG, System.currentTimeMillis()
2249 + "," + DROPBOX_TAG_USERLOG + "," + androidId + "\n");
2250 }
2251 }
2252 }
2253
2254 private void notifyForSettingsChange(int key, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08002255 final int userId = getUserIdFromKey(key);
2256 Uri uri = getNotificationUriFor(key, name);
2257
Phil Weaver83fec002016-05-11 10:55:29 -07002258 mGenerationRegistry.incrementGeneration(key);
2259
Svetoslav7e0683b2015-08-03 16:02:52 -07002260 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
2261 userId, 0, uri).sendToTarget();
2262
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002263 if (isSecureSettingsKey(key)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07002264 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
2265 sSecureCloneToManagedSettings);
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002266 } else if (isSystemSettingsKey(key)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07002267 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
2268 sSystemCloneToManagedSettings);
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002269 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002270
Svet Ganov53a441c2016-04-19 19:38:00 -07002271 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002272 }
2273
Svet Ganov53a441c2016-04-19 19:38:00 -07002274 private void maybeNotifyProfiles(int type, int userId, Uri uri, String name,
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002275 Set<String> keysCloned) {
2276 if (keysCloned.contains(name)) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002277 for (int profileId : mUserManager.getProfileIdsWithDisabled(userId)) {
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002278 // the notification for userId has already been sent.
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002279 if (profileId != userId) {
Svetoslav7e0683b2015-08-03 16:02:52 -07002280 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002281 profileId, 0, uri).sendToTarget();
Svet Ganov53a441c2016-04-19 19:38:00 -07002282 final int key = makeKey(type, profileId);
2283 mGenerationRegistry.incrementGeneration(key);
2284
2285 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002286 }
2287 }
2288 }
Svetoslav683914b2015-01-15 14:22:26 -08002289 }
2290
Svetoslav683914b2015-01-15 14:22:26 -08002291 private boolean isGlobalSettingsKey(int key) {
2292 return getTypeFromKey(key) == SETTINGS_TYPE_GLOBAL;
2293 }
2294
2295 private boolean isSystemSettingsKey(int key) {
2296 return getTypeFromKey(key) == SETTINGS_TYPE_SYSTEM;
2297 }
2298
2299 private boolean isSecureSettingsKey(int key) {
2300 return getTypeFromKey(key) == SETTINGS_TYPE_SECURE;
2301 }
2302
2303 private File getSettingsFile(int key) {
2304 if (isGlobalSettingsKey(key)) {
2305 final int userId = getUserIdFromKey(key);
2306 return new File(Environment.getUserSystemDirectory(userId),
2307 SETTINGS_FILE_GLOBAL);
2308 } else if (isSystemSettingsKey(key)) {
2309 final int userId = getUserIdFromKey(key);
2310 return new File(Environment.getUserSystemDirectory(userId),
2311 SETTINGS_FILE_SYSTEM);
2312 } else if (isSecureSettingsKey(key)) {
2313 final int userId = getUserIdFromKey(key);
2314 return new File(Environment.getUserSystemDirectory(userId),
2315 SETTINGS_FILE_SECURE);
2316 } else {
2317 throw new IllegalArgumentException("Invalid settings key:" + key);
2318 }
2319 }
2320
2321 private Uri getNotificationUriFor(int key, String name) {
2322 if (isGlobalSettingsKey(key)) {
2323 return (name != null) ? Uri.withAppendedPath(Settings.Global.CONTENT_URI, name)
2324 : Settings.Global.CONTENT_URI;
2325 } else if (isSecureSettingsKey(key)) {
2326 return (name != null) ? Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name)
2327 : Settings.Secure.CONTENT_URI;
2328 } else if (isSystemSettingsKey(key)) {
2329 return (name != null) ? Uri.withAppendedPath(Settings.System.CONTENT_URI, name)
2330 : Settings.System.CONTENT_URI;
2331 } else {
2332 throw new IllegalArgumentException("Invalid settings key:" + key);
2333 }
2334 }
2335
2336 private int getMaxBytesPerPackageForType(int type) {
2337 switch (type) {
2338 case SETTINGS_TYPE_GLOBAL:
2339 case SETTINGS_TYPE_SECURE: {
2340 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED;
2341 }
2342
2343 default: {
2344 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_LIMITED;
2345 }
2346 }
2347 }
2348
Svetoslav7e0683b2015-08-03 16:02:52 -07002349 private final class MyHandler extends Handler {
2350 private static final int MSG_NOTIFY_URI_CHANGED = 1;
2351 private static final int MSG_NOTIFY_DATA_CHANGED = 2;
2352
2353 public MyHandler(Looper looper) {
2354 super(looper);
2355 }
2356
2357 @Override
2358 public void handleMessage(Message msg) {
2359 switch (msg.what) {
2360 case MSG_NOTIFY_URI_CHANGED: {
2361 final int userId = msg.arg1;
2362 Uri uri = (Uri) msg.obj;
2363 getContext().getContentResolver().notifyChange(uri, null, true, userId);
2364 if (DEBUG) {
2365 Slog.v(LOG_TAG, "Notifying for " + userId + ": " + uri);
2366 }
2367 } break;
2368
2369 case MSG_NOTIFY_DATA_CHANGED: {
2370 mBackupManager.dataChanged();
2371 } break;
2372 }
2373 }
2374 }
2375
Svetoslav683914b2015-01-15 14:22:26 -08002376 private final class UpgradeController {
Jeremy Joslin8bdad342016-12-14 11:46:47 -08002377 private static final int SETTINGS_VERSION = 136;
Svetoslav683914b2015-01-15 14:22:26 -08002378
2379 private final int mUserId;
2380
2381 public UpgradeController(int userId) {
2382 mUserId = userId;
2383 }
2384
2385 public void upgradeIfNeededLocked() {
2386 // The version of all settings for a user is the same (all users have secure).
2387 SettingsState secureSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07002388 SETTINGS_TYPE_SECURE, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08002389
2390 // Try an update from the current state.
2391 final int oldVersion = secureSettings.getVersionLocked();
2392 final int newVersion = SETTINGS_VERSION;
2393
Svet Ganovc9755bc2015-03-28 13:21:22 -07002394 // If up do date - done.
Svetoslav683914b2015-01-15 14:22:26 -08002395 if (oldVersion == newVersion) {
2396 return;
2397 }
2398
2399 // Try to upgrade.
2400 final int curVersion = onUpgradeLocked(mUserId, oldVersion, newVersion);
2401
2402 // If upgrade failed start from scratch and upgrade.
2403 if (curVersion != newVersion) {
2404 // Drop state we have for this user.
2405 removeUserStateLocked(mUserId, true);
2406
2407 // Recreate the database.
2408 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), mUserId);
2409 SQLiteDatabase database = dbHelper.getWritableDatabase();
2410 dbHelper.recreateDatabase(database, newVersion, curVersion, oldVersion);
2411
2412 // Migrate the settings for this user.
2413 migrateLegacySettingsForUserLocked(dbHelper, database, mUserId);
2414
2415 // Now upgrade should work fine.
2416 onUpgradeLocked(mUserId, oldVersion, newVersion);
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07002417
2418 // Make a note what happened, so we don't wonder why data was lost
2419 String reason = "Settings rebuilt! Current version: "
2420 + curVersion + " while expected: " + newVersion;
2421 getGlobalSettingsLocked().insertSettingLocked(
Svetoslav Ganove080da92016-12-21 17:10:35 -08002422 Settings.Global.DATABASE_DOWNGRADE_REASON,
2423 reason, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Svetoslav683914b2015-01-15 14:22:26 -08002424 }
2425
2426 // Set the global settings version if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07002427 if (mUserId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08002428 SettingsState globalSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07002429 SETTINGS_TYPE_GLOBAL, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08002430 globalSettings.setVersionLocked(newVersion);
2431 }
2432
2433 // Set the secure settings version.
2434 secureSettings.setVersionLocked(newVersion);
2435
2436 // Set the system settings version.
2437 SettingsState systemSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07002438 SETTINGS_TYPE_SYSTEM, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08002439 systemSettings.setVersionLocked(newVersion);
2440 }
2441
2442 private SettingsState getGlobalSettingsLocked() {
Xiaohui Chen43765b72015-08-31 10:57:33 -07002443 return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002444 }
2445
2446 private SettingsState getSecureSettingsLocked(int userId) {
2447 return getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
2448 }
2449
2450 private SettingsState getSystemSettingsLocked(int userId) {
2451 return getSettingsLocked(SETTINGS_TYPE_SYSTEM, userId);
2452 }
2453
Jeff Brown503cffc2015-03-26 18:08:51 -07002454 /**
2455 * You must perform all necessary mutations to bring the settings
2456 * for this user from the old to the new version. When you add a new
2457 * upgrade step you *must* update SETTINGS_VERSION.
2458 *
2459 * This is an example of moving a setting from secure to global.
2460 *
2461 * // v119: Example settings changes.
2462 * if (currentVersion == 118) {
2463 * if (userId == UserHandle.USER_OWNER) {
2464 * // Remove from the secure settings.
2465 * SettingsState secureSettings = getSecureSettingsLocked(userId);
2466 * String name = "example_setting_to_move";
2467 * String value = secureSettings.getSetting(name);
2468 * secureSettings.deleteSetting(name);
2469 *
2470 * // Add to the global settings.
2471 * SettingsState globalSettings = getGlobalSettingsLocked();
2472 * globalSettings.insertSetting(name, value, SettingsState.SYSTEM_PACKAGE_NAME);
2473 * }
2474 *
2475 * // Update the current version.
2476 * currentVersion = 119;
2477 * }
2478 */
Svetoslav683914b2015-01-15 14:22:26 -08002479 private int onUpgradeLocked(int userId, int oldVersion, int newVersion) {
2480 if (DEBUG) {
2481 Slog.w(LOG_TAG, "Upgrading settings for user: " + userId + " from version: "
2482 + oldVersion + " to version: " + newVersion);
2483 }
2484
Jeff Brown503cffc2015-03-26 18:08:51 -07002485 int currentVersion = oldVersion;
Svetoslav683914b2015-01-15 14:22:26 -08002486
John Spurlocke11ae112015-05-11 16:09:03 -04002487 // v119: Reset zen + ringer mode.
2488 if (currentVersion == 118) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07002489 if (userId == UserHandle.USER_SYSTEM) {
John Spurlocke11ae112015-05-11 16:09:03 -04002490 final SettingsState globalSettings = getGlobalSettingsLocked();
2491 globalSettings.updateSettingLocked(Settings.Global.ZEN_MODE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002492 Integer.toString(Settings.Global.ZEN_MODE_OFF), null,
2493 true, SettingsState.SYSTEM_PACKAGE_NAME);
John Spurlocke11ae112015-05-11 16:09:03 -04002494 globalSettings.updateSettingLocked(Settings.Global.MODE_RINGER,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002495 Integer.toString(AudioManager.RINGER_MODE_NORMAL), null,
2496 true, SettingsState.SYSTEM_PACKAGE_NAME);
John Spurlocke11ae112015-05-11 16:09:03 -04002497 }
2498 currentVersion = 119;
2499 }
2500
Jason Monk27bbb2d2015-03-31 16:46:39 -04002501 // v120: Add double tap to wake setting.
2502 if (currentVersion == 119) {
2503 SettingsState secureSettings = getSecureSettingsLocked(userId);
2504 secureSettings.insertSettingLocked(Settings.Secure.DOUBLE_TAP_TO_WAKE,
2505 getContext().getResources().getBoolean(
Svetoslav Ganove080da92016-12-21 17:10:35 -08002506 R.bool.def_double_tap_to_wake) ? "1" : "0", null, true,
Jason Monk27bbb2d2015-03-31 16:46:39 -04002507 SettingsState.SYSTEM_PACKAGE_NAME);
2508
2509 currentVersion = 120;
2510 }
2511
Svetoslav7e0683b2015-08-03 16:02:52 -07002512 if (currentVersion == 120) {
2513 // Before 121, we used a different string encoding logic. We just bump the
2514 // version here; SettingsState knows how to handle pre-version 120 files.
2515 currentVersion = 121;
2516 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -07002517
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02002518 if (currentVersion == 121) {
2519 // Version 122: allow OEMs to set a default payment component in resources.
2520 // Note that we only write the default if no default has been set;
2521 // if there is, we just leave the default at whatever it currently is.
2522 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2523 String defaultComponent = (getContext().getResources().getString(
2524 R.string.def_nfc_payment_component));
2525 Setting currentSetting = secureSettings.getSettingLocked(
2526 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT);
2527 if (defaultComponent != null && !defaultComponent.isEmpty() &&
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002528 currentSetting.isNull()) {
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02002529 secureSettings.insertSettingLocked(
2530 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002531 defaultComponent, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02002532 }
2533 currentVersion = 122;
2534 }
Suprabh Shukla269c11e2015-12-02 16:51:16 -08002535
2536 if (currentVersion == 122) {
2537 // Version 123: Adding a default value for the ability to add a user from
2538 // the lock screen.
2539 if (userId == UserHandle.USER_SYSTEM) {
2540 final SettingsState globalSettings = getGlobalSettingsLocked();
2541 Setting currentSetting = globalSettings.getSettingLocked(
2542 Settings.Global.ADD_USERS_WHEN_LOCKED);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002543 if (currentSetting.isNull()) {
Suprabh Shukla269c11e2015-12-02 16:51:16 -08002544 globalSettings.insertSettingLocked(
2545 Settings.Global.ADD_USERS_WHEN_LOCKED,
2546 getContext().getResources().getBoolean(
2547 R.bool.def_add_users_from_lockscreen) ? "1" : "0",
Svetoslav Ganove080da92016-12-21 17:10:35 -08002548 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shukla269c11e2015-12-02 16:51:16 -08002549 }
2550 }
2551 currentVersion = 123;
2552 }
Bryce Leebd179282015-12-17 19:01:37 -08002553
2554 if (currentVersion == 123) {
Bryce Leeec85f342015-12-16 13:32:28 -08002555 final SettingsState globalSettings = getGlobalSettingsLocked();
2556 String defaultDisabledProfiles = (getContext().getResources().getString(
2557 R.string.def_bluetooth_disabled_profiles));
2558 globalSettings.insertSettingLocked(Settings.Global.BLUETOOTH_DISABLED_PROFILES,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002559 defaultDisabledProfiles, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Bryce Leebd179282015-12-17 19:01:37 -08002560 currentVersion = 124;
Bryce Leeec85f342015-12-16 13:32:28 -08002561 }
2562
Prathmesh Prabhude16b862016-03-04 15:22:24 -08002563 if (currentVersion == 124) {
2564 // Version 124: allow OEMs to set a default value for whether IME should be
2565 // shown when a physical keyboard is connected.
2566 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2567 Setting currentSetting = secureSettings.getSettingLocked(
2568 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002569 if (currentSetting.isNull()) {
Prathmesh Prabhude16b862016-03-04 15:22:24 -08002570 secureSettings.insertSettingLocked(
2571 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD,
2572 getContext().getResources().getBoolean(
2573 R.bool.def_show_ime_with_hard_keyboard) ? "1" : "0",
Svetoslav Ganove080da92016-12-21 17:10:35 -08002574 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Prathmesh Prabhude16b862016-03-04 15:22:24 -08002575 }
2576 currentVersion = 125;
2577 }
2578
Ruben Brunk98576cf2016-03-07 18:54:28 -08002579 if (currentVersion == 125) {
2580 // Version 125: Allow OEMs to set the default VR service.
2581 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2582
2583 Setting currentSetting = secureSettings.getSettingLocked(
2584 Settings.Secure.ENABLED_VR_LISTENERS);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002585 if (currentSetting.isNull()) {
Ruben Brunk98576cf2016-03-07 18:54:28 -08002586 ArraySet<ComponentName> l =
2587 SystemConfig.getInstance().getDefaultVrComponents();
2588
2589 if (l != null && !l.isEmpty()) {
2590 StringBuilder b = new StringBuilder();
2591 boolean start = true;
2592 for (ComponentName c : l) {
2593 if (!start) {
2594 b.append(':');
2595 }
2596 b.append(c.flattenToString());
2597 start = false;
2598 }
2599 secureSettings.insertSettingLocked(
2600 Settings.Secure.ENABLED_VR_LISTENERS, b.toString(),
Svetoslav Ganove080da92016-12-21 17:10:35 -08002601 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Ruben Brunk98576cf2016-03-07 18:54:28 -08002602 }
2603
2604 }
2605 currentVersion = 126;
2606 }
2607
Daniel U02ba6122016-04-01 18:41:42 +01002608 if (currentVersion == 126) {
2609 // Version 126: copy the primary values of LOCK_SCREEN_SHOW_NOTIFICATIONS and
2610 // LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS into managed profile.
2611 if (mUserManager.isManagedProfile(userId)) {
2612 final SettingsState systemSecureSettings =
2613 getSecureSettingsLocked(UserHandle.USER_SYSTEM);
2614
2615 final Setting showNotifications = systemSecureSettings.getSettingLocked(
2616 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09002617 if (!showNotifications.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01002618 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2619 secureSettings.insertSettingLocked(
2620 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002621 showNotifications.getValue(), null, true,
Daniel U02ba6122016-04-01 18:41:42 +01002622 SettingsState.SYSTEM_PACKAGE_NAME);
2623 }
2624
2625 final Setting allowPrivate = systemSecureSettings.getSettingLocked(
2626 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09002627 if (!allowPrivate.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01002628 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2629 secureSettings.insertSettingLocked(
2630 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002631 allowPrivate.getValue(), null, true,
Daniel U02ba6122016-04-01 18:41:42 +01002632 SettingsState.SYSTEM_PACKAGE_NAME);
2633 }
2634 }
2635 currentVersion = 127;
2636 }
2637
Steven Ngdc20ba62016-04-26 18:19:04 +01002638 if (currentVersion == 127) {
Mahaver Chopra3d9805d2016-07-07 16:25:05 +01002639 // version 127 is no longer used.
Steven Ngdc20ba62016-04-26 18:19:04 +01002640 currentVersion = 128;
2641 }
2642
Julia Reynolds1f721e12016-07-11 08:50:58 -04002643 if (currentVersion == 128) {
2644 // Version 128: Allow OEMs to grant DND access to default apps. Note that
2645 // the new apps are appended to the list of already approved apps.
2646 final SettingsState systemSecureSettings =
2647 getSecureSettingsLocked(userId);
2648
2649 final Setting policyAccess = systemSecureSettings.getSettingLocked(
2650 Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES);
2651 String defaultPolicyAccess = getContext().getResources().getString(
2652 com.android.internal.R.string.config_defaultDndAccessPackages);
2653 if (!TextUtils.isEmpty(defaultPolicyAccess)) {
2654 if (policyAccess.isNull()) {
2655 systemSecureSettings.insertSettingLocked(
2656 Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002657 defaultPolicyAccess, null, true,
Julia Reynolds1f721e12016-07-11 08:50:58 -04002658 SettingsState.SYSTEM_PACKAGE_NAME);
2659 } else {
2660 StringBuilder currentSetting =
2661 new StringBuilder(policyAccess.getValue());
2662 currentSetting.append(":");
2663 currentSetting.append(defaultPolicyAccess);
2664 systemSecureSettings.updateSettingLocked(
2665 Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002666 currentSetting.toString(), null, true,
Julia Reynolds1f721e12016-07-11 08:50:58 -04002667 SettingsState.SYSTEM_PACKAGE_NAME);
2668 }
2669 }
2670
2671 currentVersion = 129;
2672 }
2673
Dan Sandler71f85e92016-07-20 13:46:05 -04002674 if (currentVersion == 129) {
2675 // default longpress timeout changed from 500 to 400. If unchanged from the old
2676 // default, update to the new default.
2677 final SettingsState systemSecureSettings =
2678 getSecureSettingsLocked(userId);
2679 final String oldValue = systemSecureSettings.getSettingLocked(
2680 Settings.Secure.LONG_PRESS_TIMEOUT).getValue();
2681 if (TextUtils.equals("500", oldValue)) {
2682 systemSecureSettings.insertSettingLocked(
2683 Settings.Secure.LONG_PRESS_TIMEOUT,
2684 String.valueOf(getContext().getResources().getInteger(
2685 R.integer.def_long_press_timeout_millis)),
Svetoslav Ganove080da92016-12-21 17:10:35 -08002686 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Dan Sandler71f85e92016-07-20 13:46:05 -04002687 }
2688 currentVersion = 130;
2689 }
2690
Anthony Hugh96e9cc52016-07-12 15:17:24 -07002691 if (currentVersion == 130) {
Adrian Roos69741a22016-10-21 14:49:17 -07002692 // Split Ambient settings
2693 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2694 boolean dozeExplicitlyDisabled = "0".equals(secureSettings.
2695 getSettingLocked(Settings.Secure.DOZE_ENABLED).getValue());
2696
2697 if (dozeExplicitlyDisabled) {
2698 secureSettings.insertSettingLocked(Settings.Secure.DOZE_PULSE_ON_PICK_UP,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002699 "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Adrian Roos69741a22016-10-21 14:49:17 -07002700 secureSettings.insertSettingLocked(Settings.Secure.DOZE_PULSE_ON_DOUBLE_TAP,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002701 "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Adrian Roos69741a22016-10-21 14:49:17 -07002702 }
2703 currentVersion = 131;
2704 }
2705
2706 if (currentVersion == 131) {
Anthony Hugh96e9cc52016-07-12 15:17:24 -07002707 // Initialize new multi-press timeout to default value
2708 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
2709 final String oldValue = systemSecureSettings.getSettingLocked(
2710 Settings.Secure.MULTI_PRESS_TIMEOUT).getValue();
2711 if (TextUtils.equals(null, oldValue)) {
2712 systemSecureSettings.insertSettingLocked(
2713 Settings.Secure.MULTI_PRESS_TIMEOUT,
2714 String.valueOf(getContext().getResources().getInteger(
2715 R.integer.def_multi_press_timeout_millis)),
Svetoslav Ganove080da92016-12-21 17:10:35 -08002716 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Anthony Hugh96e9cc52016-07-12 15:17:24 -07002717 }
2718
Adrian Roos69741a22016-10-21 14:49:17 -07002719 currentVersion = 132;
Anthony Hugh96e9cc52016-07-12 15:17:24 -07002720 }
2721
Adrian Roos69741a22016-10-21 14:49:17 -07002722 if (currentVersion == 132) {
2723 // Version 132: Allow managed profile to optionally use the parent's ringtones
Andre Lagoea35e072016-08-04 13:41:13 +01002724 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
2725 String defaultSyncParentSounds = (getContext().getResources()
2726 .getBoolean(R.bool.def_sync_parent_sounds) ? "1" : "0");
2727 systemSecureSettings.insertSettingLocked(
Svetoslav Ganove080da92016-12-21 17:10:35 -08002728 Settings.Secure.SYNC_PARENT_SOUNDS, defaultSyncParentSounds,
2729 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Adrian Roos69741a22016-10-21 14:49:17 -07002730 currentVersion = 133;
Andre Lagoea35e072016-08-04 13:41:13 +01002731 }
2732
Adrian Roos69741a22016-10-21 14:49:17 -07002733 if (currentVersion == 133) {
2734 // Version 133: Add default end button behavior
Keun-young Parkec7a1182016-10-18 11:52:38 -07002735 final SettingsState systemSettings = getSystemSettingsLocked(userId);
2736 if (systemSettings.getSettingLocked(Settings.System.END_BUTTON_BEHAVIOR) ==
2737 null) {
2738 String defaultEndButtonBehavior = Integer.toString(getContext()
2739 .getResources().getInteger(R.integer.def_end_button_behavior));
2740 systemSettings.insertSettingLocked(Settings.System.END_BUTTON_BEHAVIOR,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002741 defaultEndButtonBehavior, null, true,
2742 SettingsState.SYSTEM_PACKAGE_NAME);
Keun-young Parkec7a1182016-10-18 11:52:38 -07002743 }
Adrian Roos69741a22016-10-21 14:49:17 -07002744 currentVersion = 134;
Keun-young Parkec7a1182016-10-18 11:52:38 -07002745 }
2746
Phil Weaver89e3ffc2016-09-19 13:51:10 -07002747 if (currentVersion == 134) {
2748 // Remove setting that specifies if magnification values should be preserved.
2749 // This setting defaulted to true and never has a UI.
2750 getSecureSettingsLocked(userId).deleteSettingLocked(
2751 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_AUTO_UPDATE);
2752 currentVersion = 135;
2753 }
2754
Jeremy Joslin8bdad342016-12-14 11:46:47 -08002755 if (currentVersion == 135) {
2756 // Version 135: Migrating the NETWORK_SCORER_APP setting to the
2757 // NETWORK_RECOMMENDATIONS_ENABLED setting.
2758 if (userId == UserHandle.USER_SYSTEM) {
2759 final SettingsState globalSettings = getGlobalSettingsLocked();
2760 Setting currentSetting = globalSettings.getSettingLocked(
2761 Global.NETWORK_SCORER_APP);
2762 if (!currentSetting.isNull()) {
2763 // A scorer was set so enable recommendations.
2764 globalSettings.insertSettingLocked(
2765 Global.NETWORK_RECOMMENDATIONS_ENABLED,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002766 "1", null, true,
Jeremy Joslin8bdad342016-12-14 11:46:47 -08002767 SettingsState.SYSTEM_PACKAGE_NAME);
2768
2769 // and clear the scorer setting since it's no longer needed.
2770 globalSettings.insertSettingLocked(
2771 Global.NETWORK_SCORER_APP,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002772 null, null, true,
Jeremy Joslin8bdad342016-12-14 11:46:47 -08002773 SettingsState.SYSTEM_PACKAGE_NAME);
2774 }
2775 }
2776 currentVersion = 136;
2777 }
2778
Dan Sandler71f85e92016-07-20 13:46:05 -04002779 if (currentVersion != newVersion) {
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07002780 Slog.wtf("SettingsProvider", "warning: upgrading settings database to version "
Dan Sandler71f85e92016-07-20 13:46:05 -04002781 + newVersion + " left it at "
2782 + currentVersion + " instead; this is probably a bug", new Throwable());
2783 if (DEBUG) {
2784 throw new RuntimeException("db upgrade error");
2785 }
2786 }
2787
Jeff Brown503cffc2015-03-26 18:08:51 -07002788 // vXXX: Add new settings above this point.
Svetoslav683914b2015-01-15 14:22:26 -08002789
Jeff Brown503cffc2015-03-26 18:08:51 -07002790 // Return the current version.
2791 return currentVersion;
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -08002792 }
2793 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002794 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07002795}