blob: 5ff9c0ce3c84701deae70a525d7b91ae42f47b33 [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;
Svet Ganov53a441c2016-04-19 19:38:00 -070045import android.os.Debug;
Amith Yamasani5cdf7f52013-06-27 15:12:01 -070046import android.os.DropBoxManager;
Svetoslav683914b2015-01-15 14:22:26 -080047import android.os.Environment;
Svetoslav7e0683b2015-08-03 16:02:52 -070048import android.os.Handler;
49import 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;
Christopher Tate06efb532012-08-24 15:29:27 -070055import android.os.UserHandle;
56import android.os.UserManager;
Svet Ganov53a441c2016-04-19 19:38:00 -070057import android.os.UserManagerInternal;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070058import android.provider.Settings;
59import android.text.TextUtils;
Svetoslav683914b2015-01-15 14:22:26 -080060import android.util.ArraySet;
Christopher Tate06efb532012-08-24 15:29:27 -070061import android.util.Slog;
62import android.util.SparseArray;
John Spurlocke11ae112015-05-11 16:09:03 -040063
Svetoslav683914b2015-01-15 14:22:26 -080064import com.android.internal.annotations.GuardedBy;
65import com.android.internal.content.PackageMonitor;
66import com.android.internal.os.BackgroundThread;
Suprabh Shukla269c11e2015-12-02 16:51:16 -080067import com.android.providers.settings.SettingsState.Setting;
Svet Ganov53a441c2016-04-19 19:38:00 -070068import com.android.server.LocalServices;
Ruben Brunk98576cf2016-03-07 18:54:28 -080069import com.android.server.SystemConfig;
John Spurlocke11ae112015-05-11 16:09:03 -040070
Svetoslav683914b2015-01-15 14:22:26 -080071import java.io.File;
Svetoslavb505ccc2015-02-17 12:41:04 -080072import java.io.FileDescriptor;
Svetoslav683914b2015-01-15 14:22:26 -080073import java.io.FileNotFoundException;
Svetoslavb505ccc2015-02-17 12:41:04 -080074import java.io.PrintWriter;
Svetoslav683914b2015-01-15 14:22:26 -080075import java.security.SecureRandom;
76import java.util.Arrays;
77import java.util.List;
Svetoslav683914b2015-01-15 14:22:26 -080078import java.util.Set;
79import java.util.regex.Pattern;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070080
Svetoslav683914b2015-01-15 14:22:26 -080081/**
82 * <p>
83 * This class is a content provider that publishes the system settings.
84 * It can be accessed via the content provider APIs or via custom call
85 * commands. The latter is a bit faster and is the preferred way to access
86 * the platform settings.
87 * </p>
88 * <p>
89 * There are three settings types, global (with signature level protection
90 * and shared across users), secure (with signature permission level
91 * protection and per user), and system (with dangerous permission level
92 * protection and per user). Global settings are stored under the device owner.
93 * Each of these settings is represented by a {@link
94 * com.android.providers.settings.SettingsState} object mapped to an integer
95 * key derived from the setting type in the most significant bits and user
96 * id in the least significant bits. Settings are synchronously loaded on
97 * instantiation of a SettingsState and asynchronously persisted on mutation.
98 * Settings are stored in the user specific system directory.
99 * </p>
100 * <p>
101 * Apps targeting APIs Lollipop MR1 and lower can add custom settings entries
102 * and get a warning. Targeting higher API version prohibits this as the
103 * system settings are not a place for apps to save their state. When a package
104 * is removed the settings it added are deleted. Apps cannot delete system
105 * settings added by the platform. System settings values are validated to
106 * ensure the clients do not put bad values. Global and secure settings are
107 * changed only by trusted parties, therefore no validation is performed. Also
108 * there is a limit on the amount of app specific settings that can be added
109 * to prevent unlimited growth of the system process memory footprint.
110 * </p>
111 */
112@SuppressWarnings("deprecation")
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700113public class SettingsProvider extends ContentProvider {
Svetoslav683914b2015-01-15 14:22:26 -0800114 private static final boolean DEBUG = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700115
Svetoslav683914b2015-01-15 14:22:26 -0800116 private static final boolean DROP_DATABASE_ON_MIGRATION = !Build.IS_DEBUGGABLE;
117
118 private static final String LOG_TAG = "SettingsProvider";
Christopher Tate0da13572013-10-13 17:34:49 -0700119
Christopher Tate06efb532012-08-24 15:29:27 -0700120 private static final String TABLE_SYSTEM = "system";
121 private static final String TABLE_SECURE = "secure";
122 private static final String TABLE_GLOBAL = "global";
Svetoslav683914b2015-01-15 14:22:26 -0800123
124 // Old tables no longer exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 private static final String TABLE_FAVORITES = "favorites";
126 private static final String TABLE_OLD_FAVORITES = "old_favorites";
Svetoslav683914b2015-01-15 14:22:26 -0800127 private static final String TABLE_BLUETOOTH_DEVICES = "bluetooth_devices";
128 private static final String TABLE_BOOKMARKS = "bookmarks";
129 private static final String TABLE_ANDROID_METADATA = "android_metadata";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130
Svetoslav683914b2015-01-15 14:22:26 -0800131 // The set of removed legacy tables.
132 private static final Set<String> REMOVED_LEGACY_TABLES = new ArraySet<>();
Christopher Tate06efb532012-08-24 15:29:27 -0700133 static {
Svetoslav683914b2015-01-15 14:22:26 -0800134 REMOVED_LEGACY_TABLES.add(TABLE_FAVORITES);
135 REMOVED_LEGACY_TABLES.add(TABLE_OLD_FAVORITES);
136 REMOVED_LEGACY_TABLES.add(TABLE_BLUETOOTH_DEVICES);
137 REMOVED_LEGACY_TABLES.add(TABLE_BOOKMARKS);
138 REMOVED_LEGACY_TABLES.add(TABLE_ANDROID_METADATA);
139 }
Christopher Tate06efb532012-08-24 15:29:27 -0700140
Svetoslav683914b2015-01-15 14:22:26 -0800141 private static final int MUTATION_OPERATION_INSERT = 1;
142 private static final int MUTATION_OPERATION_DELETE = 2;
143 private static final int MUTATION_OPERATION_UPDATE = 3;
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400144
Svetoslav683914b2015-01-15 14:22:26 -0800145 private static final String[] ALL_COLUMNS = new String[] {
146 Settings.NameValueTable._ID,
147 Settings.NameValueTable.NAME,
148 Settings.NameValueTable.VALUE
149 };
150
Svet Ganov53a441c2016-04-19 19:38:00 -0700151 public static final int SETTINGS_TYPE_GLOBAL = 0;
152 public static final int SETTINGS_TYPE_SYSTEM = 1;
153 public static final int SETTINGS_TYPE_SECURE = 2;
Svetoslav683914b2015-01-15 14:22:26 -0800154
Svet Ganov53a441c2016-04-19 19:38:00 -0700155 public static final int SETTINGS_TYPE_MASK = 0xF0000000;
156 public static final int SETTINGS_TYPE_SHIFT = 28;
157
158 private static final Bundle NULL_SETTING_BUNDLE = Bundle.forPair(
159 Settings.NameValueTable.VALUE, null);
Christopher Tate06efb532012-08-24 15:29:27 -0700160
Svetoslav683914b2015-01-15 14:22:26 -0800161 // Per user secure settings that moved to the for all users global settings.
162 static final Set<String> sSecureMovedToGlobalSettings = new ArraySet<>();
163 static {
164 Settings.Secure.getMovedToGlobalSettings(sSecureMovedToGlobalSettings);
Christopher Tate06efb532012-08-24 15:29:27 -0700165 }
166
Svetoslav683914b2015-01-15 14:22:26 -0800167 // Per user system settings that moved to the for all users global settings.
168 static final Set<String> sSystemMovedToGlobalSettings = new ArraySet<>();
169 static {
170 Settings.System.getMovedToGlobalSettings(sSystemMovedToGlobalSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700171 }
172
Svetoslav683914b2015-01-15 14:22:26 -0800173 // Per user system settings that moved to the per user secure settings.
174 static final Set<String> sSystemMovedToSecureSettings = new ArraySet<>();
175 static {
176 Settings.System.getMovedToSecureSettings(sSystemMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700177 }
178
Svetoslav683914b2015-01-15 14:22:26 -0800179 // Per all users global settings that moved to the per user secure settings.
180 static final Set<String> sGlobalMovedToSecureSettings = new ArraySet<>();
181 static {
182 Settings.Global.getMovedToSecureSettings(sGlobalMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700183 }
184
Svetoslav683914b2015-01-15 14:22:26 -0800185 // Per user secure settings that are cloned for the managed profiles of the user.
186 private static final Set<String> sSecureCloneToManagedSettings = new ArraySet<>();
187 static {
188 Settings.Secure.getCloneToManagedProfileSettings(sSecureCloneToManagedSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700189 }
190
Svetoslav683914b2015-01-15 14:22:26 -0800191 // Per user system settings that are cloned for the managed profiles of the user.
192 private static final Set<String> sSystemCloneToManagedSettings = new ArraySet<>();
193 static {
194 Settings.System.getCloneToManagedProfileSettings(sSystemCloneToManagedSettings);
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400195 }
196
Svetoslav683914b2015-01-15 14:22:26 -0800197 private final Object mLock = new Object();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700198
Svetoslav683914b2015-01-15 14:22:26 -0800199 @GuardedBy("mLock")
200 private SettingsRegistry mSettingsRegistry;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700201
Svetoslav7ec28e82015-05-20 17:01:10 -0700202 // We have to call in the user manager with no lock held,
203 private volatile UserManager mUserManager;
Svetoslav683914b2015-01-15 14:22:26 -0800204
Svetoslav7ec28e82015-05-20 17:01:10 -0700205 // We have to call in the package manager with no lock held,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700206 private volatile IPackageManager mPackageManager;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700207
Svet Ganov53a441c2016-04-19 19:38:00 -0700208 public static int makeKey(int type, int userId) {
209 return (type << SETTINGS_TYPE_SHIFT) | userId;
210 }
211
212 public static int getTypeFromKey(int key) {
213 return key >>> SETTINGS_TYPE_SHIFT;
214 }
215
216 public static int getUserIdFromKey(int key) {
217 return key & ~SETTINGS_TYPE_MASK;
218 }
219
220 public static String settingTypeToString(int type) {
221 switch (type) {
222 case SETTINGS_TYPE_GLOBAL: {
223 return "SETTINGS_GLOBAL";
224 }
225 case SETTINGS_TYPE_SECURE: {
226 return "SETTINGS_SECURE";
227 }
228 case SETTINGS_TYPE_SYSTEM: {
229 return "SETTINGS_SYSTEM";
230 }
231 default: {
232 return "UNKNOWN";
233 }
234 }
235 }
236
237 public static String keyToString(int key) {
238 return "Key[user=" + getUserIdFromKey(key) + ";type="
239 + settingTypeToString(getTypeFromKey(key)) + "]";
240 }
241
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700242 @Override
243 public boolean onCreate() {
Svetoslav683914b2015-01-15 14:22:26 -0800244 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700245 mUserManager = UserManager.get(getContext());
246 mPackageManager = AppGlobals.getPackageManager();
Svetoslav683914b2015-01-15 14:22:26 -0800247 mSettingsRegistry = new SettingsRegistry();
248 }
249 registerBroadcastReceivers();
Svet Ganov53a441c2016-04-19 19:38:00 -0700250 startWatchingUserRestrictionChanges();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700251 return true;
252 }
253
Svetoslav683914b2015-01-15 14:22:26 -0800254 @Override
255 public Bundle call(String method, String name, Bundle args) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700256 final int requestingUserId = getRequestingUserId(args);
257 switch (method) {
258 case Settings.CALL_METHOD_GET_GLOBAL: {
259 Setting setting = getGlobalSetting(name);
Svet Ganov53a441c2016-04-19 19:38:00 -0700260 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav683914b2015-01-15 14:22:26 -0800261 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700262
263 case Settings.CALL_METHOD_GET_SECURE: {
264 Setting setting = getSecureSetting(name, requestingUserId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700265 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700266 }
267
268 case Settings.CALL_METHOD_GET_SYSTEM: {
269 Setting setting = getSystemSetting(name, requestingUserId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700270 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700271 }
272
273 case Settings.CALL_METHOD_PUT_GLOBAL: {
274 String value = getSettingValue(args);
Svet Ganov53a441c2016-04-19 19:38:00 -0700275 insertGlobalSetting(name, value, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700276 break;
277 }
278
279 case Settings.CALL_METHOD_PUT_SECURE: {
280 String value = getSettingValue(args);
Svet Ganov53a441c2016-04-19 19:38:00 -0700281 insertSecureSetting(name, value, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700282 break;
283 }
284
285 case Settings.CALL_METHOD_PUT_SYSTEM: {
286 String value = getSettingValue(args);
287 insertSystemSetting(name, value, requestingUserId);
288 break;
289 }
290
291 default: {
292 Slog.w(LOG_TAG, "call() with invalid method: " + method);
293 } break;
Christopher Tate06efb532012-08-24 15:29:27 -0700294 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700295
Christopher Tate06efb532012-08-24 15:29:27 -0700296 return null;
297 }
298
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800299 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800300 public String getType(Uri uri) {
301 Arguments args = new Arguments(uri, null, null, true);
302 if (TextUtils.isEmpty(args.name)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700303 return "vnd.android.cursor.dir/" + args.table;
304 } else {
Svetoslav7ec28e82015-05-20 17:01:10 -0700305 return "vnd.android.cursor.item/" + args.table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700306 }
307 }
308
309 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800310 public Cursor query(Uri uri, String[] projection, String where, String[] whereArgs,
311 String order) {
312 if (DEBUG) {
313 Slog.v(LOG_TAG, "query() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700314 }
315
Svetoslav683914b2015-01-15 14:22:26 -0800316 Arguments args = new Arguments(uri, where, whereArgs, true);
317 String[] normalizedProjection = normalizeProjection(projection);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700318
Svetoslav683914b2015-01-15 14:22:26 -0800319 // If a legacy table that is gone, done.
320 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
321 return new MatrixCursor(normalizedProjection, 0);
322 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700323
Svetoslav7ec28e82015-05-20 17:01:10 -0700324 switch (args.table) {
325 case TABLE_GLOBAL: {
326 if (args.name != null) {
327 Setting setting = getGlobalSetting(args.name);
328 return packageSettingForQuery(setting, normalizedProjection);
329 } else {
330 return getAllGlobalSettings(projection);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700331 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700332 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700333
Svetoslav7ec28e82015-05-20 17:01:10 -0700334 case TABLE_SECURE: {
335 final int userId = UserHandle.getCallingUserId();
336 if (args.name != null) {
337 Setting setting = getSecureSetting(args.name, userId);
338 return packageSettingForQuery(setting, normalizedProjection);
339 } else {
340 return getAllSecureSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800341 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700342 }
Svetoslav683914b2015-01-15 14:22:26 -0800343
Svetoslav7ec28e82015-05-20 17:01:10 -0700344 case TABLE_SYSTEM: {
345 final int userId = UserHandle.getCallingUserId();
346 if (args.name != null) {
347 Setting setting = getSystemSetting(args.name, userId);
348 return packageSettingForQuery(setting, normalizedProjection);
349 } else {
350 return getAllSystemSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800351 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700352 }
Svetoslav683914b2015-01-15 14:22:26 -0800353
Svetoslav7ec28e82015-05-20 17:01:10 -0700354 default: {
355 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700356 }
357 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700358 }
359
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700360 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800361 public Uri insert(Uri uri, ContentValues values) {
362 if (DEBUG) {
363 Slog.v(LOG_TAG, "insert() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700364 }
365
Svetoslav683914b2015-01-15 14:22:26 -0800366 String table = getValidTableOrThrow(uri);
Christopher Tate06efb532012-08-24 15:29:27 -0700367
Svetoslav683914b2015-01-15 14:22:26 -0800368 // If a legacy table that is gone, done.
369 if (REMOVED_LEGACY_TABLES.contains(table)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 return null;
371 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700372
Svetoslav683914b2015-01-15 14:22:26 -0800373 String name = values.getAsString(Settings.Secure.NAME);
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700374 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800375 return null;
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700376 }
377
Svetoslav683914b2015-01-15 14:22:26 -0800378 String value = values.getAsString(Settings.Secure.VALUE);
379
Svetoslav7ec28e82015-05-20 17:01:10 -0700380 switch (table) {
381 case TABLE_GLOBAL: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700382 if (insertGlobalSetting(name, value, UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700383 return Uri.withAppendedPath(Settings.Global.CONTENT_URI, name);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700384 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700385 } break;
386
387 case TABLE_SECURE: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700388 if (insertSecureSetting(name, value, UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700389 return Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name);
390 }
391 } break;
392
393 case TABLE_SYSTEM: {
394 if (insertSystemSetting(name, value, UserHandle.getCallingUserId())) {
395 return Uri.withAppendedPath(Settings.System.CONTENT_URI, name);
396 }
397 } break;
398
399 default: {
400 throw new IllegalArgumentException("Bad Uri path:" + uri);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700401 }
402 }
403
Svetoslav683914b2015-01-15 14:22:26 -0800404 return null;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700405 }
406
407 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800408 public int bulkInsert(Uri uri, ContentValues[] allValues) {
409 if (DEBUG) {
410 Slog.v(LOG_TAG, "bulkInsert() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700412
Svetoslav683914b2015-01-15 14:22:26 -0800413 int insertionCount = 0;
414 final int valuesCount = allValues.length;
415 for (int i = 0; i < valuesCount; i++) {
416 ContentValues values = allValues[i];
417 if (insert(uri, values) != null) {
418 insertionCount++;
419 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700420 }
Svetoslav683914b2015-01-15 14:22:26 -0800421
422 return insertionCount;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700423 }
424
425 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800426 public int delete(Uri uri, String where, String[] whereArgs) {
427 if (DEBUG) {
428 Slog.v(LOG_TAG, "delete() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700430
Svetoslav683914b2015-01-15 14:22:26 -0800431 Arguments args = new Arguments(uri, where, whereArgs, false);
432
433 // If a legacy table that is gone, done.
434 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
435 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700436 }
Svetoslav683914b2015-01-15 14:22:26 -0800437
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700438 if (!isKeyValid(args.name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800439 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700440 }
Svetoslav683914b2015-01-15 14:22:26 -0800441
Svetoslav7ec28e82015-05-20 17:01:10 -0700442 switch (args.table) {
443 case TABLE_GLOBAL: {
444 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700445 return deleteGlobalSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700446 }
Svetoslav683914b2015-01-15 14:22:26 -0800447
Svetoslav7ec28e82015-05-20 17:01:10 -0700448 case TABLE_SECURE: {
449 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700450 return deleteSecureSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700451 }
Svetoslav683914b2015-01-15 14:22:26 -0800452
Svetoslav7ec28e82015-05-20 17:01:10 -0700453 case TABLE_SYSTEM: {
454 final int userId = UserHandle.getCallingUserId();
455 return deleteSystemSetting(args.name, userId) ? 1 : 0;
456 }
457
458 default: {
459 throw new IllegalArgumentException("Bad Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800460 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700461 }
Svetoslav683914b2015-01-15 14:22:26 -0800462 }
463
464 @Override
465 public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
466 if (DEBUG) {
467 Slog.v(LOG_TAG, "update() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700468 }
Svetoslav683914b2015-01-15 14:22:26 -0800469
470 Arguments args = new Arguments(uri, where, whereArgs, false);
471
472 // If a legacy table that is gone, done.
473 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
474 return 0;
475 }
476
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700477 String name = values.getAsString(Settings.Secure.NAME);
478 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800479 return 0;
480 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700481 String value = values.getAsString(Settings.Secure.VALUE);
Svetoslav683914b2015-01-15 14:22:26 -0800482
Svetoslav7ec28e82015-05-20 17:01:10 -0700483 switch (args.table) {
484 case TABLE_GLOBAL: {
485 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700486 return updateGlobalSetting(args.name, value, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700487 }
Svetoslav683914b2015-01-15 14:22:26 -0800488
Svetoslav7ec28e82015-05-20 17:01:10 -0700489 case TABLE_SECURE: {
490 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700491 return updateSecureSetting(args.name, value, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700492 }
Svetoslav683914b2015-01-15 14:22:26 -0800493
Svetoslav7ec28e82015-05-20 17:01:10 -0700494 case TABLE_SYSTEM: {
495 final int userId = UserHandle.getCallingUserId();
496 return updateSystemSetting(args.name, value, userId) ? 1 : 0;
497 }
Svetoslav683914b2015-01-15 14:22:26 -0800498
Svetoslav7ec28e82015-05-20 17:01:10 -0700499 default: {
500 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800501 }
502 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700503 }
504
505 @Override
506 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
Jeff Sharkey413573a2016-02-22 17:52:45 -0700507 final String cacheName;
508 if (Settings.System.RINGTONE_CACHE_URI.equals(uri)) {
509 cacheName = Settings.System.RINGTONE_CACHE;
510 } else if (Settings.System.NOTIFICATION_SOUND_CACHE_URI.equals(uri)) {
511 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
512 } else if (Settings.System.ALARM_ALERT_CACHE_URI.equals(uri)) {
513 cacheName = Settings.System.ALARM_ALERT_CACHE;
514 } else {
515 throw new FileNotFoundException("Direct file access no longer supported; "
516 + "ringtone playback is available through android.media.Ringtone");
517 }
518
519 final File cacheFile = new File(
520 getRingtoneCacheDir(UserHandle.getCallingUserId()), cacheName);
521 return ParcelFileDescriptor.open(cacheFile, ParcelFileDescriptor.parseMode(mode));
522 }
523
524 private File getRingtoneCacheDir(int userId) {
525 final File cacheDir = new File(Environment.getDataSystemDeDirectory(userId), "ringtones");
526 cacheDir.mkdir();
527 SELinux.restorecon(cacheDir);
528 return cacheDir;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700529 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800530
Svetoslavb505ccc2015-02-17 12:41:04 -0800531 @Override
532 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
533 synchronized (mLock) {
534 final long identity = Binder.clearCallingIdentity();
535 try {
536 List<UserInfo> users = mUserManager.getUsers(true);
537 final int userCount = users.size();
538 for (int i = 0; i < userCount; i++) {
539 UserInfo user = users.get(i);
540 dumpForUser(user.id, pw);
541 }
542 } finally {
543 Binder.restoreCallingIdentity(identity);
544 }
545 }
546 }
547
548 private void dumpForUser(int userId, PrintWriter pw) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700549 if (userId == UserHandle.USER_SYSTEM) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800550 pw.println("GLOBAL SETTINGS (user " + userId + ")");
Svetoslav7ec28e82015-05-20 17:01:10 -0700551 Cursor globalCursor = getAllGlobalSettings(ALL_COLUMNS);
Svetoslavb505ccc2015-02-17 12:41:04 -0800552 dumpSettings(globalCursor, pw);
553 pw.println();
554 }
555
556 pw.println("SECURE SETTINGS (user " + userId + ")");
Svetoslav7ec28e82015-05-20 17:01:10 -0700557 Cursor secureCursor = getAllSecureSettings(userId, ALL_COLUMNS);
Svetoslavb505ccc2015-02-17 12:41:04 -0800558 dumpSettings(secureCursor, pw);
559 pw.println();
560
561 pw.println("SYSTEM SETTINGS (user " + userId + ")");
Svetoslav7ec28e82015-05-20 17:01:10 -0700562 Cursor systemCursor = getAllSystemSettings(userId, ALL_COLUMNS);
Svetoslavb505ccc2015-02-17 12:41:04 -0800563 dumpSettings(systemCursor, pw);
564 pw.println();
565 }
566
567 private void dumpSettings(Cursor cursor, PrintWriter pw) {
Fyodor Kupolov1f450db2015-06-11 15:25:59 -0700568 if (cursor == null || !cursor.moveToFirst()) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800569 return;
570 }
571
572 final int idColumnIdx = cursor.getColumnIndex(Settings.NameValueTable._ID);
573 final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME);
574 final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE);
575
576 do {
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700577 pw.append("_id:").append(toDumpString(cursor.getString(idColumnIdx)));
578 pw.append(" name:").append(toDumpString(cursor.getString(nameColumnIdx)));
579 pw.append(" value:").append(toDumpString(cursor.getString(valueColumnIdx)));
Svetoslavb505ccc2015-02-17 12:41:04 -0800580 pw.println();
581 } while (cursor.moveToNext());
582 }
583
Svetoslav7e0683b2015-08-03 16:02:52 -0700584 private static String toDumpString(String s) {
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700585 if (s != null) {
586 return s;
587 }
588 return "{null}";
589 }
590
Svetoslav683914b2015-01-15 14:22:26 -0800591 private void registerBroadcastReceivers() {
592 IntentFilter userFilter = new IntentFilter();
593 userFilter.addAction(Intent.ACTION_USER_REMOVED);
594 userFilter.addAction(Intent.ACTION_USER_STOPPED);
595
596 getContext().registerReceiver(new BroadcastReceiver() {
597 @Override
598 public void onReceive(Context context, Intent intent) {
599 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700600 UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800601
602 switch (intent.getAction()) {
603 case Intent.ACTION_USER_REMOVED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700604 synchronized (mLock) {
605 mSettingsRegistry.removeUserStateLocked(userId, true);
606 }
Svetoslav683914b2015-01-15 14:22:26 -0800607 } break;
608
609 case Intent.ACTION_USER_STOPPED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700610 synchronized (mLock) {
611 mSettingsRegistry.removeUserStateLocked(userId, false);
612 }
Svetoslav683914b2015-01-15 14:22:26 -0800613 } break;
614 }
615 }
616 }, userFilter);
617
618 PackageMonitor monitor = new PackageMonitor() {
619 @Override
620 public void onPackageRemoved(String packageName, int uid) {
621 synchronized (mLock) {
622 mSettingsRegistry.onPackageRemovedLocked(packageName,
623 UserHandle.getUserId(uid));
624 }
625 }
626 };
627
628 // package changes
629 monitor.register(getContext(), BackgroundThread.getHandler().getLooper(),
630 UserHandle.ALL, true);
631 }
632
Svet Ganov53a441c2016-04-19 19:38:00 -0700633 private void startWatchingUserRestrictionChanges() {
634 // TODO: The current design of settings looking different based on user restrictions
635 // should be reworked to keep them separate and system code should check the setting
636 // first followed by checking the user restriction before performing an operation.
637 UserManagerInternal userManager = LocalServices.getService(UserManagerInternal.class);
638 userManager.addUserRestrictionsListener((int userId, Bundle newRestrictions,
639 Bundle prevRestrictions) -> {
640 // We are changing the settings affected by restrictions to their current
641 // value with a forced update to ensure that all cross profile dependencies
642 // are taken into account. Also make sure the settings update to.. the same
643 // value passes the security checks, so clear binder calling id.
644 if (newRestrictions.containsKey(UserManager.DISALLOW_SHARE_LOCATION)
645 != prevRestrictions.containsKey(UserManager.DISALLOW_SHARE_LOCATION)) {
646 final long identity = Binder.clearCallingIdentity();
647 try {
648 synchronized (mLock) {
649 Setting setting = getSecureSetting(
650 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, userId);
651 updateSecureSetting(Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
652 setting != null ? setting.getValue() : null, userId, true);
653 }
654 } finally {
655 Binder.restoreCallingIdentity(identity);
656 }
657 }
658 if (newRestrictions.containsKey(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)
659 != prevRestrictions.containsKey(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)) {
660 final long identity = Binder.clearCallingIdentity();
661 try {
662 synchronized (mLock) {
663 Setting setting = getGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS);
664 updateGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS,
665 setting != null ? setting.getValue() : null, userId, true);
666 }
667 } finally {
668 Binder.restoreCallingIdentity(identity);
669 }
670 }
671 if (newRestrictions.containsKey(UserManager.DISALLOW_DEBUGGING_FEATURES)
672 != prevRestrictions.containsKey(UserManager.DISALLOW_DEBUGGING_FEATURES)) {
673 final long identity = Binder.clearCallingIdentity();
674 try {
675 synchronized (mLock) {
676 Setting setting = getGlobalSetting(Settings.Global.ADB_ENABLED);
677 updateGlobalSetting(Settings.Global.ADB_ENABLED,
678 setting != null ? setting.getValue() : null, userId, true);
679 }
680 } finally {
681 Binder.restoreCallingIdentity(identity);
682 }
683 }
684 if (newRestrictions.containsKey(UserManager.ENSURE_VERIFY_APPS)
685 != prevRestrictions.containsKey(UserManager.ENSURE_VERIFY_APPS)) {
686 final long identity = Binder.clearCallingIdentity();
687 try {
688 synchronized (mLock) {
689 Setting enable = getGlobalSetting(
690 Settings.Global.PACKAGE_VERIFIER_ENABLE);
691 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_ENABLE,
692 enable != null ? enable.getValue() : null, userId, true);
693 Setting include = getGlobalSetting(
694 Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB);
695 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB,
696 include != null ? include.getValue() : null, userId, true);
697 }
698 } finally {
699 Binder.restoreCallingIdentity(identity);
700 }
701 }
702 if (newRestrictions.containsKey(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)
703 != prevRestrictions.containsKey(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
704 final long identity = Binder.clearCallingIdentity();
705 try {
706 synchronized (mLock) {
707 Setting setting = getGlobalSetting(
708 Settings.Global.PREFERRED_NETWORK_MODE);
709 updateGlobalSetting(Settings.Global.PREFERRED_NETWORK_MODE,
710 setting != null ? setting.getValue() : null, userId, true);
711 }
712 } finally {
713 Binder.restoreCallingIdentity(identity);
714 }
715 }
716 });
717 }
718
Svetoslav7ec28e82015-05-20 17:01:10 -0700719 private Cursor getAllGlobalSettings(String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800720 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700721 Slog.v(LOG_TAG, "getAllGlobalSettings()");
Svetoslav683914b2015-01-15 14:22:26 -0800722 }
723
Svetoslav7ec28e82015-05-20 17:01:10 -0700724 synchronized (mLock) {
725 // Get the settings.
726 SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700727 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800728
Svetoslav7ec28e82015-05-20 17:01:10 -0700729 List<String> names = settingsState.getSettingNamesLocked();
Svetoslav683914b2015-01-15 14:22:26 -0800730
Svetoslav7ec28e82015-05-20 17:01:10 -0700731 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -0800732
Svetoslav7ec28e82015-05-20 17:01:10 -0700733 String[] normalizedProjection = normalizeProjection(projection);
734 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -0800735
Svetoslav7ec28e82015-05-20 17:01:10 -0700736 // Anyone can get the global settings, so no security checks.
737 for (int i = 0; i < nameCount; i++) {
738 String name = names.get(i);
739 Setting setting = settingsState.getSettingLocked(name);
740 appendSettingToCursor(result, setting);
741 }
742
743 return result;
Svetoslav683914b2015-01-15 14:22:26 -0800744 }
Svetoslav683914b2015-01-15 14:22:26 -0800745 }
746
Svetoslav7ec28e82015-05-20 17:01:10 -0700747 private Setting getGlobalSetting(String name) {
Svetoslav683914b2015-01-15 14:22:26 -0800748 if (DEBUG) {
749 Slog.v(LOG_TAG, "getGlobalSetting(" + name + ")");
750 }
751
752 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700753 synchronized (mLock) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700754 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_GLOBAL,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700755 UserHandle.USER_SYSTEM, name);
Svetoslav683914b2015-01-15 14:22:26 -0800756 }
Svetoslav683914b2015-01-15 14:22:26 -0800757 }
758
Svet Ganov53a441c2016-04-19 19:38:00 -0700759 private boolean updateGlobalSetting(String name, String value, int requestingUserId,
760 boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800761 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700762 Slog.v(LOG_TAG, "updateGlobalSetting(" + name + ", " + value + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800763 }
Svet Ganov53a441c2016-04-19 19:38:00 -0700764 return mutateGlobalSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE,
765 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800766 }
767
Svet Ganov53a441c2016-04-19 19:38:00 -0700768 private boolean insertGlobalSetting(String name, String value, int requestingUserId,
769 boolean forceNotify) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700770 if (DEBUG) {
771 Slog.v(LOG_TAG, "insertGlobalSetting(" + name + ", " + value + ")");
772 }
Svet Ganov53a441c2016-04-19 19:38:00 -0700773 return mutateGlobalSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT,
774 forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700775 }
776
Svet Ganov53a441c2016-04-19 19:38:00 -0700777 private boolean deleteGlobalSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800778 if (DEBUG) {
779 Slog.v(LOG_TAG, "deleteGlobalSettingLocked(" + name + ")");
780 }
Svet Ganov53a441c2016-04-19 19:38:00 -0700781 return mutateGlobalSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE,
782 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800783 }
784
Svetoslav7ec28e82015-05-20 17:01:10 -0700785 private boolean mutateGlobalSetting(String name, String value, int requestingUserId,
Svet Ganov53a441c2016-04-19 19:38:00 -0700786 int operation, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800787 // Make sure the caller can change the settings - treated as secure.
788 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
789
Svetoslav683914b2015-01-15 14:22:26 -0800790 // Resolve the userId on whose behalf the call is made.
791 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
792
Makoto Onuki28da2e32015-11-20 11:30:44 -0800793 // If this is a setting that is currently restricted for this user, do not allow
794 // unrestricting changes.
795 if (isGlobalOrSecureSettingRestrictedForUser(name, callingUserId, value)) {
Svetoslav683914b2015-01-15 14:22:26 -0800796 return false;
797 }
798
799 // Perform the mutation.
Svetoslav7ec28e82015-05-20 17:01:10 -0700800 synchronized (mLock) {
801 switch (operation) {
802 case MUTATION_OPERATION_INSERT: {
803 return mSettingsRegistry
Svet Ganov53a441c2016-04-19 19:38:00 -0700804 .insertSettingLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM,
805 name, value, getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700806 }
Svetoslav683914b2015-01-15 14:22:26 -0800807
Svetoslav7ec28e82015-05-20 17:01:10 -0700808 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700809 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_GLOBAL,
810 UserHandle.USER_SYSTEM, name, forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700811 }
Svetoslav683914b2015-01-15 14:22:26 -0800812
Svetoslav7ec28e82015-05-20 17:01:10 -0700813 case MUTATION_OPERATION_UPDATE: {
814 return mSettingsRegistry
Svet Ganov53a441c2016-04-19 19:38:00 -0700815 .updateSettingLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM,
816 name, value, getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700817 }
Svetoslav683914b2015-01-15 14:22:26 -0800818 }
819 }
820
821 return false;
822 }
823
Svetoslav7ec28e82015-05-20 17:01:10 -0700824 private Cursor getAllSecureSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800825 if (DEBUG) {
826 Slog.v(LOG_TAG, "getAllSecureSettings(" + userId + ")");
827 }
828
829 // Resolve the userId on whose behalf the call is made.
830 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
831
Svetoslav7ec28e82015-05-20 17:01:10 -0700832 synchronized (mLock) {
833 List<String> names = mSettingsRegistry.getSettingsNamesLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700834 SETTINGS_TYPE_SECURE, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -0800835
Svetoslav7ec28e82015-05-20 17:01:10 -0700836 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -0800837
Svetoslav7ec28e82015-05-20 17:01:10 -0700838 String[] normalizedProjection = normalizeProjection(projection);
839 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -0800840
Svetoslav7ec28e82015-05-20 17:01:10 -0700841 for (int i = 0; i < nameCount; i++) {
842 String name = names.get(i);
843 // Determine the owning user as some profile settings are cloned from the parent.
844 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
845 name);
Svetoslav683914b2015-01-15 14:22:26 -0800846
Svetoslav7ec28e82015-05-20 17:01:10 -0700847 // Special case for location (sigh).
848 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
Svetoslav Ganov83a1f7f2016-04-27 13:50:49 -0700849 continue;
Svetoslav7ec28e82015-05-20 17:01:10 -0700850 }
Svetoslav683914b2015-01-15 14:22:26 -0800851
Svetoslav7ec28e82015-05-20 17:01:10 -0700852 Setting setting = mSettingsRegistry.getSettingLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700853 SETTINGS_TYPE_SECURE, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -0700854 appendSettingToCursor(result, setting);
Svetoslav683914b2015-01-15 14:22:26 -0800855 }
856
Svetoslav7ec28e82015-05-20 17:01:10 -0700857 return result;
Svetoslav683914b2015-01-15 14:22:26 -0800858 }
Svetoslav683914b2015-01-15 14:22:26 -0800859 }
860
Svetoslav7ec28e82015-05-20 17:01:10 -0700861 private Setting getSecureSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800862 if (DEBUG) {
863 Slog.v(LOG_TAG, "getSecureSetting(" + name + ", " + requestingUserId + ")");
864 }
865
866 // Resolve the userId on whose behalf the call is made.
867 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
868
869 // Determine the owning user as some profile settings are cloned from the parent.
870 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
871
872 // Special case for location (sigh).
873 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
Svetoslav Ganov83a1f7f2016-04-27 13:50:49 -0700874 return mSettingsRegistry.getSettingsLocked(SETTINGS_TYPE_SECURE,
875 owningUserId).getNullSetting();
Svetoslav683914b2015-01-15 14:22:26 -0800876 }
877
878 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700879 synchronized (mLock) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700880 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav7ec28e82015-05-20 17:01:10 -0700881 owningUserId, name);
882 }
Svetoslav683914b2015-01-15 14:22:26 -0800883 }
884
Svet Ganov53a441c2016-04-19 19:38:00 -0700885 private boolean insertSecureSetting(String name, String value, int requestingUserId,
886 boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800887 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700888 Slog.v(LOG_TAG, "insertSecureSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -0800889 + requestingUserId + ")");
890 }
891
Svet Ganov53a441c2016-04-19 19:38:00 -0700892 return mutateSecureSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT,
893 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800894 }
895
Svet Ganov53a441c2016-04-19 19:38:00 -0700896 private boolean deleteSecureSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800897 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700898 Slog.v(LOG_TAG, "deleteSecureSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800899 }
900
Svet Ganov53a441c2016-04-19 19:38:00 -0700901 return mutateSecureSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE,
902 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800903 }
904
Svet Ganov53a441c2016-04-19 19:38:00 -0700905 private boolean updateSecureSetting(String name, String value, int requestingUserId,
906 boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800907 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700908 Slog.v(LOG_TAG, "updateSecureSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -0800909 + requestingUserId + ")");
910 }
911
Svet Ganov53a441c2016-04-19 19:38:00 -0700912 return mutateSecureSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE,
913 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800914 }
915
Svetoslav7ec28e82015-05-20 17:01:10 -0700916 private boolean mutateSecureSetting(String name, String value, int requestingUserId,
Svet Ganov53a441c2016-04-19 19:38:00 -0700917 int operation, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800918 // Make sure the caller can change the settings.
919 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
920
Svetoslav683914b2015-01-15 14:22:26 -0800921 // Resolve the userId on whose behalf the call is made.
922 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
923
Makoto Onuki28da2e32015-11-20 11:30:44 -0800924 // If this is a setting that is currently restricted for this user, do not allow
925 // unrestricting changes.
926 if (isGlobalOrSecureSettingRestrictedForUser(name, callingUserId, value)) {
Svetoslav683914b2015-01-15 14:22:26 -0800927 return false;
928 }
929
930 // Determine the owning user as some profile settings are cloned from the parent.
931 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
932
933 // Only the owning user can change the setting.
934 if (owningUserId != callingUserId) {
935 return false;
936 }
937
938 // Special cases for location providers (sigh).
939 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700940 return updateLocationProvidersAllowedLocked(value, owningUserId, forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800941 }
942
943 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700944 synchronized (mLock) {
945 switch (operation) {
946 case MUTATION_OPERATION_INSERT: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700947 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
948 owningUserId, name, value, getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700949 }
Svetoslav683914b2015-01-15 14:22:26 -0800950
Svetoslav7ec28e82015-05-20 17:01:10 -0700951 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700952 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SECURE,
953 owningUserId, name, forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700954 }
Svetoslav683914b2015-01-15 14:22:26 -0800955
Svetoslav7ec28e82015-05-20 17:01:10 -0700956 case MUTATION_OPERATION_UPDATE: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700957 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SECURE,
958 owningUserId, name, value, getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700959 }
Svetoslav683914b2015-01-15 14:22:26 -0800960 }
961 }
962
963 return false;
964 }
965
Svetoslav7ec28e82015-05-20 17:01:10 -0700966 private Cursor getAllSystemSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800967 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700968 Slog.v(LOG_TAG, "getAllSecureSystem(" + userId + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800969 }
970
971 // Resolve the userId on whose behalf the call is made.
972 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
973
Svetoslav7ec28e82015-05-20 17:01:10 -0700974 synchronized (mLock) {
975 List<String> names = mSettingsRegistry.getSettingsNamesLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700976 SETTINGS_TYPE_SYSTEM, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -0800977
Svetoslav7ec28e82015-05-20 17:01:10 -0700978 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -0800979
Svetoslav7ec28e82015-05-20 17:01:10 -0700980 String[] normalizedProjection = normalizeProjection(projection);
981 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -0800982
Svetoslav7ec28e82015-05-20 17:01:10 -0700983 for (int i = 0; i < nameCount; i++) {
984 String name = names.get(i);
Svetoslav683914b2015-01-15 14:22:26 -0800985
Svetoslav7ec28e82015-05-20 17:01:10 -0700986 // Determine the owning user as some profile settings are cloned from the parent.
987 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId,
988 name);
Svetoslav683914b2015-01-15 14:22:26 -0800989
Svetoslav7ec28e82015-05-20 17:01:10 -0700990 Setting setting = mSettingsRegistry.getSettingLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700991 SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -0700992 appendSettingToCursor(result, setting);
993 }
994
995 return result;
Svetoslav683914b2015-01-15 14:22:26 -0800996 }
Svetoslav683914b2015-01-15 14:22:26 -0800997 }
998
Svetoslav7ec28e82015-05-20 17:01:10 -0700999 private Setting getSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001000 if (DEBUG) {
1001 Slog.v(LOG_TAG, "getSystemSetting(" + name + ", " + requestingUserId + ")");
1002 }
1003
1004 // Resolve the userId on whose behalf the call is made.
1005 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1006
1007 // Determine the owning user as some profile settings are cloned from the parent.
1008 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1009
1010 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001011 synchronized (mLock) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001012 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001013 }
Svetoslav683914b2015-01-15 14:22:26 -08001014 }
1015
Svetoslav7ec28e82015-05-20 17:01:10 -07001016 private boolean insertSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001017 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001018 Slog.v(LOG_TAG, "insertSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001019 + requestingUserId + ")");
1020 }
1021
Svetoslav7ec28e82015-05-20 17:01:10 -07001022 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT);
Svetoslav683914b2015-01-15 14:22:26 -08001023 }
1024
Svetoslav7ec28e82015-05-20 17:01:10 -07001025 private boolean deleteSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001026 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001027 Slog.v(LOG_TAG, "deleteSystemSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001028 }
1029
Svetoslav7ec28e82015-05-20 17:01:10 -07001030 return mutateSystemSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE);
Svetoslav683914b2015-01-15 14:22:26 -08001031 }
1032
Svetoslav7ec28e82015-05-20 17:01:10 -07001033 private boolean updateSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001034 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001035 Slog.v(LOG_TAG, "updateSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001036 + requestingUserId + ")");
1037 }
1038
Svetoslav7ec28e82015-05-20 17:01:10 -07001039 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE);
Svetoslav683914b2015-01-15 14:22:26 -08001040 }
1041
Svetoslav7ec28e82015-05-20 17:01:10 -07001042 private boolean mutateSystemSetting(String name, String value, int runAsUserId,
Svetoslav683914b2015-01-15 14:22:26 -08001043 int operation) {
Billy Lau6ad2d662015-07-18 00:26:58 +01001044 if (!hasWriteSecureSettingsPermission()) {
1045 // If the caller doesn't hold WRITE_SECURE_SETTINGS, we verify whether this
1046 // operation is allowed for the calling package through appops.
1047 if (!Settings.checkAndNoteWriteSettingsOperation(getContext(),
1048 Binder.getCallingUid(), getCallingPackage(), true)) {
1049 return false;
1050 }
Svetoslav683914b2015-01-15 14:22:26 -08001051 }
1052
Svetoslav683914b2015-01-15 14:22:26 -08001053 // Resolve the userId on whose behalf the call is made.
1054 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId);
1055
Svetoslavd8d25e02015-11-20 13:09:26 -08001056 // Enforce what the calling package can mutate the system settings.
1057 enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name, callingUserId);
1058
Svetoslav683914b2015-01-15 14:22:26 -08001059 // Determine the owning user as some profile settings are cloned from the parent.
1060 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1061
1062 // Only the owning user id can change the setting.
1063 if (owningUserId != callingUserId) {
1064 return false;
1065 }
1066
Jeff Sharkey413573a2016-02-22 17:52:45 -07001067 // Invalidate any relevant cache files
1068 String cacheName = null;
1069 if (Settings.System.RINGTONE.equals(name)) {
1070 cacheName = Settings.System.RINGTONE_CACHE;
1071 } else if (Settings.System.NOTIFICATION_SOUND.equals(name)) {
1072 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
1073 } else if (Settings.System.ALARM_ALERT.equals(name)) {
1074 cacheName = Settings.System.ALARM_ALERT_CACHE;
1075 }
1076 if (cacheName != null) {
1077 final File cacheFile = new File(
1078 getRingtoneCacheDir(UserHandle.getCallingUserId()), cacheName);
1079 cacheFile.delete();
1080 }
1081
Svetoslav683914b2015-01-15 14:22:26 -08001082 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001083 synchronized (mLock) {
1084 switch (operation) {
1085 case MUTATION_OPERATION_INSERT: {
1086 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001087 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SYSTEM,
1088 owningUserId, name, value, getCallingPackage(), false);
Svetoslav7ec28e82015-05-20 17:01:10 -07001089 }
1090
1091 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001092 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SYSTEM,
1093 owningUserId, name, false);
Svetoslav7ec28e82015-05-20 17:01:10 -07001094 }
1095
1096 case MUTATION_OPERATION_UPDATE: {
1097 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001098 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SYSTEM,
1099 owningUserId, name, value, getCallingPackage(), false);
Svetoslav7ec28e82015-05-20 17:01:10 -07001100 }
Svetoslav683914b2015-01-15 14:22:26 -08001101 }
1102
Svetoslav7ec28e82015-05-20 17:01:10 -07001103 return false;
Svetoslav683914b2015-01-15 14:22:26 -08001104 }
Svetoslav683914b2015-01-15 14:22:26 -08001105 }
1106
Billy Lau6ad2d662015-07-18 00:26:58 +01001107 private boolean hasWriteSecureSettingsPermission() {
Svetoslavf41334b2015-06-23 12:06:03 -07001108 // Write secure settings is a more protected permission. If caller has it we are good.
1109 if (getContext().checkCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS)
1110 == PackageManager.PERMISSION_GRANTED) {
1111 return true;
1112 }
1113
Svetoslavf41334b2015-06-23 12:06:03 -07001114 return false;
1115 }
1116
Svetoslav683914b2015-01-15 14:22:26 -08001117 private void validateSystemSettingValue(String name, String value) {
1118 Settings.System.Validator validator = Settings.System.VALIDATORS.get(name);
1119 if (validator != null && !validator.validate(value)) {
1120 throw new IllegalArgumentException("Invalid value: " + value
1121 + " for setting: " + name);
1122 }
1123 }
1124
1125 private boolean isLocationProvidersAllowedRestricted(String name, int callingUserId,
1126 int owningUserId) {
1127 // Optimization - location providers are restricted only for managed profiles.
1128 if (callingUserId == owningUserId) {
1129 return false;
1130 }
1131 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)
1132 && mUserManager.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION,
1133 new UserHandle(callingUserId))) {
1134 return true;
1135 }
1136 return false;
1137 }
1138
Makoto Onuki28da2e32015-11-20 11:30:44 -08001139 /**
1140 * Checks whether changing a setting to a value is prohibited by the corresponding user
1141 * restriction.
1142 *
Svet Ganov53a441c2016-04-19 19:38:00 -07001143 * <p>See also {@link com.android.server.pm.UserRestrictionsUtils#applyUserRestriction(
1144 * Context, int, String, boolean)}, which should be in sync with this method.
Makoto Onuki28da2e32015-11-20 11:30:44 -08001145 *
1146 * @return true if the change is prohibited, false if the change is allowed.
1147 */
1148 private boolean isGlobalOrSecureSettingRestrictedForUser(String setting, int userId,
1149 String value) {
1150 String restriction;
1151 switch (setting) {
1152 case Settings.Secure.LOCATION_MODE:
1153 // Note LOCATION_MODE will be converted into LOCATION_PROVIDERS_ALLOWED
1154 // in android.provider.Settings.Secure.putStringForUser(), so we shouldn't come
1155 // here normally, but we still protect it here from a direct provider write.
1156 if (String.valueOf(Settings.Secure.LOCATION_MODE_OFF).equals(value)) return false;
1157 restriction = UserManager.DISALLOW_SHARE_LOCATION;
1158 break;
1159
1160 case Settings.Secure.LOCATION_PROVIDERS_ALLOWED:
1161 // See SettingsProvider.updateLocationProvidersAllowedLocked. "-" is to disable
1162 // a provider, which should be allowed even if the user restriction is set.
1163 if (value != null && value.startsWith("-")) return false;
1164 restriction = UserManager.DISALLOW_SHARE_LOCATION;
1165 break;
1166
1167 case Settings.Secure.INSTALL_NON_MARKET_APPS:
1168 if ("0".equals(value)) return false;
1169 restriction = UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES;
1170 break;
1171
1172 case Settings.Global.ADB_ENABLED:
1173 if ("0".equals(value)) return false;
1174 restriction = UserManager.DISALLOW_DEBUGGING_FEATURES;
1175 break;
1176
1177 case Settings.Global.PACKAGE_VERIFIER_ENABLE:
1178 case Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB:
1179 if ("1".equals(value)) return false;
1180 restriction = UserManager.ENSURE_VERIFY_APPS;
1181 break;
1182
1183 case Settings.Global.PREFERRED_NETWORK_MODE:
1184 restriction = UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS;
1185 break;
1186
1187 default:
Mahaver Chopra87648752016-01-08 19:23:57 +00001188 if (setting != null && setting.startsWith(Settings.Global.DATA_ROAMING)) {
Mahaver Chopradea471e2015-12-17 11:02:37 +00001189 if ("0".equals(value)) return false;
1190 restriction = UserManager.DISALLOW_DATA_ROAMING;
1191 break;
1192 }
Makoto Onuki28da2e32015-11-20 11:30:44 -08001193 return false;
Svetoslav683914b2015-01-15 14:22:26 -08001194 }
Makoto Onuki28da2e32015-11-20 11:30:44 -08001195
1196 return mUserManager.hasUserRestriction(restriction, UserHandle.of(userId));
Svetoslav683914b2015-01-15 14:22:26 -08001197 }
1198
1199 private int resolveOwningUserIdForSecureSettingLocked(int userId, String setting) {
1200 return resolveOwningUserIdLocked(userId, sSecureCloneToManagedSettings, setting);
1201 }
1202
1203 private int resolveOwningUserIdForSystemSettingLocked(int userId, String setting) {
1204 return resolveOwningUserIdLocked(userId, sSystemCloneToManagedSettings, setting);
1205 }
1206
1207 private int resolveOwningUserIdLocked(int userId, Set<String> keys, String name) {
1208 final int parentId = getGroupParentLocked(userId);
1209 if (parentId != userId && keys.contains(name)) {
1210 return parentId;
1211 }
1212 return userId;
1213 }
1214
Svetoslavf41334b2015-06-23 12:06:03 -07001215 private void enforceRestrictedSystemSettingsMutationForCallingPackage(int operation,
Xiaohui Chen43765b72015-08-31 10:57:33 -07001216 String name, int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001217 // System/root/shell can mutate whatever secure settings they want.
1218 final int callingUid = Binder.getCallingUid();
1219 if (callingUid == android.os.Process.SYSTEM_UID
1220 || callingUid == Process.SHELL_UID
1221 || callingUid == Process.ROOT_UID) {
1222 return;
1223 }
1224
1225 switch (operation) {
1226 case MUTATION_OPERATION_INSERT:
1227 // Insert updates.
1228 case MUTATION_OPERATION_UPDATE: {
1229 if (Settings.System.PUBLIC_SETTINGS.contains(name)) {
1230 return;
1231 }
1232
1233 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001234 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001235
1236 // Privileged apps can do whatever they want.
1237 if ((packageInfo.applicationInfo.privateFlags
1238 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1239 return;
1240 }
1241
1242 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1243 packageInfo.applicationInfo.targetSdkVersion, name);
1244 } break;
1245
1246 case MUTATION_OPERATION_DELETE: {
1247 if (Settings.System.PUBLIC_SETTINGS.contains(name)
1248 || Settings.System.PRIVATE_SETTINGS.contains(name)) {
1249 throw new IllegalArgumentException("You cannot delete system defined"
1250 + " secure settings.");
1251 }
1252
1253 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001254 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001255
1256 // Privileged apps can do whatever they want.
1257 if ((packageInfo.applicationInfo.privateFlags &
1258 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1259 return;
1260 }
1261
1262 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1263 packageInfo.applicationInfo.targetSdkVersion, name);
1264 } break;
1265 }
1266 }
1267
Xiaohui Chen43765b72015-08-31 10:57:33 -07001268 private PackageInfo getCallingPackageInfoOrThrow(int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001269 try {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001270 PackageInfo packageInfo = mPackageManager.getPackageInfo(
1271 getCallingPackage(), 0, userId);
1272 if (packageInfo != null) {
1273 return packageInfo;
1274 }
Xiaohui Chen43765b72015-08-31 10:57:33 -07001275 } catch (RemoteException e) {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001276 /* ignore */
Svetoslav683914b2015-01-15 14:22:26 -08001277 }
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001278 throw new IllegalStateException("Calling package doesn't exist");
Svetoslav683914b2015-01-15 14:22:26 -08001279 }
1280
1281 private int getGroupParentLocked(int userId) {
1282 // Most frequent use case.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001283 if (userId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08001284 return userId;
1285 }
1286 // We are in the same process with the user manager and the returned
1287 // user info is a cached instance, so just look up instead of cache.
1288 final long identity = Binder.clearCallingIdentity();
1289 try {
Svetoslav7ec28e82015-05-20 17:01:10 -07001290 // Just a lookup and not reentrant, so holding a lock is fine.
Svetoslav683914b2015-01-15 14:22:26 -08001291 UserInfo userInfo = mUserManager.getProfileParent(userId);
1292 return (userInfo != null) ? userInfo.id : userId;
1293 } finally {
1294 Binder.restoreCallingIdentity(identity);
1295 }
1296 }
1297
Svetoslav683914b2015-01-15 14:22:26 -08001298 private void enforceWritePermission(String permission) {
1299 if (getContext().checkCallingOrSelfPermission(permission)
1300 != PackageManager.PERMISSION_GRANTED) {
1301 throw new SecurityException("Permission denial: writing to settings requires:"
1302 + permission);
1303 }
1304 }
1305
1306 /*
1307 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
1308 * This setting contains a list of the currently enabled location providers.
1309 * But helper functions in android.providers.Settings can enable or disable
1310 * a single provider by using a "+" or "-" prefix before the provider name.
1311 *
Makoto Onuki28da2e32015-11-20 11:30:44 -08001312 * <p>See also {@link #isGlobalOrSecureSettingRestrictedForUser()}. If DISALLOW_SHARE_LOCATION
1313 * is set, the said method will only allow values with the "-" prefix.
1314 *
Svetoslav683914b2015-01-15 14:22:26 -08001315 * @returns whether the enabled location providers changed.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001316 */
Svet Ganov53a441c2016-04-19 19:38:00 -07001317 private boolean updateLocationProvidersAllowedLocked(String value, int owningUserId,
1318 boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001319 if (TextUtils.isEmpty(value)) {
1320 return false;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001321 }
1322
Svetoslav683914b2015-01-15 14:22:26 -08001323 final char prefix = value.charAt(0);
1324 if (prefix != '+' && prefix != '-') {
Svet Ganov53a441c2016-04-19 19:38:00 -07001325 if (forceNotify) {
1326 final int key = makeKey(SETTINGS_TYPE_SECURE, owningUserId);
1327 mSettingsRegistry.notifyForSettingsChange(key,
1328 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
1329 }
Svetoslav683914b2015-01-15 14:22:26 -08001330 return false;
1331 }
1332
1333 // skip prefix
1334 value = value.substring(1);
1335
Svetoslav7ec28e82015-05-20 17:01:10 -07001336 Setting settingValue = getSecureSetting(
Svetoslav683914b2015-01-15 14:22:26 -08001337 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, owningUserId);
1338
1339 String oldProviders = (settingValue != null) ? settingValue.getValue() : "";
1340
1341 int index = oldProviders.indexOf(value);
1342 int end = index + value.length();
1343
1344 // check for commas to avoid matching on partial string
1345 if (index > 0 && oldProviders.charAt(index - 1) != ',') {
1346 index = -1;
1347 }
1348
1349 // check for commas to avoid matching on partial string
1350 if (end < oldProviders.length() && oldProviders.charAt(end) != ',') {
1351 index = -1;
1352 }
1353
1354 String newProviders;
1355
1356 if (prefix == '+' && index < 0) {
1357 // append the provider to the list if not present
1358 if (oldProviders.length() == 0) {
1359 newProviders = value;
1360 } else {
1361 newProviders = oldProviders + ',' + value;
1362 }
1363 } else if (prefix == '-' && index >= 0) {
1364 // remove the provider from the list if present
1365 // remove leading or trailing comma
1366 if (index > 0) {
1367 index--;
1368 } else if (end < oldProviders.length()) {
1369 end++;
1370 }
1371
1372 newProviders = oldProviders.substring(0, index);
1373 if (end < oldProviders.length()) {
1374 newProviders += oldProviders.substring(end);
1375 }
1376 } else {
1377 // nothing changed, so no need to update the database
Svet Ganov53a441c2016-04-19 19:38:00 -07001378 if (forceNotify) {
1379 final int key = makeKey(SETTINGS_TYPE_SECURE, owningUserId);
1380 mSettingsRegistry.notifyForSettingsChange(key,
1381 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
1382 }
Svetoslav683914b2015-01-15 14:22:26 -08001383 return false;
1384 }
1385
Svet Ganov53a441c2016-04-19 19:38:00 -07001386 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslavb596a2c2015-02-17 21:37:09 -08001387 owningUserId, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, newProviders,
Svet Ganov53a441c2016-04-19 19:38:00 -07001388 getCallingPackage(), forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -08001389 }
1390
Svetoslav683914b2015-01-15 14:22:26 -08001391 private static void warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1392 int targetSdkVersion, String name) {
1393 // If the app targets Lollipop MR1 or older SDK we warn, otherwise crash.
1394 if (targetSdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) {
1395 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
1396 Slog.w(LOG_TAG, "You shouldn't not change private system settings."
1397 + " This will soon become an error.");
1398 } else {
1399 Slog.w(LOG_TAG, "You shouldn't keep your settings in the secure settings."
1400 + " This will soon become an error.");
1401 }
1402 } else {
1403 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
1404 throw new IllegalArgumentException("You cannot change private secure settings.");
1405 } else {
1406 throw new IllegalArgumentException("You cannot keep your settings in"
1407 + " the secure settings.");
1408 }
1409 }
1410 }
1411
1412 private static int resolveCallingUserIdEnforcingPermissionsLocked(int requestingUserId) {
1413 if (requestingUserId == UserHandle.getCallingUserId()) {
1414 return requestingUserId;
1415 }
1416 return ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1417 Binder.getCallingUid(), requestingUserId, false, true,
1418 "get/set setting for user", null);
1419 }
1420
Svet Ganov53a441c2016-04-19 19:38:00 -07001421 private Bundle packageValueForCallResult(Setting setting,
1422 boolean trackingGeneration) {
1423 if (!trackingGeneration) {
1424 if (setting.isNull()) {
1425 return NULL_SETTING_BUNDLE;
1426 }
1427 return Bundle.forPair(Settings.NameValueTable.VALUE, setting.getValue());
Svetoslav683914b2015-01-15 14:22:26 -08001428 }
Svet Ganov53a441c2016-04-19 19:38:00 -07001429 Bundle result = new Bundle();
1430 result.putString(Settings.NameValueTable.VALUE,
1431 !setting.isNull() ? setting.getValue() : null);
1432 mSettingsRegistry.mGenerationRegistry.addGenerationData(result, setting.getkey());
1433 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001434 }
1435
1436 private static int getRequestingUserId(Bundle args) {
1437 final int callingUserId = UserHandle.getCallingUserId();
1438 return (args != null) ? args.getInt(Settings.CALL_METHOD_USER_KEY, callingUserId)
1439 : callingUserId;
1440 }
1441
Svet Ganov53a441c2016-04-19 19:38:00 -07001442 private boolean isTrackingGeneration(Bundle args) {
1443 return args != null && args.containsKey(Settings.CALL_METHOD_TRACK_GENERATION_KEY);
1444 }
1445
Svetoslav683914b2015-01-15 14:22:26 -08001446 private static String getSettingValue(Bundle args) {
1447 return (args != null) ? args.getString(Settings.NameValueTable.VALUE) : null;
1448 }
1449
1450 private static String getValidTableOrThrow(Uri uri) {
1451 if (uri.getPathSegments().size() > 0) {
1452 String table = uri.getPathSegments().get(0);
1453 if (DatabaseHelper.isValidTable(table)) {
1454 return table;
1455 }
1456 throw new IllegalArgumentException("Bad root path: " + table);
1457 }
1458 throw new IllegalArgumentException("Invalid URI:" + uri);
1459 }
1460
1461 private static MatrixCursor packageSettingForQuery(Setting setting, String[] projection) {
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07001462 if (setting.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08001463 return new MatrixCursor(projection, 0);
1464 }
1465 MatrixCursor cursor = new MatrixCursor(projection, 1);
1466 appendSettingToCursor(cursor, setting);
1467 return cursor;
1468 }
1469
1470 private static String[] normalizeProjection(String[] projection) {
1471 if (projection == null) {
1472 return ALL_COLUMNS;
1473 }
1474
1475 final int columnCount = projection.length;
1476 for (int i = 0; i < columnCount; i++) {
1477 String column = projection[i];
1478 if (!ArrayUtils.contains(ALL_COLUMNS, column)) {
1479 throw new IllegalArgumentException("Invalid column: " + column);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001480 }
1481 }
1482
Svetoslav683914b2015-01-15 14:22:26 -08001483 return projection;
1484 }
1485
1486 private static void appendSettingToCursor(MatrixCursor cursor, Setting setting) {
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07001487 if (setting.isNull()) {
Suprabh Shuklac9d064a2016-04-12 18:45:34 -07001488 return;
1489 }
Svetoslav683914b2015-01-15 14:22:26 -08001490 final int columnCount = cursor.getColumnCount();
1491
1492 String[] values = new String[columnCount];
1493
1494 for (int i = 0; i < columnCount; i++) {
1495 String column = cursor.getColumnName(i);
1496
1497 switch (column) {
1498 case Settings.NameValueTable._ID: {
1499 values[i] = setting.getId();
1500 } break;
1501
1502 case Settings.NameValueTable.NAME: {
1503 values[i] = setting.getName();
1504 } break;
1505
1506 case Settings.NameValueTable.VALUE: {
1507 values[i] = setting.getValue();
1508 } break;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001509 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001510 }
1511
Svetoslav683914b2015-01-15 14:22:26 -08001512 cursor.addRow(values);
1513 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001514
Makoto Onuki3a2c35782015-06-18 11:21:58 -07001515 private static boolean isKeyValid(String key) {
1516 return !(TextUtils.isEmpty(key) || SettingsState.isBinary(key));
1517 }
1518
Svetoslav683914b2015-01-15 14:22:26 -08001519 private static final class Arguments {
1520 private static final Pattern WHERE_PATTERN_WITH_PARAM_NO_BRACKETS =
1521 Pattern.compile("[\\s]*name[\\s]*=[\\s]*\\?[\\s]*");
1522
1523 private static final Pattern WHERE_PATTERN_WITH_PARAM_IN_BRACKETS =
1524 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*\\?[\\s]*\\)[\\s]*");
1525
1526 private static final Pattern WHERE_PATTERN_NO_PARAM_IN_BRACKETS =
1527 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*\\)[\\s]*");
1528
1529 private static final Pattern WHERE_PATTERN_NO_PARAM_NO_BRACKETS =
1530 Pattern.compile("[\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*");
1531
1532 public final String table;
1533 public final String name;
1534
1535 public Arguments(Uri uri, String where, String[] whereArgs, boolean supportAll) {
1536 final int segmentSize = uri.getPathSegments().size();
1537 switch (segmentSize) {
1538 case 1: {
1539 if (where != null
1540 && (WHERE_PATTERN_WITH_PARAM_NO_BRACKETS.matcher(where).matches()
1541 || WHERE_PATTERN_WITH_PARAM_IN_BRACKETS.matcher(where).matches())
1542 && whereArgs.length == 1) {
1543 name = whereArgs[0];
1544 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08001545 return;
Svetoslav683914b2015-01-15 14:22:26 -08001546 } else if (where != null
1547 && (WHERE_PATTERN_NO_PARAM_NO_BRACKETS.matcher(where).matches()
1548 || WHERE_PATTERN_NO_PARAM_IN_BRACKETS.matcher(where).matches())) {
1549 final int startIndex = Math.max(where.indexOf("'"),
1550 where.indexOf("\"")) + 1;
1551 final int endIndex = Math.max(where.lastIndexOf("'"),
1552 where.lastIndexOf("\""));
1553 name = where.substring(startIndex, endIndex);
1554 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08001555 return;
Svetoslav683914b2015-01-15 14:22:26 -08001556 } else if (supportAll && where == null && whereArgs == null) {
1557 name = null;
1558 table = computeTableForSetting(uri, null);
Svetoslav28494652015-02-12 14:11:42 -08001559 return;
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001560 }
Svetoslav683914b2015-01-15 14:22:26 -08001561 } break;
1562
Svetoslav28494652015-02-12 14:11:42 -08001563 case 2: {
1564 if (where == null && whereArgs == null) {
1565 name = uri.getPathSegments().get(1);
1566 table = computeTableForSetting(uri, name);
1567 return;
1568 }
1569 } break;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001570 }
Svetoslav28494652015-02-12 14:11:42 -08001571
1572 EventLogTags.writeUnsupportedSettingsQuery(
1573 uri.toSafeString(), where, Arrays.toString(whereArgs));
1574 String message = String.format( "Supported SQL:\n"
1575 + " uri content://some_table/some_property with null where and where args\n"
1576 + " uri content://some_table with query name=? and single name as arg\n"
1577 + " uri content://some_table with query name=some_name and null args\n"
1578 + " but got - uri:%1s, where:%2s whereArgs:%3s", uri, where,
1579 Arrays.toString(whereArgs));
1580 throw new IllegalArgumentException(message);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001581 }
1582
Svetoslav28494652015-02-12 14:11:42 -08001583 private static String computeTableForSetting(Uri uri, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08001584 String table = getValidTableOrThrow(uri);
1585
1586 if (name != null) {
1587 if (sSystemMovedToSecureSettings.contains(name)) {
1588 table = TABLE_SECURE;
1589 }
1590
1591 if (sSystemMovedToGlobalSettings.contains(name)) {
1592 table = TABLE_GLOBAL;
1593 }
1594
1595 if (sSecureMovedToGlobalSettings.contains(name)) {
1596 table = TABLE_GLOBAL;
1597 }
1598
1599 if (sGlobalMovedToSecureSettings.contains(name)) {
1600 table = TABLE_SECURE;
1601 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001602 }
Svetoslav683914b2015-01-15 14:22:26 -08001603
1604 return table;
1605 }
1606 }
1607
1608 final class SettingsRegistry {
1609 private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid";
1610
Svetoslav683914b2015-01-15 14:22:26 -08001611 private static final String SETTINGS_FILE_GLOBAL = "settings_global.xml";
1612 private static final String SETTINGS_FILE_SYSTEM = "settings_system.xml";
1613 private static final String SETTINGS_FILE_SECURE = "settings_secure.xml";
1614
1615 private final SparseArray<SettingsState> mSettingsStates = new SparseArray<>();
1616
Svet Ganov53a441c2016-04-19 19:38:00 -07001617 private GenerationRegistry mGenerationRegistry;
Svetoslav683914b2015-01-15 14:22:26 -08001618
Svetoslav7e0683b2015-08-03 16:02:52 -07001619 private final Handler mHandler;
1620
Svet Ganov53a441c2016-04-19 19:38:00 -07001621 private final BackupManager mBackupManager;
1622
Svetoslav683914b2015-01-15 14:22:26 -08001623 public SettingsRegistry() {
Svetoslav7e0683b2015-08-03 16:02:52 -07001624 mHandler = new MyHandler(getContext().getMainLooper());
Svet Ganov53a441c2016-04-19 19:38:00 -07001625 mGenerationRegistry = new GenerationRegistry(mLock);
1626 mBackupManager = new BackupManager(getContext());
Svetoslav683914b2015-01-15 14:22:26 -08001627 migrateAllLegacySettingsIfNeeded();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001628 }
1629
Svetoslav683914b2015-01-15 14:22:26 -08001630 public List<String> getSettingsNamesLocked(int type, int userId) {
1631 final int key = makeKey(type, userId);
1632 SettingsState settingsState = peekSettingsStateLocked(key);
1633 return settingsState.getSettingNamesLocked();
1634 }
1635
1636 public SettingsState getSettingsLocked(int type, int userId) {
1637 final int key = makeKey(type, userId);
1638 return peekSettingsStateLocked(key);
1639 }
1640
1641 public void ensureSettingsForUserLocked(int userId) {
1642 // Migrate the setting for this user if needed.
1643 migrateLegacySettingsForUserIfNeededLocked(userId);
1644
1645 // Ensure global settings loaded if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001646 if (userId == UserHandle.USER_SYSTEM) {
1647 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001648 ensureSettingsStateLocked(globalKey);
1649 }
1650
1651 // Ensure secure settings loaded.
1652 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1653 ensureSettingsStateLocked(secureKey);
1654
1655 // Make sure the secure settings have an Android id set.
1656 SettingsState secureSettings = getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
1657 ensureSecureSettingAndroidIdSetLocked(secureSettings);
1658
1659 // Ensure system settings loaded.
1660 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1661 ensureSettingsStateLocked(systemKey);
1662
1663 // Upgrade the settings to the latest version.
1664 UpgradeController upgrader = new UpgradeController(userId);
1665 upgrader.upgradeIfNeededLocked();
1666 }
1667
1668 private void ensureSettingsStateLocked(int key) {
1669 if (mSettingsStates.get(key) == null) {
1670 final int maxBytesPerPackage = getMaxBytesPerPackageForType(getTypeFromKey(key));
1671 SettingsState settingsState = new SettingsState(mLock, getSettingsFile(key), key,
1672 maxBytesPerPackage);
1673 mSettingsStates.put(key, settingsState);
1674 }
1675 }
1676
1677 public void removeUserStateLocked(int userId, boolean permanently) {
1678 // We always keep the global settings in memory.
1679
1680 // Nuke system settings.
1681 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1682 final SettingsState systemSettingsState = mSettingsStates.get(systemKey);
1683 if (systemSettingsState != null) {
1684 if (permanently) {
1685 mSettingsStates.remove(systemKey);
1686 systemSettingsState.destroyLocked(null);
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001687 } else {
Svetoslav683914b2015-01-15 14:22:26 -08001688 systemSettingsState.destroyLocked(new Runnable() {
1689 @Override
1690 public void run() {
1691 mSettingsStates.remove(systemKey);
1692 }
1693 });
1694 }
1695 }
1696
1697 // Nuke secure settings.
1698 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1699 final SettingsState secureSettingsState = mSettingsStates.get(secureKey);
1700 if (secureSettingsState != null) {
1701 if (permanently) {
1702 mSettingsStates.remove(secureKey);
1703 secureSettingsState.destroyLocked(null);
1704 } else {
1705 secureSettingsState.destroyLocked(new Runnable() {
1706 @Override
1707 public void run() {
1708 mSettingsStates.remove(secureKey);
1709 }
1710 });
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001711 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001712 }
Svet Ganov53a441c2016-04-19 19:38:00 -07001713
1714 // Nuke generation tracking data
1715 mGenerationRegistry.onUserRemoved(userId);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001716 }
1717
Svetoslav683914b2015-01-15 14:22:26 -08001718 public boolean insertSettingLocked(int type, int userId, String name, String value,
Svet Ganov53a441c2016-04-19 19:38:00 -07001719 String packageName, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001720 final int key = makeKey(type, userId);
1721
1722 SettingsState settingsState = peekSettingsStateLocked(key);
1723 final boolean success = settingsState.insertSettingLocked(name, value, packageName);
1724
Svet Ganov53a441c2016-04-19 19:38:00 -07001725 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08001726 notifyForSettingsChange(key, name);
1727 }
1728 return success;
1729 }
1730
Svet Ganov53a441c2016-04-19 19:38:00 -07001731 public boolean deleteSettingLocked(int type, int userId, String name, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001732 final int key = makeKey(type, userId);
1733
1734 SettingsState settingsState = peekSettingsStateLocked(key);
1735 final boolean success = settingsState.deleteSettingLocked(name);
1736
Svet Ganov53a441c2016-04-19 19:38:00 -07001737 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08001738 notifyForSettingsChange(key, name);
1739 }
1740 return success;
1741 }
1742
1743 public Setting getSettingLocked(int type, int userId, String name) {
1744 final int key = makeKey(type, userId);
1745
1746 SettingsState settingsState = peekSettingsStateLocked(key);
1747 return settingsState.getSettingLocked(name);
1748 }
1749
1750 public boolean updateSettingLocked(int type, int userId, String name, String value,
Svet Ganov53a441c2016-04-19 19:38:00 -07001751 String packageName, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001752 final int key = makeKey(type, userId);
1753
1754 SettingsState settingsState = peekSettingsStateLocked(key);
1755 final boolean success = settingsState.updateSettingLocked(name, value, packageName);
1756
Svet Ganov53a441c2016-04-19 19:38:00 -07001757 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08001758 notifyForSettingsChange(key, name);
1759 }
1760
1761 return success;
1762 }
1763
1764 public void onPackageRemovedLocked(String packageName, int userId) {
Svet Ganov8de34802015-04-27 09:33:40 -07001765 // Global and secure settings are signature protected. Apps signed
1766 // by the platform certificate are generally not uninstalled and
1767 // the main exception is tests. We trust components signed
1768 // by the platform certificate and do not do a clean up after them.
Svetoslav683914b2015-01-15 14:22:26 -08001769
1770 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1771 SettingsState systemSettings = mSettingsStates.get(systemKey);
Svet Ganov8de34802015-04-27 09:33:40 -07001772 if (systemSettings != null) {
1773 systemSettings.onPackageRemovedLocked(packageName);
1774 }
Svetoslav683914b2015-01-15 14:22:26 -08001775 }
1776
1777 private SettingsState peekSettingsStateLocked(int key) {
1778 SettingsState settingsState = mSettingsStates.get(key);
1779 if (settingsState != null) {
1780 return settingsState;
1781 }
1782
1783 ensureSettingsForUserLocked(getUserIdFromKey(key));
1784 return mSettingsStates.get(key);
1785 }
1786
1787 private void migrateAllLegacySettingsIfNeeded() {
1788 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07001789 final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001790 File globalFile = getSettingsFile(key);
1791 if (globalFile.exists()) {
1792 return;
1793 }
1794
1795 final long identity = Binder.clearCallingIdentity();
1796 try {
1797 List<UserInfo> users = mUserManager.getUsers(true);
1798
1799 final int userCount = users.size();
1800 for (int i = 0; i < userCount; i++) {
1801 final int userId = users.get(i).id;
1802
1803 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
1804 SQLiteDatabase database = dbHelper.getWritableDatabase();
1805 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
1806
1807 // Upgrade to the latest version.
1808 UpgradeController upgrader = new UpgradeController(userId);
1809 upgrader.upgradeIfNeededLocked();
1810
1811 // Drop from memory if not a running user.
1812 if (!mUserManager.isUserRunning(new UserHandle(userId))) {
1813 removeUserStateLocked(userId, false);
1814 }
1815 }
1816 } finally {
1817 Binder.restoreCallingIdentity(identity);
1818 }
1819 }
1820 }
1821
1822 private void migrateLegacySettingsForUserIfNeededLocked(int userId) {
1823 // Every user has secure settings and if no file we need to migrate.
1824 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1825 File secureFile = getSettingsFile(secureKey);
1826 if (secureFile.exists()) {
1827 return;
1828 }
1829
1830 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
1831 SQLiteDatabase database = dbHelper.getWritableDatabase();
1832
1833 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
1834 }
1835
1836 private void migrateLegacySettingsForUserLocked(DatabaseHelper dbHelper,
1837 SQLiteDatabase database, int userId) {
Amith Yamasanibf2ef612016-03-07 16:37:18 -08001838 // Move over the system settings.
1839 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1840 ensureSettingsStateLocked(systemKey);
1841 SettingsState systemSettings = mSettingsStates.get(systemKey);
1842 migrateLegacySettingsLocked(systemSettings, database, TABLE_SYSTEM);
1843 systemSettings.persistSyncLocked();
Svetoslav683914b2015-01-15 14:22:26 -08001844
1845 // Move over the secure settings.
Amith Yamasanibf2ef612016-03-07 16:37:18 -08001846 // Do this after System settings, since this is the first thing we check when deciding
1847 // to skip over migration from db to xml for a secondary user.
Svetoslav683914b2015-01-15 14:22:26 -08001848 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1849 ensureSettingsStateLocked(secureKey);
1850 SettingsState secureSettings = mSettingsStates.get(secureKey);
1851 migrateLegacySettingsLocked(secureSettings, database, TABLE_SECURE);
1852 ensureSecureSettingAndroidIdSetLocked(secureSettings);
1853 secureSettings.persistSyncLocked();
1854
Amith Yamasanibf2ef612016-03-07 16:37:18 -08001855 // Move over the global settings if owner.
1856 // Do this last, since this is the first thing we check when deciding
1857 // to skip over migration from db to xml for owner user.
1858 if (userId == UserHandle.USER_SYSTEM) {
1859 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, userId);
1860 ensureSettingsStateLocked(globalKey);
1861 SettingsState globalSettings = mSettingsStates.get(globalKey);
1862 migrateLegacySettingsLocked(globalSettings, database, TABLE_GLOBAL);
1863 globalSettings.persistSyncLocked();
1864 }
Svetoslav683914b2015-01-15 14:22:26 -08001865
1866 // Drop the database as now all is moved and persisted.
1867 if (DROP_DATABASE_ON_MIGRATION) {
1868 dbHelper.dropDatabase();
1869 } else {
1870 dbHelper.backupDatabase();
1871 }
1872 }
1873
1874 private void migrateLegacySettingsLocked(SettingsState settingsState,
1875 SQLiteDatabase database, String table) {
1876 SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
1877 queryBuilder.setTables(table);
1878
1879 Cursor cursor = queryBuilder.query(database, ALL_COLUMNS,
1880 null, null, null, null, null);
1881
1882 if (cursor == null) {
1883 return;
1884 }
1885
1886 try {
1887 if (!cursor.moveToFirst()) {
1888 return;
1889 }
1890
1891 final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME);
1892 final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE);
1893
1894 settingsState.setVersionLocked(database.getVersion());
1895
1896 while (!cursor.isAfterLast()) {
1897 String name = cursor.getString(nameColumnIdx);
1898 String value = cursor.getString(valueColumnIdx);
1899 settingsState.insertSettingLocked(name, value,
1900 SettingsState.SYSTEM_PACKAGE_NAME);
1901 cursor.moveToNext();
1902 }
1903 } finally {
1904 cursor.close();
1905 }
1906 }
1907
1908 private void ensureSecureSettingAndroidIdSetLocked(SettingsState secureSettings) {
1909 Setting value = secureSettings.getSettingLocked(Settings.Secure.ANDROID_ID);
1910
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09001911 if (!value.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08001912 return;
1913 }
1914
1915 final int userId = getUserIdFromKey(secureSettings.mKey);
1916
1917 final UserInfo user;
1918 final long identity = Binder.clearCallingIdentity();
1919 try {
1920 user = mUserManager.getUserInfo(userId);
1921 } finally {
1922 Binder.restoreCallingIdentity(identity);
1923 }
1924 if (user == null) {
1925 // Can happen due to races when deleting users - treat as benign.
1926 return;
1927 }
1928
1929 String androidId = Long.toHexString(new SecureRandom().nextLong());
1930 secureSettings.insertSettingLocked(Settings.Secure.ANDROID_ID, androidId,
1931 SettingsState.SYSTEM_PACKAGE_NAME);
1932
1933 Slog.d(LOG_TAG, "Generated and saved new ANDROID_ID [" + androidId
1934 + "] for user " + userId);
1935
1936 // Write a drop box entry if it's a restricted profile
1937 if (user.isRestricted()) {
1938 DropBoxManager dbm = (DropBoxManager) getContext().getSystemService(
1939 Context.DROPBOX_SERVICE);
1940 if (dbm != null && dbm.isTagEnabled(DROPBOX_TAG_USERLOG)) {
1941 dbm.addText(DROPBOX_TAG_USERLOG, System.currentTimeMillis()
1942 + "," + DROPBOX_TAG_USERLOG + "," + androidId + "\n");
1943 }
1944 }
1945 }
1946
1947 private void notifyForSettingsChange(int key, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08001948 final int userId = getUserIdFromKey(key);
1949 Uri uri = getNotificationUriFor(key, name);
1950
Svetoslav7e0683b2015-08-03 16:02:52 -07001951 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
1952 userId, 0, uri).sendToTarget();
1953
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01001954 if (isSecureSettingsKey(key)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001955 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
1956 sSecureCloneToManagedSettings);
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01001957 } else if (isSystemSettingsKey(key)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001958 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
1959 sSystemCloneToManagedSettings);
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01001960 }
Svet Ganov53a441c2016-04-19 19:38:00 -07001961
1962 mGenerationRegistry.incrementGeneration(key);
1963
1964 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01001965 }
1966
Svet Ganov53a441c2016-04-19 19:38:00 -07001967 private void maybeNotifyProfiles(int type, int userId, Uri uri, String name,
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01001968 Set<String> keysCloned) {
1969 if (keysCloned.contains(name)) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001970 for (int profileId : mUserManager.getProfileIdsWithDisabled(userId)) {
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01001971 // the notification for userId has already been sent.
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001972 if (profileId != userId) {
Svetoslav7e0683b2015-08-03 16:02:52 -07001973 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001974 profileId, 0, uri).sendToTarget();
Svet Ganov53a441c2016-04-19 19:38:00 -07001975 final int key = makeKey(type, profileId);
1976 mGenerationRegistry.incrementGeneration(key);
1977
1978 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01001979 }
1980 }
1981 }
Svetoslav683914b2015-01-15 14:22:26 -08001982 }
1983
Svetoslav683914b2015-01-15 14:22:26 -08001984 private boolean isGlobalSettingsKey(int key) {
1985 return getTypeFromKey(key) == SETTINGS_TYPE_GLOBAL;
1986 }
1987
1988 private boolean isSystemSettingsKey(int key) {
1989 return getTypeFromKey(key) == SETTINGS_TYPE_SYSTEM;
1990 }
1991
1992 private boolean isSecureSettingsKey(int key) {
1993 return getTypeFromKey(key) == SETTINGS_TYPE_SECURE;
1994 }
1995
1996 private File getSettingsFile(int key) {
1997 if (isGlobalSettingsKey(key)) {
1998 final int userId = getUserIdFromKey(key);
1999 return new File(Environment.getUserSystemDirectory(userId),
2000 SETTINGS_FILE_GLOBAL);
2001 } else if (isSystemSettingsKey(key)) {
2002 final int userId = getUserIdFromKey(key);
2003 return new File(Environment.getUserSystemDirectory(userId),
2004 SETTINGS_FILE_SYSTEM);
2005 } else if (isSecureSettingsKey(key)) {
2006 final int userId = getUserIdFromKey(key);
2007 return new File(Environment.getUserSystemDirectory(userId),
2008 SETTINGS_FILE_SECURE);
2009 } else {
2010 throw new IllegalArgumentException("Invalid settings key:" + key);
2011 }
2012 }
2013
2014 private Uri getNotificationUriFor(int key, String name) {
2015 if (isGlobalSettingsKey(key)) {
2016 return (name != null) ? Uri.withAppendedPath(Settings.Global.CONTENT_URI, name)
2017 : Settings.Global.CONTENT_URI;
2018 } else if (isSecureSettingsKey(key)) {
2019 return (name != null) ? Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name)
2020 : Settings.Secure.CONTENT_URI;
2021 } else if (isSystemSettingsKey(key)) {
2022 return (name != null) ? Uri.withAppendedPath(Settings.System.CONTENT_URI, name)
2023 : Settings.System.CONTENT_URI;
2024 } else {
2025 throw new IllegalArgumentException("Invalid settings key:" + key);
2026 }
2027 }
2028
2029 private int getMaxBytesPerPackageForType(int type) {
2030 switch (type) {
2031 case SETTINGS_TYPE_GLOBAL:
2032 case SETTINGS_TYPE_SECURE: {
2033 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED;
2034 }
2035
2036 default: {
2037 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_LIMITED;
2038 }
2039 }
2040 }
2041
Svetoslav7e0683b2015-08-03 16:02:52 -07002042 private final class MyHandler extends Handler {
2043 private static final int MSG_NOTIFY_URI_CHANGED = 1;
2044 private static final int MSG_NOTIFY_DATA_CHANGED = 2;
2045
2046 public MyHandler(Looper looper) {
2047 super(looper);
2048 }
2049
2050 @Override
2051 public void handleMessage(Message msg) {
2052 switch (msg.what) {
2053 case MSG_NOTIFY_URI_CHANGED: {
2054 final int userId = msg.arg1;
2055 Uri uri = (Uri) msg.obj;
2056 getContext().getContentResolver().notifyChange(uri, null, true, userId);
2057 if (DEBUG) {
2058 Slog.v(LOG_TAG, "Notifying for " + userId + ": " + uri);
2059 }
2060 } break;
2061
2062 case MSG_NOTIFY_DATA_CHANGED: {
2063 mBackupManager.dataChanged();
2064 } break;
2065 }
2066 }
2067 }
2068
Svetoslav683914b2015-01-15 14:22:26 -08002069 private final class UpgradeController {
Daniel U02ba6122016-04-01 18:41:42 +01002070 private static final int SETTINGS_VERSION = 127;
Svetoslav683914b2015-01-15 14:22:26 -08002071
2072 private final int mUserId;
2073
2074 public UpgradeController(int userId) {
2075 mUserId = userId;
2076 }
2077
2078 public void upgradeIfNeededLocked() {
2079 // The version of all settings for a user is the same (all users have secure).
2080 SettingsState secureSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07002081 SETTINGS_TYPE_SECURE, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08002082
2083 // Try an update from the current state.
2084 final int oldVersion = secureSettings.getVersionLocked();
2085 final int newVersion = SETTINGS_VERSION;
2086
Svet Ganovc9755bc2015-03-28 13:21:22 -07002087 // If up do date - done.
Svetoslav683914b2015-01-15 14:22:26 -08002088 if (oldVersion == newVersion) {
2089 return;
2090 }
2091
2092 // Try to upgrade.
2093 final int curVersion = onUpgradeLocked(mUserId, oldVersion, newVersion);
2094
2095 // If upgrade failed start from scratch and upgrade.
2096 if (curVersion != newVersion) {
2097 // Drop state we have for this user.
2098 removeUserStateLocked(mUserId, true);
2099
2100 // Recreate the database.
2101 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), mUserId);
2102 SQLiteDatabase database = dbHelper.getWritableDatabase();
2103 dbHelper.recreateDatabase(database, newVersion, curVersion, oldVersion);
2104
2105 // Migrate the settings for this user.
2106 migrateLegacySettingsForUserLocked(dbHelper, database, mUserId);
2107
2108 // Now upgrade should work fine.
2109 onUpgradeLocked(mUserId, oldVersion, newVersion);
2110 }
2111
2112 // Set the global settings version if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07002113 if (mUserId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08002114 SettingsState globalSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07002115 SETTINGS_TYPE_GLOBAL, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08002116 globalSettings.setVersionLocked(newVersion);
2117 }
2118
2119 // Set the secure settings version.
2120 secureSettings.setVersionLocked(newVersion);
2121
2122 // Set the system settings version.
2123 SettingsState systemSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07002124 SETTINGS_TYPE_SYSTEM, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08002125 systemSettings.setVersionLocked(newVersion);
2126 }
2127
2128 private SettingsState getGlobalSettingsLocked() {
Xiaohui Chen43765b72015-08-31 10:57:33 -07002129 return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002130 }
2131
2132 private SettingsState getSecureSettingsLocked(int userId) {
2133 return getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
2134 }
2135
2136 private SettingsState getSystemSettingsLocked(int userId) {
2137 return getSettingsLocked(SETTINGS_TYPE_SYSTEM, userId);
2138 }
2139
Jeff Brown503cffc2015-03-26 18:08:51 -07002140 /**
2141 * You must perform all necessary mutations to bring the settings
2142 * for this user from the old to the new version. When you add a new
2143 * upgrade step you *must* update SETTINGS_VERSION.
2144 *
2145 * This is an example of moving a setting from secure to global.
2146 *
2147 * // v119: Example settings changes.
2148 * if (currentVersion == 118) {
2149 * if (userId == UserHandle.USER_OWNER) {
2150 * // Remove from the secure settings.
2151 * SettingsState secureSettings = getSecureSettingsLocked(userId);
2152 * String name = "example_setting_to_move";
2153 * String value = secureSettings.getSetting(name);
2154 * secureSettings.deleteSetting(name);
2155 *
2156 * // Add to the global settings.
2157 * SettingsState globalSettings = getGlobalSettingsLocked();
2158 * globalSettings.insertSetting(name, value, SettingsState.SYSTEM_PACKAGE_NAME);
2159 * }
2160 *
2161 * // Update the current version.
2162 * currentVersion = 119;
2163 * }
2164 */
Svetoslav683914b2015-01-15 14:22:26 -08002165 private int onUpgradeLocked(int userId, int oldVersion, int newVersion) {
2166 if (DEBUG) {
2167 Slog.w(LOG_TAG, "Upgrading settings for user: " + userId + " from version: "
2168 + oldVersion + " to version: " + newVersion);
2169 }
2170
Jeff Brown503cffc2015-03-26 18:08:51 -07002171 int currentVersion = oldVersion;
Svetoslav683914b2015-01-15 14:22:26 -08002172
John Spurlocke11ae112015-05-11 16:09:03 -04002173 // v119: Reset zen + ringer mode.
2174 if (currentVersion == 118) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07002175 if (userId == UserHandle.USER_SYSTEM) {
John Spurlocke11ae112015-05-11 16:09:03 -04002176 final SettingsState globalSettings = getGlobalSettingsLocked();
2177 globalSettings.updateSettingLocked(Settings.Global.ZEN_MODE,
2178 Integer.toString(Settings.Global.ZEN_MODE_OFF),
2179 SettingsState.SYSTEM_PACKAGE_NAME);
2180 globalSettings.updateSettingLocked(Settings.Global.MODE_RINGER,
2181 Integer.toString(AudioManager.RINGER_MODE_NORMAL),
2182 SettingsState.SYSTEM_PACKAGE_NAME);
2183 }
2184 currentVersion = 119;
2185 }
2186
Jason Monk27bbb2d2015-03-31 16:46:39 -04002187 // v120: Add double tap to wake setting.
2188 if (currentVersion == 119) {
2189 SettingsState secureSettings = getSecureSettingsLocked(userId);
2190 secureSettings.insertSettingLocked(Settings.Secure.DOUBLE_TAP_TO_WAKE,
2191 getContext().getResources().getBoolean(
2192 R.bool.def_double_tap_to_wake) ? "1" : "0",
2193 SettingsState.SYSTEM_PACKAGE_NAME);
2194
2195 currentVersion = 120;
2196 }
2197
Svetoslav7e0683b2015-08-03 16:02:52 -07002198 if (currentVersion == 120) {
2199 // Before 121, we used a different string encoding logic. We just bump the
2200 // version here; SettingsState knows how to handle pre-version 120 files.
2201 currentVersion = 121;
2202 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -07002203
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02002204 if (currentVersion == 121) {
2205 // Version 122: allow OEMs to set a default payment component in resources.
2206 // Note that we only write the default if no default has been set;
2207 // if there is, we just leave the default at whatever it currently is.
2208 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2209 String defaultComponent = (getContext().getResources().getString(
2210 R.string.def_nfc_payment_component));
2211 Setting currentSetting = secureSettings.getSettingLocked(
2212 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT);
2213 if (defaultComponent != null && !defaultComponent.isEmpty() &&
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002214 currentSetting.isNull()) {
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02002215 secureSettings.insertSettingLocked(
2216 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT,
2217 defaultComponent,
2218 SettingsState.SYSTEM_PACKAGE_NAME);
2219 }
2220 currentVersion = 122;
2221 }
Suprabh Shukla269c11e2015-12-02 16:51:16 -08002222
2223 if (currentVersion == 122) {
2224 // Version 123: Adding a default value for the ability to add a user from
2225 // the lock screen.
2226 if (userId == UserHandle.USER_SYSTEM) {
2227 final SettingsState globalSettings = getGlobalSettingsLocked();
2228 Setting currentSetting = globalSettings.getSettingLocked(
2229 Settings.Global.ADD_USERS_WHEN_LOCKED);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002230 if (currentSetting.isNull()) {
Suprabh Shukla269c11e2015-12-02 16:51:16 -08002231 globalSettings.insertSettingLocked(
2232 Settings.Global.ADD_USERS_WHEN_LOCKED,
2233 getContext().getResources().getBoolean(
2234 R.bool.def_add_users_from_lockscreen) ? "1" : "0",
2235 SettingsState.SYSTEM_PACKAGE_NAME);
2236 }
2237 }
2238 currentVersion = 123;
2239 }
Bryce Leebd179282015-12-17 19:01:37 -08002240
2241 if (currentVersion == 123) {
Bryce Leeec85f342015-12-16 13:32:28 -08002242 final SettingsState globalSettings = getGlobalSettingsLocked();
2243 String defaultDisabledProfiles = (getContext().getResources().getString(
2244 R.string.def_bluetooth_disabled_profiles));
2245 globalSettings.insertSettingLocked(Settings.Global.BLUETOOTH_DISABLED_PROFILES,
2246 defaultDisabledProfiles, SettingsState.SYSTEM_PACKAGE_NAME);
Bryce Leebd179282015-12-17 19:01:37 -08002247 currentVersion = 124;
Bryce Leeec85f342015-12-16 13:32:28 -08002248 }
2249
Prathmesh Prabhude16b862016-03-04 15:22:24 -08002250 if (currentVersion == 124) {
2251 // Version 124: allow OEMs to set a default value for whether IME should be
2252 // shown when a physical keyboard is connected.
2253 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2254 Setting currentSetting = secureSettings.getSettingLocked(
2255 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002256 if (currentSetting.isNull()) {
Prathmesh Prabhude16b862016-03-04 15:22:24 -08002257 secureSettings.insertSettingLocked(
2258 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD,
2259 getContext().getResources().getBoolean(
2260 R.bool.def_show_ime_with_hard_keyboard) ? "1" : "0",
2261 SettingsState.SYSTEM_PACKAGE_NAME);
2262 }
2263 currentVersion = 125;
2264 }
2265
Ruben Brunk98576cf2016-03-07 18:54:28 -08002266 if (currentVersion == 125) {
2267 // Version 125: Allow OEMs to set the default VR service.
2268 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2269
2270 Setting currentSetting = secureSettings.getSettingLocked(
2271 Settings.Secure.ENABLED_VR_LISTENERS);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002272 if (currentSetting.isNull()) {
Ruben Brunk98576cf2016-03-07 18:54:28 -08002273 ArraySet<ComponentName> l =
2274 SystemConfig.getInstance().getDefaultVrComponents();
2275
2276 if (l != null && !l.isEmpty()) {
2277 StringBuilder b = new StringBuilder();
2278 boolean start = true;
2279 for (ComponentName c : l) {
2280 if (!start) {
2281 b.append(':');
2282 }
2283 b.append(c.flattenToString());
2284 start = false;
2285 }
2286 secureSettings.insertSettingLocked(
2287 Settings.Secure.ENABLED_VR_LISTENERS, b.toString(),
2288 SettingsState.SYSTEM_PACKAGE_NAME);
2289 }
2290
2291 }
2292 currentVersion = 126;
2293 }
2294
Daniel U02ba6122016-04-01 18:41:42 +01002295 if (currentVersion == 126) {
2296 // Version 126: copy the primary values of LOCK_SCREEN_SHOW_NOTIFICATIONS and
2297 // LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS into managed profile.
2298 if (mUserManager.isManagedProfile(userId)) {
2299 final SettingsState systemSecureSettings =
2300 getSecureSettingsLocked(UserHandle.USER_SYSTEM);
2301
2302 final Setting showNotifications = systemSecureSettings.getSettingLocked(
2303 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09002304 if (!showNotifications.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01002305 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2306 secureSettings.insertSettingLocked(
2307 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
2308 showNotifications.getValue(),
2309 SettingsState.SYSTEM_PACKAGE_NAME);
2310 }
2311
2312 final Setting allowPrivate = systemSecureSettings.getSettingLocked(
2313 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09002314 if (!allowPrivate.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01002315 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2316 secureSettings.insertSettingLocked(
2317 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,
2318 allowPrivate.getValue(),
2319 SettingsState.SYSTEM_PACKAGE_NAME);
2320 }
2321 }
2322 currentVersion = 127;
2323 }
2324
Jeff Brown503cffc2015-03-26 18:08:51 -07002325 // vXXX: Add new settings above this point.
Svetoslav683914b2015-01-15 14:22:26 -08002326
Jeff Brown503cffc2015-03-26 18:08:51 -07002327 // Return the current version.
2328 return currentVersion;
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -08002329 }
2330 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002331 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07002332}