blob: 3e9b12266733383fd879db91c676db3675493b06 [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
Svetoslav683914b2015-01-15 14:22:26 -0800646 // Resolve the userId on whose behalf the call is made.
647 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
648
649 // If this is a setting that is currently restricted for this user, done.
650 if (isGlobalOrSecureSettingRestrictedForUser(name, callingUserId)) {
651 return false;
652 }
653
654 // Perform the mutation.
Svetoslav7ec28e82015-05-20 17:01:10 -0700655 synchronized (mLock) {
656 switch (operation) {
657 case MUTATION_OPERATION_INSERT: {
658 return mSettingsRegistry
659 .insertSettingLocked(SettingsRegistry.SETTINGS_TYPE_GLOBAL,
660 UserHandle.USER_OWNER, name, value, getCallingPackage());
661 }
Svetoslav683914b2015-01-15 14:22:26 -0800662
Svetoslav7ec28e82015-05-20 17:01:10 -0700663 case MUTATION_OPERATION_DELETE: {
664 return mSettingsRegistry.deleteSettingLocked(
665 SettingsRegistry.SETTINGS_TYPE_GLOBAL,
666 UserHandle.USER_OWNER, name);
667 }
Svetoslav683914b2015-01-15 14:22:26 -0800668
Svetoslav7ec28e82015-05-20 17:01:10 -0700669 case MUTATION_OPERATION_UPDATE: {
670 return mSettingsRegistry
671 .updateSettingLocked(SettingsRegistry.SETTINGS_TYPE_GLOBAL,
672 UserHandle.USER_OWNER, name, value, getCallingPackage());
673 }
Svetoslav683914b2015-01-15 14:22:26 -0800674 }
675 }
676
677 return false;
678 }
679
Svetoslav7ec28e82015-05-20 17:01:10 -0700680 private Cursor getAllSecureSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800681 if (DEBUG) {
682 Slog.v(LOG_TAG, "getAllSecureSettings(" + userId + ")");
683 }
684
685 // Resolve the userId on whose behalf the call is made.
686 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
687
Svetoslav7ec28e82015-05-20 17:01:10 -0700688 synchronized (mLock) {
689 List<String> names = mSettingsRegistry.getSettingsNamesLocked(
690 SettingsRegistry.SETTINGS_TYPE_SECURE, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -0800691
Svetoslav7ec28e82015-05-20 17:01:10 -0700692 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -0800693
Svetoslav7ec28e82015-05-20 17:01:10 -0700694 String[] normalizedProjection = normalizeProjection(projection);
695 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -0800696
Svetoslav7ec28e82015-05-20 17:01:10 -0700697 for (int i = 0; i < nameCount; i++) {
698 String name = names.get(i);
699 // Determine the owning user as some profile settings are cloned from the parent.
700 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
701 name);
Svetoslav683914b2015-01-15 14:22:26 -0800702
Svetoslav7ec28e82015-05-20 17:01:10 -0700703 // Special case for location (sigh).
704 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
705 return null;
706 }
Svetoslav683914b2015-01-15 14:22:26 -0800707
Svetoslav7ec28e82015-05-20 17:01:10 -0700708 Setting setting = mSettingsRegistry.getSettingLocked(
709 SettingsRegistry.SETTINGS_TYPE_SECURE, owningUserId, name);
710 appendSettingToCursor(result, setting);
Svetoslav683914b2015-01-15 14:22:26 -0800711 }
712
Svetoslav7ec28e82015-05-20 17:01:10 -0700713 return result;
Svetoslav683914b2015-01-15 14:22:26 -0800714 }
Svetoslav683914b2015-01-15 14:22:26 -0800715 }
716
Svetoslav7ec28e82015-05-20 17:01:10 -0700717 private Setting getSecureSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800718 if (DEBUG) {
719 Slog.v(LOG_TAG, "getSecureSetting(" + name + ", " + requestingUserId + ")");
720 }
721
722 // Resolve the userId on whose behalf the call is made.
723 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
724
725 // Determine the owning user as some profile settings are cloned from the parent.
726 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
727
728 // Special case for location (sigh).
729 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
730 return null;
731 }
732
733 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700734 synchronized (mLock) {
735 return mSettingsRegistry.getSettingLocked(SettingsRegistry.SETTINGS_TYPE_SECURE,
736 owningUserId, name);
737 }
Svetoslav683914b2015-01-15 14:22:26 -0800738 }
739
Svetoslav7ec28e82015-05-20 17:01:10 -0700740 private boolean insertSecureSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800741 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700742 Slog.v(LOG_TAG, "insertSecureSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -0800743 + requestingUserId + ")");
744 }
745
Svetoslav7ec28e82015-05-20 17:01:10 -0700746 return mutateSecureSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT);
Svetoslav683914b2015-01-15 14:22:26 -0800747 }
748
Svetoslav7ec28e82015-05-20 17:01:10 -0700749 private boolean deleteSecureSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800750 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700751 Slog.v(LOG_TAG, "deleteSecureSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800752 }
753
Svetoslav7ec28e82015-05-20 17:01:10 -0700754 return mutateSecureSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE);
Svetoslav683914b2015-01-15 14:22:26 -0800755 }
756
Svetoslav7ec28e82015-05-20 17:01:10 -0700757 private boolean updateSecureSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800758 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700759 Slog.v(LOG_TAG, "updateSecureSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -0800760 + requestingUserId + ")");
761 }
762
Svetoslav7ec28e82015-05-20 17:01:10 -0700763 return mutateSecureSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE);
Svetoslav683914b2015-01-15 14:22:26 -0800764 }
765
Svetoslav7ec28e82015-05-20 17:01:10 -0700766 private boolean mutateSecureSetting(String name, String value, int requestingUserId,
Svetoslav683914b2015-01-15 14:22:26 -0800767 int operation) {
768 // Make sure the caller can change the settings.
769 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
770
Svetoslav683914b2015-01-15 14:22:26 -0800771 // Resolve the userId on whose behalf the call is made.
772 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
773
774 // If this is a setting that is currently restricted for this user, done.
775 if (isGlobalOrSecureSettingRestrictedForUser(name, callingUserId)) {
776 return false;
777 }
778
779 // Determine the owning user as some profile settings are cloned from the parent.
780 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
781
782 // Only the owning user can change the setting.
783 if (owningUserId != callingUserId) {
784 return false;
785 }
786
787 // Special cases for location providers (sigh).
788 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
Svetoslavb596a2c2015-02-17 21:37:09 -0800789 return updateLocationProvidersAllowedLocked(value, owningUserId);
Svetoslav683914b2015-01-15 14:22:26 -0800790 }
791
792 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700793 synchronized (mLock) {
794 switch (operation) {
795 case MUTATION_OPERATION_INSERT: {
796 return mSettingsRegistry
797 .insertSettingLocked(SettingsRegistry.SETTINGS_TYPE_SECURE,
798 owningUserId, name, value, getCallingPackage());
799 }
Svetoslav683914b2015-01-15 14:22:26 -0800800
Svetoslav7ec28e82015-05-20 17:01:10 -0700801 case MUTATION_OPERATION_DELETE: {
802 return mSettingsRegistry.deleteSettingLocked(
803 SettingsRegistry.SETTINGS_TYPE_SECURE,
804 owningUserId, name);
805 }
Svetoslav683914b2015-01-15 14:22:26 -0800806
Svetoslav7ec28e82015-05-20 17:01:10 -0700807 case MUTATION_OPERATION_UPDATE: {
808 return mSettingsRegistry
809 .updateSettingLocked(SettingsRegistry.SETTINGS_TYPE_SECURE,
810 owningUserId, name, value, getCallingPackage());
811 }
Svetoslav683914b2015-01-15 14:22:26 -0800812 }
813 }
814
815 return false;
816 }
817
Svetoslav7ec28e82015-05-20 17:01:10 -0700818 private Cursor getAllSystemSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800819 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700820 Slog.v(LOG_TAG, "getAllSecureSystem(" + userId + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800821 }
822
823 // Resolve the userId on whose behalf the call is made.
824 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
825
Svetoslav7ec28e82015-05-20 17:01:10 -0700826 synchronized (mLock) {
827 List<String> names = mSettingsRegistry.getSettingsNamesLocked(
828 SettingsRegistry.SETTINGS_TYPE_SYSTEM, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -0800829
Svetoslav7ec28e82015-05-20 17:01:10 -0700830 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -0800831
Svetoslav7ec28e82015-05-20 17:01:10 -0700832 String[] normalizedProjection = normalizeProjection(projection);
833 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -0800834
Svetoslav7ec28e82015-05-20 17:01:10 -0700835 for (int i = 0; i < nameCount; i++) {
836 String name = names.get(i);
Svetoslav683914b2015-01-15 14:22:26 -0800837
Svetoslav7ec28e82015-05-20 17:01:10 -0700838 // Determine the owning user as some profile settings are cloned from the parent.
839 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId,
840 name);
Svetoslav683914b2015-01-15 14:22:26 -0800841
Svetoslav7ec28e82015-05-20 17:01:10 -0700842 Setting setting = mSettingsRegistry.getSettingLocked(
843 SettingsRegistry.SETTINGS_TYPE_SYSTEM, owningUserId, name);
844 appendSettingToCursor(result, setting);
845 }
846
847 return result;
Svetoslav683914b2015-01-15 14:22:26 -0800848 }
Svetoslav683914b2015-01-15 14:22:26 -0800849 }
850
Svetoslav7ec28e82015-05-20 17:01:10 -0700851 private Setting getSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800852 if (DEBUG) {
853 Slog.v(LOG_TAG, "getSystemSetting(" + name + ", " + requestingUserId + ")");
854 }
855
856 // Resolve the userId on whose behalf the call is made.
857 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
858
859 // Determine the owning user as some profile settings are cloned from the parent.
860 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
861
862 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700863 synchronized (mLock) {
864 return mSettingsRegistry.getSettingLocked(SettingsRegistry.SETTINGS_TYPE_SYSTEM,
865 owningUserId, name);
866 }
Svetoslav683914b2015-01-15 14:22:26 -0800867 }
868
Svetoslav7ec28e82015-05-20 17:01:10 -0700869 private boolean insertSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800870 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700871 Slog.v(LOG_TAG, "insertSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -0800872 + requestingUserId + ")");
873 }
874
Svetoslav7ec28e82015-05-20 17:01:10 -0700875 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT);
Svetoslav683914b2015-01-15 14:22:26 -0800876 }
877
Svetoslav7ec28e82015-05-20 17:01:10 -0700878 private boolean deleteSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800879 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700880 Slog.v(LOG_TAG, "deleteSystemSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800881 }
882
Svetoslav7ec28e82015-05-20 17:01:10 -0700883 return mutateSystemSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE);
Svetoslav683914b2015-01-15 14:22:26 -0800884 }
885
Svetoslav7ec28e82015-05-20 17:01:10 -0700886 private boolean updateSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800887 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700888 Slog.v(LOG_TAG, "updateSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -0800889 + requestingUserId + ")");
890 }
891
Svetoslav7ec28e82015-05-20 17:01:10 -0700892 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE);
Svetoslav683914b2015-01-15 14:22:26 -0800893 }
894
Svetoslav7ec28e82015-05-20 17:01:10 -0700895 private boolean mutateSystemSetting(String name, String value, int runAsUserId,
Svetoslav683914b2015-01-15 14:22:26 -0800896 int operation) {
Billy Lau6ad2d662015-07-18 00:26:58 +0100897 if (!hasWriteSecureSettingsPermission()) {
898 // If the caller doesn't hold WRITE_SECURE_SETTINGS, we verify whether this
899 // operation is allowed for the calling package through appops.
900 if (!Settings.checkAndNoteWriteSettingsOperation(getContext(),
901 Binder.getCallingUid(), getCallingPackage(), true)) {
902 return false;
903 }
Svetoslav683914b2015-01-15 14:22:26 -0800904 }
905
Svetoslavf41334b2015-06-23 12:06:03 -0700906 // Enforce what the calling package can mutate the system settings.
907 enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name);
Svetoslav683914b2015-01-15 14:22:26 -0800908
909 // Resolve the userId on whose behalf the call is made.
910 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId);
911
912 // Determine the owning user as some profile settings are cloned from the parent.
913 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
914
915 // Only the owning user id can change the setting.
916 if (owningUserId != callingUserId) {
917 return false;
918 }
919
920 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700921 synchronized (mLock) {
922 switch (operation) {
923 case MUTATION_OPERATION_INSERT: {
924 validateSystemSettingValue(name, value);
925 return mSettingsRegistry
926 .insertSettingLocked(SettingsRegistry.SETTINGS_TYPE_SYSTEM,
927 owningUserId, name, value, getCallingPackage());
928 }
929
930 case MUTATION_OPERATION_DELETE: {
931 return mSettingsRegistry.deleteSettingLocked(
932 SettingsRegistry.SETTINGS_TYPE_SYSTEM,
933 owningUserId, name);
934 }
935
936 case MUTATION_OPERATION_UPDATE: {
937 validateSystemSettingValue(name, value);
938 return mSettingsRegistry
939 .updateSettingLocked(SettingsRegistry.SETTINGS_TYPE_SYSTEM,
940 owningUserId, name, value, getCallingPackage());
941 }
Svetoslav683914b2015-01-15 14:22:26 -0800942 }
943
Svetoslav7ec28e82015-05-20 17:01:10 -0700944 return false;
Svetoslav683914b2015-01-15 14:22:26 -0800945 }
Svetoslav683914b2015-01-15 14:22:26 -0800946 }
947
Billy Lau6ad2d662015-07-18 00:26:58 +0100948 private boolean hasWriteSecureSettingsPermission() {
Svetoslavf41334b2015-06-23 12:06:03 -0700949 // Write secure settings is a more protected permission. If caller has it we are good.
950 if (getContext().checkCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS)
951 == PackageManager.PERMISSION_GRANTED) {
952 return true;
953 }
954
Svetoslavf41334b2015-06-23 12:06:03 -0700955 return false;
956 }
957
Svetoslav683914b2015-01-15 14:22:26 -0800958 private void validateSystemSettingValue(String name, String value) {
959 Settings.System.Validator validator = Settings.System.VALIDATORS.get(name);
960 if (validator != null && !validator.validate(value)) {
961 throw new IllegalArgumentException("Invalid value: " + value
962 + " for setting: " + name);
963 }
964 }
965
966 private boolean isLocationProvidersAllowedRestricted(String name, int callingUserId,
967 int owningUserId) {
968 // Optimization - location providers are restricted only for managed profiles.
969 if (callingUserId == owningUserId) {
970 return false;
971 }
972 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)
973 && mUserManager.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION,
974 new UserHandle(callingUserId))) {
975 return true;
976 }
977 return false;
978 }
979
980 private boolean isGlobalOrSecureSettingRestrictedForUser(String setting, int userId) {
981 String restriction = sSettingToUserRestrictionMap.get(setting);
982 if (restriction == null) {
983 return false;
984 }
985 return mUserManager.hasUserRestriction(restriction, new UserHandle(userId));
986 }
987
988 private int resolveOwningUserIdForSecureSettingLocked(int userId, String setting) {
989 return resolveOwningUserIdLocked(userId, sSecureCloneToManagedSettings, setting);
990 }
991
992 private int resolveOwningUserIdForSystemSettingLocked(int userId, String setting) {
993 return resolveOwningUserIdLocked(userId, sSystemCloneToManagedSettings, setting);
994 }
995
996 private int resolveOwningUserIdLocked(int userId, Set<String> keys, String name) {
997 final int parentId = getGroupParentLocked(userId);
998 if (parentId != userId && keys.contains(name)) {
999 return parentId;
1000 }
1001 return userId;
1002 }
1003
Svetoslavf41334b2015-06-23 12:06:03 -07001004 private void enforceRestrictedSystemSettingsMutationForCallingPackage(int operation,
Svetoslav683914b2015-01-15 14:22:26 -08001005 String name) {
1006 // System/root/shell can mutate whatever secure settings they want.
1007 final int callingUid = Binder.getCallingUid();
1008 if (callingUid == android.os.Process.SYSTEM_UID
1009 || callingUid == Process.SHELL_UID
1010 || callingUid == Process.ROOT_UID) {
1011 return;
1012 }
1013
1014 switch (operation) {
1015 case MUTATION_OPERATION_INSERT:
1016 // Insert updates.
1017 case MUTATION_OPERATION_UPDATE: {
1018 if (Settings.System.PUBLIC_SETTINGS.contains(name)) {
1019 return;
1020 }
1021
1022 // The calling package is already verified.
1023 PackageInfo packageInfo = getCallingPackageInfoOrThrow();
1024
1025 // Privileged apps can do whatever they want.
1026 if ((packageInfo.applicationInfo.privateFlags
1027 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1028 return;
1029 }
1030
1031 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1032 packageInfo.applicationInfo.targetSdkVersion, name);
1033 } break;
1034
1035 case MUTATION_OPERATION_DELETE: {
1036 if (Settings.System.PUBLIC_SETTINGS.contains(name)
1037 || Settings.System.PRIVATE_SETTINGS.contains(name)) {
1038 throw new IllegalArgumentException("You cannot delete system defined"
1039 + " secure settings.");
1040 }
1041
1042 // The calling package is already verified.
1043 PackageInfo packageInfo = getCallingPackageInfoOrThrow();
1044
1045 // Privileged apps can do whatever they want.
1046 if ((packageInfo.applicationInfo.privateFlags &
1047 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1048 return;
1049 }
1050
1051 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1052 packageInfo.applicationInfo.targetSdkVersion, name);
1053 } break;
1054 }
1055 }
1056
1057 private PackageInfo getCallingPackageInfoOrThrow() {
1058 try {
1059 return mPackageManager.getPackageInfo(getCallingPackage(), 0);
1060 } catch (PackageManager.NameNotFoundException e) {
1061 throw new IllegalStateException("Calling package doesn't exist");
1062 }
1063 }
1064
1065 private int getGroupParentLocked(int userId) {
1066 // Most frequent use case.
1067 if (userId == UserHandle.USER_OWNER) {
1068 return userId;
1069 }
1070 // We are in the same process with the user manager and the returned
1071 // user info is a cached instance, so just look up instead of cache.
1072 final long identity = Binder.clearCallingIdentity();
1073 try {
Svetoslav7ec28e82015-05-20 17:01:10 -07001074 // Just a lookup and not reentrant, so holding a lock is fine.
Svetoslav683914b2015-01-15 14:22:26 -08001075 UserInfo userInfo = mUserManager.getProfileParent(userId);
1076 return (userInfo != null) ? userInfo.id : userId;
1077 } finally {
1078 Binder.restoreCallingIdentity(identity);
1079 }
1080 }
1081
Svetoslav683914b2015-01-15 14:22:26 -08001082 private void enforceWritePermission(String permission) {
1083 if (getContext().checkCallingOrSelfPermission(permission)
1084 != PackageManager.PERMISSION_GRANTED) {
1085 throw new SecurityException("Permission denial: writing to settings requires:"
1086 + permission);
1087 }
1088 }
1089
1090 /*
1091 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
1092 * This setting contains a list of the currently enabled location providers.
1093 * But helper functions in android.providers.Settings can enable or disable
1094 * a single provider by using a "+" or "-" prefix before the provider name.
1095 *
1096 * @returns whether the enabled location providers changed.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001097 */
Svetoslavb596a2c2015-02-17 21:37:09 -08001098 private boolean updateLocationProvidersAllowedLocked(String value, int owningUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001099 if (TextUtils.isEmpty(value)) {
1100 return false;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001101 }
1102
Svetoslav683914b2015-01-15 14:22:26 -08001103 final char prefix = value.charAt(0);
1104 if (prefix != '+' && prefix != '-') {
1105 return false;
1106 }
1107
1108 // skip prefix
1109 value = value.substring(1);
1110
Svetoslav7ec28e82015-05-20 17:01:10 -07001111 Setting settingValue = getSecureSetting(
Svetoslav683914b2015-01-15 14:22:26 -08001112 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, owningUserId);
1113
1114 String oldProviders = (settingValue != null) ? settingValue.getValue() : "";
1115
1116 int index = oldProviders.indexOf(value);
1117 int end = index + value.length();
1118
1119 // check for commas to avoid matching on partial string
1120 if (index > 0 && oldProviders.charAt(index - 1) != ',') {
1121 index = -1;
1122 }
1123
1124 // check for commas to avoid matching on partial string
1125 if (end < oldProviders.length() && oldProviders.charAt(end) != ',') {
1126 index = -1;
1127 }
1128
1129 String newProviders;
1130
1131 if (prefix == '+' && index < 0) {
1132 // append the provider to the list if not present
1133 if (oldProviders.length() == 0) {
1134 newProviders = value;
1135 } else {
1136 newProviders = oldProviders + ',' + value;
1137 }
1138 } else if (prefix == '-' && index >= 0) {
1139 // remove the provider from the list if present
1140 // remove leading or trailing comma
1141 if (index > 0) {
1142 index--;
1143 } else if (end < oldProviders.length()) {
1144 end++;
1145 }
1146
1147 newProviders = oldProviders.substring(0, index);
1148 if (end < oldProviders.length()) {
1149 newProviders += oldProviders.substring(end);
1150 }
1151 } else {
1152 // nothing changed, so no need to update the database
1153 return false;
1154 }
1155
Svetoslavb596a2c2015-02-17 21:37:09 -08001156 return mSettingsRegistry.insertSettingLocked(SettingsRegistry.SETTINGS_TYPE_SECURE,
1157 owningUserId, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, newProviders,
1158 getCallingPackage());
Svetoslav683914b2015-01-15 14:22:26 -08001159 }
1160
1161 private void sendNotify(Uri uri, int userId) {
1162 final long identity = Binder.clearCallingIdentity();
1163 try {
1164 getContext().getContentResolver().notifyChange(uri, null, true, userId);
1165 if (DEBUG) {
1166 Slog.v(LOG_TAG, "Notifying for " + userId + ": " + uri);
1167 }
1168 } finally {
1169 Binder.restoreCallingIdentity(identity);
1170 }
1171 }
1172
1173 private static void warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1174 int targetSdkVersion, String name) {
1175 // If the app targets Lollipop MR1 or older SDK we warn, otherwise crash.
1176 if (targetSdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) {
1177 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
1178 Slog.w(LOG_TAG, "You shouldn't not change private system settings."
1179 + " This will soon become an error.");
1180 } else {
1181 Slog.w(LOG_TAG, "You shouldn't keep your settings in the secure settings."
1182 + " This will soon become an error.");
1183 }
1184 } else {
1185 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
1186 throw new IllegalArgumentException("You cannot change private secure settings.");
1187 } else {
1188 throw new IllegalArgumentException("You cannot keep your settings in"
1189 + " the secure settings.");
1190 }
1191 }
1192 }
1193
1194 private static int resolveCallingUserIdEnforcingPermissionsLocked(int requestingUserId) {
1195 if (requestingUserId == UserHandle.getCallingUserId()) {
1196 return requestingUserId;
1197 }
1198 return ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1199 Binder.getCallingUid(), requestingUserId, false, true,
1200 "get/set setting for user", null);
1201 }
1202
1203 private static Bundle packageValueForCallResult(Setting setting) {
1204 if (setting == null) {
1205 return NULL_SETTING;
1206 }
1207 return Bundle.forPair(Settings.NameValueTable.VALUE, setting.getValue());
1208 }
1209
1210 private static int getRequestingUserId(Bundle args) {
1211 final int callingUserId = UserHandle.getCallingUserId();
1212 return (args != null) ? args.getInt(Settings.CALL_METHOD_USER_KEY, callingUserId)
1213 : callingUserId;
1214 }
1215
1216 private static String getSettingValue(Bundle args) {
1217 return (args != null) ? args.getString(Settings.NameValueTable.VALUE) : null;
1218 }
1219
1220 private static String getValidTableOrThrow(Uri uri) {
1221 if (uri.getPathSegments().size() > 0) {
1222 String table = uri.getPathSegments().get(0);
1223 if (DatabaseHelper.isValidTable(table)) {
1224 return table;
1225 }
1226 throw new IllegalArgumentException("Bad root path: " + table);
1227 }
1228 throw new IllegalArgumentException("Invalid URI:" + uri);
1229 }
1230
1231 private static MatrixCursor packageSettingForQuery(Setting setting, String[] projection) {
1232 if (setting == null) {
1233 return new MatrixCursor(projection, 0);
1234 }
1235 MatrixCursor cursor = new MatrixCursor(projection, 1);
1236 appendSettingToCursor(cursor, setting);
1237 return cursor;
1238 }
1239
1240 private static String[] normalizeProjection(String[] projection) {
1241 if (projection == null) {
1242 return ALL_COLUMNS;
1243 }
1244
1245 final int columnCount = projection.length;
1246 for (int i = 0; i < columnCount; i++) {
1247 String column = projection[i];
1248 if (!ArrayUtils.contains(ALL_COLUMNS, column)) {
1249 throw new IllegalArgumentException("Invalid column: " + column);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001250 }
1251 }
1252
Svetoslav683914b2015-01-15 14:22:26 -08001253 return projection;
1254 }
1255
1256 private static void appendSettingToCursor(MatrixCursor cursor, Setting setting) {
1257 final int columnCount = cursor.getColumnCount();
1258
1259 String[] values = new String[columnCount];
1260
1261 for (int i = 0; i < columnCount; i++) {
1262 String column = cursor.getColumnName(i);
1263
1264 switch (column) {
1265 case Settings.NameValueTable._ID: {
1266 values[i] = setting.getId();
1267 } break;
1268
1269 case Settings.NameValueTable.NAME: {
1270 values[i] = setting.getName();
1271 } break;
1272
1273 case Settings.NameValueTable.VALUE: {
1274 values[i] = setting.getValue();
1275 } break;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001276 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001277 }
1278
Svetoslav683914b2015-01-15 14:22:26 -08001279 cursor.addRow(values);
1280 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001281
Makoto Onuki3a2c35782015-06-18 11:21:58 -07001282 private static boolean isKeyValid(String key) {
1283 return !(TextUtils.isEmpty(key) || SettingsState.isBinary(key));
1284 }
1285
Svetoslav683914b2015-01-15 14:22:26 -08001286 private static final class Arguments {
1287 private static final Pattern WHERE_PATTERN_WITH_PARAM_NO_BRACKETS =
1288 Pattern.compile("[\\s]*name[\\s]*=[\\s]*\\?[\\s]*");
1289
1290 private static final Pattern WHERE_PATTERN_WITH_PARAM_IN_BRACKETS =
1291 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*\\?[\\s]*\\)[\\s]*");
1292
1293 private static final Pattern WHERE_PATTERN_NO_PARAM_IN_BRACKETS =
1294 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*\\)[\\s]*");
1295
1296 private static final Pattern WHERE_PATTERN_NO_PARAM_NO_BRACKETS =
1297 Pattern.compile("[\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*");
1298
1299 public final String table;
1300 public final String name;
1301
1302 public Arguments(Uri uri, String where, String[] whereArgs, boolean supportAll) {
1303 final int segmentSize = uri.getPathSegments().size();
1304 switch (segmentSize) {
1305 case 1: {
1306 if (where != null
1307 && (WHERE_PATTERN_WITH_PARAM_NO_BRACKETS.matcher(where).matches()
1308 || WHERE_PATTERN_WITH_PARAM_IN_BRACKETS.matcher(where).matches())
1309 && whereArgs.length == 1) {
1310 name = whereArgs[0];
1311 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08001312 return;
Svetoslav683914b2015-01-15 14:22:26 -08001313 } else if (where != null
1314 && (WHERE_PATTERN_NO_PARAM_NO_BRACKETS.matcher(where).matches()
1315 || WHERE_PATTERN_NO_PARAM_IN_BRACKETS.matcher(where).matches())) {
1316 final int startIndex = Math.max(where.indexOf("'"),
1317 where.indexOf("\"")) + 1;
1318 final int endIndex = Math.max(where.lastIndexOf("'"),
1319 where.lastIndexOf("\""));
1320 name = where.substring(startIndex, endIndex);
1321 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08001322 return;
Svetoslav683914b2015-01-15 14:22:26 -08001323 } else if (supportAll && where == null && whereArgs == null) {
1324 name = null;
1325 table = computeTableForSetting(uri, null);
Svetoslav28494652015-02-12 14:11:42 -08001326 return;
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001327 }
Svetoslav683914b2015-01-15 14:22:26 -08001328 } break;
1329
Svetoslav28494652015-02-12 14:11:42 -08001330 case 2: {
1331 if (where == null && whereArgs == null) {
1332 name = uri.getPathSegments().get(1);
1333 table = computeTableForSetting(uri, name);
1334 return;
1335 }
1336 } break;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001337 }
Svetoslav28494652015-02-12 14:11:42 -08001338
1339 EventLogTags.writeUnsupportedSettingsQuery(
1340 uri.toSafeString(), where, Arrays.toString(whereArgs));
1341 String message = String.format( "Supported SQL:\n"
1342 + " uri content://some_table/some_property with null where and where args\n"
1343 + " uri content://some_table with query name=? and single name as arg\n"
1344 + " uri content://some_table with query name=some_name and null args\n"
1345 + " but got - uri:%1s, where:%2s whereArgs:%3s", uri, where,
1346 Arrays.toString(whereArgs));
1347 throw new IllegalArgumentException(message);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001348 }
1349
Svetoslav28494652015-02-12 14:11:42 -08001350 private static String computeTableForSetting(Uri uri, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08001351 String table = getValidTableOrThrow(uri);
1352
1353 if (name != null) {
1354 if (sSystemMovedToSecureSettings.contains(name)) {
1355 table = TABLE_SECURE;
1356 }
1357
1358 if (sSystemMovedToGlobalSettings.contains(name)) {
1359 table = TABLE_GLOBAL;
1360 }
1361
1362 if (sSecureMovedToGlobalSettings.contains(name)) {
1363 table = TABLE_GLOBAL;
1364 }
1365
1366 if (sGlobalMovedToSecureSettings.contains(name)) {
1367 table = TABLE_SECURE;
1368 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001369 }
Svetoslav683914b2015-01-15 14:22:26 -08001370
1371 return table;
1372 }
1373 }
1374
1375 final class SettingsRegistry {
1376 private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid";
1377
1378 private static final int SETTINGS_TYPE_GLOBAL = 0;
1379 private static final int SETTINGS_TYPE_SYSTEM = 1;
1380 private static final int SETTINGS_TYPE_SECURE = 2;
1381
1382 private static final int SETTINGS_TYPE_MASK = 0xF0000000;
1383 private static final int SETTINGS_TYPE_SHIFT = 28;
1384
1385 private static final String SETTINGS_FILE_GLOBAL = "settings_global.xml";
1386 private static final String SETTINGS_FILE_SYSTEM = "settings_system.xml";
1387 private static final String SETTINGS_FILE_SECURE = "settings_secure.xml";
1388
1389 private final SparseArray<SettingsState> mSettingsStates = new SparseArray<>();
1390
1391 private final BackupManager mBackupManager;
1392
1393 public SettingsRegistry() {
1394 mBackupManager = new BackupManager(getContext());
1395 migrateAllLegacySettingsIfNeeded();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001396 }
1397
Svetoslav683914b2015-01-15 14:22:26 -08001398 public List<String> getSettingsNamesLocked(int type, int userId) {
1399 final int key = makeKey(type, userId);
1400 SettingsState settingsState = peekSettingsStateLocked(key);
1401 return settingsState.getSettingNamesLocked();
1402 }
1403
1404 public SettingsState getSettingsLocked(int type, int userId) {
1405 final int key = makeKey(type, userId);
1406 return peekSettingsStateLocked(key);
1407 }
1408
1409 public void ensureSettingsForUserLocked(int userId) {
1410 // Migrate the setting for this user if needed.
1411 migrateLegacySettingsForUserIfNeededLocked(userId);
1412
1413 // Ensure global settings loaded if owner.
1414 if (userId == UserHandle.USER_OWNER) {
1415 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_OWNER);
1416 ensureSettingsStateLocked(globalKey);
1417 }
1418
1419 // Ensure secure settings loaded.
1420 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1421 ensureSettingsStateLocked(secureKey);
1422
1423 // Make sure the secure settings have an Android id set.
1424 SettingsState secureSettings = getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
1425 ensureSecureSettingAndroidIdSetLocked(secureSettings);
1426
1427 // Ensure system settings loaded.
1428 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1429 ensureSettingsStateLocked(systemKey);
1430
1431 // Upgrade the settings to the latest version.
1432 UpgradeController upgrader = new UpgradeController(userId);
1433 upgrader.upgradeIfNeededLocked();
1434 }
1435
1436 private void ensureSettingsStateLocked(int key) {
1437 if (mSettingsStates.get(key) == null) {
1438 final int maxBytesPerPackage = getMaxBytesPerPackageForType(getTypeFromKey(key));
1439 SettingsState settingsState = new SettingsState(mLock, getSettingsFile(key), key,
1440 maxBytesPerPackage);
1441 mSettingsStates.put(key, settingsState);
1442 }
1443 }
1444
1445 public void removeUserStateLocked(int userId, boolean permanently) {
1446 // We always keep the global settings in memory.
1447
1448 // Nuke system settings.
1449 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1450 final SettingsState systemSettingsState = mSettingsStates.get(systemKey);
1451 if (systemSettingsState != null) {
1452 if (permanently) {
1453 mSettingsStates.remove(systemKey);
1454 systemSettingsState.destroyLocked(null);
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001455 } else {
Svetoslav683914b2015-01-15 14:22:26 -08001456 systemSettingsState.destroyLocked(new Runnable() {
1457 @Override
1458 public void run() {
1459 mSettingsStates.remove(systemKey);
1460 }
1461 });
1462 }
1463 }
1464
1465 // Nuke secure settings.
1466 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1467 final SettingsState secureSettingsState = mSettingsStates.get(secureKey);
1468 if (secureSettingsState != null) {
1469 if (permanently) {
1470 mSettingsStates.remove(secureKey);
1471 secureSettingsState.destroyLocked(null);
1472 } else {
1473 secureSettingsState.destroyLocked(new Runnable() {
1474 @Override
1475 public void run() {
1476 mSettingsStates.remove(secureKey);
1477 }
1478 });
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001479 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001480 }
1481 }
1482
Svetoslav683914b2015-01-15 14:22:26 -08001483 public boolean insertSettingLocked(int type, int userId, String name, String value,
1484 String packageName) {
1485 final int key = makeKey(type, userId);
1486
1487 SettingsState settingsState = peekSettingsStateLocked(key);
1488 final boolean success = settingsState.insertSettingLocked(name, value, packageName);
1489
1490 if (success) {
1491 notifyForSettingsChange(key, name);
1492 }
1493 return success;
1494 }
1495
1496 public boolean deleteSettingLocked(int type, int userId, String name) {
1497 final int key = makeKey(type, userId);
1498
1499 SettingsState settingsState = peekSettingsStateLocked(key);
1500 final boolean success = settingsState.deleteSettingLocked(name);
1501
1502 if (success) {
1503 notifyForSettingsChange(key, name);
1504 }
1505 return success;
1506 }
1507
1508 public Setting getSettingLocked(int type, int userId, String name) {
1509 final int key = makeKey(type, userId);
1510
1511 SettingsState settingsState = peekSettingsStateLocked(key);
1512 return settingsState.getSettingLocked(name);
1513 }
1514
1515 public boolean updateSettingLocked(int type, int userId, String name, String value,
1516 String packageName) {
1517 final int key = makeKey(type, userId);
1518
1519 SettingsState settingsState = peekSettingsStateLocked(key);
1520 final boolean success = settingsState.updateSettingLocked(name, value, packageName);
1521
1522 if (success) {
1523 notifyForSettingsChange(key, name);
1524 }
1525
1526 return success;
1527 }
1528
1529 public void onPackageRemovedLocked(String packageName, int userId) {
Svet Ganov8de34802015-04-27 09:33:40 -07001530 // Global and secure settings are signature protected. Apps signed
1531 // by the platform certificate are generally not uninstalled and
1532 // the main exception is tests. We trust components signed
1533 // by the platform certificate and do not do a clean up after them.
Svetoslav683914b2015-01-15 14:22:26 -08001534
1535 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1536 SettingsState systemSettings = mSettingsStates.get(systemKey);
Svet Ganov8de34802015-04-27 09:33:40 -07001537 if (systemSettings != null) {
1538 systemSettings.onPackageRemovedLocked(packageName);
1539 }
Svetoslav683914b2015-01-15 14:22:26 -08001540 }
1541
1542 private SettingsState peekSettingsStateLocked(int key) {
1543 SettingsState settingsState = mSettingsStates.get(key);
1544 if (settingsState != null) {
1545 return settingsState;
1546 }
1547
1548 ensureSettingsForUserLocked(getUserIdFromKey(key));
1549 return mSettingsStates.get(key);
1550 }
1551
1552 private void migrateAllLegacySettingsIfNeeded() {
1553 synchronized (mLock) {
1554 final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_OWNER);
1555 File globalFile = getSettingsFile(key);
1556 if (globalFile.exists()) {
1557 return;
1558 }
1559
1560 final long identity = Binder.clearCallingIdentity();
1561 try {
1562 List<UserInfo> users = mUserManager.getUsers(true);
1563
1564 final int userCount = users.size();
1565 for (int i = 0; i < userCount; i++) {
1566 final int userId = users.get(i).id;
1567
1568 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
1569 SQLiteDatabase database = dbHelper.getWritableDatabase();
1570 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
1571
1572 // Upgrade to the latest version.
1573 UpgradeController upgrader = new UpgradeController(userId);
1574 upgrader.upgradeIfNeededLocked();
1575
1576 // Drop from memory if not a running user.
1577 if (!mUserManager.isUserRunning(new UserHandle(userId))) {
1578 removeUserStateLocked(userId, false);
1579 }
1580 }
1581 } finally {
1582 Binder.restoreCallingIdentity(identity);
1583 }
1584 }
1585 }
1586
1587 private void migrateLegacySettingsForUserIfNeededLocked(int userId) {
1588 // Every user has secure settings and if no file we need to migrate.
1589 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1590 File secureFile = getSettingsFile(secureKey);
1591 if (secureFile.exists()) {
1592 return;
1593 }
1594
1595 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
1596 SQLiteDatabase database = dbHelper.getWritableDatabase();
1597
1598 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
1599 }
1600
1601 private void migrateLegacySettingsForUserLocked(DatabaseHelper dbHelper,
1602 SQLiteDatabase database, int userId) {
1603 // Move over the global settings if owner.
1604 if (userId == UserHandle.USER_OWNER) {
1605 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, userId);
1606 ensureSettingsStateLocked(globalKey);
1607 SettingsState globalSettings = mSettingsStates.get(globalKey);
1608 migrateLegacySettingsLocked(globalSettings, database, TABLE_GLOBAL);
1609 globalSettings.persistSyncLocked();
1610 }
1611
1612 // Move over the secure settings.
1613 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1614 ensureSettingsStateLocked(secureKey);
1615 SettingsState secureSettings = mSettingsStates.get(secureKey);
1616 migrateLegacySettingsLocked(secureSettings, database, TABLE_SECURE);
1617 ensureSecureSettingAndroidIdSetLocked(secureSettings);
1618 secureSettings.persistSyncLocked();
1619
1620 // Move over the system settings.
1621 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1622 ensureSettingsStateLocked(systemKey);
1623 SettingsState systemSettings = mSettingsStates.get(systemKey);
1624 migrateLegacySettingsLocked(systemSettings, database, TABLE_SYSTEM);
1625 systemSettings.persistSyncLocked();
1626
1627 // Drop the database as now all is moved and persisted.
1628 if (DROP_DATABASE_ON_MIGRATION) {
1629 dbHelper.dropDatabase();
1630 } else {
1631 dbHelper.backupDatabase();
1632 }
1633 }
1634
1635 private void migrateLegacySettingsLocked(SettingsState settingsState,
1636 SQLiteDatabase database, String table) {
1637 SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
1638 queryBuilder.setTables(table);
1639
1640 Cursor cursor = queryBuilder.query(database, ALL_COLUMNS,
1641 null, null, null, null, null);
1642
1643 if (cursor == null) {
1644 return;
1645 }
1646
1647 try {
1648 if (!cursor.moveToFirst()) {
1649 return;
1650 }
1651
1652 final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME);
1653 final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE);
1654
1655 settingsState.setVersionLocked(database.getVersion());
1656
1657 while (!cursor.isAfterLast()) {
1658 String name = cursor.getString(nameColumnIdx);
1659 String value = cursor.getString(valueColumnIdx);
1660 settingsState.insertSettingLocked(name, value,
1661 SettingsState.SYSTEM_PACKAGE_NAME);
1662 cursor.moveToNext();
1663 }
1664 } finally {
1665 cursor.close();
1666 }
1667 }
1668
1669 private void ensureSecureSettingAndroidIdSetLocked(SettingsState secureSettings) {
1670 Setting value = secureSettings.getSettingLocked(Settings.Secure.ANDROID_ID);
1671
1672 if (value != null) {
1673 return;
1674 }
1675
1676 final int userId = getUserIdFromKey(secureSettings.mKey);
1677
1678 final UserInfo user;
1679 final long identity = Binder.clearCallingIdentity();
1680 try {
1681 user = mUserManager.getUserInfo(userId);
1682 } finally {
1683 Binder.restoreCallingIdentity(identity);
1684 }
1685 if (user == null) {
1686 // Can happen due to races when deleting users - treat as benign.
1687 return;
1688 }
1689
1690 String androidId = Long.toHexString(new SecureRandom().nextLong());
1691 secureSettings.insertSettingLocked(Settings.Secure.ANDROID_ID, androidId,
1692 SettingsState.SYSTEM_PACKAGE_NAME);
1693
1694 Slog.d(LOG_TAG, "Generated and saved new ANDROID_ID [" + androidId
1695 + "] for user " + userId);
1696
1697 // Write a drop box entry if it's a restricted profile
1698 if (user.isRestricted()) {
1699 DropBoxManager dbm = (DropBoxManager) getContext().getSystemService(
1700 Context.DROPBOX_SERVICE);
1701 if (dbm != null && dbm.isTagEnabled(DROPBOX_TAG_USERLOG)) {
1702 dbm.addText(DROPBOX_TAG_USERLOG, System.currentTimeMillis()
1703 + "," + DROPBOX_TAG_USERLOG + "," + androidId + "\n");
1704 }
1705 }
1706 }
1707
1708 private void notifyForSettingsChange(int key, String name) {
1709 // Update the system property *first*, so if someone is listening for
1710 // a notification and then using the contract class to get their data,
1711 // the system property will be updated and they'll get the new data.
1712
1713 boolean backedUpDataChanged = false;
1714 String property = null;
1715 if (isGlobalSettingsKey(key)) {
1716 property = Settings.Global.SYS_PROP_SETTING_VERSION;
1717 backedUpDataChanged = true;
1718 } else if (isSecureSettingsKey(key)) {
1719 property = Settings.Secure.SYS_PROP_SETTING_VERSION;
1720 backedUpDataChanged = true;
1721 } else if (isSystemSettingsKey(key)) {
1722 property = Settings.System.SYS_PROP_SETTING_VERSION;
1723 backedUpDataChanged = true;
1724 }
1725
1726 if (property != null) {
1727 final long version = SystemProperties.getLong(property, 0) + 1;
1728 SystemProperties.set(property, Long.toString(version));
1729 if (DEBUG) {
1730 Slog.v(LOG_TAG, "System property " + property + "=" + version);
1731 }
1732 }
1733
1734 // Inform the backup manager about a data change
1735 if (backedUpDataChanged) {
1736 mBackupManager.dataChanged();
1737 }
1738
1739 // Now send the notification through the content framework.
1740
1741 final int userId = getUserIdFromKey(key);
1742 Uri uri = getNotificationUriFor(key, name);
1743
1744 sendNotify(uri, userId);
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01001745 if (isSecureSettingsKey(key)) {
1746 maybeNotifyProfiles(userId, uri, name, sSecureCloneToManagedSettings);
1747 } else if (isSystemSettingsKey(key)) {
1748 maybeNotifyProfiles(userId, uri, name, sSystemCloneToManagedSettings);
1749 }
1750 }
1751
1752 private void maybeNotifyProfiles(int userId, Uri uri, String name,
1753 Set<String> keysCloned) {
1754 if (keysCloned.contains(name)) {
1755 List<UserInfo> profiles = mUserManager.getProfiles(userId);
1756 int size = profiles.size();
1757 for (int i = 0; i < size; i++) {
1758 UserInfo profile = profiles.get(i);
1759 // the notification for userId has already been sent.
1760 if (profile.id != userId) {
1761 sendNotify(uri, profile.id);
1762 }
1763 }
1764 }
Svetoslav683914b2015-01-15 14:22:26 -08001765 }
1766
1767 private int makeKey(int type, int userId) {
1768 return (type << SETTINGS_TYPE_SHIFT) | userId;
1769 }
1770
1771 private int getTypeFromKey(int key) {
1772 return key >> SETTINGS_TYPE_SHIFT;
1773 }
1774
1775 private int getUserIdFromKey(int key) {
1776 return key & ~SETTINGS_TYPE_MASK;
1777 }
1778
1779 private boolean isGlobalSettingsKey(int key) {
1780 return getTypeFromKey(key) == SETTINGS_TYPE_GLOBAL;
1781 }
1782
1783 private boolean isSystemSettingsKey(int key) {
1784 return getTypeFromKey(key) == SETTINGS_TYPE_SYSTEM;
1785 }
1786
1787 private boolean isSecureSettingsKey(int key) {
1788 return getTypeFromKey(key) == SETTINGS_TYPE_SECURE;
1789 }
1790
1791 private File getSettingsFile(int key) {
1792 if (isGlobalSettingsKey(key)) {
1793 final int userId = getUserIdFromKey(key);
1794 return new File(Environment.getUserSystemDirectory(userId),
1795 SETTINGS_FILE_GLOBAL);
1796 } else if (isSystemSettingsKey(key)) {
1797 final int userId = getUserIdFromKey(key);
1798 return new File(Environment.getUserSystemDirectory(userId),
1799 SETTINGS_FILE_SYSTEM);
1800 } else if (isSecureSettingsKey(key)) {
1801 final int userId = getUserIdFromKey(key);
1802 return new File(Environment.getUserSystemDirectory(userId),
1803 SETTINGS_FILE_SECURE);
1804 } else {
1805 throw new IllegalArgumentException("Invalid settings key:" + key);
1806 }
1807 }
1808
1809 private Uri getNotificationUriFor(int key, String name) {
1810 if (isGlobalSettingsKey(key)) {
1811 return (name != null) ? Uri.withAppendedPath(Settings.Global.CONTENT_URI, name)
1812 : Settings.Global.CONTENT_URI;
1813 } else if (isSecureSettingsKey(key)) {
1814 return (name != null) ? Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name)
1815 : Settings.Secure.CONTENT_URI;
1816 } else if (isSystemSettingsKey(key)) {
1817 return (name != null) ? Uri.withAppendedPath(Settings.System.CONTENT_URI, name)
1818 : Settings.System.CONTENT_URI;
1819 } else {
1820 throw new IllegalArgumentException("Invalid settings key:" + key);
1821 }
1822 }
1823
1824 private int getMaxBytesPerPackageForType(int type) {
1825 switch (type) {
1826 case SETTINGS_TYPE_GLOBAL:
1827 case SETTINGS_TYPE_SECURE: {
1828 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED;
1829 }
1830
1831 default: {
1832 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_LIMITED;
1833 }
1834 }
1835 }
1836
1837 private final class UpgradeController {
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02001838 private static final int SETTINGS_VERSION = 122;
Svetoslav683914b2015-01-15 14:22:26 -08001839
1840 private final int mUserId;
1841
1842 public UpgradeController(int userId) {
1843 mUserId = userId;
1844 }
1845
1846 public void upgradeIfNeededLocked() {
1847 // The version of all settings for a user is the same (all users have secure).
1848 SettingsState secureSettings = getSettingsLocked(
1849 SettingsRegistry.SETTINGS_TYPE_SECURE, mUserId);
1850
1851 // Try an update from the current state.
1852 final int oldVersion = secureSettings.getVersionLocked();
1853 final int newVersion = SETTINGS_VERSION;
1854
Svet Ganovc9755bc2015-03-28 13:21:22 -07001855 // If up do date - done.
Svetoslav683914b2015-01-15 14:22:26 -08001856 if (oldVersion == newVersion) {
1857 return;
1858 }
1859
1860 // Try to upgrade.
1861 final int curVersion = onUpgradeLocked(mUserId, oldVersion, newVersion);
1862
1863 // If upgrade failed start from scratch and upgrade.
1864 if (curVersion != newVersion) {
1865 // Drop state we have for this user.
1866 removeUserStateLocked(mUserId, true);
1867
1868 // Recreate the database.
1869 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), mUserId);
1870 SQLiteDatabase database = dbHelper.getWritableDatabase();
1871 dbHelper.recreateDatabase(database, newVersion, curVersion, oldVersion);
1872
1873 // Migrate the settings for this user.
1874 migrateLegacySettingsForUserLocked(dbHelper, database, mUserId);
1875
1876 // Now upgrade should work fine.
1877 onUpgradeLocked(mUserId, oldVersion, newVersion);
1878 }
1879
1880 // Set the global settings version if owner.
1881 if (mUserId == UserHandle.USER_OWNER) {
1882 SettingsState globalSettings = getSettingsLocked(
1883 SettingsRegistry.SETTINGS_TYPE_GLOBAL, mUserId);
1884 globalSettings.setVersionLocked(newVersion);
1885 }
1886
1887 // Set the secure settings version.
1888 secureSettings.setVersionLocked(newVersion);
1889
1890 // Set the system settings version.
1891 SettingsState systemSettings = getSettingsLocked(
1892 SettingsRegistry.SETTINGS_TYPE_SYSTEM, mUserId);
1893 systemSettings.setVersionLocked(newVersion);
1894 }
1895
1896 private SettingsState getGlobalSettingsLocked() {
1897 return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_OWNER);
1898 }
1899
1900 private SettingsState getSecureSettingsLocked(int userId) {
1901 return getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
1902 }
1903
1904 private SettingsState getSystemSettingsLocked(int userId) {
1905 return getSettingsLocked(SETTINGS_TYPE_SYSTEM, userId);
1906 }
1907
Jeff Brown503cffc2015-03-26 18:08:51 -07001908 /**
1909 * You must perform all necessary mutations to bring the settings
1910 * for this user from the old to the new version. When you add a new
1911 * upgrade step you *must* update SETTINGS_VERSION.
1912 *
1913 * This is an example of moving a setting from secure to global.
1914 *
1915 * // v119: Example settings changes.
1916 * if (currentVersion == 118) {
1917 * if (userId == UserHandle.USER_OWNER) {
1918 * // Remove from the secure settings.
1919 * SettingsState secureSettings = getSecureSettingsLocked(userId);
1920 * String name = "example_setting_to_move";
1921 * String value = secureSettings.getSetting(name);
1922 * secureSettings.deleteSetting(name);
1923 *
1924 * // Add to the global settings.
1925 * SettingsState globalSettings = getGlobalSettingsLocked();
1926 * globalSettings.insertSetting(name, value, SettingsState.SYSTEM_PACKAGE_NAME);
1927 * }
1928 *
1929 * // Update the current version.
1930 * currentVersion = 119;
1931 * }
1932 */
Svetoslav683914b2015-01-15 14:22:26 -08001933 private int onUpgradeLocked(int userId, int oldVersion, int newVersion) {
1934 if (DEBUG) {
1935 Slog.w(LOG_TAG, "Upgrading settings for user: " + userId + " from version: "
1936 + oldVersion + " to version: " + newVersion);
1937 }
1938
Jeff Brown503cffc2015-03-26 18:08:51 -07001939 int currentVersion = oldVersion;
Svetoslav683914b2015-01-15 14:22:26 -08001940
John Spurlocke11ae112015-05-11 16:09:03 -04001941 // v119: Reset zen + ringer mode.
1942 if (currentVersion == 118) {
1943 if (userId == UserHandle.USER_OWNER) {
1944 final SettingsState globalSettings = getGlobalSettingsLocked();
1945 globalSettings.updateSettingLocked(Settings.Global.ZEN_MODE,
1946 Integer.toString(Settings.Global.ZEN_MODE_OFF),
1947 SettingsState.SYSTEM_PACKAGE_NAME);
1948 globalSettings.updateSettingLocked(Settings.Global.MODE_RINGER,
1949 Integer.toString(AudioManager.RINGER_MODE_NORMAL),
1950 SettingsState.SYSTEM_PACKAGE_NAME);
1951 }
1952 currentVersion = 119;
1953 }
1954
Jason Monk27bbb2d2015-03-31 16:46:39 -04001955 // v120: Add double tap to wake setting.
1956 if (currentVersion == 119) {
1957 SettingsState secureSettings = getSecureSettingsLocked(userId);
1958 secureSettings.insertSettingLocked(Settings.Secure.DOUBLE_TAP_TO_WAKE,
1959 getContext().getResources().getBoolean(
1960 R.bool.def_double_tap_to_wake) ? "1" : "0",
1961 SettingsState.SYSTEM_PACKAGE_NAME);
1962
1963 currentVersion = 120;
1964 }
1965
Makoto Onuki3a2c35782015-06-18 11:21:58 -07001966 // Before 121, we used a different string encoding logic. We just bump the version
1967 // here; SettingsState knows how to handle pre-version 120 files.
1968 currentVersion = 121;
1969
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02001970 if (currentVersion == 121) {
1971 // Version 122: allow OEMs to set a default payment component in resources.
1972 // Note that we only write the default if no default has been set;
1973 // if there is, we just leave the default at whatever it currently is.
1974 final SettingsState secureSettings = getSecureSettingsLocked(userId);
1975 String defaultComponent = (getContext().getResources().getString(
1976 R.string.def_nfc_payment_component));
1977 Setting currentSetting = secureSettings.getSettingLocked(
1978 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT);
1979 if (defaultComponent != null && !defaultComponent.isEmpty() &&
1980 currentSetting == null) {
1981 secureSettings.insertSettingLocked(
1982 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT,
1983 defaultComponent,
1984 SettingsState.SYSTEM_PACKAGE_NAME);
1985 }
1986 currentVersion = 122;
1987 }
Jeff Brown503cffc2015-03-26 18:08:51 -07001988 // vXXX: Add new settings above this point.
Svetoslav683914b2015-01-15 14:22:26 -08001989
Jeff Brown503cffc2015-03-26 18:08:51 -07001990 // Return the current version.
1991 return currentVersion;
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -08001992 }
1993 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001994 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001995}