blob: 01e0d52d51265e608854c6125c40af66263a7a02 [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;
Eugene Suslad72c3972016-12-27 15:49:30 -080020import android.annotation.NonNull;
Kweku Adamsb0886f32017-10-31 15:32:09 -070021import android.annotation.Nullable;
Christopher Tated5fe1472012-09-10 15:48:38 -070022import android.app.ActivityManager;
Xiaohui Chen43765b72015-08-31 10:57:33 -070023import android.app.AppGlobals;
Christopher Tate45281862010-03-05 15:46:30 -080024import android.app.backup.BackupManager;
Christopher Tate06efb532012-08-24 15:29:27 -070025import android.content.BroadcastReceiver;
Ruben Brunk98576cf2016-03-07 18:54:28 -080026import android.content.ComponentName;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070027import android.content.ContentProvider;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070028import android.content.ContentValues;
29import android.content.Context;
Christopher Tate06efb532012-08-24 15:29:27 -070030import android.content.Intent;
31import android.content.IntentFilter;
Svetoslav683914b2015-01-15 14:22:26 -080032import android.content.pm.ApplicationInfo;
Xiaohui Chen43765b72015-08-31 10:57:33 -070033import android.content.pm.IPackageManager;
Svetoslav683914b2015-01-15 14:22:26 -080034import android.content.pm.PackageInfo;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070035import android.content.pm.PackageManager;
Christopher Tate38e7a602013-09-03 16:57:34 -070036import android.content.pm.UserInfo;
Chad Brubaker20e0dc32017-04-28 18:24:55 -070037import android.content.res.Resources;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070038import android.database.Cursor;
Svetoslav683914b2015-01-15 14:22:26 -080039import android.database.MatrixCursor;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070040import android.database.sqlite.SQLiteDatabase;
41import android.database.sqlite.SQLiteQueryBuilder;
Svetoslav683914b2015-01-15 14:22:26 -080042import android.hardware.camera2.utils.ArrayUtils;
John Spurlocke11ae112015-05-11 16:09:03 -040043import android.media.AudioManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070044import android.net.Uri;
Christopher Tate06efb532012-08-24 15:29:27 -070045import android.os.Binder;
Svetoslav683914b2015-01-15 14:22:26 -080046import android.os.Build;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080047import android.os.Bundle;
Amith Yamasani5cdf7f52013-06-27 15:12:01 -070048import android.os.DropBoxManager;
Svetoslav683914b2015-01-15 14:22:26 -080049import android.os.Environment;
Svetoslav7e0683b2015-08-03 16:02:52 -070050import android.os.Handler;
Svet Ganova8f90262016-05-10 08:44:48 -070051import android.os.HandlerThread;
Svetoslav7e0683b2015-08-03 16:02:52 -070052import android.os.Looper;
53import android.os.Message;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070054import android.os.ParcelFileDescriptor;
Christopher Tate0da13572013-10-13 17:34:49 -070055import android.os.Process;
Xiaohui Chen43765b72015-08-31 10:57:33 -070056import android.os.RemoteException;
Jeff Sharkey413573a2016-02-22 17:52:45 -070057import android.os.SELinux;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -070058import android.os.ServiceManager;
Christopher Tate06efb532012-08-24 15:29:27 -070059import android.os.UserHandle;
60import android.os.UserManager;
Svet Ganov53a441c2016-04-19 19:38:00 -070061import android.os.UserManagerInternal;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070062import android.provider.Settings;
Michal Karpinski2c37b082018-01-18 16:14:27 +000063import android.provider.SettingsValidators;
Narayan Kamath94bcdbc2017-07-17 15:32:53 +010064import android.provider.Settings.Global;
Makoto Onuki0000d322017-11-28 16:31:47 -080065import android.provider.Settings.Secure;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070066import android.text.TextUtils;
Andre Lago3fa139c2016-08-04 13:53:44 +010067import android.util.ArrayMap;
Svetoslav683914b2015-01-15 14:22:26 -080068import android.util.ArraySet;
Mark Rathjend891f012017-01-19 04:10:37 +000069import android.util.ByteStringUtils;
Christopher Tate06efb532012-08-24 15:29:27 -070070import android.util.Slog;
71import android.util.SparseArray;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -070072import android.util.SparseBooleanArray;
Eugene Suslad72c3972016-12-27 15:49:30 -080073import android.util.proto.ProtoOutputStream;
John Spurlocke11ae112015-05-11 16:09:03 -040074
Svetoslav683914b2015-01-15 14:22:26 -080075import com.android.internal.annotations.GuardedBy;
76import com.android.internal.content.PackageMonitor;
77import com.android.internal.os.BackgroundThread;
Suprabh Shukla269c11e2015-12-02 16:51:16 -080078import com.android.providers.settings.SettingsState.Setting;
Svet Ganov53a441c2016-04-19 19:38:00 -070079import com.android.server.LocalServices;
Ruben Brunk98576cf2016-03-07 18:54:28 -080080import com.android.server.SystemConfig;
John Spurlocke11ae112015-05-11 16:09:03 -040081
Svetoslav683914b2015-01-15 14:22:26 -080082import java.io.File;
Svetoslavb505ccc2015-02-17 12:41:04 -080083import java.io.FileDescriptor;
Svetoslav683914b2015-01-15 14:22:26 -080084import java.io.FileNotFoundException;
Svetoslavb505ccc2015-02-17 12:41:04 -080085import java.io.PrintWriter;
Mark Rathjen7599f132017-01-23 14:15:54 -080086import java.nio.ByteBuffer;
87import java.security.InvalidKeyException;
Mark Rathjend891f012017-01-19 04:10:37 +000088import java.security.NoSuchAlgorithmException;
Svetoslav683914b2015-01-15 14:22:26 -080089import java.security.SecureRandom;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -070090import java.util.ArrayList;
Svetoslav683914b2015-01-15 14:22:26 -080091import java.util.Arrays;
Robin Lee7af9a742017-02-20 14:47:30 +000092import java.util.Collection;
Mark Rathjend891f012017-01-19 04:10:37 +000093import java.util.HashSet;
Svetoslav683914b2015-01-15 14:22:26 -080094import java.util.List;
Mark Rathjen7599f132017-01-23 14:15:54 -080095import java.util.Locale;
Andre Lago3fa139c2016-08-04 13:53:44 +010096import java.util.Map;
Svetoslav683914b2015-01-15 14:22:26 -080097import java.util.Set;
98import java.util.regex.Pattern;
Mark Rathjen7599f132017-01-23 14:15:54 -080099import javax.crypto.Mac;
100import javax.crypto.spec.SecretKeySpec;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700101
Svetoslav Ganove080da92016-12-21 17:10:35 -0800102import static android.os.Process.ROOT_UID;
Svetoslav Ganove080da92016-12-21 17:10:35 -0800103import static android.os.Process.SHELL_UID;
Eugene Suslad72c3972016-12-27 15:49:30 -0800104import static android.os.Process.SYSTEM_UID;
105
Svetoslav Ganove080da92016-12-21 17:10:35 -0800106
Svetoslav683914b2015-01-15 14:22:26 -0800107/**
108 * <p>
109 * This class is a content provider that publishes the system settings.
110 * It can be accessed via the content provider APIs or via custom call
111 * commands. The latter is a bit faster and is the preferred way to access
112 * the platform settings.
113 * </p>
114 * <p>
115 * There are three settings types, global (with signature level protection
116 * and shared across users), secure (with signature permission level
117 * protection and per user), and system (with dangerous permission level
118 * protection and per user). Global settings are stored under the device owner.
119 * Each of these settings is represented by a {@link
120 * com.android.providers.settings.SettingsState} object mapped to an integer
121 * key derived from the setting type in the most significant bits and user
122 * id in the least significant bits. Settings are synchronously loaded on
123 * instantiation of a SettingsState and asynchronously persisted on mutation.
124 * Settings are stored in the user specific system directory.
125 * </p>
126 * <p>
127 * Apps targeting APIs Lollipop MR1 and lower can add custom settings entries
128 * and get a warning. Targeting higher API version prohibits this as the
129 * system settings are not a place for apps to save their state. When a package
130 * is removed the settings it added are deleted. Apps cannot delete system
131 * settings added by the platform. System settings values are validated to
132 * ensure the clients do not put bad values. Global and secure settings are
133 * changed only by trusted parties, therefore no validation is performed. Also
134 * there is a limit on the amount of app specific settings that can be added
135 * to prevent unlimited growth of the system process memory footprint.
136 * </p>
137 */
138@SuppressWarnings("deprecation")
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700139public class SettingsProvider extends ContentProvider {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700140 static final boolean DEBUG = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700141
Svetoslav Ganov264c7a92016-08-24 17:31:14 -0700142 private static final boolean DROP_DATABASE_ON_MIGRATION = true;
Svetoslav683914b2015-01-15 14:22:26 -0800143
144 private static final String LOG_TAG = "SettingsProvider";
Christopher Tate0da13572013-10-13 17:34:49 -0700145
Christopher Tate06efb532012-08-24 15:29:27 -0700146 private static final String TABLE_SYSTEM = "system";
147 private static final String TABLE_SECURE = "secure";
148 private static final String TABLE_GLOBAL = "global";
Svetoslav683914b2015-01-15 14:22:26 -0800149
150 // Old tables no longer exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 private static final String TABLE_FAVORITES = "favorites";
152 private static final String TABLE_OLD_FAVORITES = "old_favorites";
Svetoslav683914b2015-01-15 14:22:26 -0800153 private static final String TABLE_BLUETOOTH_DEVICES = "bluetooth_devices";
154 private static final String TABLE_BOOKMARKS = "bookmarks";
155 private static final String TABLE_ANDROID_METADATA = "android_metadata";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
Svetoslav683914b2015-01-15 14:22:26 -0800157 // The set of removed legacy tables.
158 private static final Set<String> REMOVED_LEGACY_TABLES = new ArraySet<>();
Christopher Tate06efb532012-08-24 15:29:27 -0700159 static {
Svetoslav683914b2015-01-15 14:22:26 -0800160 REMOVED_LEGACY_TABLES.add(TABLE_FAVORITES);
161 REMOVED_LEGACY_TABLES.add(TABLE_OLD_FAVORITES);
162 REMOVED_LEGACY_TABLES.add(TABLE_BLUETOOTH_DEVICES);
163 REMOVED_LEGACY_TABLES.add(TABLE_BOOKMARKS);
164 REMOVED_LEGACY_TABLES.add(TABLE_ANDROID_METADATA);
165 }
Christopher Tate06efb532012-08-24 15:29:27 -0700166
Svetoslav683914b2015-01-15 14:22:26 -0800167 private static final int MUTATION_OPERATION_INSERT = 1;
168 private static final int MUTATION_OPERATION_DELETE = 2;
169 private static final int MUTATION_OPERATION_UPDATE = 3;
Svetoslav Ganove080da92016-12-21 17:10:35 -0800170 private static final int MUTATION_OPERATION_RESET = 4;
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400171
Svetoslav683914b2015-01-15 14:22:26 -0800172 private static final String[] ALL_COLUMNS = new String[] {
173 Settings.NameValueTable._ID,
174 Settings.NameValueTable.NAME,
175 Settings.NameValueTable.VALUE
176 };
177
Makoto Onuki53f0e022017-11-29 13:51:01 -0800178 public static final int SETTINGS_TYPE_GLOBAL = SettingsState.SETTINGS_TYPE_GLOBAL;
179 public static final int SETTINGS_TYPE_SYSTEM = SettingsState.SETTINGS_TYPE_SYSTEM;
180 public static final int SETTINGS_TYPE_SECURE = SettingsState.SETTINGS_TYPE_SECURE;
181 public static final int SETTINGS_TYPE_SSAID = SettingsState.SETTINGS_TYPE_SSAID;
Svet Ganov53a441c2016-04-19 19:38:00 -0700182
183 private static final Bundle NULL_SETTING_BUNDLE = Bundle.forPair(
184 Settings.NameValueTable.VALUE, null);
Christopher Tate06efb532012-08-24 15:29:27 -0700185
Chad Brubaker20e0dc32017-04-28 18:24:55 -0700186 // Overlay specified settings whitelisted for Instant Apps
187 private static final Set<String> OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS = new ArraySet<>();
188 private static final Set<String> OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS = new ArraySet<>();
189 private static final Set<String> OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS = new ArraySet<>();
190
191 static {
192 for (String name : Resources.getSystem().getStringArray(
193 com.android.internal.R.array.config_allowedGlobalInstantAppSettings)) {
194 OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS.add(name);
195 }
196 for (String name : Resources.getSystem().getStringArray(
197 com.android.internal.R.array.config_allowedSystemInstantAppSettings)) {
198 OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS.add(name);
199 }
200 for (String name : Resources.getSystem().getStringArray(
201 com.android.internal.R.array.config_allowedSecureInstantAppSettings)) {
202 OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS.add(name);
203 }
204 }
205
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -0800206 // Changes to these global settings are synchronously persisted
207 private static final Set<String> CRITICAL_GLOBAL_SETTINGS = new ArraySet<>();
208 static {
209 CRITICAL_GLOBAL_SETTINGS.add(Settings.Global.DEVICE_PROVISIONED);
210 }
211
212 // Changes to these secure settings are synchronously persisted
213 private static final Set<String> CRITICAL_SECURE_SETTINGS = new ArraySet<>();
214 static {
215 CRITICAL_SECURE_SETTINGS.add(Settings.Secure.USER_SETUP_COMPLETE);
216 }
217
Svetoslav683914b2015-01-15 14:22:26 -0800218 // Per user secure settings that moved to the for all users global settings.
219 static final Set<String> sSecureMovedToGlobalSettings = new ArraySet<>();
220 static {
221 Settings.Secure.getMovedToGlobalSettings(sSecureMovedToGlobalSettings);
Christopher Tate06efb532012-08-24 15:29:27 -0700222 }
223
Svetoslav683914b2015-01-15 14:22:26 -0800224 // Per user system settings that moved to the for all users global settings.
225 static final Set<String> sSystemMovedToGlobalSettings = new ArraySet<>();
226 static {
227 Settings.System.getMovedToGlobalSettings(sSystemMovedToGlobalSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700228 }
229
Svetoslav683914b2015-01-15 14:22:26 -0800230 // Per user system settings that moved to the per user secure settings.
231 static final Set<String> sSystemMovedToSecureSettings = new ArraySet<>();
232 static {
233 Settings.System.getMovedToSecureSettings(sSystemMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700234 }
235
Svetoslav683914b2015-01-15 14:22:26 -0800236 // Per all users global settings that moved to the per user secure settings.
237 static final Set<String> sGlobalMovedToSecureSettings = new ArraySet<>();
238 static {
239 Settings.Global.getMovedToSecureSettings(sGlobalMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700240 }
241
Svetoslav683914b2015-01-15 14:22:26 -0800242 // Per user secure settings that are cloned for the managed profiles of the user.
243 private static final Set<String> sSecureCloneToManagedSettings = new ArraySet<>();
244 static {
245 Settings.Secure.getCloneToManagedProfileSettings(sSecureCloneToManagedSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700246 }
247
Svetoslav683914b2015-01-15 14:22:26 -0800248 // Per user system settings that are cloned for the managed profiles of the user.
249 private static final Set<String> sSystemCloneToManagedSettings = new ArraySet<>();
250 static {
251 Settings.System.getCloneToManagedProfileSettings(sSystemCloneToManagedSettings);
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400252 }
253
Andre Lago3fa139c2016-08-04 13:53:44 +0100254 // Per user system settings that are cloned from the profile's parent when a dependency
255 // in {@link Settings.Secure} is set to "1".
256 public static final Map<String, String> sSystemCloneFromParentOnDependency = new ArrayMap<>();
257 static {
258 Settings.System.getCloneFromParentOnValueSettings(sSystemCloneFromParentOnDependency);
259 }
260
Svetoslav683914b2015-01-15 14:22:26 -0800261 private final Object mLock = new Object();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700262
Svetoslav683914b2015-01-15 14:22:26 -0800263 @GuardedBy("mLock")
264 private SettingsRegistry mSettingsRegistry;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700265
Svet Ganova8f90262016-05-10 08:44:48 -0700266 @GuardedBy("mLock")
267 private HandlerThread mHandlerThread;
268
Makoto Onuki73360ab2017-03-17 11:50:13 -0700269 @GuardedBy("mLock")
270 private Handler mHandler;
271
Svetoslav7ec28e82015-05-20 17:01:10 -0700272 // We have to call in the user manager with no lock held,
273 private volatile UserManager mUserManager;
Svetoslav683914b2015-01-15 14:22:26 -0800274
Svetoslav7ec28e82015-05-20 17:01:10 -0700275 // We have to call in the package manager with no lock held,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700276 private volatile IPackageManager mPackageManager;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700277
Svet Ganov53a441c2016-04-19 19:38:00 -0700278 public static int makeKey(int type, int userId) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800279 return SettingsState.makeKey(type, userId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700280 }
281
282 public static int getTypeFromKey(int key) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800283 return SettingsState.getTypeFromKey(key);
Svet Ganov53a441c2016-04-19 19:38:00 -0700284 }
285
286 public static int getUserIdFromKey(int key) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800287 return SettingsState.getUserIdFromKey(key);
Svet Ganov53a441c2016-04-19 19:38:00 -0700288 }
289
290 public static String settingTypeToString(int type) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800291 return SettingsState.settingTypeToString(type);
Svet Ganov53a441c2016-04-19 19:38:00 -0700292 }
293
294 public static String keyToString(int key) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800295 return SettingsState.keyToString(key);
Svet Ganov53a441c2016-04-19 19:38:00 -0700296 }
297
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700298 @Override
299 public boolean onCreate() {
Chad Brubaker97bccee2017-01-05 15:51:41 -0800300 Settings.setInSystemServer();
Michal Karpinski2c37b082018-01-18 16:14:27 +0000301
302 // fail to boot if there're any backed up settings that don't have a non-null validator
303 ensureAllBackedUpSystemSettingsHaveValidators();
304
Svetoslav683914b2015-01-15 14:22:26 -0800305 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700306 mUserManager = UserManager.get(getContext());
307 mPackageManager = AppGlobals.getPackageManager();
Svet Ganova8f90262016-05-10 08:44:48 -0700308 mHandlerThread = new HandlerThread(LOG_TAG,
309 Process.THREAD_PRIORITY_BACKGROUND);
310 mHandlerThread.start();
Makoto Onuki73360ab2017-03-17 11:50:13 -0700311 mHandler = new Handler(mHandlerThread.getLooper());
Svetoslav683914b2015-01-15 14:22:26 -0800312 mSettingsRegistry = new SettingsRegistry();
313 }
Makoto Onuki73360ab2017-03-17 11:50:13 -0700314 mHandler.post(() -> {
315 registerBroadcastReceivers();
316 startWatchingUserRestrictionChanges();
317 });
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700318 ServiceManager.addService("settings", new SettingsService(this));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700319 return true;
320 }
321
Michal Karpinski2c37b082018-01-18 16:14:27 +0000322 private void ensureAllBackedUpSystemSettingsHaveValidators() {
323 StringBuilder offenders = new StringBuilder();
324 for (String setting : Settings.System.SETTINGS_TO_BACKUP) {
325 if (Settings.System.VALIDATORS.get(setting) == null) {
326 offenders.append(setting).append(" ");
327 }
328 }
329
330 String offendersStr = offenders.toString();
331 if (offendersStr.length() > 0) {
332 throw new RuntimeException("All Settings.System settings that are backed up must"
333 + " have a non-null validator, but those don't: " + offendersStr);
334 }
335 }
336
Svetoslav683914b2015-01-15 14:22:26 -0800337 @Override
338 public Bundle call(String method, String name, Bundle args) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700339 final int requestingUserId = getRequestingUserId(args);
340 switch (method) {
341 case Settings.CALL_METHOD_GET_GLOBAL: {
342 Setting setting = getGlobalSetting(name);
Svet Ganov53a441c2016-04-19 19:38:00 -0700343 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav683914b2015-01-15 14:22:26 -0800344 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700345
346 case Settings.CALL_METHOD_GET_SECURE: {
Makoto Onuki0000d322017-11-28 16:31:47 -0800347 Setting setting = getSecureSetting(name, requestingUserId,
348 /*enableOverride=*/ true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700349 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700350 }
351
352 case Settings.CALL_METHOD_GET_SYSTEM: {
353 Setting setting = getSystemSetting(name, requestingUserId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700354 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700355 }
356
357 case Settings.CALL_METHOD_PUT_GLOBAL: {
358 String value = getSettingValue(args);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800359 String tag = getSettingTag(args);
360 final boolean makeDefault = getSettingMakeDefault(args);
361 insertGlobalSetting(name, value, tag, makeDefault, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700362 break;
363 }
364
365 case Settings.CALL_METHOD_PUT_SECURE: {
366 String value = getSettingValue(args);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800367 String tag = getSettingTag(args);
368 final boolean makeDefault = getSettingMakeDefault(args);
369 insertSecureSetting(name, value, tag, makeDefault, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700370 break;
371 }
372
373 case Settings.CALL_METHOD_PUT_SYSTEM: {
374 String value = getSettingValue(args);
375 insertSystemSetting(name, value, requestingUserId);
376 break;
377 }
378
Svetoslav Ganove080da92016-12-21 17:10:35 -0800379 case Settings.CALL_METHOD_RESET_GLOBAL: {
380 final int mode = getResetModeEnforcingPermission(args);
381 String tag = getSettingTag(args);
382 resetGlobalSetting(requestingUserId, mode, tag);
383 break;
384 }
385
386 case Settings.CALL_METHOD_RESET_SECURE: {
387 final int mode = getResetModeEnforcingPermission(args);
388 String tag = getSettingTag(args);
389 resetSecureSetting(requestingUserId, mode, tag);
390 break;
391 }
392
Svetoslav7ec28e82015-05-20 17:01:10 -0700393 default: {
394 Slog.w(LOG_TAG, "call() with invalid method: " + method);
395 } break;
Christopher Tate06efb532012-08-24 15:29:27 -0700396 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700397
Christopher Tate06efb532012-08-24 15:29:27 -0700398 return null;
399 }
400
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800401 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800402 public String getType(Uri uri) {
403 Arguments args = new Arguments(uri, null, null, true);
404 if (TextUtils.isEmpty(args.name)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700405 return "vnd.android.cursor.dir/" + args.table;
406 } else {
Svetoslav7ec28e82015-05-20 17:01:10 -0700407 return "vnd.android.cursor.item/" + args.table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700408 }
409 }
410
411 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800412 public Cursor query(Uri uri, String[] projection, String where, String[] whereArgs,
413 String order) {
414 if (DEBUG) {
415 Slog.v(LOG_TAG, "query() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700416 }
417
Svetoslav683914b2015-01-15 14:22:26 -0800418 Arguments args = new Arguments(uri, where, whereArgs, true);
419 String[] normalizedProjection = normalizeProjection(projection);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700420
Svetoslav683914b2015-01-15 14:22:26 -0800421 // If a legacy table that is gone, done.
422 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
423 return new MatrixCursor(normalizedProjection, 0);
424 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700425
Svetoslav7ec28e82015-05-20 17:01:10 -0700426 switch (args.table) {
427 case TABLE_GLOBAL: {
428 if (args.name != null) {
429 Setting setting = getGlobalSetting(args.name);
430 return packageSettingForQuery(setting, normalizedProjection);
431 } else {
432 return getAllGlobalSettings(projection);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700433 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700434 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700435
Svetoslav7ec28e82015-05-20 17:01:10 -0700436 case TABLE_SECURE: {
437 final int userId = UserHandle.getCallingUserId();
438 if (args.name != null) {
439 Setting setting = getSecureSetting(args.name, userId);
440 return packageSettingForQuery(setting, normalizedProjection);
441 } else {
442 return getAllSecureSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800443 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700444 }
Svetoslav683914b2015-01-15 14:22:26 -0800445
Svetoslav7ec28e82015-05-20 17:01:10 -0700446 case TABLE_SYSTEM: {
447 final int userId = UserHandle.getCallingUserId();
448 if (args.name != null) {
449 Setting setting = getSystemSetting(args.name, userId);
450 return packageSettingForQuery(setting, normalizedProjection);
451 } else {
452 return getAllSystemSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800453 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700454 }
Svetoslav683914b2015-01-15 14:22:26 -0800455
Svetoslav7ec28e82015-05-20 17:01:10 -0700456 default: {
457 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700458 }
459 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700460 }
461
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700462 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800463 public Uri insert(Uri uri, ContentValues values) {
464 if (DEBUG) {
465 Slog.v(LOG_TAG, "insert() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700466 }
467
Svetoslav683914b2015-01-15 14:22:26 -0800468 String table = getValidTableOrThrow(uri);
Christopher Tate06efb532012-08-24 15:29:27 -0700469
Svetoslav683914b2015-01-15 14:22:26 -0800470 // If a legacy table that is gone, done.
471 if (REMOVED_LEGACY_TABLES.contains(table)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 return null;
473 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700474
Svetoslav683914b2015-01-15 14:22:26 -0800475 String name = values.getAsString(Settings.Secure.NAME);
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700476 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800477 return null;
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700478 }
479
Svetoslav683914b2015-01-15 14:22:26 -0800480 String value = values.getAsString(Settings.Secure.VALUE);
481
Svetoslav7ec28e82015-05-20 17:01:10 -0700482 switch (table) {
483 case TABLE_GLOBAL: {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800484 if (insertGlobalSetting(name, value, null, false,
485 UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700486 return Uri.withAppendedPath(Settings.Global.CONTENT_URI, name);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700487 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700488 } break;
489
490 case TABLE_SECURE: {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800491 if (insertSecureSetting(name, value, null, false,
492 UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700493 return Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name);
494 }
495 } break;
496
497 case TABLE_SYSTEM: {
498 if (insertSystemSetting(name, value, UserHandle.getCallingUserId())) {
499 return Uri.withAppendedPath(Settings.System.CONTENT_URI, name);
500 }
501 } break;
502
503 default: {
504 throw new IllegalArgumentException("Bad Uri path:" + uri);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700505 }
506 }
507
Svetoslav683914b2015-01-15 14:22:26 -0800508 return null;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700509 }
510
511 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800512 public int bulkInsert(Uri uri, ContentValues[] allValues) {
513 if (DEBUG) {
514 Slog.v(LOG_TAG, "bulkInsert() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700516
Svetoslav683914b2015-01-15 14:22:26 -0800517 int insertionCount = 0;
518 final int valuesCount = allValues.length;
519 for (int i = 0; i < valuesCount; i++) {
520 ContentValues values = allValues[i];
521 if (insert(uri, values) != null) {
522 insertionCount++;
523 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700524 }
Svetoslav683914b2015-01-15 14:22:26 -0800525
526 return insertionCount;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700527 }
528
529 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800530 public int delete(Uri uri, String where, String[] whereArgs) {
531 if (DEBUG) {
532 Slog.v(LOG_TAG, "delete() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700534
Svetoslav683914b2015-01-15 14:22:26 -0800535 Arguments args = new Arguments(uri, where, whereArgs, false);
536
537 // If a legacy table that is gone, done.
538 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
539 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700540 }
Svetoslav683914b2015-01-15 14:22:26 -0800541
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700542 if (!isKeyValid(args.name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800543 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700544 }
Svetoslav683914b2015-01-15 14:22:26 -0800545
Svetoslav7ec28e82015-05-20 17:01:10 -0700546 switch (args.table) {
547 case TABLE_GLOBAL: {
548 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700549 return deleteGlobalSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700550 }
Svetoslav683914b2015-01-15 14:22:26 -0800551
Svetoslav7ec28e82015-05-20 17:01:10 -0700552 case TABLE_SECURE: {
553 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700554 return deleteSecureSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700555 }
Svetoslav683914b2015-01-15 14:22:26 -0800556
Svetoslav7ec28e82015-05-20 17:01:10 -0700557 case TABLE_SYSTEM: {
558 final int userId = UserHandle.getCallingUserId();
559 return deleteSystemSetting(args.name, userId) ? 1 : 0;
560 }
561
562 default: {
563 throw new IllegalArgumentException("Bad Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800564 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700565 }
Svetoslav683914b2015-01-15 14:22:26 -0800566 }
567
568 @Override
569 public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
570 if (DEBUG) {
571 Slog.v(LOG_TAG, "update() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700572 }
Svetoslav683914b2015-01-15 14:22:26 -0800573
574 Arguments args = new Arguments(uri, where, whereArgs, false);
575
576 // If a legacy table that is gone, done.
577 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
578 return 0;
579 }
580
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700581 String name = values.getAsString(Settings.Secure.NAME);
582 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800583 return 0;
584 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700585 String value = values.getAsString(Settings.Secure.VALUE);
Svetoslav683914b2015-01-15 14:22:26 -0800586
Svetoslav7ec28e82015-05-20 17:01:10 -0700587 switch (args.table) {
588 case TABLE_GLOBAL: {
589 final int userId = UserHandle.getCallingUserId();
Svetoslav Ganove080da92016-12-21 17:10:35 -0800590 return updateGlobalSetting(args.name, value, null, false,
591 userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700592 }
Svetoslav683914b2015-01-15 14:22:26 -0800593
Svetoslav7ec28e82015-05-20 17:01:10 -0700594 case TABLE_SECURE: {
595 final int userId = UserHandle.getCallingUserId();
Svetoslav Ganove080da92016-12-21 17:10:35 -0800596 return updateSecureSetting(args.name, value, null, false,
597 userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700598 }
Svetoslav683914b2015-01-15 14:22:26 -0800599
Svetoslav7ec28e82015-05-20 17:01:10 -0700600 case TABLE_SYSTEM: {
601 final int userId = UserHandle.getCallingUserId();
602 return updateSystemSetting(args.name, value, userId) ? 1 : 0;
603 }
Svetoslav683914b2015-01-15 14:22:26 -0800604
Svetoslav7ec28e82015-05-20 17:01:10 -0700605 default: {
606 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800607 }
608 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700609 }
610
611 @Override
612 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
Robin Lee2ab02e22016-07-28 18:41:23 +0100613 final int userId = getUserIdFromUri(uri, UserHandle.getCallingUserId());
614 if (userId != UserHandle.getCallingUserId()) {
615 getContext().enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS,
616 "Access files from the settings of another user");
617 }
618 uri = ContentProvider.getUriWithoutUserId(uri);
619
Andre Lago3fa139c2016-08-04 13:53:44 +0100620 final String cacheRingtoneSetting;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700621 final String cacheName;
622 if (Settings.System.RINGTONE_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100623 cacheRingtoneSetting = Settings.System.RINGTONE;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700624 cacheName = Settings.System.RINGTONE_CACHE;
625 } else if (Settings.System.NOTIFICATION_SOUND_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100626 cacheRingtoneSetting = Settings.System.NOTIFICATION_SOUND;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700627 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
628 } else if (Settings.System.ALARM_ALERT_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100629 cacheRingtoneSetting = Settings.System.ALARM_ALERT;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700630 cacheName = Settings.System.ALARM_ALERT_CACHE;
631 } else {
632 throw new FileNotFoundException("Direct file access no longer supported; "
633 + "ringtone playback is available through android.media.Ringtone");
634 }
635
Andre Lago3fa139c2016-08-04 13:53:44 +0100636 int actualCacheOwner;
637 // Redirect cache to parent if ringtone setting is owned by profile parent
638 synchronized (mLock) {
639 actualCacheOwner = resolveOwningUserIdForSystemSettingLocked(userId,
640 cacheRingtoneSetting);
641 }
642 final File cacheFile = new File(getRingtoneCacheDir(actualCacheOwner), cacheName);
Jeff Sharkey413573a2016-02-22 17:52:45 -0700643 return ParcelFileDescriptor.open(cacheFile, ParcelFileDescriptor.parseMode(mode));
644 }
645
646 private File getRingtoneCacheDir(int userId) {
647 final File cacheDir = new File(Environment.getDataSystemDeDirectory(userId), "ringtones");
648 cacheDir.mkdir();
649 SELinux.restorecon(cacheDir);
650 return cacheDir;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700651 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800652
Eugene Suslad72c3972016-12-27 15:49:30 -0800653 /**
654 * Dump all settings as a proto buf.
655 *
656 * @param fd The file to dump to
657 */
658 void dumpProto(@NonNull FileDescriptor fd) {
659 ProtoOutputStream proto = new ProtoOutputStream(fd);
660
661 synchronized (mLock) {
662 SettingsProtoDumpUtil.dumpProtoLocked(mSettingsRegistry, proto);
Eugene Suslad72c3972016-12-27 15:49:30 -0800663 }
664
665 proto.flush();
666 }
667
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700668 public void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800669 synchronized (mLock) {
670 final long identity = Binder.clearCallingIdentity();
671 try {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700672 SparseBooleanArray users = mSettingsRegistry.getKnownUsersLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800673 final int userCount = users.size();
674 for (int i = 0; i < userCount; i++) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700675 dumpForUserLocked(users.keyAt(i), pw);
Svetoslavb505ccc2015-02-17 12:41:04 -0800676 }
677 } finally {
678 Binder.restoreCallingIdentity(identity);
679 }
680 }
681 }
682
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700683 private void dumpForUserLocked(int userId, PrintWriter pw) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700684 if (userId == UserHandle.USER_SYSTEM) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800685 pw.println("GLOBAL SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700686 SettingsState globalSettings = mSettingsRegistry.getSettingsLocked(
687 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700688 if (globalSettings != null) {
689 dumpSettingsLocked(globalSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800690 pw.println();
691 globalSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700692 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800693 }
694
695 pw.println("SECURE SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700696 SettingsState secureSettings = mSettingsRegistry.getSettingsLocked(
697 SETTINGS_TYPE_SECURE, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700698 if (secureSettings != null) {
699 dumpSettingsLocked(secureSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800700 pw.println();
701 secureSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700702 }
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700703
Svetoslavb505ccc2015-02-17 12:41:04 -0800704 pw.println("SYSTEM SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700705 SettingsState systemSettings = mSettingsRegistry.getSettingsLocked(
706 SETTINGS_TYPE_SYSTEM, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700707 if (systemSettings != null) {
708 dumpSettingsLocked(systemSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800709 pw.println();
710 systemSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700711 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800712 }
713
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700714 private void dumpSettingsLocked(SettingsState settingsState, PrintWriter pw) {
715 List<String> names = settingsState.getSettingNamesLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800716
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700717 final int nameCount = names.size();
Svetoslavb505ccc2015-02-17 12:41:04 -0800718
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700719 for (int i = 0; i < nameCount; i++) {
720 String name = names.get(i);
721 Setting setting = settingsState.getSettingLocked(name);
722 pw.print("_id:"); pw.print(toDumpString(setting.getId()));
723 pw.print(" name:"); pw.print(toDumpString(name));
724 if (setting.getPackageName() != null) {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800725 pw.print(" pkg:"); pw.print(setting.getPackageName());
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700726 }
727 pw.print(" value:"); pw.print(toDumpString(setting.getValue()));
Svetoslav Ganove080da92016-12-21 17:10:35 -0800728 if (setting.getDefaultValue() != null) {
729 pw.print(" default:"); pw.print(setting.getDefaultValue());
Eugene Suslad72c3972016-12-27 15:49:30 -0800730 pw.print(" defaultSystemSet:"); pw.print(setting.isDefaultFromSystem());
Svetoslav Ganove080da92016-12-21 17:10:35 -0800731 }
732 if (setting.getTag() != null) {
733 pw.print(" tag:"); pw.print(setting.getTag());
734 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800735 pw.println();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700736 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800737 }
738
Svetoslav7e0683b2015-08-03 16:02:52 -0700739 private static String toDumpString(String s) {
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700740 if (s != null) {
741 return s;
742 }
743 return "{null}";
744 }
745
Svetoslav683914b2015-01-15 14:22:26 -0800746 private void registerBroadcastReceivers() {
747 IntentFilter userFilter = new IntentFilter();
748 userFilter.addAction(Intent.ACTION_USER_REMOVED);
749 userFilter.addAction(Intent.ACTION_USER_STOPPED);
750
751 getContext().registerReceiver(new BroadcastReceiver() {
752 @Override
753 public void onReceive(Context context, Intent intent) {
754 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700755 UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800756
757 switch (intent.getAction()) {
758 case Intent.ACTION_USER_REMOVED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700759 synchronized (mLock) {
760 mSettingsRegistry.removeUserStateLocked(userId, true);
761 }
Svetoslav683914b2015-01-15 14:22:26 -0800762 } break;
763
764 case Intent.ACTION_USER_STOPPED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700765 synchronized (mLock) {
766 mSettingsRegistry.removeUserStateLocked(userId, false);
767 }
Svetoslav683914b2015-01-15 14:22:26 -0800768 } break;
769 }
770 }
771 }, userFilter);
772
773 PackageMonitor monitor = new PackageMonitor() {
774 @Override
775 public void onPackageRemoved(String packageName, int uid) {
776 synchronized (mLock) {
777 mSettingsRegistry.onPackageRemovedLocked(packageName,
778 UserHandle.getUserId(uid));
779 }
780 }
Mark Rathjend891f012017-01-19 04:10:37 +0000781
782 @Override
783 public void onUidRemoved(int uid) {
784 synchronized (mLock) {
785 mSettingsRegistry.onUidRemovedLocked(uid);
786 }
787 }
Svetoslav683914b2015-01-15 14:22:26 -0800788 };
789
790 // package changes
791 monitor.register(getContext(), BackgroundThread.getHandler().getLooper(),
792 UserHandle.ALL, true);
793 }
794
Svet Ganov53a441c2016-04-19 19:38:00 -0700795 private void startWatchingUserRestrictionChanges() {
796 // TODO: The current design of settings looking different based on user restrictions
797 // should be reworked to keep them separate and system code should check the setting
798 // first followed by checking the user restriction before performing an operation.
799 UserManagerInternal userManager = LocalServices.getService(UserManagerInternal.class);
800 userManager.addUserRestrictionsListener((int userId, Bundle newRestrictions,
801 Bundle prevRestrictions) -> {
802 // We are changing the settings affected by restrictions to their current
803 // value with a forced update to ensure that all cross profile dependencies
804 // are taken into account. Also make sure the settings update to.. the same
805 // value passes the security checks, so clear binder calling id.
Svetoslav Ganove080da92016-12-21 17:10:35 -0800806 if (newRestrictions.getBoolean(UserManager.DISALLOW_SHARE_LOCATION)
807 != prevRestrictions.getBoolean(UserManager.DISALLOW_SHARE_LOCATION)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700808 final long identity = Binder.clearCallingIdentity();
809 try {
810 synchronized (mLock) {
811 Setting setting = getSecureSetting(
812 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, userId);
813 updateSecureSetting(Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800814 setting != null ? setting.getValue() : null, null,
815 true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700816 }
817 } finally {
818 Binder.restoreCallingIdentity(identity);
819 }
820 }
Svetoslav Ganove080da92016-12-21 17:10:35 -0800821 if (newRestrictions.getBoolean(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)
822 != prevRestrictions.getBoolean(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700823 final long identity = Binder.clearCallingIdentity();
824 try {
825 synchronized (mLock) {
826 Setting setting = getGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800827 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700828 updateGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800829 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700830 }
831 } finally {
832 Binder.restoreCallingIdentity(identity);
833 }
834 }
Svetoslav Ganove080da92016-12-21 17:10:35 -0800835 if (newRestrictions.getBoolean(UserManager.DISALLOW_DEBUGGING_FEATURES)
836 != prevRestrictions.getBoolean(UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700837 final long identity = Binder.clearCallingIdentity();
838 try {
839 synchronized (mLock) {
840 Setting setting = getGlobalSetting(Settings.Global.ADB_ENABLED);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800841 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700842 updateGlobalSetting(Settings.Global.ADB_ENABLED,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800843 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700844 }
845 } finally {
846 Binder.restoreCallingIdentity(identity);
847 }
848 }
Svetoslav Ganove080da92016-12-21 17:10:35 -0800849 if (newRestrictions.getBoolean(UserManager.ENSURE_VERIFY_APPS)
850 != prevRestrictions.getBoolean(UserManager.ENSURE_VERIFY_APPS)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700851 final long identity = Binder.clearCallingIdentity();
852 try {
853 synchronized (mLock) {
854 Setting enable = getGlobalSetting(
855 Settings.Global.PACKAGE_VERIFIER_ENABLE);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800856 String enableValue = enable != null ? enable.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700857 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_ENABLE,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800858 enableValue, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700859 Setting include = getGlobalSetting(
860 Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800861 String includeValue = include != null ? include.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700862 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800863 includeValue, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700864 }
865 } finally {
866 Binder.restoreCallingIdentity(identity);
867 }
868 }
Svetoslav Ganove080da92016-12-21 17:10:35 -0800869 if (newRestrictions.getBoolean(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)
870 != prevRestrictions.getBoolean(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700871 final long identity = Binder.clearCallingIdentity();
872 try {
873 synchronized (mLock) {
874 Setting setting = getGlobalSetting(
875 Settings.Global.PREFERRED_NETWORK_MODE);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800876 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700877 updateGlobalSetting(Settings.Global.PREFERRED_NETWORK_MODE,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800878 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700879 }
880 } finally {
881 Binder.restoreCallingIdentity(identity);
882 }
883 }
884 });
885 }
886
Svetoslav7ec28e82015-05-20 17:01:10 -0700887 private Cursor getAllGlobalSettings(String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -0800888 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700889 Slog.v(LOG_TAG, "getAllGlobalSettings()");
Svetoslav683914b2015-01-15 14:22:26 -0800890 }
891
Svetoslav7ec28e82015-05-20 17:01:10 -0700892 synchronized (mLock) {
893 // Get the settings.
894 SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -0700895 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800896
Chad Brubaker97bccee2017-01-05 15:51:41 -0800897 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_GLOBAL,
898 UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800899
Svetoslav7ec28e82015-05-20 17:01:10 -0700900 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -0800901
Svetoslav7ec28e82015-05-20 17:01:10 -0700902 String[] normalizedProjection = normalizeProjection(projection);
903 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -0800904
Svetoslav7ec28e82015-05-20 17:01:10 -0700905 // Anyone can get the global settings, so no security checks.
906 for (int i = 0; i < nameCount; i++) {
907 String name = names.get(i);
908 Setting setting = settingsState.getSettingLocked(name);
909 appendSettingToCursor(result, setting);
910 }
911
912 return result;
Svetoslav683914b2015-01-15 14:22:26 -0800913 }
Svetoslav683914b2015-01-15 14:22:26 -0800914 }
915
Svetoslav7ec28e82015-05-20 17:01:10 -0700916 private Setting getGlobalSetting(String name) {
Svetoslav683914b2015-01-15 14:22:26 -0800917 if (DEBUG) {
918 Slog.v(LOG_TAG, "getGlobalSetting(" + name + ")");
919 }
920
Chad Brubakera6830e72017-04-28 17:34:36 -0700921 // Ensure the caller can access the setting.
922 enforceSettingReadable(name, SETTINGS_TYPE_GLOBAL, UserHandle.getCallingUserId());
923
Svetoslav683914b2015-01-15 14:22:26 -0800924 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -0700925 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -0700926 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_GLOBAL,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700927 UserHandle.USER_SYSTEM, name);
Svetoslav683914b2015-01-15 14:22:26 -0800928 }
Svetoslav683914b2015-01-15 14:22:26 -0800929 }
930
Svetoslav Ganove080da92016-12-21 17:10:35 -0800931 private boolean updateGlobalSetting(String name, String value, String tag,
932 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800933 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800934 Slog.v(LOG_TAG, "updateGlobalSetting(" + name + ", " + value + ", "
935 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
936 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800937 }
Svetoslav Ganove080da92016-12-21 17:10:35 -0800938 return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId,
939 MUTATION_OPERATION_UPDATE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -0800940 }
941
Svetoslav Ganove080da92016-12-21 17:10:35 -0800942 private boolean insertGlobalSetting(String name, String value, String tag,
943 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700944 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800945 Slog.v(LOG_TAG, "insertGlobalSetting(" + name + ", " + value + ", "
946 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
947 + ", " + forceNotify + ")");
Svetoslav7ec28e82015-05-20 17:01:10 -0700948 }
Svetoslav Ganove080da92016-12-21 17:10:35 -0800949 return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId,
950 MUTATION_OPERATION_INSERT, forceNotify, 0);
Svetoslav7ec28e82015-05-20 17:01:10 -0700951 }
952
Svet Ganov53a441c2016-04-19 19:38:00 -0700953 private boolean deleteGlobalSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -0800954 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800955 Slog.v(LOG_TAG, "deleteGlobalSetting(" + name + ", " + requestingUserId
956 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -0800957 }
Svetoslav Ganove080da92016-12-21 17:10:35 -0800958 return mutateGlobalSetting(name, null, null, false, requestingUserId,
959 MUTATION_OPERATION_DELETE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -0800960 }
961
Svetoslav Ganove080da92016-12-21 17:10:35 -0800962 private void resetGlobalSetting(int requestingUserId, int mode, String tag) {
963 if (DEBUG) {
964 Slog.v(LOG_TAG, "resetGlobalSetting(" + requestingUserId + ", "
965 + mode + ", " + tag + ")");
966 }
967 mutateGlobalSetting(null, null, tag, false, requestingUserId,
968 MUTATION_OPERATION_RESET, false, mode);
969 }
970
971 private boolean mutateGlobalSetting(String name, String value, String tag,
972 boolean makeDefault, int requestingUserId, int operation, boolean forceNotify,
973 int mode) {
Svetoslav683914b2015-01-15 14:22:26 -0800974 // Make sure the caller can change the settings - treated as secure.
975 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
976
Svetoslav683914b2015-01-15 14:22:26 -0800977 // Resolve the userId on whose behalf the call is made.
978 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
979
Makoto Onuki28da2e32015-11-20 11:30:44 -0800980 // If this is a setting that is currently restricted for this user, do not allow
981 // unrestricting changes.
Svetoslav Ganove080da92016-12-21 17:10:35 -0800982 if (name != null && isGlobalOrSecureSettingRestrictedForUser(name, callingUserId, value,
Victor Chang9c7b7062016-07-12 23:47:29 +0100983 Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -0800984 return false;
985 }
986
987 // Perform the mutation.
Svetoslav7ec28e82015-05-20 17:01:10 -0700988 synchronized (mLock) {
989 switch (operation) {
990 case MUTATION_OPERATION_INSERT: {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800991 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_GLOBAL,
992 UserHandle.USER_SYSTEM, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -0800993 getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -0700994 }
Svetoslav683914b2015-01-15 14:22:26 -0800995
Svetoslav7ec28e82015-05-20 17:01:10 -0700996 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700997 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_GLOBAL,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -0800998 UserHandle.USER_SYSTEM, name, forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -0700999 }
Svetoslav683914b2015-01-15 14:22:26 -08001000
Svetoslav7ec28e82015-05-20 17:01:10 -07001001 case MUTATION_OPERATION_UPDATE: {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001002 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_GLOBAL,
1003 UserHandle.USER_SYSTEM, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001004 getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001005 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001006
1007 case MUTATION_OPERATION_RESET: {
1008 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_GLOBAL,
1009 UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag);
1010 } return true;
Svetoslav683914b2015-01-15 14:22:26 -08001011 }
1012 }
1013
1014 return false;
1015 }
1016
Christopher Tateb218e762017-04-05 16:34:07 -07001017 private PackageInfo getCallingPackageInfo(int userId) {
1018 try {
1019 return mPackageManager.getPackageInfo(getCallingPackage(),
1020 PackageManager.GET_SIGNATURES, userId);
1021 } catch (RemoteException e) {
1022 throw new IllegalStateException("Package " + getCallingPackage() + " doesn't exist");
1023 }
1024 }
1025
Svetoslav7ec28e82015-05-20 17:01:10 -07001026 private Cursor getAllSecureSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001027 if (DEBUG) {
1028 Slog.v(LOG_TAG, "getAllSecureSettings(" + userId + ")");
1029 }
1030
1031 // Resolve the userId on whose behalf the call is made.
1032 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
1033
Christopher Tateb218e762017-04-05 16:34:07 -07001034 // The relevant "calling package" userId will be the owning userId for some
1035 // profiles, and we can't do the lookup inside our [lock held] loop, so work out
1036 // up front who the effective "new SSAID" user ID for that settings name will be.
1037 final int ssaidUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
1038 Settings.Secure.ANDROID_ID);
1039 final PackageInfo ssaidCallingPkg = getCallingPackageInfo(ssaidUserId);
1040
Svetoslav7ec28e82015-05-20 17:01:10 -07001041 synchronized (mLock) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001042 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SECURE, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -08001043
Svetoslav7ec28e82015-05-20 17:01:10 -07001044 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001045
Svetoslav7ec28e82015-05-20 17:01:10 -07001046 String[] normalizedProjection = normalizeProjection(projection);
1047 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001048
Svetoslav7ec28e82015-05-20 17:01:10 -07001049 for (int i = 0; i < nameCount; i++) {
1050 String name = names.get(i);
1051 // Determine the owning user as some profile settings are cloned from the parent.
1052 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
1053 name);
Svetoslav683914b2015-01-15 14:22:26 -08001054
Alex Klyubin1991f572017-03-03 14:08:36 -08001055 if (!isSecureSettingAccessible(name, callingUserId, owningUserId)) {
1056 // This caller is not permitted to access this setting. Pretend the setting
1057 // doesn't exist.
Svetoslav Ganov83a1f7f2016-04-27 13:50:49 -07001058 continue;
Svetoslav7ec28e82015-05-20 17:01:10 -07001059 }
Svetoslav683914b2015-01-15 14:22:26 -08001060
Mark Rathjen7599f132017-01-23 14:15:54 -08001061 // As of Android O, the SSAID is read from an app-specific entry in table
Mark Rathjend891f012017-01-19 04:10:37 +00001062 // SETTINGS_FILE_SSAID, unless accessed by a system process.
1063 final Setting setting;
1064 if (isNewSsaidSetting(name)) {
Christopher Tateb218e762017-04-05 16:34:07 -07001065 setting = getSsaidSettingLocked(ssaidCallingPkg, owningUserId);
Mark Rathjend891f012017-01-19 04:10:37 +00001066 } else {
1067 setting = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE, owningUserId,
1068 name);
1069 }
Svetoslav7ec28e82015-05-20 17:01:10 -07001070 appendSettingToCursor(result, setting);
Svetoslav683914b2015-01-15 14:22:26 -08001071 }
1072
Svetoslav7ec28e82015-05-20 17:01:10 -07001073 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001074 }
Svetoslav683914b2015-01-15 14:22:26 -08001075 }
1076
Svetoslav7ec28e82015-05-20 17:01:10 -07001077 private Setting getSecureSetting(String name, int requestingUserId) {
Makoto Onuki0000d322017-11-28 16:31:47 -08001078 return getSecureSetting(name, requestingUserId, /*enableOverride=*/ false);
1079 }
1080
1081 private Setting getSecureSetting(String name, int requestingUserId, boolean enableOverride) {
Svetoslav683914b2015-01-15 14:22:26 -08001082 if (DEBUG) {
1083 Slog.v(LOG_TAG, "getSecureSetting(" + name + ", " + requestingUserId + ")");
1084 }
1085
1086 // Resolve the userId on whose behalf the call is made.
1087 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1088
Chad Brubakera6830e72017-04-28 17:34:36 -07001089 // Ensure the caller can access the setting.
1090 enforceSettingReadable(name, SETTINGS_TYPE_SECURE, UserHandle.getCallingUserId());
1091
Svetoslav683914b2015-01-15 14:22:26 -08001092 // Determine the owning user as some profile settings are cloned from the parent.
1093 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
1094
Alex Klyubin1991f572017-03-03 14:08:36 -08001095 if (!isSecureSettingAccessible(name, callingUserId, owningUserId)) {
1096 // This caller is not permitted to access this setting. Pretend the setting doesn't
1097 // exist.
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001098 SettingsState settings = mSettingsRegistry.getSettingsLocked(SETTINGS_TYPE_SECURE,
1099 owningUserId);
1100 return settings != null ? settings.getNullSetting() : null;
Svetoslav683914b2015-01-15 14:22:26 -08001101 }
1102
Christopher Tateb218e762017-04-05 16:34:07 -07001103 // As of Android O, the SSAID is read from an app-specific entry in table
1104 // SETTINGS_FILE_SSAID, unless accessed by a system process.
1105 if (isNewSsaidSetting(name)) {
1106 PackageInfo callingPkg = getCallingPackageInfo(owningUserId);
1107 synchronized (mLock) {
1108 return getSsaidSettingLocked(callingPkg, owningUserId);
Mark Rathjend891f012017-01-19 04:10:37 +00001109 }
Christopher Tateb218e762017-04-05 16:34:07 -07001110 }
Makoto Onuki0000d322017-11-28 16:31:47 -08001111 if (enableOverride) {
1112 if (Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
1113 final Setting overridden = getLocationProvidersAllowedSetting(owningUserId);
1114 if (overridden != null) {
1115 return overridden;
1116 }
1117 }
1118 }
Mark Rathjend891f012017-01-19 04:10:37 +00001119
Christopher Tateb218e762017-04-05 16:34:07 -07001120 // Not the SSAID; do a straight lookup
1121 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -07001122 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav7ec28e82015-05-20 17:01:10 -07001123 owningUserId, name);
1124 }
Svetoslav683914b2015-01-15 14:22:26 -08001125 }
1126
Mark Rathjend891f012017-01-19 04:10:37 +00001127 private boolean isNewSsaidSetting(String name) {
1128 return Settings.Secure.ANDROID_ID.equals(name)
1129 && UserHandle.getAppId(Binder.getCallingUid()) >= Process.FIRST_APPLICATION_UID;
1130 }
1131
Christopher Tateb218e762017-04-05 16:34:07 -07001132 private Setting getSsaidSettingLocked(PackageInfo callingPkg, int owningUserId) {
Mark Rathjend891f012017-01-19 04:10:37 +00001133 // Get uid of caller (key) used to store ssaid value
1134 String name = Integer.toString(
1135 UserHandle.getUid(owningUserId, UserHandle.getAppId(Binder.getCallingUid())));
1136
1137 if (DEBUG) {
1138 Slog.v(LOG_TAG, "getSsaidSettingLocked(" + name + "," + owningUserId + ")");
1139 }
1140
1141 // Retrieve the ssaid from the table if present.
1142 final Setting ssaid = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SSAID, owningUserId,
1143 name);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001144 // If the app is an Instant App use its stored SSAID instead of our own.
1145 final String instantSsaid;
1146 final long token = Binder.clearCallingIdentity();
1147 try {
1148 instantSsaid = mPackageManager.getInstantAppAndroidId(callingPkg.packageName,
1149 owningUserId);
1150 } catch (RemoteException e) {
1151 Slog.e(LOG_TAG, "Failed to get Instant App Android ID", e);
1152 return null;
1153 } finally {
1154 Binder.restoreCallingIdentity(token);
1155 }
Svet Ganov96c99462017-05-05 14:27:13 -07001156
1157 final SettingsState ssaidSettings = mSettingsRegistry.getSettingsLocked(
1158 SETTINGS_TYPE_SSAID, owningUserId);
1159
Chad Brubaker0d277a72017-04-12 16:56:53 -07001160 if (instantSsaid != null) {
1161 // Use the stored value if it is still valid.
1162 if (ssaid != null && instantSsaid.equals(ssaid.getValue())) {
Svet Ganov96c99462017-05-05 14:27:13 -07001163 return mascaradeSsaidSetting(ssaidSettings, ssaid);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001164 }
1165 // The value has changed, update the stored value.
Chad Brubaker0d277a72017-04-12 16:56:53 -07001166 final boolean success = ssaidSettings.insertSettingLocked(name, instantSsaid, null,
1167 true, callingPkg.packageName);
1168 if (!success) {
1169 throw new IllegalStateException("Failed to update instant app android id");
1170 }
Svet Ganov96c99462017-05-05 14:27:13 -07001171 Setting setting = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SSAID,
1172 owningUserId, name);
1173 return mascaradeSsaidSetting(ssaidSettings, setting);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001174 }
Mark Rathjend891f012017-01-19 04:10:37 +00001175
1176 // Lazy initialize ssaid if not yet present in ssaid table.
Mark Rathjenea617592017-01-18 23:03:41 -08001177 if (ssaid == null || ssaid.isNull() || ssaid.getValue() == null) {
Svet Ganov96c99462017-05-05 14:27:13 -07001178 Setting setting = mSettingsRegistry.generateSsaidLocked(callingPkg, owningUserId);
1179 return mascaradeSsaidSetting(ssaidSettings, setting);
Mark Rathjend891f012017-01-19 04:10:37 +00001180 }
1181
Svet Ganov96c99462017-05-05 14:27:13 -07001182 return mascaradeSsaidSetting(ssaidSettings, ssaid);
1183 }
1184
1185 private Setting mascaradeSsaidSetting(SettingsState settingsState, Setting ssaidSetting) {
1186 // SSAID settings are located in a dedicated table for internal bookkeeping
1187 // but for the world they reside in the secure table, so adjust the key here.
1188 // We have a special name when looking it up but want the world to see it as
1189 // "android_id".
1190 if (ssaidSetting != null) {
1191 return settingsState.new Setting(ssaidSetting) {
1192 @Override
1193 public int getKey() {
1194 final int userId = getUserIdFromKey(super.getKey());
1195 return makeKey(SETTINGS_TYPE_SECURE, userId);
1196 }
1197
1198 @Override
1199 public String getName() {
1200 return Settings.Secure.ANDROID_ID;
1201 }
1202 };
1203 }
1204 return null;
Mark Rathjend891f012017-01-19 04:10:37 +00001205 }
1206
Makoto Onuki0000d322017-11-28 16:31:47 -08001207 private Setting getLocationProvidersAllowedSetting(int owningUserId) {
1208 synchronized (mLock) {
1209 final Setting setting = getGlobalSetting(
1210 Global.LOCATION_GLOBAL_KILL_SWITCH);
1211 if (!"1".equals(setting.getValue())) {
1212 return null;
1213 }
1214 // Global kill-switch is enabled. Return an empty value.
1215 final SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
1216 SETTINGS_TYPE_SECURE, owningUserId);
1217 return settingsState.new Setting(
1218 Secure.LOCATION_PROVIDERS_ALLOWED,
1219 "", // value
1220 "", // tag
1221 "", // default value
1222 "", // package name
1223 false, // from system
1224 "0" // id
1225 ) {
1226 @Override
1227 public boolean update(String value, boolean setDefault, String packageName,
1228 String tag, boolean forceNonSystemPackage) {
1229 Slog.wtf(LOG_TAG, "update shoudln't be called on this instance.");
1230 return false;
1231 }
1232 };
1233 }
1234 }
1235
Svetoslav Ganove080da92016-12-21 17:10:35 -08001236 private boolean insertSecureSetting(String name, String value, String tag,
1237 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001238 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001239 Slog.v(LOG_TAG, "insertSecureSetting(" + name + ", " + value + ", "
Svetoslav Ganove080da92016-12-21 17:10:35 -08001240 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1241 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001242 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001243 return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId,
1244 MUTATION_OPERATION_INSERT, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001245 }
1246
Svet Ganov53a441c2016-04-19 19:38:00 -07001247 private boolean deleteSecureSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001248 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001249 Slog.v(LOG_TAG, "deleteSecureSetting(" + name + ", " + requestingUserId
1250 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001251 }
1252
Svetoslav Ganove080da92016-12-21 17:10:35 -08001253 return mutateSecureSetting(name, null, null, false, requestingUserId,
1254 MUTATION_OPERATION_DELETE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001255 }
1256
Svetoslav Ganove080da92016-12-21 17:10:35 -08001257 private boolean updateSecureSetting(String name, String value, String tag,
1258 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001259 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001260 Slog.v(LOG_TAG, "updateSecureSetting(" + name + ", " + value + ", "
Svetoslav Ganove080da92016-12-21 17:10:35 -08001261 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1262 + ", " + forceNotify +")");
Svetoslav683914b2015-01-15 14:22:26 -08001263 }
1264
Svetoslav Ganove080da92016-12-21 17:10:35 -08001265 return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId,
1266 MUTATION_OPERATION_UPDATE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001267 }
1268
Svetoslav Ganove080da92016-12-21 17:10:35 -08001269 private void resetSecureSetting(int requestingUserId, int mode, String tag) {
1270 if (DEBUG) {
1271 Slog.v(LOG_TAG, "resetSecureSetting(" + requestingUserId + ", "
1272 + mode + ", " + tag + ")");
1273 }
1274
1275 mutateSecureSetting(null, null, tag, false, requestingUserId,
1276 MUTATION_OPERATION_RESET, false, mode);
1277 }
1278
1279 private boolean mutateSecureSetting(String name, String value, String tag,
1280 boolean makeDefault, int requestingUserId, int operation, boolean forceNotify,
1281 int mode) {
Svetoslav683914b2015-01-15 14:22:26 -08001282 // Make sure the caller can change the settings.
1283 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
1284
Svetoslav683914b2015-01-15 14:22:26 -08001285 // Resolve the userId on whose behalf the call is made.
1286 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1287
Makoto Onuki28da2e32015-11-20 11:30:44 -08001288 // If this is a setting that is currently restricted for this user, do not allow
1289 // unrestricting changes.
Svetoslav Ganove080da92016-12-21 17:10:35 -08001290 if (name != null && isGlobalOrSecureSettingRestrictedForUser(name, callingUserId, value,
Victor Chang9c7b7062016-07-12 23:47:29 +01001291 Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -08001292 return false;
1293 }
1294
1295 // Determine the owning user as some profile settings are cloned from the parent.
1296 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
1297
1298 // Only the owning user can change the setting.
1299 if (owningUserId != callingUserId) {
1300 return false;
1301 }
1302
1303 // Special cases for location providers (sigh).
1304 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001305 return updateLocationProvidersAllowedLocked(value, tag, owningUserId, makeDefault,
1306 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -08001307 }
1308
1309 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001310 synchronized (mLock) {
1311 switch (operation) {
1312 case MUTATION_OPERATION_INSERT: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001313 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001314 owningUserId, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001315 getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001316 }
Svetoslav683914b2015-01-15 14:22:26 -08001317
Svetoslav7ec28e82015-05-20 17:01:10 -07001318 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001319 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001320 owningUserId, name, forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001321 }
Svetoslav683914b2015-01-15 14:22:26 -08001322
Svetoslav7ec28e82015-05-20 17:01:10 -07001323 case MUTATION_OPERATION_UPDATE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001324 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001325 owningUserId, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001326 getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001327 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001328
1329 case MUTATION_OPERATION_RESET: {
1330 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_SECURE,
1331 UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag);
1332 } return true;
Svetoslav683914b2015-01-15 14:22:26 -08001333 }
1334 }
1335
1336 return false;
1337 }
1338
Svetoslav7ec28e82015-05-20 17:01:10 -07001339 private Cursor getAllSystemSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001340 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001341 Slog.v(LOG_TAG, "getAllSecureSystem(" + userId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001342 }
1343
1344 // Resolve the userId on whose behalf the call is made.
1345 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
1346
Svetoslav7ec28e82015-05-20 17:01:10 -07001347 synchronized (mLock) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001348 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SYSTEM, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -08001349
Svetoslav7ec28e82015-05-20 17:01:10 -07001350 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001351
Svetoslav7ec28e82015-05-20 17:01:10 -07001352 String[] normalizedProjection = normalizeProjection(projection);
1353 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001354
Svetoslav7ec28e82015-05-20 17:01:10 -07001355 for (int i = 0; i < nameCount; i++) {
1356 String name = names.get(i);
Svetoslav683914b2015-01-15 14:22:26 -08001357
Svetoslav7ec28e82015-05-20 17:01:10 -07001358 // Determine the owning user as some profile settings are cloned from the parent.
1359 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId,
1360 name);
Svetoslav683914b2015-01-15 14:22:26 -08001361
Svetoslav7ec28e82015-05-20 17:01:10 -07001362 Setting setting = mSettingsRegistry.getSettingLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001363 SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001364 appendSettingToCursor(result, setting);
1365 }
1366
1367 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001368 }
Svetoslav683914b2015-01-15 14:22:26 -08001369 }
1370
Svetoslav7ec28e82015-05-20 17:01:10 -07001371 private Setting getSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001372 if (DEBUG) {
1373 Slog.v(LOG_TAG, "getSystemSetting(" + name + ", " + requestingUserId + ")");
1374 }
1375
1376 // Resolve the userId on whose behalf the call is made.
1377 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1378
Chad Brubakera6830e72017-04-28 17:34:36 -07001379 // Ensure the caller can access the setting.
1380 enforceSettingReadable(name, SETTINGS_TYPE_SYSTEM, UserHandle.getCallingUserId());
Chad Brubaker97bccee2017-01-05 15:51:41 -08001381
Svetoslav683914b2015-01-15 14:22:26 -08001382 // Determine the owning user as some profile settings are cloned from the parent.
1383 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1384
1385 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001386 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -07001387 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001388 }
Svetoslav683914b2015-01-15 14:22:26 -08001389 }
1390
Svetoslav7ec28e82015-05-20 17:01:10 -07001391 private boolean insertSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001392 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001393 Slog.v(LOG_TAG, "insertSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001394 + requestingUserId + ")");
1395 }
1396
Svetoslav7ec28e82015-05-20 17:01:10 -07001397 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT);
Svetoslav683914b2015-01-15 14:22:26 -08001398 }
1399
Svetoslav7ec28e82015-05-20 17:01:10 -07001400 private boolean deleteSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001401 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001402 Slog.v(LOG_TAG, "deleteSystemSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001403 }
1404
Svetoslav7ec28e82015-05-20 17:01:10 -07001405 return mutateSystemSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE);
Svetoslav683914b2015-01-15 14:22:26 -08001406 }
1407
Svetoslav7ec28e82015-05-20 17:01:10 -07001408 private boolean updateSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001409 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001410 Slog.v(LOG_TAG, "updateSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001411 + requestingUserId + ")");
1412 }
1413
Svetoslav7ec28e82015-05-20 17:01:10 -07001414 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE);
Svetoslav683914b2015-01-15 14:22:26 -08001415 }
1416
Svetoslav7ec28e82015-05-20 17:01:10 -07001417 private boolean mutateSystemSetting(String name, String value, int runAsUserId,
Svetoslav683914b2015-01-15 14:22:26 -08001418 int operation) {
Billy Lau6ad2d662015-07-18 00:26:58 +01001419 if (!hasWriteSecureSettingsPermission()) {
1420 // If the caller doesn't hold WRITE_SECURE_SETTINGS, we verify whether this
1421 // operation is allowed for the calling package through appops.
1422 if (!Settings.checkAndNoteWriteSettingsOperation(getContext(),
1423 Binder.getCallingUid(), getCallingPackage(), true)) {
1424 return false;
1425 }
Svetoslav683914b2015-01-15 14:22:26 -08001426 }
1427
Svetoslav683914b2015-01-15 14:22:26 -08001428 // Resolve the userId on whose behalf the call is made.
1429 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId);
1430
Svetoslavd8d25e02015-11-20 13:09:26 -08001431 // Enforce what the calling package can mutate the system settings.
1432 enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name, callingUserId);
1433
Svetoslav683914b2015-01-15 14:22:26 -08001434 // Determine the owning user as some profile settings are cloned from the parent.
1435 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1436
1437 // Only the owning user id can change the setting.
1438 if (owningUserId != callingUserId) {
1439 return false;
1440 }
1441
Jeff Sharkey413573a2016-02-22 17:52:45 -07001442 // Invalidate any relevant cache files
1443 String cacheName = null;
1444 if (Settings.System.RINGTONE.equals(name)) {
1445 cacheName = Settings.System.RINGTONE_CACHE;
1446 } else if (Settings.System.NOTIFICATION_SOUND.equals(name)) {
1447 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
1448 } else if (Settings.System.ALARM_ALERT.equals(name)) {
1449 cacheName = Settings.System.ALARM_ALERT_CACHE;
1450 }
1451 if (cacheName != null) {
1452 final File cacheFile = new File(
Andre Lago3fa139c2016-08-04 13:53:44 +01001453 getRingtoneCacheDir(owningUserId), cacheName);
Jeff Sharkey413573a2016-02-22 17:52:45 -07001454 cacheFile.delete();
1455 }
1456
Svetoslav683914b2015-01-15 14:22:26 -08001457 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001458 synchronized (mLock) {
1459 switch (operation) {
1460 case MUTATION_OPERATION_INSERT: {
1461 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001462 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001463 owningUserId, name, value, null, false, getCallingPackage(),
1464 false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001465 }
1466
1467 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001468 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001469 owningUserId, name, false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001470 }
1471
1472 case MUTATION_OPERATION_UPDATE: {
1473 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001474 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001475 owningUserId, name, value, null, false, getCallingPackage(),
1476 false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001477 }
Svetoslav683914b2015-01-15 14:22:26 -08001478 }
1479
Svetoslav7ec28e82015-05-20 17:01:10 -07001480 return false;
Svetoslav683914b2015-01-15 14:22:26 -08001481 }
Svetoslav683914b2015-01-15 14:22:26 -08001482 }
1483
Billy Lau6ad2d662015-07-18 00:26:58 +01001484 private boolean hasWriteSecureSettingsPermission() {
Svetoslavf41334b2015-06-23 12:06:03 -07001485 // Write secure settings is a more protected permission. If caller has it we are good.
1486 if (getContext().checkCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS)
1487 == PackageManager.PERMISSION_GRANTED) {
1488 return true;
1489 }
1490
Svetoslavf41334b2015-06-23 12:06:03 -07001491 return false;
1492 }
1493
Svetoslav683914b2015-01-15 14:22:26 -08001494 private void validateSystemSettingValue(String name, String value) {
Michal Karpinski2c37b082018-01-18 16:14:27 +00001495 SettingsValidators.Validator validator = Settings.System.VALIDATORS.get(name);
Svetoslav683914b2015-01-15 14:22:26 -08001496 if (validator != null && !validator.validate(value)) {
1497 throw new IllegalArgumentException("Invalid value: " + value
1498 + " for setting: " + name);
1499 }
1500 }
1501
Alex Klyubin1991f572017-03-03 14:08:36 -08001502 /**
1503 * Returns {@code true} if the specified secure setting should be accessible to the caller.
1504 */
1505 private boolean isSecureSettingAccessible(String name, int callingUserId,
1506 int owningUserId) {
1507 // Special case for location (sigh).
1508 // This check is not inside the name-based checks below because this method performs checks
1509 // only if the calling user ID is not the same as the owning user ID.
1510 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
1511 return false;
1512 }
1513
1514 switch (name) {
1515 case "bluetooth_address":
1516 // BluetoothManagerService for some reason stores the Android's Bluetooth MAC
1517 // address in this secure setting. Secure settings can normally be read by any app,
1518 // which thus enables them to bypass the recently introduced restrictions on access
1519 // to device identifiers.
1520 // To mitigate this we make this setting available only to callers privileged to see
1521 // this device's MAC addresses, same as through public API
1522 // BluetoothAdapter.getAddress() (see BluetoothManagerService for details).
1523 return getContext().checkCallingOrSelfPermission(
1524 Manifest.permission.LOCAL_MAC_ADDRESS) == PackageManager.PERMISSION_GRANTED;
1525 default:
1526 return true;
1527 }
1528 }
1529
Svetoslav683914b2015-01-15 14:22:26 -08001530 private boolean isLocationProvidersAllowedRestricted(String name, int callingUserId,
1531 int owningUserId) {
1532 // Optimization - location providers are restricted only for managed profiles.
1533 if (callingUserId == owningUserId) {
1534 return false;
1535 }
1536 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)
1537 && mUserManager.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION,
1538 new UserHandle(callingUserId))) {
1539 return true;
1540 }
1541 return false;
1542 }
1543
Makoto Onuki28da2e32015-11-20 11:30:44 -08001544 /**
1545 * Checks whether changing a setting to a value is prohibited by the corresponding user
1546 * restriction.
1547 *
Svet Ganov53a441c2016-04-19 19:38:00 -07001548 * <p>See also {@link com.android.server.pm.UserRestrictionsUtils#applyUserRestriction(
1549 * Context, int, String, boolean)}, which should be in sync with this method.
Makoto Onuki28da2e32015-11-20 11:30:44 -08001550 *
1551 * @return true if the change is prohibited, false if the change is allowed.
1552 */
1553 private boolean isGlobalOrSecureSettingRestrictedForUser(String setting, int userId,
Victor Chang9c7b7062016-07-12 23:47:29 +01001554 String value, int callingUid) {
Makoto Onuki28da2e32015-11-20 11:30:44 -08001555 String restriction;
1556 switch (setting) {
1557 case Settings.Secure.LOCATION_MODE:
1558 // Note LOCATION_MODE will be converted into LOCATION_PROVIDERS_ALLOWED
1559 // in android.provider.Settings.Secure.putStringForUser(), so we shouldn't come
1560 // here normally, but we still protect it here from a direct provider write.
1561 if (String.valueOf(Settings.Secure.LOCATION_MODE_OFF).equals(value)) return false;
1562 restriction = UserManager.DISALLOW_SHARE_LOCATION;
1563 break;
1564
1565 case Settings.Secure.LOCATION_PROVIDERS_ALLOWED:
1566 // See SettingsProvider.updateLocationProvidersAllowedLocked. "-" is to disable
1567 // a provider, which should be allowed even if the user restriction is set.
1568 if (value != null && value.startsWith("-")) return false;
1569 restriction = UserManager.DISALLOW_SHARE_LOCATION;
1570 break;
1571
1572 case Settings.Secure.INSTALL_NON_MARKET_APPS:
1573 if ("0".equals(value)) return false;
1574 restriction = UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES;
1575 break;
1576
1577 case Settings.Global.ADB_ENABLED:
1578 if ("0".equals(value)) return false;
1579 restriction = UserManager.DISALLOW_DEBUGGING_FEATURES;
1580 break;
1581
1582 case Settings.Global.PACKAGE_VERIFIER_ENABLE:
1583 case Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB:
1584 if ("1".equals(value)) return false;
1585 restriction = UserManager.ENSURE_VERIFY_APPS;
1586 break;
1587
1588 case Settings.Global.PREFERRED_NETWORK_MODE:
1589 restriction = UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS;
1590 break;
1591
Victor Chang9c7b7062016-07-12 23:47:29 +01001592 case Settings.Secure.ALWAYS_ON_VPN_APP:
1593 case Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN:
1594 // Whitelist system uid (ConnectivityService) and root uid to change always-on vpn
Svetoslav Ganove080da92016-12-21 17:10:35 -08001595 final int appId = UserHandle.getAppId(callingUid);
1596 if (appId == Process.SYSTEM_UID || appId == Process.ROOT_UID) {
Victor Chang9c7b7062016-07-12 23:47:29 +01001597 return false;
1598 }
1599 restriction = UserManager.DISALLOW_CONFIG_VPN;
1600 break;
1601
Benjamin Franz0ff13fc2016-07-12 13:42:21 +01001602 case Settings.Global.SAFE_BOOT_DISALLOWED:
1603 if ("1".equals(value)) return false;
1604 restriction = UserManager.DISALLOW_SAFE_BOOT;
1605 break;
1606
yuemingw265c6922018-01-11 18:31:14 +00001607 case Settings.Global.AIRPLANE_MODE_ON:
1608 if ("0".equals(value)) return false;
1609 restriction = UserManager.DISALLOW_AIRPLANE_MODE;
1610 break;
1611
Makoto Onuki28da2e32015-11-20 11:30:44 -08001612 default:
Mahaver Chopra87648752016-01-08 19:23:57 +00001613 if (setting != null && setting.startsWith(Settings.Global.DATA_ROAMING)) {
Mahaver Chopradea471e2015-12-17 11:02:37 +00001614 if ("0".equals(value)) return false;
1615 restriction = UserManager.DISALLOW_DATA_ROAMING;
1616 break;
1617 }
Makoto Onuki28da2e32015-11-20 11:30:44 -08001618 return false;
Svetoslav683914b2015-01-15 14:22:26 -08001619 }
Makoto Onuki28da2e32015-11-20 11:30:44 -08001620
1621 return mUserManager.hasUserRestriction(restriction, UserHandle.of(userId));
Svetoslav683914b2015-01-15 14:22:26 -08001622 }
1623
1624 private int resolveOwningUserIdForSecureSettingLocked(int userId, String setting) {
1625 return resolveOwningUserIdLocked(userId, sSecureCloneToManagedSettings, setting);
1626 }
1627
1628 private int resolveOwningUserIdForSystemSettingLocked(int userId, String setting) {
Andre Lago3fa139c2016-08-04 13:53:44 +01001629 final int parentId;
1630 // Resolves dependency if setting has a dependency and the calling user has a parent
1631 if (sSystemCloneFromParentOnDependency.containsKey(setting)
1632 && (parentId = getGroupParentLocked(userId)) != userId) {
1633 // The setting has a dependency and the profile has a parent
1634 String dependency = sSystemCloneFromParentOnDependency.get(setting);
Chad Brubaker97bccee2017-01-05 15:51:41 -08001635 // Lookup the dependency setting as ourselves, some callers may not have access to it.
1636 final long token = Binder.clearCallingIdentity();
1637 try {
1638 Setting settingObj = getSecureSetting(dependency, userId);
1639 if (settingObj != null && settingObj.getValue().equals("1")) {
1640 return parentId;
1641 }
1642 } finally {
1643 Binder.restoreCallingIdentity(token);
Andre Lago3fa139c2016-08-04 13:53:44 +01001644 }
1645 }
Svetoslav683914b2015-01-15 14:22:26 -08001646 return resolveOwningUserIdLocked(userId, sSystemCloneToManagedSettings, setting);
1647 }
1648
1649 private int resolveOwningUserIdLocked(int userId, Set<String> keys, String name) {
1650 final int parentId = getGroupParentLocked(userId);
1651 if (parentId != userId && keys.contains(name)) {
1652 return parentId;
1653 }
1654 return userId;
1655 }
1656
Svetoslavf41334b2015-06-23 12:06:03 -07001657 private void enforceRestrictedSystemSettingsMutationForCallingPackage(int operation,
Xiaohui Chen43765b72015-08-31 10:57:33 -07001658 String name, int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001659 // System/root/shell can mutate whatever secure settings they want.
1660 final int callingUid = Binder.getCallingUid();
Svetoslav Ganove080da92016-12-21 17:10:35 -08001661 final int appId = UserHandle.getAppId(callingUid);
1662 if (appId == android.os.Process.SYSTEM_UID
1663 || appId == Process.SHELL_UID
1664 || appId == Process.ROOT_UID) {
Svetoslav683914b2015-01-15 14:22:26 -08001665 return;
1666 }
1667
1668 switch (operation) {
1669 case MUTATION_OPERATION_INSERT:
1670 // Insert updates.
1671 case MUTATION_OPERATION_UPDATE: {
1672 if (Settings.System.PUBLIC_SETTINGS.contains(name)) {
1673 return;
1674 }
1675
1676 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001677 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001678
1679 // Privileged apps can do whatever they want.
1680 if ((packageInfo.applicationInfo.privateFlags
1681 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1682 return;
1683 }
1684
1685 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1686 packageInfo.applicationInfo.targetSdkVersion, name);
1687 } break;
1688
1689 case MUTATION_OPERATION_DELETE: {
1690 if (Settings.System.PUBLIC_SETTINGS.contains(name)
1691 || Settings.System.PRIVATE_SETTINGS.contains(name)) {
1692 throw new IllegalArgumentException("You cannot delete system defined"
1693 + " secure settings.");
1694 }
1695
1696 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001697 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001698
1699 // Privileged apps can do whatever they want.
1700 if ((packageInfo.applicationInfo.privateFlags &
1701 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1702 return;
1703 }
1704
1705 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1706 packageInfo.applicationInfo.targetSdkVersion, name);
1707 } break;
1708 }
1709 }
1710
Todd Kennedybe0b8892017-02-15 14:13:52 -08001711 private Set<String> getInstantAppAccessibleSettings(int settingsType) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001712 switch (settingsType) {
1713 case SETTINGS_TYPE_GLOBAL:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001714 return Settings.Global.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001715 case SETTINGS_TYPE_SECURE:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001716 return Settings.Secure.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001717 case SETTINGS_TYPE_SYSTEM:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001718 return Settings.System.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001719 default:
1720 throw new IllegalArgumentException("Invalid settings type: " + settingsType);
1721 }
1722 }
1723
Chad Brubaker20e0dc32017-04-28 18:24:55 -07001724 private Set<String> getOverlayInstantAppAccessibleSettings(int settingsType) {
1725 switch (settingsType) {
1726 case SETTINGS_TYPE_GLOBAL:
1727 return OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS;
1728 case SETTINGS_TYPE_SYSTEM:
1729 return OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS;
1730 case SETTINGS_TYPE_SECURE:
1731 return OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS;
1732 default:
1733 throw new IllegalArgumentException("Invalid settings type: " + settingsType);
1734 }
1735 }
1736
Chad Brubaker97bccee2017-01-05 15:51:41 -08001737 private List<String> getSettingsNamesLocked(int settingsType, int userId) {
Chad Brubakerb6108d62017-12-23 20:06:44 -08001738 // Don't enforce the instant app whitelist for now -- its too prone to unintended breakage
1739 // in the current form.
1740 return mSettingsRegistry.getSettingsNamesLocked(settingsType, userId);
Chad Brubaker97bccee2017-01-05 15:51:41 -08001741 }
1742
Chad Brubakera6830e72017-04-28 17:34:36 -07001743 private void enforceSettingReadable(String settingName, int settingsType, int userId) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001744 if (UserHandle.getAppId(Binder.getCallingUid()) < Process.FIRST_APPLICATION_UID) {
1745 return;
1746 }
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001747 ApplicationInfo ai = getCallingApplicationInfoOrThrow();
Svetoslav Ganov096d3042017-01-30 16:34:13 -08001748 if (!ai.isInstantApp()) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001749 return;
1750 }
Chad Brubaker20e0dc32017-04-28 18:24:55 -07001751 if (!getInstantAppAccessibleSettings(settingsType).contains(settingName)
1752 && !getOverlayInstantAppAccessibleSettings(settingsType).contains(settingName)) {
Chad Brubakerb6108d62017-12-23 20:06:44 -08001753 // Don't enforce the instant app whitelist for now -- its too prone to unintended
1754 // breakage in the current form.
1755 Slog.w(LOG_TAG, "Instant App " + ai.packageName
1756 + " trying to access unexposed setting, this will be an error in the future.");
Chad Brubaker97bccee2017-01-05 15:51:41 -08001757 }
1758 }
1759
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001760 private ApplicationInfo getCallingApplicationInfoOrThrow() {
1761 // We always use the callingUid for this lookup. This means that if hypothetically an
1762 // app was installed in user A with cross user and in user B as an Instant App
1763 // the app in A would be able to see all the settings in user B. However since cross
1764 // user is a system permission and the app must be uninstalled in B and then installed as
1765 // an Instant App that situation is not realistic or supported.
Chad Brubaker97bccee2017-01-05 15:51:41 -08001766 ApplicationInfo ai = null;
1767 try {
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001768 ai = mPackageManager.getApplicationInfo(getCallingPackage(), 0
1769 , UserHandle.getCallingUserId());
Chad Brubaker97bccee2017-01-05 15:51:41 -08001770 } catch (RemoteException ignored) {
1771 }
1772 if (ai == null) {
1773 throw new IllegalStateException("Failed to lookup info for package "
1774 + getCallingPackage());
1775 }
1776 return ai;
1777 }
1778
Xiaohui Chen43765b72015-08-31 10:57:33 -07001779 private PackageInfo getCallingPackageInfoOrThrow(int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001780 try {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001781 PackageInfo packageInfo = mPackageManager.getPackageInfo(
1782 getCallingPackage(), 0, userId);
1783 if (packageInfo != null) {
1784 return packageInfo;
1785 }
Xiaohui Chen43765b72015-08-31 10:57:33 -07001786 } catch (RemoteException e) {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001787 /* ignore */
Svetoslav683914b2015-01-15 14:22:26 -08001788 }
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001789 throw new IllegalStateException("Calling package doesn't exist");
Svetoslav683914b2015-01-15 14:22:26 -08001790 }
1791
1792 private int getGroupParentLocked(int userId) {
1793 // Most frequent use case.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001794 if (userId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08001795 return userId;
1796 }
1797 // We are in the same process with the user manager and the returned
1798 // user info is a cached instance, so just look up instead of cache.
1799 final long identity = Binder.clearCallingIdentity();
1800 try {
Svetoslav7ec28e82015-05-20 17:01:10 -07001801 // Just a lookup and not reentrant, so holding a lock is fine.
Svetoslav683914b2015-01-15 14:22:26 -08001802 UserInfo userInfo = mUserManager.getProfileParent(userId);
1803 return (userInfo != null) ? userInfo.id : userId;
1804 } finally {
1805 Binder.restoreCallingIdentity(identity);
1806 }
1807 }
1808
Svetoslav683914b2015-01-15 14:22:26 -08001809 private void enforceWritePermission(String permission) {
1810 if (getContext().checkCallingOrSelfPermission(permission)
1811 != PackageManager.PERMISSION_GRANTED) {
1812 throw new SecurityException("Permission denial: writing to settings requires:"
1813 + permission);
1814 }
1815 }
1816
1817 /*
1818 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
1819 * This setting contains a list of the currently enabled location providers.
1820 * But helper functions in android.providers.Settings can enable or disable
1821 * a single provider by using a "+" or "-" prefix before the provider name.
1822 *
Makoto Onuki28da2e32015-11-20 11:30:44 -08001823 * <p>See also {@link #isGlobalOrSecureSettingRestrictedForUser()}. If DISALLOW_SHARE_LOCATION
1824 * is set, the said method will only allow values with the "-" prefix.
1825 *
Svetoslav683914b2015-01-15 14:22:26 -08001826 * @returns whether the enabled location providers changed.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001827 */
Svetoslav Ganove080da92016-12-21 17:10:35 -08001828 private boolean updateLocationProvidersAllowedLocked(String value, String tag,
1829 int owningUserId, boolean makeDefault, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001830 if (TextUtils.isEmpty(value)) {
1831 return false;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001832 }
1833
Svetoslav683914b2015-01-15 14:22:26 -08001834 final char prefix = value.charAt(0);
1835 if (prefix != '+' && prefix != '-') {
Svet Ganov53a441c2016-04-19 19:38:00 -07001836 if (forceNotify) {
1837 final int key = makeKey(SETTINGS_TYPE_SECURE, owningUserId);
1838 mSettingsRegistry.notifyForSettingsChange(key,
1839 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
1840 }
Svetoslav683914b2015-01-15 14:22:26 -08001841 return false;
1842 }
1843
1844 // skip prefix
1845 value = value.substring(1);
1846
Svetoslav7ec28e82015-05-20 17:01:10 -07001847 Setting settingValue = getSecureSetting(
Chad Brubaker97bccee2017-01-05 15:51:41 -08001848 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, owningUserId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001849 if (settingValue == null) {
1850 return false;
1851 }
Svetoslav683914b2015-01-15 14:22:26 -08001852
Svetoslav Ganovcac64f62017-02-21 13:24:09 -08001853 String oldProviders = !settingValue.isNull() ? settingValue.getValue() : "";
Svetoslav683914b2015-01-15 14:22:26 -08001854
1855 int index = oldProviders.indexOf(value);
1856 int end = index + value.length();
1857
1858 // check for commas to avoid matching on partial string
1859 if (index > 0 && oldProviders.charAt(index - 1) != ',') {
1860 index = -1;
1861 }
1862
1863 // check for commas to avoid matching on partial string
1864 if (end < oldProviders.length() && oldProviders.charAt(end) != ',') {
1865 index = -1;
1866 }
1867
1868 String newProviders;
1869
1870 if (prefix == '+' && index < 0) {
1871 // append the provider to the list if not present
1872 if (oldProviders.length() == 0) {
1873 newProviders = value;
1874 } else {
1875 newProviders = oldProviders + ',' + value;
1876 }
1877 } else if (prefix == '-' && index >= 0) {
1878 // remove the provider from the list if present
1879 // remove leading or trailing comma
1880 if (index > 0) {
1881 index--;
1882 } else if (end < oldProviders.length()) {
1883 end++;
1884 }
1885
1886 newProviders = oldProviders.substring(0, index);
1887 if (end < oldProviders.length()) {
1888 newProviders += oldProviders.substring(end);
1889 }
1890 } else {
1891 // nothing changed, so no need to update the database
Svet Ganov53a441c2016-04-19 19:38:00 -07001892 if (forceNotify) {
1893 final int key = makeKey(SETTINGS_TYPE_SECURE, owningUserId);
1894 mSettingsRegistry.notifyForSettingsChange(key,
1895 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
1896 }
Svetoslav683914b2015-01-15 14:22:26 -08001897 return false;
1898 }
1899
Svet Ganov53a441c2016-04-19 19:38:00 -07001900 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslavb596a2c2015-02-17 21:37:09 -08001901 owningUserId, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, newProviders,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001902 tag, makeDefault, getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav683914b2015-01-15 14:22:26 -08001903 }
1904
Svetoslav683914b2015-01-15 14:22:26 -08001905 private static void warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1906 int targetSdkVersion, String name) {
1907 // If the app targets Lollipop MR1 or older SDK we warn, otherwise crash.
1908 if (targetSdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) {
1909 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
1910 Slog.w(LOG_TAG, "You shouldn't not change private system settings."
1911 + " This will soon become an error.");
1912 } else {
1913 Slog.w(LOG_TAG, "You shouldn't keep your settings in the secure settings."
1914 + " This will soon become an error.");
1915 }
1916 } else {
1917 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
1918 throw new IllegalArgumentException("You cannot change private secure settings.");
1919 } else {
1920 throw new IllegalArgumentException("You cannot keep your settings in"
1921 + " the secure settings.");
1922 }
1923 }
1924 }
1925
1926 private static int resolveCallingUserIdEnforcingPermissionsLocked(int requestingUserId) {
1927 if (requestingUserId == UserHandle.getCallingUserId()) {
1928 return requestingUserId;
1929 }
1930 return ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1931 Binder.getCallingUid(), requestingUserId, false, true,
1932 "get/set setting for user", null);
1933 }
1934
Svet Ganov53a441c2016-04-19 19:38:00 -07001935 private Bundle packageValueForCallResult(Setting setting,
1936 boolean trackingGeneration) {
1937 if (!trackingGeneration) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001938 if (setting == null || setting.isNull()) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001939 return NULL_SETTING_BUNDLE;
1940 }
1941 return Bundle.forPair(Settings.NameValueTable.VALUE, setting.getValue());
Svetoslav683914b2015-01-15 14:22:26 -08001942 }
Svet Ganov53a441c2016-04-19 19:38:00 -07001943 Bundle result = new Bundle();
1944 result.putString(Settings.NameValueTable.VALUE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001945 !setting.isNull() ? setting.getValue() : null);
Svet Ganov96c99462017-05-05 14:27:13 -07001946
Svetoslav Ganove080da92016-12-21 17:10:35 -08001947 mSettingsRegistry.mGenerationRegistry.addGenerationData(result, setting.getKey());
Svet Ganov53a441c2016-04-19 19:38:00 -07001948 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001949 }
1950
1951 private static int getRequestingUserId(Bundle args) {
1952 final int callingUserId = UserHandle.getCallingUserId();
1953 return (args != null) ? args.getInt(Settings.CALL_METHOD_USER_KEY, callingUserId)
1954 : callingUserId;
1955 }
1956
Svet Ganov53a441c2016-04-19 19:38:00 -07001957 private boolean isTrackingGeneration(Bundle args) {
1958 return args != null && args.containsKey(Settings.CALL_METHOD_TRACK_GENERATION_KEY);
1959 }
1960
Svetoslav683914b2015-01-15 14:22:26 -08001961 private static String getSettingValue(Bundle args) {
1962 return (args != null) ? args.getString(Settings.NameValueTable.VALUE) : null;
1963 }
1964
Svetoslav Ganove080da92016-12-21 17:10:35 -08001965 private static String getSettingTag(Bundle args) {
1966 return (args != null) ? args.getString(Settings.CALL_METHOD_TAG_KEY) : null;
1967 }
1968
1969 private static boolean getSettingMakeDefault(Bundle args) {
1970 return (args != null) && args.getBoolean(Settings.CALL_METHOD_MAKE_DEFAULT_KEY);
1971 }
1972
1973 private static int getResetModeEnforcingPermission(Bundle args) {
1974 final int mode = (args != null) ? args.getInt(Settings.CALL_METHOD_RESET_MODE_KEY) : 0;
1975 switch (mode) {
1976 case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: {
1977 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
1978 throw new SecurityException("Only system, shell/root on a "
1979 + "debuggable build can reset to untrusted defaults");
1980 }
1981 return mode;
1982 }
1983 case Settings.RESET_MODE_UNTRUSTED_CHANGES: {
1984 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
1985 throw new SecurityException("Only system, shell/root on a "
1986 + "debuggable build can reset untrusted changes");
1987 }
1988 return mode;
1989 }
1990 case Settings.RESET_MODE_TRUSTED_DEFAULTS: {
1991 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
1992 throw new SecurityException("Only system, shell/root on a "
1993 + "debuggable build can reset to trusted defaults");
1994 }
1995 return mode;
1996 }
1997 case Settings.RESET_MODE_PACKAGE_DEFAULTS: {
1998 return mode;
1999 }
2000 }
2001 throw new IllegalArgumentException("Invalid reset mode: " + mode);
2002 }
2003
2004 private static boolean isCallerSystemOrShellOrRootOnDebuggableBuild() {
2005 final int appId = UserHandle.getAppId(Binder.getCallingUid());
2006 return appId == SYSTEM_UID || (Build.IS_DEBUGGABLE
2007 && (appId == SHELL_UID || appId == ROOT_UID));
2008 }
2009
Svetoslav683914b2015-01-15 14:22:26 -08002010 private static String getValidTableOrThrow(Uri uri) {
2011 if (uri.getPathSegments().size() > 0) {
2012 String table = uri.getPathSegments().get(0);
2013 if (DatabaseHelper.isValidTable(table)) {
2014 return table;
2015 }
2016 throw new IllegalArgumentException("Bad root path: " + table);
2017 }
2018 throw new IllegalArgumentException("Invalid URI:" + uri);
2019 }
2020
2021 private static MatrixCursor packageSettingForQuery(Setting setting, String[] projection) {
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002022 if (setting.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08002023 return new MatrixCursor(projection, 0);
2024 }
2025 MatrixCursor cursor = new MatrixCursor(projection, 1);
2026 appendSettingToCursor(cursor, setting);
2027 return cursor;
2028 }
2029
2030 private static String[] normalizeProjection(String[] projection) {
2031 if (projection == null) {
2032 return ALL_COLUMNS;
2033 }
2034
2035 final int columnCount = projection.length;
2036 for (int i = 0; i < columnCount; i++) {
2037 String column = projection[i];
2038 if (!ArrayUtils.contains(ALL_COLUMNS, column)) {
2039 throw new IllegalArgumentException("Invalid column: " + column);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002040 }
2041 }
2042
Svetoslav683914b2015-01-15 14:22:26 -08002043 return projection;
2044 }
2045
2046 private static void appendSettingToCursor(MatrixCursor cursor, Setting setting) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002047 if (setting == null || setting.isNull()) {
Suprabh Shuklac9d064a2016-04-12 18:45:34 -07002048 return;
2049 }
Svetoslav683914b2015-01-15 14:22:26 -08002050 final int columnCount = cursor.getColumnCount();
2051
2052 String[] values = new String[columnCount];
2053
2054 for (int i = 0; i < columnCount; i++) {
2055 String column = cursor.getColumnName(i);
2056
2057 switch (column) {
2058 case Settings.NameValueTable._ID: {
2059 values[i] = setting.getId();
2060 } break;
2061
2062 case Settings.NameValueTable.NAME: {
2063 values[i] = setting.getName();
2064 } break;
2065
2066 case Settings.NameValueTable.VALUE: {
2067 values[i] = setting.getValue();
2068 } break;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002069 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002070 }
2071
Svetoslav683914b2015-01-15 14:22:26 -08002072 cursor.addRow(values);
2073 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002074
Makoto Onuki3a2c35782015-06-18 11:21:58 -07002075 private static boolean isKeyValid(String key) {
2076 return !(TextUtils.isEmpty(key) || SettingsState.isBinary(key));
2077 }
2078
Svetoslav683914b2015-01-15 14:22:26 -08002079 private static final class Arguments {
2080 private static final Pattern WHERE_PATTERN_WITH_PARAM_NO_BRACKETS =
2081 Pattern.compile("[\\s]*name[\\s]*=[\\s]*\\?[\\s]*");
2082
2083 private static final Pattern WHERE_PATTERN_WITH_PARAM_IN_BRACKETS =
2084 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*\\?[\\s]*\\)[\\s]*");
2085
2086 private static final Pattern WHERE_PATTERN_NO_PARAM_IN_BRACKETS =
2087 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*\\)[\\s]*");
2088
2089 private static final Pattern WHERE_PATTERN_NO_PARAM_NO_BRACKETS =
2090 Pattern.compile("[\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*");
2091
2092 public final String table;
2093 public final String name;
2094
2095 public Arguments(Uri uri, String where, String[] whereArgs, boolean supportAll) {
2096 final int segmentSize = uri.getPathSegments().size();
2097 switch (segmentSize) {
2098 case 1: {
2099 if (where != null
2100 && (WHERE_PATTERN_WITH_PARAM_NO_BRACKETS.matcher(where).matches()
2101 || WHERE_PATTERN_WITH_PARAM_IN_BRACKETS.matcher(where).matches())
2102 && whereArgs.length == 1) {
2103 name = whereArgs[0];
2104 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08002105 return;
Svetoslav683914b2015-01-15 14:22:26 -08002106 } else if (where != null
2107 && (WHERE_PATTERN_NO_PARAM_NO_BRACKETS.matcher(where).matches()
2108 || WHERE_PATTERN_NO_PARAM_IN_BRACKETS.matcher(where).matches())) {
2109 final int startIndex = Math.max(where.indexOf("'"),
2110 where.indexOf("\"")) + 1;
2111 final int endIndex = Math.max(where.lastIndexOf("'"),
2112 where.lastIndexOf("\""));
2113 name = where.substring(startIndex, endIndex);
2114 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08002115 return;
Svetoslav683914b2015-01-15 14:22:26 -08002116 } else if (supportAll && where == null && whereArgs == null) {
2117 name = null;
2118 table = computeTableForSetting(uri, null);
Svetoslav28494652015-02-12 14:11:42 -08002119 return;
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002120 }
Svetoslav683914b2015-01-15 14:22:26 -08002121 } break;
2122
Svetoslav28494652015-02-12 14:11:42 -08002123 case 2: {
2124 if (where == null && whereArgs == null) {
2125 name = uri.getPathSegments().get(1);
2126 table = computeTableForSetting(uri, name);
2127 return;
2128 }
2129 } break;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002130 }
Svetoslav28494652015-02-12 14:11:42 -08002131
2132 EventLogTags.writeUnsupportedSettingsQuery(
2133 uri.toSafeString(), where, Arrays.toString(whereArgs));
2134 String message = String.format( "Supported SQL:\n"
2135 + " uri content://some_table/some_property with null where and where args\n"
2136 + " uri content://some_table with query name=? and single name as arg\n"
2137 + " uri content://some_table with query name=some_name and null args\n"
2138 + " but got - uri:%1s, where:%2s whereArgs:%3s", uri, where,
2139 Arrays.toString(whereArgs));
2140 throw new IllegalArgumentException(message);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002141 }
2142
Svetoslav28494652015-02-12 14:11:42 -08002143 private static String computeTableForSetting(Uri uri, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08002144 String table = getValidTableOrThrow(uri);
2145
2146 if (name != null) {
2147 if (sSystemMovedToSecureSettings.contains(name)) {
2148 table = TABLE_SECURE;
2149 }
2150
2151 if (sSystemMovedToGlobalSettings.contains(name)) {
2152 table = TABLE_GLOBAL;
2153 }
2154
2155 if (sSecureMovedToGlobalSettings.contains(name)) {
2156 table = TABLE_GLOBAL;
2157 }
2158
2159 if (sGlobalMovedToSecureSettings.contains(name)) {
2160 table = TABLE_SECURE;
2161 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002162 }
Svetoslav683914b2015-01-15 14:22:26 -08002163
2164 return table;
2165 }
2166 }
2167
2168 final class SettingsRegistry {
2169 private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid";
2170
Svetoslav683914b2015-01-15 14:22:26 -08002171 private static final String SETTINGS_FILE_GLOBAL = "settings_global.xml";
2172 private static final String SETTINGS_FILE_SYSTEM = "settings_system.xml";
2173 private static final String SETTINGS_FILE_SECURE = "settings_secure.xml";
Mark Rathjend891f012017-01-19 04:10:37 +00002174 private static final String SETTINGS_FILE_SSAID = "settings_ssaid.xml";
2175
2176 private static final String SSAID_USER_KEY = "userkey";
Svetoslav683914b2015-01-15 14:22:26 -08002177
2178 private final SparseArray<SettingsState> mSettingsStates = new SparseArray<>();
2179
Svet Ganov53a441c2016-04-19 19:38:00 -07002180 private GenerationRegistry mGenerationRegistry;
Svetoslav683914b2015-01-15 14:22:26 -08002181
Svetoslav7e0683b2015-08-03 16:02:52 -07002182 private final Handler mHandler;
2183
Svet Ganov53a441c2016-04-19 19:38:00 -07002184 private final BackupManager mBackupManager;
2185
Amith Yamasani39452022017-03-21 15:23:47 -07002186 private String mSettingsCreationBuildId;
2187
Svetoslav683914b2015-01-15 14:22:26 -08002188 public SettingsRegistry() {
Svetoslav7e0683b2015-08-03 16:02:52 -07002189 mHandler = new MyHandler(getContext().getMainLooper());
Svet Ganov53a441c2016-04-19 19:38:00 -07002190 mGenerationRegistry = new GenerationRegistry(mLock);
2191 mBackupManager = new BackupManager(getContext());
Svetoslav683914b2015-01-15 14:22:26 -08002192 migrateAllLegacySettingsIfNeeded();
Mark Rathjend891f012017-01-19 04:10:37 +00002193 syncSsaidTableOnStart();
2194 }
2195
2196 private void generateUserKeyLocked(int userId) {
2197 // Generate a random key for each user used for creating a new ssaid.
Mark Rathjen7599f132017-01-23 14:15:54 -08002198 final byte[] keyBytes = new byte[32];
Mark Rathjend891f012017-01-19 04:10:37 +00002199 final SecureRandom rand = new SecureRandom();
2200 rand.nextBytes(keyBytes);
2201
2202 // Convert to string for storage in settings table.
Mark Rathjen7599f132017-01-23 14:15:54 -08002203 final String userKey = ByteStringUtils.toHexString(keyBytes);
Mark Rathjend891f012017-01-19 04:10:37 +00002204
2205 // Store the key in the ssaid table.
2206 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
2207 final boolean success = ssaidSettings.insertSettingLocked(SSAID_USER_KEY, userKey, null,
2208 true, SettingsState.SYSTEM_PACKAGE_NAME);
2209
2210 if (!success) {
2211 throw new IllegalStateException("Ssaid settings not accessible");
2212 }
2213 }
2214
Mark Rathjen7599f132017-01-23 14:15:54 -08002215 private byte[] getLengthPrefix(byte[] data) {
2216 return ByteBuffer.allocate(4).putInt(data.length).array();
2217 }
2218
Christopher Tateb218e762017-04-05 16:34:07 -07002219 public Setting generateSsaidLocked(PackageInfo callingPkg, int userId) {
Mark Rathjend891f012017-01-19 04:10:37 +00002220 // Read the user's key from the ssaid table.
2221 Setting userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY);
Mark Rathjenea617592017-01-18 23:03:41 -08002222 if (userKeySetting == null || userKeySetting.isNull()
2223 || userKeySetting.getValue() == null) {
Mark Rathjend891f012017-01-19 04:10:37 +00002224 // Lazy initialize and store the user key.
2225 generateUserKeyLocked(userId);
2226 userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY);
Mark Rathjenea617592017-01-18 23:03:41 -08002227 if (userKeySetting == null || userKeySetting.isNull()
2228 || userKeySetting.getValue() == null) {
Mark Rathjend891f012017-01-19 04:10:37 +00002229 throw new IllegalStateException("User key not accessible");
2230 }
2231 }
2232 final String userKey = userKeySetting.getValue();
2233
2234 // Convert the user's key back to a byte array.
Mark Rathjen7599f132017-01-23 14:15:54 -08002235 final byte[] keyBytes = ByteStringUtils.fromHexToByteArray(userKey);
2236
2237 // Validate that the key is of expected length.
2238 // Keys are currently 32 bytes, but were once 16 bytes during Android O development.
2239 if (keyBytes == null || (keyBytes.length != 16 && keyBytes.length != 32)) {
Mark Rathjend891f012017-01-19 04:10:37 +00002240 throw new IllegalStateException("User key invalid");
2241 }
2242
Mark Rathjen7599f132017-01-23 14:15:54 -08002243 final Mac m;
Mark Rathjend891f012017-01-19 04:10:37 +00002244 try {
Mark Rathjen7599f132017-01-23 14:15:54 -08002245 m = Mac.getInstance("HmacSHA256");
2246 m.init(new SecretKeySpec(keyBytes, m.getAlgorithm()));
Mark Rathjend891f012017-01-19 04:10:37 +00002247 } catch (NoSuchAlgorithmException e) {
Mark Rathjen7599f132017-01-23 14:15:54 -08002248 throw new IllegalStateException("HmacSHA256 is not available", e);
2249 } catch (InvalidKeyException e) {
2250 throw new IllegalStateException("Key is corrupted", e);
Mark Rathjend891f012017-01-19 04:10:37 +00002251 }
Mark Rathjen7599f132017-01-23 14:15:54 -08002252
Mark Rathjenf42dd912017-06-05 19:04:34 -07002253 // Mac each of the developer signatures.
Christopher Tateb218e762017-04-05 16:34:07 -07002254 for (int i = 0; i < callingPkg.signatures.length; i++) {
2255 byte[] sig = callingPkg.signatures[i].toByteArray();
Mark Rathjen7599f132017-01-23 14:15:54 -08002256 m.update(getLengthPrefix(sig), 0, 4);
2257 m.update(sig);
2258 }
Mark Rathjend891f012017-01-19 04:10:37 +00002259
2260 // Convert result to a string for storage in settings table. Only want first 64 bits.
Mark Rathjen7599f132017-01-23 14:15:54 -08002261 final String ssaid = ByteStringUtils.toHexString(m.doFinal()).substring(0, 16)
2262 .toLowerCase(Locale.US);
Mark Rathjend891f012017-01-19 04:10:37 +00002263
2264 // Save the ssaid in the ssaid table.
Christopher Tateb218e762017-04-05 16:34:07 -07002265 final String uid = Integer.toString(callingPkg.applicationInfo.uid);
Mark Rathjend891f012017-01-19 04:10:37 +00002266 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
2267 final boolean success = ssaidSettings.insertSettingLocked(uid, ssaid, null, true,
Mark Rathjenf42dd912017-06-05 19:04:34 -07002268 callingPkg.packageName);
Mark Rathjend891f012017-01-19 04:10:37 +00002269
2270 if (!success) {
2271 throw new IllegalStateException("Ssaid settings not accessible");
2272 }
2273
2274 return getSettingLocked(SETTINGS_TYPE_SSAID, userId, uid);
2275 }
2276
2277 public void syncSsaidTableOnStart() {
2278 synchronized (mLock) {
2279 // Verify that each user's packages and ssaid's are in sync.
2280 for (UserInfo user : mUserManager.getUsers(true)) {
2281 // Get all uids for the user's packages.
2282 final List<PackageInfo> packages;
2283 try {
2284 packages = mPackageManager.getInstalledPackages(0, user.id).getList();
2285 } catch (RemoteException e) {
2286 throw new IllegalStateException("Package manager not available");
2287 }
2288 final Set<String> appUids = new HashSet<>();
2289 for (PackageInfo info : packages) {
2290 appUids.add(Integer.toString(info.applicationInfo.uid));
2291 }
2292
2293 // Get all uids currently stored in the user's ssaid table.
2294 final Set<String> ssaidUids = new HashSet<>(
2295 getSettingsNamesLocked(SETTINGS_TYPE_SSAID, user.id));
2296 ssaidUids.remove(SSAID_USER_KEY);
2297
2298 // Perform a set difference for the appUids and ssaidUids.
2299 ssaidUids.removeAll(appUids);
2300
2301 // If there are ssaidUids left over they need to be removed from the table.
2302 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID,
2303 user.id);
2304 for (String uid : ssaidUids) {
2305 ssaidSettings.deleteSettingLocked(uid);
2306 }
2307 }
2308 }
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002309 }
2310
Svetoslav683914b2015-01-15 14:22:26 -08002311 public List<String> getSettingsNamesLocked(int type, int userId) {
2312 final int key = makeKey(type, userId);
2313 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002314 if (settingsState == null) {
2315 return new ArrayList<String>();
2316 }
Svetoslav683914b2015-01-15 14:22:26 -08002317 return settingsState.getSettingNamesLocked();
2318 }
2319
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002320 public SparseBooleanArray getKnownUsersLocked() {
2321 SparseBooleanArray users = new SparseBooleanArray();
2322 for (int i = mSettingsStates.size()-1; i >= 0; i--) {
2323 users.put(getUserIdFromKey(mSettingsStates.keyAt(i)), true);
2324 }
2325 return users;
2326 }
2327
Kweku Adamsb0886f32017-10-31 15:32:09 -07002328 @Nullable
Svetoslav683914b2015-01-15 14:22:26 -08002329 public SettingsState getSettingsLocked(int type, int userId) {
2330 final int key = makeKey(type, userId);
2331 return peekSettingsStateLocked(key);
2332 }
2333
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002334 public boolean ensureSettingsForUserLocked(int userId) {
2335 // First make sure this user actually exists.
2336 if (mUserManager.getUserInfo(userId) == null) {
2337 Slog.wtf(LOG_TAG, "Requested user " + userId + " does not exist");
2338 return false;
2339 }
2340
Svetoslav683914b2015-01-15 14:22:26 -08002341 // Migrate the setting for this user if needed.
2342 migrateLegacySettingsForUserIfNeededLocked(userId);
2343
2344 // Ensure global settings loaded if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07002345 if (userId == UserHandle.USER_SYSTEM) {
2346 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002347 ensureSettingsStateLocked(globalKey);
2348 }
2349
2350 // Ensure secure settings loaded.
2351 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2352 ensureSettingsStateLocked(secureKey);
2353
2354 // Make sure the secure settings have an Android id set.
2355 SettingsState secureSettings = getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
2356 ensureSecureSettingAndroidIdSetLocked(secureSettings);
2357
2358 // Ensure system settings loaded.
2359 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2360 ensureSettingsStateLocked(systemKey);
2361
Mark Rathjend891f012017-01-19 04:10:37 +00002362 // Ensure secure settings loaded.
2363 final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId);
2364 ensureSettingsStateLocked(ssaidKey);
2365
Svetoslav683914b2015-01-15 14:22:26 -08002366 // Upgrade the settings to the latest version.
2367 UpgradeController upgrader = new UpgradeController(userId);
2368 upgrader.upgradeIfNeededLocked();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002369 return true;
Svetoslav683914b2015-01-15 14:22:26 -08002370 }
2371
2372 private void ensureSettingsStateLocked(int key) {
2373 if (mSettingsStates.get(key) == null) {
2374 final int maxBytesPerPackage = getMaxBytesPerPackageForType(getTypeFromKey(key));
Svetoslav Ganove080da92016-12-21 17:10:35 -08002375 SettingsState settingsState = new SettingsState(getContext(), mLock,
2376 getSettingsFile(key), key, maxBytesPerPackage, mHandlerThread.getLooper());
Svetoslav683914b2015-01-15 14:22:26 -08002377 mSettingsStates.put(key, settingsState);
2378 }
2379 }
2380
2381 public void removeUserStateLocked(int userId, boolean permanently) {
2382 // We always keep the global settings in memory.
2383
2384 // Nuke system settings.
2385 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2386 final SettingsState systemSettingsState = mSettingsStates.get(systemKey);
2387 if (systemSettingsState != null) {
2388 if (permanently) {
2389 mSettingsStates.remove(systemKey);
2390 systemSettingsState.destroyLocked(null);
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002391 } else {
Svetoslav683914b2015-01-15 14:22:26 -08002392 systemSettingsState.destroyLocked(new Runnable() {
2393 @Override
2394 public void run() {
2395 mSettingsStates.remove(systemKey);
2396 }
2397 });
2398 }
2399 }
2400
2401 // Nuke secure settings.
2402 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2403 final SettingsState secureSettingsState = mSettingsStates.get(secureKey);
2404 if (secureSettingsState != null) {
2405 if (permanently) {
2406 mSettingsStates.remove(secureKey);
2407 secureSettingsState.destroyLocked(null);
2408 } else {
2409 secureSettingsState.destroyLocked(new Runnable() {
2410 @Override
2411 public void run() {
2412 mSettingsStates.remove(secureKey);
2413 }
2414 });
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002415 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002416 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002417
Mark Rathjend891f012017-01-19 04:10:37 +00002418 // Nuke ssaid settings.
2419 final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId);
2420 final SettingsState ssaidSettingsState = mSettingsStates.get(ssaidKey);
2421 if (ssaidSettingsState != null) {
2422 if (permanently) {
2423 mSettingsStates.remove(ssaidKey);
2424 ssaidSettingsState.destroyLocked(null);
2425 } else {
2426 ssaidSettingsState.destroyLocked(new Runnable() {
2427 @Override
2428 public void run() {
2429 mSettingsStates.remove(ssaidKey);
2430 }
2431 });
2432 }
2433 }
2434
Svet Ganov53a441c2016-04-19 19:38:00 -07002435 // Nuke generation tracking data
2436 mGenerationRegistry.onUserRemoved(userId);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002437 }
2438
Svetoslav683914b2015-01-15 14:22:26 -08002439 public boolean insertSettingLocked(int type, int userId, String name, String value,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002440 String tag, boolean makeDefault, String packageName, boolean forceNotify,
2441 Set<String> criticalSettings) {
Svetoslav683914b2015-01-15 14:22:26 -08002442 final int key = makeKey(type, userId);
2443
Svetoslav Ganove080da92016-12-21 17:10:35 -08002444 boolean success = false;
Svetoslav683914b2015-01-15 14:22:26 -08002445 SettingsState settingsState = peekSettingsStateLocked(key);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002446 if (settingsState != null) {
2447 success = settingsState.insertSettingLocked(name, value,
2448 tag, makeDefault, packageName);
2449 }
Svetoslav683914b2015-01-15 14:22:26 -08002450
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002451 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2452 settingsState.persistSyncLocked();
2453 }
2454
Svet Ganov53a441c2016-04-19 19:38:00 -07002455 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08002456 notifyForSettingsChange(key, name);
2457 }
2458 return success;
2459 }
2460
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002461 public boolean deleteSettingLocked(int type, int userId, String name, boolean forceNotify,
2462 Set<String> criticalSettings) {
Svetoslav683914b2015-01-15 14:22:26 -08002463 final int key = makeKey(type, userId);
2464
Svetoslav Ganove080da92016-12-21 17:10:35 -08002465 boolean success = false;
Svetoslav683914b2015-01-15 14:22:26 -08002466 SettingsState settingsState = peekSettingsStateLocked(key);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002467 if (settingsState != null) {
2468 success = settingsState.deleteSettingLocked(name);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002469 }
Svetoslav683914b2015-01-15 14:22:26 -08002470
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002471 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2472 settingsState.persistSyncLocked();
2473 }
2474
Svet Ganov53a441c2016-04-19 19:38:00 -07002475 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08002476 notifyForSettingsChange(key, name);
2477 }
2478 return success;
2479 }
2480
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002481 public boolean updateSettingLocked(int type, int userId, String name, String value,
2482 String tag, boolean makeDefault, String packageName, boolean forceNotify,
2483 Set<String> criticalSettings) {
2484 final int key = makeKey(type, userId);
2485
2486 boolean success = false;
2487 SettingsState settingsState = peekSettingsStateLocked(key);
2488 if (settingsState != null) {
2489 success = settingsState.updateSettingLocked(name, value, tag,
2490 makeDefault, packageName);
2491 }
2492
2493 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2494 settingsState.persistSyncLocked();
2495 }
2496
2497 if (forceNotify || success) {
2498 notifyForSettingsChange(key, name);
2499 }
2500
2501 return success;
2502 }
2503
Svetoslav683914b2015-01-15 14:22:26 -08002504 public Setting getSettingLocked(int type, int userId, String name) {
2505 final int key = makeKey(type, userId);
2506
2507 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002508 if (settingsState == null) {
Mark Rathjenea617592017-01-18 23:03:41 -08002509 return null;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002510 }
Mark Rathjend891f012017-01-19 04:10:37 +00002511
2512 // getSettingLocked will return non-null result
Svetoslav683914b2015-01-15 14:22:26 -08002513 return settingsState.getSettingLocked(name);
2514 }
2515
Svetoslav Ganove080da92016-12-21 17:10:35 -08002516 public void resetSettingsLocked(int type, int userId, String packageName, int mode,
2517 String tag) {
2518 final int key = makeKey(type, userId);
2519 SettingsState settingsState = peekSettingsStateLocked(key);
2520 if (settingsState == null) {
2521 return;
2522 }
2523
2524 switch (mode) {
2525 case Settings.RESET_MODE_PACKAGE_DEFAULTS: {
2526 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002527 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002528 Setting setting = settingsState.getSettingLocked(name);
2529 if (packageName.equals(setting.getPackageName())) {
2530 if (tag != null && !tag.equals(setting.getTag())) {
2531 continue;
2532 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002533 if (settingsState.resetSettingLocked(name)) {
2534 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002535 notifyForSettingsChange(key, name);
2536 }
2537 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002538 if (someSettingChanged) {
2539 settingsState.persistSyncLocked();
2540 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002541 }
2542 } break;
2543
2544 case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: {
2545 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002546 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002547 Setting setting = settingsState.getSettingLocked(name);
2548 if (!SettingsState.isSystemPackage(getContext(),
2549 setting.getPackageName())) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002550 if (settingsState.resetSettingLocked(name)) {
2551 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002552 notifyForSettingsChange(key, name);
2553 }
2554 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002555 if (someSettingChanged) {
2556 settingsState.persistSyncLocked();
2557 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002558 }
2559 } break;
2560
2561 case Settings.RESET_MODE_UNTRUSTED_CHANGES: {
2562 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002563 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002564 Setting setting = settingsState.getSettingLocked(name);
2565 if (!SettingsState.isSystemPackage(getContext(),
2566 setting.getPackageName())) {
Eugene Suslad72c3972016-12-27 15:49:30 -08002567 if (setting.isDefaultFromSystem()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002568 if (settingsState.resetSettingLocked(name)) {
2569 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002570 notifyForSettingsChange(key, name);
2571 }
2572 } else if (settingsState.deleteSettingLocked(name)) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002573 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002574 notifyForSettingsChange(key, name);
2575 }
2576 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002577 if (someSettingChanged) {
2578 settingsState.persistSyncLocked();
2579 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002580 }
2581 } break;
2582
2583 case Settings.RESET_MODE_TRUSTED_DEFAULTS: {
2584 for (String name : settingsState.getSettingNamesLocked()) {
2585 Setting setting = settingsState.getSettingLocked(name);
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002586 boolean someSettingChanged = false;
Eugene Suslad72c3972016-12-27 15:49:30 -08002587 if (setting.isDefaultFromSystem()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002588 if (settingsState.resetSettingLocked(name)) {
2589 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002590 notifyForSettingsChange(key, name);
2591 }
2592 } else if (settingsState.deleteSettingLocked(name)) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002593 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002594 notifyForSettingsChange(key, name);
2595 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002596 if (someSettingChanged) {
2597 settingsState.persistSyncLocked();
2598 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002599 }
2600 } break;
2601 }
2602 }
2603
Svetoslav683914b2015-01-15 14:22:26 -08002604 public void onPackageRemovedLocked(String packageName, int userId) {
Svet Ganov8de34802015-04-27 09:33:40 -07002605 // Global and secure settings are signature protected. Apps signed
2606 // by the platform certificate are generally not uninstalled and
2607 // the main exception is tests. We trust components signed
2608 // by the platform certificate and do not do a clean up after them.
Svetoslav683914b2015-01-15 14:22:26 -08002609
2610 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2611 SettingsState systemSettings = mSettingsStates.get(systemKey);
Svet Ganov8de34802015-04-27 09:33:40 -07002612 if (systemSettings != null) {
2613 systemSettings.onPackageRemovedLocked(packageName);
2614 }
Svetoslav683914b2015-01-15 14:22:26 -08002615 }
2616
Mark Rathjend891f012017-01-19 04:10:37 +00002617 public void onUidRemovedLocked(int uid) {
2618 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID,
2619 UserHandle.getUserId(uid));
Tony Mak24c6ab42018-01-08 14:35:34 +00002620 if (ssaidSettings != null) {
2621 ssaidSettings.deleteSettingLocked(Integer.toString(uid));
2622 }
Mark Rathjend891f012017-01-19 04:10:37 +00002623 }
2624
Kweku Adamsb0886f32017-10-31 15:32:09 -07002625 @Nullable
Svetoslav683914b2015-01-15 14:22:26 -08002626 private SettingsState peekSettingsStateLocked(int key) {
2627 SettingsState settingsState = mSettingsStates.get(key);
2628 if (settingsState != null) {
2629 return settingsState;
2630 }
2631
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002632 if (!ensureSettingsForUserLocked(getUserIdFromKey(key))) {
2633 return null;
2634 }
Svetoslav683914b2015-01-15 14:22:26 -08002635 return mSettingsStates.get(key);
2636 }
2637
2638 private void migrateAllLegacySettingsIfNeeded() {
2639 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07002640 final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002641 File globalFile = getSettingsFile(key);
Amith Yamasani74bf71b2017-07-19 16:49:52 -07002642 if (SettingsState.stateFileExists(globalFile)) {
Svetoslav683914b2015-01-15 14:22:26 -08002643 return;
2644 }
2645
Amith Yamasani39452022017-03-21 15:23:47 -07002646 mSettingsCreationBuildId = Build.ID;
2647
Svetoslav683914b2015-01-15 14:22:26 -08002648 final long identity = Binder.clearCallingIdentity();
2649 try {
2650 List<UserInfo> users = mUserManager.getUsers(true);
2651
2652 final int userCount = users.size();
2653 for (int i = 0; i < userCount; i++) {
2654 final int userId = users.get(i).id;
2655
2656 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
2657 SQLiteDatabase database = dbHelper.getWritableDatabase();
2658 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
2659
2660 // Upgrade to the latest version.
2661 UpgradeController upgrader = new UpgradeController(userId);
2662 upgrader.upgradeIfNeededLocked();
2663
2664 // Drop from memory if not a running user.
2665 if (!mUserManager.isUserRunning(new UserHandle(userId))) {
2666 removeUserStateLocked(userId, false);
2667 }
2668 }
2669 } finally {
2670 Binder.restoreCallingIdentity(identity);
2671 }
2672 }
2673 }
2674
2675 private void migrateLegacySettingsForUserIfNeededLocked(int userId) {
2676 // Every user has secure settings and if no file we need to migrate.
2677 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2678 File secureFile = getSettingsFile(secureKey);
Amith Yamasani74bf71b2017-07-19 16:49:52 -07002679 if (SettingsState.stateFileExists(secureFile)) {
Svetoslav683914b2015-01-15 14:22:26 -08002680 return;
2681 }
2682
2683 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
2684 SQLiteDatabase database = dbHelper.getWritableDatabase();
2685
2686 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
2687 }
2688
2689 private void migrateLegacySettingsForUserLocked(DatabaseHelper dbHelper,
2690 SQLiteDatabase database, int userId) {
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002691 // Move over the system settings.
2692 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2693 ensureSettingsStateLocked(systemKey);
2694 SettingsState systemSettings = mSettingsStates.get(systemKey);
2695 migrateLegacySettingsLocked(systemSettings, database, TABLE_SYSTEM);
2696 systemSettings.persistSyncLocked();
Svetoslav683914b2015-01-15 14:22:26 -08002697
2698 // Move over the secure settings.
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002699 // Do this after System settings, since this is the first thing we check when deciding
2700 // to skip over migration from db to xml for a secondary user.
Svetoslav683914b2015-01-15 14:22:26 -08002701 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2702 ensureSettingsStateLocked(secureKey);
2703 SettingsState secureSettings = mSettingsStates.get(secureKey);
2704 migrateLegacySettingsLocked(secureSettings, database, TABLE_SECURE);
2705 ensureSecureSettingAndroidIdSetLocked(secureSettings);
2706 secureSettings.persistSyncLocked();
2707
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002708 // Move over the global settings if owner.
2709 // Do this last, since this is the first thing we check when deciding
2710 // to skip over migration from db to xml for owner user.
2711 if (userId == UserHandle.USER_SYSTEM) {
2712 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, userId);
2713 ensureSettingsStateLocked(globalKey);
2714 SettingsState globalSettings = mSettingsStates.get(globalKey);
2715 migrateLegacySettingsLocked(globalSettings, database, TABLE_GLOBAL);
Amith Yamasani39452022017-03-21 15:23:47 -07002716 // If this was just created
2717 if (mSettingsCreationBuildId != null) {
2718 globalSettings.insertSettingLocked(Settings.Global.DATABASE_CREATION_BUILDID,
2719 mSettingsCreationBuildId, null, true,
2720 SettingsState.SYSTEM_PACKAGE_NAME);
2721 }
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002722 globalSettings.persistSyncLocked();
2723 }
Svetoslav683914b2015-01-15 14:22:26 -08002724
2725 // Drop the database as now all is moved and persisted.
2726 if (DROP_DATABASE_ON_MIGRATION) {
2727 dbHelper.dropDatabase();
2728 } else {
2729 dbHelper.backupDatabase();
2730 }
2731 }
2732
2733 private void migrateLegacySettingsLocked(SettingsState settingsState,
2734 SQLiteDatabase database, String table) {
2735 SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
2736 queryBuilder.setTables(table);
2737
2738 Cursor cursor = queryBuilder.query(database, ALL_COLUMNS,
2739 null, null, null, null, null);
2740
2741 if (cursor == null) {
2742 return;
2743 }
2744
2745 try {
2746 if (!cursor.moveToFirst()) {
2747 return;
2748 }
2749
2750 final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME);
2751 final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE);
2752
2753 settingsState.setVersionLocked(database.getVersion());
2754
2755 while (!cursor.isAfterLast()) {
2756 String name = cursor.getString(nameColumnIdx);
2757 String value = cursor.getString(valueColumnIdx);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002758 settingsState.insertSettingLocked(name, value, null, true,
Svetoslav683914b2015-01-15 14:22:26 -08002759 SettingsState.SYSTEM_PACKAGE_NAME);
2760 cursor.moveToNext();
2761 }
2762 } finally {
2763 cursor.close();
2764 }
2765 }
2766
2767 private void ensureSecureSettingAndroidIdSetLocked(SettingsState secureSettings) {
2768 Setting value = secureSettings.getSettingLocked(Settings.Secure.ANDROID_ID);
2769
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09002770 if (!value.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08002771 return;
2772 }
2773
2774 final int userId = getUserIdFromKey(secureSettings.mKey);
2775
2776 final UserInfo user;
2777 final long identity = Binder.clearCallingIdentity();
2778 try {
2779 user = mUserManager.getUserInfo(userId);
2780 } finally {
2781 Binder.restoreCallingIdentity(identity);
2782 }
2783 if (user == null) {
2784 // Can happen due to races when deleting users - treat as benign.
2785 return;
2786 }
2787
2788 String androidId = Long.toHexString(new SecureRandom().nextLong());
2789 secureSettings.insertSettingLocked(Settings.Secure.ANDROID_ID, androidId,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002790 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Svetoslav683914b2015-01-15 14:22:26 -08002791
2792 Slog.d(LOG_TAG, "Generated and saved new ANDROID_ID [" + androidId
2793 + "] for user " + userId);
2794
2795 // Write a drop box entry if it's a restricted profile
2796 if (user.isRestricted()) {
2797 DropBoxManager dbm = (DropBoxManager) getContext().getSystemService(
2798 Context.DROPBOX_SERVICE);
2799 if (dbm != null && dbm.isTagEnabled(DROPBOX_TAG_USERLOG)) {
2800 dbm.addText(DROPBOX_TAG_USERLOG, System.currentTimeMillis()
2801 + "," + DROPBOX_TAG_USERLOG + "," + androidId + "\n");
2802 }
2803 }
2804 }
2805
2806 private void notifyForSettingsChange(int key, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08002807 final int userId = getUserIdFromKey(key);
2808 Uri uri = getNotificationUriFor(key, name);
2809
Phil Weaver83fec002016-05-11 10:55:29 -07002810 mGenerationRegistry.incrementGeneration(key);
2811
Svetoslav7e0683b2015-08-03 16:02:52 -07002812 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
2813 userId, 0, uri).sendToTarget();
2814
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002815 if (isSecureSettingsKey(key)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07002816 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
2817 sSecureCloneToManagedSettings);
Robin Lee7af9a742017-02-20 14:47:30 +00002818 maybeNotifyProfiles(SETTINGS_TYPE_SYSTEM, userId, uri, name,
2819 sSystemCloneFromParentOnDependency.values());
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002820 } else if (isSystemSettingsKey(key)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07002821 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
2822 sSystemCloneToManagedSettings);
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002823 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002824
Svet Ganov53a441c2016-04-19 19:38:00 -07002825 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
Makoto Onuki0000d322017-11-28 16:31:47 -08002826
2827 // When the global kill switch is updated, send the change notification for
2828 // the location setting.
2829 if (isGlobalSettingsKey(key) && Global.LOCATION_GLOBAL_KILL_SWITCH.equals(name)) {
2830 notifyLocationChangeForRunningUsers();
2831 }
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002832 }
2833
Svet Ganov53a441c2016-04-19 19:38:00 -07002834 private void maybeNotifyProfiles(int type, int userId, Uri uri, String name,
Robin Lee7af9a742017-02-20 14:47:30 +00002835 Collection<String> keysCloned) {
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002836 if (keysCloned.contains(name)) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002837 for (int profileId : mUserManager.getProfileIdsWithDisabled(userId)) {
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002838 // the notification for userId has already been sent.
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002839 if (profileId != userId) {
Svetoslav7e0683b2015-08-03 16:02:52 -07002840 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07002841 profileId, 0, uri).sendToTarget();
Svet Ganov53a441c2016-04-19 19:38:00 -07002842 final int key = makeKey(type, profileId);
2843 mGenerationRegistry.incrementGeneration(key);
2844
2845 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01002846 }
2847 }
2848 }
Svetoslav683914b2015-01-15 14:22:26 -08002849 }
2850
Makoto Onuki0000d322017-11-28 16:31:47 -08002851 private void notifyLocationChangeForRunningUsers() {
2852 final List<UserInfo> users = mUserManager.getUsers(/*excludeDying=*/ true);
2853
2854 for (int i = 0; i < users.size(); i++) {
2855 final int userId = users.get(i).id;
2856
2857 if (!mUserManager.isUserRunning(UserHandle.of(userId))) {
2858 continue;
2859 }
2860
2861 final int key = makeKey(SETTINGS_TYPE_GLOBAL, userId);
2862 final Uri uri = getNotificationUriFor(key, Secure.LOCATION_PROVIDERS_ALLOWED);
2863
2864 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
2865 userId, 0, uri).sendToTarget();
2866 }
2867 }
2868
Svetoslav683914b2015-01-15 14:22:26 -08002869 private boolean isGlobalSettingsKey(int key) {
2870 return getTypeFromKey(key) == SETTINGS_TYPE_GLOBAL;
2871 }
2872
2873 private boolean isSystemSettingsKey(int key) {
2874 return getTypeFromKey(key) == SETTINGS_TYPE_SYSTEM;
2875 }
2876
2877 private boolean isSecureSettingsKey(int key) {
2878 return getTypeFromKey(key) == SETTINGS_TYPE_SECURE;
2879 }
2880
Mark Rathjend891f012017-01-19 04:10:37 +00002881 private boolean isSsaidSettingsKey(int key) {
2882 return getTypeFromKey(key) == SETTINGS_TYPE_SSAID;
2883 }
2884
Svetoslav683914b2015-01-15 14:22:26 -08002885 private File getSettingsFile(int key) {
2886 if (isGlobalSettingsKey(key)) {
2887 final int userId = getUserIdFromKey(key);
2888 return new File(Environment.getUserSystemDirectory(userId),
2889 SETTINGS_FILE_GLOBAL);
2890 } else if (isSystemSettingsKey(key)) {
2891 final int userId = getUserIdFromKey(key);
2892 return new File(Environment.getUserSystemDirectory(userId),
2893 SETTINGS_FILE_SYSTEM);
2894 } else if (isSecureSettingsKey(key)) {
2895 final int userId = getUserIdFromKey(key);
2896 return new File(Environment.getUserSystemDirectory(userId),
2897 SETTINGS_FILE_SECURE);
Mark Rathjend891f012017-01-19 04:10:37 +00002898 } else if (isSsaidSettingsKey(key)) {
2899 final int userId = getUserIdFromKey(key);
2900 return new File(Environment.getUserSystemDirectory(userId),
2901 SETTINGS_FILE_SSAID);
Svetoslav683914b2015-01-15 14:22:26 -08002902 } else {
2903 throw new IllegalArgumentException("Invalid settings key:" + key);
2904 }
2905 }
2906
2907 private Uri getNotificationUriFor(int key, String name) {
2908 if (isGlobalSettingsKey(key)) {
2909 return (name != null) ? Uri.withAppendedPath(Settings.Global.CONTENT_URI, name)
2910 : Settings.Global.CONTENT_URI;
2911 } else if (isSecureSettingsKey(key)) {
2912 return (name != null) ? Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name)
2913 : Settings.Secure.CONTENT_URI;
2914 } else if (isSystemSettingsKey(key)) {
2915 return (name != null) ? Uri.withAppendedPath(Settings.System.CONTENT_URI, name)
2916 : Settings.System.CONTENT_URI;
2917 } else {
2918 throw new IllegalArgumentException("Invalid settings key:" + key);
2919 }
2920 }
2921
2922 private int getMaxBytesPerPackageForType(int type) {
2923 switch (type) {
2924 case SETTINGS_TYPE_GLOBAL:
Mark Rathjend891f012017-01-19 04:10:37 +00002925 case SETTINGS_TYPE_SECURE:
2926 case SETTINGS_TYPE_SSAID: {
Svetoslav683914b2015-01-15 14:22:26 -08002927 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED;
2928 }
2929
2930 default: {
2931 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_LIMITED;
2932 }
2933 }
2934 }
2935
Svetoslav7e0683b2015-08-03 16:02:52 -07002936 private final class MyHandler extends Handler {
2937 private static final int MSG_NOTIFY_URI_CHANGED = 1;
2938 private static final int MSG_NOTIFY_DATA_CHANGED = 2;
2939
2940 public MyHandler(Looper looper) {
2941 super(looper);
2942 }
2943
2944 @Override
2945 public void handleMessage(Message msg) {
2946 switch (msg.what) {
2947 case MSG_NOTIFY_URI_CHANGED: {
2948 final int userId = msg.arg1;
2949 Uri uri = (Uri) msg.obj;
Jeff Sharkey88f9d0b2017-08-11 15:29:40 -06002950 try {
2951 getContext().getContentResolver().notifyChange(uri, null, true, userId);
2952 } catch (SecurityException e) {
2953 Slog.w(LOG_TAG, "Failed to notify for " + userId + ": " + uri, e);
2954 }
Makoto Onuki0000d322017-11-28 16:31:47 -08002955 if (DEBUG || true) {
Svetoslav7e0683b2015-08-03 16:02:52 -07002956 Slog.v(LOG_TAG, "Notifying for " + userId + ": " + uri);
2957 }
2958 } break;
2959
2960 case MSG_NOTIFY_DATA_CHANGED: {
2961 mBackupManager.dataChanged();
2962 } break;
2963 }
2964 }
2965 }
2966
Svetoslav683914b2015-01-15 14:22:26 -08002967 private final class UpgradeController {
Mike Digman4af4a6f2018-01-16 14:49:38 -08002968 private static final int SETTINGS_VERSION = 151;
Svetoslav683914b2015-01-15 14:22:26 -08002969
2970 private final int mUserId;
2971
2972 public UpgradeController(int userId) {
2973 mUserId = userId;
2974 }
2975
2976 public void upgradeIfNeededLocked() {
2977 // The version of all settings for a user is the same (all users have secure).
2978 SettingsState secureSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07002979 SETTINGS_TYPE_SECURE, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08002980
2981 // Try an update from the current state.
2982 final int oldVersion = secureSettings.getVersionLocked();
2983 final int newVersion = SETTINGS_VERSION;
2984
Svet Ganovc9755bc2015-03-28 13:21:22 -07002985 // If up do date - done.
Svetoslav683914b2015-01-15 14:22:26 -08002986 if (oldVersion == newVersion) {
2987 return;
2988 }
2989
2990 // Try to upgrade.
2991 final int curVersion = onUpgradeLocked(mUserId, oldVersion, newVersion);
2992
2993 // If upgrade failed start from scratch and upgrade.
2994 if (curVersion != newVersion) {
2995 // Drop state we have for this user.
2996 removeUserStateLocked(mUserId, true);
2997
2998 // Recreate the database.
2999 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), mUserId);
3000 SQLiteDatabase database = dbHelper.getWritableDatabase();
3001 dbHelper.recreateDatabase(database, newVersion, curVersion, oldVersion);
3002
3003 // Migrate the settings for this user.
3004 migrateLegacySettingsForUserLocked(dbHelper, database, mUserId);
3005
3006 // Now upgrade should work fine.
3007 onUpgradeLocked(mUserId, oldVersion, newVersion);
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07003008
3009 // Make a note what happened, so we don't wonder why data was lost
3010 String reason = "Settings rebuilt! Current version: "
3011 + curVersion + " while expected: " + newVersion;
3012 getGlobalSettingsLocked().insertSettingLocked(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003013 Settings.Global.DATABASE_DOWNGRADE_REASON,
3014 reason, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Svetoslav683914b2015-01-15 14:22:26 -08003015 }
3016
3017 // Set the global settings version if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07003018 if (mUserId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08003019 SettingsState globalSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07003020 SETTINGS_TYPE_GLOBAL, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08003021 globalSettings.setVersionLocked(newVersion);
3022 }
3023
3024 // Set the secure settings version.
3025 secureSettings.setVersionLocked(newVersion);
3026
3027 // Set the system settings version.
3028 SettingsState systemSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07003029 SETTINGS_TYPE_SYSTEM, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08003030 systemSettings.setVersionLocked(newVersion);
3031 }
3032
3033 private SettingsState getGlobalSettingsLocked() {
Xiaohui Chen43765b72015-08-31 10:57:33 -07003034 return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08003035 }
3036
3037 private SettingsState getSecureSettingsLocked(int userId) {
3038 return getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
3039 }
3040
Mark Rathjend891f012017-01-19 04:10:37 +00003041 private SettingsState getSsaidSettingsLocked(int userId) {
3042 return getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
3043 }
3044
Svetoslav683914b2015-01-15 14:22:26 -08003045 private SettingsState getSystemSettingsLocked(int userId) {
3046 return getSettingsLocked(SETTINGS_TYPE_SYSTEM, userId);
3047 }
3048
Jeff Brown503cffc2015-03-26 18:08:51 -07003049 /**
3050 * You must perform all necessary mutations to bring the settings
3051 * for this user from the old to the new version. When you add a new
3052 * upgrade step you *must* update SETTINGS_VERSION.
3053 *
3054 * This is an example of moving a setting from secure to global.
3055 *
3056 * // v119: Example settings changes.
3057 * if (currentVersion == 118) {
3058 * if (userId == UserHandle.USER_OWNER) {
3059 * // Remove from the secure settings.
3060 * SettingsState secureSettings = getSecureSettingsLocked(userId);
3061 * String name = "example_setting_to_move";
3062 * String value = secureSettings.getSetting(name);
3063 * secureSettings.deleteSetting(name);
3064 *
3065 * // Add to the global settings.
3066 * SettingsState globalSettings = getGlobalSettingsLocked();
3067 * globalSettings.insertSetting(name, value, SettingsState.SYSTEM_PACKAGE_NAME);
3068 * }
3069 *
3070 * // Update the current version.
3071 * currentVersion = 119;
3072 * }
3073 */
Svetoslav683914b2015-01-15 14:22:26 -08003074 private int onUpgradeLocked(int userId, int oldVersion, int newVersion) {
3075 if (DEBUG) {
3076 Slog.w(LOG_TAG, "Upgrading settings for user: " + userId + " from version: "
3077 + oldVersion + " to version: " + newVersion);
3078 }
3079
Jeff Brown503cffc2015-03-26 18:08:51 -07003080 int currentVersion = oldVersion;
Svetoslav683914b2015-01-15 14:22:26 -08003081
John Spurlocke11ae112015-05-11 16:09:03 -04003082 // v119: Reset zen + ringer mode.
3083 if (currentVersion == 118) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07003084 if (userId == UserHandle.USER_SYSTEM) {
John Spurlocke11ae112015-05-11 16:09:03 -04003085 final SettingsState globalSettings = getGlobalSettingsLocked();
3086 globalSettings.updateSettingLocked(Settings.Global.ZEN_MODE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003087 Integer.toString(Settings.Global.ZEN_MODE_OFF), null,
3088 true, SettingsState.SYSTEM_PACKAGE_NAME);
John Spurlocke11ae112015-05-11 16:09:03 -04003089 globalSettings.updateSettingLocked(Settings.Global.MODE_RINGER,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003090 Integer.toString(AudioManager.RINGER_MODE_NORMAL), null,
3091 true, SettingsState.SYSTEM_PACKAGE_NAME);
John Spurlocke11ae112015-05-11 16:09:03 -04003092 }
3093 currentVersion = 119;
3094 }
3095
Jason Monk27bbb2d2015-03-31 16:46:39 -04003096 // v120: Add double tap to wake setting.
3097 if (currentVersion == 119) {
3098 SettingsState secureSettings = getSecureSettingsLocked(userId);
3099 secureSettings.insertSettingLocked(Settings.Secure.DOUBLE_TAP_TO_WAKE,
3100 getContext().getResources().getBoolean(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003101 R.bool.def_double_tap_to_wake) ? "1" : "0", null, true,
Jason Monk27bbb2d2015-03-31 16:46:39 -04003102 SettingsState.SYSTEM_PACKAGE_NAME);
3103
3104 currentVersion = 120;
3105 }
3106
Svetoslav7e0683b2015-08-03 16:02:52 -07003107 if (currentVersion == 120) {
3108 // Before 121, we used a different string encoding logic. We just bump the
3109 // version here; SettingsState knows how to handle pre-version 120 files.
3110 currentVersion = 121;
3111 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -07003112
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003113 if (currentVersion == 121) {
3114 // Version 122: allow OEMs to set a default payment component in resources.
3115 // Note that we only write the default if no default has been set;
3116 // if there is, we just leave the default at whatever it currently is.
3117 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3118 String defaultComponent = (getContext().getResources().getString(
3119 R.string.def_nfc_payment_component));
3120 Setting currentSetting = secureSettings.getSettingLocked(
3121 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT);
3122 if (defaultComponent != null && !defaultComponent.isEmpty() &&
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003123 currentSetting.isNull()) {
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003124 secureSettings.insertSettingLocked(
3125 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003126 defaultComponent, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003127 }
3128 currentVersion = 122;
3129 }
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003130
3131 if (currentVersion == 122) {
3132 // Version 123: Adding a default value for the ability to add a user from
3133 // the lock screen.
3134 if (userId == UserHandle.USER_SYSTEM) {
3135 final SettingsState globalSettings = getGlobalSettingsLocked();
3136 Setting currentSetting = globalSettings.getSettingLocked(
3137 Settings.Global.ADD_USERS_WHEN_LOCKED);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003138 if (currentSetting.isNull()) {
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003139 globalSettings.insertSettingLocked(
3140 Settings.Global.ADD_USERS_WHEN_LOCKED,
3141 getContext().getResources().getBoolean(
3142 R.bool.def_add_users_from_lockscreen) ? "1" : "0",
Svetoslav Ganove080da92016-12-21 17:10:35 -08003143 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003144 }
3145 }
3146 currentVersion = 123;
3147 }
Bryce Leebd179282015-12-17 19:01:37 -08003148
3149 if (currentVersion == 123) {
Bryce Leeec85f342015-12-16 13:32:28 -08003150 final SettingsState globalSettings = getGlobalSettingsLocked();
3151 String defaultDisabledProfiles = (getContext().getResources().getString(
3152 R.string.def_bluetooth_disabled_profiles));
3153 globalSettings.insertSettingLocked(Settings.Global.BLUETOOTH_DISABLED_PROFILES,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003154 defaultDisabledProfiles, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Bryce Leebd179282015-12-17 19:01:37 -08003155 currentVersion = 124;
Bryce Leeec85f342015-12-16 13:32:28 -08003156 }
3157
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003158 if (currentVersion == 124) {
3159 // Version 124: allow OEMs to set a default value for whether IME should be
3160 // shown when a physical keyboard is connected.
3161 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3162 Setting currentSetting = secureSettings.getSettingLocked(
3163 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003164 if (currentSetting.isNull()) {
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003165 secureSettings.insertSettingLocked(
3166 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD,
3167 getContext().getResources().getBoolean(
3168 R.bool.def_show_ime_with_hard_keyboard) ? "1" : "0",
Svetoslav Ganove080da92016-12-21 17:10:35 -08003169 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003170 }
3171 currentVersion = 125;
3172 }
3173
Ruben Brunk98576cf2016-03-07 18:54:28 -08003174 if (currentVersion == 125) {
3175 // Version 125: Allow OEMs to set the default VR service.
3176 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3177
3178 Setting currentSetting = secureSettings.getSettingLocked(
3179 Settings.Secure.ENABLED_VR_LISTENERS);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003180 if (currentSetting.isNull()) {
Ruben Brunk98576cf2016-03-07 18:54:28 -08003181 ArraySet<ComponentName> l =
3182 SystemConfig.getInstance().getDefaultVrComponents();
3183
3184 if (l != null && !l.isEmpty()) {
3185 StringBuilder b = new StringBuilder();
3186 boolean start = true;
3187 for (ComponentName c : l) {
3188 if (!start) {
3189 b.append(':');
3190 }
3191 b.append(c.flattenToString());
3192 start = false;
3193 }
3194 secureSettings.insertSettingLocked(
3195 Settings.Secure.ENABLED_VR_LISTENERS, b.toString(),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003196 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Ruben Brunk98576cf2016-03-07 18:54:28 -08003197 }
3198
3199 }
3200 currentVersion = 126;
3201 }
3202
Daniel U02ba6122016-04-01 18:41:42 +01003203 if (currentVersion == 126) {
3204 // Version 126: copy the primary values of LOCK_SCREEN_SHOW_NOTIFICATIONS and
3205 // LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS into managed profile.
3206 if (mUserManager.isManagedProfile(userId)) {
3207 final SettingsState systemSecureSettings =
3208 getSecureSettingsLocked(UserHandle.USER_SYSTEM);
3209
3210 final Setting showNotifications = systemSecureSettings.getSettingLocked(
3211 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09003212 if (!showNotifications.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01003213 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3214 secureSettings.insertSettingLocked(
3215 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003216 showNotifications.getValue(), null, true,
Daniel U02ba6122016-04-01 18:41:42 +01003217 SettingsState.SYSTEM_PACKAGE_NAME);
3218 }
3219
3220 final Setting allowPrivate = systemSecureSettings.getSettingLocked(
3221 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09003222 if (!allowPrivate.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01003223 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3224 secureSettings.insertSettingLocked(
3225 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003226 allowPrivate.getValue(), null, true,
Daniel U02ba6122016-04-01 18:41:42 +01003227 SettingsState.SYSTEM_PACKAGE_NAME);
3228 }
3229 }
3230 currentVersion = 127;
3231 }
3232
Steven Ngdc20ba62016-04-26 18:19:04 +01003233 if (currentVersion == 127) {
Mahaver Chopra3d9805d2016-07-07 16:25:05 +01003234 // version 127 is no longer used.
Steven Ngdc20ba62016-04-26 18:19:04 +01003235 currentVersion = 128;
3236 }
3237
Julia Reynolds1f721e12016-07-11 08:50:58 -04003238 if (currentVersion == 128) {
Julia Reynoldsb852e562017-06-06 16:14:18 -04003239 // Version 128: Removed
Julia Reynolds1f721e12016-07-11 08:50:58 -04003240 currentVersion = 129;
3241 }
3242
Dan Sandler71f85e92016-07-20 13:46:05 -04003243 if (currentVersion == 129) {
3244 // default longpress timeout changed from 500 to 400. If unchanged from the old
3245 // default, update to the new default.
3246 final SettingsState systemSecureSettings =
3247 getSecureSettingsLocked(userId);
3248 final String oldValue = systemSecureSettings.getSettingLocked(
3249 Settings.Secure.LONG_PRESS_TIMEOUT).getValue();
3250 if (TextUtils.equals("500", oldValue)) {
3251 systemSecureSettings.insertSettingLocked(
3252 Settings.Secure.LONG_PRESS_TIMEOUT,
3253 String.valueOf(getContext().getResources().getInteger(
3254 R.integer.def_long_press_timeout_millis)),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003255 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Dan Sandler71f85e92016-07-20 13:46:05 -04003256 }
3257 currentVersion = 130;
3258 }
3259
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003260 if (currentVersion == 130) {
Adrian Roos69741a22016-10-21 14:49:17 -07003261 // Split Ambient settings
3262 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3263 boolean dozeExplicitlyDisabled = "0".equals(secureSettings.
3264 getSettingLocked(Settings.Secure.DOZE_ENABLED).getValue());
3265
3266 if (dozeExplicitlyDisabled) {
3267 secureSettings.insertSettingLocked(Settings.Secure.DOZE_PULSE_ON_PICK_UP,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003268 "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Adrian Roos69741a22016-10-21 14:49:17 -07003269 secureSettings.insertSettingLocked(Settings.Secure.DOZE_PULSE_ON_DOUBLE_TAP,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003270 "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Adrian Roos69741a22016-10-21 14:49:17 -07003271 }
3272 currentVersion = 131;
3273 }
3274
3275 if (currentVersion == 131) {
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003276 // Initialize new multi-press timeout to default value
3277 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3278 final String oldValue = systemSecureSettings.getSettingLocked(
3279 Settings.Secure.MULTI_PRESS_TIMEOUT).getValue();
3280 if (TextUtils.equals(null, oldValue)) {
3281 systemSecureSettings.insertSettingLocked(
3282 Settings.Secure.MULTI_PRESS_TIMEOUT,
3283 String.valueOf(getContext().getResources().getInteger(
3284 R.integer.def_multi_press_timeout_millis)),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003285 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003286 }
3287
Adrian Roos69741a22016-10-21 14:49:17 -07003288 currentVersion = 132;
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003289 }
3290
Adrian Roos69741a22016-10-21 14:49:17 -07003291 if (currentVersion == 132) {
3292 // Version 132: Allow managed profile to optionally use the parent's ringtones
Andre Lagoea35e072016-08-04 13:41:13 +01003293 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3294 String defaultSyncParentSounds = (getContext().getResources()
3295 .getBoolean(R.bool.def_sync_parent_sounds) ? "1" : "0");
3296 systemSecureSettings.insertSettingLocked(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003297 Settings.Secure.SYNC_PARENT_SOUNDS, defaultSyncParentSounds,
3298 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Adrian Roos69741a22016-10-21 14:49:17 -07003299 currentVersion = 133;
Andre Lagoea35e072016-08-04 13:41:13 +01003300 }
3301
Adrian Roos69741a22016-10-21 14:49:17 -07003302 if (currentVersion == 133) {
3303 // Version 133: Add default end button behavior
Keun-young Parkec7a1182016-10-18 11:52:38 -07003304 final SettingsState systemSettings = getSystemSettingsLocked(userId);
3305 if (systemSettings.getSettingLocked(Settings.System.END_BUTTON_BEHAVIOR) ==
3306 null) {
3307 String defaultEndButtonBehavior = Integer.toString(getContext()
3308 .getResources().getInteger(R.integer.def_end_button_behavior));
3309 systemSettings.insertSettingLocked(Settings.System.END_BUTTON_BEHAVIOR,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003310 defaultEndButtonBehavior, null, true,
3311 SettingsState.SYSTEM_PACKAGE_NAME);
Keun-young Parkec7a1182016-10-18 11:52:38 -07003312 }
Adrian Roos69741a22016-10-21 14:49:17 -07003313 currentVersion = 134;
Keun-young Parkec7a1182016-10-18 11:52:38 -07003314 }
3315
Phil Weaver89e3ffc2016-09-19 13:51:10 -07003316 if (currentVersion == 134) {
3317 // Remove setting that specifies if magnification values should be preserved.
3318 // This setting defaulted to true and never has a UI.
3319 getSecureSettingsLocked(userId).deleteSettingLocked(
3320 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_AUTO_UPDATE);
3321 currentVersion = 135;
3322 }
3323
Jeremy Joslin8bdad342016-12-14 11:46:47 -08003324 if (currentVersion == 135) {
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003325 // Version 135 no longer used.
Jeremy Joslin8bdad342016-12-14 11:46:47 -08003326 currentVersion = 136;
3327 }
3328
Mark Rathjend891f012017-01-19 04:10:37 +00003329 if (currentVersion == 136) {
3330 // Version 136: Store legacy SSAID for all apps currently installed on the
3331 // device as first step in migrating SSAID to be unique per application.
3332
3333 final boolean isUpgrade;
3334 try {
3335 isUpgrade = mPackageManager.isUpgrade();
3336 } catch (RemoteException e) {
3337 throw new IllegalStateException("Package manager not available");
3338 }
3339 // Only retain legacy ssaid if the device is performing an OTA. After wiping
3340 // user data or first boot on a new device should use new ssaid generation.
3341 if (isUpgrade) {
3342 // Retrieve the legacy ssaid from the secure settings table.
Mark Rathjenea617592017-01-18 23:03:41 -08003343 final Setting legacySsaidSetting = getSettingLocked(SETTINGS_TYPE_SECURE,
3344 userId, Settings.Secure.ANDROID_ID);
3345 if (legacySsaidSetting == null || legacySsaidSetting.isNull()
3346 || legacySsaidSetting.getValue() == null) {
3347 throw new IllegalStateException("Legacy ssaid not accessible");
3348 }
3349 final String legacySsaid = legacySsaidSetting.getValue();
Mark Rathjend891f012017-01-19 04:10:37 +00003350
3351 // Fill each uid with the legacy ssaid to be backwards compatible.
3352 final List<PackageInfo> packages;
3353 try {
3354 packages = mPackageManager.getInstalledPackages(0, userId).getList();
3355 } catch (RemoteException e) {
3356 throw new IllegalStateException("Package manager not available");
3357 }
3358
3359 final SettingsState ssaidSettings = getSsaidSettingsLocked(userId);
3360 for (PackageInfo info : packages) {
3361 // Check if the UID already has an entry in the table.
3362 final String uid = Integer.toString(info.applicationInfo.uid);
3363 final Setting ssaid = ssaidSettings.getSettingLocked(uid);
3364
3365 if (ssaid.isNull() || ssaid.getValue() == null) {
3366 // Android Id doesn't exist for this package so create it.
3367 ssaidSettings.insertSettingLocked(uid, legacySsaid, null, true,
3368 info.packageName);
3369 }
3370 }
3371 }
3372
3373 currentVersion = 137;
3374 }
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003375 if (currentVersion == 137) {
3376 // Version 138: Settings.Secure#INSTALL_NON_MARKET_APPS is deprecated and its
3377 // default value set to 1. The user can no longer change the value of this
3378 // setting through the UI.
3379 final SettingsState secureSetting = getSecureSettingsLocked(userId);
3380 if (!mUserManager.hasUserRestriction(
Suprabh Shukla0b1356f2017-02-21 14:33:50 -08003381 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, UserHandle.of(userId))
3382 && secureSetting.getSettingLocked(
3383 Settings.Secure.INSTALL_NON_MARKET_APPS).getValue().equals("0")) {
3384
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003385 secureSetting.insertSettingLocked(Settings.Secure.INSTALL_NON_MARKET_APPS,
3386 "1", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shukla0b1356f2017-02-21 14:33:50 -08003387 // For managed profiles with profile owners, DevicePolicyManagerService
3388 // may want to set the user restriction in this case
3389 secureSetting.insertSettingLocked(
3390 Settings.Secure.UNKNOWN_SOURCES_DEFAULT_REVERSED, "1", null, true,
3391 SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003392 }
3393 currentVersion = 138;
3394 }
Mark Rathjend891f012017-01-19 04:10:37 +00003395
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003396 if (currentVersion == 138) {
Jeremy Joslin27d14c42017-02-15 12:02:03 -08003397 // Version 139: Removed.
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003398 currentVersion = 139;
3399 }
3400
Phil Weaver385912e2017-02-10 10:06:56 -08003401 if (currentVersion == 139) {
3402 // Version 140: Settings.Secure#ACCESSIBILITY_SPEAK_PASSWORD is deprecated and
3403 // the user can no longer change the value of this setting through the UI.
3404 // Force to true.
3405 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3406 secureSettings.updateSettingLocked(Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD,
3407 "1", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3408 currentVersion = 140;
3409 }
3410
Julia Reynoldsad0d9e02017-02-15 08:41:48 -05003411 if (currentVersion == 140) {
Julia Reynoldsb852e562017-06-06 16:14:18 -04003412 // Version 141: Removed
Julia Reynoldsad0d9e02017-02-15 08:41:48 -05003413 currentVersion = 141;
3414 }
3415
Svet Ganov13701552017-02-23 12:45:17 -08003416 if (currentVersion == 141) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003417 // This implementation was incorrectly setting the current value of
3418 // settings changed by non-system packages as the default which default
3419 // is set by the system. We add a new upgrade step at the end to properly
3420 // handle this case which would also fix incorrect changes made by the
3421 // old implementation of this step.
Svet Ganov13701552017-02-23 12:45:17 -08003422 currentVersion = 142;
3423 }
3424
Stephen Chen5d0922f2017-03-27 10:28:04 -07003425 if (currentVersion == 142) {
Felipe Lemeff355092017-04-03 12:55:02 -07003426 // Version 143: Set a default value for Wi-Fi wakeup feature.
Stephen Chen5d0922f2017-03-27 10:28:04 -07003427 if (userId == UserHandle.USER_SYSTEM) {
3428 final SettingsState globalSettings = getGlobalSettingsLocked();
3429 Setting currentSetting = globalSettings.getSettingLocked(
3430 Settings.Global.WIFI_WAKEUP_ENABLED);
3431 if (currentSetting.isNull()) {
3432 globalSettings.insertSettingLocked(
3433 Settings.Global.WIFI_WAKEUP_ENABLED,
3434 getContext().getResources().getBoolean(
3435 R.bool.def_wifi_wakeup_enabled) ? "1" : "0",
3436 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3437 }
3438 }
3439
3440 currentVersion = 143;
3441 }
3442
Felipe Lemeff355092017-04-03 12:55:02 -07003443 if (currentVersion == 143) {
3444 // Version 144: Set a default value for Autofill service.
3445 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3446 final Setting currentSetting = secureSettings
3447 .getSettingLocked(Settings.Secure.AUTOFILL_SERVICE);
3448 if (currentSetting.isNull()) {
3449 final String defaultValue = getContext().getResources().getString(
3450 com.android.internal.R.string.config_defaultAutofillService);
3451 if (defaultValue != null) {
3452 Slog.d(LOG_TAG, "Setting [" + defaultValue + "] as Autofill Service "
3453 + "for user " + userId);
3454 secureSettings.insertSettingLocked(Settings.Secure.AUTOFILL_SERVICE,
3455 defaultValue, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3456 }
3457 }
3458
3459 currentVersion = 144;
3460 }
3461
Jeremy Joslin45caa252017-05-04 11:22:46 -07003462 if (currentVersion == 144) {
Amin Shaikh86367962017-06-07 08:58:22 -07003463 // Version 145: Removed
3464 currentVersion = 145;
3465 }
3466
3467 if (currentVersion == 145) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003468 // Version 146: In step 142 we had a bug where incorrectly
3469 // some settings were considered system set and as a result
3470 // made the default and marked as the default being set by
3471 // the system. Here reevaluate the default and default system
3472 // set flags. This would both fix corruption by the old impl
3473 // of step 142 and also properly handle devices which never
3474 // run 142.
Jeremy Joslin45caa252017-05-04 11:22:46 -07003475 if (userId == UserHandle.USER_SYSTEM) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003476 SettingsState globalSettings = getGlobalSettingsLocked();
3477 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(globalSettings, userId);
3478 globalSettings.persistSyncLocked();
Jeremy Joslin45caa252017-05-04 11:22:46 -07003479 }
3480
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003481 SettingsState secureSettings = getSecureSettingsLocked(mUserId);
3482 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(secureSettings, userId);
3483 secureSettings.persistSyncLocked();
3484
3485 SettingsState systemSettings = getSystemSettingsLocked(mUserId);
3486 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(systemSettings, userId);
3487 systemSettings.persistSyncLocked();
3488
Amin Shaikh86367962017-06-07 08:58:22 -07003489 currentVersion = 146;
Jeremy Joslin45caa252017-05-04 11:22:46 -07003490 }
3491
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003492 if (currentVersion == 146) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003493 // Version 147: Set the default value for WIFI_WAKEUP_AVAILABLE.
3494 if (userId == UserHandle.USER_SYSTEM) {
3495 final SettingsState globalSettings = getGlobalSettingsLocked();
3496 final Setting currentSetting = globalSettings.getSettingLocked(
3497 Settings.Global.WIFI_WAKEUP_AVAILABLE);
3498 if (currentSetting.getValue() == null) {
3499 final int defaultValue = getContext().getResources().getInteger(
3500 com.android.internal.R.integer.config_wifi_wakeup_available);
3501 globalSettings.insertSettingLocked(
3502 Settings.Global.WIFI_WAKEUP_AVAILABLE,
3503 String.valueOf(defaultValue),
3504 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3505 }
3506 }
3507
3508 currentVersion = 147;
3509 }
3510
3511 if (currentVersion == 147) {
3512 // Version 148: Set the default value for DEFAULT_RESTRICT_BACKGROUND_DATA.
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003513 if (userId == UserHandle.USER_SYSTEM) {
3514 final SettingsState globalSettings = getGlobalSettingsLocked();
3515 final Setting currentSetting = globalSettings.getSettingLocked(
3516 Global.DEFAULT_RESTRICT_BACKGROUND_DATA);
3517 if (currentSetting.isNull()) {
3518 globalSettings.insertSettingLocked(
3519 Global.DEFAULT_RESTRICT_BACKGROUND_DATA,
3520 getContext().getResources().getBoolean(
3521 R.bool.def_restrict_background_data) ? "1" : "0",
3522 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3523 }
3524 }
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003525 currentVersion = 148;
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003526 }
3527
Tim Zhengcc1e76a2017-08-30 17:46:19 -07003528 if (currentVersion == 148) {
3529 // Version 149: Set the default value for BACKUP_MANAGER_CONSTANTS.
3530 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3531 final String oldValue = systemSecureSettings.getSettingLocked(
3532 Settings.Secure.BACKUP_MANAGER_CONSTANTS).getValue();
3533 if (TextUtils.equals(null, oldValue)) {
3534 final String defaultValue = getContext().getResources().getString(
3535 R.string.def_backup_manager_constants);
3536 if (!TextUtils.isEmpty(defaultValue)) {
3537 systemSecureSettings.insertSettingLocked(
3538 Settings.Secure.BACKUP_MANAGER_CONSTANTS, defaultValue, null,
3539 true, SettingsState.SYSTEM_PACKAGE_NAME);
3540 }
3541 }
Tim Zhengcc1e76a2017-08-30 17:46:19 -07003542 currentVersion = 149;
3543 }
Jacky Cheung7076a312017-10-02 10:40:48 -07003544
3545 if (currentVersion == 149) {
3546 // Version 150: Set a default value for mobile data always on
3547 final SettingsState globalSettings = getGlobalSettingsLocked();
3548 final Setting currentSetting = globalSettings.getSettingLocked(
3549 Settings.Global.MOBILE_DATA_ALWAYS_ON);
3550 if (currentSetting.isNull()) {
3551 globalSettings.insertSettingLocked(
3552 Settings.Global.MOBILE_DATA_ALWAYS_ON,
3553 getContext().getResources().getBoolean(
3554 R.bool.def_mobile_data_always_on) ? "1" : "0",
3555 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3556 }
3557
3558 currentVersion = 150;
3559 }
3560
Mike Digman4af4a6f2018-01-16 14:49:38 -08003561 if (currentVersion == 150) {
3562 // Version 151: Reset rotate locked setting for upgrading users
3563 final SettingsState systemSettings = getSystemSettingsLocked(userId);
3564 systemSettings.insertSettingLocked(
3565 Settings.System.ACCELEROMETER_ROTATION,
3566 getContext().getResources().getBoolean(
3567 R.bool.def_accelerometer_rotation) ? "1" : "0",
3568 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3569
3570 currentVersion = 151;
3571 }
3572
Felipe Lemeff355092017-04-03 12:55:02 -07003573 // vXXX: Add new settings above this point.
3574
Dan Sandler71f85e92016-07-20 13:46:05 -04003575 if (currentVersion != newVersion) {
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07003576 Slog.wtf("SettingsProvider", "warning: upgrading settings database to version "
Dan Sandler71f85e92016-07-20 13:46:05 -04003577 + newVersion + " left it at "
Stephen Chen5d0922f2017-03-27 10:28:04 -07003578 + currentVersion +
3579 " instead; this is probably a bug. Did you update SETTINGS_VERSION?",
3580 new Throwable());
Dan Sandler71f85e92016-07-20 13:46:05 -04003581 if (DEBUG) {
3582 throw new RuntimeException("db upgrade error");
3583 }
3584 }
3585
Jeff Brown503cffc2015-03-26 18:08:51 -07003586 // Return the current version.
3587 return currentVersion;
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -08003588 }
3589 }
Svet Ganov13701552017-02-23 12:45:17 -08003590
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003591 private void ensureLegacyDefaultValueAndSystemSetUpdatedLocked(SettingsState settings,
3592 int userId) {
Svet Ganov13701552017-02-23 12:45:17 -08003593 List<String> names = settings.getSettingNamesLocked();
3594 final int nameCount = names.size();
3595 for (int i = 0; i < nameCount; i++) {
3596 String name = names.get(i);
3597 Setting setting = settings.getSettingLocked(name);
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003598
3599 // In the upgrade case we pretend the call is made from the app
3600 // that made the last change to the setting to properly determine
3601 // whether the call has been made by a system component.
3602 int callingUid = -1;
3603 try {
3604 callingUid = mPackageManager.getPackageUid(setting.getPackageName(), 0, userId);
3605 } catch (RemoteException e) {
3606 /* ignore - handled below */
3607 }
3608 if (callingUid < 0) {
3609 Slog.e(LOG_TAG, "Unknown package: " + setting.getPackageName());
3610 continue;
3611 }
3612 try {
3613 final boolean systemSet = SettingsState.isSystemPackage(getContext(),
3614 setting.getPackageName(), callingUid);
Svet Ganov13701552017-02-23 12:45:17 -08003615 if (systemSet) {
3616 settings.insertSettingLocked(name, setting.getValue(),
3617 setting.getTag(), true, setting.getPackageName());
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003618 } else if (setting.getDefaultValue() != null && setting.isDefaultFromSystem()) {
3619 // We had a bug where changes by non-system packages were marked
3620 // as system made and as a result set as the default. Therefore, if
3621 // the package changed the setting last is not a system one but the
3622 // setting is marked as its default coming from the system we clear
3623 // the default and clear the system set flag.
3624 settings.resetSettingDefaultValueLocked(name);
Svet Ganov13701552017-02-23 12:45:17 -08003625 }
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003626 } catch (IllegalStateException e) {
3627 // If the package goes over its quota during the upgrade, don't
3628 // crash but just log the error as the system does the upgrade.
3629 Slog.e(LOG_TAG, "Error upgrading setting: " + setting.getName(), e);
3630
Svet Ganov13701552017-02-23 12:45:17 -08003631 }
3632 }
3633 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08003634 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07003635}