blob: a5b78497a645bf2a83fabf42ab29e45cf9e03564 [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
Matt Pape25c940d2019-03-26 12:14:33 -070019import static android.os.Process.INVALID_UID;
yuemingw1d13eae2018-01-30 17:27:54 +000020import static android.os.Process.ROOT_UID;
21import static android.os.Process.SHELL_UID;
22import static android.os.Process.SYSTEM_UID;
Winson Chungd9f2fb32019-03-05 11:10:12 -080023import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_2BUTTON;
24import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_2BUTTON_OVERLAY;
25import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON;
26import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON_OVERLAY;
27import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL;
28import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL_OVERLAY;
yuemingw1d13eae2018-01-30 17:27:54 +000029
Svetoslav683914b2015-01-15 14:22:26 -080030import android.Manifest;
Eugene Suslad72c3972016-12-27 15:49:30 -080031import android.annotation.NonNull;
Kweku Adamsb0886f32017-10-31 15:32:09 -070032import android.annotation.Nullable;
Christopher Tated5fe1472012-09-10 15:48:38 -070033import android.app.ActivityManager;
Xiaohui Chen43765b72015-08-31 10:57:33 -070034import android.app.AppGlobals;
Christopher Tate45281862010-03-05 15:46:30 -080035import android.app.backup.BackupManager;
Christopher Tate06efb532012-08-24 15:29:27 -070036import android.content.BroadcastReceiver;
Ruben Brunk98576cf2016-03-07 18:54:28 -080037import android.content.ComponentName;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070038import android.content.ContentProvider;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070039import android.content.ContentValues;
40import android.content.Context;
Christopher Tate06efb532012-08-24 15:29:27 -070041import android.content.Intent;
42import android.content.IntentFilter;
Winson Chungd9f2fb32019-03-05 11:10:12 -080043import android.content.om.IOverlayManager;
44import android.content.om.OverlayInfo;
Svetoslav683914b2015-01-15 14:22:26 -080045import android.content.pm.ApplicationInfo;
Xiaohui Chen43765b72015-08-31 10:57:33 -070046import android.content.pm.IPackageManager;
Svetoslav683914b2015-01-15 14:22:26 -080047import android.content.pm.PackageInfo;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070048import android.content.pm.PackageManager;
Christopher Tate38e7a602013-09-03 16:57:34 -070049import android.content.pm.UserInfo;
Chad Brubaker20e0dc32017-04-28 18:24:55 -070050import android.content.res.Resources;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070051import android.database.Cursor;
Svetoslav683914b2015-01-15 14:22:26 -080052import android.database.MatrixCursor;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070053import android.database.sqlite.SQLiteDatabase;
54import android.database.sqlite.SQLiteQueryBuilder;
Svetoslav683914b2015-01-15 14:22:26 -080055import android.hardware.camera2.utils.ArrayUtils;
John Spurlocke11ae112015-05-11 16:09:03 -040056import android.media.AudioManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070057import android.net.Uri;
Christopher Tate06efb532012-08-24 15:29:27 -070058import android.os.Binder;
Svetoslav683914b2015-01-15 14:22:26 -080059import android.os.Build;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080060import android.os.Bundle;
Amith Yamasani5cdf7f52013-06-27 15:12:01 -070061import android.os.DropBoxManager;
Svetoslav683914b2015-01-15 14:22:26 -080062import android.os.Environment;
Svetoslav7e0683b2015-08-03 16:02:52 -070063import android.os.Handler;
Svet Ganova8f90262016-05-10 08:44:48 -070064import android.os.HandlerThread;
Svetoslav7e0683b2015-08-03 16:02:52 -070065import android.os.Looper;
66import android.os.Message;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070067import android.os.ParcelFileDescriptor;
Christopher Tate0da13572013-10-13 17:34:49 -070068import android.os.Process;
Xiaohui Chen43765b72015-08-31 10:57:33 -070069import android.os.RemoteException;
Jeff Sharkey413573a2016-02-22 17:52:45 -070070import android.os.SELinux;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -070071import android.os.ServiceManager;
Christopher Tate06efb532012-08-24 15:29:27 -070072import android.os.UserHandle;
73import android.os.UserManager;
Svet Ganov53a441c2016-04-19 19:38:00 -070074import android.os.UserManagerInternal;
Matt Pape1278d1c2018-12-11 13:03:49 -080075import android.provider.DeviceConfig;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070076import android.provider.Settings;
Narayan Kamath94bcdbc2017-07-17 15:32:53 +010077import android.provider.Settings.Global;
Makoto Onuki0000d322017-11-28 16:31:47 -080078import android.provider.Settings.Secure;
yuemingw1d13eae2018-01-30 17:27:54 +000079import android.provider.SettingsValidators;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070080import android.text.TextUtils;
Andre Lago3fa139c2016-08-04 13:53:44 +010081import android.util.ArrayMap;
Svetoslav683914b2015-01-15 14:22:26 -080082import android.util.ArraySet;
Mark Rathjend891f012017-01-19 04:10:37 +000083import android.util.ByteStringUtils;
Christopher Tate06efb532012-08-24 15:29:27 -070084import android.util.Slog;
85import android.util.SparseArray;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -070086import android.util.SparseBooleanArray;
Eugene Suslad72c3972016-12-27 15:49:30 -080087import android.util.proto.ProtoOutputStream;
John Spurlocke11ae112015-05-11 16:09:03 -040088
Svetoslav683914b2015-01-15 14:22:26 -080089import com.android.internal.annotations.GuardedBy;
90import com.android.internal.content.PackageMonitor;
91import com.android.internal.os.BackgroundThread;
Suprabh Shukla269c11e2015-12-02 16:51:16 -080092import com.android.providers.settings.SettingsState.Setting;
Svet Ganov53a441c2016-04-19 19:38:00 -070093import com.android.server.LocalServices;
Ruben Brunk98576cf2016-03-07 18:54:28 -080094import com.android.server.SystemConfig;
John Spurlocke11ae112015-05-11 16:09:03 -040095
Irina Dumitrescue3696872019-01-09 16:07:59 +000096import com.google.android.collect.Sets;
97
Svetoslav683914b2015-01-15 14:22:26 -080098import java.io.File;
Svetoslavb505ccc2015-02-17 12:41:04 -080099import java.io.FileDescriptor;
Svetoslav683914b2015-01-15 14:22:26 -0800100import java.io.FileNotFoundException;
Svetoslavb505ccc2015-02-17 12:41:04 -0800101import java.io.PrintWriter;
Mark Rathjen7599f132017-01-23 14:15:54 -0800102import java.nio.ByteBuffer;
103import java.security.InvalidKeyException;
Mark Rathjend891f012017-01-19 04:10:37 +0000104import java.security.NoSuchAlgorithmException;
Svetoslav683914b2015-01-15 14:22:26 -0800105import java.security.SecureRandom;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700106import java.util.ArrayList;
Svetoslav683914b2015-01-15 14:22:26 -0800107import java.util.Arrays;
Robin Lee7af9a742017-02-20 14:47:30 +0000108import java.util.Collection;
Matt Pape6bfc62e2018-11-28 13:16:03 -0800109import java.util.HashMap;
Mark Rathjend891f012017-01-19 04:10:37 +0000110import java.util.HashSet;
Svetoslav683914b2015-01-15 14:22:26 -0800111import java.util.List;
Mark Rathjen7599f132017-01-23 14:15:54 -0800112import java.util.Locale;
Andre Lago3fa139c2016-08-04 13:53:44 +0100113import java.util.Map;
Svetoslav683914b2015-01-15 14:22:26 -0800114import java.util.Set;
115import java.util.regex.Pattern;
yuemingw1d13eae2018-01-30 17:27:54 +0000116
Mark Rathjen7599f132017-01-23 14:15:54 -0800117import javax.crypto.Mac;
118import javax.crypto.spec.SecretKeySpec;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700119
Svetoslav Ganove080da92016-12-21 17:10:35 -0800120
Svetoslav683914b2015-01-15 14:22:26 -0800121/**
122 * <p>
123 * This class is a content provider that publishes the system settings.
124 * It can be accessed via the content provider APIs or via custom call
125 * commands. The latter is a bit faster and is the preferred way to access
126 * the platform settings.
127 * </p>
128 * <p>
129 * There are three settings types, global (with signature level protection
130 * and shared across users), secure (with signature permission level
131 * protection and per user), and system (with dangerous permission level
132 * protection and per user). Global settings are stored under the device owner.
133 * Each of these settings is represented by a {@link
134 * com.android.providers.settings.SettingsState} object mapped to an integer
135 * key derived from the setting type in the most significant bits and user
136 * id in the least significant bits. Settings are synchronously loaded on
137 * instantiation of a SettingsState and asynchronously persisted on mutation.
138 * Settings are stored in the user specific system directory.
139 * </p>
140 * <p>
141 * Apps targeting APIs Lollipop MR1 and lower can add custom settings entries
142 * and get a warning. Targeting higher API version prohibits this as the
143 * system settings are not a place for apps to save their state. When a package
144 * is removed the settings it added are deleted. Apps cannot delete system
145 * settings added by the platform. System settings values are validated to
146 * ensure the clients do not put bad values. Global and secure settings are
147 * changed only by trusted parties, therefore no validation is performed. Also
148 * there is a limit on the amount of app specific settings that can be added
149 * to prevent unlimited growth of the system process memory footprint.
150 * </p>
151 */
152@SuppressWarnings("deprecation")
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700153public class SettingsProvider extends ContentProvider {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700154 static final boolean DEBUG = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700155
Svetoslav Ganov264c7a92016-08-24 17:31:14 -0700156 private static final boolean DROP_DATABASE_ON_MIGRATION = true;
Svetoslav683914b2015-01-15 14:22:26 -0800157
158 private static final String LOG_TAG = "SettingsProvider";
Christopher Tate0da13572013-10-13 17:34:49 -0700159
Christopher Tate06efb532012-08-24 15:29:27 -0700160 private static final String TABLE_SYSTEM = "system";
161 private static final String TABLE_SECURE = "secure";
162 private static final String TABLE_GLOBAL = "global";
Matt Pape6bfc62e2018-11-28 13:16:03 -0800163 private static final String TABLE_CONFIG = "config";
Svetoslav683914b2015-01-15 14:22:26 -0800164
165 // Old tables no longer exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 private static final String TABLE_FAVORITES = "favorites";
167 private static final String TABLE_OLD_FAVORITES = "old_favorites";
Svetoslav683914b2015-01-15 14:22:26 -0800168 private static final String TABLE_BLUETOOTH_DEVICES = "bluetooth_devices";
169 private static final String TABLE_BOOKMARKS = "bookmarks";
170 private static final String TABLE_ANDROID_METADATA = "android_metadata";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171
Svetoslav683914b2015-01-15 14:22:26 -0800172 // The set of removed legacy tables.
173 private static final Set<String> REMOVED_LEGACY_TABLES = new ArraySet<>();
Christopher Tate06efb532012-08-24 15:29:27 -0700174 static {
Svetoslav683914b2015-01-15 14:22:26 -0800175 REMOVED_LEGACY_TABLES.add(TABLE_FAVORITES);
176 REMOVED_LEGACY_TABLES.add(TABLE_OLD_FAVORITES);
177 REMOVED_LEGACY_TABLES.add(TABLE_BLUETOOTH_DEVICES);
178 REMOVED_LEGACY_TABLES.add(TABLE_BOOKMARKS);
179 REMOVED_LEGACY_TABLES.add(TABLE_ANDROID_METADATA);
180 }
Christopher Tate06efb532012-08-24 15:29:27 -0700181
Svetoslav683914b2015-01-15 14:22:26 -0800182 private static final int MUTATION_OPERATION_INSERT = 1;
183 private static final int MUTATION_OPERATION_DELETE = 2;
184 private static final int MUTATION_OPERATION_UPDATE = 3;
Svetoslav Ganove080da92016-12-21 17:10:35 -0800185 private static final int MUTATION_OPERATION_RESET = 4;
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400186
Svetoslav683914b2015-01-15 14:22:26 -0800187 private static final String[] ALL_COLUMNS = new String[] {
188 Settings.NameValueTable._ID,
189 Settings.NameValueTable.NAME,
190 Settings.NameValueTable.VALUE
191 };
192
Makoto Onuki53f0e022017-11-29 13:51:01 -0800193 public static final int SETTINGS_TYPE_GLOBAL = SettingsState.SETTINGS_TYPE_GLOBAL;
194 public static final int SETTINGS_TYPE_SYSTEM = SettingsState.SETTINGS_TYPE_SYSTEM;
195 public static final int SETTINGS_TYPE_SECURE = SettingsState.SETTINGS_TYPE_SECURE;
196 public static final int SETTINGS_TYPE_SSAID = SettingsState.SETTINGS_TYPE_SSAID;
Matt Pape1b31a332018-10-17 09:58:28 -0700197 public static final int SETTINGS_TYPE_CONFIG = SettingsState.SETTINGS_TYPE_CONFIG;
Svet Ganov53a441c2016-04-19 19:38:00 -0700198
199 private static final Bundle NULL_SETTING_BUNDLE = Bundle.forPair(
200 Settings.NameValueTable.VALUE, null);
Christopher Tate06efb532012-08-24 15:29:27 -0700201
Nicholas Sauer3d87d1e2018-11-06 08:38:39 -0800202 public static final String RESULT_ROWS_DELETED = "result_rows_deleted";
Nicholas Sauer72500532018-11-21 10:30:58 -0800203 public static final String RESULT_SETTINGS_LIST = "result_settings_list";
Nicholas Sauer3d87d1e2018-11-06 08:38:39 -0800204
Chad Brubaker20e0dc32017-04-28 18:24:55 -0700205 // Overlay specified settings whitelisted for Instant Apps
206 private static final Set<String> OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS = new ArraySet<>();
207 private static final Set<String> OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS = new ArraySet<>();
208 private static final Set<String> OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS = new ArraySet<>();
209
210 static {
211 for (String name : Resources.getSystem().getStringArray(
212 com.android.internal.R.array.config_allowedGlobalInstantAppSettings)) {
213 OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS.add(name);
214 }
215 for (String name : Resources.getSystem().getStringArray(
216 com.android.internal.R.array.config_allowedSystemInstantAppSettings)) {
217 OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS.add(name);
218 }
219 for (String name : Resources.getSystem().getStringArray(
220 com.android.internal.R.array.config_allowedSecureInstantAppSettings)) {
221 OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS.add(name);
222 }
223 }
224
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -0800225 // Changes to these global settings are synchronously persisted
226 private static final Set<String> CRITICAL_GLOBAL_SETTINGS = new ArraySet<>();
227 static {
228 CRITICAL_GLOBAL_SETTINGS.add(Settings.Global.DEVICE_PROVISIONED);
229 }
230
231 // Changes to these secure settings are synchronously persisted
232 private static final Set<String> CRITICAL_SECURE_SETTINGS = new ArraySet<>();
233 static {
234 CRITICAL_SECURE_SETTINGS.add(Settings.Secure.USER_SETUP_COMPLETE);
235 }
236
Svetoslav683914b2015-01-15 14:22:26 -0800237 // Per user secure settings that moved to the for all users global settings.
238 static final Set<String> sSecureMovedToGlobalSettings = new ArraySet<>();
239 static {
240 Settings.Secure.getMovedToGlobalSettings(sSecureMovedToGlobalSettings);
Christopher Tate06efb532012-08-24 15:29:27 -0700241 }
242
Svetoslav683914b2015-01-15 14:22:26 -0800243 // Per user system settings that moved to the for all users global settings.
244 static final Set<String> sSystemMovedToGlobalSettings = new ArraySet<>();
245 static {
246 Settings.System.getMovedToGlobalSettings(sSystemMovedToGlobalSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700247 }
248
Svetoslav683914b2015-01-15 14:22:26 -0800249 // Per user system settings that moved to the per user secure settings.
250 static final Set<String> sSystemMovedToSecureSettings = new ArraySet<>();
251 static {
252 Settings.System.getMovedToSecureSettings(sSystemMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700253 }
254
Svetoslav683914b2015-01-15 14:22:26 -0800255 // Per all users global settings that moved to the per user secure settings.
256 static final Set<String> sGlobalMovedToSecureSettings = new ArraySet<>();
257 static {
258 Settings.Global.getMovedToSecureSettings(sGlobalMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700259 }
260
Svetoslav683914b2015-01-15 14:22:26 -0800261 // Per user secure settings that are cloned for the managed profiles of the user.
262 private static final Set<String> sSecureCloneToManagedSettings = new ArraySet<>();
263 static {
264 Settings.Secure.getCloneToManagedProfileSettings(sSecureCloneToManagedSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700265 }
266
Svetoslav683914b2015-01-15 14:22:26 -0800267 // Per user system settings that are cloned for the managed profiles of the user.
268 private static final Set<String> sSystemCloneToManagedSettings = new ArraySet<>();
269 static {
270 Settings.System.getCloneToManagedProfileSettings(sSystemCloneToManagedSettings);
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400271 }
272
Andre Lago3fa139c2016-08-04 13:53:44 +0100273 // Per user system settings that are cloned from the profile's parent when a dependency
274 // in {@link Settings.Secure} is set to "1".
275 public static final Map<String, String> sSystemCloneFromParentOnDependency = new ArrayMap<>();
276 static {
277 Settings.System.getCloneFromParentOnValueSettings(sSystemCloneFromParentOnDependency);
278 }
279
Svetoslav683914b2015-01-15 14:22:26 -0800280 private final Object mLock = new Object();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700281
Svetoslav683914b2015-01-15 14:22:26 -0800282 @GuardedBy("mLock")
283 private SettingsRegistry mSettingsRegistry;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700284
Svet Ganova8f90262016-05-10 08:44:48 -0700285 @GuardedBy("mLock")
286 private HandlerThread mHandlerThread;
287
Makoto Onuki73360ab2017-03-17 11:50:13 -0700288 @GuardedBy("mLock")
289 private Handler mHandler;
290
Svetoslav7ec28e82015-05-20 17:01:10 -0700291 // We have to call in the user manager with no lock held,
292 private volatile UserManager mUserManager;
Svetoslav683914b2015-01-15 14:22:26 -0800293
yuemingw1d13eae2018-01-30 17:27:54 +0000294 private UserManagerInternal mUserManagerInternal;
295
Svetoslav7ec28e82015-05-20 17:01:10 -0700296 // We have to call in the package manager with no lock held,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700297 private volatile IPackageManager mPackageManager;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700298
Svet Ganov53a441c2016-04-19 19:38:00 -0700299 public static int makeKey(int type, int userId) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800300 return SettingsState.makeKey(type, userId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700301 }
302
303 public static int getTypeFromKey(int key) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800304 return SettingsState.getTypeFromKey(key);
Svet Ganov53a441c2016-04-19 19:38:00 -0700305 }
306
307 public static int getUserIdFromKey(int key) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800308 return SettingsState.getUserIdFromKey(key);
Svet Ganov53a441c2016-04-19 19:38:00 -0700309 }
310
311 public static String settingTypeToString(int type) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800312 return SettingsState.settingTypeToString(type);
Svet Ganov53a441c2016-04-19 19:38:00 -0700313 }
314
315 public static String keyToString(int key) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800316 return SettingsState.keyToString(key);
Svet Ganov53a441c2016-04-19 19:38:00 -0700317 }
318
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700319 @Override
320 public boolean onCreate() {
Chad Brubaker97bccee2017-01-05 15:51:41 -0800321 Settings.setInSystemServer();
Michal Karpinski2c37b082018-01-18 16:14:27 +0000322
323 // fail to boot if there're any backed up settings that don't have a non-null validator
324 ensureAllBackedUpSystemSettingsHaveValidators();
Michal Karpinski5db1e432018-01-18 20:10:24 +0000325 ensureAllBackedUpGlobalSettingsHaveValidators();
Michal Karpinski964943a2018-01-19 16:28:26 +0000326 ensureAllBackedUpSecureSettingsHaveValidators();
Michal Karpinski2c37b082018-01-18 16:14:27 +0000327
Svetoslav683914b2015-01-15 14:22:26 -0800328 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700329 mUserManager = UserManager.get(getContext());
yuemingw1d13eae2018-01-30 17:27:54 +0000330 mUserManagerInternal = LocalServices.getService(UserManagerInternal.class);
Xiaohui Chen43765b72015-08-31 10:57:33 -0700331 mPackageManager = AppGlobals.getPackageManager();
Svet Ganova8f90262016-05-10 08:44:48 -0700332 mHandlerThread = new HandlerThread(LOG_TAG,
333 Process.THREAD_PRIORITY_BACKGROUND);
334 mHandlerThread.start();
Makoto Onuki73360ab2017-03-17 11:50:13 -0700335 mHandler = new Handler(mHandlerThread.getLooper());
Svetoslav683914b2015-01-15 14:22:26 -0800336 mSettingsRegistry = new SettingsRegistry();
337 }
Makoto Onuki73360ab2017-03-17 11:50:13 -0700338 mHandler.post(() -> {
339 registerBroadcastReceivers();
340 startWatchingUserRestrictionChanges();
341 });
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700342 ServiceManager.addService("settings", new SettingsService(this));
Matt Papeabb67892018-12-07 09:13:26 -0800343 ServiceManager.addService("device_config", new DeviceConfigService(this));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700344 return true;
345 }
346
Michal Karpinski2c37b082018-01-18 16:14:27 +0000347 private void ensureAllBackedUpSystemSettingsHaveValidators() {
Michal Karpinski964943a2018-01-19 16:28:26 +0000348 String offenders = getOffenders(concat(Settings.System.SETTINGS_TO_BACKUP,
349 Settings.System.LEGACY_RESTORE_SETTINGS), Settings.System.VALIDATORS);
Michal Karpinski5db1e432018-01-18 20:10:24 +0000350
351 failToBootIfOffendersPresent(offenders, "Settings.System");
352 }
353
354 private void ensureAllBackedUpGlobalSettingsHaveValidators() {
Michal Karpinski964943a2018-01-19 16:28:26 +0000355 String offenders = getOffenders(concat(Settings.Global.SETTINGS_TO_BACKUP,
356 Settings.Global.LEGACY_RESTORE_SETTINGS), Settings.Global.VALIDATORS);
Michal Karpinski5db1e432018-01-18 20:10:24 +0000357
358 failToBootIfOffendersPresent(offenders, "Settings.Global");
359 }
360
Michal Karpinski964943a2018-01-19 16:28:26 +0000361 private void ensureAllBackedUpSecureSettingsHaveValidators() {
362 String offenders = getOffenders(concat(Settings.Secure.SETTINGS_TO_BACKUP,
363 Settings.Secure.LEGACY_RESTORE_SETTINGS), Settings.Secure.VALIDATORS);
364
365 failToBootIfOffendersPresent(offenders, "Settings.Secure");
366 }
367
Michal Karpinski5db1e432018-01-18 20:10:24 +0000368 private void failToBootIfOffendersPresent(String offenders, String settingsType) {
369 if (offenders.length() > 0) {
370 throw new RuntimeException("All " + settingsType + " settings that are backed up"
371 + " have to have a non-null validator, but those don't: " + offenders);
372 }
373 }
374
375 private String getOffenders(String[] settingsToBackup, Map<String,
376 SettingsValidators.Validator> validators) {
Michal Karpinski2c37b082018-01-18 16:14:27 +0000377 StringBuilder offenders = new StringBuilder();
Michal Karpinski5db1e432018-01-18 20:10:24 +0000378 for (String setting : settingsToBackup) {
379 if (validators.get(setting) == null) {
Michal Karpinski2c37b082018-01-18 16:14:27 +0000380 offenders.append(setting).append(" ");
381 }
382 }
Michal Karpinski5db1e432018-01-18 20:10:24 +0000383 return offenders.toString();
Michal Karpinski2c37b082018-01-18 16:14:27 +0000384 }
385
Michal Karpinski964943a2018-01-19 16:28:26 +0000386 private final String[] concat(String[] first, String[] second) {
387 if (second == null || second.length == 0) {
388 return first;
389 }
390 final int firstLen = first.length;
391 final int secondLen = second.length;
392 String[] both = new String[firstLen + secondLen];
393 System.arraycopy(first, 0, both, 0, firstLen);
394 System.arraycopy(second, 0, both, firstLen, secondLen);
395 return both;
396 }
397
Svetoslav683914b2015-01-15 14:22:26 -0800398 @Override
399 public Bundle call(String method, String name, Bundle args) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700400 final int requestingUserId = getRequestingUserId(args);
401 switch (method) {
Matt Pape1b31a332018-10-17 09:58:28 -0700402 case Settings.CALL_METHOD_GET_CONFIG: {
403 Setting setting = getConfigSetting(name);
404 return packageValueForCallResult(setting, isTrackingGeneration(args));
405 }
406
Svetoslav7ec28e82015-05-20 17:01:10 -0700407 case Settings.CALL_METHOD_GET_GLOBAL: {
408 Setting setting = getGlobalSetting(name);
Svet Ganov53a441c2016-04-19 19:38:00 -0700409 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav683914b2015-01-15 14:22:26 -0800410 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700411
412 case Settings.CALL_METHOD_GET_SECURE: {
Makoto Onuki0000d322017-11-28 16:31:47 -0800413 Setting setting = getSecureSetting(name, requestingUserId,
414 /*enableOverride=*/ true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700415 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700416 }
417
418 case Settings.CALL_METHOD_GET_SYSTEM: {
419 Setting setting = getSystemSetting(name, requestingUserId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700420 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700421 }
422
Matt Pape1b31a332018-10-17 09:58:28 -0700423 case Settings.CALL_METHOD_PUT_CONFIG: {
424 String value = getSettingValue(args);
Matt Pape1b31a332018-10-17 09:58:28 -0700425 final boolean makeDefault = getSettingMakeDefault(args);
Matt Papec1323dc2018-12-11 12:32:42 -0800426 insertConfigSetting(name, value, makeDefault);
Matt Pape1b31a332018-10-17 09:58:28 -0700427 break;
428 }
429
Svetoslav7ec28e82015-05-20 17:01:10 -0700430 case Settings.CALL_METHOD_PUT_GLOBAL: {
431 String value = getSettingValue(args);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800432 String tag = getSettingTag(args);
433 final boolean makeDefault = getSettingMakeDefault(args);
434 insertGlobalSetting(name, value, tag, makeDefault, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700435 break;
436 }
437
438 case Settings.CALL_METHOD_PUT_SECURE: {
439 String value = getSettingValue(args);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800440 String tag = getSettingTag(args);
441 final boolean makeDefault = getSettingMakeDefault(args);
442 insertSecureSetting(name, value, tag, makeDefault, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700443 break;
444 }
445
446 case Settings.CALL_METHOD_PUT_SYSTEM: {
447 String value = getSettingValue(args);
448 insertSystemSetting(name, value, requestingUserId);
449 break;
450 }
451
Matt Pape1b31a332018-10-17 09:58:28 -0700452 case Settings.CALL_METHOD_RESET_CONFIG: {
453 final int mode = getResetModeEnforcingPermission(args);
Matt Pape6bfc62e2018-11-28 13:16:03 -0800454 String prefix = getSettingPrefix(args);
Matt Papec1323dc2018-12-11 12:32:42 -0800455 resetConfigSetting(mode, prefix);
Matt Pape1b31a332018-10-17 09:58:28 -0700456 break;
457 }
458
Svetoslav Ganove080da92016-12-21 17:10:35 -0800459 case Settings.CALL_METHOD_RESET_GLOBAL: {
460 final int mode = getResetModeEnforcingPermission(args);
461 String tag = getSettingTag(args);
462 resetGlobalSetting(requestingUserId, mode, tag);
463 break;
464 }
465
466 case Settings.CALL_METHOD_RESET_SECURE: {
467 final int mode = getResetModeEnforcingPermission(args);
468 String tag = getSettingTag(args);
469 resetSecureSetting(requestingUserId, mode, tag);
470 break;
471 }
472
Matt Pape6bfc62e2018-11-28 13:16:03 -0800473 case Settings.CALL_METHOD_DELETE_CONFIG: {
Matt Papec1323dc2018-12-11 12:32:42 -0800474 int rows = deleteConfigSetting(name) ? 1 : 0;
Nicholas Sauer3d87d1e2018-11-06 08:38:39 -0800475 Bundle result = new Bundle();
476 result.putInt(RESULT_ROWS_DELETED, rows);
477 return result;
478 }
479
480 case Settings.CALL_METHOD_DELETE_GLOBAL: {
481 int rows = deleteGlobalSetting(name, requestingUserId, false) ? 1 : 0;
482 Bundle result = new Bundle();
483 result.putInt(RESULT_ROWS_DELETED, rows);
484 return result;
485 }
486
Matt Pape6bfc62e2018-11-28 13:16:03 -0800487 case Settings.CALL_METHOD_DELETE_SECURE: {
488 int rows = deleteSecureSetting(name, requestingUserId, false) ? 1 : 0;
489 Bundle result = new Bundle();
490 result.putInt(RESULT_ROWS_DELETED, rows);
491 return result;
492 }
493
494 case Settings.CALL_METHOD_DELETE_SYSTEM: {
495 int rows = deleteSystemSetting(name, requestingUserId) ? 1 : 0;
496 Bundle result = new Bundle();
497 result.putInt(RESULT_ROWS_DELETED, rows);
498 return result;
499 }
500
501 case Settings.CALL_METHOD_LIST_CONFIG: {
502 String prefix = getSettingPrefix(args);
503 Bundle result = new Bundle();
504 result.putSerializable(
505 Settings.NameValueTable.VALUE, (HashMap) getAllConfigFlags(prefix));
506 return result;
507 }
508
509 case Settings.CALL_METHOD_LIST_GLOBAL: {
Nicholas Sauer72500532018-11-21 10:30:58 -0800510 Bundle result = new Bundle();
511 result.putStringArrayList(RESULT_SETTINGS_LIST,
Matt Pape6bfc62e2018-11-28 13:16:03 -0800512 buildSettingsList(getAllGlobalSettings(null)));
Nicholas Sauer72500532018-11-21 10:30:58 -0800513 return result;
514 }
515
516 case Settings.CALL_METHOD_LIST_SECURE: {
517 Bundle result = new Bundle();
518 result.putStringArrayList(RESULT_SETTINGS_LIST,
519 buildSettingsList(getAllSecureSettings(requestingUserId, null)));
520 return result;
521 }
522
Matt Pape6bfc62e2018-11-28 13:16:03 -0800523 case Settings.CALL_METHOD_LIST_SYSTEM: {
Nicholas Sauer72500532018-11-21 10:30:58 -0800524 Bundle result = new Bundle();
525 result.putStringArrayList(RESULT_SETTINGS_LIST,
Matt Pape6bfc62e2018-11-28 13:16:03 -0800526 buildSettingsList(getAllSystemSettings(requestingUserId, null)));
Nicholas Sauer72500532018-11-21 10:30:58 -0800527 return result;
528 }
529
Svetoslav7ec28e82015-05-20 17:01:10 -0700530 default: {
531 Slog.w(LOG_TAG, "call() with invalid method: " + method);
532 } break;
Christopher Tate06efb532012-08-24 15:29:27 -0700533 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700534
Christopher Tate06efb532012-08-24 15:29:27 -0700535 return null;
536 }
537
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800538 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800539 public String getType(Uri uri) {
540 Arguments args = new Arguments(uri, null, null, true);
541 if (TextUtils.isEmpty(args.name)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700542 return "vnd.android.cursor.dir/" + args.table;
543 } else {
Svetoslav7ec28e82015-05-20 17:01:10 -0700544 return "vnd.android.cursor.item/" + args.table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700545 }
546 }
547
548 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800549 public Cursor query(Uri uri, String[] projection, String where, String[] whereArgs,
550 String order) {
551 if (DEBUG) {
552 Slog.v(LOG_TAG, "query() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700553 }
554
Svetoslav683914b2015-01-15 14:22:26 -0800555 Arguments args = new Arguments(uri, where, whereArgs, true);
556 String[] normalizedProjection = normalizeProjection(projection);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700557
Svetoslav683914b2015-01-15 14:22:26 -0800558 // If a legacy table that is gone, done.
559 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
560 return new MatrixCursor(normalizedProjection, 0);
561 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700562
Svetoslav7ec28e82015-05-20 17:01:10 -0700563 switch (args.table) {
564 case TABLE_GLOBAL: {
565 if (args.name != null) {
566 Setting setting = getGlobalSetting(args.name);
567 return packageSettingForQuery(setting, normalizedProjection);
568 } else {
569 return getAllGlobalSettings(projection);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700570 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700571 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700572
Svetoslav7ec28e82015-05-20 17:01:10 -0700573 case TABLE_SECURE: {
574 final int userId = UserHandle.getCallingUserId();
575 if (args.name != null) {
576 Setting setting = getSecureSetting(args.name, userId);
577 return packageSettingForQuery(setting, normalizedProjection);
578 } else {
579 return getAllSecureSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800580 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700581 }
Svetoslav683914b2015-01-15 14:22:26 -0800582
Svetoslav7ec28e82015-05-20 17:01:10 -0700583 case TABLE_SYSTEM: {
584 final int userId = UserHandle.getCallingUserId();
585 if (args.name != null) {
586 Setting setting = getSystemSetting(args.name, userId);
587 return packageSettingForQuery(setting, normalizedProjection);
588 } else {
589 return getAllSystemSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800590 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700591 }
Svetoslav683914b2015-01-15 14:22:26 -0800592
Svetoslav7ec28e82015-05-20 17:01:10 -0700593 default: {
594 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700595 }
596 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700597 }
598
Nicholas Sauer72500532018-11-21 10:30:58 -0800599 private ArrayList<String> buildSettingsList(Cursor cursor) {
600 final ArrayList<String> lines = new ArrayList<String>();
601 try {
602 while (cursor != null && cursor.moveToNext()) {
603 lines.add(cursor.getString(1) + "=" + cursor.getString(2));
604 }
605 } finally {
606 if (cursor != null) {
607 cursor.close();
608 }
609 }
610 return lines;
611 }
612
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700613 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800614 public Uri insert(Uri uri, ContentValues values) {
615 if (DEBUG) {
616 Slog.v(LOG_TAG, "insert() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700617 }
618
Svetoslav683914b2015-01-15 14:22:26 -0800619 String table = getValidTableOrThrow(uri);
Christopher Tate06efb532012-08-24 15:29:27 -0700620
Svetoslav683914b2015-01-15 14:22:26 -0800621 // If a legacy table that is gone, done.
622 if (REMOVED_LEGACY_TABLES.contains(table)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 return null;
624 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700625
Svetoslav683914b2015-01-15 14:22:26 -0800626 String name = values.getAsString(Settings.Secure.NAME);
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700627 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800628 return null;
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700629 }
630
Svetoslav683914b2015-01-15 14:22:26 -0800631 String value = values.getAsString(Settings.Secure.VALUE);
632
Svetoslav7ec28e82015-05-20 17:01:10 -0700633 switch (table) {
634 case TABLE_GLOBAL: {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800635 if (insertGlobalSetting(name, value, null, false,
636 UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700637 return Uri.withAppendedPath(Settings.Global.CONTENT_URI, name);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700638 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700639 } break;
640
641 case TABLE_SECURE: {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800642 if (insertSecureSetting(name, value, null, false,
643 UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700644 return Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name);
645 }
646 } break;
647
648 case TABLE_SYSTEM: {
649 if (insertSystemSetting(name, value, UserHandle.getCallingUserId())) {
650 return Uri.withAppendedPath(Settings.System.CONTENT_URI, name);
651 }
652 } break;
653
654 default: {
655 throw new IllegalArgumentException("Bad Uri path:" + uri);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700656 }
657 }
658
Svetoslav683914b2015-01-15 14:22:26 -0800659 return null;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700660 }
661
662 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800663 public int bulkInsert(Uri uri, ContentValues[] allValues) {
664 if (DEBUG) {
665 Slog.v(LOG_TAG, "bulkInsert() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700667
Svetoslav683914b2015-01-15 14:22:26 -0800668 int insertionCount = 0;
669 final int valuesCount = allValues.length;
670 for (int i = 0; i < valuesCount; i++) {
671 ContentValues values = allValues[i];
672 if (insert(uri, values) != null) {
673 insertionCount++;
674 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700675 }
Svetoslav683914b2015-01-15 14:22:26 -0800676
677 return insertionCount;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700678 }
679
680 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800681 public int delete(Uri uri, String where, String[] whereArgs) {
682 if (DEBUG) {
683 Slog.v(LOG_TAG, "delete() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700685
Svetoslav683914b2015-01-15 14:22:26 -0800686 Arguments args = new Arguments(uri, where, whereArgs, false);
687
688 // If a legacy table that is gone, done.
689 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
690 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700691 }
Svetoslav683914b2015-01-15 14:22:26 -0800692
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700693 if (!isKeyValid(args.name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800694 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700695 }
Svetoslav683914b2015-01-15 14:22:26 -0800696
Svetoslav7ec28e82015-05-20 17:01:10 -0700697 switch (args.table) {
698 case TABLE_GLOBAL: {
699 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700700 return deleteGlobalSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700701 }
Svetoslav683914b2015-01-15 14:22:26 -0800702
Svetoslav7ec28e82015-05-20 17:01:10 -0700703 case TABLE_SECURE: {
704 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700705 return deleteSecureSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700706 }
Svetoslav683914b2015-01-15 14:22:26 -0800707
Svetoslav7ec28e82015-05-20 17:01:10 -0700708 case TABLE_SYSTEM: {
709 final int userId = UserHandle.getCallingUserId();
710 return deleteSystemSetting(args.name, userId) ? 1 : 0;
711 }
712
713 default: {
714 throw new IllegalArgumentException("Bad Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800715 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700716 }
Svetoslav683914b2015-01-15 14:22:26 -0800717 }
718
719 @Override
720 public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
721 if (DEBUG) {
722 Slog.v(LOG_TAG, "update() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700723 }
Svetoslav683914b2015-01-15 14:22:26 -0800724
725 Arguments args = new Arguments(uri, where, whereArgs, false);
726
727 // If a legacy table that is gone, done.
728 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
729 return 0;
730 }
731
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700732 String name = values.getAsString(Settings.Secure.NAME);
733 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800734 return 0;
735 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700736 String value = values.getAsString(Settings.Secure.VALUE);
Svetoslav683914b2015-01-15 14:22:26 -0800737
Svetoslav7ec28e82015-05-20 17:01:10 -0700738 switch (args.table) {
739 case TABLE_GLOBAL: {
740 final int userId = UserHandle.getCallingUserId();
Svetoslav Ganove080da92016-12-21 17:10:35 -0800741 return updateGlobalSetting(args.name, value, null, false,
742 userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700743 }
Svetoslav683914b2015-01-15 14:22:26 -0800744
Svetoslav7ec28e82015-05-20 17:01:10 -0700745 case TABLE_SECURE: {
746 final int userId = UserHandle.getCallingUserId();
Svetoslav Ganove080da92016-12-21 17:10:35 -0800747 return updateSecureSetting(args.name, value, null, false,
748 userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700749 }
Svetoslav683914b2015-01-15 14:22:26 -0800750
Svetoslav7ec28e82015-05-20 17:01:10 -0700751 case TABLE_SYSTEM: {
752 final int userId = UserHandle.getCallingUserId();
753 return updateSystemSetting(args.name, value, userId) ? 1 : 0;
754 }
Svetoslav683914b2015-01-15 14:22:26 -0800755
Svetoslav7ec28e82015-05-20 17:01:10 -0700756 default: {
757 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800758 }
759 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700760 }
761
762 @Override
763 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
Robin Lee2ab02e22016-07-28 18:41:23 +0100764 final int userId = getUserIdFromUri(uri, UserHandle.getCallingUserId());
765 if (userId != UserHandle.getCallingUserId()) {
766 getContext().enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS,
767 "Access files from the settings of another user");
768 }
769 uri = ContentProvider.getUriWithoutUserId(uri);
770
Andre Lago3fa139c2016-08-04 13:53:44 +0100771 final String cacheRingtoneSetting;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700772 final String cacheName;
773 if (Settings.System.RINGTONE_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100774 cacheRingtoneSetting = Settings.System.RINGTONE;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700775 cacheName = Settings.System.RINGTONE_CACHE;
776 } else if (Settings.System.NOTIFICATION_SOUND_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100777 cacheRingtoneSetting = Settings.System.NOTIFICATION_SOUND;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700778 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
779 } else if (Settings.System.ALARM_ALERT_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100780 cacheRingtoneSetting = Settings.System.ALARM_ALERT;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700781 cacheName = Settings.System.ALARM_ALERT_CACHE;
782 } else {
783 throw new FileNotFoundException("Direct file access no longer supported; "
784 + "ringtone playback is available through android.media.Ringtone");
785 }
786
Andre Lago3fa139c2016-08-04 13:53:44 +0100787 int actualCacheOwner;
788 // Redirect cache to parent if ringtone setting is owned by profile parent
789 synchronized (mLock) {
790 actualCacheOwner = resolveOwningUserIdForSystemSettingLocked(userId,
791 cacheRingtoneSetting);
792 }
793 final File cacheFile = new File(getRingtoneCacheDir(actualCacheOwner), cacheName);
Jeff Sharkey413573a2016-02-22 17:52:45 -0700794 return ParcelFileDescriptor.open(cacheFile, ParcelFileDescriptor.parseMode(mode));
795 }
796
797 private File getRingtoneCacheDir(int userId) {
798 final File cacheDir = new File(Environment.getDataSystemDeDirectory(userId), "ringtones");
799 cacheDir.mkdir();
800 SELinux.restorecon(cacheDir);
801 return cacheDir;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700802 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800803
Eugene Suslad72c3972016-12-27 15:49:30 -0800804 /**
805 * Dump all settings as a proto buf.
806 *
807 * @param fd The file to dump to
808 */
809 void dumpProto(@NonNull FileDescriptor fd) {
810 ProtoOutputStream proto = new ProtoOutputStream(fd);
811
812 synchronized (mLock) {
813 SettingsProtoDumpUtil.dumpProtoLocked(mSettingsRegistry, proto);
Eugene Suslad72c3972016-12-27 15:49:30 -0800814 }
815
816 proto.flush();
817 }
818
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700819 public void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800820 synchronized (mLock) {
821 final long identity = Binder.clearCallingIdentity();
822 try {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700823 SparseBooleanArray users = mSettingsRegistry.getKnownUsersLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800824 final int userCount = users.size();
825 for (int i = 0; i < userCount; i++) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700826 dumpForUserLocked(users.keyAt(i), pw);
Svetoslavb505ccc2015-02-17 12:41:04 -0800827 }
828 } finally {
829 Binder.restoreCallingIdentity(identity);
830 }
831 }
832 }
833
Andreas Gampeb58893072018-09-05 16:52:31 -0700834 @GuardedBy("mLock")
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700835 private void dumpForUserLocked(int userId, PrintWriter pw) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700836 if (userId == UserHandle.USER_SYSTEM) {
Matt Pape1b31a332018-10-17 09:58:28 -0700837 pw.println("CONFIG SETTINGS (user " + userId + ")");
838 SettingsState configSettings = mSettingsRegistry.getSettingsLocked(
839 SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM);
840 if (configSettings != null) {
841 dumpSettingsLocked(configSettings, pw);
842 pw.println();
843 configSettings.dumpHistoricalOperations(pw);
844 }
845
Svetoslavb505ccc2015-02-17 12:41:04 -0800846 pw.println("GLOBAL SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700847 SettingsState globalSettings = mSettingsRegistry.getSettingsLocked(
848 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700849 if (globalSettings != null) {
850 dumpSettingsLocked(globalSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800851 pw.println();
852 globalSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700853 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800854 }
855
856 pw.println("SECURE SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700857 SettingsState secureSettings = mSettingsRegistry.getSettingsLocked(
858 SETTINGS_TYPE_SECURE, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700859 if (secureSettings != null) {
860 dumpSettingsLocked(secureSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800861 pw.println();
862 secureSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700863 }
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700864
Svetoslavb505ccc2015-02-17 12:41:04 -0800865 pw.println("SYSTEM SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700866 SettingsState systemSettings = mSettingsRegistry.getSettingsLocked(
867 SETTINGS_TYPE_SYSTEM, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700868 if (systemSettings != null) {
869 dumpSettingsLocked(systemSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800870 pw.println();
871 systemSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700872 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800873 }
874
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700875 private void dumpSettingsLocked(SettingsState settingsState, PrintWriter pw) {
876 List<String> names = settingsState.getSettingNamesLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800877
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700878 final int nameCount = names.size();
Svetoslavb505ccc2015-02-17 12:41:04 -0800879
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700880 for (int i = 0; i < nameCount; i++) {
881 String name = names.get(i);
882 Setting setting = settingsState.getSettingLocked(name);
883 pw.print("_id:"); pw.print(toDumpString(setting.getId()));
884 pw.print(" name:"); pw.print(toDumpString(name));
885 if (setting.getPackageName() != null) {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800886 pw.print(" pkg:"); pw.print(setting.getPackageName());
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700887 }
888 pw.print(" value:"); pw.print(toDumpString(setting.getValue()));
Svetoslav Ganove080da92016-12-21 17:10:35 -0800889 if (setting.getDefaultValue() != null) {
890 pw.print(" default:"); pw.print(setting.getDefaultValue());
Eugene Suslad72c3972016-12-27 15:49:30 -0800891 pw.print(" defaultSystemSet:"); pw.print(setting.isDefaultFromSystem());
Svetoslav Ganove080da92016-12-21 17:10:35 -0800892 }
893 if (setting.getTag() != null) {
894 pw.print(" tag:"); pw.print(setting.getTag());
895 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800896 pw.println();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700897 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800898 }
899
Svetoslav7e0683b2015-08-03 16:02:52 -0700900 private static String toDumpString(String s) {
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700901 if (s != null) {
902 return s;
903 }
904 return "{null}";
905 }
906
Svetoslav683914b2015-01-15 14:22:26 -0800907 private void registerBroadcastReceivers() {
908 IntentFilter userFilter = new IntentFilter();
909 userFilter.addAction(Intent.ACTION_USER_REMOVED);
910 userFilter.addAction(Intent.ACTION_USER_STOPPED);
911
912 getContext().registerReceiver(new BroadcastReceiver() {
913 @Override
914 public void onReceive(Context context, Intent intent) {
915 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700916 UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800917
918 switch (intent.getAction()) {
919 case Intent.ACTION_USER_REMOVED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700920 synchronized (mLock) {
921 mSettingsRegistry.removeUserStateLocked(userId, true);
922 }
Svetoslav683914b2015-01-15 14:22:26 -0800923 } break;
924
925 case Intent.ACTION_USER_STOPPED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700926 synchronized (mLock) {
927 mSettingsRegistry.removeUserStateLocked(userId, false);
928 }
Svetoslav683914b2015-01-15 14:22:26 -0800929 } break;
930 }
931 }
932 }, userFilter);
933
934 PackageMonitor monitor = new PackageMonitor() {
935 @Override
936 public void onPackageRemoved(String packageName, int uid) {
937 synchronized (mLock) {
Zimuzoc56192c2018-07-25 10:40:01 +0100938 mSettingsRegistry.removeSettingsForPackageLocked(packageName,
Svetoslav683914b2015-01-15 14:22:26 -0800939 UserHandle.getUserId(uid));
940 }
941 }
Mark Rathjend891f012017-01-19 04:10:37 +0000942
943 @Override
944 public void onUidRemoved(int uid) {
945 synchronized (mLock) {
946 mSettingsRegistry.onUidRemovedLocked(uid);
947 }
948 }
Zimuzoc56192c2018-07-25 10:40:01 +0100949
950 @Override
951 public void onPackageDataCleared(String packageName, int uid) {
952 synchronized (mLock) {
953 mSettingsRegistry.removeSettingsForPackageLocked(packageName,
954 UserHandle.getUserId(uid));
955 }
956 }
Svetoslav683914b2015-01-15 14:22:26 -0800957 };
958
959 // package changes
960 monitor.register(getContext(), BackgroundThread.getHandler().getLooper(),
961 UserHandle.ALL, true);
962 }
963
Svet Ganov53a441c2016-04-19 19:38:00 -0700964 private void startWatchingUserRestrictionChanges() {
965 // TODO: The current design of settings looking different based on user restrictions
966 // should be reworked to keep them separate and system code should check the setting
967 // first followed by checking the user restriction before performing an operation.
968 UserManagerInternal userManager = LocalServices.getService(UserManagerInternal.class);
969 userManager.addUserRestrictionsListener((int userId, Bundle newRestrictions,
970 Bundle prevRestrictions) -> {
Irina Dumitrescue3696872019-01-09 16:07:59 +0000971 Set<String> changedRestrictions = getRestrictionDiff(prevRestrictions, newRestrictions);
Svet Ganov53a441c2016-04-19 19:38:00 -0700972 // We are changing the settings affected by restrictions to their current
973 // value with a forced update to ensure that all cross profile dependencies
974 // are taken into account. Also make sure the settings update to.. the same
975 // value passes the security checks, so clear binder calling id.
Irina Dumitrescue3696872019-01-09 16:07:59 +0000976 if (changedRestrictions.contains(UserManager.DISALLOW_SHARE_LOCATION)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700977 final long identity = Binder.clearCallingIdentity();
978 try {
979 synchronized (mLock) {
980 Setting setting = getSecureSetting(
Soonil Nagarkar4ee3ac22019-02-08 19:19:24 -0800981 Settings.Secure.LOCATION_MODE, userId);
982 updateSecureSetting(Settings.Secure.LOCATION_MODE,
983 setting != null ? setting.getValue() : null, null,
984 true, userId, true);
985 setting = getSecureSetting(
Svet Ganov53a441c2016-04-19 19:38:00 -0700986 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, userId);
987 updateSecureSetting(Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800988 setting != null ? setting.getValue() : null, null,
989 true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700990 }
991 } finally {
992 Binder.restoreCallingIdentity(identity);
993 }
994 }
Irina Dumitrescue3696872019-01-09 16:07:59 +0000995 if (changedRestrictions.contains(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)
996 || changedRestrictions.contains(
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100997 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700998 final long identity = Binder.clearCallingIdentity();
999 try {
1000 synchronized (mLock) {
1001 Setting setting = getGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS);
Svetoslav Ganove080da92016-12-21 17:10:35 -08001002 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -07001003 updateGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001004 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -07001005 }
1006 } finally {
1007 Binder.restoreCallingIdentity(identity);
1008 }
1009 }
Irina Dumitrescue3696872019-01-09 16:07:59 +00001010 if (changedRestrictions.contains(UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001011 final long identity = Binder.clearCallingIdentity();
1012 try {
1013 synchronized (mLock) {
1014 Setting setting = getGlobalSetting(Settings.Global.ADB_ENABLED);
Svetoslav Ganove080da92016-12-21 17:10:35 -08001015 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -07001016 updateGlobalSetting(Settings.Global.ADB_ENABLED,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001017 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -07001018 }
1019 } finally {
1020 Binder.restoreCallingIdentity(identity);
1021 }
1022 }
Irina Dumitrescue3696872019-01-09 16:07:59 +00001023 if (changedRestrictions.contains(UserManager.ENSURE_VERIFY_APPS)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001024 final long identity = Binder.clearCallingIdentity();
1025 try {
1026 synchronized (mLock) {
1027 Setting enable = getGlobalSetting(
1028 Settings.Global.PACKAGE_VERIFIER_ENABLE);
Svetoslav Ganove080da92016-12-21 17:10:35 -08001029 String enableValue = enable != null ? enable.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -07001030 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_ENABLE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001031 enableValue, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -07001032 Setting include = getGlobalSetting(
1033 Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB);
Svetoslav Ganove080da92016-12-21 17:10:35 -08001034 String includeValue = include != null ? include.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -07001035 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001036 includeValue, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -07001037 }
1038 } finally {
1039 Binder.restoreCallingIdentity(identity);
1040 }
1041 }
Irina Dumitrescue3696872019-01-09 16:07:59 +00001042 if (changedRestrictions.contains(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001043 final long identity = Binder.clearCallingIdentity();
1044 try {
1045 synchronized (mLock) {
1046 Setting setting = getGlobalSetting(
1047 Settings.Global.PREFERRED_NETWORK_MODE);
Svetoslav Ganove080da92016-12-21 17:10:35 -08001048 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -07001049 updateGlobalSetting(Settings.Global.PREFERRED_NETWORK_MODE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001050 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -07001051 }
1052 } finally {
1053 Binder.restoreCallingIdentity(identity);
1054 }
1055 }
1056 });
1057 }
1058
Irina Dumitrescue3696872019-01-09 16:07:59 +00001059 private static Set<String> getRestrictionDiff(Bundle prevRestrictions, Bundle newRestrictions) {
1060 Set<String> restrictionNames = Sets.newArraySet();
1061 restrictionNames.addAll(prevRestrictions.keySet());
1062 restrictionNames.addAll(newRestrictions.keySet());
1063 Set<String> diff = Sets.newArraySet();
1064 for (String restrictionName : restrictionNames) {
1065 if (prevRestrictions.getBoolean(restrictionName) != newRestrictions.getBoolean(
1066 restrictionName)) {
1067 diff.add(restrictionName);
1068 }
1069 }
1070 return diff;
1071 }
1072
Matt Pape1b31a332018-10-17 09:58:28 -07001073 private Setting getConfigSetting(String name) {
1074 if (DEBUG) {
1075 Slog.v(LOG_TAG, "getConfigSetting(" + name + ")");
1076 }
1077
Stanislav Zholnin55799502019-03-08 14:54:55 +00001078 DeviceConfig.enforceReadPermission(getContext(), /*namespace=*/name.split("/")[0]);
Matt Pape1b31a332018-10-17 09:58:28 -07001079
1080 // Get the value.
1081 synchronized (mLock) {
1082 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_CONFIG,
1083 UserHandle.USER_SYSTEM, name);
1084 }
1085 }
1086
Matt Papec1323dc2018-12-11 12:32:42 -08001087 private boolean insertConfigSetting(String name, String value, boolean makeDefault) {
Matt Pape1b31a332018-10-17 09:58:28 -07001088 if (DEBUG) {
1089 Slog.v(LOG_TAG, "insertConfigSetting(" + name + ", " + value + ", "
Matt Papec1323dc2018-12-11 12:32:42 -08001090 + makeDefault + ")");
Matt Pape1b31a332018-10-17 09:58:28 -07001091 }
Matt Papec1323dc2018-12-11 12:32:42 -08001092 return mutateConfigSetting(name, value, null, makeDefault,
1093 MUTATION_OPERATION_INSERT, 0);
Matt Pape1b31a332018-10-17 09:58:28 -07001094 }
1095
Matt Papec1323dc2018-12-11 12:32:42 -08001096 private boolean deleteConfigSetting(String name) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08001097 if (DEBUG) {
Matt Papec1323dc2018-12-11 12:32:42 -08001098 Slog.v(LOG_TAG, "deleteConfigSetting(" + name + ")");
Matt Pape6bfc62e2018-11-28 13:16:03 -08001099 }
Matt Papec1323dc2018-12-11 12:32:42 -08001100 return mutateConfigSetting(name, null, null, false,
1101 MUTATION_OPERATION_DELETE, 0);
Matt Pape6bfc62e2018-11-28 13:16:03 -08001102 }
1103
Matt Papec1323dc2018-12-11 12:32:42 -08001104 private void resetConfigSetting(int mode, String prefix) {
Matt Pape1b31a332018-10-17 09:58:28 -07001105 if (DEBUG) {
Matt Papec1323dc2018-12-11 12:32:42 -08001106 Slog.v(LOG_TAG, "resetConfigSetting(" + mode + ", " + prefix + ")");
Matt Pape1b31a332018-10-17 09:58:28 -07001107 }
Matt Papec1323dc2018-12-11 12:32:42 -08001108 mutateConfigSetting(null, null, prefix, false,
1109 MUTATION_OPERATION_RESET, mode);
Matt Pape1b31a332018-10-17 09:58:28 -07001110 }
1111
Matt Pape6bfc62e2018-11-28 13:16:03 -08001112 private boolean mutateConfigSetting(String name, String value, String prefix,
Matt Papec1323dc2018-12-11 12:32:42 -08001113 boolean makeDefault, int operation, int mode) {
Stanislav Zholnin5a25a842019-03-13 14:43:26 +00001114 enforceWritePermission(Manifest.permission.WRITE_DEVICE_CONFIG);
Matt Pape1b31a332018-10-17 09:58:28 -07001115
Matt Pape1b31a332018-10-17 09:58:28 -07001116 // Perform the mutation.
1117 synchronized (mLock) {
1118 switch (operation) {
1119 case MUTATION_OPERATION_INSERT: {
1120 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_CONFIG,
Matt Papec1323dc2018-12-11 12:32:42 -08001121 UserHandle.USER_SYSTEM, name, value, null, makeDefault, true,
Matt Pape7b1c6cd2019-01-04 08:10:41 -08001122 resolveCallingPackage(), false, null);
Matt Pape1b31a332018-10-17 09:58:28 -07001123 }
1124
Matt Pape6bfc62e2018-11-28 13:16:03 -08001125 case MUTATION_OPERATION_DELETE: {
1126 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_CONFIG,
Matt Papec1323dc2018-12-11 12:32:42 -08001127 UserHandle.USER_SYSTEM, name, false, null);
Matt Pape6bfc62e2018-11-28 13:16:03 -08001128 }
1129
Matt Pape1b31a332018-10-17 09:58:28 -07001130 case MUTATION_OPERATION_RESET: {
1131 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_CONFIG,
Matt Pape7b1c6cd2019-01-04 08:10:41 -08001132 UserHandle.USER_SYSTEM, resolveCallingPackage(), mode, null, prefix);
Matt Pape1b31a332018-10-17 09:58:28 -07001133 } return true;
1134 }
1135 }
1136
1137 return false;
1138 }
1139
Matt Pape6bfc62e2018-11-28 13:16:03 -08001140 private Map<String, String> getAllConfigFlags(@Nullable String prefix) {
1141 if (DEBUG) {
1142 Slog.v(LOG_TAG, "getAllConfigFlags() for " + prefix);
1143 }
1144
1145 synchronized (mLock) {
1146 // Get the settings.
1147 SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
1148 SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM);
1149
1150 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_CONFIG,
1151 UserHandle.USER_SYSTEM);
1152
1153 final int nameCount = names.size();
1154 Map<String, String> flagsToValues = new HashMap<>(names.size());
1155
1156 for (int i = 0; i < nameCount; i++) {
1157 String name = names.get(i);
1158 Setting setting = settingsState.getSettingLocked(name);
1159 if (prefix == null || setting.getName().startsWith(prefix)) {
1160 flagsToValues.put(setting.getName(), setting.getValue());
1161 }
1162 }
1163
1164 return flagsToValues;
1165 }
1166 }
1167
Svetoslav7ec28e82015-05-20 17:01:10 -07001168 private Cursor getAllGlobalSettings(String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001169 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001170 Slog.v(LOG_TAG, "getAllGlobalSettings()");
Svetoslav683914b2015-01-15 14:22:26 -08001171 }
1172
Svetoslav7ec28e82015-05-20 17:01:10 -07001173 synchronized (mLock) {
1174 // Get the settings.
1175 SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001176 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001177
Chad Brubaker97bccee2017-01-05 15:51:41 -08001178 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_GLOBAL,
1179 UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001180
Svetoslav7ec28e82015-05-20 17:01:10 -07001181 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001182
Svetoslav7ec28e82015-05-20 17:01:10 -07001183 String[] normalizedProjection = normalizeProjection(projection);
1184 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001185
Svetoslav7ec28e82015-05-20 17:01:10 -07001186 // Anyone can get the global settings, so no security checks.
1187 for (int i = 0; i < nameCount; i++) {
1188 String name = names.get(i);
1189 Setting setting = settingsState.getSettingLocked(name);
1190 appendSettingToCursor(result, setting);
1191 }
1192
1193 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001194 }
Svetoslav683914b2015-01-15 14:22:26 -08001195 }
1196
Svetoslav7ec28e82015-05-20 17:01:10 -07001197 private Setting getGlobalSetting(String name) {
Svetoslav683914b2015-01-15 14:22:26 -08001198 if (DEBUG) {
1199 Slog.v(LOG_TAG, "getGlobalSetting(" + name + ")");
1200 }
1201
Chad Brubakera6830e72017-04-28 17:34:36 -07001202 // Ensure the caller can access the setting.
1203 enforceSettingReadable(name, SETTINGS_TYPE_GLOBAL, UserHandle.getCallingUserId());
1204
Svetoslav683914b2015-01-15 14:22:26 -08001205 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001206 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -07001207 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_GLOBAL,
Xiaohui Chen43765b72015-08-31 10:57:33 -07001208 UserHandle.USER_SYSTEM, name);
Svetoslav683914b2015-01-15 14:22:26 -08001209 }
Svetoslav683914b2015-01-15 14:22:26 -08001210 }
1211
Svetoslav Ganove080da92016-12-21 17:10:35 -08001212 private boolean updateGlobalSetting(String name, String value, String tag,
1213 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001214 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001215 Slog.v(LOG_TAG, "updateGlobalSetting(" + name + ", " + value + ", "
1216 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1217 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001218 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001219 return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId,
1220 MUTATION_OPERATION_UPDATE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001221 }
1222
Svetoslav Ganove080da92016-12-21 17:10:35 -08001223 private boolean insertGlobalSetting(String name, String value, String tag,
1224 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001225 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001226 Slog.v(LOG_TAG, "insertGlobalSetting(" + name + ", " + value + ", "
1227 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1228 + ", " + forceNotify + ")");
Svetoslav7ec28e82015-05-20 17:01:10 -07001229 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001230 return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId,
1231 MUTATION_OPERATION_INSERT, forceNotify, 0);
Svetoslav7ec28e82015-05-20 17:01:10 -07001232 }
1233
Svet Ganov53a441c2016-04-19 19:38:00 -07001234 private boolean deleteGlobalSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001235 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001236 Slog.v(LOG_TAG, "deleteGlobalSetting(" + name + ", " + requestingUserId
1237 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001238 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001239 return mutateGlobalSetting(name, null, null, false, requestingUserId,
1240 MUTATION_OPERATION_DELETE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001241 }
1242
Svetoslav Ganove080da92016-12-21 17:10:35 -08001243 private void resetGlobalSetting(int requestingUserId, int mode, String tag) {
1244 if (DEBUG) {
1245 Slog.v(LOG_TAG, "resetGlobalSetting(" + requestingUserId + ", "
1246 + mode + ", " + tag + ")");
1247 }
1248 mutateGlobalSetting(null, null, tag, false, requestingUserId,
1249 MUTATION_OPERATION_RESET, false, mode);
1250 }
1251
1252 private boolean mutateGlobalSetting(String name, String value, String tag,
1253 boolean makeDefault, int requestingUserId, int operation, boolean forceNotify,
1254 int mode) {
Svetoslav683914b2015-01-15 14:22:26 -08001255 // Make sure the caller can change the settings - treated as secure.
1256 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
1257
Svetoslav683914b2015-01-15 14:22:26 -08001258 // Resolve the userId on whose behalf the call is made.
1259 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1260
Makoto Onuki28da2e32015-11-20 11:30:44 -08001261 // If this is a setting that is currently restricted for this user, do not allow
1262 // unrestricting changes.
yuemingw1d13eae2018-01-30 17:27:54 +00001263 if (name != null && mUserManagerInternal.isSettingRestrictedForUser(
1264 name, callingUserId, value, Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -08001265 return false;
1266 }
1267
1268 // Perform the mutation.
Svetoslav7ec28e82015-05-20 17:01:10 -07001269 synchronized (mLock) {
1270 switch (operation) {
1271 case MUTATION_OPERATION_INSERT: {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001272 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_GLOBAL,
1273 UserHandle.USER_SYSTEM, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001274 getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001275 }
Svetoslav683914b2015-01-15 14:22:26 -08001276
Svetoslav7ec28e82015-05-20 17:01:10 -07001277 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001278 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_GLOBAL,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001279 UserHandle.USER_SYSTEM, name, forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001280 }
Svetoslav683914b2015-01-15 14:22:26 -08001281
Svetoslav7ec28e82015-05-20 17:01:10 -07001282 case MUTATION_OPERATION_UPDATE: {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001283 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_GLOBAL,
1284 UserHandle.USER_SYSTEM, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001285 getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001286 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001287
1288 case MUTATION_OPERATION_RESET: {
1289 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_GLOBAL,
1290 UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag);
1291 } return true;
Svetoslav683914b2015-01-15 14:22:26 -08001292 }
1293 }
1294
1295 return false;
1296 }
1297
Christopher Tateb218e762017-04-05 16:34:07 -07001298 private PackageInfo getCallingPackageInfo(int userId) {
1299 try {
1300 return mPackageManager.getPackageInfo(getCallingPackage(),
1301 PackageManager.GET_SIGNATURES, userId);
1302 } catch (RemoteException e) {
1303 throw new IllegalStateException("Package " + getCallingPackage() + " doesn't exist");
1304 }
1305 }
1306
Svetoslav7ec28e82015-05-20 17:01:10 -07001307 private Cursor getAllSecureSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001308 if (DEBUG) {
1309 Slog.v(LOG_TAG, "getAllSecureSettings(" + userId + ")");
1310 }
1311
1312 // Resolve the userId on whose behalf the call is made.
1313 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
1314
Christopher Tateb218e762017-04-05 16:34:07 -07001315 // The relevant "calling package" userId will be the owning userId for some
1316 // profiles, and we can't do the lookup inside our [lock held] loop, so work out
1317 // up front who the effective "new SSAID" user ID for that settings name will be.
1318 final int ssaidUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
1319 Settings.Secure.ANDROID_ID);
1320 final PackageInfo ssaidCallingPkg = getCallingPackageInfo(ssaidUserId);
1321
Svetoslav7ec28e82015-05-20 17:01:10 -07001322 synchronized (mLock) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001323 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SECURE, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -08001324
Svetoslav7ec28e82015-05-20 17:01:10 -07001325 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001326
Svetoslav7ec28e82015-05-20 17:01:10 -07001327 String[] normalizedProjection = normalizeProjection(projection);
1328 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001329
Svetoslav7ec28e82015-05-20 17:01:10 -07001330 for (int i = 0; i < nameCount; i++) {
1331 String name = names.get(i);
1332 // Determine the owning user as some profile settings are cloned from the parent.
1333 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
1334 name);
Svetoslav683914b2015-01-15 14:22:26 -08001335
Alex Klyubin1991f572017-03-03 14:08:36 -08001336 if (!isSecureSettingAccessible(name, callingUserId, owningUserId)) {
1337 // This caller is not permitted to access this setting. Pretend the setting
1338 // doesn't exist.
Svetoslav Ganov83a1f7f2016-04-27 13:50:49 -07001339 continue;
Svetoslav7ec28e82015-05-20 17:01:10 -07001340 }
Svetoslav683914b2015-01-15 14:22:26 -08001341
Mark Rathjen7599f132017-01-23 14:15:54 -08001342 // As of Android O, the SSAID is read from an app-specific entry in table
Mark Rathjend891f012017-01-19 04:10:37 +00001343 // SETTINGS_FILE_SSAID, unless accessed by a system process.
1344 final Setting setting;
1345 if (isNewSsaidSetting(name)) {
Christopher Tateb218e762017-04-05 16:34:07 -07001346 setting = getSsaidSettingLocked(ssaidCallingPkg, owningUserId);
Mark Rathjend891f012017-01-19 04:10:37 +00001347 } else {
1348 setting = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE, owningUserId,
1349 name);
1350 }
Svetoslav7ec28e82015-05-20 17:01:10 -07001351 appendSettingToCursor(result, setting);
Svetoslav683914b2015-01-15 14:22:26 -08001352 }
1353
Svetoslav7ec28e82015-05-20 17:01:10 -07001354 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001355 }
Svetoslav683914b2015-01-15 14:22:26 -08001356 }
1357
Svetoslav7ec28e82015-05-20 17:01:10 -07001358 private Setting getSecureSetting(String name, int requestingUserId) {
Makoto Onuki0000d322017-11-28 16:31:47 -08001359 return getSecureSetting(name, requestingUserId, /*enableOverride=*/ false);
1360 }
1361
1362 private Setting getSecureSetting(String name, int requestingUserId, boolean enableOverride) {
Svetoslav683914b2015-01-15 14:22:26 -08001363 if (DEBUG) {
1364 Slog.v(LOG_TAG, "getSecureSetting(" + name + ", " + requestingUserId + ")");
1365 }
1366
1367 // Resolve the userId on whose behalf the call is made.
1368 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1369
Chad Brubakera6830e72017-04-28 17:34:36 -07001370 // Ensure the caller can access the setting.
1371 enforceSettingReadable(name, SETTINGS_TYPE_SECURE, UserHandle.getCallingUserId());
1372
Svetoslav683914b2015-01-15 14:22:26 -08001373 // Determine the owning user as some profile settings are cloned from the parent.
1374 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
1375
Alex Klyubin1991f572017-03-03 14:08:36 -08001376 if (!isSecureSettingAccessible(name, callingUserId, owningUserId)) {
1377 // This caller is not permitted to access this setting. Pretend the setting doesn't
1378 // exist.
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001379 SettingsState settings = mSettingsRegistry.getSettingsLocked(SETTINGS_TYPE_SECURE,
1380 owningUserId);
1381 return settings != null ? settings.getNullSetting() : null;
Svetoslav683914b2015-01-15 14:22:26 -08001382 }
1383
Christopher Tateb218e762017-04-05 16:34:07 -07001384 // As of Android O, the SSAID is read from an app-specific entry in table
1385 // SETTINGS_FILE_SSAID, unless accessed by a system process.
1386 if (isNewSsaidSetting(name)) {
1387 PackageInfo callingPkg = getCallingPackageInfo(owningUserId);
1388 synchronized (mLock) {
1389 return getSsaidSettingLocked(callingPkg, owningUserId);
Mark Rathjend891f012017-01-19 04:10:37 +00001390 }
Christopher Tateb218e762017-04-05 16:34:07 -07001391 }
Makoto Onuki0000d322017-11-28 16:31:47 -08001392 if (enableOverride) {
Kweku Adams5e0052b2019-02-22 15:17:52 -08001393 if (Secure.LOCATION_MODE.equals(name)) {
1394 final Setting overridden = getLocationModeSetting(owningUserId);
Makoto Onuki0000d322017-11-28 16:31:47 -08001395 if (overridden != null) {
1396 return overridden;
1397 }
1398 }
1399 }
Mark Rathjend891f012017-01-19 04:10:37 +00001400
Christopher Tateb218e762017-04-05 16:34:07 -07001401 // Not the SSAID; do a straight lookup
1402 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -07001403 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav7ec28e82015-05-20 17:01:10 -07001404 owningUserId, name);
1405 }
Svetoslav683914b2015-01-15 14:22:26 -08001406 }
1407
Mark Rathjend891f012017-01-19 04:10:37 +00001408 private boolean isNewSsaidSetting(String name) {
1409 return Settings.Secure.ANDROID_ID.equals(name)
1410 && UserHandle.getAppId(Binder.getCallingUid()) >= Process.FIRST_APPLICATION_UID;
1411 }
1412
Andreas Gampeb58893072018-09-05 16:52:31 -07001413 @GuardedBy("mLock")
Christopher Tateb218e762017-04-05 16:34:07 -07001414 private Setting getSsaidSettingLocked(PackageInfo callingPkg, int owningUserId) {
Mark Rathjend891f012017-01-19 04:10:37 +00001415 // Get uid of caller (key) used to store ssaid value
1416 String name = Integer.toString(
1417 UserHandle.getUid(owningUserId, UserHandle.getAppId(Binder.getCallingUid())));
1418
1419 if (DEBUG) {
1420 Slog.v(LOG_TAG, "getSsaidSettingLocked(" + name + "," + owningUserId + ")");
1421 }
1422
1423 // Retrieve the ssaid from the table if present.
1424 final Setting ssaid = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SSAID, owningUserId,
1425 name);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001426 // If the app is an Instant App use its stored SSAID instead of our own.
1427 final String instantSsaid;
1428 final long token = Binder.clearCallingIdentity();
1429 try {
1430 instantSsaid = mPackageManager.getInstantAppAndroidId(callingPkg.packageName,
1431 owningUserId);
1432 } catch (RemoteException e) {
1433 Slog.e(LOG_TAG, "Failed to get Instant App Android ID", e);
1434 return null;
1435 } finally {
1436 Binder.restoreCallingIdentity(token);
1437 }
Svet Ganov96c99462017-05-05 14:27:13 -07001438
1439 final SettingsState ssaidSettings = mSettingsRegistry.getSettingsLocked(
1440 SETTINGS_TYPE_SSAID, owningUserId);
1441
Chad Brubaker0d277a72017-04-12 16:56:53 -07001442 if (instantSsaid != null) {
1443 // Use the stored value if it is still valid.
1444 if (ssaid != null && instantSsaid.equals(ssaid.getValue())) {
Svet Ganov96c99462017-05-05 14:27:13 -07001445 return mascaradeSsaidSetting(ssaidSettings, ssaid);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001446 }
1447 // The value has changed, update the stored value.
Chad Brubaker0d277a72017-04-12 16:56:53 -07001448 final boolean success = ssaidSettings.insertSettingLocked(name, instantSsaid, null,
1449 true, callingPkg.packageName);
1450 if (!success) {
1451 throw new IllegalStateException("Failed to update instant app android id");
1452 }
Svet Ganov96c99462017-05-05 14:27:13 -07001453 Setting setting = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SSAID,
1454 owningUserId, name);
1455 return mascaradeSsaidSetting(ssaidSettings, setting);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001456 }
Mark Rathjend891f012017-01-19 04:10:37 +00001457
1458 // Lazy initialize ssaid if not yet present in ssaid table.
Mark Rathjenea617592017-01-18 23:03:41 -08001459 if (ssaid == null || ssaid.isNull() || ssaid.getValue() == null) {
Svet Ganov96c99462017-05-05 14:27:13 -07001460 Setting setting = mSettingsRegistry.generateSsaidLocked(callingPkg, owningUserId);
1461 return mascaradeSsaidSetting(ssaidSettings, setting);
Mark Rathjend891f012017-01-19 04:10:37 +00001462 }
1463
Svet Ganov96c99462017-05-05 14:27:13 -07001464 return mascaradeSsaidSetting(ssaidSettings, ssaid);
1465 }
1466
1467 private Setting mascaradeSsaidSetting(SettingsState settingsState, Setting ssaidSetting) {
1468 // SSAID settings are located in a dedicated table for internal bookkeeping
1469 // but for the world they reside in the secure table, so adjust the key here.
1470 // We have a special name when looking it up but want the world to see it as
1471 // "android_id".
1472 if (ssaidSetting != null) {
1473 return settingsState.new Setting(ssaidSetting) {
1474 @Override
1475 public int getKey() {
1476 final int userId = getUserIdFromKey(super.getKey());
1477 return makeKey(SETTINGS_TYPE_SECURE, userId);
1478 }
1479
1480 @Override
1481 public String getName() {
1482 return Settings.Secure.ANDROID_ID;
1483 }
1484 };
1485 }
1486 return null;
Mark Rathjend891f012017-01-19 04:10:37 +00001487 }
1488
Kweku Adams5e0052b2019-02-22 15:17:52 -08001489 private Setting getLocationModeSetting(int owningUserId) {
Makoto Onuki0000d322017-11-28 16:31:47 -08001490 synchronized (mLock) {
1491 final Setting setting = getGlobalSetting(
1492 Global.LOCATION_GLOBAL_KILL_SWITCH);
1493 if (!"1".equals(setting.getValue())) {
1494 return null;
1495 }
1496 // Global kill-switch is enabled. Return an empty value.
1497 final SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
1498 SETTINGS_TYPE_SECURE, owningUserId);
1499 return settingsState.new Setting(
Kweku Adams5e0052b2019-02-22 15:17:52 -08001500 Secure.LOCATION_MODE,
Makoto Onuki0000d322017-11-28 16:31:47 -08001501 "", // value
1502 "", // tag
1503 "", // default value
1504 "", // package name
1505 false, // from system
1506 "0" // id
1507 ) {
1508 @Override
1509 public boolean update(String value, boolean setDefault, String packageName,
1510 String tag, boolean forceNonSystemPackage) {
Kweku Adams5e0052b2019-02-22 15:17:52 -08001511 Slog.wtf(LOG_TAG, "update shouldn't be called on this instance.");
Makoto Onuki0000d322017-11-28 16:31:47 -08001512 return false;
1513 }
1514 };
1515 }
1516 }
1517
Svetoslav Ganove080da92016-12-21 17:10:35 -08001518 private boolean insertSecureSetting(String name, String value, String tag,
1519 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001520 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001521 Slog.v(LOG_TAG, "insertSecureSetting(" + name + ", " + value + ", "
Svetoslav Ganove080da92016-12-21 17:10:35 -08001522 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1523 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001524 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001525 return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId,
1526 MUTATION_OPERATION_INSERT, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001527 }
1528
Svet Ganov53a441c2016-04-19 19:38:00 -07001529 private boolean deleteSecureSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001530 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001531 Slog.v(LOG_TAG, "deleteSecureSetting(" + name + ", " + requestingUserId
1532 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001533 }
1534
Svetoslav Ganove080da92016-12-21 17:10:35 -08001535 return mutateSecureSetting(name, null, null, false, requestingUserId,
1536 MUTATION_OPERATION_DELETE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001537 }
1538
Svetoslav Ganove080da92016-12-21 17:10:35 -08001539 private boolean updateSecureSetting(String name, String value, String tag,
1540 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001541 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001542 Slog.v(LOG_TAG, "updateSecureSetting(" + name + ", " + value + ", "
Svetoslav Ganove080da92016-12-21 17:10:35 -08001543 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1544 + ", " + forceNotify +")");
Svetoslav683914b2015-01-15 14:22:26 -08001545 }
1546
Svetoslav Ganove080da92016-12-21 17:10:35 -08001547 return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId,
1548 MUTATION_OPERATION_UPDATE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001549 }
1550
Svetoslav Ganove080da92016-12-21 17:10:35 -08001551 private void resetSecureSetting(int requestingUserId, int mode, String tag) {
1552 if (DEBUG) {
1553 Slog.v(LOG_TAG, "resetSecureSetting(" + requestingUserId + ", "
1554 + mode + ", " + tag + ")");
1555 }
1556
1557 mutateSecureSetting(null, null, tag, false, requestingUserId,
1558 MUTATION_OPERATION_RESET, false, mode);
1559 }
1560
1561 private boolean mutateSecureSetting(String name, String value, String tag,
1562 boolean makeDefault, int requestingUserId, int operation, boolean forceNotify,
1563 int mode) {
Svetoslav683914b2015-01-15 14:22:26 -08001564 // Make sure the caller can change the settings.
1565 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
1566
Svetoslav683914b2015-01-15 14:22:26 -08001567 // Resolve the userId on whose behalf the call is made.
1568 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1569
Makoto Onuki28da2e32015-11-20 11:30:44 -08001570 // If this is a setting that is currently restricted for this user, do not allow
1571 // unrestricting changes.
yuemingw1d13eae2018-01-30 17:27:54 +00001572 if (name != null && mUserManagerInternal.isSettingRestrictedForUser(
1573 name, callingUserId, value, Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -08001574 return false;
1575 }
1576
1577 // Determine the owning user as some profile settings are cloned from the parent.
1578 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
1579
1580 // Only the owning user can change the setting.
1581 if (owningUserId != callingUserId) {
1582 return false;
1583 }
1584
1585 // Special cases for location providers (sigh).
1586 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001587 return updateLocationProvidersAllowedLocked(value, tag, owningUserId, makeDefault,
1588 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -08001589 }
1590
1591 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001592 synchronized (mLock) {
1593 switch (operation) {
1594 case MUTATION_OPERATION_INSERT: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001595 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001596 owningUserId, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001597 getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001598 }
Svetoslav683914b2015-01-15 14:22:26 -08001599
Svetoslav7ec28e82015-05-20 17:01:10 -07001600 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001601 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001602 owningUserId, name, forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001603 }
Svetoslav683914b2015-01-15 14:22:26 -08001604
Svetoslav7ec28e82015-05-20 17:01:10 -07001605 case MUTATION_OPERATION_UPDATE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001606 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001607 owningUserId, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001608 getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001609 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001610
1611 case MUTATION_OPERATION_RESET: {
1612 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_SECURE,
1613 UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag);
1614 } return true;
Svetoslav683914b2015-01-15 14:22:26 -08001615 }
1616 }
1617
1618 return false;
1619 }
1620
Svetoslav7ec28e82015-05-20 17:01:10 -07001621 private Cursor getAllSystemSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001622 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001623 Slog.v(LOG_TAG, "getAllSecureSystem(" + userId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001624 }
1625
1626 // Resolve the userId on whose behalf the call is made.
1627 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
1628
Svetoslav7ec28e82015-05-20 17:01:10 -07001629 synchronized (mLock) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001630 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SYSTEM, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -08001631
Svetoslav7ec28e82015-05-20 17:01:10 -07001632 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001633
Svetoslav7ec28e82015-05-20 17:01:10 -07001634 String[] normalizedProjection = normalizeProjection(projection);
1635 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001636
Svetoslav7ec28e82015-05-20 17:01:10 -07001637 for (int i = 0; i < nameCount; i++) {
1638 String name = names.get(i);
Svetoslav683914b2015-01-15 14:22:26 -08001639
Svetoslav7ec28e82015-05-20 17:01:10 -07001640 // Determine the owning user as some profile settings are cloned from the parent.
1641 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId,
1642 name);
Svetoslav683914b2015-01-15 14:22:26 -08001643
Svetoslav7ec28e82015-05-20 17:01:10 -07001644 Setting setting = mSettingsRegistry.getSettingLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001645 SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001646 appendSettingToCursor(result, setting);
1647 }
1648
1649 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001650 }
Svetoslav683914b2015-01-15 14:22:26 -08001651 }
1652
Svetoslav7ec28e82015-05-20 17:01:10 -07001653 private Setting getSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001654 if (DEBUG) {
1655 Slog.v(LOG_TAG, "getSystemSetting(" + name + ", " + requestingUserId + ")");
1656 }
1657
1658 // Resolve the userId on whose behalf the call is made.
1659 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1660
Chad Brubakera6830e72017-04-28 17:34:36 -07001661 // Ensure the caller can access the setting.
1662 enforceSettingReadable(name, SETTINGS_TYPE_SYSTEM, UserHandle.getCallingUserId());
Chad Brubaker97bccee2017-01-05 15:51:41 -08001663
Svetoslav683914b2015-01-15 14:22:26 -08001664 // Determine the owning user as some profile settings are cloned from the parent.
1665 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1666
1667 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001668 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -07001669 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001670 }
Svetoslav683914b2015-01-15 14:22:26 -08001671 }
1672
Svetoslav7ec28e82015-05-20 17:01:10 -07001673 private boolean insertSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001674 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001675 Slog.v(LOG_TAG, "insertSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001676 + requestingUserId + ")");
1677 }
1678
Svetoslav7ec28e82015-05-20 17:01:10 -07001679 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT);
Svetoslav683914b2015-01-15 14:22:26 -08001680 }
1681
Svetoslav7ec28e82015-05-20 17:01:10 -07001682 private boolean deleteSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001683 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001684 Slog.v(LOG_TAG, "deleteSystemSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001685 }
1686
Svetoslav7ec28e82015-05-20 17:01:10 -07001687 return mutateSystemSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE);
Svetoslav683914b2015-01-15 14:22:26 -08001688 }
1689
Svetoslav7ec28e82015-05-20 17:01:10 -07001690 private boolean updateSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001691 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001692 Slog.v(LOG_TAG, "updateSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001693 + requestingUserId + ")");
1694 }
1695
Svetoslav7ec28e82015-05-20 17:01:10 -07001696 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE);
Svetoslav683914b2015-01-15 14:22:26 -08001697 }
1698
Svetoslav7ec28e82015-05-20 17:01:10 -07001699 private boolean mutateSystemSetting(String name, String value, int runAsUserId,
Svetoslav683914b2015-01-15 14:22:26 -08001700 int operation) {
Billy Lau6ad2d662015-07-18 00:26:58 +01001701 if (!hasWriteSecureSettingsPermission()) {
1702 // If the caller doesn't hold WRITE_SECURE_SETTINGS, we verify whether this
1703 // operation is allowed for the calling package through appops.
1704 if (!Settings.checkAndNoteWriteSettingsOperation(getContext(),
1705 Binder.getCallingUid(), getCallingPackage(), true)) {
1706 return false;
1707 }
Svetoslav683914b2015-01-15 14:22:26 -08001708 }
1709
Svetoslav683914b2015-01-15 14:22:26 -08001710 // Resolve the userId on whose behalf the call is made.
1711 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId);
1712
yuemingw1d13eae2018-01-30 17:27:54 +00001713 if (name != null && mUserManagerInternal.isSettingRestrictedForUser(
1714 name, callingUserId, value, Binder.getCallingUid())) {
1715 return false;
1716 }
1717
Svetoslavd8d25e02015-11-20 13:09:26 -08001718 // Enforce what the calling package can mutate the system settings.
1719 enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name, callingUserId);
1720
Svetoslav683914b2015-01-15 14:22:26 -08001721 // Determine the owning user as some profile settings are cloned from the parent.
1722 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1723
1724 // Only the owning user id can change the setting.
1725 if (owningUserId != callingUserId) {
1726 return false;
1727 }
1728
Jeff Sharkey413573a2016-02-22 17:52:45 -07001729 // Invalidate any relevant cache files
1730 String cacheName = null;
1731 if (Settings.System.RINGTONE.equals(name)) {
1732 cacheName = Settings.System.RINGTONE_CACHE;
1733 } else if (Settings.System.NOTIFICATION_SOUND.equals(name)) {
1734 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
1735 } else if (Settings.System.ALARM_ALERT.equals(name)) {
1736 cacheName = Settings.System.ALARM_ALERT_CACHE;
1737 }
1738 if (cacheName != null) {
1739 final File cacheFile = new File(
Andre Lago3fa139c2016-08-04 13:53:44 +01001740 getRingtoneCacheDir(owningUserId), cacheName);
Jeff Sharkey413573a2016-02-22 17:52:45 -07001741 cacheFile.delete();
1742 }
1743
Svetoslav683914b2015-01-15 14:22:26 -08001744 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001745 synchronized (mLock) {
1746 switch (operation) {
1747 case MUTATION_OPERATION_INSERT: {
1748 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001749 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001750 owningUserId, name, value, null, false, getCallingPackage(),
1751 false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001752 }
1753
1754 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001755 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001756 owningUserId, name, false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001757 }
1758
1759 case MUTATION_OPERATION_UPDATE: {
1760 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001761 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001762 owningUserId, name, value, null, false, getCallingPackage(),
1763 false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001764 }
Svetoslav683914b2015-01-15 14:22:26 -08001765 }
1766
Svetoslav7ec28e82015-05-20 17:01:10 -07001767 return false;
Svetoslav683914b2015-01-15 14:22:26 -08001768 }
Svetoslav683914b2015-01-15 14:22:26 -08001769 }
1770
Billy Lau6ad2d662015-07-18 00:26:58 +01001771 private boolean hasWriteSecureSettingsPermission() {
Svetoslavf41334b2015-06-23 12:06:03 -07001772 // Write secure settings is a more protected permission. If caller has it we are good.
1773 if (getContext().checkCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS)
1774 == PackageManager.PERMISSION_GRANTED) {
1775 return true;
1776 }
1777
Svetoslavf41334b2015-06-23 12:06:03 -07001778 return false;
1779 }
1780
Svetoslav683914b2015-01-15 14:22:26 -08001781 private void validateSystemSettingValue(String name, String value) {
Michal Karpinski2c37b082018-01-18 16:14:27 +00001782 SettingsValidators.Validator validator = Settings.System.VALIDATORS.get(name);
Svetoslav683914b2015-01-15 14:22:26 -08001783 if (validator != null && !validator.validate(value)) {
1784 throw new IllegalArgumentException("Invalid value: " + value
1785 + " for setting: " + name);
1786 }
1787 }
1788
Alex Klyubin1991f572017-03-03 14:08:36 -08001789 /**
1790 * Returns {@code true} if the specified secure setting should be accessible to the caller.
1791 */
1792 private boolean isSecureSettingAccessible(String name, int callingUserId,
1793 int owningUserId) {
1794 // Special case for location (sigh).
1795 // This check is not inside the name-based checks below because this method performs checks
1796 // only if the calling user ID is not the same as the owning user ID.
1797 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
1798 return false;
1799 }
1800
1801 switch (name) {
1802 case "bluetooth_address":
1803 // BluetoothManagerService for some reason stores the Android's Bluetooth MAC
1804 // address in this secure setting. Secure settings can normally be read by any app,
1805 // which thus enables them to bypass the recently introduced restrictions on access
1806 // to device identifiers.
1807 // To mitigate this we make this setting available only to callers privileged to see
1808 // this device's MAC addresses, same as through public API
1809 // BluetoothAdapter.getAddress() (see BluetoothManagerService for details).
1810 return getContext().checkCallingOrSelfPermission(
1811 Manifest.permission.LOCAL_MAC_ADDRESS) == PackageManager.PERMISSION_GRANTED;
1812 default:
1813 return true;
1814 }
1815 }
1816
Svetoslav683914b2015-01-15 14:22:26 -08001817 private boolean isLocationProvidersAllowedRestricted(String name, int callingUserId,
1818 int owningUserId) {
1819 // Optimization - location providers are restricted only for managed profiles.
1820 if (callingUserId == owningUserId) {
1821 return false;
1822 }
1823 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)
1824 && mUserManager.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION,
1825 new UserHandle(callingUserId))) {
1826 return true;
1827 }
1828 return false;
1829 }
1830
Svetoslav683914b2015-01-15 14:22:26 -08001831 private int resolveOwningUserIdForSecureSettingLocked(int userId, String setting) {
1832 return resolveOwningUserIdLocked(userId, sSecureCloneToManagedSettings, setting);
1833 }
1834
1835 private int resolveOwningUserIdForSystemSettingLocked(int userId, String setting) {
Andre Lago3fa139c2016-08-04 13:53:44 +01001836 final int parentId;
1837 // Resolves dependency if setting has a dependency and the calling user has a parent
1838 if (sSystemCloneFromParentOnDependency.containsKey(setting)
1839 && (parentId = getGroupParentLocked(userId)) != userId) {
1840 // The setting has a dependency and the profile has a parent
1841 String dependency = sSystemCloneFromParentOnDependency.get(setting);
Chad Brubaker97bccee2017-01-05 15:51:41 -08001842 // Lookup the dependency setting as ourselves, some callers may not have access to it.
1843 final long token = Binder.clearCallingIdentity();
1844 try {
1845 Setting settingObj = getSecureSetting(dependency, userId);
1846 if (settingObj != null && settingObj.getValue().equals("1")) {
1847 return parentId;
1848 }
1849 } finally {
1850 Binder.restoreCallingIdentity(token);
Andre Lago3fa139c2016-08-04 13:53:44 +01001851 }
1852 }
Svetoslav683914b2015-01-15 14:22:26 -08001853 return resolveOwningUserIdLocked(userId, sSystemCloneToManagedSettings, setting);
1854 }
1855
1856 private int resolveOwningUserIdLocked(int userId, Set<String> keys, String name) {
1857 final int parentId = getGroupParentLocked(userId);
1858 if (parentId != userId && keys.contains(name)) {
1859 return parentId;
1860 }
1861 return userId;
1862 }
1863
Svetoslavf41334b2015-06-23 12:06:03 -07001864 private void enforceRestrictedSystemSettingsMutationForCallingPackage(int operation,
Xiaohui Chen43765b72015-08-31 10:57:33 -07001865 String name, int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001866 // System/root/shell can mutate whatever secure settings they want.
1867 final int callingUid = Binder.getCallingUid();
Svetoslav Ganove080da92016-12-21 17:10:35 -08001868 final int appId = UserHandle.getAppId(callingUid);
1869 if (appId == android.os.Process.SYSTEM_UID
1870 || appId == Process.SHELL_UID
1871 || appId == Process.ROOT_UID) {
Svetoslav683914b2015-01-15 14:22:26 -08001872 return;
1873 }
1874
1875 switch (operation) {
1876 case MUTATION_OPERATION_INSERT:
1877 // Insert updates.
1878 case MUTATION_OPERATION_UPDATE: {
1879 if (Settings.System.PUBLIC_SETTINGS.contains(name)) {
1880 return;
1881 }
1882
1883 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001884 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001885
1886 // Privileged apps can do whatever they want.
1887 if ((packageInfo.applicationInfo.privateFlags
1888 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1889 return;
1890 }
1891
1892 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1893 packageInfo.applicationInfo.targetSdkVersion, name);
1894 } break;
1895
1896 case MUTATION_OPERATION_DELETE: {
1897 if (Settings.System.PUBLIC_SETTINGS.contains(name)
1898 || Settings.System.PRIVATE_SETTINGS.contains(name)) {
1899 throw new IllegalArgumentException("You cannot delete system defined"
1900 + " secure settings.");
1901 }
1902
1903 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001904 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001905
1906 // Privileged apps can do whatever they want.
1907 if ((packageInfo.applicationInfo.privateFlags &
1908 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1909 return;
1910 }
1911
1912 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1913 packageInfo.applicationInfo.targetSdkVersion, name);
1914 } break;
1915 }
1916 }
1917
Todd Kennedybe0b8892017-02-15 14:13:52 -08001918 private Set<String> getInstantAppAccessibleSettings(int settingsType) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001919 switch (settingsType) {
1920 case SETTINGS_TYPE_GLOBAL:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001921 return Settings.Global.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001922 case SETTINGS_TYPE_SECURE:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001923 return Settings.Secure.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001924 case SETTINGS_TYPE_SYSTEM:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001925 return Settings.System.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001926 default:
1927 throw new IllegalArgumentException("Invalid settings type: " + settingsType);
1928 }
1929 }
1930
Chad Brubaker20e0dc32017-04-28 18:24:55 -07001931 private Set<String> getOverlayInstantAppAccessibleSettings(int settingsType) {
1932 switch (settingsType) {
1933 case SETTINGS_TYPE_GLOBAL:
1934 return OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS;
1935 case SETTINGS_TYPE_SYSTEM:
1936 return OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS;
1937 case SETTINGS_TYPE_SECURE:
1938 return OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS;
1939 default:
1940 throw new IllegalArgumentException("Invalid settings type: " + settingsType);
1941 }
1942 }
1943
Andreas Gampeb58893072018-09-05 16:52:31 -07001944 @GuardedBy("mLock")
Chad Brubaker97bccee2017-01-05 15:51:41 -08001945 private List<String> getSettingsNamesLocked(int settingsType, int userId) {
Chad Brubakerb6108d62017-12-23 20:06:44 -08001946 // Don't enforce the instant app whitelist for now -- its too prone to unintended breakage
1947 // in the current form.
1948 return mSettingsRegistry.getSettingsNamesLocked(settingsType, userId);
Chad Brubaker97bccee2017-01-05 15:51:41 -08001949 }
1950
Chad Brubakera6830e72017-04-28 17:34:36 -07001951 private void enforceSettingReadable(String settingName, int settingsType, int userId) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001952 if (UserHandle.getAppId(Binder.getCallingUid()) < Process.FIRST_APPLICATION_UID) {
1953 return;
1954 }
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001955 ApplicationInfo ai = getCallingApplicationInfoOrThrow();
Svetoslav Ganov096d3042017-01-30 16:34:13 -08001956 if (!ai.isInstantApp()) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001957 return;
1958 }
Chad Brubaker20e0dc32017-04-28 18:24:55 -07001959 if (!getInstantAppAccessibleSettings(settingsType).contains(settingName)
1960 && !getOverlayInstantAppAccessibleSettings(settingsType).contains(settingName)) {
Chad Brubakerb6108d62017-12-23 20:06:44 -08001961 // Don't enforce the instant app whitelist for now -- its too prone to unintended
1962 // breakage in the current form.
1963 Slog.w(LOG_TAG, "Instant App " + ai.packageName
1964 + " trying to access unexposed setting, this will be an error in the future.");
Chad Brubaker97bccee2017-01-05 15:51:41 -08001965 }
1966 }
1967
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001968 private ApplicationInfo getCallingApplicationInfoOrThrow() {
1969 // We always use the callingUid for this lookup. This means that if hypothetically an
1970 // app was installed in user A with cross user and in user B as an Instant App
1971 // the app in A would be able to see all the settings in user B. However since cross
1972 // user is a system permission and the app must be uninstalled in B and then installed as
1973 // an Instant App that situation is not realistic or supported.
Chad Brubaker97bccee2017-01-05 15:51:41 -08001974 ApplicationInfo ai = null;
1975 try {
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001976 ai = mPackageManager.getApplicationInfo(getCallingPackage(), 0
1977 , UserHandle.getCallingUserId());
Chad Brubaker97bccee2017-01-05 15:51:41 -08001978 } catch (RemoteException ignored) {
1979 }
1980 if (ai == null) {
1981 throw new IllegalStateException("Failed to lookup info for package "
1982 + getCallingPackage());
1983 }
1984 return ai;
1985 }
1986
Xiaohui Chen43765b72015-08-31 10:57:33 -07001987 private PackageInfo getCallingPackageInfoOrThrow(int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001988 try {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001989 PackageInfo packageInfo = mPackageManager.getPackageInfo(
1990 getCallingPackage(), 0, userId);
1991 if (packageInfo != null) {
1992 return packageInfo;
1993 }
Xiaohui Chen43765b72015-08-31 10:57:33 -07001994 } catch (RemoteException e) {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001995 /* ignore */
Svetoslav683914b2015-01-15 14:22:26 -08001996 }
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001997 throw new IllegalStateException("Calling package doesn't exist");
Svetoslav683914b2015-01-15 14:22:26 -08001998 }
1999
2000 private int getGroupParentLocked(int userId) {
2001 // Most frequent use case.
Xiaohui Chen43765b72015-08-31 10:57:33 -07002002 if (userId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08002003 return userId;
2004 }
2005 // We are in the same process with the user manager and the returned
2006 // user info is a cached instance, so just look up instead of cache.
2007 final long identity = Binder.clearCallingIdentity();
2008 try {
Svetoslav7ec28e82015-05-20 17:01:10 -07002009 // Just a lookup and not reentrant, so holding a lock is fine.
Svetoslav683914b2015-01-15 14:22:26 -08002010 UserInfo userInfo = mUserManager.getProfileParent(userId);
2011 return (userInfo != null) ? userInfo.id : userId;
2012 } finally {
2013 Binder.restoreCallingIdentity(identity);
2014 }
2015 }
2016
Svetoslav683914b2015-01-15 14:22:26 -08002017 private void enforceWritePermission(String permission) {
2018 if (getContext().checkCallingOrSelfPermission(permission)
2019 != PackageManager.PERMISSION_GRANTED) {
2020 throw new SecurityException("Permission denial: writing to settings requires:"
2021 + permission);
2022 }
2023 }
2024
2025 /*
2026 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
2027 * This setting contains a list of the currently enabled location providers.
2028 * But helper functions in android.providers.Settings can enable or disable
2029 * a single provider by using a "+" or "-" prefix before the provider name.
2030 *
yuemingw1d13eae2018-01-30 17:27:54 +00002031 * <p>See also {@link com.android.server.pm.UserRestrictionsUtils#isSettingRestrictedForUser()}.
2032 * If DISALLOW_SHARE_LOCATION is set, the said method will only allow values with
2033 * the "-" prefix.
Makoto Onuki28da2e32015-11-20 11:30:44 -08002034 *
Svetoslav683914b2015-01-15 14:22:26 -08002035 * @returns whether the enabled location providers changed.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002036 */
Andreas Gampeb58893072018-09-05 16:52:31 -07002037 @GuardedBy("mLock")
Svetoslav Ganove080da92016-12-21 17:10:35 -08002038 private boolean updateLocationProvidersAllowedLocked(String value, String tag,
2039 int owningUserId, boolean makeDefault, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08002040 if (TextUtils.isEmpty(value)) {
2041 return false;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002042 }
Maggie83e03f52018-03-16 12:22:20 -07002043 Setting oldSetting = getSecureSetting(
2044 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, owningUserId);
2045 if (oldSetting == null) {
Svetoslav683914b2015-01-15 14:22:26 -08002046 return false;
2047 }
Maggie83e03f52018-03-16 12:22:20 -07002048 String oldProviders = oldSetting.getValue();
2049 List<String> oldProvidersList = TextUtils.isEmpty(oldProviders)
2050 ? new ArrayList<>() : new ArrayList<>(Arrays.asList(oldProviders.split(",")));
2051 Set<String> newProvidersSet = new ArraySet<>();
2052 newProvidersSet.addAll(oldProvidersList);
Svetoslav683914b2015-01-15 14:22:26 -08002053
Maggie83e03f52018-03-16 12:22:20 -07002054 String[] providerUpdates = value.split(",");
2055 boolean inputError = false;
2056 for (String provider : providerUpdates) {
2057 // do not update location_providers_allowed when input is invalid
2058 if (TextUtils.isEmpty(provider)) {
2059 inputError = true;
2060 break;
Svetoslav683914b2015-01-15 14:22:26 -08002061 }
Maggie83e03f52018-03-16 12:22:20 -07002062 final char prefix = provider.charAt(0);
2063 // do not update location_providers_allowed when input is invalid
2064 if (prefix != '+' && prefix != '-') {
2065 inputError = true;
2066 break;
Svetoslav683914b2015-01-15 14:22:26 -08002067 }
Maggie83e03f52018-03-16 12:22:20 -07002068 // skip prefix
2069 provider = provider.substring(1);
2070 if (prefix == '+') {
2071 newProvidersSet.add(provider);
2072 } else if (prefix == '-') {
2073 newProvidersSet.remove(provider);
Svetoslav683914b2015-01-15 14:22:26 -08002074 }
Maggie83e03f52018-03-16 12:22:20 -07002075 }
2076 String newProviders = TextUtils.join(",", newProvidersSet.toArray());
2077 if (inputError == true || newProviders.equals(oldProviders)) {
Svetoslav683914b2015-01-15 14:22:26 -08002078 // nothing changed, so no need to update the database
Svet Ganov53a441c2016-04-19 19:38:00 -07002079 if (forceNotify) {
Maggie83e03f52018-03-16 12:22:20 -07002080 mSettingsRegistry.notifyForSettingsChange(
2081 makeKey(SETTINGS_TYPE_SECURE, owningUserId),
Svet Ganov53a441c2016-04-19 19:38:00 -07002082 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
2083 }
Svetoslav683914b2015-01-15 14:22:26 -08002084 return false;
2085 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002086 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Maggie83e03f52018-03-16 12:22:20 -07002087 owningUserId, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, newProviders, tag,
2088 makeDefault, getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav683914b2015-01-15 14:22:26 -08002089 }
2090
Svetoslav683914b2015-01-15 14:22:26 -08002091 private static void warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
2092 int targetSdkVersion, String name) {
2093 // If the app targets Lollipop MR1 or older SDK we warn, otherwise crash.
2094 if (targetSdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) {
2095 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
2096 Slog.w(LOG_TAG, "You shouldn't not change private system settings."
2097 + " This will soon become an error.");
2098 } else {
2099 Slog.w(LOG_TAG, "You shouldn't keep your settings in the secure settings."
2100 + " This will soon become an error.");
2101 }
2102 } else {
2103 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
2104 throw new IllegalArgumentException("You cannot change private secure settings.");
2105 } else {
2106 throw new IllegalArgumentException("You cannot keep your settings in"
2107 + " the secure settings.");
2108 }
2109 }
2110 }
2111
2112 private static int resolveCallingUserIdEnforcingPermissionsLocked(int requestingUserId) {
2113 if (requestingUserId == UserHandle.getCallingUserId()) {
2114 return requestingUserId;
2115 }
2116 return ActivityManager.handleIncomingUser(Binder.getCallingPid(),
2117 Binder.getCallingUid(), requestingUserId, false, true,
2118 "get/set setting for user", null);
2119 }
2120
Svet Ganov53a441c2016-04-19 19:38:00 -07002121 private Bundle packageValueForCallResult(Setting setting,
2122 boolean trackingGeneration) {
2123 if (!trackingGeneration) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002124 if (setting == null || setting.isNull()) {
Svet Ganov53a441c2016-04-19 19:38:00 -07002125 return NULL_SETTING_BUNDLE;
2126 }
2127 return Bundle.forPair(Settings.NameValueTable.VALUE, setting.getValue());
Svetoslav683914b2015-01-15 14:22:26 -08002128 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002129 Bundle result = new Bundle();
2130 result.putString(Settings.NameValueTable.VALUE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002131 !setting.isNull() ? setting.getValue() : null);
Svet Ganov96c99462017-05-05 14:27:13 -07002132
Svetoslav Ganove080da92016-12-21 17:10:35 -08002133 mSettingsRegistry.mGenerationRegistry.addGenerationData(result, setting.getKey());
Svet Ganov53a441c2016-04-19 19:38:00 -07002134 return result;
Svetoslav683914b2015-01-15 14:22:26 -08002135 }
2136
2137 private static int getRequestingUserId(Bundle args) {
2138 final int callingUserId = UserHandle.getCallingUserId();
2139 return (args != null) ? args.getInt(Settings.CALL_METHOD_USER_KEY, callingUserId)
2140 : callingUserId;
2141 }
2142
Svet Ganov53a441c2016-04-19 19:38:00 -07002143 private boolean isTrackingGeneration(Bundle args) {
2144 return args != null && args.containsKey(Settings.CALL_METHOD_TRACK_GENERATION_KEY);
2145 }
2146
Svetoslav683914b2015-01-15 14:22:26 -08002147 private static String getSettingValue(Bundle args) {
2148 return (args != null) ? args.getString(Settings.NameValueTable.VALUE) : null;
2149 }
2150
Svetoslav Ganove080da92016-12-21 17:10:35 -08002151 private static String getSettingTag(Bundle args) {
2152 return (args != null) ? args.getString(Settings.CALL_METHOD_TAG_KEY) : null;
2153 }
2154
Matt Pape6bfc62e2018-11-28 13:16:03 -08002155 private static String getSettingPrefix(Bundle args) {
2156 String prefix = (args != null) ? args.getString(Settings.CALL_METHOD_PREFIX_KEY) : null;
2157 // Append '/' to ensure we only match properties with this exact prefix.
2158 // i.e. "foo" should match "foo/property" but not "foobar/property"
2159 return prefix != null ? prefix + "/" : null;
2160 }
2161
Svetoslav Ganove080da92016-12-21 17:10:35 -08002162 private static boolean getSettingMakeDefault(Bundle args) {
2163 return (args != null) && args.getBoolean(Settings.CALL_METHOD_MAKE_DEFAULT_KEY);
2164 }
2165
2166 private static int getResetModeEnforcingPermission(Bundle args) {
2167 final int mode = (args != null) ? args.getInt(Settings.CALL_METHOD_RESET_MODE_KEY) : 0;
2168 switch (mode) {
2169 case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: {
2170 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
2171 throw new SecurityException("Only system, shell/root on a "
2172 + "debuggable build can reset to untrusted defaults");
2173 }
2174 return mode;
2175 }
2176 case Settings.RESET_MODE_UNTRUSTED_CHANGES: {
2177 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
2178 throw new SecurityException("Only system, shell/root on a "
2179 + "debuggable build can reset untrusted changes");
2180 }
2181 return mode;
2182 }
2183 case Settings.RESET_MODE_TRUSTED_DEFAULTS: {
2184 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
2185 throw new SecurityException("Only system, shell/root on a "
2186 + "debuggable build can reset to trusted defaults");
2187 }
2188 return mode;
2189 }
2190 case Settings.RESET_MODE_PACKAGE_DEFAULTS: {
2191 return mode;
2192 }
2193 }
2194 throw new IllegalArgumentException("Invalid reset mode: " + mode);
2195 }
2196
2197 private static boolean isCallerSystemOrShellOrRootOnDebuggableBuild() {
2198 final int appId = UserHandle.getAppId(Binder.getCallingUid());
2199 return appId == SYSTEM_UID || (Build.IS_DEBUGGABLE
2200 && (appId == SHELL_UID || appId == ROOT_UID));
2201 }
2202
Svetoslav683914b2015-01-15 14:22:26 -08002203 private static String getValidTableOrThrow(Uri uri) {
2204 if (uri.getPathSegments().size() > 0) {
2205 String table = uri.getPathSegments().get(0);
2206 if (DatabaseHelper.isValidTable(table)) {
2207 return table;
2208 }
2209 throw new IllegalArgumentException("Bad root path: " + table);
2210 }
2211 throw new IllegalArgumentException("Invalid URI:" + uri);
2212 }
2213
2214 private static MatrixCursor packageSettingForQuery(Setting setting, String[] projection) {
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002215 if (setting.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08002216 return new MatrixCursor(projection, 0);
2217 }
2218 MatrixCursor cursor = new MatrixCursor(projection, 1);
2219 appendSettingToCursor(cursor, setting);
2220 return cursor;
2221 }
2222
2223 private static String[] normalizeProjection(String[] projection) {
2224 if (projection == null) {
2225 return ALL_COLUMNS;
2226 }
2227
2228 final int columnCount = projection.length;
2229 for (int i = 0; i < columnCount; i++) {
2230 String column = projection[i];
2231 if (!ArrayUtils.contains(ALL_COLUMNS, column)) {
2232 throw new IllegalArgumentException("Invalid column: " + column);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002233 }
2234 }
2235
Svetoslav683914b2015-01-15 14:22:26 -08002236 return projection;
2237 }
2238
2239 private static void appendSettingToCursor(MatrixCursor cursor, Setting setting) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002240 if (setting == null || setting.isNull()) {
Suprabh Shuklac9d064a2016-04-12 18:45:34 -07002241 return;
2242 }
Svetoslav683914b2015-01-15 14:22:26 -08002243 final int columnCount = cursor.getColumnCount();
2244
2245 String[] values = new String[columnCount];
2246
2247 for (int i = 0; i < columnCount; i++) {
2248 String column = cursor.getColumnName(i);
2249
2250 switch (column) {
2251 case Settings.NameValueTable._ID: {
2252 values[i] = setting.getId();
2253 } break;
2254
2255 case Settings.NameValueTable.NAME: {
2256 values[i] = setting.getName();
2257 } break;
2258
2259 case Settings.NameValueTable.VALUE: {
2260 values[i] = setting.getValue();
2261 } break;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002262 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002263 }
2264
Svetoslav683914b2015-01-15 14:22:26 -08002265 cursor.addRow(values);
2266 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002267
Makoto Onuki3a2c35782015-06-18 11:21:58 -07002268 private static boolean isKeyValid(String key) {
2269 return !(TextUtils.isEmpty(key) || SettingsState.isBinary(key));
2270 }
2271
Matt Pape7b1c6cd2019-01-04 08:10:41 -08002272 private String resolveCallingPackage() {
2273 switch (Binder.getCallingUid()) {
2274 case Process.ROOT_UID: {
2275 return "root";
2276 }
2277
2278 case Process.SHELL_UID: {
2279 return "com.android.shell";
2280 }
2281
2282 default: {
2283 return getCallingPackage();
2284 }
2285 }
2286 }
2287
Svetoslav683914b2015-01-15 14:22:26 -08002288 private static final class Arguments {
2289 private static final Pattern WHERE_PATTERN_WITH_PARAM_NO_BRACKETS =
2290 Pattern.compile("[\\s]*name[\\s]*=[\\s]*\\?[\\s]*");
2291
2292 private static final Pattern WHERE_PATTERN_WITH_PARAM_IN_BRACKETS =
2293 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*\\?[\\s]*\\)[\\s]*");
2294
2295 private static final Pattern WHERE_PATTERN_NO_PARAM_IN_BRACKETS =
2296 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*\\)[\\s]*");
2297
2298 private static final Pattern WHERE_PATTERN_NO_PARAM_NO_BRACKETS =
2299 Pattern.compile("[\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*");
2300
2301 public final String table;
2302 public final String name;
2303
2304 public Arguments(Uri uri, String where, String[] whereArgs, boolean supportAll) {
2305 final int segmentSize = uri.getPathSegments().size();
2306 switch (segmentSize) {
2307 case 1: {
2308 if (where != null
2309 && (WHERE_PATTERN_WITH_PARAM_NO_BRACKETS.matcher(where).matches()
2310 || WHERE_PATTERN_WITH_PARAM_IN_BRACKETS.matcher(where).matches())
2311 && whereArgs.length == 1) {
2312 name = whereArgs[0];
2313 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08002314 return;
Svetoslav683914b2015-01-15 14:22:26 -08002315 } else if (where != null
2316 && (WHERE_PATTERN_NO_PARAM_NO_BRACKETS.matcher(where).matches()
2317 || WHERE_PATTERN_NO_PARAM_IN_BRACKETS.matcher(where).matches())) {
2318 final int startIndex = Math.max(where.indexOf("'"),
2319 where.indexOf("\"")) + 1;
2320 final int endIndex = Math.max(where.lastIndexOf("'"),
2321 where.lastIndexOf("\""));
2322 name = where.substring(startIndex, endIndex);
2323 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08002324 return;
Svetoslav683914b2015-01-15 14:22:26 -08002325 } else if (supportAll && where == null && whereArgs == null) {
2326 name = null;
2327 table = computeTableForSetting(uri, null);
Svetoslav28494652015-02-12 14:11:42 -08002328 return;
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002329 }
Svetoslav683914b2015-01-15 14:22:26 -08002330 } break;
2331
Svetoslav28494652015-02-12 14:11:42 -08002332 case 2: {
2333 if (where == null && whereArgs == null) {
2334 name = uri.getPathSegments().get(1);
2335 table = computeTableForSetting(uri, name);
2336 return;
2337 }
2338 } break;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002339 }
Svetoslav28494652015-02-12 14:11:42 -08002340
2341 EventLogTags.writeUnsupportedSettingsQuery(
2342 uri.toSafeString(), where, Arrays.toString(whereArgs));
2343 String message = String.format( "Supported SQL:\n"
2344 + " uri content://some_table/some_property with null where and where args\n"
2345 + " uri content://some_table with query name=? and single name as arg\n"
2346 + " uri content://some_table with query name=some_name and null args\n"
2347 + " but got - uri:%1s, where:%2s whereArgs:%3s", uri, where,
2348 Arrays.toString(whereArgs));
2349 throw new IllegalArgumentException(message);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002350 }
2351
Svetoslav28494652015-02-12 14:11:42 -08002352 private static String computeTableForSetting(Uri uri, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08002353 String table = getValidTableOrThrow(uri);
2354
2355 if (name != null) {
2356 if (sSystemMovedToSecureSettings.contains(name)) {
2357 table = TABLE_SECURE;
2358 }
2359
2360 if (sSystemMovedToGlobalSettings.contains(name)) {
2361 table = TABLE_GLOBAL;
2362 }
2363
2364 if (sSecureMovedToGlobalSettings.contains(name)) {
2365 table = TABLE_GLOBAL;
2366 }
2367
2368 if (sGlobalMovedToSecureSettings.contains(name)) {
2369 table = TABLE_SECURE;
2370 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002371 }
Svetoslav683914b2015-01-15 14:22:26 -08002372
2373 return table;
2374 }
2375 }
2376
2377 final class SettingsRegistry {
2378 private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid";
2379
Svetoslav683914b2015-01-15 14:22:26 -08002380 private static final String SETTINGS_FILE_GLOBAL = "settings_global.xml";
2381 private static final String SETTINGS_FILE_SYSTEM = "settings_system.xml";
2382 private static final String SETTINGS_FILE_SECURE = "settings_secure.xml";
Mark Rathjend891f012017-01-19 04:10:37 +00002383 private static final String SETTINGS_FILE_SSAID = "settings_ssaid.xml";
Matt Pape1b31a332018-10-17 09:58:28 -07002384 private static final String SETTINGS_FILE_CONFIG = "settings_config.xml";
Mark Rathjend891f012017-01-19 04:10:37 +00002385
2386 private static final String SSAID_USER_KEY = "userkey";
Svetoslav683914b2015-01-15 14:22:26 -08002387
2388 private final SparseArray<SettingsState> mSettingsStates = new SparseArray<>();
2389
Svet Ganov53a441c2016-04-19 19:38:00 -07002390 private GenerationRegistry mGenerationRegistry;
Svetoslav683914b2015-01-15 14:22:26 -08002391
Svetoslav7e0683b2015-08-03 16:02:52 -07002392 private final Handler mHandler;
2393
Svet Ganov53a441c2016-04-19 19:38:00 -07002394 private final BackupManager mBackupManager;
2395
Amith Yamasani39452022017-03-21 15:23:47 -07002396 private String mSettingsCreationBuildId;
2397
Svetoslav683914b2015-01-15 14:22:26 -08002398 public SettingsRegistry() {
Svetoslav7e0683b2015-08-03 16:02:52 -07002399 mHandler = new MyHandler(getContext().getMainLooper());
Svet Ganov53a441c2016-04-19 19:38:00 -07002400 mGenerationRegistry = new GenerationRegistry(mLock);
2401 mBackupManager = new BackupManager(getContext());
Svetoslav683914b2015-01-15 14:22:26 -08002402 migrateAllLegacySettingsIfNeeded();
Mark Rathjend891f012017-01-19 04:10:37 +00002403 syncSsaidTableOnStart();
2404 }
2405
2406 private void generateUserKeyLocked(int userId) {
2407 // Generate a random key for each user used for creating a new ssaid.
Mark Rathjen7599f132017-01-23 14:15:54 -08002408 final byte[] keyBytes = new byte[32];
Mark Rathjend891f012017-01-19 04:10:37 +00002409 final SecureRandom rand = new SecureRandom();
2410 rand.nextBytes(keyBytes);
2411
2412 // Convert to string for storage in settings table.
Mark Rathjen7599f132017-01-23 14:15:54 -08002413 final String userKey = ByteStringUtils.toHexString(keyBytes);
Mark Rathjend891f012017-01-19 04:10:37 +00002414
2415 // Store the key in the ssaid table.
2416 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
2417 final boolean success = ssaidSettings.insertSettingLocked(SSAID_USER_KEY, userKey, null,
2418 true, SettingsState.SYSTEM_PACKAGE_NAME);
2419
2420 if (!success) {
2421 throw new IllegalStateException("Ssaid settings not accessible");
2422 }
2423 }
2424
Mark Rathjen7599f132017-01-23 14:15:54 -08002425 private byte[] getLengthPrefix(byte[] data) {
2426 return ByteBuffer.allocate(4).putInt(data.length).array();
2427 }
2428
Christopher Tateb218e762017-04-05 16:34:07 -07002429 public Setting generateSsaidLocked(PackageInfo callingPkg, int userId) {
Mark Rathjend891f012017-01-19 04:10:37 +00002430 // Read the user's key from the ssaid table.
2431 Setting userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY);
Mark Rathjenea617592017-01-18 23:03:41 -08002432 if (userKeySetting == null || userKeySetting.isNull()
2433 || userKeySetting.getValue() == null) {
Mark Rathjend891f012017-01-19 04:10:37 +00002434 // Lazy initialize and store the user key.
2435 generateUserKeyLocked(userId);
2436 userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY);
Mark Rathjenea617592017-01-18 23:03:41 -08002437 if (userKeySetting == null || userKeySetting.isNull()
2438 || userKeySetting.getValue() == null) {
Mark Rathjend891f012017-01-19 04:10:37 +00002439 throw new IllegalStateException("User key not accessible");
2440 }
2441 }
2442 final String userKey = userKeySetting.getValue();
2443
2444 // Convert the user's key back to a byte array.
Mark Rathjen7599f132017-01-23 14:15:54 -08002445 final byte[] keyBytes = ByteStringUtils.fromHexToByteArray(userKey);
2446
2447 // Validate that the key is of expected length.
2448 // Keys are currently 32 bytes, but were once 16 bytes during Android O development.
2449 if (keyBytes == null || (keyBytes.length != 16 && keyBytes.length != 32)) {
Mark Rathjend891f012017-01-19 04:10:37 +00002450 throw new IllegalStateException("User key invalid");
2451 }
2452
Mark Rathjen7599f132017-01-23 14:15:54 -08002453 final Mac m;
Mark Rathjend891f012017-01-19 04:10:37 +00002454 try {
Mark Rathjen7599f132017-01-23 14:15:54 -08002455 m = Mac.getInstance("HmacSHA256");
2456 m.init(new SecretKeySpec(keyBytes, m.getAlgorithm()));
Mark Rathjend891f012017-01-19 04:10:37 +00002457 } catch (NoSuchAlgorithmException e) {
Mark Rathjen7599f132017-01-23 14:15:54 -08002458 throw new IllegalStateException("HmacSHA256 is not available", e);
2459 } catch (InvalidKeyException e) {
2460 throw new IllegalStateException("Key is corrupted", e);
Mark Rathjend891f012017-01-19 04:10:37 +00002461 }
Mark Rathjen7599f132017-01-23 14:15:54 -08002462
Mark Rathjenf42dd912017-06-05 19:04:34 -07002463 // Mac each of the developer signatures.
Christopher Tateb218e762017-04-05 16:34:07 -07002464 for (int i = 0; i < callingPkg.signatures.length; i++) {
2465 byte[] sig = callingPkg.signatures[i].toByteArray();
Mark Rathjen7599f132017-01-23 14:15:54 -08002466 m.update(getLengthPrefix(sig), 0, 4);
2467 m.update(sig);
2468 }
Mark Rathjend891f012017-01-19 04:10:37 +00002469
2470 // Convert result to a string for storage in settings table. Only want first 64 bits.
Mark Rathjen7599f132017-01-23 14:15:54 -08002471 final String ssaid = ByteStringUtils.toHexString(m.doFinal()).substring(0, 16)
2472 .toLowerCase(Locale.US);
Mark Rathjend891f012017-01-19 04:10:37 +00002473
2474 // Save the ssaid in the ssaid table.
Christopher Tateb218e762017-04-05 16:34:07 -07002475 final String uid = Integer.toString(callingPkg.applicationInfo.uid);
Mark Rathjend891f012017-01-19 04:10:37 +00002476 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
2477 final boolean success = ssaidSettings.insertSettingLocked(uid, ssaid, null, true,
Mark Rathjenf42dd912017-06-05 19:04:34 -07002478 callingPkg.packageName);
Mark Rathjend891f012017-01-19 04:10:37 +00002479
2480 if (!success) {
2481 throw new IllegalStateException("Ssaid settings not accessible");
2482 }
2483
2484 return getSettingLocked(SETTINGS_TYPE_SSAID, userId, uid);
2485 }
2486
2487 public void syncSsaidTableOnStart() {
2488 synchronized (mLock) {
2489 // Verify that each user's packages and ssaid's are in sync.
2490 for (UserInfo user : mUserManager.getUsers(true)) {
2491 // Get all uids for the user's packages.
2492 final List<PackageInfo> packages;
2493 try {
Tetsutoki Shiozawaebe0e5f2018-01-17 11:07:09 +09002494 packages = mPackageManager.getInstalledPackages(
2495 PackageManager.MATCH_UNINSTALLED_PACKAGES,
2496 user.id).getList();
Mark Rathjend891f012017-01-19 04:10:37 +00002497 } catch (RemoteException e) {
2498 throw new IllegalStateException("Package manager not available");
2499 }
2500 final Set<String> appUids = new HashSet<>();
2501 for (PackageInfo info : packages) {
2502 appUids.add(Integer.toString(info.applicationInfo.uid));
2503 }
2504
2505 // Get all uids currently stored in the user's ssaid table.
2506 final Set<String> ssaidUids = new HashSet<>(
2507 getSettingsNamesLocked(SETTINGS_TYPE_SSAID, user.id));
2508 ssaidUids.remove(SSAID_USER_KEY);
2509
2510 // Perform a set difference for the appUids and ssaidUids.
2511 ssaidUids.removeAll(appUids);
2512
2513 // If there are ssaidUids left over they need to be removed from the table.
2514 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID,
2515 user.id);
2516 for (String uid : ssaidUids) {
2517 ssaidSettings.deleteSettingLocked(uid);
2518 }
2519 }
2520 }
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002521 }
2522
Svetoslav683914b2015-01-15 14:22:26 -08002523 public List<String> getSettingsNamesLocked(int type, int userId) {
2524 final int key = makeKey(type, userId);
2525 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002526 if (settingsState == null) {
2527 return new ArrayList<String>();
2528 }
Svetoslav683914b2015-01-15 14:22:26 -08002529 return settingsState.getSettingNamesLocked();
2530 }
2531
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002532 public SparseBooleanArray getKnownUsersLocked() {
2533 SparseBooleanArray users = new SparseBooleanArray();
2534 for (int i = mSettingsStates.size()-1; i >= 0; i--) {
2535 users.put(getUserIdFromKey(mSettingsStates.keyAt(i)), true);
2536 }
2537 return users;
2538 }
2539
Kweku Adamsb0886f32017-10-31 15:32:09 -07002540 @Nullable
Svetoslav683914b2015-01-15 14:22:26 -08002541 public SettingsState getSettingsLocked(int type, int userId) {
2542 final int key = makeKey(type, userId);
2543 return peekSettingsStateLocked(key);
2544 }
2545
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002546 public boolean ensureSettingsForUserLocked(int userId) {
2547 // First make sure this user actually exists.
2548 if (mUserManager.getUserInfo(userId) == null) {
2549 Slog.wtf(LOG_TAG, "Requested user " + userId + " does not exist");
2550 return false;
2551 }
2552
Svetoslav683914b2015-01-15 14:22:26 -08002553 // Migrate the setting for this user if needed.
2554 migrateLegacySettingsForUserIfNeededLocked(userId);
2555
Matt Pape1b31a332018-10-17 09:58:28 -07002556 // Ensure config settings loaded if owner.
2557 if (userId == UserHandle.USER_SYSTEM) {
2558 final int configKey
2559 = makeKey(SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM);
2560 ensureSettingsStateLocked(configKey);
2561 }
2562
Svetoslav683914b2015-01-15 14:22:26 -08002563 // Ensure global settings loaded if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07002564 if (userId == UserHandle.USER_SYSTEM) {
2565 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002566 ensureSettingsStateLocked(globalKey);
2567 }
2568
2569 // Ensure secure settings loaded.
2570 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2571 ensureSettingsStateLocked(secureKey);
2572
2573 // Make sure the secure settings have an Android id set.
2574 SettingsState secureSettings = getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
2575 ensureSecureSettingAndroidIdSetLocked(secureSettings);
2576
2577 // Ensure system settings loaded.
2578 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2579 ensureSettingsStateLocked(systemKey);
2580
Mark Rathjend891f012017-01-19 04:10:37 +00002581 // Ensure secure settings loaded.
2582 final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId);
2583 ensureSettingsStateLocked(ssaidKey);
2584
Svetoslav683914b2015-01-15 14:22:26 -08002585 // Upgrade the settings to the latest version.
2586 UpgradeController upgrader = new UpgradeController(userId);
2587 upgrader.upgradeIfNeededLocked();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002588 return true;
Svetoslav683914b2015-01-15 14:22:26 -08002589 }
2590
2591 private void ensureSettingsStateLocked(int key) {
2592 if (mSettingsStates.get(key) == null) {
2593 final int maxBytesPerPackage = getMaxBytesPerPackageForType(getTypeFromKey(key));
Svetoslav Ganove080da92016-12-21 17:10:35 -08002594 SettingsState settingsState = new SettingsState(getContext(), mLock,
2595 getSettingsFile(key), key, maxBytesPerPackage, mHandlerThread.getLooper());
Svetoslav683914b2015-01-15 14:22:26 -08002596 mSettingsStates.put(key, settingsState);
2597 }
2598 }
2599
2600 public void removeUserStateLocked(int userId, boolean permanently) {
2601 // We always keep the global settings in memory.
2602
2603 // Nuke system settings.
2604 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2605 final SettingsState systemSettingsState = mSettingsStates.get(systemKey);
2606 if (systemSettingsState != null) {
2607 if (permanently) {
2608 mSettingsStates.remove(systemKey);
2609 systemSettingsState.destroyLocked(null);
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002610 } else {
Svetoslav683914b2015-01-15 14:22:26 -08002611 systemSettingsState.destroyLocked(new Runnable() {
2612 @Override
2613 public void run() {
2614 mSettingsStates.remove(systemKey);
2615 }
2616 });
2617 }
2618 }
2619
2620 // Nuke secure settings.
2621 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2622 final SettingsState secureSettingsState = mSettingsStates.get(secureKey);
2623 if (secureSettingsState != null) {
2624 if (permanently) {
2625 mSettingsStates.remove(secureKey);
2626 secureSettingsState.destroyLocked(null);
2627 } else {
2628 secureSettingsState.destroyLocked(new Runnable() {
2629 @Override
2630 public void run() {
2631 mSettingsStates.remove(secureKey);
2632 }
2633 });
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002634 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002635 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002636
Mark Rathjend891f012017-01-19 04:10:37 +00002637 // Nuke ssaid settings.
2638 final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId);
2639 final SettingsState ssaidSettingsState = mSettingsStates.get(ssaidKey);
2640 if (ssaidSettingsState != null) {
2641 if (permanently) {
2642 mSettingsStates.remove(ssaidKey);
2643 ssaidSettingsState.destroyLocked(null);
2644 } else {
2645 ssaidSettingsState.destroyLocked(new Runnable() {
2646 @Override
2647 public void run() {
2648 mSettingsStates.remove(ssaidKey);
2649 }
2650 });
2651 }
2652 }
2653
Svet Ganov53a441c2016-04-19 19:38:00 -07002654 // Nuke generation tracking data
2655 mGenerationRegistry.onUserRemoved(userId);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002656 }
2657
Svetoslav683914b2015-01-15 14:22:26 -08002658 public boolean insertSettingLocked(int type, int userId, String name, String value,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002659 String tag, boolean makeDefault, String packageName, boolean forceNotify,
2660 Set<String> criticalSettings) {
Matt Papec1323dc2018-12-11 12:32:42 -08002661 return insertSettingLocked(type, userId, name, value, tag, makeDefault, false,
2662 packageName, forceNotify, criticalSettings);
2663 }
2664
2665 public boolean insertSettingLocked(int type, int userId, String name, String value,
2666 String tag, boolean makeDefault, boolean forceNonSystemPackage, String packageName,
2667 boolean forceNotify, Set<String> criticalSettings) {
Svetoslav683914b2015-01-15 14:22:26 -08002668 final int key = makeKey(type, userId);
2669
Svetoslav Ganove080da92016-12-21 17:10:35 -08002670 boolean success = false;
Svetoslav683914b2015-01-15 14:22:26 -08002671 SettingsState settingsState = peekSettingsStateLocked(key);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002672 if (settingsState != null) {
2673 success = settingsState.insertSettingLocked(name, value,
Matt Papec1323dc2018-12-11 12:32:42 -08002674 tag, makeDefault, forceNonSystemPackage, packageName);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002675 }
Svetoslav683914b2015-01-15 14:22:26 -08002676
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002677 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2678 settingsState.persistSyncLocked();
2679 }
2680
Svet Ganov53a441c2016-04-19 19:38:00 -07002681 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08002682 notifyForSettingsChange(key, name);
2683 }
2684 return success;
2685 }
2686
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002687 public boolean deleteSettingLocked(int type, int userId, String name, boolean forceNotify,
2688 Set<String> criticalSettings) {
Svetoslav683914b2015-01-15 14:22:26 -08002689 final int key = makeKey(type, userId);
2690
Svetoslav Ganove080da92016-12-21 17:10:35 -08002691 boolean success = false;
Svetoslav683914b2015-01-15 14:22:26 -08002692 SettingsState settingsState = peekSettingsStateLocked(key);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002693 if (settingsState != null) {
2694 success = settingsState.deleteSettingLocked(name);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002695 }
Svetoslav683914b2015-01-15 14:22:26 -08002696
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002697 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2698 settingsState.persistSyncLocked();
2699 }
2700
Svet Ganov53a441c2016-04-19 19:38:00 -07002701 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08002702 notifyForSettingsChange(key, name);
2703 }
2704 return success;
2705 }
2706
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002707 public boolean updateSettingLocked(int type, int userId, String name, String value,
2708 String tag, boolean makeDefault, String packageName, boolean forceNotify,
2709 Set<String> criticalSettings) {
2710 final int key = makeKey(type, userId);
2711
2712 boolean success = false;
2713 SettingsState settingsState = peekSettingsStateLocked(key);
2714 if (settingsState != null) {
2715 success = settingsState.updateSettingLocked(name, value, tag,
2716 makeDefault, packageName);
2717 }
2718
2719 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2720 settingsState.persistSyncLocked();
2721 }
2722
2723 if (forceNotify || success) {
2724 notifyForSettingsChange(key, name);
2725 }
2726
2727 return success;
2728 }
2729
Svetoslav683914b2015-01-15 14:22:26 -08002730 public Setting getSettingLocked(int type, int userId, String name) {
2731 final int key = makeKey(type, userId);
2732
2733 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002734 if (settingsState == null) {
Mark Rathjenea617592017-01-18 23:03:41 -08002735 return null;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002736 }
Mark Rathjend891f012017-01-19 04:10:37 +00002737
2738 // getSettingLocked will return non-null result
Svetoslav683914b2015-01-15 14:22:26 -08002739 return settingsState.getSettingLocked(name);
2740 }
2741
Svetoslav Ganove080da92016-12-21 17:10:35 -08002742 public void resetSettingsLocked(int type, int userId, String packageName, int mode,
2743 String tag) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002744 resetSettingsLocked(type, userId, packageName, mode, tag, null);
2745 }
2746
2747 public void resetSettingsLocked(int type, int userId, String packageName, int mode,
2748 String tag, @Nullable String prefix) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08002749 final int key = makeKey(type, userId);
2750 SettingsState settingsState = peekSettingsStateLocked(key);
2751 if (settingsState == null) {
2752 return;
2753 }
2754
2755 switch (mode) {
2756 case Settings.RESET_MODE_PACKAGE_DEFAULTS: {
2757 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002758 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002759 Setting setting = settingsState.getSettingLocked(name);
2760 if (packageName.equals(setting.getPackageName())) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002761 if ((tag != null && !tag.equals(setting.getTag()))
2762 || (prefix != null && !setting.getName().startsWith(prefix))) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08002763 continue;
2764 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002765 if (settingsState.resetSettingLocked(name)) {
2766 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002767 notifyForSettingsChange(key, name);
2768 }
2769 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002770 if (someSettingChanged) {
2771 settingsState.persistSyncLocked();
2772 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002773 }
2774 } break;
2775
2776 case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: {
2777 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002778 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002779 Setting setting = settingsState.getSettingLocked(name);
2780 if (!SettingsState.isSystemPackage(getContext(),
Matt Pape25c940d2019-03-26 12:14:33 -07002781 setting.getPackageName(), INVALID_UID, userId)) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002782 if (prefix != null && !setting.getName().startsWith(prefix)) {
2783 continue;
2784 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002785 if (settingsState.resetSettingLocked(name)) {
2786 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002787 notifyForSettingsChange(key, name);
2788 }
2789 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002790 if (someSettingChanged) {
2791 settingsState.persistSyncLocked();
2792 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002793 }
2794 } break;
2795
2796 case Settings.RESET_MODE_UNTRUSTED_CHANGES: {
2797 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002798 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002799 Setting setting = settingsState.getSettingLocked(name);
2800 if (!SettingsState.isSystemPackage(getContext(),
Matt Pape25c940d2019-03-26 12:14:33 -07002801 setting.getPackageName(), INVALID_UID, userId)) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002802 if (prefix != null && !setting.getName().startsWith(prefix)) {
2803 continue;
2804 }
Eugene Suslad72c3972016-12-27 15:49:30 -08002805 if (setting.isDefaultFromSystem()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002806 if (settingsState.resetSettingLocked(name)) {
2807 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002808 notifyForSettingsChange(key, name);
2809 }
2810 } else if (settingsState.deleteSettingLocked(name)) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002811 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002812 notifyForSettingsChange(key, name);
2813 }
2814 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002815 if (someSettingChanged) {
2816 settingsState.persistSyncLocked();
2817 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002818 }
2819 } break;
2820
2821 case Settings.RESET_MODE_TRUSTED_DEFAULTS: {
2822 for (String name : settingsState.getSettingNamesLocked()) {
2823 Setting setting = settingsState.getSettingLocked(name);
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002824 boolean someSettingChanged = false;
Matt Pape6bfc62e2018-11-28 13:16:03 -08002825 if (prefix != null && !setting.getName().startsWith(prefix)) {
2826 continue;
2827 }
Eugene Suslad72c3972016-12-27 15:49:30 -08002828 if (setting.isDefaultFromSystem()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002829 if (settingsState.resetSettingLocked(name)) {
2830 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002831 notifyForSettingsChange(key, name);
2832 }
2833 } else if (settingsState.deleteSettingLocked(name)) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002834 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002835 notifyForSettingsChange(key, name);
2836 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002837 if (someSettingChanged) {
2838 settingsState.persistSyncLocked();
2839 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002840 }
2841 } break;
2842 }
2843 }
2844
Zimuzoc56192c2018-07-25 10:40:01 +01002845 public void removeSettingsForPackageLocked(String packageName, int userId) {
Svet Ganov8de34802015-04-27 09:33:40 -07002846 // Global and secure settings are signature protected. Apps signed
2847 // by the platform certificate are generally not uninstalled and
2848 // the main exception is tests. We trust components signed
2849 // by the platform certificate and do not do a clean up after them.
Svetoslav683914b2015-01-15 14:22:26 -08002850
2851 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2852 SettingsState systemSettings = mSettingsStates.get(systemKey);
Svet Ganov8de34802015-04-27 09:33:40 -07002853 if (systemSettings != null) {
Zimuzoc56192c2018-07-25 10:40:01 +01002854 systemSettings.removeSettingsForPackageLocked(packageName);
Svet Ganov8de34802015-04-27 09:33:40 -07002855 }
Svetoslav683914b2015-01-15 14:22:26 -08002856 }
2857
Mark Rathjend891f012017-01-19 04:10:37 +00002858 public void onUidRemovedLocked(int uid) {
2859 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID,
2860 UserHandle.getUserId(uid));
Tony Mak24c6ab42018-01-08 14:35:34 +00002861 if (ssaidSettings != null) {
2862 ssaidSettings.deleteSettingLocked(Integer.toString(uid));
2863 }
Mark Rathjend891f012017-01-19 04:10:37 +00002864 }
2865
Kweku Adamsb0886f32017-10-31 15:32:09 -07002866 @Nullable
Svetoslav683914b2015-01-15 14:22:26 -08002867 private SettingsState peekSettingsStateLocked(int key) {
2868 SettingsState settingsState = mSettingsStates.get(key);
2869 if (settingsState != null) {
2870 return settingsState;
2871 }
2872
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002873 if (!ensureSettingsForUserLocked(getUserIdFromKey(key))) {
2874 return null;
2875 }
Svetoslav683914b2015-01-15 14:22:26 -08002876 return mSettingsStates.get(key);
2877 }
2878
2879 private void migrateAllLegacySettingsIfNeeded() {
2880 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07002881 final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002882 File globalFile = getSettingsFile(key);
Amith Yamasani74bf71b2017-07-19 16:49:52 -07002883 if (SettingsState.stateFileExists(globalFile)) {
Svetoslav683914b2015-01-15 14:22:26 -08002884 return;
2885 }
2886
Amith Yamasani39452022017-03-21 15:23:47 -07002887 mSettingsCreationBuildId = Build.ID;
2888
Svetoslav683914b2015-01-15 14:22:26 -08002889 final long identity = Binder.clearCallingIdentity();
2890 try {
2891 List<UserInfo> users = mUserManager.getUsers(true);
2892
2893 final int userCount = users.size();
2894 for (int i = 0; i < userCount; i++) {
2895 final int userId = users.get(i).id;
2896
2897 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
2898 SQLiteDatabase database = dbHelper.getWritableDatabase();
2899 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
2900
2901 // Upgrade to the latest version.
2902 UpgradeController upgrader = new UpgradeController(userId);
2903 upgrader.upgradeIfNeededLocked();
2904
2905 // Drop from memory if not a running user.
2906 if (!mUserManager.isUserRunning(new UserHandle(userId))) {
2907 removeUserStateLocked(userId, false);
2908 }
2909 }
2910 } finally {
2911 Binder.restoreCallingIdentity(identity);
2912 }
2913 }
2914 }
2915
2916 private void migrateLegacySettingsForUserIfNeededLocked(int userId) {
2917 // Every user has secure settings and if no file we need to migrate.
2918 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2919 File secureFile = getSettingsFile(secureKey);
Amith Yamasani74bf71b2017-07-19 16:49:52 -07002920 if (SettingsState.stateFileExists(secureFile)) {
Svetoslav683914b2015-01-15 14:22:26 -08002921 return;
2922 }
2923
2924 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
2925 SQLiteDatabase database = dbHelper.getWritableDatabase();
2926
2927 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
2928 }
2929
2930 private void migrateLegacySettingsForUserLocked(DatabaseHelper dbHelper,
2931 SQLiteDatabase database, int userId) {
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002932 // Move over the system settings.
2933 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2934 ensureSettingsStateLocked(systemKey);
2935 SettingsState systemSettings = mSettingsStates.get(systemKey);
2936 migrateLegacySettingsLocked(systemSettings, database, TABLE_SYSTEM);
2937 systemSettings.persistSyncLocked();
Svetoslav683914b2015-01-15 14:22:26 -08002938
2939 // Move over the secure settings.
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002940 // Do this after System settings, since this is the first thing we check when deciding
2941 // to skip over migration from db to xml for a secondary user.
Svetoslav683914b2015-01-15 14:22:26 -08002942 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2943 ensureSettingsStateLocked(secureKey);
2944 SettingsState secureSettings = mSettingsStates.get(secureKey);
2945 migrateLegacySettingsLocked(secureSettings, database, TABLE_SECURE);
2946 ensureSecureSettingAndroidIdSetLocked(secureSettings);
2947 secureSettings.persistSyncLocked();
2948
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002949 // Move over the global settings if owner.
2950 // Do this last, since this is the first thing we check when deciding
2951 // to skip over migration from db to xml for owner user.
2952 if (userId == UserHandle.USER_SYSTEM) {
2953 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, userId);
2954 ensureSettingsStateLocked(globalKey);
2955 SettingsState globalSettings = mSettingsStates.get(globalKey);
2956 migrateLegacySettingsLocked(globalSettings, database, TABLE_GLOBAL);
Amith Yamasani39452022017-03-21 15:23:47 -07002957 // If this was just created
2958 if (mSettingsCreationBuildId != null) {
2959 globalSettings.insertSettingLocked(Settings.Global.DATABASE_CREATION_BUILDID,
2960 mSettingsCreationBuildId, null, true,
2961 SettingsState.SYSTEM_PACKAGE_NAME);
2962 }
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002963 globalSettings.persistSyncLocked();
2964 }
Svetoslav683914b2015-01-15 14:22:26 -08002965
2966 // Drop the database as now all is moved and persisted.
2967 if (DROP_DATABASE_ON_MIGRATION) {
2968 dbHelper.dropDatabase();
2969 } else {
2970 dbHelper.backupDatabase();
2971 }
2972 }
2973
2974 private void migrateLegacySettingsLocked(SettingsState settingsState,
2975 SQLiteDatabase database, String table) {
2976 SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
2977 queryBuilder.setTables(table);
2978
2979 Cursor cursor = queryBuilder.query(database, ALL_COLUMNS,
2980 null, null, null, null, null);
2981
2982 if (cursor == null) {
2983 return;
2984 }
2985
2986 try {
2987 if (!cursor.moveToFirst()) {
2988 return;
2989 }
2990
2991 final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME);
2992 final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE);
2993
2994 settingsState.setVersionLocked(database.getVersion());
2995
2996 while (!cursor.isAfterLast()) {
2997 String name = cursor.getString(nameColumnIdx);
2998 String value = cursor.getString(valueColumnIdx);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002999 settingsState.insertSettingLocked(name, value, null, true,
Svetoslav683914b2015-01-15 14:22:26 -08003000 SettingsState.SYSTEM_PACKAGE_NAME);
3001 cursor.moveToNext();
3002 }
3003 } finally {
3004 cursor.close();
3005 }
3006 }
3007
Andreas Gampeb58893072018-09-05 16:52:31 -07003008 @GuardedBy("secureSettings.mLock")
Svetoslav683914b2015-01-15 14:22:26 -08003009 private void ensureSecureSettingAndroidIdSetLocked(SettingsState secureSettings) {
3010 Setting value = secureSettings.getSettingLocked(Settings.Secure.ANDROID_ID);
3011
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09003012 if (!value.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08003013 return;
3014 }
3015
3016 final int userId = getUserIdFromKey(secureSettings.mKey);
3017
3018 final UserInfo user;
3019 final long identity = Binder.clearCallingIdentity();
3020 try {
3021 user = mUserManager.getUserInfo(userId);
3022 } finally {
3023 Binder.restoreCallingIdentity(identity);
3024 }
3025 if (user == null) {
3026 // Can happen due to races when deleting users - treat as benign.
3027 return;
3028 }
3029
3030 String androidId = Long.toHexString(new SecureRandom().nextLong());
3031 secureSettings.insertSettingLocked(Settings.Secure.ANDROID_ID, androidId,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003032 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Svetoslav683914b2015-01-15 14:22:26 -08003033
3034 Slog.d(LOG_TAG, "Generated and saved new ANDROID_ID [" + androidId
3035 + "] for user " + userId);
3036
3037 // Write a drop box entry if it's a restricted profile
3038 if (user.isRestricted()) {
3039 DropBoxManager dbm = (DropBoxManager) getContext().getSystemService(
3040 Context.DROPBOX_SERVICE);
3041 if (dbm != null && dbm.isTagEnabled(DROPBOX_TAG_USERLOG)) {
3042 dbm.addText(DROPBOX_TAG_USERLOG, System.currentTimeMillis()
3043 + "," + DROPBOX_TAG_USERLOG + "," + androidId + "\n");
3044 }
3045 }
3046 }
3047
3048 private void notifyForSettingsChange(int key, String name) {
Svet Ganov945864c2018-03-22 21:49:10 -07003049 // Increment the generation first, so observers always see the new value
Phil Weaver83fec002016-05-11 10:55:29 -07003050 mGenerationRegistry.incrementGeneration(key);
3051
Svet Ganov945864c2018-03-22 21:49:10 -07003052 if (isGlobalSettingsKey(key)) {
Jeff Sharkey308093f2018-03-25 22:53:29 -06003053 final long token = Binder.clearCallingIdentity();
3054 try {
3055 if (Global.LOCATION_GLOBAL_KILL_SWITCH.equals(name)) {
3056 // When the global kill switch is updated, send the
3057 // change notification for the location setting.
3058 notifyLocationChangeForRunningUsers();
3059 }
3060 notifyGlobalSettingChangeForRunningUsers(key, name);
3061 } finally {
3062 Binder.restoreCallingIdentity(token);
Svet Ganov945864c2018-03-22 21:49:10 -07003063 }
Svet Ganov945864c2018-03-22 21:49:10 -07003064 } else {
3065 final int userId = getUserIdFromKey(key);
3066 final Uri uri = getNotificationUriFor(key, name);
3067 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
3068 userId, 0, uri).sendToTarget();
3069 if (isSecureSettingsKey(key)) {
3070 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
3071 sSecureCloneToManagedSettings);
3072 maybeNotifyProfiles(SETTINGS_TYPE_SYSTEM, userId, uri, name,
3073 sSystemCloneFromParentOnDependency.values());
3074 } else if (isSystemSettingsKey(key)) {
3075 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
3076 sSystemCloneToManagedSettings);
3077 }
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003078 }
Svet Ganov53a441c2016-04-19 19:38:00 -07003079
Svet Ganov945864c2018-03-22 21:49:10 -07003080 // Always notify that our data changed
Svet Ganov53a441c2016-04-19 19:38:00 -07003081 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003082 }
3083
Svet Ganov53a441c2016-04-19 19:38:00 -07003084 private void maybeNotifyProfiles(int type, int userId, Uri uri, String name,
Robin Lee7af9a742017-02-20 14:47:30 +00003085 Collection<String> keysCloned) {
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003086 if (keysCloned.contains(name)) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003087 for (int profileId : mUserManager.getProfileIdsWithDisabled(userId)) {
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003088 // the notification for userId has already been sent.
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003089 if (profileId != userId) {
Svet Ganov945864c2018-03-22 21:49:10 -07003090 final int key = makeKey(type, profileId);
3091 // Increment the generation first, so observers always see the new value
3092 mGenerationRegistry.incrementGeneration(key);
Svetoslav7e0683b2015-08-03 16:02:52 -07003093 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003094 profileId, 0, uri).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003095 }
3096 }
3097 }
Svetoslav683914b2015-01-15 14:22:26 -08003098 }
3099
Svet Ganov945864c2018-03-22 21:49:10 -07003100 private void notifyGlobalSettingChangeForRunningUsers(int key, String name) {
3101 // Important: No need to update generation for each user as there
3102 // is a singleton generation entry for the global settings which
3103 // is already incremented be the caller.
3104 final Uri uri = getNotificationUriFor(key, name);
3105 final List<UserInfo> users = mUserManager.getUsers(/*excludeDying*/ true);
3106 for (int i = 0; i < users.size(); i++) {
3107 final int userId = users.get(i).id;
3108 if (mUserManager.isUserRunning(UserHandle.of(userId))) {
3109 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
3110 userId, 0, uri).sendToTarget();
3111 }
3112 }
3113 }
3114
Makoto Onuki0000d322017-11-28 16:31:47 -08003115 private void notifyLocationChangeForRunningUsers() {
3116 final List<UserInfo> users = mUserManager.getUsers(/*excludeDying=*/ true);
3117
3118 for (int i = 0; i < users.size(); i++) {
3119 final int userId = users.get(i).id;
3120
3121 if (!mUserManager.isUserRunning(UserHandle.of(userId))) {
3122 continue;
3123 }
Makoto Onuki0000d322017-11-28 16:31:47 -08003124
Svet Ganov945864c2018-03-22 21:49:10 -07003125 // Increment the generation first, so observers always see the new value
3126 final int key = makeKey(SETTINGS_TYPE_SECURE, userId);
3127 mGenerationRegistry.incrementGeneration(key);
3128
Kweku Adams5e0052b2019-02-22 15:17:52 -08003129 final Uri uri = getNotificationUriFor(key, Secure.LOCATION_MODE);
Makoto Onuki0000d322017-11-28 16:31:47 -08003130 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
3131 userId, 0, uri).sendToTarget();
3132 }
3133 }
3134
Matt Pape1b31a332018-10-17 09:58:28 -07003135 private boolean isConfigSettingsKey(int key) {
3136 return getTypeFromKey(key) == SETTINGS_TYPE_CONFIG;
3137 }
3138
Svetoslav683914b2015-01-15 14:22:26 -08003139 private boolean isGlobalSettingsKey(int key) {
3140 return getTypeFromKey(key) == SETTINGS_TYPE_GLOBAL;
3141 }
3142
3143 private boolean isSystemSettingsKey(int key) {
3144 return getTypeFromKey(key) == SETTINGS_TYPE_SYSTEM;
3145 }
3146
3147 private boolean isSecureSettingsKey(int key) {
3148 return getTypeFromKey(key) == SETTINGS_TYPE_SECURE;
3149 }
3150
Mark Rathjend891f012017-01-19 04:10:37 +00003151 private boolean isSsaidSettingsKey(int key) {
3152 return getTypeFromKey(key) == SETTINGS_TYPE_SSAID;
3153 }
3154
Svetoslav683914b2015-01-15 14:22:26 -08003155 private File getSettingsFile(int key) {
Matt Pape1b31a332018-10-17 09:58:28 -07003156 if (isConfigSettingsKey(key)) {
3157 final int userId = getUserIdFromKey(key);
3158 return new File(Environment.getUserSystemDirectory(userId),
3159 SETTINGS_FILE_CONFIG);
3160 } else if (isGlobalSettingsKey(key)) {
Svetoslav683914b2015-01-15 14:22:26 -08003161 final int userId = getUserIdFromKey(key);
3162 return new File(Environment.getUserSystemDirectory(userId),
3163 SETTINGS_FILE_GLOBAL);
3164 } else if (isSystemSettingsKey(key)) {
3165 final int userId = getUserIdFromKey(key);
3166 return new File(Environment.getUserSystemDirectory(userId),
3167 SETTINGS_FILE_SYSTEM);
3168 } else if (isSecureSettingsKey(key)) {
3169 final int userId = getUserIdFromKey(key);
3170 return new File(Environment.getUserSystemDirectory(userId),
3171 SETTINGS_FILE_SECURE);
Mark Rathjend891f012017-01-19 04:10:37 +00003172 } else if (isSsaidSettingsKey(key)) {
3173 final int userId = getUserIdFromKey(key);
3174 return new File(Environment.getUserSystemDirectory(userId),
3175 SETTINGS_FILE_SSAID);
Svetoslav683914b2015-01-15 14:22:26 -08003176 } else {
3177 throw new IllegalArgumentException("Invalid settings key:" + key);
3178 }
3179 }
3180
3181 private Uri getNotificationUriFor(int key, String name) {
Matt Pape1b31a332018-10-17 09:58:28 -07003182 if (isConfigSettingsKey(key)) {
Matt Pape1278d1c2018-12-11 13:03:49 -08003183 return (name != null) ? Uri.withAppendedPath(DeviceConfig.CONTENT_URI, name)
3184 : DeviceConfig.CONTENT_URI;
Matt Pape1b31a332018-10-17 09:58:28 -07003185 } else if (isGlobalSettingsKey(key)) {
Svetoslav683914b2015-01-15 14:22:26 -08003186 return (name != null) ? Uri.withAppendedPath(Settings.Global.CONTENT_URI, name)
3187 : Settings.Global.CONTENT_URI;
3188 } else if (isSecureSettingsKey(key)) {
3189 return (name != null) ? Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name)
3190 : Settings.Secure.CONTENT_URI;
3191 } else if (isSystemSettingsKey(key)) {
3192 return (name != null) ? Uri.withAppendedPath(Settings.System.CONTENT_URI, name)
3193 : Settings.System.CONTENT_URI;
3194 } else {
3195 throw new IllegalArgumentException("Invalid settings key:" + key);
3196 }
3197 }
3198
3199 private int getMaxBytesPerPackageForType(int type) {
3200 switch (type) {
Matt Pape1b31a332018-10-17 09:58:28 -07003201 case SETTINGS_TYPE_CONFIG:
Svetoslav683914b2015-01-15 14:22:26 -08003202 case SETTINGS_TYPE_GLOBAL:
Mark Rathjend891f012017-01-19 04:10:37 +00003203 case SETTINGS_TYPE_SECURE:
3204 case SETTINGS_TYPE_SSAID: {
Svetoslav683914b2015-01-15 14:22:26 -08003205 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED;
3206 }
3207
3208 default: {
3209 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_LIMITED;
3210 }
3211 }
3212 }
3213
Svetoslav7e0683b2015-08-03 16:02:52 -07003214 private final class MyHandler extends Handler {
3215 private static final int MSG_NOTIFY_URI_CHANGED = 1;
3216 private static final int MSG_NOTIFY_DATA_CHANGED = 2;
3217
3218 public MyHandler(Looper looper) {
3219 super(looper);
3220 }
3221
3222 @Override
3223 public void handleMessage(Message msg) {
3224 switch (msg.what) {
3225 case MSG_NOTIFY_URI_CHANGED: {
3226 final int userId = msg.arg1;
3227 Uri uri = (Uri) msg.obj;
Jeff Sharkey88f9d0b2017-08-11 15:29:40 -06003228 try {
3229 getContext().getContentResolver().notifyChange(uri, null, true, userId);
3230 } catch (SecurityException e) {
3231 Slog.w(LOG_TAG, "Failed to notify for " + userId + ": " + uri, e);
3232 }
Makoto Onuki0000d322017-11-28 16:31:47 -08003233 if (DEBUG || true) {
Svetoslav7e0683b2015-08-03 16:02:52 -07003234 Slog.v(LOG_TAG, "Notifying for " + userId + ": " + uri);
3235 }
3236 } break;
3237
3238 case MSG_NOTIFY_DATA_CHANGED: {
3239 mBackupManager.dataChanged();
3240 } break;
3241 }
3242 }
3243 }
3244
Svetoslav683914b2015-01-15 14:22:26 -08003245 private final class UpgradeController {
Winson Chungd9f2fb32019-03-05 11:10:12 -08003246 private static final int SETTINGS_VERSION = 177;
Svetoslav683914b2015-01-15 14:22:26 -08003247
3248 private final int mUserId;
3249
3250 public UpgradeController(int userId) {
3251 mUserId = userId;
3252 }
3253
3254 public void upgradeIfNeededLocked() {
3255 // The version of all settings for a user is the same (all users have secure).
3256 SettingsState secureSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07003257 SETTINGS_TYPE_SECURE, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08003258
3259 // Try an update from the current state.
3260 final int oldVersion = secureSettings.getVersionLocked();
3261 final int newVersion = SETTINGS_VERSION;
3262
Svet Ganovc9755bc2015-03-28 13:21:22 -07003263 // If up do date - done.
Svetoslav683914b2015-01-15 14:22:26 -08003264 if (oldVersion == newVersion) {
3265 return;
3266 }
3267
3268 // Try to upgrade.
3269 final int curVersion = onUpgradeLocked(mUserId, oldVersion, newVersion);
3270
3271 // If upgrade failed start from scratch and upgrade.
3272 if (curVersion != newVersion) {
3273 // Drop state we have for this user.
3274 removeUserStateLocked(mUserId, true);
3275
3276 // Recreate the database.
3277 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), mUserId);
3278 SQLiteDatabase database = dbHelper.getWritableDatabase();
3279 dbHelper.recreateDatabase(database, newVersion, curVersion, oldVersion);
3280
3281 // Migrate the settings for this user.
3282 migrateLegacySettingsForUserLocked(dbHelper, database, mUserId);
3283
3284 // Now upgrade should work fine.
3285 onUpgradeLocked(mUserId, oldVersion, newVersion);
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07003286
3287 // Make a note what happened, so we don't wonder why data was lost
3288 String reason = "Settings rebuilt! Current version: "
3289 + curVersion + " while expected: " + newVersion;
3290 getGlobalSettingsLocked().insertSettingLocked(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003291 Settings.Global.DATABASE_DOWNGRADE_REASON,
3292 reason, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Svetoslav683914b2015-01-15 14:22:26 -08003293 }
3294
3295 // Set the global settings version if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07003296 if (mUserId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08003297 SettingsState globalSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07003298 SETTINGS_TYPE_GLOBAL, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08003299 globalSettings.setVersionLocked(newVersion);
3300 }
3301
3302 // Set the secure settings version.
3303 secureSettings.setVersionLocked(newVersion);
3304
3305 // Set the system settings version.
3306 SettingsState systemSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07003307 SETTINGS_TYPE_SYSTEM, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08003308 systemSettings.setVersionLocked(newVersion);
3309 }
3310
3311 private SettingsState getGlobalSettingsLocked() {
Xiaohui Chen43765b72015-08-31 10:57:33 -07003312 return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08003313 }
3314
3315 private SettingsState getSecureSettingsLocked(int userId) {
3316 return getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
3317 }
3318
Mark Rathjend891f012017-01-19 04:10:37 +00003319 private SettingsState getSsaidSettingsLocked(int userId) {
3320 return getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
3321 }
3322
Svetoslav683914b2015-01-15 14:22:26 -08003323 private SettingsState getSystemSettingsLocked(int userId) {
3324 return getSettingsLocked(SETTINGS_TYPE_SYSTEM, userId);
3325 }
3326
Jeff Brown503cffc2015-03-26 18:08:51 -07003327 /**
3328 * You must perform all necessary mutations to bring the settings
3329 * for this user from the old to the new version. When you add a new
3330 * upgrade step you *must* update SETTINGS_VERSION.
3331 *
3332 * This is an example of moving a setting from secure to global.
3333 *
3334 * // v119: Example settings changes.
3335 * if (currentVersion == 118) {
3336 * if (userId == UserHandle.USER_OWNER) {
3337 * // Remove from the secure settings.
3338 * SettingsState secureSettings = getSecureSettingsLocked(userId);
3339 * String name = "example_setting_to_move";
3340 * String value = secureSettings.getSetting(name);
3341 * secureSettings.deleteSetting(name);
3342 *
3343 * // Add to the global settings.
3344 * SettingsState globalSettings = getGlobalSettingsLocked();
3345 * globalSettings.insertSetting(name, value, SettingsState.SYSTEM_PACKAGE_NAME);
3346 * }
3347 *
3348 * // Update the current version.
3349 * currentVersion = 119;
3350 * }
3351 */
Svetoslav683914b2015-01-15 14:22:26 -08003352 private int onUpgradeLocked(int userId, int oldVersion, int newVersion) {
3353 if (DEBUG) {
3354 Slog.w(LOG_TAG, "Upgrading settings for user: " + userId + " from version: "
3355 + oldVersion + " to version: " + newVersion);
3356 }
3357
Jeff Brown503cffc2015-03-26 18:08:51 -07003358 int currentVersion = oldVersion;
Svetoslav683914b2015-01-15 14:22:26 -08003359
John Spurlocke11ae112015-05-11 16:09:03 -04003360 // v119: Reset zen + ringer mode.
3361 if (currentVersion == 118) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07003362 if (userId == UserHandle.USER_SYSTEM) {
John Spurlocke11ae112015-05-11 16:09:03 -04003363 final SettingsState globalSettings = getGlobalSettingsLocked();
3364 globalSettings.updateSettingLocked(Settings.Global.ZEN_MODE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003365 Integer.toString(Settings.Global.ZEN_MODE_OFF), null,
3366 true, SettingsState.SYSTEM_PACKAGE_NAME);
John Spurlocke11ae112015-05-11 16:09:03 -04003367 globalSettings.updateSettingLocked(Settings.Global.MODE_RINGER,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003368 Integer.toString(AudioManager.RINGER_MODE_NORMAL), null,
3369 true, SettingsState.SYSTEM_PACKAGE_NAME);
John Spurlocke11ae112015-05-11 16:09:03 -04003370 }
3371 currentVersion = 119;
3372 }
3373
Jason Monk27bbb2d2015-03-31 16:46:39 -04003374 // v120: Add double tap to wake setting.
3375 if (currentVersion == 119) {
3376 SettingsState secureSettings = getSecureSettingsLocked(userId);
3377 secureSettings.insertSettingLocked(Settings.Secure.DOUBLE_TAP_TO_WAKE,
3378 getContext().getResources().getBoolean(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003379 R.bool.def_double_tap_to_wake) ? "1" : "0", null, true,
Jason Monk27bbb2d2015-03-31 16:46:39 -04003380 SettingsState.SYSTEM_PACKAGE_NAME);
3381
3382 currentVersion = 120;
3383 }
3384
Svetoslav7e0683b2015-08-03 16:02:52 -07003385 if (currentVersion == 120) {
3386 // Before 121, we used a different string encoding logic. We just bump the
3387 // version here; SettingsState knows how to handle pre-version 120 files.
3388 currentVersion = 121;
3389 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -07003390
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003391 if (currentVersion == 121) {
3392 // Version 122: allow OEMs to set a default payment component in resources.
3393 // Note that we only write the default if no default has been set;
3394 // if there is, we just leave the default at whatever it currently is.
3395 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3396 String defaultComponent = (getContext().getResources().getString(
3397 R.string.def_nfc_payment_component));
3398 Setting currentSetting = secureSettings.getSettingLocked(
3399 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT);
3400 if (defaultComponent != null && !defaultComponent.isEmpty() &&
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003401 currentSetting.isNull()) {
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003402 secureSettings.insertSettingLocked(
3403 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003404 defaultComponent, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003405 }
3406 currentVersion = 122;
3407 }
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003408
3409 if (currentVersion == 122) {
3410 // Version 123: Adding a default value for the ability to add a user from
3411 // the lock screen.
3412 if (userId == UserHandle.USER_SYSTEM) {
3413 final SettingsState globalSettings = getGlobalSettingsLocked();
3414 Setting currentSetting = globalSettings.getSettingLocked(
3415 Settings.Global.ADD_USERS_WHEN_LOCKED);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003416 if (currentSetting.isNull()) {
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003417 globalSettings.insertSettingLocked(
3418 Settings.Global.ADD_USERS_WHEN_LOCKED,
3419 getContext().getResources().getBoolean(
3420 R.bool.def_add_users_from_lockscreen) ? "1" : "0",
Svetoslav Ganove080da92016-12-21 17:10:35 -08003421 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003422 }
3423 }
3424 currentVersion = 123;
3425 }
Bryce Leebd179282015-12-17 19:01:37 -08003426
3427 if (currentVersion == 123) {
Bryce Leeec85f342015-12-16 13:32:28 -08003428 final SettingsState globalSettings = getGlobalSettingsLocked();
3429 String defaultDisabledProfiles = (getContext().getResources().getString(
3430 R.string.def_bluetooth_disabled_profiles));
3431 globalSettings.insertSettingLocked(Settings.Global.BLUETOOTH_DISABLED_PROFILES,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003432 defaultDisabledProfiles, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Bryce Leebd179282015-12-17 19:01:37 -08003433 currentVersion = 124;
Bryce Leeec85f342015-12-16 13:32:28 -08003434 }
3435
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003436 if (currentVersion == 124) {
3437 // Version 124: allow OEMs to set a default value for whether IME should be
3438 // shown when a physical keyboard is connected.
3439 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3440 Setting currentSetting = secureSettings.getSettingLocked(
3441 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003442 if (currentSetting.isNull()) {
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003443 secureSettings.insertSettingLocked(
3444 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD,
3445 getContext().getResources().getBoolean(
3446 R.bool.def_show_ime_with_hard_keyboard) ? "1" : "0",
Svetoslav Ganove080da92016-12-21 17:10:35 -08003447 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003448 }
3449 currentVersion = 125;
3450 }
3451
Ruben Brunk98576cf2016-03-07 18:54:28 -08003452 if (currentVersion == 125) {
3453 // Version 125: Allow OEMs to set the default VR service.
3454 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3455
3456 Setting currentSetting = secureSettings.getSettingLocked(
3457 Settings.Secure.ENABLED_VR_LISTENERS);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003458 if (currentSetting.isNull()) {
Ruben Brunk98576cf2016-03-07 18:54:28 -08003459 ArraySet<ComponentName> l =
3460 SystemConfig.getInstance().getDefaultVrComponents();
3461
3462 if (l != null && !l.isEmpty()) {
3463 StringBuilder b = new StringBuilder();
3464 boolean start = true;
3465 for (ComponentName c : l) {
3466 if (!start) {
3467 b.append(':');
3468 }
3469 b.append(c.flattenToString());
3470 start = false;
3471 }
3472 secureSettings.insertSettingLocked(
3473 Settings.Secure.ENABLED_VR_LISTENERS, b.toString(),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003474 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Ruben Brunk98576cf2016-03-07 18:54:28 -08003475 }
3476
3477 }
3478 currentVersion = 126;
3479 }
3480
Daniel U02ba6122016-04-01 18:41:42 +01003481 if (currentVersion == 126) {
3482 // Version 126: copy the primary values of LOCK_SCREEN_SHOW_NOTIFICATIONS and
3483 // LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS into managed profile.
3484 if (mUserManager.isManagedProfile(userId)) {
3485 final SettingsState systemSecureSettings =
3486 getSecureSettingsLocked(UserHandle.USER_SYSTEM);
3487
3488 final Setting showNotifications = systemSecureSettings.getSettingLocked(
3489 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09003490 if (!showNotifications.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01003491 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3492 secureSettings.insertSettingLocked(
3493 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003494 showNotifications.getValue(), null, true,
Daniel U02ba6122016-04-01 18:41:42 +01003495 SettingsState.SYSTEM_PACKAGE_NAME);
3496 }
3497
3498 final Setting allowPrivate = systemSecureSettings.getSettingLocked(
3499 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09003500 if (!allowPrivate.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01003501 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3502 secureSettings.insertSettingLocked(
3503 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003504 allowPrivate.getValue(), null, true,
Daniel U02ba6122016-04-01 18:41:42 +01003505 SettingsState.SYSTEM_PACKAGE_NAME);
3506 }
3507 }
3508 currentVersion = 127;
3509 }
3510
Steven Ngdc20ba62016-04-26 18:19:04 +01003511 if (currentVersion == 127) {
Mahaver Chopra3d9805d2016-07-07 16:25:05 +01003512 // version 127 is no longer used.
Steven Ngdc20ba62016-04-26 18:19:04 +01003513 currentVersion = 128;
3514 }
3515
Julia Reynolds1f721e12016-07-11 08:50:58 -04003516 if (currentVersion == 128) {
Julia Reynoldsb852e562017-06-06 16:14:18 -04003517 // Version 128: Removed
Julia Reynolds1f721e12016-07-11 08:50:58 -04003518 currentVersion = 129;
3519 }
3520
Dan Sandler71f85e92016-07-20 13:46:05 -04003521 if (currentVersion == 129) {
3522 // default longpress timeout changed from 500 to 400. If unchanged from the old
3523 // default, update to the new default.
3524 final SettingsState systemSecureSettings =
3525 getSecureSettingsLocked(userId);
3526 final String oldValue = systemSecureSettings.getSettingLocked(
3527 Settings.Secure.LONG_PRESS_TIMEOUT).getValue();
3528 if (TextUtils.equals("500", oldValue)) {
3529 systemSecureSettings.insertSettingLocked(
3530 Settings.Secure.LONG_PRESS_TIMEOUT,
3531 String.valueOf(getContext().getResources().getInteger(
3532 R.integer.def_long_press_timeout_millis)),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003533 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Dan Sandler71f85e92016-07-20 13:46:05 -04003534 }
3535 currentVersion = 130;
3536 }
3537
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003538 if (currentVersion == 130) {
Adrian Roos69741a22016-10-21 14:49:17 -07003539 // Split Ambient settings
3540 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3541 boolean dozeExplicitlyDisabled = "0".equals(secureSettings.
3542 getSettingLocked(Settings.Secure.DOZE_ENABLED).getValue());
3543
3544 if (dozeExplicitlyDisabled) {
Lucas Dupin4359b552018-08-09 15:07:54 -07003545 secureSettings.insertSettingLocked(Settings.Secure.DOZE_PICK_UP_GESTURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003546 "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Lucas Dupin4359b552018-08-09 15:07:54 -07003547 secureSettings.insertSettingLocked(Settings.Secure.DOZE_DOUBLE_TAP_GESTURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003548 "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Adrian Roos69741a22016-10-21 14:49:17 -07003549 }
3550 currentVersion = 131;
3551 }
3552
3553 if (currentVersion == 131) {
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003554 // Initialize new multi-press timeout to default value
3555 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3556 final String oldValue = systemSecureSettings.getSettingLocked(
3557 Settings.Secure.MULTI_PRESS_TIMEOUT).getValue();
3558 if (TextUtils.equals(null, oldValue)) {
3559 systemSecureSettings.insertSettingLocked(
3560 Settings.Secure.MULTI_PRESS_TIMEOUT,
3561 String.valueOf(getContext().getResources().getInteger(
3562 R.integer.def_multi_press_timeout_millis)),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003563 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003564 }
3565
Adrian Roos69741a22016-10-21 14:49:17 -07003566 currentVersion = 132;
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003567 }
3568
Adrian Roos69741a22016-10-21 14:49:17 -07003569 if (currentVersion == 132) {
3570 // Version 132: Allow managed profile to optionally use the parent's ringtones
Andre Lagoea35e072016-08-04 13:41:13 +01003571 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3572 String defaultSyncParentSounds = (getContext().getResources()
3573 .getBoolean(R.bool.def_sync_parent_sounds) ? "1" : "0");
3574 systemSecureSettings.insertSettingLocked(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003575 Settings.Secure.SYNC_PARENT_SOUNDS, defaultSyncParentSounds,
3576 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Adrian Roos69741a22016-10-21 14:49:17 -07003577 currentVersion = 133;
Andre Lagoea35e072016-08-04 13:41:13 +01003578 }
3579
Adrian Roos69741a22016-10-21 14:49:17 -07003580 if (currentVersion == 133) {
3581 // Version 133: Add default end button behavior
Keun-young Parkec7a1182016-10-18 11:52:38 -07003582 final SettingsState systemSettings = getSystemSettingsLocked(userId);
davidlnedb31e12018-09-25 10:21:20 -07003583 if (systemSettings.getSettingLocked(Settings.System.END_BUTTON_BEHAVIOR)
3584 .isNull()) {
Keun-young Parkec7a1182016-10-18 11:52:38 -07003585 String defaultEndButtonBehavior = Integer.toString(getContext()
3586 .getResources().getInteger(R.integer.def_end_button_behavior));
3587 systemSettings.insertSettingLocked(Settings.System.END_BUTTON_BEHAVIOR,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003588 defaultEndButtonBehavior, null, true,
3589 SettingsState.SYSTEM_PACKAGE_NAME);
Keun-young Parkec7a1182016-10-18 11:52:38 -07003590 }
Adrian Roos69741a22016-10-21 14:49:17 -07003591 currentVersion = 134;
Keun-young Parkec7a1182016-10-18 11:52:38 -07003592 }
3593
Phil Weaver89e3ffc2016-09-19 13:51:10 -07003594 if (currentVersion == 134) {
3595 // Remove setting that specifies if magnification values should be preserved.
3596 // This setting defaulted to true and never has a UI.
3597 getSecureSettingsLocked(userId).deleteSettingLocked(
3598 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_AUTO_UPDATE);
3599 currentVersion = 135;
3600 }
3601
Jeremy Joslin8bdad342016-12-14 11:46:47 -08003602 if (currentVersion == 135) {
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003603 // Version 135 no longer used.
Jeremy Joslin8bdad342016-12-14 11:46:47 -08003604 currentVersion = 136;
3605 }
3606
Mark Rathjend891f012017-01-19 04:10:37 +00003607 if (currentVersion == 136) {
3608 // Version 136: Store legacy SSAID for all apps currently installed on the
3609 // device as first step in migrating SSAID to be unique per application.
3610
3611 final boolean isUpgrade;
3612 try {
Svet Ganovd8eb8b22019-04-05 18:52:08 -07003613 isUpgrade = mPackageManager.isDeviceUpgrading();
Mark Rathjend891f012017-01-19 04:10:37 +00003614 } catch (RemoteException e) {
3615 throw new IllegalStateException("Package manager not available");
3616 }
3617 // Only retain legacy ssaid if the device is performing an OTA. After wiping
3618 // user data or first boot on a new device should use new ssaid generation.
3619 if (isUpgrade) {
3620 // Retrieve the legacy ssaid from the secure settings table.
Mark Rathjenea617592017-01-18 23:03:41 -08003621 final Setting legacySsaidSetting = getSettingLocked(SETTINGS_TYPE_SECURE,
3622 userId, Settings.Secure.ANDROID_ID);
3623 if (legacySsaidSetting == null || legacySsaidSetting.isNull()
3624 || legacySsaidSetting.getValue() == null) {
3625 throw new IllegalStateException("Legacy ssaid not accessible");
3626 }
3627 final String legacySsaid = legacySsaidSetting.getValue();
Mark Rathjend891f012017-01-19 04:10:37 +00003628
3629 // Fill each uid with the legacy ssaid to be backwards compatible.
3630 final List<PackageInfo> packages;
3631 try {
Tetsutoki Shiozawaebe0e5f2018-01-17 11:07:09 +09003632 packages = mPackageManager.getInstalledPackages(
3633 PackageManager.MATCH_UNINSTALLED_PACKAGES,
3634 userId).getList();
Mark Rathjend891f012017-01-19 04:10:37 +00003635 } catch (RemoteException e) {
3636 throw new IllegalStateException("Package manager not available");
3637 }
3638
3639 final SettingsState ssaidSettings = getSsaidSettingsLocked(userId);
3640 for (PackageInfo info : packages) {
3641 // Check if the UID already has an entry in the table.
3642 final String uid = Integer.toString(info.applicationInfo.uid);
3643 final Setting ssaid = ssaidSettings.getSettingLocked(uid);
3644
3645 if (ssaid.isNull() || ssaid.getValue() == null) {
3646 // Android Id doesn't exist for this package so create it.
3647 ssaidSettings.insertSettingLocked(uid, legacySsaid, null, true,
3648 info.packageName);
Tetsutoki Shiozawaebe0e5f2018-01-17 11:07:09 +09003649 if (DEBUG) {
3650 Slog.d(LOG_TAG, "Keep the legacy ssaid for uid=" + uid);
3651 }
Mark Rathjend891f012017-01-19 04:10:37 +00003652 }
3653 }
3654 }
3655
3656 currentVersion = 137;
3657 }
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003658 if (currentVersion == 137) {
3659 // Version 138: Settings.Secure#INSTALL_NON_MARKET_APPS is deprecated and its
3660 // default value set to 1. The user can no longer change the value of this
3661 // setting through the UI.
3662 final SettingsState secureSetting = getSecureSettingsLocked(userId);
3663 if (!mUserManager.hasUserRestriction(
Suprabh Shukla0b1356f2017-02-21 14:33:50 -08003664 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, UserHandle.of(userId))
3665 && secureSetting.getSettingLocked(
3666 Settings.Secure.INSTALL_NON_MARKET_APPS).getValue().equals("0")) {
3667
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003668 secureSetting.insertSettingLocked(Settings.Secure.INSTALL_NON_MARKET_APPS,
3669 "1", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shukla0b1356f2017-02-21 14:33:50 -08003670 // For managed profiles with profile owners, DevicePolicyManagerService
3671 // may want to set the user restriction in this case
3672 secureSetting.insertSettingLocked(
3673 Settings.Secure.UNKNOWN_SOURCES_DEFAULT_REVERSED, "1", null, true,
3674 SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003675 }
3676 currentVersion = 138;
3677 }
Mark Rathjend891f012017-01-19 04:10:37 +00003678
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003679 if (currentVersion == 138) {
Jeremy Joslin27d14c42017-02-15 12:02:03 -08003680 // Version 139: Removed.
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003681 currentVersion = 139;
3682 }
3683
Phil Weaver385912e2017-02-10 10:06:56 -08003684 if (currentVersion == 139) {
3685 // Version 140: Settings.Secure#ACCESSIBILITY_SPEAK_PASSWORD is deprecated and
3686 // the user can no longer change the value of this setting through the UI.
3687 // Force to true.
3688 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3689 secureSettings.updateSettingLocked(Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD,
3690 "1", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3691 currentVersion = 140;
3692 }
3693
Julia Reynoldsad0d9e02017-02-15 08:41:48 -05003694 if (currentVersion == 140) {
Julia Reynoldsb852e562017-06-06 16:14:18 -04003695 // Version 141: Removed
Julia Reynoldsad0d9e02017-02-15 08:41:48 -05003696 currentVersion = 141;
3697 }
3698
Svet Ganov13701552017-02-23 12:45:17 -08003699 if (currentVersion == 141) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003700 // This implementation was incorrectly setting the current value of
3701 // settings changed by non-system packages as the default which default
3702 // is set by the system. We add a new upgrade step at the end to properly
3703 // handle this case which would also fix incorrect changes made by the
3704 // old implementation of this step.
Svet Ganov13701552017-02-23 12:45:17 -08003705 currentVersion = 142;
3706 }
3707
Stephen Chen5d0922f2017-03-27 10:28:04 -07003708 if (currentVersion == 142) {
Felipe Lemeff355092017-04-03 12:55:02 -07003709 // Version 143: Set a default value for Wi-Fi wakeup feature.
Stephen Chen5d0922f2017-03-27 10:28:04 -07003710 if (userId == UserHandle.USER_SYSTEM) {
3711 final SettingsState globalSettings = getGlobalSettingsLocked();
3712 Setting currentSetting = globalSettings.getSettingLocked(
3713 Settings.Global.WIFI_WAKEUP_ENABLED);
3714 if (currentSetting.isNull()) {
3715 globalSettings.insertSettingLocked(
3716 Settings.Global.WIFI_WAKEUP_ENABLED,
3717 getContext().getResources().getBoolean(
3718 R.bool.def_wifi_wakeup_enabled) ? "1" : "0",
3719 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3720 }
3721 }
3722
3723 currentVersion = 143;
3724 }
3725
Felipe Lemeff355092017-04-03 12:55:02 -07003726 if (currentVersion == 143) {
3727 // Version 144: Set a default value for Autofill service.
3728 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3729 final Setting currentSetting = secureSettings
3730 .getSettingLocked(Settings.Secure.AUTOFILL_SERVICE);
3731 if (currentSetting.isNull()) {
3732 final String defaultValue = getContext().getResources().getString(
3733 com.android.internal.R.string.config_defaultAutofillService);
3734 if (defaultValue != null) {
3735 Slog.d(LOG_TAG, "Setting [" + defaultValue + "] as Autofill Service "
3736 + "for user " + userId);
3737 secureSettings.insertSettingLocked(Settings.Secure.AUTOFILL_SERVICE,
3738 defaultValue, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3739 }
3740 }
3741
3742 currentVersion = 144;
3743 }
3744
Jeremy Joslin45caa252017-05-04 11:22:46 -07003745 if (currentVersion == 144) {
Amin Shaikh86367962017-06-07 08:58:22 -07003746 // Version 145: Removed
3747 currentVersion = 145;
3748 }
3749
3750 if (currentVersion == 145) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003751 // Version 146: In step 142 we had a bug where incorrectly
3752 // some settings were considered system set and as a result
3753 // made the default and marked as the default being set by
3754 // the system. Here reevaluate the default and default system
3755 // set flags. This would both fix corruption by the old impl
3756 // of step 142 and also properly handle devices which never
3757 // run 142.
Jeremy Joslin45caa252017-05-04 11:22:46 -07003758 if (userId == UserHandle.USER_SYSTEM) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003759 SettingsState globalSettings = getGlobalSettingsLocked();
3760 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(globalSettings, userId);
3761 globalSettings.persistSyncLocked();
Jeremy Joslin45caa252017-05-04 11:22:46 -07003762 }
3763
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003764 SettingsState secureSettings = getSecureSettingsLocked(mUserId);
3765 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(secureSettings, userId);
3766 secureSettings.persistSyncLocked();
3767
3768 SettingsState systemSettings = getSystemSettingsLocked(mUserId);
3769 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(systemSettings, userId);
3770 systemSettings.persistSyncLocked();
3771
Amin Shaikh86367962017-06-07 08:58:22 -07003772 currentVersion = 146;
Jeremy Joslin45caa252017-05-04 11:22:46 -07003773 }
3774
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003775 if (currentVersion == 146) {
Joe LaPenna250d7842018-01-25 10:19:42 -08003776 // Version 147: Removed. (This version previously allowed showing the
3777 // "wifi_wakeup_available" setting).
3778 // The setting that was added here is deleted in 153.
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003779 currentVersion = 147;
3780 }
3781
3782 if (currentVersion == 147) {
3783 // Version 148: Set the default value for DEFAULT_RESTRICT_BACKGROUND_DATA.
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003784 if (userId == UserHandle.USER_SYSTEM) {
3785 final SettingsState globalSettings = getGlobalSettingsLocked();
3786 final Setting currentSetting = globalSettings.getSettingLocked(
3787 Global.DEFAULT_RESTRICT_BACKGROUND_DATA);
3788 if (currentSetting.isNull()) {
3789 globalSettings.insertSettingLocked(
3790 Global.DEFAULT_RESTRICT_BACKGROUND_DATA,
3791 getContext().getResources().getBoolean(
3792 R.bool.def_restrict_background_data) ? "1" : "0",
3793 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3794 }
3795 }
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003796 currentVersion = 148;
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003797 }
3798
Tim Zhengcc1e76a2017-08-30 17:46:19 -07003799 if (currentVersion == 148) {
3800 // Version 149: Set the default value for BACKUP_MANAGER_CONSTANTS.
3801 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3802 final String oldValue = systemSecureSettings.getSettingLocked(
3803 Settings.Secure.BACKUP_MANAGER_CONSTANTS).getValue();
3804 if (TextUtils.equals(null, oldValue)) {
3805 final String defaultValue = getContext().getResources().getString(
3806 R.string.def_backup_manager_constants);
3807 if (!TextUtils.isEmpty(defaultValue)) {
3808 systemSecureSettings.insertSettingLocked(
3809 Settings.Secure.BACKUP_MANAGER_CONSTANTS, defaultValue, null,
3810 true, SettingsState.SYSTEM_PACKAGE_NAME);
3811 }
3812 }
Tim Zhengcc1e76a2017-08-30 17:46:19 -07003813 currentVersion = 149;
3814 }
Jacky Cheung7076a312017-10-02 10:40:48 -07003815
3816 if (currentVersion == 149) {
3817 // Version 150: Set a default value for mobile data always on
3818 final SettingsState globalSettings = getGlobalSettingsLocked();
3819 final Setting currentSetting = globalSettings.getSettingLocked(
3820 Settings.Global.MOBILE_DATA_ALWAYS_ON);
3821 if (currentSetting.isNull()) {
3822 globalSettings.insertSettingLocked(
3823 Settings.Global.MOBILE_DATA_ALWAYS_ON,
3824 getContext().getResources().getBoolean(
3825 R.bool.def_mobile_data_always_on) ? "1" : "0",
3826 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3827 }
3828
3829 currentVersion = 150;
3830 }
3831
Mike Digman4af4a6f2018-01-16 14:49:38 -08003832 if (currentVersion == 150) {
Mike Digman32e03312018-05-16 16:43:23 -07003833 // Version 151: Removed.
Mike Digman4af4a6f2018-01-16 14:49:38 -08003834 currentVersion = 151;
3835 }
3836
Joe LaPenna4f50ed42018-01-22 14:54:45 -08003837 if (currentVersion == 151) {
Joe LaPenna250d7842018-01-25 10:19:42 -08003838 // Version 152: Removed. (This version made the setting for wifi_wakeup enabled
3839 // by default but it is now no longer configurable).
3840 // The setting updated here is deleted in 153.
Joe LaPenna4f50ed42018-01-22 14:54:45 -08003841 currentVersion = 152;
3842 }
3843
Joe LaPenna250d7842018-01-25 10:19:42 -08003844 if (currentVersion == 152) {
3845 getGlobalSettingsLocked().deleteSettingLocked("wifi_wakeup_available");
3846 currentVersion = 153;
3847 }
3848
Ben Linb4df8bc2018-01-29 11:48:20 -08003849 if (currentVersion == 153) {
3850 // Version 154: Read notification badge configuration from config.
3851 // If user has already set the value, don't do anything.
3852 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3853 final Setting showNotificationBadges = systemSecureSettings.getSettingLocked(
3854 Settings.Secure.NOTIFICATION_BADGING);
3855 if (showNotificationBadges.isNull()) {
3856 final boolean defaultValue = getContext().getResources().getBoolean(
3857 com.android.internal.R.bool.config_notificationBadging);
3858 systemSecureSettings.insertSettingLocked(
3859 Secure.NOTIFICATION_BADGING,
3860 defaultValue ? "1" : "0",
3861 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3862 }
3863 currentVersion = 154;
3864 }
3865
Bernardo Rufinoeaa78b92018-01-26 11:25:37 +00003866 if (currentVersion == 154) {
3867 // Version 155: Set the default value for BACKUP_LOCAL_TRANSPORT_PARAMETERS.
3868 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3869 final String oldValue = systemSecureSettings.getSettingLocked(
3870 Settings.Secure.BACKUP_LOCAL_TRANSPORT_PARAMETERS).getValue();
3871 if (TextUtils.equals(null, oldValue)) {
3872 final String defaultValue = getContext().getResources().getString(
3873 R.string.def_backup_local_transport_parameters);
3874 if (!TextUtils.isEmpty(defaultValue)) {
3875 systemSecureSettings.insertSettingLocked(
3876 Settings.Secure.BACKUP_LOCAL_TRANSPORT_PARAMETERS, defaultValue,
3877 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3878 }
3879
3880 }
3881 currentVersion = 155;
3882 }
3883
Beverlyda904812018-03-02 09:55:30 -05003884 if (currentVersion == 155) {
Annie Mengd069a882018-03-13 15:31:40 +00003885 // Version 156: Set the default value for CHARGING_STARTED_SOUND.
Beverlyda904812018-03-02 09:55:30 -05003886 final SettingsState globalSettings = getGlobalSettingsLocked();
3887 final String oldValue = globalSettings.getSettingLocked(
3888 Global.CHARGING_STARTED_SOUND).getValue();
3889 final String oldDefault = getContext().getResources().getString(
3890 R.string.def_wireless_charging_started_sound);
3891 if (TextUtils.equals(null, oldValue)
3892 || TextUtils.equals(oldValue, oldDefault)) {
3893 final String defaultValue = getContext().getResources().getString(
3894 R.string.def_charging_started_sound);
3895 if (!TextUtils.isEmpty(defaultValue)) {
3896 globalSettings.insertSettingLocked(
3897 Settings.Global.CHARGING_STARTED_SOUND, defaultValue,
3898 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3899 }
3900
3901 }
3902 currentVersion = 156;
3903 }
3904
Beverly09da25f2018-02-26 09:17:07 -05003905 if (currentVersion == 156) {
Beverly91d0a632018-07-02 16:45:00 -04003906 // Version 157: Set a default value for zen duration,
3907 // in version 169, zen duration is moved to secure settings
Beverly09da25f2018-02-26 09:17:07 -05003908 final SettingsState globalSettings = getGlobalSettingsLocked();
3909 final Setting currentSetting = globalSettings.getSettingLocked(
3910 Global.ZEN_DURATION);
3911 if (currentSetting.isNull()) {
3912 String defaultZenDuration = Integer.toString(getContext()
3913 .getResources().getInteger(R.integer.def_zen_duration));
3914 globalSettings.insertSettingLocked(
3915 Global.ZEN_DURATION, defaultZenDuration,
3916 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3917 }
Beverly09da25f2018-02-26 09:17:07 -05003918 currentVersion = 157;
3919 }
Annie Mengd069a882018-03-13 15:31:40 +00003920
3921 if (currentVersion == 157) {
3922 // Version 158: Set default value for BACKUP_AGENT_TIMEOUT_PARAMETERS.
3923 final SettingsState globalSettings = getGlobalSettingsLocked();
3924 final String oldValue = globalSettings.getSettingLocked(
3925 Settings.Global.BACKUP_AGENT_TIMEOUT_PARAMETERS).getValue();
3926 if (TextUtils.equals(null, oldValue)) {
3927 final String defaultValue = getContext().getResources().getString(
3928 R.string.def_backup_agent_timeout_parameters);
3929 if (!TextUtils.isEmpty(defaultValue)) {
3930 globalSettings.insertSettingLocked(
3931 Settings.Global.BACKUP_AGENT_TIMEOUT_PARAMETERS, defaultValue,
3932 null, true,
3933 SettingsState.SYSTEM_PACKAGE_NAME);
3934 }
3935 }
3936 currentVersion = 158;
3937 }
Roshan Pius9c396672018-03-02 14:54:13 -08003938
3939 if (currentVersion == 158) {
3940 // Remove setting that specifies wifi bgscan throttling params
3941 getGlobalSettingsLocked().deleteSettingLocked(
3942 "wifi_scan_background_throttle_interval_ms");
3943 getGlobalSettingsLocked().deleteSettingLocked(
3944 "wifi_scan_background_throttle_package_whitelist");
3945 currentVersion = 159;
3946 }
3947
Pavel Grafovc5c97302018-03-19 13:37:15 +00003948 if (currentVersion == 159) {
3949 // Version 160: Hiding notifications from the lockscreen is only available as
3950 // primary user option, profiles can only make them redacted. If a profile was
3951 // configured to not show lockscreen notifications, ensure that at the very
3952 // least these will be come hidden.
3953 if (mUserManager.isManagedProfile(userId)) {
3954 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3955 Setting showNotifications = secureSettings.getSettingLocked(
3956 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
3957 // The default value is "1", check if user has turned it off.
3958 if ("0".equals(showNotifications.getValue())) {
3959 secureSettings.insertSettingLocked(
3960 Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, "0",
3961 null /* tag */, false /* makeDefault */,
3962 SettingsState.SYSTEM_PACKAGE_NAME);
3963 }
3964 // The setting is no longer valid for managed profiles, it should be
3965 // treated as if it was set to "1".
3966 secureSettings.deleteSettingLocked(Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
3967 }
3968 currentVersion = 160;
3969 }
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08003970
3971 if (currentVersion == 160) {
3972 // Version 161: Set the default value for
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08003973 // MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY and
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08003974 // SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT
3975 final SettingsState globalSettings = getGlobalSettingsLocked();
3976
3977 String oldValue = globalSettings.getSettingLocked(
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08003978 Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY).getValue();
3979 if (TextUtils.equals(null, oldValue)) {
3980 globalSettings.insertSettingLocked(
3981 Settings.Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY,
3982 Integer.toString(getContext().getResources().getInteger(
3983 R.integer.def_max_sound_trigger_detection_service_ops_per_day)),
3984 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3985 }
3986
3987 oldValue = globalSettings.getSettingLocked(
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08003988 Global.SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT).getValue();
3989 if (TextUtils.equals(null, oldValue)) {
3990 globalSettings.insertSettingLocked(
3991 Settings.Global.SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT,
3992 Integer.toString(getContext().getResources().getInteger(
3993 R.integer.def_sound_trigger_detection_service_op_timeout)),
3994 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3995 }
3996 currentVersion = 161;
3997 }
3998
Mike Digman55272862018-02-20 14:35:17 -08003999 if (currentVersion == 161) {
4000 // Version 161: Add a gesture for silencing phones
4001 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4002 final Setting currentSetting = secureSettings.getSettingLocked(
4003 Secure.VOLUME_HUSH_GESTURE);
4004 if (currentSetting.isNull()) {
4005 secureSettings.insertSettingLocked(
4006 Secure.VOLUME_HUSH_GESTURE,
4007 Integer.toString(Secure.VOLUME_HUSH_VIBRATE),
4008 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4009 }
4010
4011 currentVersion = 162;
4012 }
4013
Julia Reynoldsc4e5ecf2018-04-11 11:33:32 -04004014 if (currentVersion == 162) {
Julia Reynolds76bfa602018-04-23 09:38:47 -04004015 // Version 162: REMOVED: Add a gesture for silencing phones
Julia Reynoldsc4e5ecf2018-04-11 11:33:32 -04004016 currentVersion = 163;
4017 }
4018
Philip P. Moltmanncb58a832018-04-16 09:19:42 -07004019 if (currentVersion == 163) {
4020 // Version 163: Update default value of
4021 // MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY from old to new default
4022 final SettingsState settings = getGlobalSettingsLocked();
4023 final Setting currentSetting = settings.getSettingLocked(
4024 Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY);
4025 if (currentSetting.isDefaultFromSystem()) {
4026 settings.insertSettingLocked(
4027 Settings.Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY,
4028 Integer.toString(getContext().getResources().getInteger(
4029 R.integer
4030 .def_max_sound_trigger_detection_service_ops_per_day)),
4031 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4032 }
4033
4034 currentVersion = 164;
4035 }
4036
Julia Reynolds76bfa602018-04-23 09:38:47 -04004037 if (currentVersion == 164) {
Julia Reynolds24836c52018-06-06 14:36:03 -04004038 // Version 164: REMOVED: show zen upgrade notification
Julia Reynolds76bfa602018-04-23 09:38:47 -04004039 currentVersion = 165;
4040 }
4041
Beverly301e92a2018-04-27 09:43:05 -04004042 if (currentVersion == 165) {
Beverly91d0a632018-07-02 16:45:00 -04004043 // Version 165: MOVED: Show zen settings suggestion and zen updated settings
4044 // moved to secure settings and are set in version 169
Beverly301e92a2018-04-27 09:43:05 -04004045 currentVersion = 166;
4046 }
4047
Beverly38fcfd02018-05-18 17:33:40 -04004048 if (currentVersion == 166) {
4049 // Version 166: add default values for hush gesture used and manual ringer
4050 // toggle
4051 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4052 Setting currentHushUsedSetting = secureSettings.getSettingLocked(
4053 Secure.HUSH_GESTURE_USED);
4054 if (currentHushUsedSetting.isNull()) {
4055 secureSettings.insertSettingLocked(
4056 Settings.Secure.HUSH_GESTURE_USED, "0", null, true,
4057 SettingsState.SYSTEM_PACKAGE_NAME);
4058 }
4059
4060 Setting currentRingerToggleCountSetting = secureSettings.getSettingLocked(
4061 Secure.MANUAL_RINGER_TOGGLE_COUNT);
4062 if (currentRingerToggleCountSetting.isNull()) {
4063 secureSettings.insertSettingLocked(
4064 Settings.Secure.MANUAL_RINGER_TOGGLE_COUNT, "0", null, true,
4065 SettingsState.SYSTEM_PACKAGE_NAME);
4066 }
4067 currentVersion = 167;
4068 }
4069
Beverly155c9d22018-05-23 18:03:23 -04004070 if (currentVersion == 167) {
Beverly91d0a632018-07-02 16:45:00 -04004071 // Version 167: MOVED - Settings.Global.CHARGING_VIBRATION_ENABLED moved to
4072 // Settings.Secure.CHARGING_VIBRATION_ENABLED, set in version 170
Beverly155c9d22018-05-23 18:03:23 -04004073 currentVersion = 168;
4074 }
4075
Michael Wright0e9eeee2018-05-26 00:31:20 +01004076 if (currentVersion == 168) {
4077 // Version 168: by default, vibrate for phone calls
4078 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4079 final Setting currentSetting = systemSettings.getSettingLocked(
4080 Settings.System.VIBRATE_WHEN_RINGING);
4081 if (currentSetting.isNull()) {
4082 systemSettings.insertSettingLocked(
4083 Settings.System.VIBRATE_WHEN_RINGING,
4084 getContext().getResources().getBoolean(
4085 R.bool.def_vibrate_when_ringing) ? "1" : "0",
4086 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4087 }
4088 currentVersion = 169;
4089 }
4090
Nadav Barf9f115d2018-06-24 10:06:50 +03004091 if (currentVersion == 169) {
Beverly91d0a632018-07-02 16:45:00 -04004092 // Version 169: Set the default value for Secure Settings ZEN_DURATION,
4093 // SHOW_ZEN_SETTINGS_SUGGESTION, ZEN_SETTINGS_UPDATE and
4094 // ZEN_SETTINGS_SUGGESTION_VIEWED
Nadav Barf9f115d2018-06-24 10:06:50 +03004095
Beverly91d0a632018-07-02 16:45:00 -04004096 final SettingsState globalSettings = getGlobalSettingsLocked();
4097 final Setting globalZenDuration = globalSettings.getSettingLocked(
4098 Global.ZEN_DURATION);
4099
4100 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4101 final Setting secureZenDuration = secureSettings.getSettingLocked(
4102 Secure.ZEN_DURATION);
4103
4104 // ZEN_DURATION
4105 if (!globalZenDuration.isNull()) {
4106 secureSettings.insertSettingLocked(
4107 Secure.ZEN_DURATION, globalZenDuration.getValue(), null, false,
4108 SettingsState.SYSTEM_PACKAGE_NAME);
4109
4110 // set global zen duration setting to null since it's deprecated
4111 globalSettings.insertSettingLocked(
4112 Global.ZEN_DURATION, null, null, true,
4113 SettingsState.SYSTEM_PACKAGE_NAME);
4114 } else if (secureZenDuration.isNull()) {
4115 String defaultZenDuration = Integer.toString(getContext()
4116 .getResources().getInteger(R.integer.def_zen_duration));
4117 secureSettings.insertSettingLocked(
4118 Secure.ZEN_DURATION, defaultZenDuration, null, true,
4119 SettingsState.SYSTEM_PACKAGE_NAME);
Nadav Barf9f115d2018-06-24 10:06:50 +03004120 }
Beverly91d0a632018-07-02 16:45:00 -04004121
4122 // SHOW_ZEN_SETTINGS_SUGGESTION
4123 final Setting currentShowZenSettingSuggestion = secureSettings.getSettingLocked(
4124 Secure.SHOW_ZEN_SETTINGS_SUGGESTION);
4125 if (currentShowZenSettingSuggestion.isNull()) {
4126 secureSettings.insertSettingLocked(
4127 Secure.SHOW_ZEN_SETTINGS_SUGGESTION, "1",
4128 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4129 }
4130
4131 // ZEN_SETTINGS_UPDATED
4132 final Setting currentUpdatedSetting = secureSettings.getSettingLocked(
4133 Secure.ZEN_SETTINGS_UPDATED);
4134 if (currentUpdatedSetting.isNull()) {
4135 secureSettings.insertSettingLocked(
4136 Secure.ZEN_SETTINGS_UPDATED, "0",
4137 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4138 }
4139
4140 // ZEN_SETTINGS_SUGGESTION_VIEWED
4141 final Setting currentSettingSuggestionViewed = secureSettings.getSettingLocked(
4142 Secure.ZEN_SETTINGS_SUGGESTION_VIEWED);
4143 if (currentSettingSuggestionViewed.isNull()) {
4144 secureSettings.insertSettingLocked(
4145 Secure.ZEN_SETTINGS_SUGGESTION_VIEWED, "0",
4146 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4147 }
4148
Nadav Barf9f115d2018-06-24 10:06:50 +03004149 currentVersion = 170;
4150 }
4151
Beverly91d0a632018-07-02 16:45:00 -04004152 if (currentVersion == 170) {
4153 // Version 170: Set the default value for Secure Settings:
4154 // CHARGING_SOUNDS_ENABLED and CHARGING_VIBRATION_ENABLED
4155
4156 final SettingsState globalSettings = getGlobalSettingsLocked();
4157 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4158
4159 // CHARGING_SOUNDS_ENABLED
4160 final Setting globalChargingSoundEnabled = globalSettings.getSettingLocked(
4161 Global.CHARGING_SOUNDS_ENABLED);
4162 final Setting secureChargingSoundsEnabled = secureSettings.getSettingLocked(
4163 Secure.CHARGING_SOUNDS_ENABLED);
4164
4165 if (!globalChargingSoundEnabled.isNull()) {
Beverlyff9c5872019-01-02 15:44:00 -05004166 if (secureChargingSoundsEnabled.isNull()) {
4167 secureSettings.insertSettingLocked(
4168 Secure.CHARGING_SOUNDS_ENABLED,
4169 globalChargingSoundEnabled.getValue(), null, false,
4170 SettingsState.SYSTEM_PACKAGE_NAME);
4171 }
Beverly91d0a632018-07-02 16:45:00 -04004172
4173 // set global charging_sounds_enabled setting to null since it's deprecated
4174 globalSettings.insertSettingLocked(
4175 Global.CHARGING_SOUNDS_ENABLED, null, null, true,
4176 SettingsState.SYSTEM_PACKAGE_NAME);
4177 } else if (secureChargingSoundsEnabled.isNull()) {
4178 String defChargingSoundsEnabled = getContext().getResources()
4179 .getBoolean(R.bool.def_charging_sounds_enabled) ? "1" : "0";
4180 secureSettings.insertSettingLocked(
4181 Secure.CHARGING_SOUNDS_ENABLED, defChargingSoundsEnabled, null,
4182 true, SettingsState.SYSTEM_PACKAGE_NAME);
4183 }
4184
4185 // CHARGING_VIBRATION_ENABLED
4186 final Setting secureChargingVibrationEnabled = secureSettings.getSettingLocked(
4187 Secure.CHARGING_VIBRATION_ENABLED);
4188
4189 if (secureChargingVibrationEnabled.isNull()) {
4190 String defChargingVibrationEnabled = getContext().getResources()
4191 .getBoolean(R.bool.def_charging_vibration_enabled) ? "1" : "0";
4192 secureSettings.insertSettingLocked(
4193 Secure.CHARGING_VIBRATION_ENABLED, defChargingVibrationEnabled,
4194 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4195 }
4196
4197 currentVersion = 171;
4198 }
4199
Nadav Bar8bc8c9e2018-09-12 15:41:51 +03004200 if (currentVersion == 171) {
4201 // Version 171: by default, add STREAM_VOICE_CALL to list of streams that can
4202 // be muted.
4203 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4204 final Setting currentSetting = systemSettings.getSettingLocked(
4205 Settings.System.MUTE_STREAMS_AFFECTED);
4206 if (!currentSetting.isNull()) {
4207 try {
4208 int currentSettingIntegerValue = Integer.parseInt(
4209 currentSetting.getValue());
4210 if ((currentSettingIntegerValue
4211 & (1 << AudioManager.STREAM_VOICE_CALL)) == 0) {
4212 systemSettings.insertSettingLocked(
4213 Settings.System.MUTE_STREAMS_AFFECTED,
4214 Integer.toString(
4215 currentSettingIntegerValue
4216 | (1 << AudioManager.STREAM_VOICE_CALL)),
4217 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4218 }
4219 } catch (NumberFormatException e) {
4220 // remove the setting in case it is not a valid integer
4221 Slog.w("Failed to parse integer value of MUTE_STREAMS_AFFECTED"
4222 + "setting, removing setting", e);
4223 systemSettings.deleteSettingLocked(
4224 Settings.System.MUTE_STREAMS_AFFECTED);
4225 }
4226
4227 }
4228 currentVersion = 172;
4229 }
4230
Soonil Nagarkar42da1b12019-01-22 11:29:27 -08004231 if (currentVersion == 172) {
4232 // Version 172: Set the default value for Secure Settings: LOCATION_MODE
4233
4234 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4235
4236 final Setting locationMode = secureSettings.getSettingLocked(
4237 Secure.LOCATION_MODE);
4238
4239 if (locationMode.isNull()) {
4240 final Setting locationProvidersAllowed = secureSettings.getSettingLocked(
4241 Secure.LOCATION_PROVIDERS_ALLOWED);
4242
Soonil Nagarkar4ee3ac22019-02-08 19:19:24 -08004243 final int defLocationMode;
4244 if (locationProvidersAllowed.isNull()) {
4245 defLocationMode = getContext().getResources().getInteger(
4246 R.integer.def_location_mode);
4247 } else {
4248 defLocationMode =
4249 !TextUtils.isEmpty(locationProvidersAllowed.getValue())
4250 ? Secure.LOCATION_MODE_ON
4251 : Secure.LOCATION_MODE_OFF;
4252 }
Soonil Nagarkar42da1b12019-01-22 11:29:27 -08004253 secureSettings.insertSettingLocked(
Soonil Nagarkar4ee3ac22019-02-08 19:19:24 -08004254 Secure.LOCATION_MODE, Integer.toString(defLocationMode),
Soonil Nagarkar42da1b12019-01-22 11:29:27 -08004255 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4256 }
4257
4258 currentVersion = 173;
4259 }
4260
Beverly4179f992019-02-08 11:34:16 -05004261 if (currentVersion == 173) {
4262 // Version 173: Set the default value for Secure Settings: NOTIFICATION_BUBBLES
4263
4264 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4265
4266 final Setting bubblesSetting = secureSettings.getSettingLocked(
4267 Secure.NOTIFICATION_BUBBLES);
4268
4269 if (bubblesSetting.isNull()) {
4270 secureSettings.insertSettingLocked(Secure.NOTIFICATION_BUBBLES,
4271 getContext().getResources().getBoolean(
4272 R.bool.def_notification_bubbles) ? "1" : "0", null,
4273 true, SettingsState.SYSTEM_PACKAGE_NAME);
4274 }
4275
4276 currentVersion = 174;
4277 }
4278
Yiwen Chen0305b572019-03-05 11:26:59 -08004279 if (currentVersion == 174) {
4280 // Version 174: Set the default value for Global Settings: APPLY_RAMPING_RINGER
4281
4282 final SettingsState globalSettings = getGlobalSettingsLocked();
4283
4284 Setting currentRampingRingerSetting = globalSettings.getSettingLocked(
4285 Settings.Global.APPLY_RAMPING_RINGER);
4286 if (currentRampingRingerSetting.isNull()) {
4287 globalSettings.insertSettingLocked(
4288 Settings.Global.APPLY_RAMPING_RINGER,
4289 getContext().getResources().getBoolean(
4290 R.bool.def_apply_ramping_ringer) ? "1" : "0", null,
4291 true, SettingsState.SYSTEM_PACKAGE_NAME);
4292 }
4293
4294 currentVersion = 175;
4295 }
4296
wilsonshih5d999e22019-03-20 11:50:42 +08004297 if (currentVersion == 175) {
4298 // Version 175: Set the default value for System Settings:
4299 // RING_VIBRATION_INTENSITY. If the notification vibration intensity has been
4300 // set and ring vibration intensity hasn't, the ring vibration intensity should
4301 // followed notification vibration intensity.
4302
4303 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4304
4305 Setting notificationVibrationIntensity = systemSettings.getSettingLocked(
4306 Settings.System.NOTIFICATION_VIBRATION_INTENSITY);
4307
4308 Setting ringVibrationIntensity = systemSettings.getSettingLocked(
4309 Settings.System.RING_VIBRATION_INTENSITY);
4310
4311 if (!notificationVibrationIntensity.isNull()
4312 && ringVibrationIntensity.isNull()) {
4313 systemSettings.insertSettingLocked(
4314 Settings.System.RING_VIBRATION_INTENSITY,
4315 notificationVibrationIntensity.getValue(),
4316 null , true, SettingsState.SYSTEM_PACKAGE_NAME);
4317 }
4318
4319 currentVersion = 176;
4320 }
4321
Winson Chungd9f2fb32019-03-05 11:10:12 -08004322 if (currentVersion == 176) {
4323 // Version 176: Migrate the existing swipe up setting into the resource overlay
4324 // for the navigation bar interaction mode.
4325
4326 final IOverlayManager overlayManager = IOverlayManager.Stub.asInterface(
4327 ServiceManager.getService(Context.OVERLAY_SERVICE));
4328 int navBarMode = -1;
4329
4330 // Migrate the swipe up setting only if it is set
4331 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4332 final Setting swipeUpSetting = secureSettings.getSettingLocked(
Winson Chung85e74592019-03-06 11:49:22 -08004333 "swipe_up_to_switch_apps_enabled");
Winson Chungd9f2fb32019-03-05 11:10:12 -08004334 if (swipeUpSetting != null && !swipeUpSetting.isNull()) {
4335 navBarMode = swipeUpSetting.getValue().equals("1")
4336 ? NAV_BAR_MODE_2BUTTON
4337 : NAV_BAR_MODE_3BUTTON;
4338 }
4339
4340 // Temporary: Only for migration for dogfooders, to be removed
4341 try {
4342 final OverlayInfo info = overlayManager.getOverlayInfo(
4343 "com.android.internal.experiment.navbar.type.inset",
4344 UserHandle.USER_CURRENT);
4345 if (info != null && info.isEnabled()) {
4346 navBarMode = NAV_BAR_MODE_GESTURAL;
4347 }
4348 } catch (RemoteException e) {
4349 // Ingore, fall through
4350 }
4351
4352 if (navBarMode != -1) {
4353 try {
4354 overlayManager.setEnabled(NAV_BAR_MODE_3BUTTON_OVERLAY,
4355 navBarMode == NAV_BAR_MODE_3BUTTON,
4356 UserHandle.USER_CURRENT);
4357 overlayManager.setEnabled(NAV_BAR_MODE_2BUTTON_OVERLAY,
4358 navBarMode == NAV_BAR_MODE_2BUTTON,
4359 UserHandle.USER_CURRENT);
4360 overlayManager.setEnabled(NAV_BAR_MODE_GESTURAL_OVERLAY,
4361 navBarMode == NAV_BAR_MODE_GESTURAL,
4362 UserHandle.USER_CURRENT);
4363 } catch (RemoteException e) {
4364 throw new IllegalStateException(
4365 "Failed to set nav bar interaction mode overlay");
4366 }
4367 }
4368
4369 currentVersion = 177;
4370 }
4371
4372
Felipe Lemeff355092017-04-03 12:55:02 -07004373 // vXXX: Add new settings above this point.
4374
Dan Sandler71f85e92016-07-20 13:46:05 -04004375 if (currentVersion != newVersion) {
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07004376 Slog.wtf("SettingsProvider", "warning: upgrading settings database to version "
Dan Sandler71f85e92016-07-20 13:46:05 -04004377 + newVersion + " left it at "
Stephen Chen5d0922f2017-03-27 10:28:04 -07004378 + currentVersion +
4379 " instead; this is probably a bug. Did you update SETTINGS_VERSION?",
4380 new Throwable());
Dan Sandler71f85e92016-07-20 13:46:05 -04004381 if (DEBUG) {
4382 throw new RuntimeException("db upgrade error");
4383 }
4384 }
4385
Jeff Brown503cffc2015-03-26 18:08:51 -07004386 // Return the current version.
4387 return currentVersion;
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -08004388 }
4389 }
Svet Ganov13701552017-02-23 12:45:17 -08004390
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004391 private void ensureLegacyDefaultValueAndSystemSetUpdatedLocked(SettingsState settings,
4392 int userId) {
Svet Ganov13701552017-02-23 12:45:17 -08004393 List<String> names = settings.getSettingNamesLocked();
4394 final int nameCount = names.size();
4395 for (int i = 0; i < nameCount; i++) {
4396 String name = names.get(i);
4397 Setting setting = settings.getSettingLocked(name);
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004398
4399 // In the upgrade case we pretend the call is made from the app
4400 // that made the last change to the setting to properly determine
4401 // whether the call has been made by a system component.
4402 int callingUid = -1;
4403 try {
4404 callingUid = mPackageManager.getPackageUid(setting.getPackageName(), 0, userId);
4405 } catch (RemoteException e) {
4406 /* ignore - handled below */
4407 }
4408 if (callingUid < 0) {
4409 Slog.e(LOG_TAG, "Unknown package: " + setting.getPackageName());
4410 continue;
4411 }
4412 try {
4413 final boolean systemSet = SettingsState.isSystemPackage(getContext(),
Matt Pape25c940d2019-03-26 12:14:33 -07004414 setting.getPackageName(), callingUid, userId);
Svet Ganov13701552017-02-23 12:45:17 -08004415 if (systemSet) {
4416 settings.insertSettingLocked(name, setting.getValue(),
4417 setting.getTag(), true, setting.getPackageName());
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004418 } else if (setting.getDefaultValue() != null && setting.isDefaultFromSystem()) {
4419 // We had a bug where changes by non-system packages were marked
4420 // as system made and as a result set as the default. Therefore, if
4421 // the package changed the setting last is not a system one but the
4422 // setting is marked as its default coming from the system we clear
4423 // the default and clear the system set flag.
4424 settings.resetSettingDefaultValueLocked(name);
Svet Ganov13701552017-02-23 12:45:17 -08004425 }
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004426 } catch (IllegalStateException e) {
4427 // If the package goes over its quota during the upgrade, don't
4428 // crash but just log the error as the system does the upgrade.
4429 Slog.e(LOG_TAG, "Error upgrading setting: " + setting.getName(), e);
4430
Svet Ganov13701552017-02-23 12:45:17 -08004431 }
4432 }
4433 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08004434 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004435}