blob: aff6ad89e6000ff577261fd2575b134ab635132d [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;
Dianne Hackborn961321f2013-02-05 17:22:41 -080021import android.app.AppOpsManager;
Christopher Tate45281862010-03-05 15:46:30 -080022import android.app.backup.BackupManager;
Christopher Tate06efb532012-08-24 15:29:27 -070023import android.content.BroadcastReceiver;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070024import android.content.ContentProvider;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070025import android.content.ContentValues;
26import android.content.Context;
Christopher Tate06efb532012-08-24 15:29:27 -070027import android.content.Intent;
28import android.content.IntentFilter;
Svetoslav683914b2015-01-15 14:22:26 -080029import android.content.pm.ApplicationInfo;
30import android.content.pm.PackageInfo;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070031import android.content.pm.PackageManager;
Christopher Tate38e7a602013-09-03 16:57:34 -070032import android.content.pm.UserInfo;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070033import android.database.Cursor;
Svetoslav683914b2015-01-15 14:22:26 -080034import android.database.MatrixCursor;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070035import android.database.sqlite.SQLiteDatabase;
36import android.database.sqlite.SQLiteQueryBuilder;
Svetoslav683914b2015-01-15 14:22:26 -080037import android.hardware.camera2.utils.ArrayUtils;
John Spurlocke11ae112015-05-11 16:09:03 -040038import android.media.AudioManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070039import android.net.Uri;
Christopher Tate06efb532012-08-24 15:29:27 -070040import android.os.Binder;
Svetoslav683914b2015-01-15 14:22:26 -080041import android.os.Build;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080042import android.os.Bundle;
Amith Yamasani5cdf7f52013-06-27 15:12:01 -070043import android.os.DropBoxManager;
Svetoslav683914b2015-01-15 14:22:26 -080044import android.os.Environment;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070045import android.os.ParcelFileDescriptor;
Christopher Tate0da13572013-10-13 17:34:49 -070046import android.os.Process;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070047import android.os.SystemProperties;
Christopher Tate06efb532012-08-24 15:29:27 -070048import android.os.UserHandle;
49import android.os.UserManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070050import android.provider.Settings;
51import android.text.TextUtils;
Svetoslav683914b2015-01-15 14:22:26 -080052import android.util.ArrayMap;
53import android.util.ArraySet;
Christopher Tate06efb532012-08-24 15:29:27 -070054import android.util.Slog;
55import android.util.SparseArray;
John Spurlocke11ae112015-05-11 16:09:03 -040056
Svetoslav683914b2015-01-15 14:22:26 -080057import com.android.internal.annotations.GuardedBy;
58import com.android.internal.content.PackageMonitor;
59import com.android.internal.os.BackgroundThread;
John Spurlocke11ae112015-05-11 16:09:03 -040060
Svetoslav683914b2015-01-15 14:22:26 -080061import java.io.File;
Svetoslavb505ccc2015-02-17 12:41:04 -080062import java.io.FileDescriptor;
Svetoslav683914b2015-01-15 14:22:26 -080063import java.io.FileNotFoundException;
Svetoslavb505ccc2015-02-17 12:41:04 -080064import java.io.PrintWriter;
Svetoslav683914b2015-01-15 14:22:26 -080065import java.security.SecureRandom;
66import java.util.Arrays;
67import java.util.List;
68import java.util.Map;
69import java.util.Set;
70import java.util.regex.Pattern;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070071
Svetoslav683914b2015-01-15 14:22:26 -080072import com.android.providers.settings.SettingsState.Setting;
73
74/**
75 * <p>
76 * This class is a content provider that publishes the system settings.
77 * It can be accessed via the content provider APIs or via custom call
78 * commands. The latter is a bit faster and is the preferred way to access
79 * the platform settings.
80 * </p>
81 * <p>
82 * There are three settings types, global (with signature level protection
83 * and shared across users), secure (with signature permission level
84 * protection and per user), and system (with dangerous permission level
85 * protection and per user). Global settings are stored under the device owner.
86 * Each of these settings is represented by a {@link
87 * com.android.providers.settings.SettingsState} object mapped to an integer
88 * key derived from the setting type in the most significant bits and user
89 * id in the least significant bits. Settings are synchronously loaded on
90 * instantiation of a SettingsState and asynchronously persisted on mutation.
91 * Settings are stored in the user specific system directory.
92 * </p>
93 * <p>
94 * Apps targeting APIs Lollipop MR1 and lower can add custom settings entries
95 * and get a warning. Targeting higher API version prohibits this as the
96 * system settings are not a place for apps to save their state. When a package
97 * is removed the settings it added are deleted. Apps cannot delete system
98 * settings added by the platform. System settings values are validated to
99 * ensure the clients do not put bad values. Global and secure settings are
100 * changed only by trusted parties, therefore no validation is performed. Also
101 * there is a limit on the amount of app specific settings that can be added
102 * to prevent unlimited growth of the system process memory footprint.
103 * </p>
104 */
105@SuppressWarnings("deprecation")
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700106public class SettingsProvider extends ContentProvider {
Svetoslav683914b2015-01-15 14:22:26 -0800107 private static final boolean DEBUG = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700108
Svetoslav683914b2015-01-15 14:22:26 -0800109 private static final boolean DROP_DATABASE_ON_MIGRATION = !Build.IS_DEBUGGABLE;
110
111 private static final String LOG_TAG = "SettingsProvider";
Christopher Tate0da13572013-10-13 17:34:49 -0700112
Christopher Tate06efb532012-08-24 15:29:27 -0700113 private static final String TABLE_SYSTEM = "system";
114 private static final String TABLE_SECURE = "secure";
115 private static final String TABLE_GLOBAL = "global";
Svetoslav683914b2015-01-15 14:22:26 -0800116
117 // Old tables no longer exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 private static final String TABLE_FAVORITES = "favorites";
119 private static final String TABLE_OLD_FAVORITES = "old_favorites";
Svetoslav683914b2015-01-15 14:22:26 -0800120 private static final String TABLE_BLUETOOTH_DEVICES = "bluetooth_devices";
121 private static final String TABLE_BOOKMARKS = "bookmarks";
122 private static final String TABLE_ANDROID_METADATA = "android_metadata";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
Svetoslav683914b2015-01-15 14:22:26 -0800124 // The set of removed legacy tables.
125 private static final Set<String> REMOVED_LEGACY_TABLES = new ArraySet<>();
Christopher Tate06efb532012-08-24 15:29:27 -0700126 static {
Svetoslav683914b2015-01-15 14:22:26 -0800127 REMOVED_LEGACY_TABLES.add(TABLE_FAVORITES);
128 REMOVED_LEGACY_TABLES.add(TABLE_OLD_FAVORITES);
129 REMOVED_LEGACY_TABLES.add(TABLE_BLUETOOTH_DEVICES);
130 REMOVED_LEGACY_TABLES.add(TABLE_BOOKMARKS);
131 REMOVED_LEGACY_TABLES.add(TABLE_ANDROID_METADATA);
132 }
Christopher Tate06efb532012-08-24 15:29:27 -0700133
Svetoslav683914b2015-01-15 14:22:26 -0800134 private static final int MUTATION_OPERATION_INSERT = 1;
135 private static final int MUTATION_OPERATION_DELETE = 2;
136 private static final int MUTATION_OPERATION_UPDATE = 3;
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400137
Svetoslav683914b2015-01-15 14:22:26 -0800138 private static final String[] ALL_COLUMNS = new String[] {
139 Settings.NameValueTable._ID,
140 Settings.NameValueTable.NAME,
141 Settings.NameValueTable.VALUE
142 };
143
144 private static final Bundle NULL_SETTING = Bundle.forPair(Settings.NameValueTable.VALUE, null);
145
146 // Per user settings that cannot be modified if associated user restrictions are enabled.
147 private static final Map<String, String> sSettingToUserRestrictionMap = new ArrayMap<>();
148 static {
149 sSettingToUserRestrictionMap.put(Settings.Secure.LOCATION_MODE,
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400150 UserManager.DISALLOW_SHARE_LOCATION);
Svetoslav683914b2015-01-15 14:22:26 -0800151 sSettingToUserRestrictionMap.put(Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
152 UserManager.DISALLOW_SHARE_LOCATION);
153 sSettingToUserRestrictionMap.put(Settings.Secure.INSTALL_NON_MARKET_APPS,
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400154 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
Svetoslav683914b2015-01-15 14:22:26 -0800155 sSettingToUserRestrictionMap.put(Settings.Global.ADB_ENABLED,
156 UserManager.DISALLOW_DEBUGGING_FEATURES);
157 sSettingToUserRestrictionMap.put(Settings.Global.PACKAGE_VERIFIER_ENABLE,
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400158 UserManager.ENSURE_VERIFY_APPS);
Svetoslav683914b2015-01-15 14:22:26 -0800159 sSettingToUserRestrictionMap.put(Settings.Global.PREFERRED_NETWORK_MODE,
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400160 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
Christopher Tate06efb532012-08-24 15:29:27 -0700161 }
162
Svetoslav683914b2015-01-15 14:22:26 -0800163 // Per user secure settings that moved to the for all users global settings.
164 static final Set<String> sSecureMovedToGlobalSettings = new ArraySet<>();
165 static {
166 Settings.Secure.getMovedToGlobalSettings(sSecureMovedToGlobalSettings);
Christopher Tate06efb532012-08-24 15:29:27 -0700167 }
168
Svetoslav683914b2015-01-15 14:22:26 -0800169 // Per user system settings that moved to the for all users global settings.
170 static final Set<String> sSystemMovedToGlobalSettings = new ArraySet<>();
171 static {
172 Settings.System.getMovedToGlobalSettings(sSystemMovedToGlobalSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700173 }
174
Svetoslav683914b2015-01-15 14:22:26 -0800175 // Per user system settings that moved to the per user secure settings.
176 static final Set<String> sSystemMovedToSecureSettings = new ArraySet<>();
177 static {
178 Settings.System.getMovedToSecureSettings(sSystemMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700179 }
180
Svetoslav683914b2015-01-15 14:22:26 -0800181 // Per all users global settings that moved to the per user secure settings.
182 static final Set<String> sGlobalMovedToSecureSettings = new ArraySet<>();
183 static {
184 Settings.Global.getMovedToSecureSettings(sGlobalMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700185 }
186
Svetoslav683914b2015-01-15 14:22:26 -0800187 // Per user secure settings that are cloned for the managed profiles of the user.
188 private static final Set<String> sSecureCloneToManagedSettings = new ArraySet<>();
189 static {
190 Settings.Secure.getCloneToManagedProfileSettings(sSecureCloneToManagedSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700191 }
192
Svetoslav683914b2015-01-15 14:22:26 -0800193 // Per user system settings that are cloned for the managed profiles of the user.
194 private static final Set<String> sSystemCloneToManagedSettings = new ArraySet<>();
195 static {
196 Settings.System.getCloneToManagedProfileSettings(sSystemCloneToManagedSettings);
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400197 }
198
Svetoslav683914b2015-01-15 14:22:26 -0800199 private final Object mLock = new Object();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700200
Svetoslav683914b2015-01-15 14:22:26 -0800201 @GuardedBy("mLock")
202 private SettingsRegistry mSettingsRegistry;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700203
Svetoslav7ec28e82015-05-20 17:01:10 -0700204 // We have to call in the user manager with no lock held,
205 private volatile UserManager mUserManager;
Svetoslav683914b2015-01-15 14:22:26 -0800206
Svetoslav7ec28e82015-05-20 17:01:10 -0700207 // We have to call in the app ops manager with no lock held,
208 private volatile AppOpsManager mAppOpsManager;
Svetoslav683914b2015-01-15 14:22:26 -0800209
Svetoslav7ec28e82015-05-20 17:01:10 -0700210 // We have to call in the package manager with no lock held,
211 private volatile PackageManager mPackageManager;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700212
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700213 @Override
214 public boolean onCreate() {
Svetoslav683914b2015-01-15 14:22:26 -0800215 synchronized (mLock) {
216 mUserManager = (UserManager) getContext().getSystemService(Context.USER_SERVICE);
217 mAppOpsManager = (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE);
218 mPackageManager = getContext().getPackageManager();
219 mSettingsRegistry = new SettingsRegistry();
220 }
221 registerBroadcastReceivers();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700222 return true;
223 }
224
Svetoslav683914b2015-01-15 14:22:26 -0800225 @Override
226 public Bundle call(String method, String name, Bundle args) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700227 final int requestingUserId = getRequestingUserId(args);
228 switch (method) {
229 case Settings.CALL_METHOD_GET_GLOBAL: {
230 Setting setting = getGlobalSetting(name);
231 return packageValueForCallResult(setting);
Svetoslav683914b2015-01-15 14:22:26 -0800232 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700233
234 case Settings.CALL_METHOD_GET_SECURE: {
235 Setting setting = getSecureSetting(name, requestingUserId);
236 return packageValueForCallResult(setting);
237 }
238
239 case Settings.CALL_METHOD_GET_SYSTEM: {
240 Setting setting = getSystemSetting(name, requestingUserId);
241 return packageValueForCallResult(setting);
242 }
243
244 case Settings.CALL_METHOD_PUT_GLOBAL: {
245 String value = getSettingValue(args);
246 insertGlobalSetting(name, value, requestingUserId);
247 break;
248 }
249
250 case Settings.CALL_METHOD_PUT_SECURE: {
251 String value = getSettingValue(args);
252 insertSecureSetting(name, value, requestingUserId);
253 break;
254 }
255
256 case Settings.CALL_METHOD_PUT_SYSTEM: {
257 String value = getSettingValue(args);
258 insertSystemSetting(name, value, requestingUserId);
259 break;
260 }
261
262 default: {
263 Slog.w(LOG_TAG, "call() with invalid method: " + method);
264 } break;
Christopher Tate06efb532012-08-24 15:29:27 -0700265 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700266
Christopher Tate06efb532012-08-24 15:29:27 -0700267 return null;
268 }
269
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800270 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800271 public String getType(Uri uri) {
272 Arguments args = new Arguments(uri, null, null, true);
273 if (TextUtils.isEmpty(args.name)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700274 return "vnd.android.cursor.dir/" + args.table;
275 } else {
Svetoslav7ec28e82015-05-20 17:01:10 -0700276 return "vnd.android.cursor.item/" + args.table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700277 }
278 }
279
280 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800281 public Cursor query(Uri uri, String[] projection, String where, String[] whereArgs,
282 String order) {
283 if (DEBUG) {
284 Slog.v(LOG_TAG, "query() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700285 }
286
Svetoslav683914b2015-01-15 14:22:26 -0800287 Arguments args = new Arguments(uri, where, whereArgs, true);
288 String[] normalizedProjection = normalizeProjection(projection);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700289
Svetoslav683914b2015-01-15 14:22:26 -0800290 // If a legacy table that is gone, done.
291 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
292 return new MatrixCursor(normalizedProjection, 0);
293 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700294
Svetoslav7ec28e82015-05-20 17:01:10 -0700295 switch (args.table) {
296 case TABLE_GLOBAL: {
297 if (args.name != null) {
298 Setting setting = getGlobalSetting(args.name);
299 return packageSettingForQuery(setting, normalizedProjection);
300 } else {
301 return getAllGlobalSettings(projection);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700302 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700303 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700304
Svetoslav7ec28e82015-05-20 17:01:10 -0700305 case TABLE_SECURE: {
306 final int userId = UserHandle.getCallingUserId();
307 if (args.name != null) {
308 Setting setting = getSecureSetting(args.name, userId);
309 return packageSettingForQuery(setting, normalizedProjection);
310 } else {
311 return getAllSecureSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800312 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700313 }
Svetoslav683914b2015-01-15 14:22:26 -0800314
Svetoslav7ec28e82015-05-20 17:01:10 -0700315 case TABLE_SYSTEM: {
316 final int userId = UserHandle.getCallingUserId();
317 if (args.name != null) {
318 Setting setting = getSystemSetting(args.name, userId);
319 return packageSettingForQuery(setting, normalizedProjection);
320 } else {
321 return getAllSystemSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800322 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700323 }
Svetoslav683914b2015-01-15 14:22:26 -0800324
Svetoslav7ec28e82015-05-20 17:01:10 -0700325 default: {
326 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700327 }
328 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700329 }
330
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700331 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800332 public Uri insert(Uri uri, ContentValues values) {
333 if (DEBUG) {
334 Slog.v(LOG_TAG, "insert() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700335 }
336
Svetoslav683914b2015-01-15 14:22:26 -0800337 String table = getValidTableOrThrow(uri);
Christopher Tate06efb532012-08-24 15:29:27 -0700338
Svetoslav683914b2015-01-15 14:22:26 -0800339 // If a legacy table that is gone, done.
340 if (REMOVED_LEGACY_TABLES.contains(table)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 return null;
342 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700343
Svetoslav683914b2015-01-15 14:22:26 -0800344 String name = values.getAsString(Settings.Secure.NAME);
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700345 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800346 return null;
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700347 }
348
Svetoslav683914b2015-01-15 14:22:26 -0800349 String value = values.getAsString(Settings.Secure.VALUE);
350
Svetoslav7ec28e82015-05-20 17:01:10 -0700351 switch (table) {
352 case TABLE_GLOBAL: {
353 if (insertGlobalSetting(name, value, UserHandle.getCallingUserId())) {
354 return Uri.withAppendedPath(Settings.Global.CONTENT_URI, name);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700355 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700356 } break;
357
358 case TABLE_SECURE: {
359 if (insertSecureSetting(name, value, UserHandle.getCallingUserId())) {
360 return Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name);
361 }
362 } break;
363
364 case TABLE_SYSTEM: {
365 if (insertSystemSetting(name, value, UserHandle.getCallingUserId())) {
366 return Uri.withAppendedPath(Settings.System.CONTENT_URI, name);
367 }
368 } break;
369
370 default: {
371 throw new IllegalArgumentException("Bad Uri path:" + uri);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700372 }
373 }
374
Svetoslav683914b2015-01-15 14:22:26 -0800375 return null;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700376 }
377
378 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800379 public int bulkInsert(Uri uri, ContentValues[] allValues) {
380 if (DEBUG) {
381 Slog.v(LOG_TAG, "bulkInsert() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700383
Svetoslav683914b2015-01-15 14:22:26 -0800384 int insertionCount = 0;
385 final int valuesCount = allValues.length;
386 for (int i = 0; i < valuesCount; i++) {
387 ContentValues values = allValues[i];
388 if (insert(uri, values) != null) {
389 insertionCount++;
390 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700391 }
Svetoslav683914b2015-01-15 14:22:26 -0800392
393 return insertionCount;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700394 }
395
396 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800397 public int delete(Uri uri, String where, String[] whereArgs) {
398 if (DEBUG) {
399 Slog.v(LOG_TAG, "delete() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700401
Svetoslav683914b2015-01-15 14:22:26 -0800402 Arguments args = new Arguments(uri, where, whereArgs, false);
403
404 // If a legacy table that is gone, done.
405 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
406 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700407 }
Svetoslav683914b2015-01-15 14:22:26 -0800408
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700409 if (!isKeyValid(args.name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800410 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700411 }
Svetoslav683914b2015-01-15 14:22:26 -0800412
Svetoslav7ec28e82015-05-20 17:01:10 -0700413 switch (args.table) {
414 case TABLE_GLOBAL: {
415 final int userId = UserHandle.getCallingUserId();
416 return deleteGlobalSetting(args.name, userId) ? 1 : 0;
417 }
Svetoslav683914b2015-01-15 14:22:26 -0800418
Svetoslav7ec28e82015-05-20 17:01:10 -0700419 case TABLE_SECURE: {
420 final int userId = UserHandle.getCallingUserId();
421 return deleteSecureSetting(args.name, userId) ? 1 : 0;
422 }
Svetoslav683914b2015-01-15 14:22:26 -0800423
Svetoslav7ec28e82015-05-20 17:01:10 -0700424 case TABLE_SYSTEM: {
425 final int userId = UserHandle.getCallingUserId();
426 return deleteSystemSetting(args.name, userId) ? 1 : 0;
427 }
428
429 default: {
430 throw new IllegalArgumentException("Bad Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800431 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700432 }
Svetoslav683914b2015-01-15 14:22:26 -0800433 }
434
435 @Override
436 public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
437 if (DEBUG) {
438 Slog.v(LOG_TAG, "update() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700439 }
Svetoslav683914b2015-01-15 14:22:26 -0800440
441 Arguments args = new Arguments(uri, where, whereArgs, false);
442
443 // If a legacy table that is gone, done.
444 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
445 return 0;
446 }
447
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700448 String name = values.getAsString(Settings.Secure.NAME);
449 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800450 return 0;
451 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700452 String value = values.getAsString(Settings.Secure.VALUE);
Svetoslav683914b2015-01-15 14:22:26 -0800453
Svetoslav7ec28e82015-05-20 17:01:10 -0700454 switch (args.table) {
455 case TABLE_GLOBAL: {
456 final int userId = UserHandle.getCallingUserId();
457 return updateGlobalSetting(args.name, value, userId) ? 1 : 0;
458 }
Svetoslav683914b2015-01-15 14:22:26 -0800459
Svetoslav7ec28e82015-05-20 17:01:10 -0700460 case TABLE_SECURE: {
461 final int userId = UserHandle.getCallingUserId();
462 return updateSecureSetting(args.name, value, userId) ? 1 : 0;
463 }
Svetoslav683914b2015-01-15 14:22:26 -0800464
Svetoslav7ec28e82015-05-20 17:01:10 -0700465 case TABLE_SYSTEM: {
466 final int userId = UserHandle.getCallingUserId();
467 return updateSystemSetting(args.name, value, userId) ? 1 : 0;
468 }
Svetoslav683914b2015-01-15 14:22:26 -0800469
Svetoslav7ec28e82015-05-20 17:01:10 -0700470 default: {
471 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800472 }
473 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700474 }
475
476 @Override
477 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
Jeff Sharkey3b566b82014-11-12 10:39:56 -0800478 throw new FileNotFoundException("Direct file access no longer supported; "
479 + "ringtone playback is available through android.media.Ringtone");
Marco Nelissen69f593c2009-07-28 09:55:04 -0700480 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800481
Svetoslavb505ccc2015-02-17 12:41:04 -0800482 @Override
483 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
484 synchronized (mLock) {
485 final long identity = Binder.clearCallingIdentity();
486 try {
487 List<UserInfo> users = mUserManager.getUsers(true);
488 final int userCount = users.size();
489 for (int i = 0; i < userCount; i++) {
490 UserInfo user = users.get(i);
491 dumpForUser(user.id, pw);
492 }
493 } finally {
494 Binder.restoreCallingIdentity(identity);
495 }
496 }
497 }
498
499 private void dumpForUser(int userId, PrintWriter pw) {
500 if (userId == UserHandle.USER_OWNER) {
501 pw.println("GLOBAL SETTINGS (user " + userId + ")");
Svetoslav7ec28e82015-05-20 17:01:10 -0700502 Cursor globalCursor = getAllGlobalSettings(ALL_COLUMNS);
Svetoslavb505ccc2015-02-17 12:41:04 -0800503 dumpSettings(globalCursor, pw);
504 pw.println();
505 }
506
507 pw.println("SECURE SETTINGS (user " + userId + ")");
Svetoslav7ec28e82015-05-20 17:01:10 -0700508 Cursor secureCursor = getAllSecureSettings(userId, ALL_COLUMNS);
Svetoslavb505ccc2015-02-17 12:41:04 -0800509 dumpSettings(secureCursor, pw);
510 pw.println();
511
512 pw.println("SYSTEM SETTINGS (user " + userId + ")");
Svetoslav7ec28e82015-05-20 17:01:10 -0700513 Cursor systemCursor = getAllSystemSettings(userId, ALL_COLUMNS);
Svetoslavb505ccc2015-02-17 12:41:04 -0800514 dumpSettings(systemCursor, pw);
515 pw.println();
516 }
517
518 private void dumpSettings(Cursor cursor, PrintWriter pw) {
Fyodor Kupolov1f450db2015-06-11 15:25:59 -0700519 if (cursor == null || !cursor.moveToFirst()) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800520 return;
521 }
522
523 final int idColumnIdx = cursor.getColumnIndex(Settings.NameValueTable._ID);
524 final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME);
525 final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE);
526
527 do {
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700528 pw.append("_id:").append(toDumpString(cursor.getString(idColumnIdx)));
529 pw.append(" name:").append(toDumpString(cursor.getString(nameColumnIdx)));
530 pw.append(" value:").append(toDumpString(cursor.getString(valueColumnIdx)));
Svetoslavb505ccc2015-02-17 12:41:04 -0800531 pw.println();
532 } while (cursor.moveToNext());
533 }
534
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700535 private static final String toDumpString(String s) {
536 if (s != null) {
537 return s;
538 }
539 return "{null}";
540 }
541
Svetoslav683914b2015-01-15 14:22:26 -0800542 private void registerBroadcastReceivers() {
543 IntentFilter userFilter = new IntentFilter();
544 userFilter.addAction(Intent.ACTION_USER_REMOVED);
545 userFilter.addAction(Intent.ACTION_USER_STOPPED);
546
547 getContext().registerReceiver(new BroadcastReceiver() {
548 @Override
549 public void onReceive(Context context, Intent intent) {
550 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
551 UserHandle.USER_OWNER);
552
553 switch (intent.getAction()) {
554 case Intent.ACTION_USER_REMOVED: {
555 mSettingsRegistry.removeUserStateLocked(userId, true);
556 } break;
557
558 case Intent.ACTION_USER_STOPPED: {
559 mSettingsRegistry.removeUserStateLocked(userId, false);
560 } break;
561 }
562 }
563 }, userFilter);
564
565 PackageMonitor monitor = new PackageMonitor() {
566 @Override
567 public void onPackageRemoved(String packageName, int uid) {
568 synchronized (mLock) {
569 mSettingsRegistry.onPackageRemovedLocked(packageName,
570 UserHandle.getUserId(uid));
571 }
572 }
573 };
574
575 // package changes
576 monitor.register(getContext(), BackgroundThread.getHandler().getLooper(),
577 UserHandle.ALL, true);
578 }
579
Svetoslav7ec28e82015-05-20 17:01:10 -0700580 private Cursor getAllGlobalSettings(String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800581 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700582 Slog.v(LOG_TAG, "getAllGlobalSettings()");
Svetoslav683914b2015-01-15 14:22:26 -0800583 }
584
Svetoslav7ec28e82015-05-20 17:01:10 -0700585 synchronized (mLock) {
586 // Get the settings.
587 SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
588 SettingsRegistry.SETTINGS_TYPE_GLOBAL, UserHandle.USER_OWNER);
Svetoslav683914b2015-01-15 14:22:26 -0800589
Svetoslav7ec28e82015-05-20 17:01:10 -0700590 List<String> names = settingsState.getSettingNamesLocked();
Svetoslav683914b2015-01-15 14:22:26 -0800591
Svetoslav7ec28e82015-05-20 17:01:10 -0700592 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -0800593
Svetoslav7ec28e82015-05-20 17:01:10 -0700594 String[] normalizedProjection = normalizeProjection(projection);
595 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -0800596
Svetoslav7ec28e82015-05-20 17:01:10 -0700597 // Anyone can get the global settings, so no security checks.
598 for (int i = 0; i < nameCount; i++) {
599 String name = names.get(i);
600 Setting setting = settingsState.getSettingLocked(name);
601 appendSettingToCursor(result, setting);
602 }
603
604 return result;
Svetoslav683914b2015-01-15 14:22:26 -0800605 }
Svetoslav683914b2015-01-15 14:22:26 -0800606 }
607
Svetoslav7ec28e82015-05-20 17:01:10 -0700608 private Setting getGlobalSetting(String name) {
Svetoslav683914b2015-01-15 14:22:26 -0800609 if (DEBUG) {
610 Slog.v(LOG_TAG, "getGlobalSetting(" + name + ")");
611 }
612
613 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700614 synchronized (mLock) {
615 return mSettingsRegistry.getSettingLocked(SettingsRegistry.SETTINGS_TYPE_GLOBAL,
616 UserHandle.USER_OWNER, name);
Svetoslav683914b2015-01-15 14:22:26 -0800617 }
Svetoslav683914b2015-01-15 14:22:26 -0800618 }
619
Svetoslav7ec28e82015-05-20 17:01:10 -0700620 private boolean updateGlobalSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800621 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700622 Slog.v(LOG_TAG, "updateGlobalSetting(" + name + ", " + value + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800623 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700624 return mutateGlobalSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE);
Svetoslav683914b2015-01-15 14:22:26 -0800625 }
626
Svetoslav7ec28e82015-05-20 17:01:10 -0700627 private boolean insertGlobalSetting(String name, String value, int requestingUserId) {
628 if (DEBUG) {
629 Slog.v(LOG_TAG, "insertGlobalSetting(" + name + ", " + value + ")");
630 }
631 return mutateGlobalSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT);
632 }
633
634 private boolean deleteGlobalSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800635 if (DEBUG) {
636 Slog.v(LOG_TAG, "deleteGlobalSettingLocked(" + name + ")");
637 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700638 return mutateGlobalSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE);
Svetoslav683914b2015-01-15 14:22:26 -0800639 }
640
Svetoslav7ec28e82015-05-20 17:01:10 -0700641 private boolean mutateGlobalSetting(String name, String value, int requestingUserId,
Svetoslav683914b2015-01-15 14:22:26 -0800642 int operation) {
643 // Make sure the caller can change the settings - treated as secure.
644 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
645
646 // Verify whether this operation is allowed for the calling package.
647 if (!isAppOpWriteSettingsAllowedForCallingPackage()) {
648 return false;
649 }
650
651 // Resolve the userId on whose behalf the call is made.
652 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
653
654 // If this is a setting that is currently restricted for this user, done.
655 if (isGlobalOrSecureSettingRestrictedForUser(name, callingUserId)) {
656 return false;
657 }
658
659 // Perform the mutation.
Svetoslav7ec28e82015-05-20 17:01:10 -0700660 synchronized (mLock) {
661 switch (operation) {
662 case MUTATION_OPERATION_INSERT: {
663 return mSettingsRegistry
664 .insertSettingLocked(SettingsRegistry.SETTINGS_TYPE_GLOBAL,
665 UserHandle.USER_OWNER, name, value, getCallingPackage());
666 }
Svetoslav683914b2015-01-15 14:22:26 -0800667
Svetoslav7ec28e82015-05-20 17:01:10 -0700668 case MUTATION_OPERATION_DELETE: {
669 return mSettingsRegistry.deleteSettingLocked(
670 SettingsRegistry.SETTINGS_TYPE_GLOBAL,
671 UserHandle.USER_OWNER, name);
672 }
Svetoslav683914b2015-01-15 14:22:26 -0800673
Svetoslav7ec28e82015-05-20 17:01:10 -0700674 case MUTATION_OPERATION_UPDATE: {
675 return mSettingsRegistry
676 .updateSettingLocked(SettingsRegistry.SETTINGS_TYPE_GLOBAL,
677 UserHandle.USER_OWNER, name, value, getCallingPackage());
678 }
Svetoslav683914b2015-01-15 14:22:26 -0800679 }
680 }
681
682 return false;
683 }
684
Svetoslav7ec28e82015-05-20 17:01:10 -0700685 private Cursor getAllSecureSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800686 if (DEBUG) {
687 Slog.v(LOG_TAG, "getAllSecureSettings(" + userId + ")");
688 }
689
690 // Resolve the userId on whose behalf the call is made.
691 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
692
Svetoslav7ec28e82015-05-20 17:01:10 -0700693 synchronized (mLock) {
694 List<String> names = mSettingsRegistry.getSettingsNamesLocked(
695 SettingsRegistry.SETTINGS_TYPE_SECURE, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -0800696
Svetoslav7ec28e82015-05-20 17:01:10 -0700697 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -0800698
Svetoslav7ec28e82015-05-20 17:01:10 -0700699 String[] normalizedProjection = normalizeProjection(projection);
700 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -0800701
Svetoslav7ec28e82015-05-20 17:01:10 -0700702 for (int i = 0; i < nameCount; i++) {
703 String name = names.get(i);
704 // Determine the owning user as some profile settings are cloned from the parent.
705 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
706 name);
Svetoslav683914b2015-01-15 14:22:26 -0800707
Svetoslav7ec28e82015-05-20 17:01:10 -0700708 // Special case for location (sigh).
709 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
710 return null;
711 }
Svetoslav683914b2015-01-15 14:22:26 -0800712
Svetoslav7ec28e82015-05-20 17:01:10 -0700713 Setting setting = mSettingsRegistry.getSettingLocked(
714 SettingsRegistry.SETTINGS_TYPE_SECURE, owningUserId, name);
715 appendSettingToCursor(result, setting);
Svetoslav683914b2015-01-15 14:22:26 -0800716 }
717
Svetoslav7ec28e82015-05-20 17:01:10 -0700718 return result;
Svetoslav683914b2015-01-15 14:22:26 -0800719 }
Svetoslav683914b2015-01-15 14:22:26 -0800720 }
721
Svetoslav7ec28e82015-05-20 17:01:10 -0700722 private Setting getSecureSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800723 if (DEBUG) {
724 Slog.v(LOG_TAG, "getSecureSetting(" + name + ", " + requestingUserId + ")");
725 }
726
727 // Resolve the userId on whose behalf the call is made.
728 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
729
730 // Determine the owning user as some profile settings are cloned from the parent.
731 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
732
733 // Special case for location (sigh).
734 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
735 return null;
736 }
737
738 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700739 synchronized (mLock) {
740 return mSettingsRegistry.getSettingLocked(SettingsRegistry.SETTINGS_TYPE_SECURE,
741 owningUserId, name);
742 }
Svetoslav683914b2015-01-15 14:22:26 -0800743 }
744
Svetoslav7ec28e82015-05-20 17:01:10 -0700745 private boolean insertSecureSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800746 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700747 Slog.v(LOG_TAG, "insertSecureSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -0800748 + requestingUserId + ")");
749 }
750
Svetoslav7ec28e82015-05-20 17:01:10 -0700751 return mutateSecureSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT);
Svetoslav683914b2015-01-15 14:22:26 -0800752 }
753
Svetoslav7ec28e82015-05-20 17:01:10 -0700754 private boolean deleteSecureSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800755 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700756 Slog.v(LOG_TAG, "deleteSecureSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800757 }
758
Svetoslav7ec28e82015-05-20 17:01:10 -0700759 return mutateSecureSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE);
Svetoslav683914b2015-01-15 14:22:26 -0800760 }
761
Svetoslav7ec28e82015-05-20 17:01:10 -0700762 private boolean updateSecureSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800763 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700764 Slog.v(LOG_TAG, "updateSecureSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -0800765 + requestingUserId + ")");
766 }
767
Svetoslav7ec28e82015-05-20 17:01:10 -0700768 return mutateSecureSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE);
Svetoslav683914b2015-01-15 14:22:26 -0800769 }
770
Svetoslav7ec28e82015-05-20 17:01:10 -0700771 private boolean mutateSecureSetting(String name, String value, int requestingUserId,
Svetoslav683914b2015-01-15 14:22:26 -0800772 int operation) {
773 // Make sure the caller can change the settings.
774 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
775
776 // Verify whether this operation is allowed for the calling package.
777 if (!isAppOpWriteSettingsAllowedForCallingPackage()) {
778 return false;
779 }
780
781 // Resolve the userId on whose behalf the call is made.
782 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
783
784 // If this is a setting that is currently restricted for this user, done.
785 if (isGlobalOrSecureSettingRestrictedForUser(name, callingUserId)) {
786 return false;
787 }
788
789 // Determine the owning user as some profile settings are cloned from the parent.
790 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
791
792 // Only the owning user can change the setting.
793 if (owningUserId != callingUserId) {
794 return false;
795 }
796
797 // Special cases for location providers (sigh).
798 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
Svetoslavb596a2c2015-02-17 21:37:09 -0800799 return updateLocationProvidersAllowedLocked(value, owningUserId);
Svetoslav683914b2015-01-15 14:22:26 -0800800 }
801
802 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700803 synchronized (mLock) {
804 switch (operation) {
805 case MUTATION_OPERATION_INSERT: {
806 return mSettingsRegistry
807 .insertSettingLocked(SettingsRegistry.SETTINGS_TYPE_SECURE,
808 owningUserId, name, value, getCallingPackage());
809 }
Svetoslav683914b2015-01-15 14:22:26 -0800810
Svetoslav7ec28e82015-05-20 17:01:10 -0700811 case MUTATION_OPERATION_DELETE: {
812 return mSettingsRegistry.deleteSettingLocked(
813 SettingsRegistry.SETTINGS_TYPE_SECURE,
814 owningUserId, name);
815 }
Svetoslav683914b2015-01-15 14:22:26 -0800816
Svetoslav7ec28e82015-05-20 17:01:10 -0700817 case MUTATION_OPERATION_UPDATE: {
818 return mSettingsRegistry
819 .updateSettingLocked(SettingsRegistry.SETTINGS_TYPE_SECURE,
820 owningUserId, name, value, getCallingPackage());
821 }
Svetoslav683914b2015-01-15 14:22:26 -0800822 }
823 }
824
825 return false;
826 }
827
Svetoslav7ec28e82015-05-20 17:01:10 -0700828 private Cursor getAllSystemSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800829 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700830 Slog.v(LOG_TAG, "getAllSecureSystem(" + userId + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800831 }
832
833 // Resolve the userId on whose behalf the call is made.
834 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
835
Svetoslav7ec28e82015-05-20 17:01:10 -0700836 synchronized (mLock) {
837 List<String> names = mSettingsRegistry.getSettingsNamesLocked(
838 SettingsRegistry.SETTINGS_TYPE_SYSTEM, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -0800839
Svetoslav7ec28e82015-05-20 17:01:10 -0700840 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -0800841
Svetoslav7ec28e82015-05-20 17:01:10 -0700842 String[] normalizedProjection = normalizeProjection(projection);
843 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -0800844
Svetoslav7ec28e82015-05-20 17:01:10 -0700845 for (int i = 0; i < nameCount; i++) {
846 String name = names.get(i);
Svetoslav683914b2015-01-15 14:22:26 -0800847
Svetoslav7ec28e82015-05-20 17:01:10 -0700848 // Determine the owning user as some profile settings are cloned from the parent.
849 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId,
850 name);
Svetoslav683914b2015-01-15 14:22:26 -0800851
Svetoslav7ec28e82015-05-20 17:01:10 -0700852 Setting setting = mSettingsRegistry.getSettingLocked(
853 SettingsRegistry.SETTINGS_TYPE_SYSTEM, owningUserId, name);
854 appendSettingToCursor(result, setting);
855 }
856
857 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 getSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800862 if (DEBUG) {
863 Slog.v(LOG_TAG, "getSystemSetting(" + 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 = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
871
872 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700873 synchronized (mLock) {
874 return mSettingsRegistry.getSettingLocked(SettingsRegistry.SETTINGS_TYPE_SYSTEM,
875 owningUserId, name);
876 }
Svetoslav683914b2015-01-15 14:22:26 -0800877 }
878
Svetoslav7ec28e82015-05-20 17:01:10 -0700879 private boolean insertSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800880 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700881 Slog.v(LOG_TAG, "insertSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -0800882 + requestingUserId + ")");
883 }
884
Svetoslav7ec28e82015-05-20 17:01:10 -0700885 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT);
Svetoslav683914b2015-01-15 14:22:26 -0800886 }
887
Svetoslav7ec28e82015-05-20 17:01:10 -0700888 private boolean deleteSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800889 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700890 Slog.v(LOG_TAG, "deleteSystemSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800891 }
892
Svetoslav7ec28e82015-05-20 17:01:10 -0700893 return mutateSystemSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE);
Svetoslav683914b2015-01-15 14:22:26 -0800894 }
895
Svetoslav7ec28e82015-05-20 17:01:10 -0700896 private boolean updateSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800897 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700898 Slog.v(LOG_TAG, "updateSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -0800899 + requestingUserId + ")");
900 }
901
Svetoslav7ec28e82015-05-20 17:01:10 -0700902 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE);
Svetoslav683914b2015-01-15 14:22:26 -0800903 }
904
Svetoslav7ec28e82015-05-20 17:01:10 -0700905 private boolean mutateSystemSetting(String name, String value, int runAsUserId,
Svetoslav683914b2015-01-15 14:22:26 -0800906 int operation) {
907 // Make sure the caller can change the settings.
908 enforceWritePermission(Manifest.permission.WRITE_SETTINGS);
909
910 // Verify whether this operation is allowed for the calling package.
911 if (!isAppOpWriteSettingsAllowedForCallingPackage()) {
912 return false;
913 }
914
915 // Enforce what the calling package can mutate in the system settings.
916 enforceRestrictedSystemSettingsMutationForCallingPackageLocked(operation, name);
917
918 // Resolve the userId on whose behalf the call is made.
919 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId);
920
921 // Determine the owning user as some profile settings are cloned from the parent.
922 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
923
924 // Only the owning user id can change the setting.
925 if (owningUserId != callingUserId) {
926 return false;
927 }
928
929 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700930 synchronized (mLock) {
931 switch (operation) {
932 case MUTATION_OPERATION_INSERT: {
933 validateSystemSettingValue(name, value);
934 return mSettingsRegistry
935 .insertSettingLocked(SettingsRegistry.SETTINGS_TYPE_SYSTEM,
936 owningUserId, name, value, getCallingPackage());
937 }
938
939 case MUTATION_OPERATION_DELETE: {
940 return mSettingsRegistry.deleteSettingLocked(
941 SettingsRegistry.SETTINGS_TYPE_SYSTEM,
942 owningUserId, name);
943 }
944
945 case MUTATION_OPERATION_UPDATE: {
946 validateSystemSettingValue(name, value);
947 return mSettingsRegistry
948 .updateSettingLocked(SettingsRegistry.SETTINGS_TYPE_SYSTEM,
949 owningUserId, name, value, getCallingPackage());
950 }
Svetoslav683914b2015-01-15 14:22:26 -0800951 }
952
Svetoslav7ec28e82015-05-20 17:01:10 -0700953 return false;
Svetoslav683914b2015-01-15 14:22:26 -0800954 }
Svetoslav683914b2015-01-15 14:22:26 -0800955 }
956
957 private void validateSystemSettingValue(String name, String value) {
958 Settings.System.Validator validator = Settings.System.VALIDATORS.get(name);
959 if (validator != null && !validator.validate(value)) {
960 throw new IllegalArgumentException("Invalid value: " + value
961 + " for setting: " + name);
962 }
963 }
964
965 private boolean isLocationProvidersAllowedRestricted(String name, int callingUserId,
966 int owningUserId) {
967 // Optimization - location providers are restricted only for managed profiles.
968 if (callingUserId == owningUserId) {
969 return false;
970 }
971 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)
972 && mUserManager.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION,
973 new UserHandle(callingUserId))) {
974 return true;
975 }
976 return false;
977 }
978
979 private boolean isGlobalOrSecureSettingRestrictedForUser(String setting, int userId) {
980 String restriction = sSettingToUserRestrictionMap.get(setting);
981 if (restriction == null) {
982 return false;
983 }
984 return mUserManager.hasUserRestriction(restriction, new UserHandle(userId));
985 }
986
987 private int resolveOwningUserIdForSecureSettingLocked(int userId, String setting) {
988 return resolveOwningUserIdLocked(userId, sSecureCloneToManagedSettings, setting);
989 }
990
991 private int resolveOwningUserIdForSystemSettingLocked(int userId, String setting) {
992 return resolveOwningUserIdLocked(userId, sSystemCloneToManagedSettings, setting);
993 }
994
995 private int resolveOwningUserIdLocked(int userId, Set<String> keys, String name) {
996 final int parentId = getGroupParentLocked(userId);
997 if (parentId != userId && keys.contains(name)) {
998 return parentId;
999 }
1000 return userId;
1001 }
1002
1003 private void enforceRestrictedSystemSettingsMutationForCallingPackageLocked(int operation,
1004 String name) {
1005 // System/root/shell can mutate whatever secure settings they want.
1006 final int callingUid = Binder.getCallingUid();
1007 if (callingUid == android.os.Process.SYSTEM_UID
1008 || callingUid == Process.SHELL_UID
1009 || callingUid == Process.ROOT_UID) {
1010 return;
1011 }
1012
1013 switch (operation) {
1014 case MUTATION_OPERATION_INSERT:
1015 // Insert updates.
1016 case MUTATION_OPERATION_UPDATE: {
1017 if (Settings.System.PUBLIC_SETTINGS.contains(name)) {
1018 return;
1019 }
1020
1021 // The calling package is already verified.
1022 PackageInfo packageInfo = getCallingPackageInfoOrThrow();
1023
1024 // Privileged apps can do whatever they want.
1025 if ((packageInfo.applicationInfo.privateFlags
1026 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1027 return;
1028 }
1029
1030 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1031 packageInfo.applicationInfo.targetSdkVersion, name);
1032 } break;
1033
1034 case MUTATION_OPERATION_DELETE: {
1035 if (Settings.System.PUBLIC_SETTINGS.contains(name)
1036 || Settings.System.PRIVATE_SETTINGS.contains(name)) {
1037 throw new IllegalArgumentException("You cannot delete system defined"
1038 + " secure settings.");
1039 }
1040
1041 // The calling package is already verified.
1042 PackageInfo packageInfo = getCallingPackageInfoOrThrow();
1043
1044 // Privileged apps can do whatever they want.
1045 if ((packageInfo.applicationInfo.privateFlags &
1046 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1047 return;
1048 }
1049
1050 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1051 packageInfo.applicationInfo.targetSdkVersion, name);
1052 } break;
1053 }
1054 }
1055
1056 private PackageInfo getCallingPackageInfoOrThrow() {
1057 try {
1058 return mPackageManager.getPackageInfo(getCallingPackage(), 0);
1059 } catch (PackageManager.NameNotFoundException e) {
1060 throw new IllegalStateException("Calling package doesn't exist");
1061 }
1062 }
1063
1064 private int getGroupParentLocked(int userId) {
1065 // Most frequent use case.
1066 if (userId == UserHandle.USER_OWNER) {
1067 return userId;
1068 }
1069 // We are in the same process with the user manager and the returned
1070 // user info is a cached instance, so just look up instead of cache.
1071 final long identity = Binder.clearCallingIdentity();
1072 try {
Svetoslav7ec28e82015-05-20 17:01:10 -07001073 // Just a lookup and not reentrant, so holding a lock is fine.
Svetoslav683914b2015-01-15 14:22:26 -08001074 UserInfo userInfo = mUserManager.getProfileParent(userId);
1075 return (userInfo != null) ? userInfo.id : userId;
1076 } finally {
1077 Binder.restoreCallingIdentity(identity);
1078 }
1079 }
1080
1081 private boolean isAppOpWriteSettingsAllowedForCallingPackage() {
1082 final int callingUid = Binder.getCallingUid();
1083
1084 mAppOpsManager.checkPackage(Binder.getCallingUid(), getCallingPackage());
1085
1086 return mAppOpsManager.noteOp(AppOpsManager.OP_WRITE_SETTINGS, callingUid,
1087 getCallingPackage()) == AppOpsManager.MODE_ALLOWED;
1088 }
1089
1090 private void enforceWritePermission(String permission) {
1091 if (getContext().checkCallingOrSelfPermission(permission)
1092 != PackageManager.PERMISSION_GRANTED) {
1093 throw new SecurityException("Permission denial: writing to settings requires:"
1094 + permission);
1095 }
1096 }
1097
1098 /*
1099 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
1100 * This setting contains a list of the currently enabled location providers.
1101 * But helper functions in android.providers.Settings can enable or disable
1102 * a single provider by using a "+" or "-" prefix before the provider name.
1103 *
1104 * @returns whether the enabled location providers changed.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001105 */
Svetoslavb596a2c2015-02-17 21:37:09 -08001106 private boolean updateLocationProvidersAllowedLocked(String value, int owningUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001107 if (TextUtils.isEmpty(value)) {
1108 return false;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001109 }
1110
Svetoslav683914b2015-01-15 14:22:26 -08001111 final char prefix = value.charAt(0);
1112 if (prefix != '+' && prefix != '-') {
1113 return false;
1114 }
1115
1116 // skip prefix
1117 value = value.substring(1);
1118
Svetoslav7ec28e82015-05-20 17:01:10 -07001119 Setting settingValue = getSecureSetting(
Svetoslav683914b2015-01-15 14:22:26 -08001120 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, owningUserId);
1121
1122 String oldProviders = (settingValue != null) ? settingValue.getValue() : "";
1123
1124 int index = oldProviders.indexOf(value);
1125 int end = index + value.length();
1126
1127 // check for commas to avoid matching on partial string
1128 if (index > 0 && oldProviders.charAt(index - 1) != ',') {
1129 index = -1;
1130 }
1131
1132 // check for commas to avoid matching on partial string
1133 if (end < oldProviders.length() && oldProviders.charAt(end) != ',') {
1134 index = -1;
1135 }
1136
1137 String newProviders;
1138
1139 if (prefix == '+' && index < 0) {
1140 // append the provider to the list if not present
1141 if (oldProviders.length() == 0) {
1142 newProviders = value;
1143 } else {
1144 newProviders = oldProviders + ',' + value;
1145 }
1146 } else if (prefix == '-' && index >= 0) {
1147 // remove the provider from the list if present
1148 // remove leading or trailing comma
1149 if (index > 0) {
1150 index--;
1151 } else if (end < oldProviders.length()) {
1152 end++;
1153 }
1154
1155 newProviders = oldProviders.substring(0, index);
1156 if (end < oldProviders.length()) {
1157 newProviders += oldProviders.substring(end);
1158 }
1159 } else {
1160 // nothing changed, so no need to update the database
1161 return false;
1162 }
1163
Svetoslavb596a2c2015-02-17 21:37:09 -08001164 return mSettingsRegistry.insertSettingLocked(SettingsRegistry.SETTINGS_TYPE_SECURE,
1165 owningUserId, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, newProviders,
1166 getCallingPackage());
Svetoslav683914b2015-01-15 14:22:26 -08001167 }
1168
1169 private void sendNotify(Uri uri, int userId) {
1170 final long identity = Binder.clearCallingIdentity();
1171 try {
1172 getContext().getContentResolver().notifyChange(uri, null, true, userId);
1173 if (DEBUG) {
1174 Slog.v(LOG_TAG, "Notifying for " + userId + ": " + uri);
1175 }
1176 } finally {
1177 Binder.restoreCallingIdentity(identity);
1178 }
1179 }
1180
1181 private static void warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1182 int targetSdkVersion, String name) {
1183 // If the app targets Lollipop MR1 or older SDK we warn, otherwise crash.
1184 if (targetSdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) {
1185 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
1186 Slog.w(LOG_TAG, "You shouldn't not change private system settings."
1187 + " This will soon become an error.");
1188 } else {
1189 Slog.w(LOG_TAG, "You shouldn't keep your settings in the secure settings."
1190 + " This will soon become an error.");
1191 }
1192 } else {
1193 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
1194 throw new IllegalArgumentException("You cannot change private secure settings.");
1195 } else {
1196 throw new IllegalArgumentException("You cannot keep your settings in"
1197 + " the secure settings.");
1198 }
1199 }
1200 }
1201
1202 private static int resolveCallingUserIdEnforcingPermissionsLocked(int requestingUserId) {
1203 if (requestingUserId == UserHandle.getCallingUserId()) {
1204 return requestingUserId;
1205 }
1206 return ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1207 Binder.getCallingUid(), requestingUserId, false, true,
1208 "get/set setting for user", null);
1209 }
1210
1211 private static Bundle packageValueForCallResult(Setting setting) {
1212 if (setting == null) {
1213 return NULL_SETTING;
1214 }
1215 return Bundle.forPair(Settings.NameValueTable.VALUE, setting.getValue());
1216 }
1217
1218 private static int getRequestingUserId(Bundle args) {
1219 final int callingUserId = UserHandle.getCallingUserId();
1220 return (args != null) ? args.getInt(Settings.CALL_METHOD_USER_KEY, callingUserId)
1221 : callingUserId;
1222 }
1223
1224 private static String getSettingValue(Bundle args) {
1225 return (args != null) ? args.getString(Settings.NameValueTable.VALUE) : null;
1226 }
1227
1228 private static String getValidTableOrThrow(Uri uri) {
1229 if (uri.getPathSegments().size() > 0) {
1230 String table = uri.getPathSegments().get(0);
1231 if (DatabaseHelper.isValidTable(table)) {
1232 return table;
1233 }
1234 throw new IllegalArgumentException("Bad root path: " + table);
1235 }
1236 throw new IllegalArgumentException("Invalid URI:" + uri);
1237 }
1238
1239 private static MatrixCursor packageSettingForQuery(Setting setting, String[] projection) {
1240 if (setting == null) {
1241 return new MatrixCursor(projection, 0);
1242 }
1243 MatrixCursor cursor = new MatrixCursor(projection, 1);
1244 appendSettingToCursor(cursor, setting);
1245 return cursor;
1246 }
1247
1248 private static String[] normalizeProjection(String[] projection) {
1249 if (projection == null) {
1250 return ALL_COLUMNS;
1251 }
1252
1253 final int columnCount = projection.length;
1254 for (int i = 0; i < columnCount; i++) {
1255 String column = projection[i];
1256 if (!ArrayUtils.contains(ALL_COLUMNS, column)) {
1257 throw new IllegalArgumentException("Invalid column: " + column);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001258 }
1259 }
1260
Svetoslav683914b2015-01-15 14:22:26 -08001261 return projection;
1262 }
1263
1264 private static void appendSettingToCursor(MatrixCursor cursor, Setting setting) {
1265 final int columnCount = cursor.getColumnCount();
1266
1267 String[] values = new String[columnCount];
1268
1269 for (int i = 0; i < columnCount; i++) {
1270 String column = cursor.getColumnName(i);
1271
1272 switch (column) {
1273 case Settings.NameValueTable._ID: {
1274 values[i] = setting.getId();
1275 } break;
1276
1277 case Settings.NameValueTable.NAME: {
1278 values[i] = setting.getName();
1279 } break;
1280
1281 case Settings.NameValueTable.VALUE: {
1282 values[i] = setting.getValue();
1283 } break;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001284 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001285 }
1286
Svetoslav683914b2015-01-15 14:22:26 -08001287 cursor.addRow(values);
1288 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001289
Makoto Onuki3a2c35782015-06-18 11:21:58 -07001290 private static boolean isKeyValid(String key) {
1291 return !(TextUtils.isEmpty(key) || SettingsState.isBinary(key));
1292 }
1293
Svetoslav683914b2015-01-15 14:22:26 -08001294 private static final class Arguments {
1295 private static final Pattern WHERE_PATTERN_WITH_PARAM_NO_BRACKETS =
1296 Pattern.compile("[\\s]*name[\\s]*=[\\s]*\\?[\\s]*");
1297
1298 private static final Pattern WHERE_PATTERN_WITH_PARAM_IN_BRACKETS =
1299 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*\\?[\\s]*\\)[\\s]*");
1300
1301 private static final Pattern WHERE_PATTERN_NO_PARAM_IN_BRACKETS =
1302 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*\\)[\\s]*");
1303
1304 private static final Pattern WHERE_PATTERN_NO_PARAM_NO_BRACKETS =
1305 Pattern.compile("[\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*");
1306
1307 public final String table;
1308 public final String name;
1309
1310 public Arguments(Uri uri, String where, String[] whereArgs, boolean supportAll) {
1311 final int segmentSize = uri.getPathSegments().size();
1312 switch (segmentSize) {
1313 case 1: {
1314 if (where != null
1315 && (WHERE_PATTERN_WITH_PARAM_NO_BRACKETS.matcher(where).matches()
1316 || WHERE_PATTERN_WITH_PARAM_IN_BRACKETS.matcher(where).matches())
1317 && whereArgs.length == 1) {
1318 name = whereArgs[0];
1319 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08001320 return;
Svetoslav683914b2015-01-15 14:22:26 -08001321 } else if (where != null
1322 && (WHERE_PATTERN_NO_PARAM_NO_BRACKETS.matcher(where).matches()
1323 || WHERE_PATTERN_NO_PARAM_IN_BRACKETS.matcher(where).matches())) {
1324 final int startIndex = Math.max(where.indexOf("'"),
1325 where.indexOf("\"")) + 1;
1326 final int endIndex = Math.max(where.lastIndexOf("'"),
1327 where.lastIndexOf("\""));
1328 name = where.substring(startIndex, endIndex);
1329 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08001330 return;
Svetoslav683914b2015-01-15 14:22:26 -08001331 } else if (supportAll && where == null && whereArgs == null) {
1332 name = null;
1333 table = computeTableForSetting(uri, null);
Svetoslav28494652015-02-12 14:11:42 -08001334 return;
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001335 }
Svetoslav683914b2015-01-15 14:22:26 -08001336 } break;
1337
Svetoslav28494652015-02-12 14:11:42 -08001338 case 2: {
1339 if (where == null && whereArgs == null) {
1340 name = uri.getPathSegments().get(1);
1341 table = computeTableForSetting(uri, name);
1342 return;
1343 }
1344 } break;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001345 }
Svetoslav28494652015-02-12 14:11:42 -08001346
1347 EventLogTags.writeUnsupportedSettingsQuery(
1348 uri.toSafeString(), where, Arrays.toString(whereArgs));
1349 String message = String.format( "Supported SQL:\n"
1350 + " uri content://some_table/some_property with null where and where args\n"
1351 + " uri content://some_table with query name=? and single name as arg\n"
1352 + " uri content://some_table with query name=some_name and null args\n"
1353 + " but got - uri:%1s, where:%2s whereArgs:%3s", uri, where,
1354 Arrays.toString(whereArgs));
1355 throw new IllegalArgumentException(message);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001356 }
1357
Svetoslav28494652015-02-12 14:11:42 -08001358 private static String computeTableForSetting(Uri uri, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08001359 String table = getValidTableOrThrow(uri);
1360
1361 if (name != null) {
1362 if (sSystemMovedToSecureSettings.contains(name)) {
1363 table = TABLE_SECURE;
1364 }
1365
1366 if (sSystemMovedToGlobalSettings.contains(name)) {
1367 table = TABLE_GLOBAL;
1368 }
1369
1370 if (sSecureMovedToGlobalSettings.contains(name)) {
1371 table = TABLE_GLOBAL;
1372 }
1373
1374 if (sGlobalMovedToSecureSettings.contains(name)) {
1375 table = TABLE_SECURE;
1376 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001377 }
Svetoslav683914b2015-01-15 14:22:26 -08001378
1379 return table;
1380 }
1381 }
1382
1383 final class SettingsRegistry {
1384 private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid";
1385
1386 private static final int SETTINGS_TYPE_GLOBAL = 0;
1387 private static final int SETTINGS_TYPE_SYSTEM = 1;
1388 private static final int SETTINGS_TYPE_SECURE = 2;
1389
1390 private static final int SETTINGS_TYPE_MASK = 0xF0000000;
1391 private static final int SETTINGS_TYPE_SHIFT = 28;
1392
1393 private static final String SETTINGS_FILE_GLOBAL = "settings_global.xml";
1394 private static final String SETTINGS_FILE_SYSTEM = "settings_system.xml";
1395 private static final String SETTINGS_FILE_SECURE = "settings_secure.xml";
1396
1397 private final SparseArray<SettingsState> mSettingsStates = new SparseArray<>();
1398
1399 private final BackupManager mBackupManager;
1400
1401 public SettingsRegistry() {
1402 mBackupManager = new BackupManager(getContext());
1403 migrateAllLegacySettingsIfNeeded();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001404 }
1405
Svetoslav683914b2015-01-15 14:22:26 -08001406 public List<String> getSettingsNamesLocked(int type, int userId) {
1407 final int key = makeKey(type, userId);
1408 SettingsState settingsState = peekSettingsStateLocked(key);
1409 return settingsState.getSettingNamesLocked();
1410 }
1411
1412 public SettingsState getSettingsLocked(int type, int userId) {
1413 final int key = makeKey(type, userId);
1414 return peekSettingsStateLocked(key);
1415 }
1416
1417 public void ensureSettingsForUserLocked(int userId) {
1418 // Migrate the setting for this user if needed.
1419 migrateLegacySettingsForUserIfNeededLocked(userId);
1420
1421 // Ensure global settings loaded if owner.
1422 if (userId == UserHandle.USER_OWNER) {
1423 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_OWNER);
1424 ensureSettingsStateLocked(globalKey);
1425 }
1426
1427 // Ensure secure settings loaded.
1428 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1429 ensureSettingsStateLocked(secureKey);
1430
1431 // Make sure the secure settings have an Android id set.
1432 SettingsState secureSettings = getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
1433 ensureSecureSettingAndroidIdSetLocked(secureSettings);
1434
1435 // Ensure system settings loaded.
1436 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1437 ensureSettingsStateLocked(systemKey);
1438
1439 // Upgrade the settings to the latest version.
1440 UpgradeController upgrader = new UpgradeController(userId);
1441 upgrader.upgradeIfNeededLocked();
1442 }
1443
1444 private void ensureSettingsStateLocked(int key) {
1445 if (mSettingsStates.get(key) == null) {
1446 final int maxBytesPerPackage = getMaxBytesPerPackageForType(getTypeFromKey(key));
1447 SettingsState settingsState = new SettingsState(mLock, getSettingsFile(key), key,
1448 maxBytesPerPackage);
1449 mSettingsStates.put(key, settingsState);
1450 }
1451 }
1452
1453 public void removeUserStateLocked(int userId, boolean permanently) {
1454 // We always keep the global settings in memory.
1455
1456 // Nuke system settings.
1457 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1458 final SettingsState systemSettingsState = mSettingsStates.get(systemKey);
1459 if (systemSettingsState != null) {
1460 if (permanently) {
1461 mSettingsStates.remove(systemKey);
1462 systemSettingsState.destroyLocked(null);
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001463 } else {
Svetoslav683914b2015-01-15 14:22:26 -08001464 systemSettingsState.destroyLocked(new Runnable() {
1465 @Override
1466 public void run() {
1467 mSettingsStates.remove(systemKey);
1468 }
1469 });
1470 }
1471 }
1472
1473 // Nuke secure settings.
1474 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1475 final SettingsState secureSettingsState = mSettingsStates.get(secureKey);
1476 if (secureSettingsState != null) {
1477 if (permanently) {
1478 mSettingsStates.remove(secureKey);
1479 secureSettingsState.destroyLocked(null);
1480 } else {
1481 secureSettingsState.destroyLocked(new Runnable() {
1482 @Override
1483 public void run() {
1484 mSettingsStates.remove(secureKey);
1485 }
1486 });
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001487 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001488 }
1489 }
1490
Svetoslav683914b2015-01-15 14:22:26 -08001491 public boolean insertSettingLocked(int type, int userId, String name, String value,
1492 String packageName) {
1493 final int key = makeKey(type, userId);
1494
1495 SettingsState settingsState = peekSettingsStateLocked(key);
1496 final boolean success = settingsState.insertSettingLocked(name, value, packageName);
1497
1498 if (success) {
1499 notifyForSettingsChange(key, name);
1500 }
1501 return success;
1502 }
1503
1504 public boolean deleteSettingLocked(int type, int userId, String name) {
1505 final int key = makeKey(type, userId);
1506
1507 SettingsState settingsState = peekSettingsStateLocked(key);
1508 final boolean success = settingsState.deleteSettingLocked(name);
1509
1510 if (success) {
1511 notifyForSettingsChange(key, name);
1512 }
1513 return success;
1514 }
1515
1516 public Setting getSettingLocked(int type, int userId, String name) {
1517 final int key = makeKey(type, userId);
1518
1519 SettingsState settingsState = peekSettingsStateLocked(key);
1520 return settingsState.getSettingLocked(name);
1521 }
1522
1523 public boolean updateSettingLocked(int type, int userId, String name, String value,
1524 String packageName) {
1525 final int key = makeKey(type, userId);
1526
1527 SettingsState settingsState = peekSettingsStateLocked(key);
1528 final boolean success = settingsState.updateSettingLocked(name, value, packageName);
1529
1530 if (success) {
1531 notifyForSettingsChange(key, name);
1532 }
1533
1534 return success;
1535 }
1536
1537 public void onPackageRemovedLocked(String packageName, int userId) {
Svet Ganov8de34802015-04-27 09:33:40 -07001538 // Global and secure settings are signature protected. Apps signed
1539 // by the platform certificate are generally not uninstalled and
1540 // the main exception is tests. We trust components signed
1541 // by the platform certificate and do not do a clean up after them.
Svetoslav683914b2015-01-15 14:22:26 -08001542
1543 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1544 SettingsState systemSettings = mSettingsStates.get(systemKey);
Svet Ganov8de34802015-04-27 09:33:40 -07001545 if (systemSettings != null) {
1546 systemSettings.onPackageRemovedLocked(packageName);
1547 }
Svetoslav683914b2015-01-15 14:22:26 -08001548 }
1549
1550 private SettingsState peekSettingsStateLocked(int key) {
1551 SettingsState settingsState = mSettingsStates.get(key);
1552 if (settingsState != null) {
1553 return settingsState;
1554 }
1555
1556 ensureSettingsForUserLocked(getUserIdFromKey(key));
1557 return mSettingsStates.get(key);
1558 }
1559
1560 private void migrateAllLegacySettingsIfNeeded() {
1561 synchronized (mLock) {
1562 final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_OWNER);
1563 File globalFile = getSettingsFile(key);
1564 if (globalFile.exists()) {
1565 return;
1566 }
1567
1568 final long identity = Binder.clearCallingIdentity();
1569 try {
1570 List<UserInfo> users = mUserManager.getUsers(true);
1571
1572 final int userCount = users.size();
1573 for (int i = 0; i < userCount; i++) {
1574 final int userId = users.get(i).id;
1575
1576 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
1577 SQLiteDatabase database = dbHelper.getWritableDatabase();
1578 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
1579
1580 // Upgrade to the latest version.
1581 UpgradeController upgrader = new UpgradeController(userId);
1582 upgrader.upgradeIfNeededLocked();
1583
1584 // Drop from memory if not a running user.
1585 if (!mUserManager.isUserRunning(new UserHandle(userId))) {
1586 removeUserStateLocked(userId, false);
1587 }
1588 }
1589 } finally {
1590 Binder.restoreCallingIdentity(identity);
1591 }
1592 }
1593 }
1594
1595 private void migrateLegacySettingsForUserIfNeededLocked(int userId) {
1596 // Every user has secure settings and if no file we need to migrate.
1597 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1598 File secureFile = getSettingsFile(secureKey);
1599 if (secureFile.exists()) {
1600 return;
1601 }
1602
1603 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
1604 SQLiteDatabase database = dbHelper.getWritableDatabase();
1605
1606 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
1607 }
1608
1609 private void migrateLegacySettingsForUserLocked(DatabaseHelper dbHelper,
1610 SQLiteDatabase database, int userId) {
1611 // Move over the global settings if owner.
1612 if (userId == UserHandle.USER_OWNER) {
1613 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, userId);
1614 ensureSettingsStateLocked(globalKey);
1615 SettingsState globalSettings = mSettingsStates.get(globalKey);
1616 migrateLegacySettingsLocked(globalSettings, database, TABLE_GLOBAL);
1617 globalSettings.persistSyncLocked();
1618 }
1619
1620 // Move over the secure settings.
1621 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1622 ensureSettingsStateLocked(secureKey);
1623 SettingsState secureSettings = mSettingsStates.get(secureKey);
1624 migrateLegacySettingsLocked(secureSettings, database, TABLE_SECURE);
1625 ensureSecureSettingAndroidIdSetLocked(secureSettings);
1626 secureSettings.persistSyncLocked();
1627
1628 // Move over the system settings.
1629 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1630 ensureSettingsStateLocked(systemKey);
1631 SettingsState systemSettings = mSettingsStates.get(systemKey);
1632 migrateLegacySettingsLocked(systemSettings, database, TABLE_SYSTEM);
1633 systemSettings.persistSyncLocked();
1634
1635 // Drop the database as now all is moved and persisted.
1636 if (DROP_DATABASE_ON_MIGRATION) {
1637 dbHelper.dropDatabase();
1638 } else {
1639 dbHelper.backupDatabase();
1640 }
1641 }
1642
1643 private void migrateLegacySettingsLocked(SettingsState settingsState,
1644 SQLiteDatabase database, String table) {
1645 SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
1646 queryBuilder.setTables(table);
1647
1648 Cursor cursor = queryBuilder.query(database, ALL_COLUMNS,
1649 null, null, null, null, null);
1650
1651 if (cursor == null) {
1652 return;
1653 }
1654
1655 try {
1656 if (!cursor.moveToFirst()) {
1657 return;
1658 }
1659
1660 final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME);
1661 final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE);
1662
1663 settingsState.setVersionLocked(database.getVersion());
1664
1665 while (!cursor.isAfterLast()) {
1666 String name = cursor.getString(nameColumnIdx);
1667 String value = cursor.getString(valueColumnIdx);
1668 settingsState.insertSettingLocked(name, value,
1669 SettingsState.SYSTEM_PACKAGE_NAME);
1670 cursor.moveToNext();
1671 }
1672 } finally {
1673 cursor.close();
1674 }
1675 }
1676
1677 private void ensureSecureSettingAndroidIdSetLocked(SettingsState secureSettings) {
1678 Setting value = secureSettings.getSettingLocked(Settings.Secure.ANDROID_ID);
1679
1680 if (value != null) {
1681 return;
1682 }
1683
1684 final int userId = getUserIdFromKey(secureSettings.mKey);
1685
1686 final UserInfo user;
1687 final long identity = Binder.clearCallingIdentity();
1688 try {
1689 user = mUserManager.getUserInfo(userId);
1690 } finally {
1691 Binder.restoreCallingIdentity(identity);
1692 }
1693 if (user == null) {
1694 // Can happen due to races when deleting users - treat as benign.
1695 return;
1696 }
1697
1698 String androidId = Long.toHexString(new SecureRandom().nextLong());
1699 secureSettings.insertSettingLocked(Settings.Secure.ANDROID_ID, androidId,
1700 SettingsState.SYSTEM_PACKAGE_NAME);
1701
1702 Slog.d(LOG_TAG, "Generated and saved new ANDROID_ID [" + androidId
1703 + "] for user " + userId);
1704
1705 // Write a drop box entry if it's a restricted profile
1706 if (user.isRestricted()) {
1707 DropBoxManager dbm = (DropBoxManager) getContext().getSystemService(
1708 Context.DROPBOX_SERVICE);
1709 if (dbm != null && dbm.isTagEnabled(DROPBOX_TAG_USERLOG)) {
1710 dbm.addText(DROPBOX_TAG_USERLOG, System.currentTimeMillis()
1711 + "," + DROPBOX_TAG_USERLOG + "," + androidId + "\n");
1712 }
1713 }
1714 }
1715
1716 private void notifyForSettingsChange(int key, String name) {
1717 // Update the system property *first*, so if someone is listening for
1718 // a notification and then using the contract class to get their data,
1719 // the system property will be updated and they'll get the new data.
1720
1721 boolean backedUpDataChanged = false;
1722 String property = null;
1723 if (isGlobalSettingsKey(key)) {
1724 property = Settings.Global.SYS_PROP_SETTING_VERSION;
1725 backedUpDataChanged = true;
1726 } else if (isSecureSettingsKey(key)) {
1727 property = Settings.Secure.SYS_PROP_SETTING_VERSION;
1728 backedUpDataChanged = true;
1729 } else if (isSystemSettingsKey(key)) {
1730 property = Settings.System.SYS_PROP_SETTING_VERSION;
1731 backedUpDataChanged = true;
1732 }
1733
1734 if (property != null) {
1735 final long version = SystemProperties.getLong(property, 0) + 1;
1736 SystemProperties.set(property, Long.toString(version));
1737 if (DEBUG) {
1738 Slog.v(LOG_TAG, "System property " + property + "=" + version);
1739 }
1740 }
1741
1742 // Inform the backup manager about a data change
1743 if (backedUpDataChanged) {
1744 mBackupManager.dataChanged();
1745 }
1746
1747 // Now send the notification through the content framework.
1748
1749 final int userId = getUserIdFromKey(key);
1750 Uri uri = getNotificationUriFor(key, name);
1751
1752 sendNotify(uri, userId);
1753 }
1754
1755 private int makeKey(int type, int userId) {
1756 return (type << SETTINGS_TYPE_SHIFT) | userId;
1757 }
1758
1759 private int getTypeFromKey(int key) {
1760 return key >> SETTINGS_TYPE_SHIFT;
1761 }
1762
1763 private int getUserIdFromKey(int key) {
1764 return key & ~SETTINGS_TYPE_MASK;
1765 }
1766
1767 private boolean isGlobalSettingsKey(int key) {
1768 return getTypeFromKey(key) == SETTINGS_TYPE_GLOBAL;
1769 }
1770
1771 private boolean isSystemSettingsKey(int key) {
1772 return getTypeFromKey(key) == SETTINGS_TYPE_SYSTEM;
1773 }
1774
1775 private boolean isSecureSettingsKey(int key) {
1776 return getTypeFromKey(key) == SETTINGS_TYPE_SECURE;
1777 }
1778
1779 private File getSettingsFile(int key) {
1780 if (isGlobalSettingsKey(key)) {
1781 final int userId = getUserIdFromKey(key);
1782 return new File(Environment.getUserSystemDirectory(userId),
1783 SETTINGS_FILE_GLOBAL);
1784 } else if (isSystemSettingsKey(key)) {
1785 final int userId = getUserIdFromKey(key);
1786 return new File(Environment.getUserSystemDirectory(userId),
1787 SETTINGS_FILE_SYSTEM);
1788 } else if (isSecureSettingsKey(key)) {
1789 final int userId = getUserIdFromKey(key);
1790 return new File(Environment.getUserSystemDirectory(userId),
1791 SETTINGS_FILE_SECURE);
1792 } else {
1793 throw new IllegalArgumentException("Invalid settings key:" + key);
1794 }
1795 }
1796
1797 private Uri getNotificationUriFor(int key, String name) {
1798 if (isGlobalSettingsKey(key)) {
1799 return (name != null) ? Uri.withAppendedPath(Settings.Global.CONTENT_URI, name)
1800 : Settings.Global.CONTENT_URI;
1801 } else if (isSecureSettingsKey(key)) {
1802 return (name != null) ? Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name)
1803 : Settings.Secure.CONTENT_URI;
1804 } else if (isSystemSettingsKey(key)) {
1805 return (name != null) ? Uri.withAppendedPath(Settings.System.CONTENT_URI, name)
1806 : Settings.System.CONTENT_URI;
1807 } else {
1808 throw new IllegalArgumentException("Invalid settings key:" + key);
1809 }
1810 }
1811
1812 private int getMaxBytesPerPackageForType(int type) {
1813 switch (type) {
1814 case SETTINGS_TYPE_GLOBAL:
1815 case SETTINGS_TYPE_SECURE: {
1816 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED;
1817 }
1818
1819 default: {
1820 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_LIMITED;
1821 }
1822 }
1823 }
1824
1825 private final class UpgradeController {
Makoto Onuki3a2c35782015-06-18 11:21:58 -07001826 private static final int SETTINGS_VERSION = 121;
Svetoslav683914b2015-01-15 14:22:26 -08001827
1828 private final int mUserId;
1829
1830 public UpgradeController(int userId) {
1831 mUserId = userId;
1832 }
1833
1834 public void upgradeIfNeededLocked() {
1835 // The version of all settings for a user is the same (all users have secure).
1836 SettingsState secureSettings = getSettingsLocked(
1837 SettingsRegistry.SETTINGS_TYPE_SECURE, mUserId);
1838
1839 // Try an update from the current state.
1840 final int oldVersion = secureSettings.getVersionLocked();
1841 final int newVersion = SETTINGS_VERSION;
1842
Svet Ganovc9755bc2015-03-28 13:21:22 -07001843 // If up do date - done.
Svetoslav683914b2015-01-15 14:22:26 -08001844 if (oldVersion == newVersion) {
1845 return;
1846 }
1847
1848 // Try to upgrade.
1849 final int curVersion = onUpgradeLocked(mUserId, oldVersion, newVersion);
1850
1851 // If upgrade failed start from scratch and upgrade.
1852 if (curVersion != newVersion) {
1853 // Drop state we have for this user.
1854 removeUserStateLocked(mUserId, true);
1855
1856 // Recreate the database.
1857 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), mUserId);
1858 SQLiteDatabase database = dbHelper.getWritableDatabase();
1859 dbHelper.recreateDatabase(database, newVersion, curVersion, oldVersion);
1860
1861 // Migrate the settings for this user.
1862 migrateLegacySettingsForUserLocked(dbHelper, database, mUserId);
1863
1864 // Now upgrade should work fine.
1865 onUpgradeLocked(mUserId, oldVersion, newVersion);
1866 }
1867
1868 // Set the global settings version if owner.
1869 if (mUserId == UserHandle.USER_OWNER) {
1870 SettingsState globalSettings = getSettingsLocked(
1871 SettingsRegistry.SETTINGS_TYPE_GLOBAL, mUserId);
1872 globalSettings.setVersionLocked(newVersion);
1873 }
1874
1875 // Set the secure settings version.
1876 secureSettings.setVersionLocked(newVersion);
1877
1878 // Set the system settings version.
1879 SettingsState systemSettings = getSettingsLocked(
1880 SettingsRegistry.SETTINGS_TYPE_SYSTEM, mUserId);
1881 systemSettings.setVersionLocked(newVersion);
1882 }
1883
1884 private SettingsState getGlobalSettingsLocked() {
1885 return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_OWNER);
1886 }
1887
1888 private SettingsState getSecureSettingsLocked(int userId) {
1889 return getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
1890 }
1891
1892 private SettingsState getSystemSettingsLocked(int userId) {
1893 return getSettingsLocked(SETTINGS_TYPE_SYSTEM, userId);
1894 }
1895
Jeff Brown503cffc2015-03-26 18:08:51 -07001896 /**
1897 * You must perform all necessary mutations to bring the settings
1898 * for this user from the old to the new version. When you add a new
1899 * upgrade step you *must* update SETTINGS_VERSION.
1900 *
1901 * This is an example of moving a setting from secure to global.
1902 *
1903 * // v119: Example settings changes.
1904 * if (currentVersion == 118) {
1905 * if (userId == UserHandle.USER_OWNER) {
1906 * // Remove from the secure settings.
1907 * SettingsState secureSettings = getSecureSettingsLocked(userId);
1908 * String name = "example_setting_to_move";
1909 * String value = secureSettings.getSetting(name);
1910 * secureSettings.deleteSetting(name);
1911 *
1912 * // Add to the global settings.
1913 * SettingsState globalSettings = getGlobalSettingsLocked();
1914 * globalSettings.insertSetting(name, value, SettingsState.SYSTEM_PACKAGE_NAME);
1915 * }
1916 *
1917 * // Update the current version.
1918 * currentVersion = 119;
1919 * }
1920 */
Svetoslav683914b2015-01-15 14:22:26 -08001921 private int onUpgradeLocked(int userId, int oldVersion, int newVersion) {
1922 if (DEBUG) {
1923 Slog.w(LOG_TAG, "Upgrading settings for user: " + userId + " from version: "
1924 + oldVersion + " to version: " + newVersion);
1925 }
1926
Jeff Brown503cffc2015-03-26 18:08:51 -07001927 int currentVersion = oldVersion;
Svetoslav683914b2015-01-15 14:22:26 -08001928
John Spurlocke11ae112015-05-11 16:09:03 -04001929 // v119: Reset zen + ringer mode.
1930 if (currentVersion == 118) {
1931 if (userId == UserHandle.USER_OWNER) {
1932 final SettingsState globalSettings = getGlobalSettingsLocked();
1933 globalSettings.updateSettingLocked(Settings.Global.ZEN_MODE,
1934 Integer.toString(Settings.Global.ZEN_MODE_OFF),
1935 SettingsState.SYSTEM_PACKAGE_NAME);
1936 globalSettings.updateSettingLocked(Settings.Global.MODE_RINGER,
1937 Integer.toString(AudioManager.RINGER_MODE_NORMAL),
1938 SettingsState.SYSTEM_PACKAGE_NAME);
1939 }
1940 currentVersion = 119;
1941 }
1942
Jason Monk27bbb2d2015-03-31 16:46:39 -04001943 // v120: Add double tap to wake setting.
1944 if (currentVersion == 119) {
1945 SettingsState secureSettings = getSecureSettingsLocked(userId);
1946 secureSettings.insertSettingLocked(Settings.Secure.DOUBLE_TAP_TO_WAKE,
1947 getContext().getResources().getBoolean(
1948 R.bool.def_double_tap_to_wake) ? "1" : "0",
1949 SettingsState.SYSTEM_PACKAGE_NAME);
1950
1951 currentVersion = 120;
1952 }
1953
Makoto Onuki3a2c35782015-06-18 11:21:58 -07001954 // Before 121, we used a different string encoding logic. We just bump the version
1955 // here; SettingsState knows how to handle pre-version 120 files.
1956 currentVersion = 121;
1957
Jeff Brown503cffc2015-03-26 18:08:51 -07001958 // vXXX: Add new settings above this point.
Svetoslav683914b2015-01-15 14:22:26 -08001959
Jeff Brown503cffc2015-03-26 18:08:51 -07001960 // Return the current version.
1961 return currentVersion;
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -08001962 }
1963 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001964 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001965}