blob: f1d1b1f6e240b019d12e5073d41cc3d2808df252 [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.providers.settings;
18
Svetoslav683914b2015-01-15 14:22:26 -080019import android.Manifest;
Christopher Tated5fe1472012-09-10 15:48:38 -070020import android.app.ActivityManager;
Xiaohui Chen43765b72015-08-31 10:57:33 -070021import android.app.AppGlobals;
Christopher Tate45281862010-03-05 15:46:30 -080022import android.app.backup.BackupManager;
Christopher Tate06efb532012-08-24 15:29:27 -070023import android.content.BroadcastReceiver;
Ruben Brunk98576cf2016-03-07 18:54:28 -080024import android.content.ComponentName;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070025import android.content.ContentProvider;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070026import android.content.ContentValues;
27import android.content.Context;
Christopher Tate06efb532012-08-24 15:29:27 -070028import android.content.Intent;
29import android.content.IntentFilter;
Svetoslav683914b2015-01-15 14:22:26 -080030import android.content.pm.ApplicationInfo;
Xiaohui Chen43765b72015-08-31 10:57:33 -070031import android.content.pm.IPackageManager;
Svetoslav683914b2015-01-15 14:22:26 -080032import android.content.pm.PackageInfo;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070033import android.content.pm.PackageManager;
Christopher Tate38e7a602013-09-03 16:57:34 -070034import android.content.pm.UserInfo;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070035import android.database.Cursor;
Svetoslav683914b2015-01-15 14:22:26 -080036import android.database.MatrixCursor;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070037import android.database.sqlite.SQLiteDatabase;
38import android.database.sqlite.SQLiteQueryBuilder;
Svetoslav683914b2015-01-15 14:22:26 -080039import android.hardware.camera2.utils.ArrayUtils;
John Spurlocke11ae112015-05-11 16:09:03 -040040import android.media.AudioManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070041import android.net.Uri;
Christopher Tate06efb532012-08-24 15:29:27 -070042import android.os.Binder;
Svetoslav683914b2015-01-15 14:22:26 -080043import android.os.Build;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080044import android.os.Bundle;
Amith Yamasani5cdf7f52013-06-27 15:12:01 -070045import android.os.DropBoxManager;
Svetoslav683914b2015-01-15 14:22:26 -080046import android.os.Environment;
Svetoslav7e0683b2015-08-03 16:02:52 -070047import android.os.Handler;
Svet Ganova8f90262016-05-10 08:44:48 -070048import android.os.HandlerThread;
Svetoslav7e0683b2015-08-03 16:02:52 -070049import android.os.Looper;
50import android.os.Message;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070051import android.os.ParcelFileDescriptor;
Christopher Tate0da13572013-10-13 17:34:49 -070052import android.os.Process;
Xiaohui Chen43765b72015-08-31 10:57:33 -070053import android.os.RemoteException;
Jeff Sharkey413573a2016-02-22 17:52:45 -070054import android.os.SELinux;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -070055import android.os.ServiceManager;
Christopher Tate06efb532012-08-24 15:29:27 -070056import android.os.UserHandle;
57import android.os.UserManager;
Svet Ganov53a441c2016-04-19 19:38:00 -070058import android.os.UserManagerInternal;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070059import android.provider.Settings;
60import android.text.TextUtils;
Andre Lago3fa139c2016-08-04 13:53:44 +010061import android.util.ArrayMap;
Svetoslav683914b2015-01-15 14:22:26 -080062import android.util.ArraySet;
Christopher Tate06efb532012-08-24 15:29:27 -070063import android.util.Slog;
64import android.util.SparseArray;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -070065import android.util.SparseBooleanArray;
John Spurlocke11ae112015-05-11 16:09:03 -040066
Svetoslav683914b2015-01-15 14:22:26 -080067import com.android.internal.annotations.GuardedBy;
68import com.android.internal.content.PackageMonitor;
69import com.android.internal.os.BackgroundThread;
Suprabh Shukla269c11e2015-12-02 16:51:16 -080070import com.android.providers.settings.SettingsState.Setting;
Svet Ganov53a441c2016-04-19 19:38:00 -070071import com.android.server.LocalServices;
Ruben Brunk98576cf2016-03-07 18:54:28 -080072import com.android.server.SystemConfig;
John Spurlocke11ae112015-05-11 16:09:03 -040073
Svetoslav683914b2015-01-15 14:22:26 -080074import java.io.File;
Svetoslavb505ccc2015-02-17 12:41:04 -080075import java.io.FileDescriptor;
Svetoslav683914b2015-01-15 14:22:26 -080076import java.io.FileNotFoundException;
Svetoslavb505ccc2015-02-17 12:41:04 -080077import java.io.PrintWriter;
Svetoslav683914b2015-01-15 14:22:26 -080078import java.security.SecureRandom;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -070079import java.util.ArrayList;
Svetoslav683914b2015-01-15 14:22:26 -080080import java.util.Arrays;
81import java.util.List;
Andre Lago3fa139c2016-08-04 13:53:44 +010082import java.util.Map;
Svetoslav683914b2015-01-15 14:22:26 -080083import java.util.Set;
84import java.util.regex.Pattern;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070085
Svetoslav683914b2015-01-15 14:22:26 -080086/**
87 * <p>
88 * This class is a content provider that publishes the system settings.
89 * It can be accessed via the content provider APIs or via custom call
90 * commands. The latter is a bit faster and is the preferred way to access
91 * the platform settings.
92 * </p>
93 * <p>
94 * There are three settings types, global (with signature level protection
95 * and shared across users), secure (with signature permission level
96 * protection and per user), and system (with dangerous permission level
97 * protection and per user). Global settings are stored under the device owner.
98 * Each of these settings is represented by a {@link
99 * com.android.providers.settings.SettingsState} object mapped to an integer
100 * key derived from the setting type in the most significant bits and user
101 * id in the least significant bits. Settings are synchronously loaded on
102 * instantiation of a SettingsState and asynchronously persisted on mutation.
103 * Settings are stored in the user specific system directory.
104 * </p>
105 * <p>
106 * Apps targeting APIs Lollipop MR1 and lower can add custom settings entries
107 * and get a warning. Targeting higher API version prohibits this as the
108 * system settings are not a place for apps to save their state. When a package
109 * is removed the settings it added are deleted. Apps cannot delete system
110 * settings added by the platform. System settings values are validated to
111 * ensure the clients do not put bad values. Global and secure settings are
112 * changed only by trusted parties, therefore no validation is performed. Also
113 * there is a limit on the amount of app specific settings that can be added
114 * to prevent unlimited growth of the system process memory footprint.
115 * </p>
116 */
117@SuppressWarnings("deprecation")
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700118public class SettingsProvider extends ContentProvider {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700119 static final boolean DEBUG = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700120
Svetoslav Ganov264c7a92016-08-24 17:31:14 -0700121 private static final boolean DROP_DATABASE_ON_MIGRATION = true;
Svetoslav683914b2015-01-15 14:22:26 -0800122
123 private static final String LOG_TAG = "SettingsProvider";
Christopher Tate0da13572013-10-13 17:34:49 -0700124
Christopher Tate06efb532012-08-24 15:29:27 -0700125 private static final String TABLE_SYSTEM = "system";
126 private static final String TABLE_SECURE = "secure";
127 private static final String TABLE_GLOBAL = "global";
Svetoslav683914b2015-01-15 14:22:26 -0800128
129 // Old tables no longer exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 private static final String TABLE_FAVORITES = "favorites";
131 private static final String TABLE_OLD_FAVORITES = "old_favorites";
Svetoslav683914b2015-01-15 14:22:26 -0800132 private static final String TABLE_BLUETOOTH_DEVICES = "bluetooth_devices";
133 private static final String TABLE_BOOKMARKS = "bookmarks";
134 private static final String TABLE_ANDROID_METADATA = "android_metadata";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
Svetoslav683914b2015-01-15 14:22:26 -0800136 // The set of removed legacy tables.
137 private static final Set<String> REMOVED_LEGACY_TABLES = new ArraySet<>();
Christopher Tate06efb532012-08-24 15:29:27 -0700138 static {
Svetoslav683914b2015-01-15 14:22:26 -0800139 REMOVED_LEGACY_TABLES.add(TABLE_FAVORITES);
140 REMOVED_LEGACY_TABLES.add(TABLE_OLD_FAVORITES);
141 REMOVED_LEGACY_TABLES.add(TABLE_BLUETOOTH_DEVICES);
142 REMOVED_LEGACY_TABLES.add(TABLE_BOOKMARKS);
143 REMOVED_LEGACY_TABLES.add(TABLE_ANDROID_METADATA);
144 }
Christopher Tate06efb532012-08-24 15:29:27 -0700145
Svetoslav683914b2015-01-15 14:22:26 -0800146 private static final int MUTATION_OPERATION_INSERT = 1;
147 private static final int MUTATION_OPERATION_DELETE = 2;
148 private static final int MUTATION_OPERATION_UPDATE = 3;
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400149
Svetoslav683914b2015-01-15 14:22:26 -0800150 private static final String[] ALL_COLUMNS = new String[] {
151 Settings.NameValueTable._ID,
152 Settings.NameValueTable.NAME,
153 Settings.NameValueTable.VALUE
154 };
155
Svet Ganov53a441c2016-04-19 19:38:00 -0700156 public static final int SETTINGS_TYPE_GLOBAL = 0;
157 public static final int SETTINGS_TYPE_SYSTEM = 1;
158 public static final int SETTINGS_TYPE_SECURE = 2;
Svetoslav683914b2015-01-15 14:22:26 -0800159
Svet Ganov53a441c2016-04-19 19:38:00 -0700160 public static final int SETTINGS_TYPE_MASK = 0xF0000000;
161 public static final int SETTINGS_TYPE_SHIFT = 28;
162
163 private static final Bundle NULL_SETTING_BUNDLE = Bundle.forPair(
164 Settings.NameValueTable.VALUE, null);
Christopher Tate06efb532012-08-24 15:29:27 -0700165
Svetoslav683914b2015-01-15 14:22:26 -0800166 // Per user secure settings that moved to the for all users global settings.
167 static final Set<String> sSecureMovedToGlobalSettings = new ArraySet<>();
168 static {
169 Settings.Secure.getMovedToGlobalSettings(sSecureMovedToGlobalSettings);
Christopher Tate06efb532012-08-24 15:29:27 -0700170 }
171
Svetoslav683914b2015-01-15 14:22:26 -0800172 // Per user system settings that moved to the for all users global settings.
173 static final Set<String> sSystemMovedToGlobalSettings = new ArraySet<>();
174 static {
175 Settings.System.getMovedToGlobalSettings(sSystemMovedToGlobalSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700176 }
177
Svetoslav683914b2015-01-15 14:22:26 -0800178 // Per user system settings that moved to the per user secure settings.
179 static final Set<String> sSystemMovedToSecureSettings = new ArraySet<>();
180 static {
181 Settings.System.getMovedToSecureSettings(sSystemMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700182 }
183
Svetoslav683914b2015-01-15 14:22:26 -0800184 // Per all users global settings that moved to the per user secure settings.
185 static final Set<String> sGlobalMovedToSecureSettings = new ArraySet<>();
186 static {
187 Settings.Global.getMovedToSecureSettings(sGlobalMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700188 }
189
Svetoslav683914b2015-01-15 14:22:26 -0800190 // Per user secure settings that are cloned for the managed profiles of the user.
191 private static final Set<String> sSecureCloneToManagedSettings = new ArraySet<>();
192 static {
193 Settings.Secure.getCloneToManagedProfileSettings(sSecureCloneToManagedSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700194 }
195
Svetoslav683914b2015-01-15 14:22:26 -0800196 // Per user system settings that are cloned for the managed profiles of the user.
197 private static final Set<String> sSystemCloneToManagedSettings = new ArraySet<>();
198 static {
199 Settings.System.getCloneToManagedProfileSettings(sSystemCloneToManagedSettings);
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400200 }
201
Andre Lago3fa139c2016-08-04 13:53:44 +0100202 // Per user system settings that are cloned from the profile's parent when a dependency
203 // in {@link Settings.Secure} is set to "1".
204 public static final Map<String, String> sSystemCloneFromParentOnDependency = new ArrayMap<>();
205 static {
206 Settings.System.getCloneFromParentOnValueSettings(sSystemCloneFromParentOnDependency);
207 }
208
Svetoslav683914b2015-01-15 14:22:26 -0800209 private final Object mLock = new Object();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700210
Svetoslav683914b2015-01-15 14:22:26 -0800211 @GuardedBy("mLock")
212 private SettingsRegistry mSettingsRegistry;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700213
Svet Ganova8f90262016-05-10 08:44:48 -0700214 @GuardedBy("mLock")
215 private HandlerThread mHandlerThread;
216
Svetoslav7ec28e82015-05-20 17:01:10 -0700217 // We have to call in the user manager with no lock held,
218 private volatile UserManager mUserManager;
Svetoslav683914b2015-01-15 14:22:26 -0800219
Svetoslav7ec28e82015-05-20 17:01:10 -0700220 // We have to call in the package manager with no lock held,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700221 private volatile IPackageManager mPackageManager;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700222
Svet Ganov53a441c2016-04-19 19:38:00 -0700223 public static int makeKey(int type, int userId) {
224 return (type << SETTINGS_TYPE_SHIFT) | userId;
225 }
226
227 public static int getTypeFromKey(int key) {
228 return key >>> SETTINGS_TYPE_SHIFT;
229 }
230
231 public static int getUserIdFromKey(int key) {
232 return key & ~SETTINGS_TYPE_MASK;
233 }
234
235 public static String settingTypeToString(int type) {
236 switch (type) {
237 case SETTINGS_TYPE_GLOBAL: {
238 return "SETTINGS_GLOBAL";
239 }
240 case SETTINGS_TYPE_SECURE: {
241 return "SETTINGS_SECURE";
242 }
243 case SETTINGS_TYPE_SYSTEM: {
244 return "SETTINGS_SYSTEM";
245 }
246 default: {
247 return "UNKNOWN";
248 }
249 }
250 }
251
252 public static String keyToString(int key) {
253 return "Key[user=" + getUserIdFromKey(key) + ";type="
254 + settingTypeToString(getTypeFromKey(key)) + "]";
255 }
256
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700257 @Override
258 public boolean onCreate() {
Svetoslav683914b2015-01-15 14:22:26 -0800259 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700260 mUserManager = UserManager.get(getContext());
261 mPackageManager = AppGlobals.getPackageManager();
Svet Ganova8f90262016-05-10 08:44:48 -0700262 mHandlerThread = new HandlerThread(LOG_TAG,
263 Process.THREAD_PRIORITY_BACKGROUND);
264 mHandlerThread.start();
Svetoslav683914b2015-01-15 14:22:26 -0800265 mSettingsRegistry = new SettingsRegistry();
266 }
267 registerBroadcastReceivers();
Svet Ganov53a441c2016-04-19 19:38:00 -0700268 startWatchingUserRestrictionChanges();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700269 ServiceManager.addService("settings", new SettingsService(this));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700270 return true;
271 }
272
Svetoslav683914b2015-01-15 14:22:26 -0800273 @Override
274 public Bundle call(String method, String name, Bundle args) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700275 final int requestingUserId = getRequestingUserId(args);
276 switch (method) {
277 case Settings.CALL_METHOD_GET_GLOBAL: {
278 Setting setting = getGlobalSetting(name);
Svet Ganov53a441c2016-04-19 19:38:00 -0700279 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav683914b2015-01-15 14:22:26 -0800280 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700281
282 case Settings.CALL_METHOD_GET_SECURE: {
283 Setting setting = getSecureSetting(name, requestingUserId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700284 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700285 }
286
287 case Settings.CALL_METHOD_GET_SYSTEM: {
288 Setting setting = getSystemSetting(name, requestingUserId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700289 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700290 }
291
292 case Settings.CALL_METHOD_PUT_GLOBAL: {
293 String value = getSettingValue(args);
Svet Ganov53a441c2016-04-19 19:38:00 -0700294 insertGlobalSetting(name, value, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700295 break;
296 }
297
298 case Settings.CALL_METHOD_PUT_SECURE: {
299 String value = getSettingValue(args);
Svet Ganov53a441c2016-04-19 19:38:00 -0700300 insertSecureSetting(name, value, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700301 break;
302 }
303
304 case Settings.CALL_METHOD_PUT_SYSTEM: {
305 String value = getSettingValue(args);
306 insertSystemSetting(name, value, requestingUserId);
307 break;
308 }
309
310 default: {
311 Slog.w(LOG_TAG, "call() with invalid method: " + method);
312 } break;
Christopher Tate06efb532012-08-24 15:29:27 -0700313 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700314
Christopher Tate06efb532012-08-24 15:29:27 -0700315 return null;
316 }
317
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800318 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800319 public String getType(Uri uri) {
320 Arguments args = new Arguments(uri, null, null, true);
321 if (TextUtils.isEmpty(args.name)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700322 return "vnd.android.cursor.dir/" + args.table;
323 } else {
Svetoslav7ec28e82015-05-20 17:01:10 -0700324 return "vnd.android.cursor.item/" + args.table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700325 }
326 }
327
328 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800329 public Cursor query(Uri uri, String[] projection, String where, String[] whereArgs,
330 String order) {
331 if (DEBUG) {
332 Slog.v(LOG_TAG, "query() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700333 }
334
Svetoslav683914b2015-01-15 14:22:26 -0800335 Arguments args = new Arguments(uri, where, whereArgs, true);
336 String[] normalizedProjection = normalizeProjection(projection);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700337
Svetoslav683914b2015-01-15 14:22:26 -0800338 // If a legacy table that is gone, done.
339 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
340 return new MatrixCursor(normalizedProjection, 0);
341 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700342
Svetoslav7ec28e82015-05-20 17:01:10 -0700343 switch (args.table) {
344 case TABLE_GLOBAL: {
345 if (args.name != null) {
346 Setting setting = getGlobalSetting(args.name);
347 return packageSettingForQuery(setting, normalizedProjection);
348 } else {
349 return getAllGlobalSettings(projection);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700350 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700351 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700352
Svetoslav7ec28e82015-05-20 17:01:10 -0700353 case TABLE_SECURE: {
354 final int userId = UserHandle.getCallingUserId();
355 if (args.name != null) {
356 Setting setting = getSecureSetting(args.name, userId);
357 return packageSettingForQuery(setting, normalizedProjection);
358 } else {
359 return getAllSecureSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800360 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700361 }
Svetoslav683914b2015-01-15 14:22:26 -0800362
Svetoslav7ec28e82015-05-20 17:01:10 -0700363 case TABLE_SYSTEM: {
364 final int userId = UserHandle.getCallingUserId();
365 if (args.name != null) {
366 Setting setting = getSystemSetting(args.name, userId);
367 return packageSettingForQuery(setting, normalizedProjection);
368 } else {
369 return getAllSystemSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800370 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700371 }
Svetoslav683914b2015-01-15 14:22:26 -0800372
Svetoslav7ec28e82015-05-20 17:01:10 -0700373 default: {
374 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700375 }
376 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700377 }
378
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700379 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800380 public Uri insert(Uri uri, ContentValues values) {
381 if (DEBUG) {
382 Slog.v(LOG_TAG, "insert() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700383 }
384
Svetoslav683914b2015-01-15 14:22:26 -0800385 String table = getValidTableOrThrow(uri);
Christopher Tate06efb532012-08-24 15:29:27 -0700386
Svetoslav683914b2015-01-15 14:22:26 -0800387 // If a legacy table that is gone, done.
388 if (REMOVED_LEGACY_TABLES.contains(table)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 return null;
390 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700391
Svetoslav683914b2015-01-15 14:22:26 -0800392 String name = values.getAsString(Settings.Secure.NAME);
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700393 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800394 return null;
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700395 }
396
Svetoslav683914b2015-01-15 14:22:26 -0800397 String value = values.getAsString(Settings.Secure.VALUE);
398
Svetoslav7ec28e82015-05-20 17:01:10 -0700399 switch (table) {
400 case TABLE_GLOBAL: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700401 if (insertGlobalSetting(name, value, UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700402 return Uri.withAppendedPath(Settings.Global.CONTENT_URI, name);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700403 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700404 } break;
405
406 case TABLE_SECURE: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700407 if (insertSecureSetting(name, value, UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700408 return Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name);
409 }
410 } break;
411
412 case TABLE_SYSTEM: {
413 if (insertSystemSetting(name, value, UserHandle.getCallingUserId())) {
414 return Uri.withAppendedPath(Settings.System.CONTENT_URI, name);
415 }
416 } break;
417
418 default: {
419 throw new IllegalArgumentException("Bad Uri path:" + uri);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700420 }
421 }
422
Svetoslav683914b2015-01-15 14:22:26 -0800423 return null;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700424 }
425
426 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800427 public int bulkInsert(Uri uri, ContentValues[] allValues) {
428 if (DEBUG) {
429 Slog.v(LOG_TAG, "bulkInsert() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700431
Svetoslav683914b2015-01-15 14:22:26 -0800432 int insertionCount = 0;
433 final int valuesCount = allValues.length;
434 for (int i = 0; i < valuesCount; i++) {
435 ContentValues values = allValues[i];
436 if (insert(uri, values) != null) {
437 insertionCount++;
438 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700439 }
Svetoslav683914b2015-01-15 14:22:26 -0800440
441 return insertionCount;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700442 }
443
444 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800445 public int delete(Uri uri, String where, String[] whereArgs) {
446 if (DEBUG) {
447 Slog.v(LOG_TAG, "delete() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700449
Svetoslav683914b2015-01-15 14:22:26 -0800450 Arguments args = new Arguments(uri, where, whereArgs, false);
451
452 // If a legacy table that is gone, done.
453 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
454 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700455 }
Svetoslav683914b2015-01-15 14:22:26 -0800456
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700457 if (!isKeyValid(args.name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800458 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700459 }
Svetoslav683914b2015-01-15 14:22:26 -0800460
Svetoslav7ec28e82015-05-20 17:01:10 -0700461 switch (args.table) {
462 case TABLE_GLOBAL: {
463 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700464 return deleteGlobalSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700465 }
Svetoslav683914b2015-01-15 14:22:26 -0800466
Svetoslav7ec28e82015-05-20 17:01:10 -0700467 case TABLE_SECURE: {
468 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700469 return deleteSecureSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700470 }
Svetoslav683914b2015-01-15 14:22:26 -0800471
Svetoslav7ec28e82015-05-20 17:01:10 -0700472 case TABLE_SYSTEM: {
473 final int userId = UserHandle.getCallingUserId();
474 return deleteSystemSetting(args.name, userId) ? 1 : 0;
475 }
476
477 default: {
478 throw new IllegalArgumentException("Bad Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800479 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700480 }
Svetoslav683914b2015-01-15 14:22:26 -0800481 }
482
483 @Override
484 public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
485 if (DEBUG) {
486 Slog.v(LOG_TAG, "update() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700487 }
Svetoslav683914b2015-01-15 14:22:26 -0800488
489 Arguments args = new Arguments(uri, where, whereArgs, false);
490
491 // If a legacy table that is gone, done.
492 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
493 return 0;
494 }
495
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700496 String name = values.getAsString(Settings.Secure.NAME);
497 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800498 return 0;
499 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700500 String value = values.getAsString(Settings.Secure.VALUE);
Svetoslav683914b2015-01-15 14:22:26 -0800501
Svetoslav7ec28e82015-05-20 17:01:10 -0700502 switch (args.table) {
503 case TABLE_GLOBAL: {
504 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700505 return updateGlobalSetting(args.name, value, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700506 }
Svetoslav683914b2015-01-15 14:22:26 -0800507
Svetoslav7ec28e82015-05-20 17:01:10 -0700508 case TABLE_SECURE: {
509 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700510 return updateSecureSetting(args.name, value, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700511 }
Svetoslav683914b2015-01-15 14:22:26 -0800512
Svetoslav7ec28e82015-05-20 17:01:10 -0700513 case TABLE_SYSTEM: {
514 final int userId = UserHandle.getCallingUserId();
515 return updateSystemSetting(args.name, value, userId) ? 1 : 0;
516 }
Svetoslav683914b2015-01-15 14:22:26 -0800517
Svetoslav7ec28e82015-05-20 17:01:10 -0700518 default: {
519 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800520 }
521 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700522 }
523
524 @Override
525 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
Robin Lee2ab02e22016-07-28 18:41:23 +0100526 final int userId = getUserIdFromUri(uri, UserHandle.getCallingUserId());
527 if (userId != UserHandle.getCallingUserId()) {
528 getContext().enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS,
529 "Access files from the settings of another user");
530 }
531 uri = ContentProvider.getUriWithoutUserId(uri);
532
Andre Lago3fa139c2016-08-04 13:53:44 +0100533 final String cacheRingtoneSetting;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700534 final String cacheName;
535 if (Settings.System.RINGTONE_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100536 cacheRingtoneSetting = Settings.System.RINGTONE;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700537 cacheName = Settings.System.RINGTONE_CACHE;
538 } else if (Settings.System.NOTIFICATION_SOUND_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100539 cacheRingtoneSetting = Settings.System.NOTIFICATION_SOUND;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700540 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
541 } else if (Settings.System.ALARM_ALERT_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100542 cacheRingtoneSetting = Settings.System.ALARM_ALERT;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700543 cacheName = Settings.System.ALARM_ALERT_CACHE;
544 } else {
545 throw new FileNotFoundException("Direct file access no longer supported; "
546 + "ringtone playback is available through android.media.Ringtone");
547 }
548
Andre Lago3fa139c2016-08-04 13:53:44 +0100549 int actualCacheOwner;
550 // Redirect cache to parent if ringtone setting is owned by profile parent
551 synchronized (mLock) {
552 actualCacheOwner = resolveOwningUserIdForSystemSettingLocked(userId,
553 cacheRingtoneSetting);
554 }
555 final File cacheFile = new File(getRingtoneCacheDir(actualCacheOwner), cacheName);
Jeff Sharkey413573a2016-02-22 17:52:45 -0700556 return ParcelFileDescriptor.open(cacheFile, ParcelFileDescriptor.parseMode(mode));
557 }
558
559 private File getRingtoneCacheDir(int userId) {
560 final File cacheDir = new File(Environment.getDataSystemDeDirectory(userId), "ringtones");
561 cacheDir.mkdir();
562 SELinux.restorecon(cacheDir);
563 return cacheDir;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700564 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800565
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700566 public void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800567 synchronized (mLock) {
568 final long identity = Binder.clearCallingIdentity();
569 try {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700570 SparseBooleanArray users = mSettingsRegistry.getKnownUsersLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800571 final int userCount = users.size();
572 for (int i = 0; i < userCount; i++) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700573 dumpForUserLocked(users.keyAt(i), pw);
Svetoslavb505ccc2015-02-17 12:41:04 -0800574 }
575 } finally {
576 Binder.restoreCallingIdentity(identity);
577 }
578 }
579 }
580
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700581 private void dumpForUserLocked(int userId, PrintWriter pw) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700582 if (userId == UserHandle.USER_SYSTEM) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800583 pw.println("GLOBAL SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700584 SettingsState globalSettings = mSettingsRegistry.getSettingsLocked(
585 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700586 if (globalSettings != null) {
587 dumpSettingsLocked(globalSettings, pw);
588 }
589 pw.println();
590
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700591 globalSettings.dumpHistoricalOperations(pw);
Svetoslavb505ccc2015-02-17 12:41:04 -0800592 }
593
594 pw.println("SECURE SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700595 SettingsState secureSettings = mSettingsRegistry.getSettingsLocked(
596 SETTINGS_TYPE_SECURE, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700597 if (secureSettings != null) {
598 dumpSettingsLocked(secureSettings, pw);
599 }
600 pw.println();
601
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700602 secureSettings.dumpHistoricalOperations(pw);
603
Svetoslavb505ccc2015-02-17 12:41:04 -0800604 pw.println("SYSTEM SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700605 SettingsState systemSettings = mSettingsRegistry.getSettingsLocked(
606 SETTINGS_TYPE_SYSTEM, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700607 if (systemSettings != null) {
608 dumpSettingsLocked(systemSettings, pw);
609 }
610 pw.println();
611
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700612 systemSettings.dumpHistoricalOperations(pw);
Svetoslavb505ccc2015-02-17 12:41:04 -0800613 }
614
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700615 private void dumpSettingsLocked(SettingsState settingsState, PrintWriter pw) {
616 List<String> names = settingsState.getSettingNamesLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800617
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700618 final int nameCount = names.size();
Svetoslavb505ccc2015-02-17 12:41:04 -0800619
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700620 for (int i = 0; i < nameCount; i++) {
621 String name = names.get(i);
622 Setting setting = settingsState.getSettingLocked(name);
623 pw.print("_id:"); pw.print(toDumpString(setting.getId()));
624 pw.print(" name:"); pw.print(toDumpString(name));
625 if (setting.getPackageName() != null) {
626 pw.print(" pkg:"); pw.print(toDumpString(setting.getPackageName()));
627 }
628 pw.print(" value:"); pw.print(toDumpString(setting.getValue()));
Svetoslavb505ccc2015-02-17 12:41:04 -0800629 pw.println();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700630 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800631 }
632
Svetoslav7e0683b2015-08-03 16:02:52 -0700633 private static String toDumpString(String s) {
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700634 if (s != null) {
635 return s;
636 }
637 return "{null}";
638 }
639
Svetoslav683914b2015-01-15 14:22:26 -0800640 private void registerBroadcastReceivers() {
641 IntentFilter userFilter = new IntentFilter();
642 userFilter.addAction(Intent.ACTION_USER_REMOVED);
643 userFilter.addAction(Intent.ACTION_USER_STOPPED);
644
645 getContext().registerReceiver(new BroadcastReceiver() {
646 @Override
647 public void onReceive(Context context, Intent intent) {
648 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700649 UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800650
651 switch (intent.getAction()) {
652 case Intent.ACTION_USER_REMOVED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700653 synchronized (mLock) {
654 mSettingsRegistry.removeUserStateLocked(userId, true);
655 }
Svetoslav683914b2015-01-15 14:22:26 -0800656 } break;
657
658 case Intent.ACTION_USER_STOPPED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700659 synchronized (mLock) {
660 mSettingsRegistry.removeUserStateLocked(userId, false);
661 }
Svetoslav683914b2015-01-15 14:22:26 -0800662 } break;
663 }
664 }
665 }, userFilter);
666
667 PackageMonitor monitor = new PackageMonitor() {
668 @Override
669 public void onPackageRemoved(String packageName, int uid) {
670 synchronized (mLock) {
671 mSettingsRegistry.onPackageRemovedLocked(packageName,
672 UserHandle.getUserId(uid));
673 }
674 }
675 };
676
677 // package changes
678 monitor.register(getContext(), BackgroundThread.getHandler().getLooper(),
679 UserHandle.ALL, true);
680 }
681
Svet Ganov53a441c2016-04-19 19:38:00 -0700682 private void startWatchingUserRestrictionChanges() {
683 // TODO: The current design of settings looking different based on user restrictions
684 // should be reworked to keep them separate and system code should check the setting
685 // first followed by checking the user restriction before performing an operation.
686 UserManagerInternal userManager = LocalServices.getService(UserManagerInternal.class);
687 userManager.addUserRestrictionsListener((int userId, Bundle newRestrictions,
688 Bundle prevRestrictions) -> {
689 // We are changing the settings affected by restrictions to their current
690 // value with a forced update to ensure that all cross profile dependencies
691 // are taken into account. Also make sure the settings update to.. the same
692 // value passes the security checks, so clear binder calling id.
693 if (newRestrictions.containsKey(UserManager.DISALLOW_SHARE_LOCATION)
694 != prevRestrictions.containsKey(UserManager.DISALLOW_SHARE_LOCATION)) {
695 final long identity = Binder.clearCallingIdentity();
696 try {
697 synchronized (mLock) {
698 Setting setting = getSecureSetting(
699 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, userId);
700 updateSecureSetting(Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
701 setting != null ? setting.getValue() : null, userId, true);
702 }
703 } finally {
704 Binder.restoreCallingIdentity(identity);
705 }
706 }
707 if (newRestrictions.containsKey(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)
708 != prevRestrictions.containsKey(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)) {
709 final long identity = Binder.clearCallingIdentity();
710 try {
711 synchronized (mLock) {
712 Setting setting = getGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS);
713 updateGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS,
714 setting != null ? setting.getValue() : null, userId, true);
715 }
716 } finally {
717 Binder.restoreCallingIdentity(identity);
718 }
719 }
720 if (newRestrictions.containsKey(UserManager.DISALLOW_DEBUGGING_FEATURES)
721 != prevRestrictions.containsKey(UserManager.DISALLOW_DEBUGGING_FEATURES)) {
722 final long identity = Binder.clearCallingIdentity();
723 try {
724 synchronized (mLock) {
725 Setting setting = getGlobalSetting(Settings.Global.ADB_ENABLED);
726 updateGlobalSetting(Settings.Global.ADB_ENABLED,
727 setting != null ? setting.getValue() : null, userId, true);
728 }
729 } finally {
730 Binder.restoreCallingIdentity(identity);
731 }
732 }
733 if (newRestrictions.containsKey(UserManager.ENSURE_VERIFY_APPS)
734 != prevRestrictions.containsKey(UserManager.ENSURE_VERIFY_APPS)) {
735 final long identity = Binder.clearCallingIdentity();
736 try {
737 synchronized (mLock) {
738 Setting enable = getGlobalSetting(
739 Settings.Global.PACKAGE_VERIFIER_ENABLE);
740 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_ENABLE,
741 enable != null ? enable.getValue() : null, userId, true);
742 Setting include = getGlobalSetting(
743 Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB);
744 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB,
745 include != null ? include.getValue() : null, userId, true);
746 }
747 } finally {
748 Binder.restoreCallingIdentity(identity);
749 }
750 }
751 if (newRestrictions.containsKey(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)
752 != prevRestrictions.containsKey(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
753 final long identity = Binder.clearCallingIdentity();
754 try {
755 synchronized (mLock) {
756 Setting setting = getGlobalSetting(
757 Settings.Global.PREFERRED_NETWORK_MODE);
758 updateGlobalSetting(Settings.Global.PREFERRED_NETWORK_MODE,
759 setting != null ? setting.getValue() : null, userId, true);
760 }
761 } finally {
762 Binder.restoreCallingIdentity(identity);
763 }
764 }
765 });
766 }
767
Svetoslav7ec28e82015-05-20 17:01:10 -0700768 private Cursor getAllGlobalSettings(String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800769 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700770 Slog.v(LOG_TAG, "getAllGlobalSettings()");
Svetoslav683914b2015-01-15 14:22:26 -0800771 }
772
Svetoslav7ec28e82015-05-20 17:01:10 -0700773 synchronized (mLock) {
774 // Get the settings.
775 SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700776 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800777
Svetoslav7ec28e82015-05-20 17:01:10 -0700778 List<String> names = settingsState.getSettingNamesLocked();
Svetoslav683914b2015-01-15 14:22:26 -0800779
Svetoslav7ec28e82015-05-20 17:01:10 -0700780 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -0800781
Svetoslav7ec28e82015-05-20 17:01:10 -0700782 String[] normalizedProjection = normalizeProjection(projection);
783 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -0800784
Svetoslav7ec28e82015-05-20 17:01:10 -0700785 // Anyone can get the global settings, so no security checks.
786 for (int i = 0; i < nameCount; i++) {
787 String name = names.get(i);
788 Setting setting = settingsState.getSettingLocked(name);
789 appendSettingToCursor(result, setting);
790 }
791
792 return result;
Svetoslav683914b2015-01-15 14:22:26 -0800793 }
Svetoslav683914b2015-01-15 14:22:26 -0800794 }
795
Svetoslav7ec28e82015-05-20 17:01:10 -0700796 private Setting getGlobalSetting(String name) {
Svetoslav683914b2015-01-15 14:22:26 -0800797 if (DEBUG) {
798 Slog.v(LOG_TAG, "getGlobalSetting(" + name + ")");
799 }
800
801 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700802 synchronized (mLock) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700803 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_GLOBAL,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700804 UserHandle.USER_SYSTEM, name);
Svetoslav683914b2015-01-15 14:22:26 -0800805 }
Svetoslav683914b2015-01-15 14:22:26 -0800806 }
807
Svet Ganov53a441c2016-04-19 19:38:00 -0700808 private boolean updateGlobalSetting(String name, String value, int requestingUserId,
809 boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800810 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700811 Slog.v(LOG_TAG, "updateGlobalSetting(" + name + ", " + value + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800812 }
Svet Ganov53a441c2016-04-19 19:38:00 -0700813 return mutateGlobalSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE,
814 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800815 }
816
Svet Ganov53a441c2016-04-19 19:38:00 -0700817 private boolean insertGlobalSetting(String name, String value, int requestingUserId,
818 boolean forceNotify) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700819 if (DEBUG) {
820 Slog.v(LOG_TAG, "insertGlobalSetting(" + name + ", " + value + ")");
821 }
Svet Ganov53a441c2016-04-19 19:38:00 -0700822 return mutateGlobalSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT,
823 forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700824 }
825
Svet Ganov53a441c2016-04-19 19:38:00 -0700826 private boolean deleteGlobalSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800827 if (DEBUG) {
828 Slog.v(LOG_TAG, "deleteGlobalSettingLocked(" + name + ")");
829 }
Svet Ganov53a441c2016-04-19 19:38:00 -0700830 return mutateGlobalSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE,
831 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800832 }
833
Svetoslav7ec28e82015-05-20 17:01:10 -0700834 private boolean mutateGlobalSetting(String name, String value, int requestingUserId,
Svet Ganov53a441c2016-04-19 19:38:00 -0700835 int operation, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800836 // Make sure the caller can change the settings - treated as secure.
837 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
838
Svetoslav683914b2015-01-15 14:22:26 -0800839 // Resolve the userId on whose behalf the call is made.
840 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
841
Makoto Onuki28da2e32015-11-20 11:30:44 -0800842 // If this is a setting that is currently restricted for this user, do not allow
843 // unrestricting changes.
Victor Chang9c7b7062016-07-12 23:47:29 +0100844 if (isGlobalOrSecureSettingRestrictedForUser(name, callingUserId, value,
845 Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -0800846 return false;
847 }
848
849 // Perform the mutation.
Svetoslav7ec28e82015-05-20 17:01:10 -0700850 synchronized (mLock) {
851 switch (operation) {
852 case MUTATION_OPERATION_INSERT: {
853 return mSettingsRegistry
Svet Ganov53a441c2016-04-19 19:38:00 -0700854 .insertSettingLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM,
855 name, value, getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700856 }
Svetoslav683914b2015-01-15 14:22:26 -0800857
Svetoslav7ec28e82015-05-20 17:01:10 -0700858 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700859 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_GLOBAL,
860 UserHandle.USER_SYSTEM, name, forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700861 }
Svetoslav683914b2015-01-15 14:22:26 -0800862
Svetoslav7ec28e82015-05-20 17:01:10 -0700863 case MUTATION_OPERATION_UPDATE: {
864 return mSettingsRegistry
Svet Ganov53a441c2016-04-19 19:38:00 -0700865 .updateSettingLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM,
866 name, value, getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -0700867 }
Svetoslav683914b2015-01-15 14:22:26 -0800868 }
869 }
870
871 return false;
872 }
873
Svetoslav7ec28e82015-05-20 17:01:10 -0700874 private Cursor getAllSecureSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800875 if (DEBUG) {
876 Slog.v(LOG_TAG, "getAllSecureSettings(" + userId + ")");
877 }
878
879 // Resolve the userId on whose behalf the call is made.
880 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
881
Svetoslav7ec28e82015-05-20 17:01:10 -0700882 synchronized (mLock) {
883 List<String> names = mSettingsRegistry.getSettingsNamesLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700884 SETTINGS_TYPE_SECURE, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -0800885
Svetoslav7ec28e82015-05-20 17:01:10 -0700886 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -0800887
Svetoslav7ec28e82015-05-20 17:01:10 -0700888 String[] normalizedProjection = normalizeProjection(projection);
889 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -0800890
Svetoslav7ec28e82015-05-20 17:01:10 -0700891 for (int i = 0; i < nameCount; i++) {
892 String name = names.get(i);
893 // Determine the owning user as some profile settings are cloned from the parent.
894 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
895 name);
Svetoslav683914b2015-01-15 14:22:26 -0800896
Svetoslav7ec28e82015-05-20 17:01:10 -0700897 // Special case for location (sigh).
898 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
Svetoslav Ganov83a1f7f2016-04-27 13:50:49 -0700899 continue;
Svetoslav7ec28e82015-05-20 17:01:10 -0700900 }
Svetoslav683914b2015-01-15 14:22:26 -0800901
Svetoslav7ec28e82015-05-20 17:01:10 -0700902 Setting setting = mSettingsRegistry.getSettingLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700903 SETTINGS_TYPE_SECURE, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -0700904 appendSettingToCursor(result, setting);
Svetoslav683914b2015-01-15 14:22:26 -0800905 }
906
Svetoslav7ec28e82015-05-20 17:01:10 -0700907 return result;
Svetoslav683914b2015-01-15 14:22:26 -0800908 }
Svetoslav683914b2015-01-15 14:22:26 -0800909 }
910
Svetoslav7ec28e82015-05-20 17:01:10 -0700911 private Setting getSecureSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -0800912 if (DEBUG) {
913 Slog.v(LOG_TAG, "getSecureSetting(" + name + ", " + requestingUserId + ")");
914 }
915
916 // Resolve the userId on whose behalf the call is made.
917 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
918
919 // Determine the owning user as some profile settings are cloned from the parent.
920 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
921
922 // Special case for location (sigh).
923 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700924 SettingsState settings = mSettingsRegistry.getSettingsLocked(SETTINGS_TYPE_SECURE,
925 owningUserId);
926 return settings != null ? settings.getNullSetting() : null;
Svetoslav683914b2015-01-15 14:22:26 -0800927 }
928
929 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700930 synchronized (mLock) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700931 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav7ec28e82015-05-20 17:01:10 -0700932 owningUserId, name);
933 }
Svetoslav683914b2015-01-15 14:22:26 -0800934 }
935
Svet Ganov53a441c2016-04-19 19:38:00 -0700936 private boolean insertSecureSetting(String name, String value, int requestingUserId,
937 boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800938 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700939 Slog.v(LOG_TAG, "insertSecureSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -0800940 + requestingUserId + ")");
941 }
942
Svet Ganov53a441c2016-04-19 19:38:00 -0700943 return mutateSecureSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT,
944 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800945 }
946
Svet Ganov53a441c2016-04-19 19:38:00 -0700947 private boolean deleteSecureSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800948 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700949 Slog.v(LOG_TAG, "deleteSecureSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800950 }
951
Svet Ganov53a441c2016-04-19 19:38:00 -0700952 return mutateSecureSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE,
953 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800954 }
955
Svet Ganov53a441c2016-04-19 19:38:00 -0700956 private boolean updateSecureSetting(String name, String value, int requestingUserId,
957 boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800958 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700959 Slog.v(LOG_TAG, "updateSecureSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -0800960 + requestingUserId + ")");
961 }
962
Svet Ganov53a441c2016-04-19 19:38:00 -0700963 return mutateSecureSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE,
964 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800965 }
966
Svetoslav7ec28e82015-05-20 17:01:10 -0700967 private boolean mutateSecureSetting(String name, String value, int requestingUserId,
Svet Ganov53a441c2016-04-19 19:38:00 -0700968 int operation, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800969 // Make sure the caller can change the settings.
970 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
971
Svetoslav683914b2015-01-15 14:22:26 -0800972 // Resolve the userId on whose behalf the call is made.
973 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
974
Makoto Onuki28da2e32015-11-20 11:30:44 -0800975 // If this is a setting that is currently restricted for this user, do not allow
976 // unrestricting changes.
Victor Chang9c7b7062016-07-12 23:47:29 +0100977 if (isGlobalOrSecureSettingRestrictedForUser(name, callingUserId, value,
978 Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -0800979 return false;
980 }
981
982 // Determine the owning user as some profile settings are cloned from the parent.
983 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
984
985 // Only the owning user can change the setting.
986 if (owningUserId != callingUserId) {
987 return false;
988 }
989
990 // Special cases for location providers (sigh).
991 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700992 return updateLocationProvidersAllowedLocked(value, owningUserId, forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -0800993 }
994
995 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700996 synchronized (mLock) {
997 switch (operation) {
998 case MUTATION_OPERATION_INSERT: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700999 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
1000 owningUserId, name, value, getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -07001001 }
Svetoslav683914b2015-01-15 14:22:26 -08001002
Svetoslav7ec28e82015-05-20 17:01:10 -07001003 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001004 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SECURE,
1005 owningUserId, name, forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -07001006 }
Svetoslav683914b2015-01-15 14:22:26 -08001007
Svetoslav7ec28e82015-05-20 17:01:10 -07001008 case MUTATION_OPERATION_UPDATE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001009 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SECURE,
1010 owningUserId, name, value, getCallingPackage(), forceNotify);
Svetoslav7ec28e82015-05-20 17:01:10 -07001011 }
Svetoslav683914b2015-01-15 14:22:26 -08001012 }
1013 }
1014
1015 return false;
1016 }
1017
Svetoslav7ec28e82015-05-20 17:01:10 -07001018 private Cursor getAllSystemSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001019 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001020 Slog.v(LOG_TAG, "getAllSecureSystem(" + userId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001021 }
1022
1023 // Resolve the userId on whose behalf the call is made.
1024 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
1025
Svetoslav7ec28e82015-05-20 17:01:10 -07001026 synchronized (mLock) {
1027 List<String> names = mSettingsRegistry.getSettingsNamesLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001028 SETTINGS_TYPE_SYSTEM, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -08001029
Svetoslav7ec28e82015-05-20 17:01:10 -07001030 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001031
Svetoslav7ec28e82015-05-20 17:01:10 -07001032 String[] normalizedProjection = normalizeProjection(projection);
1033 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001034
Svetoslav7ec28e82015-05-20 17:01:10 -07001035 for (int i = 0; i < nameCount; i++) {
1036 String name = names.get(i);
Svetoslav683914b2015-01-15 14:22:26 -08001037
Svetoslav7ec28e82015-05-20 17:01:10 -07001038 // Determine the owning user as some profile settings are cloned from the parent.
1039 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId,
1040 name);
Svetoslav683914b2015-01-15 14:22:26 -08001041
Svetoslav7ec28e82015-05-20 17:01:10 -07001042 Setting setting = mSettingsRegistry.getSettingLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001043 SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001044 appendSettingToCursor(result, setting);
1045 }
1046
1047 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001048 }
Svetoslav683914b2015-01-15 14:22:26 -08001049 }
1050
Svetoslav7ec28e82015-05-20 17:01:10 -07001051 private Setting getSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001052 if (DEBUG) {
1053 Slog.v(LOG_TAG, "getSystemSetting(" + name + ", " + requestingUserId + ")");
1054 }
1055
1056 // Resolve the userId on whose behalf the call is made.
1057 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1058
1059 // Determine the owning user as some profile settings are cloned from the parent.
1060 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1061
1062 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001063 synchronized (mLock) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001064 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001065 }
Svetoslav683914b2015-01-15 14:22:26 -08001066 }
1067
Svetoslav7ec28e82015-05-20 17:01:10 -07001068 private boolean insertSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001069 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001070 Slog.v(LOG_TAG, "insertSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001071 + requestingUserId + ")");
1072 }
1073
Svetoslav7ec28e82015-05-20 17:01:10 -07001074 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT);
Svetoslav683914b2015-01-15 14:22:26 -08001075 }
1076
Svetoslav7ec28e82015-05-20 17:01:10 -07001077 private boolean deleteSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001078 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001079 Slog.v(LOG_TAG, "deleteSystemSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001080 }
1081
Svetoslav7ec28e82015-05-20 17:01:10 -07001082 return mutateSystemSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE);
Svetoslav683914b2015-01-15 14:22:26 -08001083 }
1084
Svetoslav7ec28e82015-05-20 17:01:10 -07001085 private boolean updateSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001086 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001087 Slog.v(LOG_TAG, "updateSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001088 + requestingUserId + ")");
1089 }
1090
Svetoslav7ec28e82015-05-20 17:01:10 -07001091 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE);
Svetoslav683914b2015-01-15 14:22:26 -08001092 }
1093
Svetoslav7ec28e82015-05-20 17:01:10 -07001094 private boolean mutateSystemSetting(String name, String value, int runAsUserId,
Svetoslav683914b2015-01-15 14:22:26 -08001095 int operation) {
Billy Lau6ad2d662015-07-18 00:26:58 +01001096 if (!hasWriteSecureSettingsPermission()) {
1097 // If the caller doesn't hold WRITE_SECURE_SETTINGS, we verify whether this
1098 // operation is allowed for the calling package through appops.
1099 if (!Settings.checkAndNoteWriteSettingsOperation(getContext(),
1100 Binder.getCallingUid(), getCallingPackage(), true)) {
1101 return false;
1102 }
Svetoslav683914b2015-01-15 14:22:26 -08001103 }
1104
Svetoslav683914b2015-01-15 14:22:26 -08001105 // Resolve the userId on whose behalf the call is made.
1106 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId);
1107
Svetoslavd8d25e02015-11-20 13:09:26 -08001108 // Enforce what the calling package can mutate the system settings.
1109 enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name, callingUserId);
1110
Svetoslav683914b2015-01-15 14:22:26 -08001111 // Determine the owning user as some profile settings are cloned from the parent.
1112 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1113
1114 // Only the owning user id can change the setting.
1115 if (owningUserId != callingUserId) {
1116 return false;
1117 }
1118
Jeff Sharkey413573a2016-02-22 17:52:45 -07001119 // Invalidate any relevant cache files
1120 String cacheName = null;
1121 if (Settings.System.RINGTONE.equals(name)) {
1122 cacheName = Settings.System.RINGTONE_CACHE;
1123 } else if (Settings.System.NOTIFICATION_SOUND.equals(name)) {
1124 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
1125 } else if (Settings.System.ALARM_ALERT.equals(name)) {
1126 cacheName = Settings.System.ALARM_ALERT_CACHE;
1127 }
1128 if (cacheName != null) {
1129 final File cacheFile = new File(
Andre Lago3fa139c2016-08-04 13:53:44 +01001130 getRingtoneCacheDir(owningUserId), cacheName);
Jeff Sharkey413573a2016-02-22 17:52:45 -07001131 cacheFile.delete();
1132 }
1133
Svetoslav683914b2015-01-15 14:22:26 -08001134 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001135 synchronized (mLock) {
1136 switch (operation) {
1137 case MUTATION_OPERATION_INSERT: {
1138 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001139 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SYSTEM,
1140 owningUserId, name, value, getCallingPackage(), false);
Svetoslav7ec28e82015-05-20 17:01:10 -07001141 }
1142
1143 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001144 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SYSTEM,
1145 owningUserId, name, false);
Svetoslav7ec28e82015-05-20 17:01:10 -07001146 }
1147
1148 case MUTATION_OPERATION_UPDATE: {
1149 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001150 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SYSTEM,
1151 owningUserId, name, value, getCallingPackage(), false);
Svetoslav7ec28e82015-05-20 17:01:10 -07001152 }
Svetoslav683914b2015-01-15 14:22:26 -08001153 }
1154
Svetoslav7ec28e82015-05-20 17:01:10 -07001155 return false;
Svetoslav683914b2015-01-15 14:22:26 -08001156 }
Svetoslav683914b2015-01-15 14:22:26 -08001157 }
1158
Billy Lau6ad2d662015-07-18 00:26:58 +01001159 private boolean hasWriteSecureSettingsPermission() {
Svetoslavf41334b2015-06-23 12:06:03 -07001160 // Write secure settings is a more protected permission. If caller has it we are good.
1161 if (getContext().checkCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS)
1162 == PackageManager.PERMISSION_GRANTED) {
1163 return true;
1164 }
1165
Svetoslavf41334b2015-06-23 12:06:03 -07001166 return false;
1167 }
1168
Svetoslav683914b2015-01-15 14:22:26 -08001169 private void validateSystemSettingValue(String name, String value) {
1170 Settings.System.Validator validator = Settings.System.VALIDATORS.get(name);
1171 if (validator != null && !validator.validate(value)) {
1172 throw new IllegalArgumentException("Invalid value: " + value
1173 + " for setting: " + name);
1174 }
1175 }
1176
1177 private boolean isLocationProvidersAllowedRestricted(String name, int callingUserId,
1178 int owningUserId) {
1179 // Optimization - location providers are restricted only for managed profiles.
1180 if (callingUserId == owningUserId) {
1181 return false;
1182 }
1183 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)
1184 && mUserManager.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION,
1185 new UserHandle(callingUserId))) {
1186 return true;
1187 }
1188 return false;
1189 }
1190
Makoto Onuki28da2e32015-11-20 11:30:44 -08001191 /**
1192 * Checks whether changing a setting to a value is prohibited by the corresponding user
1193 * restriction.
1194 *
Svet Ganov53a441c2016-04-19 19:38:00 -07001195 * <p>See also {@link com.android.server.pm.UserRestrictionsUtils#applyUserRestriction(
1196 * Context, int, String, boolean)}, which should be in sync with this method.
Makoto Onuki28da2e32015-11-20 11:30:44 -08001197 *
1198 * @return true if the change is prohibited, false if the change is allowed.
1199 */
1200 private boolean isGlobalOrSecureSettingRestrictedForUser(String setting, int userId,
Victor Chang9c7b7062016-07-12 23:47:29 +01001201 String value, int callingUid) {
Makoto Onuki28da2e32015-11-20 11:30:44 -08001202 String restriction;
1203 switch (setting) {
1204 case Settings.Secure.LOCATION_MODE:
1205 // Note LOCATION_MODE will be converted into LOCATION_PROVIDERS_ALLOWED
1206 // in android.provider.Settings.Secure.putStringForUser(), so we shouldn't come
1207 // here normally, but we still protect it here from a direct provider write.
1208 if (String.valueOf(Settings.Secure.LOCATION_MODE_OFF).equals(value)) return false;
1209 restriction = UserManager.DISALLOW_SHARE_LOCATION;
1210 break;
1211
1212 case Settings.Secure.LOCATION_PROVIDERS_ALLOWED:
1213 // See SettingsProvider.updateLocationProvidersAllowedLocked. "-" is to disable
1214 // a provider, which should be allowed even if the user restriction is set.
1215 if (value != null && value.startsWith("-")) return false;
1216 restriction = UserManager.DISALLOW_SHARE_LOCATION;
1217 break;
1218
1219 case Settings.Secure.INSTALL_NON_MARKET_APPS:
1220 if ("0".equals(value)) return false;
1221 restriction = UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES;
1222 break;
1223
1224 case Settings.Global.ADB_ENABLED:
1225 if ("0".equals(value)) return false;
1226 restriction = UserManager.DISALLOW_DEBUGGING_FEATURES;
1227 break;
1228
1229 case Settings.Global.PACKAGE_VERIFIER_ENABLE:
1230 case Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB:
1231 if ("1".equals(value)) return false;
1232 restriction = UserManager.ENSURE_VERIFY_APPS;
1233 break;
1234
1235 case Settings.Global.PREFERRED_NETWORK_MODE:
1236 restriction = UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS;
1237 break;
1238
Victor Chang9c7b7062016-07-12 23:47:29 +01001239 case Settings.Secure.ALWAYS_ON_VPN_APP:
1240 case Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN:
1241 // Whitelist system uid (ConnectivityService) and root uid to change always-on vpn
1242 if (callingUid == Process.SYSTEM_UID || callingUid == Process.ROOT_UID) {
1243 return false;
1244 }
1245 restriction = UserManager.DISALLOW_CONFIG_VPN;
1246 break;
1247
Benjamin Franz0ff13fc2016-07-12 13:42:21 +01001248 case Settings.Global.SAFE_BOOT_DISALLOWED:
1249 if ("1".equals(value)) return false;
1250 restriction = UserManager.DISALLOW_SAFE_BOOT;
1251 break;
1252
Makoto Onuki28da2e32015-11-20 11:30:44 -08001253 default:
Mahaver Chopra87648752016-01-08 19:23:57 +00001254 if (setting != null && setting.startsWith(Settings.Global.DATA_ROAMING)) {
Mahaver Chopradea471e2015-12-17 11:02:37 +00001255 if ("0".equals(value)) return false;
1256 restriction = UserManager.DISALLOW_DATA_ROAMING;
1257 break;
1258 }
Makoto Onuki28da2e32015-11-20 11:30:44 -08001259 return false;
Svetoslav683914b2015-01-15 14:22:26 -08001260 }
Makoto Onuki28da2e32015-11-20 11:30:44 -08001261
1262 return mUserManager.hasUserRestriction(restriction, UserHandle.of(userId));
Svetoslav683914b2015-01-15 14:22:26 -08001263 }
1264
1265 private int resolveOwningUserIdForSecureSettingLocked(int userId, String setting) {
1266 return resolveOwningUserIdLocked(userId, sSecureCloneToManagedSettings, setting);
1267 }
1268
1269 private int resolveOwningUserIdForSystemSettingLocked(int userId, String setting) {
Andre Lago3fa139c2016-08-04 13:53:44 +01001270 final int parentId;
1271 // Resolves dependency if setting has a dependency and the calling user has a parent
1272 if (sSystemCloneFromParentOnDependency.containsKey(setting)
1273 && (parentId = getGroupParentLocked(userId)) != userId) {
1274 // The setting has a dependency and the profile has a parent
1275 String dependency = sSystemCloneFromParentOnDependency.get(setting);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001276 Setting settingObj = getSecureSetting(dependency, userId);
1277 if (settingObj != null && settingObj.getValue().equals("1")) {
Andre Lago3fa139c2016-08-04 13:53:44 +01001278 return parentId;
1279 }
1280 }
Svetoslav683914b2015-01-15 14:22:26 -08001281 return resolveOwningUserIdLocked(userId, sSystemCloneToManagedSettings, setting);
1282 }
1283
1284 private int resolveOwningUserIdLocked(int userId, Set<String> keys, String name) {
1285 final int parentId = getGroupParentLocked(userId);
1286 if (parentId != userId && keys.contains(name)) {
1287 return parentId;
1288 }
1289 return userId;
1290 }
1291
Svetoslavf41334b2015-06-23 12:06:03 -07001292 private void enforceRestrictedSystemSettingsMutationForCallingPackage(int operation,
Xiaohui Chen43765b72015-08-31 10:57:33 -07001293 String name, int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001294 // System/root/shell can mutate whatever secure settings they want.
1295 final int callingUid = Binder.getCallingUid();
1296 if (callingUid == android.os.Process.SYSTEM_UID
1297 || callingUid == Process.SHELL_UID
1298 || callingUid == Process.ROOT_UID) {
1299 return;
1300 }
1301
1302 switch (operation) {
1303 case MUTATION_OPERATION_INSERT:
1304 // Insert updates.
1305 case MUTATION_OPERATION_UPDATE: {
1306 if (Settings.System.PUBLIC_SETTINGS.contains(name)) {
1307 return;
1308 }
1309
1310 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001311 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001312
1313 // Privileged apps can do whatever they want.
1314 if ((packageInfo.applicationInfo.privateFlags
1315 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1316 return;
1317 }
1318
1319 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1320 packageInfo.applicationInfo.targetSdkVersion, name);
1321 } break;
1322
1323 case MUTATION_OPERATION_DELETE: {
1324 if (Settings.System.PUBLIC_SETTINGS.contains(name)
1325 || Settings.System.PRIVATE_SETTINGS.contains(name)) {
1326 throw new IllegalArgumentException("You cannot delete system defined"
1327 + " secure settings.");
1328 }
1329
1330 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001331 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001332
1333 // Privileged apps can do whatever they want.
1334 if ((packageInfo.applicationInfo.privateFlags &
1335 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1336 return;
1337 }
1338
1339 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1340 packageInfo.applicationInfo.targetSdkVersion, name);
1341 } break;
1342 }
1343 }
1344
Xiaohui Chen43765b72015-08-31 10:57:33 -07001345 private PackageInfo getCallingPackageInfoOrThrow(int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001346 try {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001347 PackageInfo packageInfo = mPackageManager.getPackageInfo(
1348 getCallingPackage(), 0, userId);
1349 if (packageInfo != null) {
1350 return packageInfo;
1351 }
Xiaohui Chen43765b72015-08-31 10:57:33 -07001352 } catch (RemoteException e) {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001353 /* ignore */
Svetoslav683914b2015-01-15 14:22:26 -08001354 }
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001355 throw new IllegalStateException("Calling package doesn't exist");
Svetoslav683914b2015-01-15 14:22:26 -08001356 }
1357
1358 private int getGroupParentLocked(int userId) {
1359 // Most frequent use case.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001360 if (userId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08001361 return userId;
1362 }
1363 // We are in the same process with the user manager and the returned
1364 // user info is a cached instance, so just look up instead of cache.
1365 final long identity = Binder.clearCallingIdentity();
1366 try {
Svetoslav7ec28e82015-05-20 17:01:10 -07001367 // Just a lookup and not reentrant, so holding a lock is fine.
Svetoslav683914b2015-01-15 14:22:26 -08001368 UserInfo userInfo = mUserManager.getProfileParent(userId);
1369 return (userInfo != null) ? userInfo.id : userId;
1370 } finally {
1371 Binder.restoreCallingIdentity(identity);
1372 }
1373 }
1374
Svetoslav683914b2015-01-15 14:22:26 -08001375 private void enforceWritePermission(String permission) {
1376 if (getContext().checkCallingOrSelfPermission(permission)
1377 != PackageManager.PERMISSION_GRANTED) {
1378 throw new SecurityException("Permission denial: writing to settings requires:"
1379 + permission);
1380 }
1381 }
1382
1383 /*
1384 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
1385 * This setting contains a list of the currently enabled location providers.
1386 * But helper functions in android.providers.Settings can enable or disable
1387 * a single provider by using a "+" or "-" prefix before the provider name.
1388 *
Makoto Onuki28da2e32015-11-20 11:30:44 -08001389 * <p>See also {@link #isGlobalOrSecureSettingRestrictedForUser()}. If DISALLOW_SHARE_LOCATION
1390 * is set, the said method will only allow values with the "-" prefix.
1391 *
Svetoslav683914b2015-01-15 14:22:26 -08001392 * @returns whether the enabled location providers changed.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001393 */
Svet Ganov53a441c2016-04-19 19:38:00 -07001394 private boolean updateLocationProvidersAllowedLocked(String value, int owningUserId,
1395 boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001396 if (TextUtils.isEmpty(value)) {
1397 return false;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001398 }
1399
Svetoslav683914b2015-01-15 14:22:26 -08001400 final char prefix = value.charAt(0);
1401 if (prefix != '+' && prefix != '-') {
Svet Ganov53a441c2016-04-19 19:38:00 -07001402 if (forceNotify) {
1403 final int key = makeKey(SETTINGS_TYPE_SECURE, owningUserId);
1404 mSettingsRegistry.notifyForSettingsChange(key,
1405 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
1406 }
Svetoslav683914b2015-01-15 14:22:26 -08001407 return false;
1408 }
1409
1410 // skip prefix
1411 value = value.substring(1);
1412
Svetoslav7ec28e82015-05-20 17:01:10 -07001413 Setting settingValue = getSecureSetting(
Svetoslav683914b2015-01-15 14:22:26 -08001414 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, owningUserId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001415 if (settingValue == null) {
1416 return false;
1417 }
Svetoslav683914b2015-01-15 14:22:26 -08001418
1419 String oldProviders = (settingValue != null) ? settingValue.getValue() : "";
1420
1421 int index = oldProviders.indexOf(value);
1422 int end = index + value.length();
1423
1424 // check for commas to avoid matching on partial string
1425 if (index > 0 && oldProviders.charAt(index - 1) != ',') {
1426 index = -1;
1427 }
1428
1429 // check for commas to avoid matching on partial string
1430 if (end < oldProviders.length() && oldProviders.charAt(end) != ',') {
1431 index = -1;
1432 }
1433
1434 String newProviders;
1435
1436 if (prefix == '+' && index < 0) {
1437 // append the provider to the list if not present
1438 if (oldProviders.length() == 0) {
1439 newProviders = value;
1440 } else {
1441 newProviders = oldProviders + ',' + value;
1442 }
1443 } else if (prefix == '-' && index >= 0) {
1444 // remove the provider from the list if present
1445 // remove leading or trailing comma
1446 if (index > 0) {
1447 index--;
1448 } else if (end < oldProviders.length()) {
1449 end++;
1450 }
1451
1452 newProviders = oldProviders.substring(0, index);
1453 if (end < oldProviders.length()) {
1454 newProviders += oldProviders.substring(end);
1455 }
1456 } else {
1457 // nothing changed, so no need to update the database
Svet Ganov53a441c2016-04-19 19:38:00 -07001458 if (forceNotify) {
1459 final int key = makeKey(SETTINGS_TYPE_SECURE, owningUserId);
1460 mSettingsRegistry.notifyForSettingsChange(key,
1461 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
1462 }
Svetoslav683914b2015-01-15 14:22:26 -08001463 return false;
1464 }
1465
Svet Ganov53a441c2016-04-19 19:38:00 -07001466 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslavb596a2c2015-02-17 21:37:09 -08001467 owningUserId, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, newProviders,
Svet Ganov53a441c2016-04-19 19:38:00 -07001468 getCallingPackage(), forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -08001469 }
1470
Svetoslav683914b2015-01-15 14:22:26 -08001471 private static void warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1472 int targetSdkVersion, String name) {
1473 // If the app targets Lollipop MR1 or older SDK we warn, otherwise crash.
1474 if (targetSdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) {
1475 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
1476 Slog.w(LOG_TAG, "You shouldn't not change private system settings."
1477 + " This will soon become an error.");
1478 } else {
1479 Slog.w(LOG_TAG, "You shouldn't keep your settings in the secure settings."
1480 + " This will soon become an error.");
1481 }
1482 } else {
1483 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
1484 throw new IllegalArgumentException("You cannot change private secure settings.");
1485 } else {
1486 throw new IllegalArgumentException("You cannot keep your settings in"
1487 + " the secure settings.");
1488 }
1489 }
1490 }
1491
1492 private static int resolveCallingUserIdEnforcingPermissionsLocked(int requestingUserId) {
1493 if (requestingUserId == UserHandle.getCallingUserId()) {
1494 return requestingUserId;
1495 }
1496 return ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1497 Binder.getCallingUid(), requestingUserId, false, true,
1498 "get/set setting for user", null);
1499 }
1500
Svet Ganov53a441c2016-04-19 19:38:00 -07001501 private Bundle packageValueForCallResult(Setting setting,
1502 boolean trackingGeneration) {
1503 if (!trackingGeneration) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001504 if (setting == null || setting.isNull()) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001505 return NULL_SETTING_BUNDLE;
1506 }
1507 return Bundle.forPair(Settings.NameValueTable.VALUE, setting.getValue());
Svetoslav683914b2015-01-15 14:22:26 -08001508 }
Svet Ganov53a441c2016-04-19 19:38:00 -07001509 Bundle result = new Bundle();
1510 result.putString(Settings.NameValueTable.VALUE,
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001511 setting != null && !setting.isNull() ? setting.getValue() : null);
Svet Ganov53a441c2016-04-19 19:38:00 -07001512 mSettingsRegistry.mGenerationRegistry.addGenerationData(result, setting.getkey());
1513 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001514 }
1515
1516 private static int getRequestingUserId(Bundle args) {
1517 final int callingUserId = UserHandle.getCallingUserId();
1518 return (args != null) ? args.getInt(Settings.CALL_METHOD_USER_KEY, callingUserId)
1519 : callingUserId;
1520 }
1521
Svet Ganov53a441c2016-04-19 19:38:00 -07001522 private boolean isTrackingGeneration(Bundle args) {
1523 return args != null && args.containsKey(Settings.CALL_METHOD_TRACK_GENERATION_KEY);
1524 }
1525
Svetoslav683914b2015-01-15 14:22:26 -08001526 private static String getSettingValue(Bundle args) {
1527 return (args != null) ? args.getString(Settings.NameValueTable.VALUE) : null;
1528 }
1529
1530 private static String getValidTableOrThrow(Uri uri) {
1531 if (uri.getPathSegments().size() > 0) {
1532 String table = uri.getPathSegments().get(0);
1533 if (DatabaseHelper.isValidTable(table)) {
1534 return table;
1535 }
1536 throw new IllegalArgumentException("Bad root path: " + table);
1537 }
1538 throw new IllegalArgumentException("Invalid URI:" + uri);
1539 }
1540
1541 private static MatrixCursor packageSettingForQuery(Setting setting, String[] projection) {
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07001542 if (setting.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08001543 return new MatrixCursor(projection, 0);
1544 }
1545 MatrixCursor cursor = new MatrixCursor(projection, 1);
1546 appendSettingToCursor(cursor, setting);
1547 return cursor;
1548 }
1549
1550 private static String[] normalizeProjection(String[] projection) {
1551 if (projection == null) {
1552 return ALL_COLUMNS;
1553 }
1554
1555 final int columnCount = projection.length;
1556 for (int i = 0; i < columnCount; i++) {
1557 String column = projection[i];
1558 if (!ArrayUtils.contains(ALL_COLUMNS, column)) {
1559 throw new IllegalArgumentException("Invalid column: " + column);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001560 }
1561 }
1562
Svetoslav683914b2015-01-15 14:22:26 -08001563 return projection;
1564 }
1565
1566 private static void appendSettingToCursor(MatrixCursor cursor, Setting setting) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001567 if (setting == null || setting.isNull()) {
Suprabh Shuklac9d064a2016-04-12 18:45:34 -07001568 return;
1569 }
Svetoslav683914b2015-01-15 14:22:26 -08001570 final int columnCount = cursor.getColumnCount();
1571
1572 String[] values = new String[columnCount];
1573
1574 for (int i = 0; i < columnCount; i++) {
1575 String column = cursor.getColumnName(i);
1576
1577 switch (column) {
1578 case Settings.NameValueTable._ID: {
1579 values[i] = setting.getId();
1580 } break;
1581
1582 case Settings.NameValueTable.NAME: {
1583 values[i] = setting.getName();
1584 } break;
1585
1586 case Settings.NameValueTable.VALUE: {
1587 values[i] = setting.getValue();
1588 } break;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001589 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001590 }
1591
Svetoslav683914b2015-01-15 14:22:26 -08001592 cursor.addRow(values);
1593 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001594
Makoto Onuki3a2c35782015-06-18 11:21:58 -07001595 private static boolean isKeyValid(String key) {
1596 return !(TextUtils.isEmpty(key) || SettingsState.isBinary(key));
1597 }
1598
Svetoslav683914b2015-01-15 14:22:26 -08001599 private static final class Arguments {
1600 private static final Pattern WHERE_PATTERN_WITH_PARAM_NO_BRACKETS =
1601 Pattern.compile("[\\s]*name[\\s]*=[\\s]*\\?[\\s]*");
1602
1603 private static final Pattern WHERE_PATTERN_WITH_PARAM_IN_BRACKETS =
1604 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*\\?[\\s]*\\)[\\s]*");
1605
1606 private static final Pattern WHERE_PATTERN_NO_PARAM_IN_BRACKETS =
1607 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*\\)[\\s]*");
1608
1609 private static final Pattern WHERE_PATTERN_NO_PARAM_NO_BRACKETS =
1610 Pattern.compile("[\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*");
1611
1612 public final String table;
1613 public final String name;
1614
1615 public Arguments(Uri uri, String where, String[] whereArgs, boolean supportAll) {
1616 final int segmentSize = uri.getPathSegments().size();
1617 switch (segmentSize) {
1618 case 1: {
1619 if (where != null
1620 && (WHERE_PATTERN_WITH_PARAM_NO_BRACKETS.matcher(where).matches()
1621 || WHERE_PATTERN_WITH_PARAM_IN_BRACKETS.matcher(where).matches())
1622 && whereArgs.length == 1) {
1623 name = whereArgs[0];
1624 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08001625 return;
Svetoslav683914b2015-01-15 14:22:26 -08001626 } else if (where != null
1627 && (WHERE_PATTERN_NO_PARAM_NO_BRACKETS.matcher(where).matches()
1628 || WHERE_PATTERN_NO_PARAM_IN_BRACKETS.matcher(where).matches())) {
1629 final int startIndex = Math.max(where.indexOf("'"),
1630 where.indexOf("\"")) + 1;
1631 final int endIndex = Math.max(where.lastIndexOf("'"),
1632 where.lastIndexOf("\""));
1633 name = where.substring(startIndex, endIndex);
1634 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08001635 return;
Svetoslav683914b2015-01-15 14:22:26 -08001636 } else if (supportAll && where == null && whereArgs == null) {
1637 name = null;
1638 table = computeTableForSetting(uri, null);
Svetoslav28494652015-02-12 14:11:42 -08001639 return;
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001640 }
Svetoslav683914b2015-01-15 14:22:26 -08001641 } break;
1642
Svetoslav28494652015-02-12 14:11:42 -08001643 case 2: {
1644 if (where == null && whereArgs == null) {
1645 name = uri.getPathSegments().get(1);
1646 table = computeTableForSetting(uri, name);
1647 return;
1648 }
1649 } break;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001650 }
Svetoslav28494652015-02-12 14:11:42 -08001651
1652 EventLogTags.writeUnsupportedSettingsQuery(
1653 uri.toSafeString(), where, Arrays.toString(whereArgs));
1654 String message = String.format( "Supported SQL:\n"
1655 + " uri content://some_table/some_property with null where and where args\n"
1656 + " uri content://some_table with query name=? and single name as arg\n"
1657 + " uri content://some_table with query name=some_name and null args\n"
1658 + " but got - uri:%1s, where:%2s whereArgs:%3s", uri, where,
1659 Arrays.toString(whereArgs));
1660 throw new IllegalArgumentException(message);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001661 }
1662
Svetoslav28494652015-02-12 14:11:42 -08001663 private static String computeTableForSetting(Uri uri, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08001664 String table = getValidTableOrThrow(uri);
1665
1666 if (name != null) {
1667 if (sSystemMovedToSecureSettings.contains(name)) {
1668 table = TABLE_SECURE;
1669 }
1670
1671 if (sSystemMovedToGlobalSettings.contains(name)) {
1672 table = TABLE_GLOBAL;
1673 }
1674
1675 if (sSecureMovedToGlobalSettings.contains(name)) {
1676 table = TABLE_GLOBAL;
1677 }
1678
1679 if (sGlobalMovedToSecureSettings.contains(name)) {
1680 table = TABLE_SECURE;
1681 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001682 }
Svetoslav683914b2015-01-15 14:22:26 -08001683
1684 return table;
1685 }
1686 }
1687
1688 final class SettingsRegistry {
1689 private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid";
1690
Svetoslav683914b2015-01-15 14:22:26 -08001691 private static final String SETTINGS_FILE_GLOBAL = "settings_global.xml";
1692 private static final String SETTINGS_FILE_SYSTEM = "settings_system.xml";
1693 private static final String SETTINGS_FILE_SECURE = "settings_secure.xml";
1694
1695 private final SparseArray<SettingsState> mSettingsStates = new SparseArray<>();
1696
Svet Ganov53a441c2016-04-19 19:38:00 -07001697 private GenerationRegistry mGenerationRegistry;
Svetoslav683914b2015-01-15 14:22:26 -08001698
Svetoslav7e0683b2015-08-03 16:02:52 -07001699 private final Handler mHandler;
1700
Svet Ganov53a441c2016-04-19 19:38:00 -07001701 private final BackupManager mBackupManager;
1702
Svetoslav683914b2015-01-15 14:22:26 -08001703 public SettingsRegistry() {
Svetoslav7e0683b2015-08-03 16:02:52 -07001704 mHandler = new MyHandler(getContext().getMainLooper());
Svet Ganov53a441c2016-04-19 19:38:00 -07001705 mGenerationRegistry = new GenerationRegistry(mLock);
1706 mBackupManager = new BackupManager(getContext());
Svetoslav683914b2015-01-15 14:22:26 -08001707 migrateAllLegacySettingsIfNeeded();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001708 }
1709
Svetoslav683914b2015-01-15 14:22:26 -08001710 public List<String> getSettingsNamesLocked(int type, int userId) {
1711 final int key = makeKey(type, userId);
1712 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001713 if (settingsState == null) {
1714 return new ArrayList<String>();
1715 }
Svetoslav683914b2015-01-15 14:22:26 -08001716 return settingsState.getSettingNamesLocked();
1717 }
1718
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001719 public SparseBooleanArray getKnownUsersLocked() {
1720 SparseBooleanArray users = new SparseBooleanArray();
1721 for (int i = mSettingsStates.size()-1; i >= 0; i--) {
1722 users.put(getUserIdFromKey(mSettingsStates.keyAt(i)), true);
1723 }
1724 return users;
1725 }
1726
Svetoslav683914b2015-01-15 14:22:26 -08001727 public SettingsState getSettingsLocked(int type, int userId) {
1728 final int key = makeKey(type, userId);
1729 return peekSettingsStateLocked(key);
1730 }
1731
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001732 public boolean ensureSettingsForUserLocked(int userId) {
1733 // First make sure this user actually exists.
1734 if (mUserManager.getUserInfo(userId) == null) {
1735 Slog.wtf(LOG_TAG, "Requested user " + userId + " does not exist");
1736 return false;
1737 }
1738
Svetoslav683914b2015-01-15 14:22:26 -08001739 // Migrate the setting for this user if needed.
1740 migrateLegacySettingsForUserIfNeededLocked(userId);
1741
1742 // Ensure global settings loaded if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001743 if (userId == UserHandle.USER_SYSTEM) {
1744 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001745 ensureSettingsStateLocked(globalKey);
1746 }
1747
1748 // Ensure secure settings loaded.
1749 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1750 ensureSettingsStateLocked(secureKey);
1751
1752 // Make sure the secure settings have an Android id set.
1753 SettingsState secureSettings = getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
1754 ensureSecureSettingAndroidIdSetLocked(secureSettings);
1755
1756 // Ensure system settings loaded.
1757 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1758 ensureSettingsStateLocked(systemKey);
1759
1760 // Upgrade the settings to the latest version.
1761 UpgradeController upgrader = new UpgradeController(userId);
1762 upgrader.upgradeIfNeededLocked();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001763 return true;
Svetoslav683914b2015-01-15 14:22:26 -08001764 }
1765
1766 private void ensureSettingsStateLocked(int key) {
1767 if (mSettingsStates.get(key) == null) {
1768 final int maxBytesPerPackage = getMaxBytesPerPackageForType(getTypeFromKey(key));
1769 SettingsState settingsState = new SettingsState(mLock, getSettingsFile(key), key,
Svetoslav Ganov92057492016-05-16 12:36:43 -07001770 maxBytesPerPackage, mHandlerThread.getLooper());
Svetoslav683914b2015-01-15 14:22:26 -08001771 mSettingsStates.put(key, settingsState);
1772 }
1773 }
1774
1775 public void removeUserStateLocked(int userId, boolean permanently) {
1776 // We always keep the global settings in memory.
1777
1778 // Nuke system settings.
1779 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1780 final SettingsState systemSettingsState = mSettingsStates.get(systemKey);
1781 if (systemSettingsState != null) {
1782 if (permanently) {
1783 mSettingsStates.remove(systemKey);
1784 systemSettingsState.destroyLocked(null);
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001785 } else {
Svetoslav683914b2015-01-15 14:22:26 -08001786 systemSettingsState.destroyLocked(new Runnable() {
1787 @Override
1788 public void run() {
1789 mSettingsStates.remove(systemKey);
1790 }
1791 });
1792 }
1793 }
1794
1795 // Nuke secure settings.
1796 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1797 final SettingsState secureSettingsState = mSettingsStates.get(secureKey);
1798 if (secureSettingsState != null) {
1799 if (permanently) {
1800 mSettingsStates.remove(secureKey);
1801 secureSettingsState.destroyLocked(null);
1802 } else {
1803 secureSettingsState.destroyLocked(new Runnable() {
1804 @Override
1805 public void run() {
1806 mSettingsStates.remove(secureKey);
1807 }
1808 });
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001809 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001810 }
Svet Ganov53a441c2016-04-19 19:38:00 -07001811
1812 // Nuke generation tracking data
1813 mGenerationRegistry.onUserRemoved(userId);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001814 }
1815
Svetoslav683914b2015-01-15 14:22:26 -08001816 public boolean insertSettingLocked(int type, int userId, String name, String value,
Svet Ganov53a441c2016-04-19 19:38:00 -07001817 String packageName, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001818 final int key = makeKey(type, userId);
1819
1820 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001821 final boolean success = settingsState != null
1822 && settingsState.insertSettingLocked(name, value, packageName);
Svetoslav683914b2015-01-15 14:22:26 -08001823
Svet Ganov53a441c2016-04-19 19:38:00 -07001824 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08001825 notifyForSettingsChange(key, name);
1826 }
1827 return success;
1828 }
1829
Svet Ganov53a441c2016-04-19 19:38:00 -07001830 public boolean deleteSettingLocked(int type, int userId, String name, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001831 final int key = makeKey(type, userId);
1832
1833 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001834 if (settingsState == null) {
1835 return false;
1836 }
Svetoslav683914b2015-01-15 14:22:26 -08001837 final boolean success = settingsState.deleteSettingLocked(name);
1838
Svet Ganov53a441c2016-04-19 19:38:00 -07001839 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08001840 notifyForSettingsChange(key, name);
1841 }
1842 return success;
1843 }
1844
1845 public Setting getSettingLocked(int type, int userId, String name) {
1846 final int key = makeKey(type, userId);
1847
1848 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001849 if (settingsState == null) {
1850 return null;
1851 }
Svetoslav683914b2015-01-15 14:22:26 -08001852 return settingsState.getSettingLocked(name);
1853 }
1854
1855 public boolean updateSettingLocked(int type, int userId, String name, String value,
Svet Ganov53a441c2016-04-19 19:38:00 -07001856 String packageName, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001857 final int key = makeKey(type, userId);
1858
1859 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001860 final boolean success = settingsState != null
1861 && settingsState.updateSettingLocked(name, value, packageName);
Svetoslav683914b2015-01-15 14:22:26 -08001862
Svet Ganov53a441c2016-04-19 19:38:00 -07001863 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08001864 notifyForSettingsChange(key, name);
1865 }
1866
1867 return success;
1868 }
1869
1870 public void onPackageRemovedLocked(String packageName, int userId) {
Svet Ganov8de34802015-04-27 09:33:40 -07001871 // Global and secure settings are signature protected. Apps signed
1872 // by the platform certificate are generally not uninstalled and
1873 // the main exception is tests. We trust components signed
1874 // by the platform certificate and do not do a clean up after them.
Svetoslav683914b2015-01-15 14:22:26 -08001875
1876 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1877 SettingsState systemSettings = mSettingsStates.get(systemKey);
Svet Ganov8de34802015-04-27 09:33:40 -07001878 if (systemSettings != null) {
1879 systemSettings.onPackageRemovedLocked(packageName);
1880 }
Svetoslav683914b2015-01-15 14:22:26 -08001881 }
1882
1883 private SettingsState peekSettingsStateLocked(int key) {
1884 SettingsState settingsState = mSettingsStates.get(key);
1885 if (settingsState != null) {
1886 return settingsState;
1887 }
1888
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001889 if (!ensureSettingsForUserLocked(getUserIdFromKey(key))) {
1890 return null;
1891 }
Svetoslav683914b2015-01-15 14:22:26 -08001892 return mSettingsStates.get(key);
1893 }
1894
1895 private void migrateAllLegacySettingsIfNeeded() {
1896 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07001897 final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001898 File globalFile = getSettingsFile(key);
1899 if (globalFile.exists()) {
1900 return;
1901 }
1902
1903 final long identity = Binder.clearCallingIdentity();
1904 try {
1905 List<UserInfo> users = mUserManager.getUsers(true);
1906
1907 final int userCount = users.size();
1908 for (int i = 0; i < userCount; i++) {
1909 final int userId = users.get(i).id;
1910
1911 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
1912 SQLiteDatabase database = dbHelper.getWritableDatabase();
1913 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
1914
1915 // Upgrade to the latest version.
1916 UpgradeController upgrader = new UpgradeController(userId);
1917 upgrader.upgradeIfNeededLocked();
1918
1919 // Drop from memory if not a running user.
1920 if (!mUserManager.isUserRunning(new UserHandle(userId))) {
1921 removeUserStateLocked(userId, false);
1922 }
1923 }
1924 } finally {
1925 Binder.restoreCallingIdentity(identity);
1926 }
1927 }
1928 }
1929
1930 private void migrateLegacySettingsForUserIfNeededLocked(int userId) {
1931 // Every user has secure settings and if no file we need to migrate.
1932 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1933 File secureFile = getSettingsFile(secureKey);
1934 if (secureFile.exists()) {
1935 return;
1936 }
1937
1938 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
1939 SQLiteDatabase database = dbHelper.getWritableDatabase();
1940
1941 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
1942 }
1943
1944 private void migrateLegacySettingsForUserLocked(DatabaseHelper dbHelper,
1945 SQLiteDatabase database, int userId) {
Amith Yamasanibf2ef612016-03-07 16:37:18 -08001946 // Move over the system settings.
1947 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
1948 ensureSettingsStateLocked(systemKey);
1949 SettingsState systemSettings = mSettingsStates.get(systemKey);
1950 migrateLegacySettingsLocked(systemSettings, database, TABLE_SYSTEM);
1951 systemSettings.persistSyncLocked();
Svetoslav683914b2015-01-15 14:22:26 -08001952
1953 // Move over the secure settings.
Amith Yamasanibf2ef612016-03-07 16:37:18 -08001954 // Do this after System settings, since this is the first thing we check when deciding
1955 // to skip over migration from db to xml for a secondary user.
Svetoslav683914b2015-01-15 14:22:26 -08001956 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
1957 ensureSettingsStateLocked(secureKey);
1958 SettingsState secureSettings = mSettingsStates.get(secureKey);
1959 migrateLegacySettingsLocked(secureSettings, database, TABLE_SECURE);
1960 ensureSecureSettingAndroidIdSetLocked(secureSettings);
1961 secureSettings.persistSyncLocked();
1962
Amith Yamasanibf2ef612016-03-07 16:37:18 -08001963 // Move over the global settings if owner.
1964 // Do this last, since this is the first thing we check when deciding
1965 // to skip over migration from db to xml for owner user.
1966 if (userId == UserHandle.USER_SYSTEM) {
1967 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, userId);
1968 ensureSettingsStateLocked(globalKey);
1969 SettingsState globalSettings = mSettingsStates.get(globalKey);
1970 migrateLegacySettingsLocked(globalSettings, database, TABLE_GLOBAL);
1971 globalSettings.persistSyncLocked();
1972 }
Svetoslav683914b2015-01-15 14:22:26 -08001973
1974 // Drop the database as now all is moved and persisted.
1975 if (DROP_DATABASE_ON_MIGRATION) {
1976 dbHelper.dropDatabase();
1977 } else {
1978 dbHelper.backupDatabase();
1979 }
1980 }
1981
1982 private void migrateLegacySettingsLocked(SettingsState settingsState,
1983 SQLiteDatabase database, String table) {
1984 SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
1985 queryBuilder.setTables(table);
1986
1987 Cursor cursor = queryBuilder.query(database, ALL_COLUMNS,
1988 null, null, null, null, null);
1989
1990 if (cursor == null) {
1991 return;
1992 }
1993
1994 try {
1995 if (!cursor.moveToFirst()) {
1996 return;
1997 }
1998
1999 final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME);
2000 final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE);
2001
2002 settingsState.setVersionLocked(database.getVersion());
2003
2004 while (!cursor.isAfterLast()) {
2005 String name = cursor.getString(nameColumnIdx);
2006 String value = cursor.getString(valueColumnIdx);
2007 settingsState.insertSettingLocked(name, value,
2008 SettingsState.SYSTEM_PACKAGE_NAME);
2009 cursor.moveToNext();
2010 }
2011 } finally {
2012 cursor.close();
2013 }
2014 }
2015
2016 private void ensureSecureSettingAndroidIdSetLocked(SettingsState secureSettings) {
2017 Setting value = secureSettings.getSettingLocked(Settings.Secure.ANDROID_ID);
2018
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09002019 if (!value.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08002020 return;
2021 }
2022
2023 final int userId = getUserIdFromKey(secureSettings.mKey);
2024
2025 final UserInfo user;
2026 final long identity = Binder.clearCallingIdentity();
2027 try {
2028 user = mUserManager.getUserInfo(userId);
2029 } finally {
2030 Binder.restoreCallingIdentity(identity);
2031 }
2032 if (user == null) {
2033 // Can happen due to races when deleting users - treat as benign.
2034 return;
2035 }
2036
2037 String androidId = Long.toHexString(new SecureRandom().nextLong());
2038 secureSettings.insertSettingLocked(Settings.Secure.ANDROID_ID, androidId,
2039 SettingsState.SYSTEM_PACKAGE_NAME);
2040
2041 Slog.d(LOG_TAG, "Generated and saved new ANDROID_ID [" + androidId
2042 + "] for user " + userId);
2043
2044 // Write a drop box entry if it's a restricted profile
2045 if (user.isRestricted()) {
2046 DropBoxManager dbm = (DropBoxManager) getContext().getSystemService(
2047 Context.DROPBOX_SERVICE);
2048 if (dbm != null && dbm.isTagEnabled(DROPBOX_TAG_USERLOG)) {
2049 dbm.addText(DROPBOX_TAG_USERLOG, System.currentTimeMillis()
2050 + "," + DROPBOX_TAG_USERLOG + "," + androidId + "\n");
2051 }
2052 }
2053 }
2054
2055 private void notifyForSettingsChange(int key, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08002056 final int userId = getUserIdFromKey(key);
2057 Uri uri = getNotificationUriFor(key, name);
2058
Phil Weaver83fec002016-05-11 10:55:29 -07002059 mGenerationRegistry.incrementGeneration(key);
2060
Svetoslav7e0683b2015-08-03 16:02:52 -07002061 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
2062 userId, 0, uri).sendToTarget();
2063
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002064 if (isSecureSettingsKey(key)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07002065 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
2066 sSecureCloneToManagedSettings);
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002067 } else if (isSystemSettingsKey(key)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07002068 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
2069 sSystemCloneToManagedSettings);
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002070 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002071
Svet Ganov53a441c2016-04-19 19:38:00 -07002072 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002073 }
2074
Svet Ganov53a441c2016-04-19 19:38:00 -07002075 private void maybeNotifyProfiles(int type, int userId, Uri uri, String name,
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002076 Set<String> keysCloned) {
2077 if (keysCloned.contains(name)) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002078 for (int profileId : mUserManager.getProfileIdsWithDisabled(userId)) {
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002079 // the notification for userId has already been sent.
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002080 if (profileId != userId) {
Svetoslav7e0683b2015-08-03 16:02:52 -07002081 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002082 profileId, 0, uri).sendToTarget();
Svet Ganov53a441c2016-04-19 19:38:00 -07002083 final int key = makeKey(type, profileId);
2084 mGenerationRegistry.incrementGeneration(key);
2085
2086 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002087 }
2088 }
2089 }
Svetoslav683914b2015-01-15 14:22:26 -08002090 }
2091
Svetoslav683914b2015-01-15 14:22:26 -08002092 private boolean isGlobalSettingsKey(int key) {
2093 return getTypeFromKey(key) == SETTINGS_TYPE_GLOBAL;
2094 }
2095
2096 private boolean isSystemSettingsKey(int key) {
2097 return getTypeFromKey(key) == SETTINGS_TYPE_SYSTEM;
2098 }
2099
2100 private boolean isSecureSettingsKey(int key) {
2101 return getTypeFromKey(key) == SETTINGS_TYPE_SECURE;
2102 }
2103
2104 private File getSettingsFile(int key) {
2105 if (isGlobalSettingsKey(key)) {
2106 final int userId = getUserIdFromKey(key);
2107 return new File(Environment.getUserSystemDirectory(userId),
2108 SETTINGS_FILE_GLOBAL);
2109 } else if (isSystemSettingsKey(key)) {
2110 final int userId = getUserIdFromKey(key);
2111 return new File(Environment.getUserSystemDirectory(userId),
2112 SETTINGS_FILE_SYSTEM);
2113 } else if (isSecureSettingsKey(key)) {
2114 final int userId = getUserIdFromKey(key);
2115 return new File(Environment.getUserSystemDirectory(userId),
2116 SETTINGS_FILE_SECURE);
2117 } else {
2118 throw new IllegalArgumentException("Invalid settings key:" + key);
2119 }
2120 }
2121
2122 private Uri getNotificationUriFor(int key, String name) {
2123 if (isGlobalSettingsKey(key)) {
2124 return (name != null) ? Uri.withAppendedPath(Settings.Global.CONTENT_URI, name)
2125 : Settings.Global.CONTENT_URI;
2126 } else if (isSecureSettingsKey(key)) {
2127 return (name != null) ? Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name)
2128 : Settings.Secure.CONTENT_URI;
2129 } else if (isSystemSettingsKey(key)) {
2130 return (name != null) ? Uri.withAppendedPath(Settings.System.CONTENT_URI, name)
2131 : Settings.System.CONTENT_URI;
2132 } else {
2133 throw new IllegalArgumentException("Invalid settings key:" + key);
2134 }
2135 }
2136
2137 private int getMaxBytesPerPackageForType(int type) {
2138 switch (type) {
2139 case SETTINGS_TYPE_GLOBAL:
2140 case SETTINGS_TYPE_SECURE: {
2141 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED;
2142 }
2143
2144 default: {
2145 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_LIMITED;
2146 }
2147 }
2148 }
2149
Svetoslav7e0683b2015-08-03 16:02:52 -07002150 private final class MyHandler extends Handler {
2151 private static final int MSG_NOTIFY_URI_CHANGED = 1;
2152 private static final int MSG_NOTIFY_DATA_CHANGED = 2;
2153
2154 public MyHandler(Looper looper) {
2155 super(looper);
2156 }
2157
2158 @Override
2159 public void handleMessage(Message msg) {
2160 switch (msg.what) {
2161 case MSG_NOTIFY_URI_CHANGED: {
2162 final int userId = msg.arg1;
2163 Uri uri = (Uri) msg.obj;
2164 getContext().getContentResolver().notifyChange(uri, null, true, userId);
2165 if (DEBUG) {
2166 Slog.v(LOG_TAG, "Notifying for " + userId + ": " + uri);
2167 }
2168 } break;
2169
2170 case MSG_NOTIFY_DATA_CHANGED: {
2171 mBackupManager.dataChanged();
2172 } break;
2173 }
2174 }
2175 }
2176
Svetoslav683914b2015-01-15 14:22:26 -08002177 private final class UpgradeController {
Adrian Roos69741a22016-10-21 14:49:17 -07002178 private static final int SETTINGS_VERSION = 134;
Svetoslav683914b2015-01-15 14:22:26 -08002179
2180 private final int mUserId;
2181
2182 public UpgradeController(int userId) {
2183 mUserId = userId;
2184 }
2185
2186 public void upgradeIfNeededLocked() {
2187 // The version of all settings for a user is the same (all users have secure).
2188 SettingsState secureSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07002189 SETTINGS_TYPE_SECURE, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08002190
2191 // Try an update from the current state.
2192 final int oldVersion = secureSettings.getVersionLocked();
2193 final int newVersion = SETTINGS_VERSION;
2194
Svet Ganovc9755bc2015-03-28 13:21:22 -07002195 // If up do date - done.
Svetoslav683914b2015-01-15 14:22:26 -08002196 if (oldVersion == newVersion) {
2197 return;
2198 }
2199
2200 // Try to upgrade.
2201 final int curVersion = onUpgradeLocked(mUserId, oldVersion, newVersion);
2202
2203 // If upgrade failed start from scratch and upgrade.
2204 if (curVersion != newVersion) {
2205 // Drop state we have for this user.
2206 removeUserStateLocked(mUserId, true);
2207
2208 // Recreate the database.
2209 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), mUserId);
2210 SQLiteDatabase database = dbHelper.getWritableDatabase();
2211 dbHelper.recreateDatabase(database, newVersion, curVersion, oldVersion);
2212
2213 // Migrate the settings for this user.
2214 migrateLegacySettingsForUserLocked(dbHelper, database, mUserId);
2215
2216 // Now upgrade should work fine.
2217 onUpgradeLocked(mUserId, oldVersion, newVersion);
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07002218
2219 // Make a note what happened, so we don't wonder why data was lost
2220 String reason = "Settings rebuilt! Current version: "
2221 + curVersion + " while expected: " + newVersion;
2222 getGlobalSettingsLocked().insertSettingLocked(
2223 Settings.Global.DATABASE_DOWNGRADE_REASON, reason, "android");
Svetoslav683914b2015-01-15 14:22:26 -08002224 }
2225
2226 // Set the global settings version if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07002227 if (mUserId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08002228 SettingsState globalSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07002229 SETTINGS_TYPE_GLOBAL, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08002230 globalSettings.setVersionLocked(newVersion);
2231 }
2232
2233 // Set the secure settings version.
2234 secureSettings.setVersionLocked(newVersion);
2235
2236 // Set the system settings version.
2237 SettingsState systemSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07002238 SETTINGS_TYPE_SYSTEM, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08002239 systemSettings.setVersionLocked(newVersion);
2240 }
2241
2242 private SettingsState getGlobalSettingsLocked() {
Xiaohui Chen43765b72015-08-31 10:57:33 -07002243 return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002244 }
2245
2246 private SettingsState getSecureSettingsLocked(int userId) {
2247 return getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
2248 }
2249
2250 private SettingsState getSystemSettingsLocked(int userId) {
2251 return getSettingsLocked(SETTINGS_TYPE_SYSTEM, userId);
2252 }
2253
Jeff Brown503cffc2015-03-26 18:08:51 -07002254 /**
2255 * You must perform all necessary mutations to bring the settings
2256 * for this user from the old to the new version. When you add a new
2257 * upgrade step you *must* update SETTINGS_VERSION.
2258 *
2259 * This is an example of moving a setting from secure to global.
2260 *
2261 * // v119: Example settings changes.
2262 * if (currentVersion == 118) {
2263 * if (userId == UserHandle.USER_OWNER) {
2264 * // Remove from the secure settings.
2265 * SettingsState secureSettings = getSecureSettingsLocked(userId);
2266 * String name = "example_setting_to_move";
2267 * String value = secureSettings.getSetting(name);
2268 * secureSettings.deleteSetting(name);
2269 *
2270 * // Add to the global settings.
2271 * SettingsState globalSettings = getGlobalSettingsLocked();
2272 * globalSettings.insertSetting(name, value, SettingsState.SYSTEM_PACKAGE_NAME);
2273 * }
2274 *
2275 * // Update the current version.
2276 * currentVersion = 119;
2277 * }
2278 */
Svetoslav683914b2015-01-15 14:22:26 -08002279 private int onUpgradeLocked(int userId, int oldVersion, int newVersion) {
2280 if (DEBUG) {
2281 Slog.w(LOG_TAG, "Upgrading settings for user: " + userId + " from version: "
2282 + oldVersion + " to version: " + newVersion);
2283 }
2284
Jeff Brown503cffc2015-03-26 18:08:51 -07002285 int currentVersion = oldVersion;
Svetoslav683914b2015-01-15 14:22:26 -08002286
John Spurlocke11ae112015-05-11 16:09:03 -04002287 // v119: Reset zen + ringer mode.
2288 if (currentVersion == 118) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07002289 if (userId == UserHandle.USER_SYSTEM) {
John Spurlocke11ae112015-05-11 16:09:03 -04002290 final SettingsState globalSettings = getGlobalSettingsLocked();
2291 globalSettings.updateSettingLocked(Settings.Global.ZEN_MODE,
2292 Integer.toString(Settings.Global.ZEN_MODE_OFF),
2293 SettingsState.SYSTEM_PACKAGE_NAME);
2294 globalSettings.updateSettingLocked(Settings.Global.MODE_RINGER,
2295 Integer.toString(AudioManager.RINGER_MODE_NORMAL),
2296 SettingsState.SYSTEM_PACKAGE_NAME);
2297 }
2298 currentVersion = 119;
2299 }
2300
Jason Monk27bbb2d2015-03-31 16:46:39 -04002301 // v120: Add double tap to wake setting.
2302 if (currentVersion == 119) {
2303 SettingsState secureSettings = getSecureSettingsLocked(userId);
2304 secureSettings.insertSettingLocked(Settings.Secure.DOUBLE_TAP_TO_WAKE,
2305 getContext().getResources().getBoolean(
2306 R.bool.def_double_tap_to_wake) ? "1" : "0",
2307 SettingsState.SYSTEM_PACKAGE_NAME);
2308
2309 currentVersion = 120;
2310 }
2311
Svetoslav7e0683b2015-08-03 16:02:52 -07002312 if (currentVersion == 120) {
2313 // Before 121, we used a different string encoding logic. We just bump the
2314 // version here; SettingsState knows how to handle pre-version 120 files.
2315 currentVersion = 121;
2316 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -07002317
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02002318 if (currentVersion == 121) {
2319 // Version 122: allow OEMs to set a default payment component in resources.
2320 // Note that we only write the default if no default has been set;
2321 // if there is, we just leave the default at whatever it currently is.
2322 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2323 String defaultComponent = (getContext().getResources().getString(
2324 R.string.def_nfc_payment_component));
2325 Setting currentSetting = secureSettings.getSettingLocked(
2326 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT);
2327 if (defaultComponent != null && !defaultComponent.isEmpty() &&
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002328 currentSetting.isNull()) {
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02002329 secureSettings.insertSettingLocked(
2330 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT,
2331 defaultComponent,
2332 SettingsState.SYSTEM_PACKAGE_NAME);
2333 }
2334 currentVersion = 122;
2335 }
Suprabh Shukla269c11e2015-12-02 16:51:16 -08002336
2337 if (currentVersion == 122) {
2338 // Version 123: Adding a default value for the ability to add a user from
2339 // the lock screen.
2340 if (userId == UserHandle.USER_SYSTEM) {
2341 final SettingsState globalSettings = getGlobalSettingsLocked();
2342 Setting currentSetting = globalSettings.getSettingLocked(
2343 Settings.Global.ADD_USERS_WHEN_LOCKED);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002344 if (currentSetting.isNull()) {
Suprabh Shukla269c11e2015-12-02 16:51:16 -08002345 globalSettings.insertSettingLocked(
2346 Settings.Global.ADD_USERS_WHEN_LOCKED,
2347 getContext().getResources().getBoolean(
2348 R.bool.def_add_users_from_lockscreen) ? "1" : "0",
2349 SettingsState.SYSTEM_PACKAGE_NAME);
2350 }
2351 }
2352 currentVersion = 123;
2353 }
Bryce Leebd179282015-12-17 19:01:37 -08002354
2355 if (currentVersion == 123) {
Bryce Leeec85f342015-12-16 13:32:28 -08002356 final SettingsState globalSettings = getGlobalSettingsLocked();
2357 String defaultDisabledProfiles = (getContext().getResources().getString(
2358 R.string.def_bluetooth_disabled_profiles));
2359 globalSettings.insertSettingLocked(Settings.Global.BLUETOOTH_DISABLED_PROFILES,
2360 defaultDisabledProfiles, SettingsState.SYSTEM_PACKAGE_NAME);
Bryce Leebd179282015-12-17 19:01:37 -08002361 currentVersion = 124;
Bryce Leeec85f342015-12-16 13:32:28 -08002362 }
2363
Prathmesh Prabhude16b862016-03-04 15:22:24 -08002364 if (currentVersion == 124) {
2365 // Version 124: allow OEMs to set a default value for whether IME should be
2366 // shown when a physical keyboard is connected.
2367 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2368 Setting currentSetting = secureSettings.getSettingLocked(
2369 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002370 if (currentSetting.isNull()) {
Prathmesh Prabhude16b862016-03-04 15:22:24 -08002371 secureSettings.insertSettingLocked(
2372 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD,
2373 getContext().getResources().getBoolean(
2374 R.bool.def_show_ime_with_hard_keyboard) ? "1" : "0",
2375 SettingsState.SYSTEM_PACKAGE_NAME);
2376 }
2377 currentVersion = 125;
2378 }
2379
Ruben Brunk98576cf2016-03-07 18:54:28 -08002380 if (currentVersion == 125) {
2381 // Version 125: Allow OEMs to set the default VR service.
2382 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2383
2384 Setting currentSetting = secureSettings.getSettingLocked(
2385 Settings.Secure.ENABLED_VR_LISTENERS);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002386 if (currentSetting.isNull()) {
Ruben Brunk98576cf2016-03-07 18:54:28 -08002387 ArraySet<ComponentName> l =
2388 SystemConfig.getInstance().getDefaultVrComponents();
2389
2390 if (l != null && !l.isEmpty()) {
2391 StringBuilder b = new StringBuilder();
2392 boolean start = true;
2393 for (ComponentName c : l) {
2394 if (!start) {
2395 b.append(':');
2396 }
2397 b.append(c.flattenToString());
2398 start = false;
2399 }
2400 secureSettings.insertSettingLocked(
2401 Settings.Secure.ENABLED_VR_LISTENERS, b.toString(),
2402 SettingsState.SYSTEM_PACKAGE_NAME);
2403 }
2404
2405 }
2406 currentVersion = 126;
2407 }
2408
Daniel U02ba6122016-04-01 18:41:42 +01002409 if (currentVersion == 126) {
2410 // Version 126: copy the primary values of LOCK_SCREEN_SHOW_NOTIFICATIONS and
2411 // LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS into managed profile.
2412 if (mUserManager.isManagedProfile(userId)) {
2413 final SettingsState systemSecureSettings =
2414 getSecureSettingsLocked(UserHandle.USER_SYSTEM);
2415
2416 final Setting showNotifications = systemSecureSettings.getSettingLocked(
2417 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09002418 if (!showNotifications.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01002419 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2420 secureSettings.insertSettingLocked(
2421 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
2422 showNotifications.getValue(),
2423 SettingsState.SYSTEM_PACKAGE_NAME);
2424 }
2425
2426 final Setting allowPrivate = systemSecureSettings.getSettingLocked(
2427 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09002428 if (!allowPrivate.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01002429 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2430 secureSettings.insertSettingLocked(
2431 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,
2432 allowPrivate.getValue(),
2433 SettingsState.SYSTEM_PACKAGE_NAME);
2434 }
2435 }
2436 currentVersion = 127;
2437 }
2438
Steven Ngdc20ba62016-04-26 18:19:04 +01002439 if (currentVersion == 127) {
Mahaver Chopra3d9805d2016-07-07 16:25:05 +01002440 // version 127 is no longer used.
Steven Ngdc20ba62016-04-26 18:19:04 +01002441 currentVersion = 128;
2442 }
2443
Julia Reynolds1f721e12016-07-11 08:50:58 -04002444 if (currentVersion == 128) {
2445 // Version 128: Allow OEMs to grant DND access to default apps. Note that
2446 // the new apps are appended to the list of already approved apps.
2447 final SettingsState systemSecureSettings =
2448 getSecureSettingsLocked(userId);
2449
2450 final Setting policyAccess = systemSecureSettings.getSettingLocked(
2451 Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES);
2452 String defaultPolicyAccess = getContext().getResources().getString(
2453 com.android.internal.R.string.config_defaultDndAccessPackages);
2454 if (!TextUtils.isEmpty(defaultPolicyAccess)) {
2455 if (policyAccess.isNull()) {
2456 systemSecureSettings.insertSettingLocked(
2457 Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES,
2458 defaultPolicyAccess,
2459 SettingsState.SYSTEM_PACKAGE_NAME);
2460 } else {
2461 StringBuilder currentSetting =
2462 new StringBuilder(policyAccess.getValue());
2463 currentSetting.append(":");
2464 currentSetting.append(defaultPolicyAccess);
2465 systemSecureSettings.updateSettingLocked(
2466 Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES,
2467 currentSetting.toString(),
2468 SettingsState.SYSTEM_PACKAGE_NAME);
2469 }
2470 }
2471
2472 currentVersion = 129;
2473 }
2474
Dan Sandler71f85e92016-07-20 13:46:05 -04002475 if (currentVersion == 129) {
2476 // default longpress timeout changed from 500 to 400. If unchanged from the old
2477 // default, update to the new default.
2478 final SettingsState systemSecureSettings =
2479 getSecureSettingsLocked(userId);
2480 final String oldValue = systemSecureSettings.getSettingLocked(
2481 Settings.Secure.LONG_PRESS_TIMEOUT).getValue();
2482 if (TextUtils.equals("500", oldValue)) {
2483 systemSecureSettings.insertSettingLocked(
2484 Settings.Secure.LONG_PRESS_TIMEOUT,
2485 String.valueOf(getContext().getResources().getInteger(
2486 R.integer.def_long_press_timeout_millis)),
2487 SettingsState.SYSTEM_PACKAGE_NAME);
2488 }
2489 currentVersion = 130;
2490 }
2491
Anthony Hugh96e9cc52016-07-12 15:17:24 -07002492 if (currentVersion == 130) {
Adrian Roos69741a22016-10-21 14:49:17 -07002493 // Split Ambient settings
2494 final SettingsState secureSettings = getSecureSettingsLocked(userId);
2495 boolean dozeExplicitlyDisabled = "0".equals(secureSettings.
2496 getSettingLocked(Settings.Secure.DOZE_ENABLED).getValue());
2497
2498 if (dozeExplicitlyDisabled) {
2499 secureSettings.insertSettingLocked(Settings.Secure.DOZE_PULSE_ON_PICK_UP,
2500 "0", SettingsState.SYSTEM_PACKAGE_NAME);
2501 secureSettings.insertSettingLocked(Settings.Secure.DOZE_PULSE_ON_DOUBLE_TAP,
2502 "0", SettingsState.SYSTEM_PACKAGE_NAME);
2503 }
2504 currentVersion = 131;
2505 }
2506
2507 if (currentVersion == 131) {
Anthony Hugh96e9cc52016-07-12 15:17:24 -07002508 // Initialize new multi-press timeout to default value
2509 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
2510 final String oldValue = systemSecureSettings.getSettingLocked(
2511 Settings.Secure.MULTI_PRESS_TIMEOUT).getValue();
2512 if (TextUtils.equals(null, oldValue)) {
2513 systemSecureSettings.insertSettingLocked(
2514 Settings.Secure.MULTI_PRESS_TIMEOUT,
2515 String.valueOf(getContext().getResources().getInteger(
2516 R.integer.def_multi_press_timeout_millis)),
2517 SettingsState.SYSTEM_PACKAGE_NAME);
2518 }
2519
Adrian Roos69741a22016-10-21 14:49:17 -07002520 currentVersion = 132;
Anthony Hugh96e9cc52016-07-12 15:17:24 -07002521 }
2522
Adrian Roos69741a22016-10-21 14:49:17 -07002523 if (currentVersion == 132) {
2524 // Version 132: Allow managed profile to optionally use the parent's ringtones
Andre Lagoea35e072016-08-04 13:41:13 +01002525 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
2526 String defaultSyncParentSounds = (getContext().getResources()
2527 .getBoolean(R.bool.def_sync_parent_sounds) ? "1" : "0");
2528 systemSecureSettings.insertSettingLocked(
2529 Settings.Secure.SYNC_PARENT_SOUNDS,
2530 defaultSyncParentSounds,
2531 SettingsState.SYSTEM_PACKAGE_NAME);
Adrian Roos69741a22016-10-21 14:49:17 -07002532 currentVersion = 133;
Andre Lagoea35e072016-08-04 13:41:13 +01002533 }
2534
Adrian Roos69741a22016-10-21 14:49:17 -07002535 if (currentVersion == 133) {
2536 // Version 133: Add default end button behavior
Keun-young Parkec7a1182016-10-18 11:52:38 -07002537 final SettingsState systemSettings = getSystemSettingsLocked(userId);
2538 if (systemSettings.getSettingLocked(Settings.System.END_BUTTON_BEHAVIOR) ==
2539 null) {
2540 String defaultEndButtonBehavior = Integer.toString(getContext()
2541 .getResources().getInteger(R.integer.def_end_button_behavior));
2542 systemSettings.insertSettingLocked(Settings.System.END_BUTTON_BEHAVIOR,
2543 defaultEndButtonBehavior, SettingsState.SYSTEM_PACKAGE_NAME);
2544 }
Adrian Roos69741a22016-10-21 14:49:17 -07002545 currentVersion = 134;
Keun-young Parkec7a1182016-10-18 11:52:38 -07002546 }
2547
Dan Sandler71f85e92016-07-20 13:46:05 -04002548 if (currentVersion != newVersion) {
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07002549 Slog.wtf("SettingsProvider", "warning: upgrading settings database to version "
Dan Sandler71f85e92016-07-20 13:46:05 -04002550 + newVersion + " left it at "
2551 + currentVersion + " instead; this is probably a bug", new Throwable());
2552 if (DEBUG) {
2553 throw new RuntimeException("db upgrade error");
2554 }
2555 }
2556
Jeff Brown503cffc2015-03-26 18:08:51 -07002557 // vXXX: Add new settings above this point.
Svetoslav683914b2015-01-15 14:22:26 -08002558
Jeff Brown503cffc2015-03-26 18:08:51 -07002559 // Return the current version.
2560 return currentVersion;
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -08002561 }
2562 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002563 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07002564}