blob: a43c398f04ad94bfff683e2371ca5f69674c734a [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.providers.settings;
18
Svetoslav683914b2015-01-15 14:22:26 -080019import android.Manifest;
Christopher Tated5fe1472012-09-10 15:48:38 -070020import android.app.ActivityManager;
Xiaohui Chen43765b72015-08-31 10:57:33 -070021import android.app.AppGlobals;
Christopher Tate45281862010-03-05 15:46:30 -080022import android.app.backup.BackupManager;
Christopher Tate06efb532012-08-24 15:29:27 -070023import android.content.BroadcastReceiver;
Ruben Brunk98576cf2016-03-07 18:54:28 -080024import android.content.ComponentName;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070025import android.content.ContentProvider;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070026import android.content.ContentValues;
27import android.content.Context;
Christopher Tate06efb532012-08-24 15:29:27 -070028import android.content.Intent;
29import android.content.IntentFilter;
Svetoslav683914b2015-01-15 14:22:26 -080030import android.content.pm.ApplicationInfo;
Xiaohui Chen43765b72015-08-31 10:57:33 -070031import android.content.pm.IPackageManager;
Svetoslav683914b2015-01-15 14:22:26 -080032import android.content.pm.PackageInfo;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070033import android.content.pm.PackageManager;
Christopher Tate38e7a602013-09-03 16:57:34 -070034import android.content.pm.UserInfo;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070035import android.database.Cursor;
Svetoslav683914b2015-01-15 14:22:26 -080036import android.database.MatrixCursor;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070037import android.database.sqlite.SQLiteDatabase;
38import android.database.sqlite.SQLiteQueryBuilder;
Svetoslav683914b2015-01-15 14:22:26 -080039import android.hardware.camera2.utils.ArrayUtils;
John Spurlocke11ae112015-05-11 16:09:03 -040040import android.media.AudioManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070041import android.net.Uri;
Christopher Tate06efb532012-08-24 15:29:27 -070042import android.os.Binder;
Svetoslav683914b2015-01-15 14:22:26 -080043import android.os.Build;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080044import android.os.Bundle;
Svet Ganov53a441c2016-04-19 19:38:00 -070045import android.os.Debug;
Amith Yamasani5cdf7f52013-06-27 15:12:01 -070046import android.os.DropBoxManager;
Svetoslav683914b2015-01-15 14:22:26 -080047import android.os.Environment;
Svetoslav7e0683b2015-08-03 16:02:52 -070048import android.os.Handler;
Svet Ganova8f90262016-05-10 08:44:48 -070049import android.os.HandlerThread;
Svetoslav7e0683b2015-08-03 16:02:52 -070050import android.os.Looper;
51import android.os.Message;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070052import android.os.ParcelFileDescriptor;
Christopher Tate0da13572013-10-13 17:34:49 -070053import android.os.Process;
Xiaohui Chen43765b72015-08-31 10:57:33 -070054import android.os.RemoteException;
Jeff Sharkey413573a2016-02-22 17:52:45 -070055import android.os.SELinux;
Christopher Tate06efb532012-08-24 15:29:27 -070056import android.os.UserHandle;
57import android.os.UserManager;
Svet Ganov53a441c2016-04-19 19:38:00 -070058import android.os.UserManagerInternal;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070059import android.provider.Settings;
60import android.text.TextUtils;
Svetoslav683914b2015-01-15 14:22:26 -080061import android.util.ArraySet;
Christopher Tate06efb532012-08-24 15:29:27 -070062import android.util.Slog;
63import android.util.SparseArray;
John Spurlocke11ae112015-05-11 16:09:03 -040064
Svetoslav683914b2015-01-15 14:22:26 -080065import com.android.internal.annotations.GuardedBy;
66import com.android.internal.content.PackageMonitor;
67import com.android.internal.os.BackgroundThread;
Suprabh Shukla269c11e2015-12-02 16:51:16 -080068import com.android.providers.settings.SettingsState.Setting;
Svet Ganov53a441c2016-04-19 19:38:00 -070069import com.android.server.LocalServices;
Ruben Brunk98576cf2016-03-07 18:54:28 -080070import com.android.server.SystemConfig;
John Spurlocke11ae112015-05-11 16:09:03 -040071
Svetoslav683914b2015-01-15 14:22:26 -080072import java.io.File;
Svetoslavb505ccc2015-02-17 12:41:04 -080073import java.io.FileDescriptor;
Svetoslav683914b2015-01-15 14:22:26 -080074import java.io.FileNotFoundException;
Svetoslavb505ccc2015-02-17 12:41:04 -080075import java.io.PrintWriter;
Svetoslav683914b2015-01-15 14:22:26 -080076import java.security.SecureRandom;
77import java.util.Arrays;
78import java.util.List;
Svetoslav683914b2015-01-15 14:22:26 -080079import java.util.Set;
80import java.util.regex.Pattern;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070081
Svetoslav683914b2015-01-15 14:22:26 -080082/**
83 * <p>
84 * This class is a content provider that publishes the system settings.
85 * It can be accessed via the content provider APIs or via custom call
86 * commands. The latter is a bit faster and is the preferred way to access
87 * the platform settings.
88 * </p>
89 * <p>
90 * There are three settings types, global (with signature level protection
91 * and shared across users), secure (with signature permission level
92 * protection and per user), and system (with dangerous permission level
93 * protection and per user). Global settings are stored under the device owner.
94 * Each of these settings is represented by a {@link
95 * com.android.providers.settings.SettingsState} object mapped to an integer
96 * key derived from the setting type in the most significant bits and user
97 * id in the least significant bits. Settings are synchronously loaded on
98 * instantiation of a SettingsState and asynchronously persisted on mutation.
99 * Settings are stored in the user specific system directory.
100 * </p>
101 * <p>
102 * Apps targeting APIs Lollipop MR1 and lower can add custom settings entries
103 * and get a warning. Targeting higher API version prohibits this as the
104 * system settings are not a place for apps to save their state. When a package
105 * is removed the settings it added are deleted. Apps cannot delete system
106 * settings added by the platform. System settings values are validated to
107 * ensure the clients do not put bad values. Global and secure settings are
108 * changed only by trusted parties, therefore no validation is performed. Also
109 * there is a limit on the amount of app specific settings that can be added
110 * to prevent unlimited growth of the system process memory footprint.
111 * </p>
112 */
113@SuppressWarnings("deprecation")
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700114public class SettingsProvider extends ContentProvider {
Svetoslav683914b2015-01-15 14:22:26 -0800115 private static final boolean DEBUG = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700116
Svetoslav Ganov264c7a92016-08-24 17:31:14 -0700117 private static final boolean DROP_DATABASE_ON_MIGRATION = true;
Svetoslav683914b2015-01-15 14:22:26 -0800118
119 private static final String LOG_TAG = "SettingsProvider";
Christopher Tate0da13572013-10-13 17:34:49 -0700120
Christopher Tate06efb532012-08-24 15:29:27 -0700121 private static final String TABLE_SYSTEM = "system";
122 private static final String TABLE_SECURE = "secure";
123 private static final String TABLE_GLOBAL = "global";
Svetoslav683914b2015-01-15 14:22:26 -0800124
125 // Old tables no longer exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 private static final String TABLE_FAVORITES = "favorites";
127 private static final String TABLE_OLD_FAVORITES = "old_favorites";
Svetoslav683914b2015-01-15 14:22:26 -0800128 private static final String TABLE_BLUETOOTH_DEVICES = "bluetooth_devices";
129 private static final String TABLE_BOOKMARKS = "bookmarks";
130 private static final String TABLE_ANDROID_METADATA = "android_metadata";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131
Svetoslav683914b2015-01-15 14:22:26 -0800132 // The set of removed legacy tables.
133 private static final Set<String> REMOVED_LEGACY_TABLES = new ArraySet<>();
Christopher Tate06efb532012-08-24 15:29:27 -0700134 static {
Svetoslav683914b2015-01-15 14:22:26 -0800135 REMOVED_LEGACY_TABLES.add(TABLE_FAVORITES);
136 REMOVED_LEGACY_TABLES.add(TABLE_OLD_FAVORITES);
137 REMOVED_LEGACY_TABLES.add(TABLE_BLUETOOTH_DEVICES);
138 REMOVED_LEGACY_TABLES.add(TABLE_BOOKMARKS);
139 REMOVED_LEGACY_TABLES.add(TABLE_ANDROID_METADATA);
140 }
Christopher Tate06efb532012-08-24 15:29:27 -0700141
Svetoslav683914b2015-01-15 14:22:26 -0800142 private static final int MUTATION_OPERATION_INSERT = 1;
143 private static final int MUTATION_OPERATION_DELETE = 2;
144 private static final int MUTATION_OPERATION_UPDATE = 3;
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400145
Svetoslav683914b2015-01-15 14:22:26 -0800146 private static final String[] ALL_COLUMNS = new String[] {
147 Settings.NameValueTable._ID,
148 Settings.NameValueTable.NAME,
149 Settings.NameValueTable.VALUE
150 };
151
Svet Ganov53a441c2016-04-19 19:38:00 -0700152 public static final int SETTINGS_TYPE_GLOBAL = 0;
153 public static final int SETTINGS_TYPE_SYSTEM = 1;
154 public static final int SETTINGS_TYPE_SECURE = 2;
Svetoslav683914b2015-01-15 14:22:26 -0800155
Svet Ganov53a441c2016-04-19 19:38:00 -0700156 public static final int SETTINGS_TYPE_MASK = 0xF0000000;
157 public static final int SETTINGS_TYPE_SHIFT = 28;
158
159 private static final Bundle NULL_SETTING_BUNDLE = Bundle.forPair(
160 Settings.NameValueTable.VALUE, null);
Christopher Tate06efb532012-08-24 15:29:27 -0700161
Svetoslav683914b2015-01-15 14:22:26 -0800162 // Per user secure settings that moved to the for all users global settings.
163 static final Set<String> sSecureMovedToGlobalSettings = new ArraySet<>();
164 static {
165 Settings.Secure.getMovedToGlobalSettings(sSecureMovedToGlobalSettings);
Christopher Tate06efb532012-08-24 15:29:27 -0700166 }
167
Svetoslav683914b2015-01-15 14:22:26 -0800168 // Per user system settings that moved to the for all users global settings.
169 static final Set<String> sSystemMovedToGlobalSettings = new ArraySet<>();
170 static {
171 Settings.System.getMovedToGlobalSettings(sSystemMovedToGlobalSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700172 }
173
Svetoslav683914b2015-01-15 14:22:26 -0800174 // Per user system settings that moved to the per user secure settings.
175 static final Set<String> sSystemMovedToSecureSettings = new ArraySet<>();
176 static {
177 Settings.System.getMovedToSecureSettings(sSystemMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700178 }
179
Svetoslav683914b2015-01-15 14:22:26 -0800180 // Per all users global settings that moved to the per user secure settings.
181 static final Set<String> sGlobalMovedToSecureSettings = new ArraySet<>();
182 static {
183 Settings.Global.getMovedToSecureSettings(sGlobalMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700184 }
185
Svetoslav683914b2015-01-15 14:22:26 -0800186 // Per user secure settings that are cloned for the managed profiles of the user.
187 private static final Set<String> sSecureCloneToManagedSettings = new ArraySet<>();
188 static {
189 Settings.Secure.getCloneToManagedProfileSettings(sSecureCloneToManagedSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700190 }
191
Svetoslav683914b2015-01-15 14:22:26 -0800192 // Per user system settings that are cloned for the managed profiles of the user.
193 private static final Set<String> sSystemCloneToManagedSettings = new ArraySet<>();
194 static {
195 Settings.System.getCloneToManagedProfileSettings(sSystemCloneToManagedSettings);
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400196 }
197
Svetoslav683914b2015-01-15 14:22:26 -0800198 private final Object mLock = new Object();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700199
Svetoslav683914b2015-01-15 14:22:26 -0800200 @GuardedBy("mLock")
201 private SettingsRegistry mSettingsRegistry;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700202
Svet Ganova8f90262016-05-10 08:44:48 -0700203 @GuardedBy("mLock")
204 private HandlerThread mHandlerThread;
205
Svetoslav7ec28e82015-05-20 17:01:10 -0700206 // We have to call in the user manager with no lock held,
207 private volatile UserManager mUserManager;
Svetoslav683914b2015-01-15 14:22:26 -0800208
Svetoslav7ec28e82015-05-20 17:01:10 -0700209 // We have to call in the package manager with no lock held,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700210 private volatile IPackageManager mPackageManager;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700211
Svet Ganov53a441c2016-04-19 19:38:00 -0700212 public static int makeKey(int type, int userId) {
213 return (type << SETTINGS_TYPE_SHIFT) | userId;
214 }
215
216 public static int getTypeFromKey(int key) {
217 return key >>> SETTINGS_TYPE_SHIFT;
218 }
219
220 public static int getUserIdFromKey(int key) {
221 return key & ~SETTINGS_TYPE_MASK;
222 }
223
224 public static String settingTypeToString(int type) {
225 switch (type) {
226 case SETTINGS_TYPE_GLOBAL: {
227 return "SETTINGS_GLOBAL";
228 }
229 case SETTINGS_TYPE_SECURE: {
230 return "SETTINGS_SECURE";
231 }
232 case SETTINGS_TYPE_SYSTEM: {
233 return "SETTINGS_SYSTEM";
234 }
235 default: {
236 return "UNKNOWN";
237 }
238 }
239 }
240
241 public static String keyToString(int key) {
242 return "Key[user=" + getUserIdFromKey(key) + ";type="
243 + settingTypeToString(getTypeFromKey(key)) + "]";
244 }
245
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700246 @Override
247 public boolean onCreate() {
Svetoslav683914b2015-01-15 14:22:26 -0800248 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700249 mUserManager = UserManager.get(getContext());
250 mPackageManager = AppGlobals.getPackageManager();
Svet Ganova8f90262016-05-10 08:44:48 -0700251 mHandlerThread = new HandlerThread(LOG_TAG,
252 Process.THREAD_PRIORITY_BACKGROUND);
253 mHandlerThread.start();
Svetoslav683914b2015-01-15 14:22:26 -0800254 mSettingsRegistry = new SettingsRegistry();
255 }
256 registerBroadcastReceivers();
Svet Ganov53a441c2016-04-19 19:38:00 -0700257 startWatchingUserRestrictionChanges();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700258 return true;
259 }
260
Svetoslav683914b2015-01-15 14:22:26 -0800261 @Override
262 public Bundle call(String method, String name, Bundle args) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700263 final int requestingUserId = getRequestingUserId(args);
264 switch (method) {
265 case Settings.CALL_METHOD_GET_GLOBAL: {
266 Setting setting = getGlobalSetting(name);
Svet Ganov53a441c2016-04-19 19:38:00 -0700267 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav683914b2015-01-15 14:22:26 -0800268 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700269
270 case Settings.CALL_METHOD_GET_SECURE: {
271 Setting setting = getSecureSetting(name, requestingUserId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700272 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700273 }
274
275 case Settings.CALL_METHOD_GET_SYSTEM: {
276 Setting setting = getSystemSetting(name, requestingUserId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700277 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700278 }
279
280 case Settings.CALL_METHOD_PUT_GLOBAL: {
281 String value = getSettingValue(args);
Svet Ganov53a441c2016-04-19 19:38:00 -0700282 insertGlobalSetting(name, value, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700283 break;
284 }
285
286 case Settings.CALL_METHOD_PUT_SECURE: {
287 String value = getSettingValue(args);
Svet Ganov53a441c2016-04-19 19:38:00 -0700288 insertSecureSetting(name, value, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700289 break;
290 }
291
292 case Settings.CALL_METHOD_PUT_SYSTEM: {
293 String value = getSettingValue(args);
294 insertSystemSetting(name, value, requestingUserId);
295 break;
296 }
297
298 default: {
299 Slog.w(LOG_TAG, "call() with invalid method: " + method);
300 } break;
Christopher Tate06efb532012-08-24 15:29:27 -0700301 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700302
Christopher Tate06efb532012-08-24 15:29:27 -0700303 return null;
304 }
305
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800306 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800307 public String getType(Uri uri) {
308 Arguments args = new Arguments(uri, null, null, true);
309 if (TextUtils.isEmpty(args.name)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700310 return "vnd.android.cursor.dir/" + args.table;
311 } else {
Svetoslav7ec28e82015-05-20 17:01:10 -0700312 return "vnd.android.cursor.item/" + args.table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700313 }
314 }
315
316 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800317 public Cursor query(Uri uri, String[] projection, String where, String[] whereArgs,
318 String order) {
319 if (DEBUG) {
320 Slog.v(LOG_TAG, "query() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700321 }
322
Svetoslav683914b2015-01-15 14:22:26 -0800323 Arguments args = new Arguments(uri, where, whereArgs, true);
324 String[] normalizedProjection = normalizeProjection(projection);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700325
Svetoslav683914b2015-01-15 14:22:26 -0800326 // If a legacy table that is gone, done.
327 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
328 return new MatrixCursor(normalizedProjection, 0);
329 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700330
Svetoslav7ec28e82015-05-20 17:01:10 -0700331 switch (args.table) {
332 case TABLE_GLOBAL: {
333 if (args.name != null) {
334 Setting setting = getGlobalSetting(args.name);
335 return packageSettingForQuery(setting, normalizedProjection);
336 } else {
337 return getAllGlobalSettings(projection);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700338 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700339 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700340
Svetoslav7ec28e82015-05-20 17:01:10 -0700341 case TABLE_SECURE: {
342 final int userId = UserHandle.getCallingUserId();
343 if (args.name != null) {
344 Setting setting = getSecureSetting(args.name, userId);
345 return packageSettingForQuery(setting, normalizedProjection);
346 } else {
347 return getAllSecureSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800348 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700349 }
Svetoslav683914b2015-01-15 14:22:26 -0800350
Svetoslav7ec28e82015-05-20 17:01:10 -0700351 case TABLE_SYSTEM: {
352 final int userId = UserHandle.getCallingUserId();
353 if (args.name != null) {
354 Setting setting = getSystemSetting(args.name, userId);
355 return packageSettingForQuery(setting, normalizedProjection);
356 } else {
357 return getAllSystemSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800358 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700359 }
Svetoslav683914b2015-01-15 14:22:26 -0800360
Svetoslav7ec28e82015-05-20 17:01:10 -0700361 default: {
362 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700363 }
364 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700365 }
366
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700367 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800368 public Uri insert(Uri uri, ContentValues values) {
369 if (DEBUG) {
370 Slog.v(LOG_TAG, "insert() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700371 }
372
Svetoslav683914b2015-01-15 14:22:26 -0800373 String table = getValidTableOrThrow(uri);
Christopher Tate06efb532012-08-24 15:29:27 -0700374
Svetoslav683914b2015-01-15 14:22:26 -0800375 // If a legacy table that is gone, done.
376 if (REMOVED_LEGACY_TABLES.contains(table)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 return null;
378 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700379
Svetoslav683914b2015-01-15 14:22:26 -0800380 String name = values.getAsString(Settings.Secure.NAME);
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700381 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800382 return null;
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700383 }
384
Svetoslav683914b2015-01-15 14:22:26 -0800385 String value = values.getAsString(Settings.Secure.VALUE);
386
Svetoslav7ec28e82015-05-20 17:01:10 -0700387 switch (table) {
388 case TABLE_GLOBAL: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700389 if (insertGlobalSetting(name, value, UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700390 return Uri.withAppendedPath(Settings.Global.CONTENT_URI, name);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700391 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700392 } break;
393
394 case TABLE_SECURE: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700395 if (insertSecureSetting(name, value, UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700396 return Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name);
397 }
398 } break;
399
400 case TABLE_SYSTEM: {
401 if (insertSystemSetting(name, value, UserHandle.getCallingUserId())) {
402 return Uri.withAppendedPath(Settings.System.CONTENT_URI, name);
403 }
404 } break;
405
406 default: {
407 throw new IllegalArgumentException("Bad Uri path:" + uri);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700408 }
409 }
410
Svetoslav683914b2015-01-15 14:22:26 -0800411 return null;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700412 }
413
414 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800415 public int bulkInsert(Uri uri, ContentValues[] allValues) {
416 if (DEBUG) {
417 Slog.v(LOG_TAG, "bulkInsert() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700419
Svetoslav683914b2015-01-15 14:22:26 -0800420 int insertionCount = 0;
421 final int valuesCount = allValues.length;
422 for (int i = 0; i < valuesCount; i++) {
423 ContentValues values = allValues[i];
424 if (insert(uri, values) != null) {
425 insertionCount++;
426 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700427 }
Svetoslav683914b2015-01-15 14:22:26 -0800428
429 return insertionCount;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700430 }
431
432 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800433 public int delete(Uri uri, String where, String[] whereArgs) {
434 if (DEBUG) {
435 Slog.v(LOG_TAG, "delete() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700437
Svetoslav683914b2015-01-15 14:22:26 -0800438 Arguments args = new Arguments(uri, where, whereArgs, false);
439
440 // If a legacy table that is gone, done.
441 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
442 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700443 }
Svetoslav683914b2015-01-15 14:22:26 -0800444
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700445 if (!isKeyValid(args.name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800446 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700447 }
Svetoslav683914b2015-01-15 14:22:26 -0800448
Svetoslav7ec28e82015-05-20 17:01:10 -0700449 switch (args.table) {
450 case TABLE_GLOBAL: {
451 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700452 return deleteGlobalSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700453 }
Svetoslav683914b2015-01-15 14:22:26 -0800454
Svetoslav7ec28e82015-05-20 17:01:10 -0700455 case TABLE_SECURE: {
456 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700457 return deleteSecureSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700458 }
Svetoslav683914b2015-01-15 14:22:26 -0800459
Svetoslav7ec28e82015-05-20 17:01:10 -0700460 case TABLE_SYSTEM: {
461 final int userId = UserHandle.getCallingUserId();
462 return deleteSystemSetting(args.name, userId) ? 1 : 0;
463 }
464
465 default: {
466 throw new IllegalArgumentException("Bad Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800467 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700468 }
Svetoslav683914b2015-01-15 14:22:26 -0800469 }
470
471 @Override
472 public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
473 if (DEBUG) {
474 Slog.v(LOG_TAG, "update() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700475 }
Svetoslav683914b2015-01-15 14:22:26 -0800476
477 Arguments args = new Arguments(uri, where, whereArgs, false);
478
479 // If a legacy table that is gone, done.
480 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
481 return 0;
482 }
483
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700484 String name = values.getAsString(Settings.Secure.NAME);
485 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800486 return 0;
487 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700488 String value = values.getAsString(Settings.Secure.VALUE);
Svetoslav683914b2015-01-15 14:22:26 -0800489
Svetoslav7ec28e82015-05-20 17:01:10 -0700490 switch (args.table) {
491 case TABLE_GLOBAL: {
492 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700493 return updateGlobalSetting(args.name, value, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700494 }
Svetoslav683914b2015-01-15 14:22:26 -0800495
Svetoslav7ec28e82015-05-20 17:01:10 -0700496 case TABLE_SECURE: {
497 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700498 return updateSecureSetting(args.name, value, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700499 }
Svetoslav683914b2015-01-15 14:22:26 -0800500
Svetoslav7ec28e82015-05-20 17:01:10 -0700501 case TABLE_SYSTEM: {
502 final int userId = UserHandle.getCallingUserId();
503 return updateSystemSetting(args.name, value, userId) ? 1 : 0;
504 }
Svetoslav683914b2015-01-15 14:22:26 -0800505
Svetoslav7ec28e82015-05-20 17:01:10 -0700506 default: {
507 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800508 }
509 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700510 }
511
512 @Override
513 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
Jeff Sharkey413573a2016-02-22 17:52:45 -0700514 final String cacheName;
515 if (Settings.System.RINGTONE_CACHE_URI.equals(uri)) {
516 cacheName = Settings.System.RINGTONE_CACHE;
517 } else if (Settings.System.NOTIFICATION_SOUND_CACHE_URI.equals(uri)) {
518 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
519 } else if (Settings.System.ALARM_ALERT_CACHE_URI.equals(uri)) {
520 cacheName = Settings.System.ALARM_ALERT_CACHE;
521 } else {
522 throw new FileNotFoundException("Direct file access no longer supported; "
523 + "ringtone playback is available through android.media.Ringtone");
524 }
525
526 final File cacheFile = new File(
527 getRingtoneCacheDir(UserHandle.getCallingUserId()), cacheName);
528 return ParcelFileDescriptor.open(cacheFile, ParcelFileDescriptor.parseMode(mode));
529 }
530
531 private File getRingtoneCacheDir(int userId) {
532 final File cacheDir = new File(Environment.getDataSystemDeDirectory(userId), "ringtones");
533 cacheDir.mkdir();
534 SELinux.restorecon(cacheDir);
535 return cacheDir;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700536 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800537
Svetoslavb505ccc2015-02-17 12:41:04 -0800538 @Override
539 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
540 synchronized (mLock) {
541 final long identity = Binder.clearCallingIdentity();
542 try {
543 List<UserInfo> users = mUserManager.getUsers(true);
544 final int userCount = users.size();
545 for (int i = 0; i < userCount; i++) {
546 UserInfo user = users.get(i);
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700547 dumpForUserLocked(user.id, pw);
Svetoslavb505ccc2015-02-17 12:41:04 -0800548 }
549 } finally {
550 Binder.restoreCallingIdentity(identity);
551 }
552 }
553 }
554
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700555 private void dumpForUserLocked(int userId, PrintWriter pw) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700556 if (userId == UserHandle.USER_SYSTEM) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800557 pw.println("GLOBAL SETTINGS (user " + userId + ")");
Svetoslav7ec28e82015-05-20 17:01:10 -0700558 Cursor globalCursor = getAllGlobalSettings(ALL_COLUMNS);
Svetoslavb505ccc2015-02-17 12:41:04 -0800559 dumpSettings(globalCursor, pw);
560 pw.println();
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700561
562 SettingsState globalSettings = mSettingsRegistry.getSettingsLocked(
563 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
564 globalSettings.dumpHistoricalOperations(pw);
Svetoslavb505ccc2015-02-17 12:41:04 -0800565 }
566
567 pw.println("SECURE SETTINGS (user " + userId + ")");
Svetoslav7ec28e82015-05-20 17:01:10 -0700568 Cursor secureCursor = getAllSecureSettings(userId, ALL_COLUMNS);
Svetoslavb505ccc2015-02-17 12:41:04 -0800569 dumpSettings(secureCursor, pw);
570 pw.println();
571
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700572 SettingsState secureSettings = mSettingsRegistry.getSettingsLocked(
573 SETTINGS_TYPE_SECURE, userId);
574 secureSettings.dumpHistoricalOperations(pw);
575
Svetoslavb505ccc2015-02-17 12:41:04 -0800576 pw.println("SYSTEM SETTINGS (user " + userId + ")");
Svetoslav7ec28e82015-05-20 17:01:10 -0700577 Cursor systemCursor = getAllSystemSettings(userId, ALL_COLUMNS);
Svetoslavb505ccc2015-02-17 12:41:04 -0800578 dumpSettings(systemCursor, pw);
579 pw.println();
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700580
581 SettingsState systemSettings = mSettingsRegistry.getSettingsLocked(
582 SETTINGS_TYPE_SYSTEM, userId);
583 systemSettings.dumpHistoricalOperations(pw);
Svetoslavb505ccc2015-02-17 12:41:04 -0800584 }
585
586 private void dumpSettings(Cursor cursor, PrintWriter pw) {
Fyodor Kupolov1f450db2015-06-11 15:25:59 -0700587 if (cursor == null || !cursor.moveToFirst()) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800588 return;
589 }
590
591 final int idColumnIdx = cursor.getColumnIndex(Settings.NameValueTable._ID);
592 final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME);
593 final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE);
594
595 do {
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700596 pw.append("_id:").append(toDumpString(cursor.getString(idColumnIdx)));
597 pw.append(" name:").append(toDumpString(cursor.getString(nameColumnIdx)));
598 pw.append(" value:").append(toDumpString(cursor.getString(valueColumnIdx)));
Svetoslavb505ccc2015-02-17 12:41:04 -0800599 pw.println();
600 } while (cursor.moveToNext());
601 }
602
Svetoslav7e0683b2015-08-03 16:02:52 -0700603 private static String toDumpString(String s) {
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700604 if (s != null) {
605 return s;
606 }
607 return "{null}";
608 }
609
Svetoslav683914b2015-01-15 14:22:26 -0800610 private void registerBroadcastReceivers() {
611 IntentFilter userFilter = new IntentFilter();
612 userFilter.addAction(Intent.ACTION_USER_REMOVED);
613 userFilter.addAction(Intent.ACTION_USER_STOPPED);
614
615 getContext().registerReceiver(new BroadcastReceiver() {
616 @Override
617 public void onReceive(Context context, Intent intent) {
618 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700619 UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800620
621 switch (intent.getAction()) {
622 case Intent.ACTION_USER_REMOVED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700623 synchronized (mLock) {
624 mSettingsRegistry.removeUserStateLocked(userId, true);
625 }
Svetoslav683914b2015-01-15 14:22:26 -0800626 } break;
627
628 case Intent.ACTION_USER_STOPPED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700629 synchronized (mLock) {
630 mSettingsRegistry.removeUserStateLocked(userId, false);
631 }
Svetoslav683914b2015-01-15 14:22:26 -0800632 } break;
633 }
634 }
635 }, userFilter);
636
637 PackageMonitor monitor = new PackageMonitor() {
638 @Override
639 public void onPackageRemoved(String packageName, int uid) {
640 synchronized (mLock) {
641 mSettingsRegistry.onPackageRemovedLocked(packageName,
642 UserHandle.getUserId(uid));
643 }
644 }
645 };
646
647 // package changes
648 monitor.register(getContext(), BackgroundThread.getHandler().getLooper(),
649 UserHandle.ALL, true);
650 }
651
Svet Ganov53a441c2016-04-19 19:38:00 -0700652 private void startWatchingUserRestrictionChanges() {
653 // TODO: The current design of settings looking different based on user restrictions
654 // should be reworked to keep them separate and system code should check the setting
655 // first followed by checking the user restriction before performing an operation.
656 UserManagerInternal userManager = LocalServices.getService(UserManagerInternal.class);
657 userManager.addUserRestrictionsListener((int userId, Bundle newRestrictions,
658 Bundle prevRestrictions) -> {
659 // We are changing the settings affected by restrictions to their current
660 // value with a forced update to ensure that all cross profile dependencies
661 // are taken into account. Also make sure the settings update to.. the same
662 // value passes the security checks, so clear binder calling id.
663 if (newRestrictions.containsKey(UserManager.DISALLOW_SHARE_LOCATION)
664 != prevRestrictions.containsKey(UserManager.DISALLOW_SHARE_LOCATION)) {
665 final long identity = Binder.clearCallingIdentity();
666 try {
667 synchronized (mLock) {
668 Setting setting = getSecureSetting(
669 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, userId);
670 updateSecureSetting(Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
671 setting != null ? setting.getValue() : null, userId, true);
672 }
673 } finally {
674 Binder.restoreCallingIdentity(identity);
675 }
676 }
677 if (newRestrictions.containsKey(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)
678 != prevRestrictions.containsKey(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)) {
679 final long identity = Binder.clearCallingIdentity();
680 try {
681 synchronized (mLock) {
682 Setting setting = getGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS);
683 updateGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS,
684 setting != null ? setting.getValue() : null, userId, true);
685 }
686 } finally {
687 Binder.restoreCallingIdentity(identity);
688 }
689 }
690 if (newRestrictions.containsKey(UserManager.DISALLOW_DEBUGGING_FEATURES)
691 != prevRestrictions.containsKey(UserManager.DISALLOW_DEBUGGING_FEATURES)) {
692 final long identity = Binder.clearCallingIdentity();
693 try {
694 synchronized (mLock) {
695 Setting setting = getGlobalSetting(Settings.Global.ADB_ENABLED);
696 updateGlobalSetting(Settings.Global.ADB_ENABLED,
697 setting != null ? setting.getValue() : null, userId, true);
698 }
699 } finally {
700 Binder.restoreCallingIdentity(identity);
701 }
702 }
703 if (newRestrictions.containsKey(UserManager.ENSURE_VERIFY_APPS)
704 != prevRestrictions.containsKey(UserManager.ENSURE_VERIFY_APPS)) {
705 final long identity = Binder.clearCallingIdentity();
706 try {
707 synchronized (mLock) {
708 Setting enable = getGlobalSetting(
709 Settings.Global.PACKAGE_VERIFIER_ENABLE);
710 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_ENABLE,
711 enable != null ? enable.getValue() : null, userId, true);
712 Setting include = getGlobalSetting(
713 Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB);
714 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB,
715 include != null ? include.getValue() : null, userId, true);
716 }
717 } finally {
718 Binder.restoreCallingIdentity(identity);
719 }
720 }
721 if (newRestrictions.containsKey(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)
722 != prevRestrictions.containsKey(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
723 final long identity = Binder.clearCallingIdentity();
724 try {
725 synchronized (mLock) {
726 Setting setting = getGlobalSetting(
727 Settings.Global.PREFERRED_NETWORK_MODE);
728 updateGlobalSetting(Settings.Global.PREFERRED_NETWORK_MODE,
729 setting != null ? setting.getValue() : null, userId, true);
730 }
731 } finally {
732 Binder.restoreCallingIdentity(identity);
733 }
734 }
735 });
736 }
737
Svetoslav7ec28e82015-05-20 17:01:10 -0700738 private Cursor getAllGlobalSettings(String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800739 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700740 Slog.v(LOG_TAG, "getAllGlobalSettings()");
Svetoslav683914b2015-01-15 14:22:26 -0800741 }
742
Svetoslav7ec28e82015-05-20 17:01:10 -0700743 synchronized (mLock) {
744 // Get the settings.
745 SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700746 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800747
Svetoslav7ec28e82015-05-20 17:01:10 -0700748 List<String> names = settingsState.getSettingNamesLocked();
Svetoslav683914b2015-01-15 14:22:26 -0800749
Svetoslav7ec28e82015-05-20 17:01:10 -0700750 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -0800751
Svetoslav7ec28e82015-05-20 17:01:10 -0700752 String[] normalizedProjection = normalizeProjection(projection);
753 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -0800754
Svetoslav7ec28e82015-05-20 17:01:10 -0700755 // Anyone can get the global settings, so no security checks.
756 for (int i = 0; i < nameCount; i++) {
757 String name = names.get(i);
758 Setting setting = settingsState.getSettingLocked(name);
759 appendSettingToCursor(result, setting);
760 }
761
762 return result;
Svetoslav683914b2015-01-15 14:22:26 -0800763 }
Svetoslav683914b2015-01-15 14:22:26 -0800764 }
765
Svetoslav7ec28e82015-05-20 17:01:10 -0700766 private Setting getGlobalSetting(String name) {
Svetoslav683914b2015-01-15 14:22:26 -0800767 if (DEBUG) {
768 Slog.v(LOG_TAG, "getGlobalSetting(" + name + ")");
769 }
770
771 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700772 synchronized (mLock) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700773 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_GLOBAL,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700774 UserHandle.USER_SYSTEM, name);
Svetoslav683914b2015-01-15 14:22:26 -0800775 }
Svetoslav683914b2015-01-15 14:22:26 -0800776 }
777
Svet Ganov53a441c2016-04-19 19:38:00 -0700778 private boolean updateGlobalSetting(String name, String value, int requestingUserId,
779 boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800780 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700781 Slog.v(LOG_TAG, "updateGlobalSetting(" + name + ", " + value + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800782 }
Svet Ganov53a441c2016-04-19 19:38:00 -0700783 return mutateGlobalSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE,
784 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800785 }
786
Svet Ganov53a441c2016-04-19 19:38:00 -0700787 private boolean insertGlobalSetting(String name, String value, int requestingUserId,
788 boolean forceNotify) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700789 if (DEBUG) {
790 Slog.v(LOG_TAG, "insertGlobalSetting(" + name + ", " + value + ")");
791 }
Svet Ganov53a441c2016-04-19 19:38:00 -0700792 return mutateGlobalSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT,
793 forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700794 }
795
Svet Ganov53a441c2016-04-19 19:38:00 -0700796 private boolean deleteGlobalSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800797 if (DEBUG) {
798 Slog.v(LOG_TAG, "deleteGlobalSettingLocked(" + name + ")");
799 }
Svet Ganov53a441c2016-04-19 19:38:00 -0700800 return mutateGlobalSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE,
801 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800802 }
803
Svetoslav7ec28e82015-05-20 17:01:10 -0700804 private boolean mutateGlobalSetting(String name, String value, int requestingUserId,
Svet Ganov53a441c2016-04-19 19:38:00 -0700805 int operation, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800806 // Make sure the caller can change the settings - treated as secure.
807 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
808
Svetoslav683914b2015-01-15 14:22:26 -0800809 // Resolve the userId on whose behalf the call is made.
810 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
811
Makoto Onuki28da2e32015-11-20 11:30:44 -0800812 // If this is a setting that is currently restricted for this user, do not allow
813 // unrestricting changes.
Victor Chang9c7b7062016-07-12 23:47:29 +0100814 if (isGlobalOrSecureSettingRestrictedForUser(name, callingUserId, value,
815 Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -0800816 return false;
817 }
818
819 // Perform the mutation.
Svetoslav7ec28e82015-05-20 17:01:10 -0700820 synchronized (mLock) {
821 switch (operation) {
822 case MUTATION_OPERATION_INSERT: {
823 return mSettingsRegistry
Svet Ganov53a441c2016-04-19 19:38:00 -0700824 .insertSettingLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM,
825 name, value, getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700826 }
Svetoslav683914b2015-01-15 14:22:26 -0800827
Svetoslav7ec28e82015-05-20 17:01:10 -0700828 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700829 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_GLOBAL,
830 UserHandle.USER_SYSTEM, name, forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700831 }
Svetoslav683914b2015-01-15 14:22:26 -0800832
Svetoslav7ec28e82015-05-20 17:01:10 -0700833 case MUTATION_OPERATION_UPDATE: {
834 return mSettingsRegistry
Svet Ganov53a441c2016-04-19 19:38:00 -0700835 .updateSettingLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM,
836 name, value, getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700837 }
Svetoslav683914b2015-01-15 14:22:26 -0800838 }
839 }
840
841 return false;
842 }
843
Svetoslav7ec28e82015-05-20 17:01:10 -0700844 private Cursor getAllSecureSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800845 if (DEBUG) {
846 Slog.v(LOG_TAG, "getAllSecureSettings(" + userId + ")");
847 }
848
849 // Resolve the userId on whose behalf the call is made.
850 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
851
Svetoslav7ec28e82015-05-20 17:01:10 -0700852 synchronized (mLock) {
853 List<String> names = mSettingsRegistry.getSettingsNamesLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700854 SETTINGS_TYPE_SECURE, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -0800855
Svetoslav7ec28e82015-05-20 17:01:10 -0700856 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -0800857
Svetoslav7ec28e82015-05-20 17:01:10 -0700858 String[] normalizedProjection = normalizeProjection(projection);
859 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -0800860
Svetoslav7ec28e82015-05-20 17:01:10 -0700861 for (int i = 0; i < nameCount; i++) {
862 String name = names.get(i);
863 // Determine the owning user as some profile settings are cloned from the parent.
864 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
865 name);
Svetoslav683914b2015-01-15 14:22:26 -0800866
Svetoslav7ec28e82015-05-20 17:01:10 -0700867 // Special case for location (sigh).
868 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
Svetoslav Ganov83a1f7f2016-04-27 13:50:49 -0700869 continue;
Svetoslav7ec28e82015-05-20 17:01:10 -0700870 }
Svetoslav683914b2015-01-15 14:22:26 -0800871
Svetoslav7ec28e82015-05-20 17:01:10 -0700872 Setting setting = mSettingsRegistry.getSettingLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700873 SETTINGS_TYPE_SECURE, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -0700874 appendSettingToCursor(result, setting);
Svetoslav683914b2015-01-15 14:22:26 -0800875 }
876
Svetoslav7ec28e82015-05-20 17:01:10 -0700877 return result;
Svetoslav683914b2015-01-15 14:22:26 -0800878 }
Svetoslav683914b2015-01-15 14:22:26 -0800879 }
880
Svetoslav7ec28e82015-05-20 17:01:10 -0700881 private Setting getSecureSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800882 if (DEBUG) {
883 Slog.v(LOG_TAG, "getSecureSetting(" + name + ", " + requestingUserId + ")");
884 }
885
886 // Resolve the userId on whose behalf the call is made.
887 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
888
889 // Determine the owning user as some profile settings are cloned from the parent.
890 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
891
892 // Special case for location (sigh).
893 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
Svetoslav Ganov83a1f7f2016-04-27 13:50:49 -0700894 return mSettingsRegistry.getSettingsLocked(SETTINGS_TYPE_SECURE,
895 owningUserId).getNullSetting();
Svetoslav683914b2015-01-15 14:22:26 -0800896 }
897
898 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700899 synchronized (mLock) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700900 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav7ec28e82015-05-20 17:01:10 -0700901 owningUserId, name);
902 }
Svetoslav683914b2015-01-15 14:22:26 -0800903 }
904
Svet Ganov53a441c2016-04-19 19:38:00 -0700905 private boolean insertSecureSetting(String name, String value, int requestingUserId,
906 boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800907 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700908 Slog.v(LOG_TAG, "insertSecureSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -0800909 + requestingUserId + ")");
910 }
911
Svet Ganov53a441c2016-04-19 19:38:00 -0700912 return mutateSecureSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT,
913 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800914 }
915
Svet Ganov53a441c2016-04-19 19:38:00 -0700916 private boolean deleteSecureSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800917 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700918 Slog.v(LOG_TAG, "deleteSecureSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800919 }
920
Svet Ganov53a441c2016-04-19 19:38:00 -0700921 return mutateSecureSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE,
922 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800923 }
924
Svet Ganov53a441c2016-04-19 19:38:00 -0700925 private boolean updateSecureSetting(String name, String value, int requestingUserId,
926 boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800927 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700928 Slog.v(LOG_TAG, "updateSecureSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -0800929 + requestingUserId + ")");
930 }
931
Svet Ganov53a441c2016-04-19 19:38:00 -0700932 return mutateSecureSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE,
933 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800934 }
935
Svetoslav7ec28e82015-05-20 17:01:10 -0700936 private boolean mutateSecureSetting(String name, String value, int requestingUserId,
Svet Ganov53a441c2016-04-19 19:38:00 -0700937 int operation, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800938 // Make sure the caller can change the settings.
939 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
940
Svetoslav683914b2015-01-15 14:22:26 -0800941 // Resolve the userId on whose behalf the call is made.
942 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
943
Makoto Onuki28da2e32015-11-20 11:30:44 -0800944 // If this is a setting that is currently restricted for this user, do not allow
945 // unrestricting changes.
Victor Chang9c7b7062016-07-12 23:47:29 +0100946 if (isGlobalOrSecureSettingRestrictedForUser(name, callingUserId, value,
947 Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -0800948 return false;
949 }
950
951 // Determine the owning user as some profile settings are cloned from the parent.
952 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
953
954 // Only the owning user can change the setting.
955 if (owningUserId != callingUserId) {
956 return false;
957 }
958
959 // Special cases for location providers (sigh).
960 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700961 return updateLocationProvidersAllowedLocked(value, owningUserId, forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800962 }
963
964 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700965 synchronized (mLock) {
966 switch (operation) {
967 case MUTATION_OPERATION_INSERT: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700968 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
969 owningUserId, name, value, getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700970 }
Svetoslav683914b2015-01-15 14:22:26 -0800971
Svetoslav7ec28e82015-05-20 17:01:10 -0700972 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700973 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SECURE,
974 owningUserId, name, forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700975 }
Svetoslav683914b2015-01-15 14:22:26 -0800976
Svetoslav7ec28e82015-05-20 17:01:10 -0700977 case MUTATION_OPERATION_UPDATE: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700978 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SECURE,
979 owningUserId, name, value, getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700980 }
Svetoslav683914b2015-01-15 14:22:26 -0800981 }
982 }
983
984 return false;
985 }
986
Svetoslav7ec28e82015-05-20 17:01:10 -0700987 private Cursor getAllSystemSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800988 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700989 Slog.v(LOG_TAG, "getAllSecureSystem(" + userId + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800990 }
991
992 // Resolve the userId on whose behalf the call is made.
993 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
994
Svetoslav7ec28e82015-05-20 17:01:10 -0700995 synchronized (mLock) {
996 List<String> names = mSettingsRegistry.getSettingsNamesLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700997 SETTINGS_TYPE_SYSTEM, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -0800998
Svetoslav7ec28e82015-05-20 17:01:10 -0700999 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001000
Svetoslav7ec28e82015-05-20 17:01:10 -07001001 String[] normalizedProjection = normalizeProjection(projection);
1002 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001003
Svetoslav7ec28e82015-05-20 17:01:10 -07001004 for (int i = 0; i < nameCount; i++) {
1005 String name = names.get(i);
Svetoslav683914b2015-01-15 14:22:26 -08001006
Svetoslav7ec28e82015-05-20 17:01:10 -07001007 // Determine the owning user as some profile settings are cloned from the parent.
1008 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId,
1009 name);
Svetoslav683914b2015-01-15 14:22:26 -08001010
Svetoslav7ec28e82015-05-20 17:01:10 -07001011 Setting setting = mSettingsRegistry.getSettingLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001012 SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001013 appendSettingToCursor(result, setting);
1014 }
1015
1016 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001017 }
Svetoslav683914b2015-01-15 14:22:26 -08001018 }
1019
Svetoslav7ec28e82015-05-20 17:01:10 -07001020 private Setting getSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001021 if (DEBUG) {
1022 Slog.v(LOG_TAG, "getSystemSetting(" + name + ", " + requestingUserId + ")");
1023 }
1024
1025 // Resolve the userId on whose behalf the call is made.
1026 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1027
1028 // Determine the owning user as some profile settings are cloned from the parent.
1029 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1030
1031 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001032 synchronized (mLock) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001033 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001034 }
Svetoslav683914b2015-01-15 14:22:26 -08001035 }
1036
Svetoslav7ec28e82015-05-20 17:01:10 -07001037 private boolean insertSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001038 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001039 Slog.v(LOG_TAG, "insertSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001040 + requestingUserId + ")");
1041 }
1042
Svetoslav7ec28e82015-05-20 17:01:10 -07001043 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT);
Svetoslav683914b2015-01-15 14:22:26 -08001044 }
1045
Svetoslav7ec28e82015-05-20 17:01:10 -07001046 private boolean deleteSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001047 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001048 Slog.v(LOG_TAG, "deleteSystemSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001049 }
1050
Svetoslav7ec28e82015-05-20 17:01:10 -07001051 return mutateSystemSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE);
Svetoslav683914b2015-01-15 14:22:26 -08001052 }
1053
Svetoslav7ec28e82015-05-20 17:01:10 -07001054 private boolean updateSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001055 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001056 Slog.v(LOG_TAG, "updateSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001057 + requestingUserId + ")");
1058 }
1059
Svetoslav7ec28e82015-05-20 17:01:10 -07001060 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE);
Svetoslav683914b2015-01-15 14:22:26 -08001061 }
1062
Svetoslav7ec28e82015-05-20 17:01:10 -07001063 private boolean mutateSystemSetting(String name, String value, int runAsUserId,
Svetoslav683914b2015-01-15 14:22:26 -08001064 int operation) {
Billy Lau6ad2d662015-07-18 00:26:58 +01001065 if (!hasWriteSecureSettingsPermission()) {
1066 // If the caller doesn't hold WRITE_SECURE_SETTINGS, we verify whether this
1067 // operation is allowed for the calling package through appops.
1068 if (!Settings.checkAndNoteWriteSettingsOperation(getContext(),
1069 Binder.getCallingUid(), getCallingPackage(), true)) {
1070 return false;
1071 }
Svetoslav683914b2015-01-15 14:22:26 -08001072 }
1073
Svetoslav683914b2015-01-15 14:22:26 -08001074 // Resolve the userId on whose behalf the call is made.
1075 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId);
1076
Svetoslavd8d25e02015-11-20 13:09:26 -08001077 // Enforce what the calling package can mutate the system settings.
1078 enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name, callingUserId);
1079
Svetoslav683914b2015-01-15 14:22:26 -08001080 // Determine the owning user as some profile settings are cloned from the parent.
1081 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1082
1083 // Only the owning user id can change the setting.
1084 if (owningUserId != callingUserId) {
1085 return false;
1086 }
1087
Jeff Sharkey413573a2016-02-22 17:52:45 -07001088 // Invalidate any relevant cache files
1089 String cacheName = null;
1090 if (Settings.System.RINGTONE.equals(name)) {
1091 cacheName = Settings.System.RINGTONE_CACHE;
1092 } else if (Settings.System.NOTIFICATION_SOUND.equals(name)) {
1093 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
1094 } else if (Settings.System.ALARM_ALERT.equals(name)) {
1095 cacheName = Settings.System.ALARM_ALERT_CACHE;
1096 }
1097 if (cacheName != null) {
1098 final File cacheFile = new File(
1099 getRingtoneCacheDir(UserHandle.getCallingUserId()), cacheName);
1100 cacheFile.delete();
1101 }
1102
Svetoslav683914b2015-01-15 14:22:26 -08001103 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001104 synchronized (mLock) {
1105 switch (operation) {
1106 case MUTATION_OPERATION_INSERT: {
1107 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001108 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SYSTEM,
1109 owningUserId, name, value, getCallingPackage(), false);
Svetoslav7ec28e82015-05-20 17:01:10 -07001110 }
1111
1112 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001113 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SYSTEM,
1114 owningUserId, name, false);
Svetoslav7ec28e82015-05-20 17:01:10 -07001115 }
1116
1117 case MUTATION_OPERATION_UPDATE: {
1118 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001119 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SYSTEM,
1120 owningUserId, name, value, getCallingPackage(), false);
Svetoslav7ec28e82015-05-20 17:01:10 -07001121 }
Svetoslav683914b2015-01-15 14:22:26 -08001122 }
1123
Svetoslav7ec28e82015-05-20 17:01:10 -07001124 return false;
Svetoslav683914b2015-01-15 14:22:26 -08001125 }
Svetoslav683914b2015-01-15 14:22:26 -08001126 }
1127
Billy Lau6ad2d662015-07-18 00:26:58 +01001128 private boolean hasWriteSecureSettingsPermission() {
Svetoslavf41334b2015-06-23 12:06:03 -07001129 // Write secure settings is a more protected permission. If caller has it we are good.
1130 if (getContext().checkCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS)
1131 == PackageManager.PERMISSION_GRANTED) {
1132 return true;
1133 }
1134
Svetoslavf41334b2015-06-23 12:06:03 -07001135 return false;
1136 }
1137
Svetoslav683914b2015-01-15 14:22:26 -08001138 private void validateSystemSettingValue(String name, String value) {
1139 Settings.System.Validator validator = Settings.System.VALIDATORS.get(name);
1140 if (validator != null && !validator.validate(value)) {
1141 throw new IllegalArgumentException("Invalid value: " + value
1142 + " for setting: " + name);
1143 }
1144 }
1145
1146 private boolean isLocationProvidersAllowedRestricted(String name, int callingUserId,
1147 int owningUserId) {
1148 // Optimization - location providers are restricted only for managed profiles.
1149 if (callingUserId == owningUserId) {
1150 return false;
1151 }
1152 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)
1153 && mUserManager.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION,
1154 new UserHandle(callingUserId))) {
1155 return true;
1156 }
1157 return false;
1158 }
1159
Makoto Onuki28da2e32015-11-20 11:30:44 -08001160 /**
1161 * Checks whether changing a setting to a value is prohibited by the corresponding user
1162 * restriction.
1163 *
Svet Ganov53a441c2016-04-19 19:38:00 -07001164 * <p>See also {@link com.android.server.pm.UserRestrictionsUtils#applyUserRestriction(
1165 * Context, int, String, boolean)}, which should be in sync with this method.
Makoto Onuki28da2e32015-11-20 11:30:44 -08001166 *
1167 * @return true if the change is prohibited, false if the change is allowed.
1168 */
1169 private boolean isGlobalOrSecureSettingRestrictedForUser(String setting, int userId,
Victor Chang9c7b7062016-07-12 23:47:29 +01001170 String value, int callingUid) {
Makoto Onuki28da2e32015-11-20 11:30:44 -08001171 String restriction;
1172 switch (setting) {
1173 case Settings.Secure.LOCATION_MODE:
1174 // Note LOCATION_MODE will be converted into LOCATION_PROVIDERS_ALLOWED
1175 // in android.provider.Settings.Secure.putStringForUser(), so we shouldn't come
1176 // here normally, but we still protect it here from a direct provider write.
1177 if (String.valueOf(Settings.Secure.LOCATION_MODE_OFF).equals(value)) return false;
1178 restriction = UserManager.DISALLOW_SHARE_LOCATION;
1179 break;
1180
1181 case Settings.Secure.LOCATION_PROVIDERS_ALLOWED:
1182 // See SettingsProvider.updateLocationProvidersAllowedLocked. "-" is to disable
1183 // a provider, which should be allowed even if the user restriction is set.
1184 if (value != null && value.startsWith("-")) return false;
1185 restriction = UserManager.DISALLOW_SHARE_LOCATION;
1186 break;
1187
1188 case Settings.Secure.INSTALL_NON_MARKET_APPS:
1189 if ("0".equals(value)) return false;
1190 restriction = UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES;
1191 break;
1192
1193 case Settings.Global.ADB_ENABLED:
1194 if ("0".equals(value)) return false;
1195 restriction = UserManager.DISALLOW_DEBUGGING_FEATURES;
1196 break;
1197
1198 case Settings.Global.PACKAGE_VERIFIER_ENABLE:
1199 case Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB:
1200 if ("1".equals(value)) return false;
1201 restriction = UserManager.ENSURE_VERIFY_APPS;
1202 break;
1203
1204 case Settings.Global.PREFERRED_NETWORK_MODE:
1205 restriction = UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS;
1206 break;
1207
Victor Chang9c7b7062016-07-12 23:47:29 +01001208 case Settings.Secure.ALWAYS_ON_VPN_APP:
1209 case Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN:
1210 // Whitelist system uid (ConnectivityService) and root uid to change always-on vpn
1211 if (callingUid == Process.SYSTEM_UID || callingUid == Process.ROOT_UID) {
1212 return false;
1213 }
1214 restriction = UserManager.DISALLOW_CONFIG_VPN;
1215 break;
1216
Benjamin Franz0ff13fc2016-07-12 13:42:21 +01001217 case Settings.Global.SAFE_BOOT_DISALLOWED:
1218 if ("1".equals(value)) return false;
1219 restriction = UserManager.DISALLOW_SAFE_BOOT;
1220 break;
1221
Makoto Onuki28da2e32015-11-20 11:30:44 -08001222 default:
Mahaver Chopra87648752016-01-08 19:23:57 +00001223 if (setting != null && setting.startsWith(Settings.Global.DATA_ROAMING)) {
Mahaver Chopradea471e2015-12-17 11:02:37 +00001224 if ("0".equals(value)) return false;
1225 restriction = UserManager.DISALLOW_DATA_ROAMING;
1226 break;
1227 }
Makoto Onuki28da2e32015-11-20 11:30:44 -08001228 return false;
Svetoslav683914b2015-01-15 14:22:26 -08001229 }
Makoto Onuki28da2e32015-11-20 11:30:44 -08001230
1231 return mUserManager.hasUserRestriction(restriction, UserHandle.of(userId));
Svetoslav683914b2015-01-15 14:22:26 -08001232 }
1233
1234 private int resolveOwningUserIdForSecureSettingLocked(int userId, String setting) {
1235 return resolveOwningUserIdLocked(userId, sSecureCloneToManagedSettings, setting);
1236 }
1237
1238 private int resolveOwningUserIdForSystemSettingLocked(int userId, String setting) {
1239 return resolveOwningUserIdLocked(userId, sSystemCloneToManagedSettings, setting);
1240 }
1241
1242 private int resolveOwningUserIdLocked(int userId, Set<String> keys, String name) {
1243 final int parentId = getGroupParentLocked(userId);
1244 if (parentId != userId && keys.contains(name)) {
1245 return parentId;
1246 }
1247 return userId;
1248 }
1249
Svetoslavf41334b2015-06-23 12:06:03 -07001250 private void enforceRestrictedSystemSettingsMutationForCallingPackage(int operation,
Xiaohui Chen43765b72015-08-31 10:57:33 -07001251 String name, int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001252 // System/root/shell can mutate whatever secure settings they want.
1253 final int callingUid = Binder.getCallingUid();
1254 if (callingUid == android.os.Process.SYSTEM_UID
1255 || callingUid == Process.SHELL_UID
1256 || callingUid == Process.ROOT_UID) {
1257 return;
1258 }
1259
1260 switch (operation) {
1261 case MUTATION_OPERATION_INSERT:
1262 // Insert updates.
1263 case MUTATION_OPERATION_UPDATE: {
1264 if (Settings.System.PUBLIC_SETTINGS.contains(name)) {
1265 return;
1266 }
1267
1268 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001269 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001270
1271 // Privileged apps can do whatever they want.
1272 if ((packageInfo.applicationInfo.privateFlags
1273 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1274 return;
1275 }
1276
1277 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1278 packageInfo.applicationInfo.targetSdkVersion, name);
1279 } break;
1280
1281 case MUTATION_OPERATION_DELETE: {
1282 if (Settings.System.PUBLIC_SETTINGS.contains(name)
1283 || Settings.System.PRIVATE_SETTINGS.contains(name)) {
1284 throw new IllegalArgumentException("You cannot delete system defined"
1285 + " secure settings.");
1286 }
1287
1288 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001289 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001290
1291 // Privileged apps can do whatever they want.
1292 if ((packageInfo.applicationInfo.privateFlags &
1293 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1294 return;
1295 }
1296
1297 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1298 packageInfo.applicationInfo.targetSdkVersion, name);
1299 } break;
1300 }
1301 }
1302
Xiaohui Chen43765b72015-08-31 10:57:33 -07001303 private PackageInfo getCallingPackageInfoOrThrow(int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001304 try {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001305 PackageInfo packageInfo = mPackageManager.getPackageInfo(
1306 getCallingPackage(), 0, userId);
1307 if (packageInfo != null) {
1308 return packageInfo;
1309 }
Xiaohui Chen43765b72015-08-31 10:57:33 -07001310 } catch (RemoteException e) {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001311 /* ignore */
Svetoslav683914b2015-01-15 14:22:26 -08001312 }
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001313 throw new IllegalStateException("Calling package doesn't exist");
Svetoslav683914b2015-01-15 14:22:26 -08001314 }
1315
1316 private int getGroupParentLocked(int userId) {
1317 // Most frequent use case.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001318 if (userId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08001319 return userId;
1320 }
1321 // We are in the same process with the user manager and the returned
1322 // user info is a cached instance, so just look up instead of cache.
1323 final long identity = Binder.clearCallingIdentity();
1324 try {
Svetoslav7ec28e82015-05-20 17:01:10 -07001325 // Just a lookup and not reentrant, so holding a lock is fine.
Svetoslav683914b2015-01-15 14:22:26 -08001326 UserInfo userInfo = mUserManager.getProfileParent(userId);
1327 return (userInfo != null) ? userInfo.id : userId;
1328 } finally {
1329 Binder.restoreCallingIdentity(identity);
1330 }
1331 }
1332
Svetoslav683914b2015-01-15 14:22:26 -08001333 private void enforceWritePermission(String permission) {
1334 if (getContext().checkCallingOrSelfPermission(permission)
1335 != PackageManager.PERMISSION_GRANTED) {
1336 throw new SecurityException("Permission denial: writing to settings requires:"
1337 + permission);
1338 }
1339 }
1340
1341 /*
1342 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
1343 * This setting contains a list of the currently enabled location providers.
1344 * But helper functions in android.providers.Settings can enable or disable
1345 * a single provider by using a "+" or "-" prefix before the provider name.
1346 *
Makoto Onuki28da2e32015-11-20 11:30:44 -08001347 * <p>See also {@link #isGlobalOrSecureSettingRestrictedForUser()}. If DISALLOW_SHARE_LOCATION
1348 * is set, the said method will only allow values with the "-" prefix.
1349 *
Svetoslav683914b2015-01-15 14:22:26 -08001350 * @returns whether the enabled location providers changed.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001351 */
Svet Ganov53a441c2016-04-19 19:38:00 -07001352 private boolean updateLocationProvidersAllowedLocked(String value, int owningUserId,
1353 boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001354 if (TextUtils.isEmpty(value)) {
1355 return false;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001356 }
1357
Svetoslav683914b2015-01-15 14:22:26 -08001358 final char prefix = value.charAt(0);
1359 if (prefix != '+' && prefix != '-') {
Svet Ganov53a441c2016-04-19 19:38:00 -07001360 if (forceNotify) {
1361 final int key = makeKey(SETTINGS_TYPE_SECURE, owningUserId);
1362 mSettingsRegistry.notifyForSettingsChange(key,
1363 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
1364 }
Svetoslav683914b2015-01-15 14:22:26 -08001365 return false;
1366 }
1367
1368 // skip prefix
1369 value = value.substring(1);
1370
Svetoslav7ec28e82015-05-20 17:01:10 -07001371 Setting settingValue = getSecureSetting(
Svetoslav683914b2015-01-15 14:22:26 -08001372 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, owningUserId);
1373
1374 String oldProviders = (settingValue != null) ? settingValue.getValue() : "";
1375
1376 int index = oldProviders.indexOf(value);
1377 int end = index + value.length();
1378
1379 // check for commas to avoid matching on partial string
1380 if (index > 0 && oldProviders.charAt(index - 1) != ',') {
1381 index = -1;
1382 }
1383
1384 // check for commas to avoid matching on partial string
1385 if (end < oldProviders.length() && oldProviders.charAt(end) != ',') {
1386 index = -1;
1387 }
1388
1389 String newProviders;
1390
1391 if (prefix == '+' && index < 0) {
1392 // append the provider to the list if not present
1393 if (oldProviders.length() == 0) {
1394 newProviders = value;
1395 } else {
1396 newProviders = oldProviders + ',' + value;
1397 }
1398 } else if (prefix == '-' && index >= 0) {
1399 // remove the provider from the list if present
1400 // remove leading or trailing comma
1401 if (index > 0) {
1402 index--;
1403 } else if (end < oldProviders.length()) {
1404 end++;
1405 }
1406
1407 newProviders = oldProviders.substring(0, index);
1408 if (end < oldProviders.length()) {
1409 newProviders += oldProviders.substring(end);
1410 }
1411 } else {
1412 // nothing changed, so no need to update the database
Svet Ganov53a441c2016-04-19 19:38:00 -07001413 if (forceNotify) {
1414 final int key = makeKey(SETTINGS_TYPE_SECURE, owningUserId);
1415 mSettingsRegistry.notifyForSettingsChange(key,
1416 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
1417 }
Svetoslav683914b2015-01-15 14:22:26 -08001418 return false;
1419 }
1420
Svet Ganov53a441c2016-04-19 19:38:00 -07001421 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslavb596a2c2015-02-17 21:37:09 -08001422 owningUserId, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, newProviders,
Svet Ganov53a441c2016-04-19 19:38:00 -07001423 getCallingPackage(), forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -08001424 }
1425
Svetoslav683914b2015-01-15 14:22:26 -08001426 private static void warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1427 int targetSdkVersion, String name) {
1428 // If the app targets Lollipop MR1 or older SDK we warn, otherwise crash.
1429 if (targetSdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) {
1430 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
1431 Slog.w(LOG_TAG, "You shouldn't not change private system settings."
1432 + " This will soon become an error.");
1433 } else {
1434 Slog.w(LOG_TAG, "You shouldn't keep your settings in the secure settings."
1435 + " This will soon become an error.");
1436 }
1437 } else {
1438 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
1439 throw new IllegalArgumentException("You cannot change private secure settings.");
1440 } else {
1441 throw new IllegalArgumentException("You cannot keep your settings in"
1442 + " the secure settings.");
1443 }
1444 }
1445 }
1446
1447 private static int resolveCallingUserIdEnforcingPermissionsLocked(int requestingUserId) {
1448 if (requestingUserId == UserHandle.getCallingUserId()) {
1449 return requestingUserId;
1450 }
1451 return ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1452 Binder.getCallingUid(), requestingUserId, false, true,
1453 "get/set setting for user", null);
1454 }
1455
Svet Ganov53a441c2016-04-19 19:38:00 -07001456 private Bundle packageValueForCallResult(Setting setting,
1457 boolean trackingGeneration) {
1458 if (!trackingGeneration) {
1459 if (setting.isNull()) {
1460 return NULL_SETTING_BUNDLE;
1461 }
1462 return Bundle.forPair(Settings.NameValueTable.VALUE, setting.getValue());
Svetoslav683914b2015-01-15 14:22:26 -08001463 }
Svet Ganov53a441c2016-04-19 19:38:00 -07001464 Bundle result = new Bundle();
1465 result.putString(Settings.NameValueTable.VALUE,
1466 !setting.isNull() ? setting.getValue() : null);
1467 mSettingsRegistry.mGenerationRegistry.addGenerationData(result, setting.getkey());
1468 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001469 }
1470
1471 private static int getRequestingUserId(Bundle args) {
1472 final int callingUserId = UserHandle.getCallingUserId();
1473 return (args != null) ? args.getInt(Settings.CALL_METHOD_USER_KEY, callingUserId)
1474 : callingUserId;
1475 }
1476
Svet Ganov53a441c2016-04-19 19:38:00 -07001477 private boolean isTrackingGeneration(Bundle args) {
1478 return args != null && args.containsKey(Settings.CALL_METHOD_TRACK_GENERATION_KEY);
1479 }
1480
Svetoslav683914b2015-01-15 14:22:26 -08001481 private static String getSettingValue(Bundle args) {
1482 return (args != null) ? args.getString(Settings.NameValueTable.VALUE) : null;
1483 }
1484
1485 private static String getValidTableOrThrow(Uri uri) {
1486 if (uri.getPathSegments().size() > 0) {
1487 String table = uri.getPathSegments().get(0);
1488 if (DatabaseHelper.isValidTable(table)) {
1489 return table;
1490 }
1491 throw new IllegalArgumentException("Bad root path: " + table);
1492 }
1493 throw new IllegalArgumentException("Invalid URI:" + uri);
1494 }
1495
1496 private static MatrixCursor packageSettingForQuery(Setting setting, String[] projection) {
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07001497 if (setting.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08001498 return new MatrixCursor(projection, 0);
1499 }
1500 MatrixCursor cursor = new MatrixCursor(projection, 1);
1501 appendSettingToCursor(cursor, setting);
1502 return cursor;
1503 }
1504
1505 private static String[] normalizeProjection(String[] projection) {
1506 if (projection == null) {
1507 return ALL_COLUMNS;
1508 }
1509
1510 final int columnCount = projection.length;
1511 for (int i = 0; i < columnCount; i++) {
1512 String column = projection[i];
1513 if (!ArrayUtils.contains(ALL_COLUMNS, column)) {
1514 throw new IllegalArgumentException("Invalid column: " + column);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001515 }
1516 }
1517
Svetoslav683914b2015-01-15 14:22:26 -08001518 return projection;
1519 }
1520
1521 private static void appendSettingToCursor(MatrixCursor cursor, Setting setting) {
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07001522 if (setting.isNull()) {
Suprabh Shuklac9d064a2016-04-12 18:45:34 -07001523 return;
1524 }
Svetoslav683914b2015-01-15 14:22:26 -08001525 final int columnCount = cursor.getColumnCount();
1526
1527 String[] values = new String[columnCount];
1528
1529 for (int i = 0; i < columnCount; i++) {
1530 String column = cursor.getColumnName(i);
1531
1532 switch (column) {
1533 case Settings.NameValueTable._ID: {
1534 values[i] = setting.getId();
1535 } break;
1536
1537 case Settings.NameValueTable.NAME: {
1538 values[i] = setting.getName();
1539 } break;
1540
1541 case Settings.NameValueTable.VALUE: {
1542 values[i] = setting.getValue();
1543 } break;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001544 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001545 }
1546
Svetoslav683914b2015-01-15 14:22:26 -08001547 cursor.addRow(values);
1548 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001549
Makoto Onuki3a2c35782015-06-18 11:21:58 -07001550 private static boolean isKeyValid(String key) {
1551 return !(TextUtils.isEmpty(key) || SettingsState.isBinary(key));
1552 }
1553
Svetoslav683914b2015-01-15 14:22:26 -08001554 private static final class Arguments {
1555 private static final Pattern WHERE_PATTERN_WITH_PARAM_NO_BRACKETS =
1556 Pattern.compile("[\\s]*name[\\s]*=[\\s]*\\?[\\s]*");
1557
1558 private static final Pattern WHERE_PATTERN_WITH_PARAM_IN_BRACKETS =
1559 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*\\?[\\s]*\\)[\\s]*");
1560
1561 private static final Pattern WHERE_PATTERN_NO_PARAM_IN_BRACKETS =
1562 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*\\)[\\s]*");
1563
1564 private static final Pattern WHERE_PATTERN_NO_PARAM_NO_BRACKETS =
1565 Pattern.compile("[\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*");
1566
1567 public final String table;
1568 public final String name;
1569
1570 public Arguments(Uri uri, String where, String[] whereArgs, boolean supportAll) {
1571 final int segmentSize = uri.getPathSegments().size();
1572 switch (segmentSize) {
1573 case 1: {
1574 if (where != null
1575 && (WHERE_PATTERN_WITH_PARAM_NO_BRACKETS.matcher(where).matches()
1576 || WHERE_PATTERN_WITH_PARAM_IN_BRACKETS.matcher(where).matches())
1577 && whereArgs.length == 1) {
1578 name = whereArgs[0];
1579 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08001580 return;
Svetoslav683914b2015-01-15 14:22:26 -08001581 } else if (where != null
1582 && (WHERE_PATTERN_NO_PARAM_NO_BRACKETS.matcher(where).matches()
1583 || WHERE_PATTERN_NO_PARAM_IN_BRACKETS.matcher(where).matches())) {
1584 final int startIndex = Math.max(where.indexOf("'"),
1585 where.indexOf("\"")) + 1;
1586 final int endIndex = Math.max(where.lastIndexOf("'"),
1587 where.lastIndexOf("\""));
1588 name = where.substring(startIndex, endIndex);
1589 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08001590 return;
Svetoslav683914b2015-01-15 14:22:26 -08001591 } else if (supportAll && where == null && whereArgs == null) {
1592 name = null;
1593 table = computeTableForSetting(uri, null);
Svetoslav28494652015-02-12 14:11:42 -08001594 return;
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001595 }
Svetoslav683914b2015-01-15 14:22:26 -08001596 } break;
1597
Svetoslav28494652015-02-12 14:11:42 -08001598 case 2: {
1599 if (where == null && whereArgs == null) {
1600 name = uri.getPathSegments().get(1);
1601 table = computeTableForSetting(uri, name);
1602 return;
1603 }
1604 } break;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001605 }
Svetoslav28494652015-02-12 14:11:42 -08001606
1607 EventLogTags.writeUnsupportedSettingsQuery(
1608 uri.toSafeString(), where, Arrays.toString(whereArgs));
1609 String message = String.format( "Supported SQL:\n"
1610 + " uri content://some_table/some_property with null where and where args\n"
1611 + " uri content://some_table with query name=? and single name as arg\n"
1612 + " uri content://some_table with query name=some_name and null args\n"
1613 + " but got - uri:%1s, where:%2s whereArgs:%3s", uri, where,
1614 Arrays.toString(whereArgs));
1615 throw new IllegalArgumentException(message);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001616 }
1617
Svetoslav28494652015-02-12 14:11:42 -08001618 private static String computeTableForSetting(Uri uri, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08001619 String table = getValidTableOrThrow(uri);
1620
1621 if (name != null) {
1622 if (sSystemMovedToSecureSettings.contains(name)) {
1623 table = TABLE_SECURE;
1624 }
1625
1626 if (sSystemMovedToGlobalSettings.contains(name)) {
1627 table = TABLE_GLOBAL;
1628 }
1629
1630 if (sSecureMovedToGlobalSettings.contains(name)) {
1631 table = TABLE_GLOBAL;
1632 }
1633
1634 if (sGlobalMovedToSecureSettings.contains(name)) {
1635 table = TABLE_SECURE;
1636 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001637 }
Svetoslav683914b2015-01-15 14:22:26 -08001638
1639 return table;
1640 }
1641 }
1642
1643 final class SettingsRegistry {
1644 private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid";
1645
Svetoslav683914b2015-01-15 14:22:26 -08001646 private static final String SETTINGS_FILE_GLOBAL = "settings_global.xml";
1647 private static final String SETTINGS_FILE_SYSTEM = "settings_system.xml";
1648 private static final String SETTINGS_FILE_SECURE = "settings_secure.xml";
1649
1650 private final SparseArray<SettingsState> mSettingsStates = new SparseArray<>();
1651
Svet Ganov53a441c2016-04-19 19:38:00 -07001652 private GenerationRegistry mGenerationRegistry;
Svetoslav683914b2015-01-15 14:22:26 -08001653
Svetoslav7e0683b2015-08-03 16:02:52 -07001654 private final Handler mHandler;
1655
Svet Ganov53a441c2016-04-19 19:38:00 -07001656 private final BackupManager mBackupManager;
1657
Svetoslav683914b2015-01-15 14:22:26 -08001658 public SettingsRegistry() {
Svetoslav7e0683b2015-08-03 16:02:52 -07001659 mHandler = new MyHandler(getContext().getMainLooper());
Svet Ganov53a441c2016-04-19 19:38:00 -07001660 mGenerationRegistry = new GenerationRegistry(mLock);
1661 mBackupManager = new BackupManager(getContext());
Svetoslav683914b2015-01-15 14:22:26 -08001662 migrateAllLegacySettingsIfNeeded();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001663 }
1664
Svetoslav683914b2015-01-15 14:22:26 -08001665 public List<String> getSettingsNamesLocked(int type, int userId) {
1666 final int key = makeKey(type, userId);
1667 SettingsState settingsState = peekSettingsStateLocked(key);
1668 return settingsState.getSettingNamesLocked();
1669 }
1670
1671 public SettingsState getSettingsLocked(int type, int userId) {
1672 final int key = makeKey(type, userId);
1673 return peekSettingsStateLocked(key);
1674 }
1675
1676 public void ensureSettingsForUserLocked(int userId) {
1677 // Migrate the setting for this user if needed.
1678 migrateLegacySettingsForUserIfNeededLocked(userId);
1679
1680 // Ensure global settings loaded if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001681 if (userId == UserHandle.USER_SYSTEM) {
1682 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001683 ensureSettingsStateLocked(globalKey);
1684 }
1685
1686 // Ensure secure settings loaded.
1687 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1688 ensureSettingsStateLocked(secureKey);
1689
1690 // Make sure the secure settings have an Android id set.
1691 SettingsState secureSettings = getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
1692 ensureSecureSettingAndroidIdSetLocked(secureSettings);
1693
1694 // Ensure system settings loaded.
1695 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1696 ensureSettingsStateLocked(systemKey);
1697
1698 // Upgrade the settings to the latest version.
1699 UpgradeController upgrader = new UpgradeController(userId);
1700 upgrader.upgradeIfNeededLocked();
1701 }
1702
1703 private void ensureSettingsStateLocked(int key) {
1704 if (mSettingsStates.get(key) == null) {
1705 final int maxBytesPerPackage = getMaxBytesPerPackageForType(getTypeFromKey(key));
1706 SettingsState settingsState = new SettingsState(mLock, getSettingsFile(key), key,
Svetoslav Ganov92057492016-05-16 12:36:43 -07001707 maxBytesPerPackage, mHandlerThread.getLooper());
Svetoslav683914b2015-01-15 14:22:26 -08001708 mSettingsStates.put(key, settingsState);
1709 }
1710 }
1711
1712 public void removeUserStateLocked(int userId, boolean permanently) {
1713 // We always keep the global settings in memory.
1714
1715 // Nuke system settings.
1716 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1717 final SettingsState systemSettingsState = mSettingsStates.get(systemKey);
1718 if (systemSettingsState != null) {
1719 if (permanently) {
1720 mSettingsStates.remove(systemKey);
1721 systemSettingsState.destroyLocked(null);
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001722 } else {
Svetoslav683914b2015-01-15 14:22:26 -08001723 systemSettingsState.destroyLocked(new Runnable() {
1724 @Override
1725 public void run() {
1726 mSettingsStates.remove(systemKey);
1727 }
1728 });
1729 }
1730 }
1731
1732 // Nuke secure settings.
1733 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1734 final SettingsState secureSettingsState = mSettingsStates.get(secureKey);
1735 if (secureSettingsState != null) {
1736 if (permanently) {
1737 mSettingsStates.remove(secureKey);
1738 secureSettingsState.destroyLocked(null);
1739 } else {
1740 secureSettingsState.destroyLocked(new Runnable() {
1741 @Override
1742 public void run() {
1743 mSettingsStates.remove(secureKey);
1744 }
1745 });
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001746 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001747 }
Svet Ganov53a441c2016-04-19 19:38:00 -07001748
1749 // Nuke generation tracking data
1750 mGenerationRegistry.onUserRemoved(userId);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001751 }
1752
Svetoslav683914b2015-01-15 14:22:26 -08001753 public boolean insertSettingLocked(int type, int userId, String name, String value,
Svet Ganov53a441c2016-04-19 19:38:00 -07001754 String packageName, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001755 final int key = makeKey(type, userId);
1756
1757 SettingsState settingsState = peekSettingsStateLocked(key);
1758 final boolean success = settingsState.insertSettingLocked(name, value, packageName);
1759
Svet Ganov53a441c2016-04-19 19:38:00 -07001760 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08001761 notifyForSettingsChange(key, name);
1762 }
1763 return success;
1764 }
1765
Svet Ganov53a441c2016-04-19 19:38:00 -07001766 public boolean deleteSettingLocked(int type, int userId, String name, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001767 final int key = makeKey(type, userId);
1768
1769 SettingsState settingsState = peekSettingsStateLocked(key);
1770 final boolean success = settingsState.deleteSettingLocked(name);
1771
Svet Ganov53a441c2016-04-19 19:38:00 -07001772 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08001773 notifyForSettingsChange(key, name);
1774 }
1775 return success;
1776 }
1777
1778 public Setting getSettingLocked(int type, int userId, String name) {
1779 final int key = makeKey(type, userId);
1780
1781 SettingsState settingsState = peekSettingsStateLocked(key);
1782 return settingsState.getSettingLocked(name);
1783 }
1784
1785 public boolean updateSettingLocked(int type, int userId, String name, String value,
Svet Ganov53a441c2016-04-19 19:38:00 -07001786 String packageName, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001787 final int key = makeKey(type, userId);
1788
1789 SettingsState settingsState = peekSettingsStateLocked(key);
1790 final boolean success = settingsState.updateSettingLocked(name, value, packageName);
1791
Svet Ganov53a441c2016-04-19 19:38:00 -07001792 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08001793 notifyForSettingsChange(key, name);
1794 }
1795
1796 return success;
1797 }
1798
1799 public void onPackageRemovedLocked(String packageName, int userId) {
Svet Ganov8de34802015-04-27 09:33:40 -07001800 // Global and secure settings are signature protected. Apps signed
1801 // by the platform certificate are generally not uninstalled and
1802 // the main exception is tests. We trust components signed
1803 // by the platform certificate and do not do a clean up after them.
Svetoslav683914b2015-01-15 14:22:26 -08001804
1805 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1806 SettingsState systemSettings = mSettingsStates.get(systemKey);
Svet Ganov8de34802015-04-27 09:33:40 -07001807 if (systemSettings != null) {
1808 systemSettings.onPackageRemovedLocked(packageName);
1809 }
Svetoslav683914b2015-01-15 14:22:26 -08001810 }
1811
1812 private SettingsState peekSettingsStateLocked(int key) {
1813 SettingsState settingsState = mSettingsStates.get(key);
1814 if (settingsState != null) {
1815 return settingsState;
1816 }
1817
1818 ensureSettingsForUserLocked(getUserIdFromKey(key));
1819 return mSettingsStates.get(key);
1820 }
1821
1822 private void migrateAllLegacySettingsIfNeeded() {
1823 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07001824 final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001825 File globalFile = getSettingsFile(key);
1826 if (globalFile.exists()) {
1827 return;
1828 }
1829
1830 final long identity = Binder.clearCallingIdentity();
1831 try {
1832 List<UserInfo> users = mUserManager.getUsers(true);
1833
1834 final int userCount = users.size();
1835 for (int i = 0; i < userCount; i++) {
1836 final int userId = users.get(i).id;
1837
1838 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
1839 SQLiteDatabase database = dbHelper.getWritableDatabase();
1840 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
1841
1842 // Upgrade to the latest version.
1843 UpgradeController upgrader = new UpgradeController(userId);
1844 upgrader.upgradeIfNeededLocked();
1845
1846 // Drop from memory if not a running user.
1847 if (!mUserManager.isUserRunning(new UserHandle(userId))) {
1848 removeUserStateLocked(userId, false);
1849 }
1850 }
1851 } finally {
1852 Binder.restoreCallingIdentity(identity);
1853 }
1854 }
1855 }
1856
1857 private void migrateLegacySettingsForUserIfNeededLocked(int userId) {
1858 // Every user has secure settings and if no file we need to migrate.
1859 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1860 File secureFile = getSettingsFile(secureKey);
1861 if (secureFile.exists()) {
1862 return;
1863 }
1864
1865 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
1866 SQLiteDatabase database = dbHelper.getWritableDatabase();
1867
1868 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
1869 }
1870
1871 private void migrateLegacySettingsForUserLocked(DatabaseHelper dbHelper,
1872 SQLiteDatabase database, int userId) {
Amith Yamasanibf2ef612016-03-07 16:37:18 -08001873 // Move over the system settings.
1874 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1875 ensureSettingsStateLocked(systemKey);
1876 SettingsState systemSettings = mSettingsStates.get(systemKey);
1877 migrateLegacySettingsLocked(systemSettings, database, TABLE_SYSTEM);
1878 systemSettings.persistSyncLocked();
Svetoslav683914b2015-01-15 14:22:26 -08001879
1880 // Move over the secure settings.
Amith Yamasanibf2ef612016-03-07 16:37:18 -08001881 // Do this after System settings, since this is the first thing we check when deciding
1882 // to skip over migration from db to xml for a secondary user.
Svetoslav683914b2015-01-15 14:22:26 -08001883 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1884 ensureSettingsStateLocked(secureKey);
1885 SettingsState secureSettings = mSettingsStates.get(secureKey);
1886 migrateLegacySettingsLocked(secureSettings, database, TABLE_SECURE);
1887 ensureSecureSettingAndroidIdSetLocked(secureSettings);
1888 secureSettings.persistSyncLocked();
1889
Amith Yamasanibf2ef612016-03-07 16:37:18 -08001890 // Move over the global settings if owner.
1891 // Do this last, since this is the first thing we check when deciding
1892 // to skip over migration from db to xml for owner user.
1893 if (userId == UserHandle.USER_SYSTEM) {
1894 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, userId);
1895 ensureSettingsStateLocked(globalKey);
1896 SettingsState globalSettings = mSettingsStates.get(globalKey);
1897 migrateLegacySettingsLocked(globalSettings, database, TABLE_GLOBAL);
1898 globalSettings.persistSyncLocked();
1899 }
Svetoslav683914b2015-01-15 14:22:26 -08001900
1901 // Drop the database as now all is moved and persisted.
1902 if (DROP_DATABASE_ON_MIGRATION) {
1903 dbHelper.dropDatabase();
1904 } else {
1905 dbHelper.backupDatabase();
1906 }
1907 }
1908
1909 private void migrateLegacySettingsLocked(SettingsState settingsState,
1910 SQLiteDatabase database, String table) {
1911 SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
1912 queryBuilder.setTables(table);
1913
1914 Cursor cursor = queryBuilder.query(database, ALL_COLUMNS,
1915 null, null, null, null, null);
1916
1917 if (cursor == null) {
1918 return;
1919 }
1920
1921 try {
1922 if (!cursor.moveToFirst()) {
1923 return;
1924 }
1925
1926 final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME);
1927 final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE);
1928
1929 settingsState.setVersionLocked(database.getVersion());
1930
1931 while (!cursor.isAfterLast()) {
1932 String name = cursor.getString(nameColumnIdx);
1933 String value = cursor.getString(valueColumnIdx);
1934 settingsState.insertSettingLocked(name, value,
1935 SettingsState.SYSTEM_PACKAGE_NAME);
1936 cursor.moveToNext();
1937 }
1938 } finally {
1939 cursor.close();
1940 }
1941 }
1942
1943 private void ensureSecureSettingAndroidIdSetLocked(SettingsState secureSettings) {
1944 Setting value = secureSettings.getSettingLocked(Settings.Secure.ANDROID_ID);
1945
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09001946 if (!value.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08001947 return;
1948 }
1949
1950 final int userId = getUserIdFromKey(secureSettings.mKey);
1951
1952 final UserInfo user;
1953 final long identity = Binder.clearCallingIdentity();
1954 try {
1955 user = mUserManager.getUserInfo(userId);
1956 } finally {
1957 Binder.restoreCallingIdentity(identity);
1958 }
1959 if (user == null) {
1960 // Can happen due to races when deleting users - treat as benign.
1961 return;
1962 }
1963
1964 String androidId = Long.toHexString(new SecureRandom().nextLong());
1965 secureSettings.insertSettingLocked(Settings.Secure.ANDROID_ID, androidId,
1966 SettingsState.SYSTEM_PACKAGE_NAME);
1967
1968 Slog.d(LOG_TAG, "Generated and saved new ANDROID_ID [" + androidId
1969 + "] for user " + userId);
1970
1971 // Write a drop box entry if it's a restricted profile
1972 if (user.isRestricted()) {
1973 DropBoxManager dbm = (DropBoxManager) getContext().getSystemService(
1974 Context.DROPBOX_SERVICE);
1975 if (dbm != null && dbm.isTagEnabled(DROPBOX_TAG_USERLOG)) {
1976 dbm.addText(DROPBOX_TAG_USERLOG, System.currentTimeMillis()
1977 + "," + DROPBOX_TAG_USERLOG + "," + androidId + "\n");
1978 }
1979 }
1980 }
1981
1982 private void notifyForSettingsChange(int key, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08001983 final int userId = getUserIdFromKey(key);
1984 Uri uri = getNotificationUriFor(key, name);
1985
Phil Weaver83fec002016-05-11 10:55:29 -07001986 mGenerationRegistry.incrementGeneration(key);
1987
Svetoslav7e0683b2015-08-03 16:02:52 -07001988 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
1989 userId, 0, uri).sendToTarget();
1990
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01001991 if (isSecureSettingsKey(key)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001992 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
1993 sSecureCloneToManagedSettings);
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01001994 } else if (isSystemSettingsKey(key)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001995 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
1996 sSystemCloneToManagedSettings);
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01001997 }
Svet Ganov53a441c2016-04-19 19:38:00 -07001998
Svet Ganov53a441c2016-04-19 19:38:00 -07001999 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002000 }
2001
Svet Ganov53a441c2016-04-19 19:38:00 -07002002 private void maybeNotifyProfiles(int type, int userId, Uri uri, String name,
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002003 Set<String> keysCloned) {
2004 if (keysCloned.contains(name)) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002005 for (int profileId : mUserManager.getProfileIdsWithDisabled(userId)) {
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002006 // the notification for userId has already been sent.
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002007 if (profileId != userId) {
Svetoslav7e0683b2015-08-03 16:02:52 -07002008 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002009 profileId, 0, uri).sendToTarget();
Svet Ganov53a441c2016-04-19 19:38:00 -07002010 final int key = makeKey(type, profileId);
2011 mGenerationRegistry.incrementGeneration(key);
2012
2013 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002014 }
2015 }
2016 }
Svetoslav683914b2015-01-15 14:22:26 -08002017 }
2018
Svetoslav683914b2015-01-15 14:22:26 -08002019 private boolean isGlobalSettingsKey(int key) {
2020 return getTypeFromKey(key) == SETTINGS_TYPE_GLOBAL;
2021 }
2022
2023 private boolean isSystemSettingsKey(int key) {
2024 return getTypeFromKey(key) == SETTINGS_TYPE_SYSTEM;
2025 }
2026
2027 private boolean isSecureSettingsKey(int key) {
2028 return getTypeFromKey(key) == SETTINGS_TYPE_SECURE;
2029 }
2030
2031 private File getSettingsFile(int key) {
2032 if (isGlobalSettingsKey(key)) {
2033 final int userId = getUserIdFromKey(key);
2034 return new File(Environment.getUserSystemDirectory(userId),
2035 SETTINGS_FILE_GLOBAL);
2036 } else if (isSystemSettingsKey(key)) {
2037 final int userId = getUserIdFromKey(key);
2038 return new File(Environment.getUserSystemDirectory(userId),
2039 SETTINGS_FILE_SYSTEM);
2040 } else if (isSecureSettingsKey(key)) {
2041 final int userId = getUserIdFromKey(key);
2042 return new File(Environment.getUserSystemDirectory(userId),
2043 SETTINGS_FILE_SECURE);
2044 } else {
2045 throw new IllegalArgumentException("Invalid settings key:" + key);
2046 }
2047 }
2048
2049 private Uri getNotificationUriFor(int key, String name) {
2050 if (isGlobalSettingsKey(key)) {
2051 return (name != null) ? Uri.withAppendedPath(Settings.Global.CONTENT_URI, name)
2052 : Settings.Global.CONTENT_URI;
2053 } else if (isSecureSettingsKey(key)) {
2054 return (name != null) ? Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name)
2055 : Settings.Secure.CONTENT_URI;
2056 } else if (isSystemSettingsKey(key)) {
2057 return (name != null) ? Uri.withAppendedPath(Settings.System.CONTENT_URI, name)
2058 : Settings.System.CONTENT_URI;
2059 } else {
2060 throw new IllegalArgumentException("Invalid settings key:" + key);
2061 }
2062 }
2063
2064 private int getMaxBytesPerPackageForType(int type) {
2065 switch (type) {
2066 case SETTINGS_TYPE_GLOBAL:
2067 case SETTINGS_TYPE_SECURE: {
2068 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED;
2069 }
2070
2071 default: {
2072 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_LIMITED;
2073 }
2074 }
2075 }
2076
Svetoslav7e0683b2015-08-03 16:02:52 -07002077 private final class MyHandler extends Handler {
2078 private static final int MSG_NOTIFY_URI_CHANGED = 1;
2079 private static final int MSG_NOTIFY_DATA_CHANGED = 2;
2080
2081 public MyHandler(Looper looper) {
2082 super(looper);
2083 }
2084
2085 @Override
2086 public void handleMessage(Message msg) {
2087 switch (msg.what) {
2088 case MSG_NOTIFY_URI_CHANGED: {
2089 final int userId = msg.arg1;
2090 Uri uri = (Uri) msg.obj;
2091 getContext().getContentResolver().notifyChange(uri, null, true, userId);
2092 if (DEBUG) {
2093 Slog.v(LOG_TAG, "Notifying for " + userId + ": " + uri);
2094 }
2095 } break;
2096
2097 case MSG_NOTIFY_DATA_CHANGED: {
2098 mBackupManager.dataChanged();
2099 } break;
2100 }
2101 }
2102 }
2103
Svetoslav683914b2015-01-15 14:22:26 -08002104 private final class UpgradeController {
Dan Sandler71f85e92016-07-20 13:46:05 -04002105 private static final int SETTINGS_VERSION = 130;
Svetoslav683914b2015-01-15 14:22:26 -08002106
2107 private final int mUserId;
2108
2109 public UpgradeController(int userId) {
2110 mUserId = userId;
2111 }
2112
2113 public void upgradeIfNeededLocked() {
2114 // The version of all settings for a user is the same (all users have secure).
2115 SettingsState secureSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07002116 SETTINGS_TYPE_SECURE, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08002117
2118 // Try an update from the current state.
2119 final int oldVersion = secureSettings.getVersionLocked();
2120 final int newVersion = SETTINGS_VERSION;
2121
Svet Ganovc9755bc2015-03-28 13:21:22 -07002122 // If up do date - done.
Svetoslav683914b2015-01-15 14:22:26 -08002123 if (oldVersion == newVersion) {
2124 return;
2125 }
2126
2127 // Try to upgrade.
2128 final int curVersion = onUpgradeLocked(mUserId, oldVersion, newVersion);
2129
2130 // If upgrade failed start from scratch and upgrade.
2131 if (curVersion != newVersion) {
2132 // Drop state we have for this user.
2133 removeUserStateLocked(mUserId, true);
2134
2135 // Recreate the database.
2136 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), mUserId);
2137 SQLiteDatabase database = dbHelper.getWritableDatabase();
2138 dbHelper.recreateDatabase(database, newVersion, curVersion, oldVersion);
2139
2140 // Migrate the settings for this user.
2141 migrateLegacySettingsForUserLocked(dbHelper, database, mUserId);
2142
2143 // Now upgrade should work fine.
2144 onUpgradeLocked(mUserId, oldVersion, newVersion);
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07002145
2146 // Make a note what happened, so we don't wonder why data was lost
2147 String reason = "Settings rebuilt! Current version: "
2148 + curVersion + " while expected: " + newVersion;
2149 getGlobalSettingsLocked().insertSettingLocked(
2150 Settings.Global.DATABASE_DOWNGRADE_REASON, reason, "android");
Svetoslav683914b2015-01-15 14:22:26 -08002151 }
2152
2153 // Set the global settings version if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07002154 if (mUserId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08002155 SettingsState globalSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07002156 SETTINGS_TYPE_GLOBAL, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08002157 globalSettings.setVersionLocked(newVersion);
2158 }
2159
2160 // Set the secure settings version.
2161 secureSettings.setVersionLocked(newVersion);
2162
2163 // Set the system settings version.
2164 SettingsState systemSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07002165 SETTINGS_TYPE_SYSTEM, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08002166 systemSettings.setVersionLocked(newVersion);
2167 }
2168
2169 private SettingsState getGlobalSettingsLocked() {
Xiaohui Chen43765b72015-08-31 10:57:33 -07002170 return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002171 }
2172
2173 private SettingsState getSecureSettingsLocked(int userId) {
2174 return getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
2175 }
2176
2177 private SettingsState getSystemSettingsLocked(int userId) {
2178 return getSettingsLocked(SETTINGS_TYPE_SYSTEM, userId);
2179 }
2180
Jeff Brown503cffc2015-03-26 18:08:51 -07002181 /**
2182 * You must perform all necessary mutations to bring the settings
2183 * for this user from the old to the new version. When you add a new
2184 * upgrade step you *must* update SETTINGS_VERSION.
2185 *
2186 * This is an example of moving a setting from secure to global.
2187 *
2188 * // v119: Example settings changes.
2189 * if (currentVersion == 118) {
2190 * if (userId == UserHandle.USER_OWNER) {
2191 * // Remove from the secure settings.
2192 * SettingsState secureSettings = getSecureSettingsLocked(userId);
2193 * String name = "example_setting_to_move";
2194 * String value = secureSettings.getSetting(name);
2195 * secureSettings.deleteSetting(name);
2196 *
2197 * // Add to the global settings.
2198 * SettingsState globalSettings = getGlobalSettingsLocked();
2199 * globalSettings.insertSetting(name, value, SettingsState.SYSTEM_PACKAGE_NAME);
2200 * }
2201 *
2202 * // Update the current version.
2203 * currentVersion = 119;
2204 * }
2205 */
Svetoslav683914b2015-01-15 14:22:26 -08002206 private int onUpgradeLocked(int userId, int oldVersion, int newVersion) {
2207 if (DEBUG) {
2208 Slog.w(LOG_TAG, "Upgrading settings for user: " + userId + " from version: "
2209 + oldVersion + " to version: " + newVersion);
2210 }
2211
Jeff Brown503cffc2015-03-26 18:08:51 -07002212 int currentVersion = oldVersion;
Svetoslav683914b2015-01-15 14:22:26 -08002213
John Spurlocke11ae112015-05-11 16:09:03 -04002214 // v119: Reset zen + ringer mode.
2215 if (currentVersion == 118) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07002216 if (userId == UserHandle.USER_SYSTEM) {
John Spurlocke11ae112015-05-11 16:09:03 -04002217 final SettingsState globalSettings = getGlobalSettingsLocked();
2218 globalSettings.updateSettingLocked(Settings.Global.ZEN_MODE,
2219 Integer.toString(Settings.Global.ZEN_MODE_OFF),
2220 SettingsState.SYSTEM_PACKAGE_NAME);
2221 globalSettings.updateSettingLocked(Settings.Global.MODE_RINGER,
2222 Integer.toString(AudioManager.RINGER_MODE_NORMAL),
2223 SettingsState.SYSTEM_PACKAGE_NAME);
2224 }
2225 currentVersion = 119;
2226 }
2227
Jason Monk27bbb2d2015-03-31 16:46:39 -04002228 // v120: Add double tap to wake setting.
2229 if (currentVersion == 119) {
2230 SettingsState secureSettings = getSecureSettingsLocked(userId);
2231 secureSettings.insertSettingLocked(Settings.Secure.DOUBLE_TAP_TO_WAKE,
2232 getContext().getResources().getBoolean(
2233 R.bool.def_double_tap_to_wake) ? "1" : "0",
2234 SettingsState.SYSTEM_PACKAGE_NAME);
2235
2236 currentVersion = 120;
2237 }
2238
Svetoslav7e0683b2015-08-03 16:02:52 -07002239 if (currentVersion == 120) {
2240 // Before 121, we used a different string encoding logic. We just bump the
2241 // version here; SettingsState knows how to handle pre-version 120 files.
2242 currentVersion = 121;
2243 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -07002244
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02002245 if (currentVersion == 121) {
2246 // Version 122: allow OEMs to set a default payment component in resources.
2247 // Note that we only write the default if no default has been set;
2248 // if there is, we just leave the default at whatever it currently is.
2249 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2250 String defaultComponent = (getContext().getResources().getString(
2251 R.string.def_nfc_payment_component));
2252 Setting currentSetting = secureSettings.getSettingLocked(
2253 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT);
2254 if (defaultComponent != null && !defaultComponent.isEmpty() &&
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002255 currentSetting.isNull()) {
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02002256 secureSettings.insertSettingLocked(
2257 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT,
2258 defaultComponent,
2259 SettingsState.SYSTEM_PACKAGE_NAME);
2260 }
2261 currentVersion = 122;
2262 }
Suprabh Shukla269c11e2015-12-02 16:51:16 -08002263
2264 if (currentVersion == 122) {
2265 // Version 123: Adding a default value for the ability to add a user from
2266 // the lock screen.
2267 if (userId == UserHandle.USER_SYSTEM) {
2268 final SettingsState globalSettings = getGlobalSettingsLocked();
2269 Setting currentSetting = globalSettings.getSettingLocked(
2270 Settings.Global.ADD_USERS_WHEN_LOCKED);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002271 if (currentSetting.isNull()) {
Suprabh Shukla269c11e2015-12-02 16:51:16 -08002272 globalSettings.insertSettingLocked(
2273 Settings.Global.ADD_USERS_WHEN_LOCKED,
2274 getContext().getResources().getBoolean(
2275 R.bool.def_add_users_from_lockscreen) ? "1" : "0",
2276 SettingsState.SYSTEM_PACKAGE_NAME);
2277 }
2278 }
2279 currentVersion = 123;
2280 }
Bryce Leebd179282015-12-17 19:01:37 -08002281
2282 if (currentVersion == 123) {
Bryce Leeec85f342015-12-16 13:32:28 -08002283 final SettingsState globalSettings = getGlobalSettingsLocked();
2284 String defaultDisabledProfiles = (getContext().getResources().getString(
2285 R.string.def_bluetooth_disabled_profiles));
2286 globalSettings.insertSettingLocked(Settings.Global.BLUETOOTH_DISABLED_PROFILES,
2287 defaultDisabledProfiles, SettingsState.SYSTEM_PACKAGE_NAME);
Bryce Leebd179282015-12-17 19:01:37 -08002288 currentVersion = 124;
Bryce Leeec85f342015-12-16 13:32:28 -08002289 }
2290
Prathmesh Prabhude16b862016-03-04 15:22:24 -08002291 if (currentVersion == 124) {
2292 // Version 124: allow OEMs to set a default value for whether IME should be
2293 // shown when a physical keyboard is connected.
2294 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2295 Setting currentSetting = secureSettings.getSettingLocked(
2296 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002297 if (currentSetting.isNull()) {
Prathmesh Prabhude16b862016-03-04 15:22:24 -08002298 secureSettings.insertSettingLocked(
2299 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD,
2300 getContext().getResources().getBoolean(
2301 R.bool.def_show_ime_with_hard_keyboard) ? "1" : "0",
2302 SettingsState.SYSTEM_PACKAGE_NAME);
2303 }
2304 currentVersion = 125;
2305 }
2306
Ruben Brunk98576cf2016-03-07 18:54:28 -08002307 if (currentVersion == 125) {
2308 // Version 125: Allow OEMs to set the default VR service.
2309 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2310
2311 Setting currentSetting = secureSettings.getSettingLocked(
2312 Settings.Secure.ENABLED_VR_LISTENERS);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002313 if (currentSetting.isNull()) {
Ruben Brunk98576cf2016-03-07 18:54:28 -08002314 ArraySet<ComponentName> l =
2315 SystemConfig.getInstance().getDefaultVrComponents();
2316
2317 if (l != null && !l.isEmpty()) {
2318 StringBuilder b = new StringBuilder();
2319 boolean start = true;
2320 for (ComponentName c : l) {
2321 if (!start) {
2322 b.append(':');
2323 }
2324 b.append(c.flattenToString());
2325 start = false;
2326 }
2327 secureSettings.insertSettingLocked(
2328 Settings.Secure.ENABLED_VR_LISTENERS, b.toString(),
2329 SettingsState.SYSTEM_PACKAGE_NAME);
2330 }
2331
2332 }
2333 currentVersion = 126;
2334 }
2335
Daniel U02ba6122016-04-01 18:41:42 +01002336 if (currentVersion == 126) {
2337 // Version 126: copy the primary values of LOCK_SCREEN_SHOW_NOTIFICATIONS and
2338 // LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS into managed profile.
2339 if (mUserManager.isManagedProfile(userId)) {
2340 final SettingsState systemSecureSettings =
2341 getSecureSettingsLocked(UserHandle.USER_SYSTEM);
2342
2343 final Setting showNotifications = systemSecureSettings.getSettingLocked(
2344 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09002345 if (!showNotifications.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01002346 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2347 secureSettings.insertSettingLocked(
2348 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
2349 showNotifications.getValue(),
2350 SettingsState.SYSTEM_PACKAGE_NAME);
2351 }
2352
2353 final Setting allowPrivate = systemSecureSettings.getSettingLocked(
2354 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09002355 if (!allowPrivate.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01002356 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2357 secureSettings.insertSettingLocked(
2358 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,
2359 allowPrivate.getValue(),
2360 SettingsState.SYSTEM_PACKAGE_NAME);
2361 }
2362 }
2363 currentVersion = 127;
2364 }
2365
Steven Ngdc20ba62016-04-26 18:19:04 +01002366 if (currentVersion == 127) {
Mahaver Chopra3d9805d2016-07-07 16:25:05 +01002367 // version 127 is no longer used.
Steven Ngdc20ba62016-04-26 18:19:04 +01002368 currentVersion = 128;
2369 }
2370
Julia Reynolds1f721e12016-07-11 08:50:58 -04002371 if (currentVersion == 128) {
2372 // Version 128: Allow OEMs to grant DND access to default apps. Note that
2373 // the new apps are appended to the list of already approved apps.
2374 final SettingsState systemSecureSettings =
2375 getSecureSettingsLocked(userId);
2376
2377 final Setting policyAccess = systemSecureSettings.getSettingLocked(
2378 Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES);
2379 String defaultPolicyAccess = getContext().getResources().getString(
2380 com.android.internal.R.string.config_defaultDndAccessPackages);
2381 if (!TextUtils.isEmpty(defaultPolicyAccess)) {
2382 if (policyAccess.isNull()) {
2383 systemSecureSettings.insertSettingLocked(
2384 Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES,
2385 defaultPolicyAccess,
2386 SettingsState.SYSTEM_PACKAGE_NAME);
2387 } else {
2388 StringBuilder currentSetting =
2389 new StringBuilder(policyAccess.getValue());
2390 currentSetting.append(":");
2391 currentSetting.append(defaultPolicyAccess);
2392 systemSecureSettings.updateSettingLocked(
2393 Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES,
2394 currentSetting.toString(),
2395 SettingsState.SYSTEM_PACKAGE_NAME);
2396 }
2397 }
2398
2399 currentVersion = 129;
2400 }
2401
Dan Sandler71f85e92016-07-20 13:46:05 -04002402 if (currentVersion == 129) {
2403 // default longpress timeout changed from 500 to 400. If unchanged from the old
2404 // default, update to the new default.
2405 final SettingsState systemSecureSettings =
2406 getSecureSettingsLocked(userId);
2407 final String oldValue = systemSecureSettings.getSettingLocked(
2408 Settings.Secure.LONG_PRESS_TIMEOUT).getValue();
2409 if (TextUtils.equals("500", oldValue)) {
2410 systemSecureSettings.insertSettingLocked(
2411 Settings.Secure.LONG_PRESS_TIMEOUT,
2412 String.valueOf(getContext().getResources().getInteger(
2413 R.integer.def_long_press_timeout_millis)),
2414 SettingsState.SYSTEM_PACKAGE_NAME);
2415 }
2416 currentVersion = 130;
2417 }
2418
2419 if (currentVersion != newVersion) {
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07002420 Slog.wtf("SettingsProvider", "warning: upgrading settings database to version "
Dan Sandler71f85e92016-07-20 13:46:05 -04002421 + newVersion + " left it at "
2422 + currentVersion + " instead; this is probably a bug", new Throwable());
2423 if (DEBUG) {
2424 throw new RuntimeException("db upgrade error");
2425 }
2426 }
2427
Jeff Brown503cffc2015-03-26 18:08:51 -07002428 // vXXX: Add new settings above this point.
Svetoslav683914b2015-01-15 14:22:26 -08002429
Jeff Brown503cffc2015-03-26 18:08:51 -07002430 // Return the current version.
2431 return currentVersion;
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -08002432 }
2433 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002434 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07002435}