blob: e8d726ed715a6cb92b9a7bf595468a9a0b6f53fb [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
yuemingw1d13eae2018-01-30 17:27:54 +000019import static android.os.Process.ROOT_UID;
20import static android.os.Process.SHELL_UID;
21import static android.os.Process.SYSTEM_UID;
Winson Chungd9f2fb32019-03-05 11:10:12 -080022import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_2BUTTON;
23import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_2BUTTON_OVERLAY;
24import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON;
25import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON_OVERLAY;
26import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL;
27import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL_OVERLAY;
yuemingw1d13eae2018-01-30 17:27:54 +000028
Svetoslav683914b2015-01-15 14:22:26 -080029import android.Manifest;
Eugene Suslad72c3972016-12-27 15:49:30 -080030import android.annotation.NonNull;
Kweku Adamsb0886f32017-10-31 15:32:09 -070031import android.annotation.Nullable;
Christopher Tated5fe1472012-09-10 15:48:38 -070032import android.app.ActivityManager;
Xiaohui Chen43765b72015-08-31 10:57:33 -070033import android.app.AppGlobals;
Christopher Tate45281862010-03-05 15:46:30 -080034import android.app.backup.BackupManager;
Christopher Tate06efb532012-08-24 15:29:27 -070035import android.content.BroadcastReceiver;
Ruben Brunk98576cf2016-03-07 18:54:28 -080036import android.content.ComponentName;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070037import android.content.ContentProvider;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070038import android.content.ContentValues;
39import android.content.Context;
Christopher Tate06efb532012-08-24 15:29:27 -070040import android.content.Intent;
41import android.content.IntentFilter;
Winson Chungd9f2fb32019-03-05 11:10:12 -080042import android.content.om.IOverlayManager;
43import android.content.om.OverlayInfo;
Svetoslav683914b2015-01-15 14:22:26 -080044import android.content.pm.ApplicationInfo;
Xiaohui Chen43765b72015-08-31 10:57:33 -070045import android.content.pm.IPackageManager;
Svetoslav683914b2015-01-15 14:22:26 -080046import android.content.pm.PackageInfo;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070047import android.content.pm.PackageManager;
Christopher Tate38e7a602013-09-03 16:57:34 -070048import android.content.pm.UserInfo;
Chad Brubaker20e0dc32017-04-28 18:24:55 -070049import android.content.res.Resources;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070050import android.database.Cursor;
Svetoslav683914b2015-01-15 14:22:26 -080051import android.database.MatrixCursor;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070052import android.database.sqlite.SQLiteDatabase;
53import android.database.sqlite.SQLiteQueryBuilder;
Svetoslav683914b2015-01-15 14:22:26 -080054import android.hardware.camera2.utils.ArrayUtils;
John Spurlocke11ae112015-05-11 16:09:03 -040055import android.media.AudioManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070056import android.net.Uri;
Christopher Tate06efb532012-08-24 15:29:27 -070057import android.os.Binder;
Svetoslav683914b2015-01-15 14:22:26 -080058import android.os.Build;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080059import android.os.Bundle;
Amith Yamasani5cdf7f52013-06-27 15:12:01 -070060import android.os.DropBoxManager;
Svetoslav683914b2015-01-15 14:22:26 -080061import android.os.Environment;
Svetoslav7e0683b2015-08-03 16:02:52 -070062import android.os.Handler;
Svet Ganova8f90262016-05-10 08:44:48 -070063import android.os.HandlerThread;
Svetoslav7e0683b2015-08-03 16:02:52 -070064import android.os.Looper;
65import android.os.Message;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070066import android.os.ParcelFileDescriptor;
Christopher Tate0da13572013-10-13 17:34:49 -070067import android.os.Process;
Xiaohui Chen43765b72015-08-31 10:57:33 -070068import android.os.RemoteException;
Jeff Sharkey413573a2016-02-22 17:52:45 -070069import android.os.SELinux;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -070070import android.os.ServiceManager;
Christopher Tate06efb532012-08-24 15:29:27 -070071import android.os.UserHandle;
72import android.os.UserManager;
Svet Ganov53a441c2016-04-19 19:38:00 -070073import android.os.UserManagerInternal;
Matt Pape1278d1c2018-12-11 13:03:49 -080074import android.provider.DeviceConfig;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070075import android.provider.Settings;
Narayan Kamath94bcdbc2017-07-17 15:32:53 +010076import android.provider.Settings.Global;
Makoto Onuki0000d322017-11-28 16:31:47 -080077import android.provider.Settings.Secure;
yuemingw1d13eae2018-01-30 17:27:54 +000078import android.provider.SettingsValidators;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070079import android.text.TextUtils;
Andre Lago3fa139c2016-08-04 13:53:44 +010080import android.util.ArrayMap;
Svetoslav683914b2015-01-15 14:22:26 -080081import android.util.ArraySet;
Mark Rathjend891f012017-01-19 04:10:37 +000082import android.util.ByteStringUtils;
Christopher Tate06efb532012-08-24 15:29:27 -070083import android.util.Slog;
84import android.util.SparseArray;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -070085import android.util.SparseBooleanArray;
Eugene Suslad72c3972016-12-27 15:49:30 -080086import android.util.proto.ProtoOutputStream;
John Spurlocke11ae112015-05-11 16:09:03 -040087
Svetoslav683914b2015-01-15 14:22:26 -080088import com.android.internal.annotations.GuardedBy;
89import com.android.internal.content.PackageMonitor;
90import com.android.internal.os.BackgroundThread;
Suprabh Shukla269c11e2015-12-02 16:51:16 -080091import com.android.providers.settings.SettingsState.Setting;
Svet Ganov53a441c2016-04-19 19:38:00 -070092import com.android.server.LocalServices;
Ruben Brunk98576cf2016-03-07 18:54:28 -080093import com.android.server.SystemConfig;
John Spurlocke11ae112015-05-11 16:09:03 -040094
Irina Dumitrescue3696872019-01-09 16:07:59 +000095import com.google.android.collect.Sets;
96
Svetoslav683914b2015-01-15 14:22:26 -080097import java.io.File;
Svetoslavb505ccc2015-02-17 12:41:04 -080098import java.io.FileDescriptor;
Svetoslav683914b2015-01-15 14:22:26 -080099import java.io.FileNotFoundException;
Svetoslavb505ccc2015-02-17 12:41:04 -0800100import java.io.PrintWriter;
Mark Rathjen7599f132017-01-23 14:15:54 -0800101import java.nio.ByteBuffer;
102import java.security.InvalidKeyException;
Mark Rathjend891f012017-01-19 04:10:37 +0000103import java.security.NoSuchAlgorithmException;
Svetoslav683914b2015-01-15 14:22:26 -0800104import java.security.SecureRandom;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700105import java.util.ArrayList;
Svetoslav683914b2015-01-15 14:22:26 -0800106import java.util.Arrays;
Robin Lee7af9a742017-02-20 14:47:30 +0000107import java.util.Collection;
Matt Pape6bfc62e2018-11-28 13:16:03 -0800108import java.util.HashMap;
Mark Rathjend891f012017-01-19 04:10:37 +0000109import java.util.HashSet;
Svetoslav683914b2015-01-15 14:22:26 -0800110import java.util.List;
Mark Rathjen7599f132017-01-23 14:15:54 -0800111import java.util.Locale;
Andre Lago3fa139c2016-08-04 13:53:44 +0100112import java.util.Map;
Svetoslav683914b2015-01-15 14:22:26 -0800113import java.util.Set;
114import java.util.regex.Pattern;
yuemingw1d13eae2018-01-30 17:27:54 +0000115
Mark Rathjen7599f132017-01-23 14:15:54 -0800116import javax.crypto.Mac;
117import javax.crypto.spec.SecretKeySpec;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700118
Svetoslav Ganove080da92016-12-21 17:10:35 -0800119
Svetoslav683914b2015-01-15 14:22:26 -0800120/**
121 * <p>
122 * This class is a content provider that publishes the system settings.
123 * It can be accessed via the content provider APIs or via custom call
124 * commands. The latter is a bit faster and is the preferred way to access
125 * the platform settings.
126 * </p>
127 * <p>
128 * There are three settings types, global (with signature level protection
129 * and shared across users), secure (with signature permission level
130 * protection and per user), and system (with dangerous permission level
131 * protection and per user). Global settings are stored under the device owner.
132 * Each of these settings is represented by a {@link
133 * com.android.providers.settings.SettingsState} object mapped to an integer
134 * key derived from the setting type in the most significant bits and user
135 * id in the least significant bits. Settings are synchronously loaded on
136 * instantiation of a SettingsState and asynchronously persisted on mutation.
137 * Settings are stored in the user specific system directory.
138 * </p>
139 * <p>
140 * Apps targeting APIs Lollipop MR1 and lower can add custom settings entries
141 * and get a warning. Targeting higher API version prohibits this as the
142 * system settings are not a place for apps to save their state. When a package
143 * is removed the settings it added are deleted. Apps cannot delete system
144 * settings added by the platform. System settings values are validated to
145 * ensure the clients do not put bad values. Global and secure settings are
146 * changed only by trusted parties, therefore no validation is performed. Also
147 * there is a limit on the amount of app specific settings that can be added
148 * to prevent unlimited growth of the system process memory footprint.
149 * </p>
150 */
151@SuppressWarnings("deprecation")
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700152public class SettingsProvider extends ContentProvider {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700153 static final boolean DEBUG = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700154
Svetoslav Ganov264c7a92016-08-24 17:31:14 -0700155 private static final boolean DROP_DATABASE_ON_MIGRATION = true;
Svetoslav683914b2015-01-15 14:22:26 -0800156
157 private static final String LOG_TAG = "SettingsProvider";
Christopher Tate0da13572013-10-13 17:34:49 -0700158
Christopher Tate06efb532012-08-24 15:29:27 -0700159 private static final String TABLE_SYSTEM = "system";
160 private static final String TABLE_SECURE = "secure";
161 private static final String TABLE_GLOBAL = "global";
Matt Pape6bfc62e2018-11-28 13:16:03 -0800162 private static final String TABLE_CONFIG = "config";
Svetoslav683914b2015-01-15 14:22:26 -0800163
164 // Old tables no longer exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 private static final String TABLE_FAVORITES = "favorites";
166 private static final String TABLE_OLD_FAVORITES = "old_favorites";
Svetoslav683914b2015-01-15 14:22:26 -0800167 private static final String TABLE_BLUETOOTH_DEVICES = "bluetooth_devices";
168 private static final String TABLE_BOOKMARKS = "bookmarks";
169 private static final String TABLE_ANDROID_METADATA = "android_metadata";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170
Svetoslav683914b2015-01-15 14:22:26 -0800171 // The set of removed legacy tables.
172 private static final Set<String> REMOVED_LEGACY_TABLES = new ArraySet<>();
Christopher Tate06efb532012-08-24 15:29:27 -0700173 static {
Svetoslav683914b2015-01-15 14:22:26 -0800174 REMOVED_LEGACY_TABLES.add(TABLE_FAVORITES);
175 REMOVED_LEGACY_TABLES.add(TABLE_OLD_FAVORITES);
176 REMOVED_LEGACY_TABLES.add(TABLE_BLUETOOTH_DEVICES);
177 REMOVED_LEGACY_TABLES.add(TABLE_BOOKMARKS);
178 REMOVED_LEGACY_TABLES.add(TABLE_ANDROID_METADATA);
179 }
Christopher Tate06efb532012-08-24 15:29:27 -0700180
Svetoslav683914b2015-01-15 14:22:26 -0800181 private static final int MUTATION_OPERATION_INSERT = 1;
182 private static final int MUTATION_OPERATION_DELETE = 2;
183 private static final int MUTATION_OPERATION_UPDATE = 3;
Svetoslav Ganove080da92016-12-21 17:10:35 -0800184 private static final int MUTATION_OPERATION_RESET = 4;
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400185
Svetoslav683914b2015-01-15 14:22:26 -0800186 private static final String[] ALL_COLUMNS = new String[] {
187 Settings.NameValueTable._ID,
188 Settings.NameValueTable.NAME,
189 Settings.NameValueTable.VALUE
190 };
191
Makoto Onuki53f0e022017-11-29 13:51:01 -0800192 public static final int SETTINGS_TYPE_GLOBAL = SettingsState.SETTINGS_TYPE_GLOBAL;
193 public static final int SETTINGS_TYPE_SYSTEM = SettingsState.SETTINGS_TYPE_SYSTEM;
194 public static final int SETTINGS_TYPE_SECURE = SettingsState.SETTINGS_TYPE_SECURE;
195 public static final int SETTINGS_TYPE_SSAID = SettingsState.SETTINGS_TYPE_SSAID;
Matt Pape1b31a332018-10-17 09:58:28 -0700196 public static final int SETTINGS_TYPE_CONFIG = SettingsState.SETTINGS_TYPE_CONFIG;
Svet Ganov53a441c2016-04-19 19:38:00 -0700197
198 private static final Bundle NULL_SETTING_BUNDLE = Bundle.forPair(
199 Settings.NameValueTable.VALUE, null);
Christopher Tate06efb532012-08-24 15:29:27 -0700200
Nicholas Sauer3d87d1e2018-11-06 08:38:39 -0800201 public static final String RESULT_ROWS_DELETED = "result_rows_deleted";
Nicholas Sauer72500532018-11-21 10:30:58 -0800202 public static final String RESULT_SETTINGS_LIST = "result_settings_list";
Nicholas Sauer3d87d1e2018-11-06 08:38:39 -0800203
Chad Brubaker20e0dc32017-04-28 18:24:55 -0700204 // Overlay specified settings whitelisted for Instant Apps
205 private static final Set<String> OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS = new ArraySet<>();
206 private static final Set<String> OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS = new ArraySet<>();
207 private static final Set<String> OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS = new ArraySet<>();
208
209 static {
210 for (String name : Resources.getSystem().getStringArray(
211 com.android.internal.R.array.config_allowedGlobalInstantAppSettings)) {
212 OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS.add(name);
213 }
214 for (String name : Resources.getSystem().getStringArray(
215 com.android.internal.R.array.config_allowedSystemInstantAppSettings)) {
216 OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS.add(name);
217 }
218 for (String name : Resources.getSystem().getStringArray(
219 com.android.internal.R.array.config_allowedSecureInstantAppSettings)) {
220 OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS.add(name);
221 }
222 }
223
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -0800224 // Changes to these global settings are synchronously persisted
225 private static final Set<String> CRITICAL_GLOBAL_SETTINGS = new ArraySet<>();
226 static {
227 CRITICAL_GLOBAL_SETTINGS.add(Settings.Global.DEVICE_PROVISIONED);
228 }
229
230 // Changes to these secure settings are synchronously persisted
231 private static final Set<String> CRITICAL_SECURE_SETTINGS = new ArraySet<>();
232 static {
233 CRITICAL_SECURE_SETTINGS.add(Settings.Secure.USER_SETUP_COMPLETE);
234 }
235
Svetoslav683914b2015-01-15 14:22:26 -0800236 // Per user secure settings that moved to the for all users global settings.
237 static final Set<String> sSecureMovedToGlobalSettings = new ArraySet<>();
238 static {
239 Settings.Secure.getMovedToGlobalSettings(sSecureMovedToGlobalSettings);
Christopher Tate06efb532012-08-24 15:29:27 -0700240 }
241
Svetoslav683914b2015-01-15 14:22:26 -0800242 // Per user system settings that moved to the for all users global settings.
243 static final Set<String> sSystemMovedToGlobalSettings = new ArraySet<>();
244 static {
245 Settings.System.getMovedToGlobalSettings(sSystemMovedToGlobalSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700246 }
247
Svetoslav683914b2015-01-15 14:22:26 -0800248 // Per user system settings that moved to the per user secure settings.
249 static final Set<String> sSystemMovedToSecureSettings = new ArraySet<>();
250 static {
251 Settings.System.getMovedToSecureSettings(sSystemMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700252 }
253
Svetoslav683914b2015-01-15 14:22:26 -0800254 // Per all users global settings that moved to the per user secure settings.
255 static final Set<String> sGlobalMovedToSecureSettings = new ArraySet<>();
256 static {
257 Settings.Global.getMovedToSecureSettings(sGlobalMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700258 }
259
Svetoslav683914b2015-01-15 14:22:26 -0800260 // Per user secure settings that are cloned for the managed profiles of the user.
261 private static final Set<String> sSecureCloneToManagedSettings = new ArraySet<>();
262 static {
263 Settings.Secure.getCloneToManagedProfileSettings(sSecureCloneToManagedSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700264 }
265
Svetoslav683914b2015-01-15 14:22:26 -0800266 // Per user system settings that are cloned for the managed profiles of the user.
267 private static final Set<String> sSystemCloneToManagedSettings = new ArraySet<>();
268 static {
269 Settings.System.getCloneToManagedProfileSettings(sSystemCloneToManagedSettings);
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400270 }
271
Andre Lago3fa139c2016-08-04 13:53:44 +0100272 // Per user system settings that are cloned from the profile's parent when a dependency
273 // in {@link Settings.Secure} is set to "1".
274 public static final Map<String, String> sSystemCloneFromParentOnDependency = new ArrayMap<>();
275 static {
276 Settings.System.getCloneFromParentOnValueSettings(sSystemCloneFromParentOnDependency);
277 }
278
Svetoslav683914b2015-01-15 14:22:26 -0800279 private final Object mLock = new Object();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700280
Svetoslav683914b2015-01-15 14:22:26 -0800281 @GuardedBy("mLock")
282 private SettingsRegistry mSettingsRegistry;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700283
Svet Ganova8f90262016-05-10 08:44:48 -0700284 @GuardedBy("mLock")
285 private HandlerThread mHandlerThread;
286
Makoto Onuki73360ab2017-03-17 11:50:13 -0700287 @GuardedBy("mLock")
288 private Handler mHandler;
289
Svetoslav7ec28e82015-05-20 17:01:10 -0700290 // We have to call in the user manager with no lock held,
291 private volatile UserManager mUserManager;
Svetoslav683914b2015-01-15 14:22:26 -0800292
yuemingw1d13eae2018-01-30 17:27:54 +0000293 private UserManagerInternal mUserManagerInternal;
294
Svetoslav7ec28e82015-05-20 17:01:10 -0700295 // We have to call in the package manager with no lock held,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700296 private volatile IPackageManager mPackageManager;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700297
Svet Ganov53a441c2016-04-19 19:38:00 -0700298 public static int makeKey(int type, int userId) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800299 return SettingsState.makeKey(type, userId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700300 }
301
302 public static int getTypeFromKey(int key) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800303 return SettingsState.getTypeFromKey(key);
Svet Ganov53a441c2016-04-19 19:38:00 -0700304 }
305
306 public static int getUserIdFromKey(int key) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800307 return SettingsState.getUserIdFromKey(key);
Svet Ganov53a441c2016-04-19 19:38:00 -0700308 }
309
310 public static String settingTypeToString(int type) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800311 return SettingsState.settingTypeToString(type);
Svet Ganov53a441c2016-04-19 19:38:00 -0700312 }
313
314 public static String keyToString(int key) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800315 return SettingsState.keyToString(key);
Svet Ganov53a441c2016-04-19 19:38:00 -0700316 }
317
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700318 @Override
319 public boolean onCreate() {
Chad Brubaker97bccee2017-01-05 15:51:41 -0800320 Settings.setInSystemServer();
Michal Karpinski2c37b082018-01-18 16:14:27 +0000321
322 // fail to boot if there're any backed up settings that don't have a non-null validator
323 ensureAllBackedUpSystemSettingsHaveValidators();
Michal Karpinski5db1e432018-01-18 20:10:24 +0000324 ensureAllBackedUpGlobalSettingsHaveValidators();
Michal Karpinski964943a2018-01-19 16:28:26 +0000325 ensureAllBackedUpSecureSettingsHaveValidators();
Michal Karpinski2c37b082018-01-18 16:14:27 +0000326
Svetoslav683914b2015-01-15 14:22:26 -0800327 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700328 mUserManager = UserManager.get(getContext());
yuemingw1d13eae2018-01-30 17:27:54 +0000329 mUserManagerInternal = LocalServices.getService(UserManagerInternal.class);
Xiaohui Chen43765b72015-08-31 10:57:33 -0700330 mPackageManager = AppGlobals.getPackageManager();
Svet Ganova8f90262016-05-10 08:44:48 -0700331 mHandlerThread = new HandlerThread(LOG_TAG,
332 Process.THREAD_PRIORITY_BACKGROUND);
333 mHandlerThread.start();
Makoto Onuki73360ab2017-03-17 11:50:13 -0700334 mHandler = new Handler(mHandlerThread.getLooper());
Svetoslav683914b2015-01-15 14:22:26 -0800335 mSettingsRegistry = new SettingsRegistry();
336 }
Makoto Onuki73360ab2017-03-17 11:50:13 -0700337 mHandler.post(() -> {
338 registerBroadcastReceivers();
339 startWatchingUserRestrictionChanges();
340 });
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700341 ServiceManager.addService("settings", new SettingsService(this));
Matt Papeabb67892018-12-07 09:13:26 -0800342 ServiceManager.addService("device_config", new DeviceConfigService(this));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700343 return true;
344 }
345
Michal Karpinski2c37b082018-01-18 16:14:27 +0000346 private void ensureAllBackedUpSystemSettingsHaveValidators() {
Michal Karpinski964943a2018-01-19 16:28:26 +0000347 String offenders = getOffenders(concat(Settings.System.SETTINGS_TO_BACKUP,
348 Settings.System.LEGACY_RESTORE_SETTINGS), Settings.System.VALIDATORS);
Michal Karpinski5db1e432018-01-18 20:10:24 +0000349
350 failToBootIfOffendersPresent(offenders, "Settings.System");
351 }
352
353 private void ensureAllBackedUpGlobalSettingsHaveValidators() {
Michal Karpinski964943a2018-01-19 16:28:26 +0000354 String offenders = getOffenders(concat(Settings.Global.SETTINGS_TO_BACKUP,
355 Settings.Global.LEGACY_RESTORE_SETTINGS), Settings.Global.VALIDATORS);
Michal Karpinski5db1e432018-01-18 20:10:24 +0000356
357 failToBootIfOffendersPresent(offenders, "Settings.Global");
358 }
359
Michal Karpinski964943a2018-01-19 16:28:26 +0000360 private void ensureAllBackedUpSecureSettingsHaveValidators() {
361 String offenders = getOffenders(concat(Settings.Secure.SETTINGS_TO_BACKUP,
362 Settings.Secure.LEGACY_RESTORE_SETTINGS), Settings.Secure.VALIDATORS);
363
364 failToBootIfOffendersPresent(offenders, "Settings.Secure");
365 }
366
Michal Karpinski5db1e432018-01-18 20:10:24 +0000367 private void failToBootIfOffendersPresent(String offenders, String settingsType) {
368 if (offenders.length() > 0) {
369 throw new RuntimeException("All " + settingsType + " settings that are backed up"
370 + " have to have a non-null validator, but those don't: " + offenders);
371 }
372 }
373
374 private String getOffenders(String[] settingsToBackup, Map<String,
375 SettingsValidators.Validator> validators) {
Michal Karpinski2c37b082018-01-18 16:14:27 +0000376 StringBuilder offenders = new StringBuilder();
Michal Karpinski5db1e432018-01-18 20:10:24 +0000377 for (String setting : settingsToBackup) {
378 if (validators.get(setting) == null) {
Michal Karpinski2c37b082018-01-18 16:14:27 +0000379 offenders.append(setting).append(" ");
380 }
381 }
Michal Karpinski5db1e432018-01-18 20:10:24 +0000382 return offenders.toString();
Michal Karpinski2c37b082018-01-18 16:14:27 +0000383 }
384
Michal Karpinski964943a2018-01-19 16:28:26 +0000385 private final String[] concat(String[] first, String[] second) {
386 if (second == null || second.length == 0) {
387 return first;
388 }
389 final int firstLen = first.length;
390 final int secondLen = second.length;
391 String[] both = new String[firstLen + secondLen];
392 System.arraycopy(first, 0, both, 0, firstLen);
393 System.arraycopy(second, 0, both, firstLen, secondLen);
394 return both;
395 }
396
Svetoslav683914b2015-01-15 14:22:26 -0800397 @Override
398 public Bundle call(String method, String name, Bundle args) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700399 final int requestingUserId = getRequestingUserId(args);
400 switch (method) {
Matt Pape1b31a332018-10-17 09:58:28 -0700401 case Settings.CALL_METHOD_GET_CONFIG: {
402 Setting setting = getConfigSetting(name);
403 return packageValueForCallResult(setting, isTrackingGeneration(args));
404 }
405
Svetoslav7ec28e82015-05-20 17:01:10 -0700406 case Settings.CALL_METHOD_GET_GLOBAL: {
407 Setting setting = getGlobalSetting(name);
Svet Ganov53a441c2016-04-19 19:38:00 -0700408 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav683914b2015-01-15 14:22:26 -0800409 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700410
411 case Settings.CALL_METHOD_GET_SECURE: {
Makoto Onuki0000d322017-11-28 16:31:47 -0800412 Setting setting = getSecureSetting(name, requestingUserId,
413 /*enableOverride=*/ true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700414 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700415 }
416
417 case Settings.CALL_METHOD_GET_SYSTEM: {
418 Setting setting = getSystemSetting(name, requestingUserId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700419 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700420 }
421
Matt Pape1b31a332018-10-17 09:58:28 -0700422 case Settings.CALL_METHOD_PUT_CONFIG: {
423 String value = getSettingValue(args);
Matt Pape1b31a332018-10-17 09:58:28 -0700424 final boolean makeDefault = getSettingMakeDefault(args);
Matt Papec1323dc2018-12-11 12:32:42 -0800425 insertConfigSetting(name, value, makeDefault);
Matt Pape1b31a332018-10-17 09:58:28 -0700426 break;
427 }
428
Svetoslav7ec28e82015-05-20 17:01:10 -0700429 case Settings.CALL_METHOD_PUT_GLOBAL: {
430 String value = getSettingValue(args);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800431 String tag = getSettingTag(args);
432 final boolean makeDefault = getSettingMakeDefault(args);
433 insertGlobalSetting(name, value, tag, makeDefault, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700434 break;
435 }
436
437 case Settings.CALL_METHOD_PUT_SECURE: {
438 String value = getSettingValue(args);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800439 String tag = getSettingTag(args);
440 final boolean makeDefault = getSettingMakeDefault(args);
441 insertSecureSetting(name, value, tag, makeDefault, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700442 break;
443 }
444
445 case Settings.CALL_METHOD_PUT_SYSTEM: {
446 String value = getSettingValue(args);
447 insertSystemSetting(name, value, requestingUserId);
448 break;
449 }
450
Matt Pape1b31a332018-10-17 09:58:28 -0700451 case Settings.CALL_METHOD_RESET_CONFIG: {
452 final int mode = getResetModeEnforcingPermission(args);
Matt Pape6bfc62e2018-11-28 13:16:03 -0800453 String prefix = getSettingPrefix(args);
Matt Papec1323dc2018-12-11 12:32:42 -0800454 resetConfigSetting(mode, prefix);
Matt Pape1b31a332018-10-17 09:58:28 -0700455 break;
456 }
457
Svetoslav Ganove080da92016-12-21 17:10:35 -0800458 case Settings.CALL_METHOD_RESET_GLOBAL: {
459 final int mode = getResetModeEnforcingPermission(args);
460 String tag = getSettingTag(args);
461 resetGlobalSetting(requestingUserId, mode, tag);
462 break;
463 }
464
465 case Settings.CALL_METHOD_RESET_SECURE: {
466 final int mode = getResetModeEnforcingPermission(args);
467 String tag = getSettingTag(args);
468 resetSecureSetting(requestingUserId, mode, tag);
469 break;
470 }
471
Matt Pape6bfc62e2018-11-28 13:16:03 -0800472 case Settings.CALL_METHOD_DELETE_CONFIG: {
Matt Papec1323dc2018-12-11 12:32:42 -0800473 int rows = deleteConfigSetting(name) ? 1 : 0;
Nicholas Sauer3d87d1e2018-11-06 08:38:39 -0800474 Bundle result = new Bundle();
475 result.putInt(RESULT_ROWS_DELETED, rows);
476 return result;
477 }
478
479 case Settings.CALL_METHOD_DELETE_GLOBAL: {
480 int rows = deleteGlobalSetting(name, requestingUserId, false) ? 1 : 0;
481 Bundle result = new Bundle();
482 result.putInt(RESULT_ROWS_DELETED, rows);
483 return result;
484 }
485
Matt Pape6bfc62e2018-11-28 13:16:03 -0800486 case Settings.CALL_METHOD_DELETE_SECURE: {
487 int rows = deleteSecureSetting(name, requestingUserId, false) ? 1 : 0;
488 Bundle result = new Bundle();
489 result.putInt(RESULT_ROWS_DELETED, rows);
490 return result;
491 }
492
493 case Settings.CALL_METHOD_DELETE_SYSTEM: {
494 int rows = deleteSystemSetting(name, requestingUserId) ? 1 : 0;
495 Bundle result = new Bundle();
496 result.putInt(RESULT_ROWS_DELETED, rows);
497 return result;
498 }
499
500 case Settings.CALL_METHOD_LIST_CONFIG: {
501 String prefix = getSettingPrefix(args);
502 Bundle result = new Bundle();
503 result.putSerializable(
504 Settings.NameValueTable.VALUE, (HashMap) getAllConfigFlags(prefix));
505 return result;
506 }
507
508 case Settings.CALL_METHOD_LIST_GLOBAL: {
Nicholas Sauer72500532018-11-21 10:30:58 -0800509 Bundle result = new Bundle();
510 result.putStringArrayList(RESULT_SETTINGS_LIST,
Matt Pape6bfc62e2018-11-28 13:16:03 -0800511 buildSettingsList(getAllGlobalSettings(null)));
Nicholas Sauer72500532018-11-21 10:30:58 -0800512 return result;
513 }
514
515 case Settings.CALL_METHOD_LIST_SECURE: {
516 Bundle result = new Bundle();
517 result.putStringArrayList(RESULT_SETTINGS_LIST,
518 buildSettingsList(getAllSecureSettings(requestingUserId, null)));
519 return result;
520 }
521
Matt Pape6bfc62e2018-11-28 13:16:03 -0800522 case Settings.CALL_METHOD_LIST_SYSTEM: {
Nicholas Sauer72500532018-11-21 10:30:58 -0800523 Bundle result = new Bundle();
524 result.putStringArrayList(RESULT_SETTINGS_LIST,
Matt Pape6bfc62e2018-11-28 13:16:03 -0800525 buildSettingsList(getAllSystemSettings(requestingUserId, null)));
Nicholas Sauer72500532018-11-21 10:30:58 -0800526 return result;
527 }
528
Svetoslav7ec28e82015-05-20 17:01:10 -0700529 default: {
530 Slog.w(LOG_TAG, "call() with invalid method: " + method);
531 } break;
Christopher Tate06efb532012-08-24 15:29:27 -0700532 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700533
Christopher Tate06efb532012-08-24 15:29:27 -0700534 return null;
535 }
536
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800537 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800538 public String getType(Uri uri) {
539 Arguments args = new Arguments(uri, null, null, true);
540 if (TextUtils.isEmpty(args.name)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700541 return "vnd.android.cursor.dir/" + args.table;
542 } else {
Svetoslav7ec28e82015-05-20 17:01:10 -0700543 return "vnd.android.cursor.item/" + args.table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700544 }
545 }
546
547 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800548 public Cursor query(Uri uri, String[] projection, String where, String[] whereArgs,
549 String order) {
550 if (DEBUG) {
551 Slog.v(LOG_TAG, "query() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700552 }
553
Svetoslav683914b2015-01-15 14:22:26 -0800554 Arguments args = new Arguments(uri, where, whereArgs, true);
555 String[] normalizedProjection = normalizeProjection(projection);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700556
Svetoslav683914b2015-01-15 14:22:26 -0800557 // If a legacy table that is gone, done.
558 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
559 return new MatrixCursor(normalizedProjection, 0);
560 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700561
Svetoslav7ec28e82015-05-20 17:01:10 -0700562 switch (args.table) {
563 case TABLE_GLOBAL: {
564 if (args.name != null) {
565 Setting setting = getGlobalSetting(args.name);
566 return packageSettingForQuery(setting, normalizedProjection);
567 } else {
568 return getAllGlobalSettings(projection);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700569 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700570 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700571
Svetoslav7ec28e82015-05-20 17:01:10 -0700572 case TABLE_SECURE: {
573 final int userId = UserHandle.getCallingUserId();
574 if (args.name != null) {
575 Setting setting = getSecureSetting(args.name, userId);
576 return packageSettingForQuery(setting, normalizedProjection);
577 } else {
578 return getAllSecureSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800579 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700580 }
Svetoslav683914b2015-01-15 14:22:26 -0800581
Svetoslav7ec28e82015-05-20 17:01:10 -0700582 case TABLE_SYSTEM: {
583 final int userId = UserHandle.getCallingUserId();
584 if (args.name != null) {
585 Setting setting = getSystemSetting(args.name, userId);
586 return packageSettingForQuery(setting, normalizedProjection);
587 } else {
588 return getAllSystemSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800589 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700590 }
Svetoslav683914b2015-01-15 14:22:26 -0800591
Svetoslav7ec28e82015-05-20 17:01:10 -0700592 default: {
593 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700594 }
595 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700596 }
597
Nicholas Sauer72500532018-11-21 10:30:58 -0800598 private ArrayList<String> buildSettingsList(Cursor cursor) {
599 final ArrayList<String> lines = new ArrayList<String>();
600 try {
601 while (cursor != null && cursor.moveToNext()) {
602 lines.add(cursor.getString(1) + "=" + cursor.getString(2));
603 }
604 } finally {
605 if (cursor != null) {
606 cursor.close();
607 }
608 }
609 return lines;
610 }
611
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700612 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800613 public Uri insert(Uri uri, ContentValues values) {
614 if (DEBUG) {
615 Slog.v(LOG_TAG, "insert() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700616 }
617
Svetoslav683914b2015-01-15 14:22:26 -0800618 String table = getValidTableOrThrow(uri);
Christopher Tate06efb532012-08-24 15:29:27 -0700619
Svetoslav683914b2015-01-15 14:22:26 -0800620 // If a legacy table that is gone, done.
621 if (REMOVED_LEGACY_TABLES.contains(table)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 return null;
623 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700624
Svetoslav683914b2015-01-15 14:22:26 -0800625 String name = values.getAsString(Settings.Secure.NAME);
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700626 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800627 return null;
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700628 }
629
Svetoslav683914b2015-01-15 14:22:26 -0800630 String value = values.getAsString(Settings.Secure.VALUE);
631
Svetoslav7ec28e82015-05-20 17:01:10 -0700632 switch (table) {
633 case TABLE_GLOBAL: {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800634 if (insertGlobalSetting(name, value, null, false,
635 UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700636 return Uri.withAppendedPath(Settings.Global.CONTENT_URI, name);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700637 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700638 } break;
639
640 case TABLE_SECURE: {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800641 if (insertSecureSetting(name, value, null, false,
642 UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700643 return Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name);
644 }
645 } break;
646
647 case TABLE_SYSTEM: {
648 if (insertSystemSetting(name, value, UserHandle.getCallingUserId())) {
649 return Uri.withAppendedPath(Settings.System.CONTENT_URI, name);
650 }
651 } break;
652
653 default: {
654 throw new IllegalArgumentException("Bad Uri path:" + uri);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700655 }
656 }
657
Svetoslav683914b2015-01-15 14:22:26 -0800658 return null;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700659 }
660
661 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800662 public int bulkInsert(Uri uri, ContentValues[] allValues) {
663 if (DEBUG) {
664 Slog.v(LOG_TAG, "bulkInsert() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700666
Svetoslav683914b2015-01-15 14:22:26 -0800667 int insertionCount = 0;
668 final int valuesCount = allValues.length;
669 for (int i = 0; i < valuesCount; i++) {
670 ContentValues values = allValues[i];
671 if (insert(uri, values) != null) {
672 insertionCount++;
673 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700674 }
Svetoslav683914b2015-01-15 14:22:26 -0800675
676 return insertionCount;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700677 }
678
679 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800680 public int delete(Uri uri, String where, String[] whereArgs) {
681 if (DEBUG) {
682 Slog.v(LOG_TAG, "delete() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700684
Svetoslav683914b2015-01-15 14:22:26 -0800685 Arguments args = new Arguments(uri, where, whereArgs, false);
686
687 // If a legacy table that is gone, done.
688 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
689 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700690 }
Svetoslav683914b2015-01-15 14:22:26 -0800691
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700692 if (!isKeyValid(args.name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800693 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700694 }
Svetoslav683914b2015-01-15 14:22:26 -0800695
Svetoslav7ec28e82015-05-20 17:01:10 -0700696 switch (args.table) {
697 case TABLE_GLOBAL: {
698 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700699 return deleteGlobalSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700700 }
Svetoslav683914b2015-01-15 14:22:26 -0800701
Svetoslav7ec28e82015-05-20 17:01:10 -0700702 case TABLE_SECURE: {
703 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700704 return deleteSecureSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700705 }
Svetoslav683914b2015-01-15 14:22:26 -0800706
Svetoslav7ec28e82015-05-20 17:01:10 -0700707 case TABLE_SYSTEM: {
708 final int userId = UserHandle.getCallingUserId();
709 return deleteSystemSetting(args.name, userId) ? 1 : 0;
710 }
711
712 default: {
713 throw new IllegalArgumentException("Bad Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800714 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700715 }
Svetoslav683914b2015-01-15 14:22:26 -0800716 }
717
718 @Override
719 public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
720 if (DEBUG) {
721 Slog.v(LOG_TAG, "update() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700722 }
Svetoslav683914b2015-01-15 14:22:26 -0800723
724 Arguments args = new Arguments(uri, where, whereArgs, false);
725
726 // If a legacy table that is gone, done.
727 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
728 return 0;
729 }
730
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700731 String name = values.getAsString(Settings.Secure.NAME);
732 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800733 return 0;
734 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700735 String value = values.getAsString(Settings.Secure.VALUE);
Svetoslav683914b2015-01-15 14:22:26 -0800736
Svetoslav7ec28e82015-05-20 17:01:10 -0700737 switch (args.table) {
738 case TABLE_GLOBAL: {
739 final int userId = UserHandle.getCallingUserId();
Svetoslav Ganove080da92016-12-21 17:10:35 -0800740 return updateGlobalSetting(args.name, value, null, false,
741 userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700742 }
Svetoslav683914b2015-01-15 14:22:26 -0800743
Svetoslav7ec28e82015-05-20 17:01:10 -0700744 case TABLE_SECURE: {
745 final int userId = UserHandle.getCallingUserId();
Svetoslav Ganove080da92016-12-21 17:10:35 -0800746 return updateSecureSetting(args.name, value, null, false,
747 userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700748 }
Svetoslav683914b2015-01-15 14:22:26 -0800749
Svetoslav7ec28e82015-05-20 17:01:10 -0700750 case TABLE_SYSTEM: {
751 final int userId = UserHandle.getCallingUserId();
752 return updateSystemSetting(args.name, value, userId) ? 1 : 0;
753 }
Svetoslav683914b2015-01-15 14:22:26 -0800754
Svetoslav7ec28e82015-05-20 17:01:10 -0700755 default: {
756 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800757 }
758 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700759 }
760
761 @Override
762 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
Robin Lee2ab02e22016-07-28 18:41:23 +0100763 final int userId = getUserIdFromUri(uri, UserHandle.getCallingUserId());
764 if (userId != UserHandle.getCallingUserId()) {
765 getContext().enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS,
766 "Access files from the settings of another user");
767 }
768 uri = ContentProvider.getUriWithoutUserId(uri);
769
Andre Lago3fa139c2016-08-04 13:53:44 +0100770 final String cacheRingtoneSetting;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700771 final String cacheName;
772 if (Settings.System.RINGTONE_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100773 cacheRingtoneSetting = Settings.System.RINGTONE;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700774 cacheName = Settings.System.RINGTONE_CACHE;
775 } else if (Settings.System.NOTIFICATION_SOUND_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100776 cacheRingtoneSetting = Settings.System.NOTIFICATION_SOUND;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700777 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
778 } else if (Settings.System.ALARM_ALERT_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100779 cacheRingtoneSetting = Settings.System.ALARM_ALERT;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700780 cacheName = Settings.System.ALARM_ALERT_CACHE;
781 } else {
782 throw new FileNotFoundException("Direct file access no longer supported; "
783 + "ringtone playback is available through android.media.Ringtone");
784 }
785
Andre Lago3fa139c2016-08-04 13:53:44 +0100786 int actualCacheOwner;
787 // Redirect cache to parent if ringtone setting is owned by profile parent
788 synchronized (mLock) {
789 actualCacheOwner = resolveOwningUserIdForSystemSettingLocked(userId,
790 cacheRingtoneSetting);
791 }
792 final File cacheFile = new File(getRingtoneCacheDir(actualCacheOwner), cacheName);
Jeff Sharkey413573a2016-02-22 17:52:45 -0700793 return ParcelFileDescriptor.open(cacheFile, ParcelFileDescriptor.parseMode(mode));
794 }
795
796 private File getRingtoneCacheDir(int userId) {
797 final File cacheDir = new File(Environment.getDataSystemDeDirectory(userId), "ringtones");
798 cacheDir.mkdir();
799 SELinux.restorecon(cacheDir);
800 return cacheDir;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700801 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800802
Eugene Suslad72c3972016-12-27 15:49:30 -0800803 /**
804 * Dump all settings as a proto buf.
805 *
806 * @param fd The file to dump to
807 */
808 void dumpProto(@NonNull FileDescriptor fd) {
809 ProtoOutputStream proto = new ProtoOutputStream(fd);
810
811 synchronized (mLock) {
812 SettingsProtoDumpUtil.dumpProtoLocked(mSettingsRegistry, proto);
Eugene Suslad72c3972016-12-27 15:49:30 -0800813 }
814
815 proto.flush();
816 }
817
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700818 public void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800819 synchronized (mLock) {
820 final long identity = Binder.clearCallingIdentity();
821 try {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700822 SparseBooleanArray users = mSettingsRegistry.getKnownUsersLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800823 final int userCount = users.size();
824 for (int i = 0; i < userCount; i++) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700825 dumpForUserLocked(users.keyAt(i), pw);
Svetoslavb505ccc2015-02-17 12:41:04 -0800826 }
827 } finally {
828 Binder.restoreCallingIdentity(identity);
829 }
830 }
831 }
832
Andreas Gampeb58893072018-09-05 16:52:31 -0700833 @GuardedBy("mLock")
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700834 private void dumpForUserLocked(int userId, PrintWriter pw) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700835 if (userId == UserHandle.USER_SYSTEM) {
Matt Pape1b31a332018-10-17 09:58:28 -0700836 pw.println("CONFIG SETTINGS (user " + userId + ")");
837 SettingsState configSettings = mSettingsRegistry.getSettingsLocked(
838 SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM);
839 if (configSettings != null) {
840 dumpSettingsLocked(configSettings, pw);
841 pw.println();
842 configSettings.dumpHistoricalOperations(pw);
843 }
844
Svetoslavb505ccc2015-02-17 12:41:04 -0800845 pw.println("GLOBAL SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700846 SettingsState globalSettings = mSettingsRegistry.getSettingsLocked(
847 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700848 if (globalSettings != null) {
849 dumpSettingsLocked(globalSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800850 pw.println();
851 globalSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700852 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800853 }
854
855 pw.println("SECURE SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700856 SettingsState secureSettings = mSettingsRegistry.getSettingsLocked(
857 SETTINGS_TYPE_SECURE, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700858 if (secureSettings != null) {
859 dumpSettingsLocked(secureSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800860 pw.println();
861 secureSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700862 }
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700863
Svetoslavb505ccc2015-02-17 12:41:04 -0800864 pw.println("SYSTEM SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700865 SettingsState systemSettings = mSettingsRegistry.getSettingsLocked(
866 SETTINGS_TYPE_SYSTEM, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700867 if (systemSettings != null) {
868 dumpSettingsLocked(systemSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800869 pw.println();
870 systemSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700871 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800872 }
873
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700874 private void dumpSettingsLocked(SettingsState settingsState, PrintWriter pw) {
875 List<String> names = settingsState.getSettingNamesLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800876
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700877 final int nameCount = names.size();
Svetoslavb505ccc2015-02-17 12:41:04 -0800878
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700879 for (int i = 0; i < nameCount; i++) {
880 String name = names.get(i);
881 Setting setting = settingsState.getSettingLocked(name);
882 pw.print("_id:"); pw.print(toDumpString(setting.getId()));
883 pw.print(" name:"); pw.print(toDumpString(name));
884 if (setting.getPackageName() != null) {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800885 pw.print(" pkg:"); pw.print(setting.getPackageName());
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700886 }
887 pw.print(" value:"); pw.print(toDumpString(setting.getValue()));
Svetoslav Ganove080da92016-12-21 17:10:35 -0800888 if (setting.getDefaultValue() != null) {
889 pw.print(" default:"); pw.print(setting.getDefaultValue());
Eugene Suslad72c3972016-12-27 15:49:30 -0800890 pw.print(" defaultSystemSet:"); pw.print(setting.isDefaultFromSystem());
Svetoslav Ganove080da92016-12-21 17:10:35 -0800891 }
892 if (setting.getTag() != null) {
893 pw.print(" tag:"); pw.print(setting.getTag());
894 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800895 pw.println();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700896 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800897 }
898
Svetoslav7e0683b2015-08-03 16:02:52 -0700899 private static String toDumpString(String s) {
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700900 if (s != null) {
901 return s;
902 }
903 return "{null}";
904 }
905
Svetoslav683914b2015-01-15 14:22:26 -0800906 private void registerBroadcastReceivers() {
907 IntentFilter userFilter = new IntentFilter();
908 userFilter.addAction(Intent.ACTION_USER_REMOVED);
909 userFilter.addAction(Intent.ACTION_USER_STOPPED);
910
911 getContext().registerReceiver(new BroadcastReceiver() {
912 @Override
913 public void onReceive(Context context, Intent intent) {
914 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700915 UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800916
917 switch (intent.getAction()) {
918 case Intent.ACTION_USER_REMOVED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700919 synchronized (mLock) {
920 mSettingsRegistry.removeUserStateLocked(userId, true);
921 }
Svetoslav683914b2015-01-15 14:22:26 -0800922 } break;
923
924 case Intent.ACTION_USER_STOPPED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700925 synchronized (mLock) {
926 mSettingsRegistry.removeUserStateLocked(userId, false);
927 }
Svetoslav683914b2015-01-15 14:22:26 -0800928 } break;
929 }
930 }
931 }, userFilter);
932
933 PackageMonitor monitor = new PackageMonitor() {
934 @Override
935 public void onPackageRemoved(String packageName, int uid) {
936 synchronized (mLock) {
Zimuzoc56192c2018-07-25 10:40:01 +0100937 mSettingsRegistry.removeSettingsForPackageLocked(packageName,
Svetoslav683914b2015-01-15 14:22:26 -0800938 UserHandle.getUserId(uid));
939 }
940 }
Mark Rathjend891f012017-01-19 04:10:37 +0000941
942 @Override
943 public void onUidRemoved(int uid) {
944 synchronized (mLock) {
945 mSettingsRegistry.onUidRemovedLocked(uid);
946 }
947 }
Zimuzoc56192c2018-07-25 10:40:01 +0100948
949 @Override
950 public void onPackageDataCleared(String packageName, int uid) {
951 synchronized (mLock) {
952 mSettingsRegistry.removeSettingsForPackageLocked(packageName,
953 UserHandle.getUserId(uid));
954 }
955 }
Svetoslav683914b2015-01-15 14:22:26 -0800956 };
957
958 // package changes
959 monitor.register(getContext(), BackgroundThread.getHandler().getLooper(),
960 UserHandle.ALL, true);
961 }
962
Svet Ganov53a441c2016-04-19 19:38:00 -0700963 private void startWatchingUserRestrictionChanges() {
964 // TODO: The current design of settings looking different based on user restrictions
965 // should be reworked to keep them separate and system code should check the setting
966 // first followed by checking the user restriction before performing an operation.
967 UserManagerInternal userManager = LocalServices.getService(UserManagerInternal.class);
968 userManager.addUserRestrictionsListener((int userId, Bundle newRestrictions,
969 Bundle prevRestrictions) -> {
Irina Dumitrescue3696872019-01-09 16:07:59 +0000970 Set<String> changedRestrictions = getRestrictionDiff(prevRestrictions, newRestrictions);
Svet Ganov53a441c2016-04-19 19:38:00 -0700971 // We are changing the settings affected by restrictions to their current
972 // value with a forced update to ensure that all cross profile dependencies
973 // are taken into account. Also make sure the settings update to.. the same
974 // value passes the security checks, so clear binder calling id.
Irina Dumitrescue3696872019-01-09 16:07:59 +0000975 if (changedRestrictions.contains(UserManager.DISALLOW_SHARE_LOCATION)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700976 final long identity = Binder.clearCallingIdentity();
977 try {
978 synchronized (mLock) {
979 Setting setting = getSecureSetting(
Soonil Nagarkar4ee3ac22019-02-08 19:19:24 -0800980 Settings.Secure.LOCATION_MODE, userId);
981 updateSecureSetting(Settings.Secure.LOCATION_MODE,
982 setting != null ? setting.getValue() : null, null,
983 true, userId, true);
984 setting = getSecureSetting(
Svet Ganov53a441c2016-04-19 19:38:00 -0700985 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, userId);
986 updateSecureSetting(Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800987 setting != null ? setting.getValue() : null, null,
988 true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700989 }
990 } finally {
991 Binder.restoreCallingIdentity(identity);
992 }
993 }
Irina Dumitrescue3696872019-01-09 16:07:59 +0000994 if (changedRestrictions.contains(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)
995 || changedRestrictions.contains(
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100996 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700997 final long identity = Binder.clearCallingIdentity();
998 try {
999 synchronized (mLock) {
1000 Setting setting = getGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS);
Svetoslav Ganove080da92016-12-21 17:10:35 -08001001 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -07001002 updateGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001003 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -07001004 }
1005 } finally {
1006 Binder.restoreCallingIdentity(identity);
1007 }
1008 }
Irina Dumitrescue3696872019-01-09 16:07:59 +00001009 if (changedRestrictions.contains(UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001010 final long identity = Binder.clearCallingIdentity();
1011 try {
1012 synchronized (mLock) {
1013 Setting setting = getGlobalSetting(Settings.Global.ADB_ENABLED);
Svetoslav Ganove080da92016-12-21 17:10:35 -08001014 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -07001015 updateGlobalSetting(Settings.Global.ADB_ENABLED,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001016 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -07001017 }
1018 } finally {
1019 Binder.restoreCallingIdentity(identity);
1020 }
1021 }
Irina Dumitrescue3696872019-01-09 16:07:59 +00001022 if (changedRestrictions.contains(UserManager.ENSURE_VERIFY_APPS)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001023 final long identity = Binder.clearCallingIdentity();
1024 try {
1025 synchronized (mLock) {
1026 Setting enable = getGlobalSetting(
1027 Settings.Global.PACKAGE_VERIFIER_ENABLE);
Svetoslav Ganove080da92016-12-21 17:10:35 -08001028 String enableValue = enable != null ? enable.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -07001029 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_ENABLE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001030 enableValue, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -07001031 Setting include = getGlobalSetting(
1032 Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB);
Svetoslav Ganove080da92016-12-21 17:10:35 -08001033 String includeValue = include != null ? include.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -07001034 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001035 includeValue, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -07001036 }
1037 } finally {
1038 Binder.restoreCallingIdentity(identity);
1039 }
1040 }
Irina Dumitrescue3696872019-01-09 16:07:59 +00001041 if (changedRestrictions.contains(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganov53a441c2016-04-19 19:38:00 -07001042 final long identity = Binder.clearCallingIdentity();
1043 try {
1044 synchronized (mLock) {
1045 Setting setting = getGlobalSetting(
1046 Settings.Global.PREFERRED_NETWORK_MODE);
Svetoslav Ganove080da92016-12-21 17:10:35 -08001047 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -07001048 updateGlobalSetting(Settings.Global.PREFERRED_NETWORK_MODE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001049 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -07001050 }
1051 } finally {
1052 Binder.restoreCallingIdentity(identity);
1053 }
1054 }
1055 });
1056 }
1057
Irina Dumitrescue3696872019-01-09 16:07:59 +00001058 private static Set<String> getRestrictionDiff(Bundle prevRestrictions, Bundle newRestrictions) {
1059 Set<String> restrictionNames = Sets.newArraySet();
1060 restrictionNames.addAll(prevRestrictions.keySet());
1061 restrictionNames.addAll(newRestrictions.keySet());
1062 Set<String> diff = Sets.newArraySet();
1063 for (String restrictionName : restrictionNames) {
1064 if (prevRestrictions.getBoolean(restrictionName) != newRestrictions.getBoolean(
1065 restrictionName)) {
1066 diff.add(restrictionName);
1067 }
1068 }
1069 return diff;
1070 }
1071
Matt Pape1b31a332018-10-17 09:58:28 -07001072 private Setting getConfigSetting(String name) {
1073 if (DEBUG) {
1074 Slog.v(LOG_TAG, "getConfigSetting(" + name + ")");
1075 }
1076
Stanislav Zholnin55799502019-03-08 14:54:55 +00001077 DeviceConfig.enforceReadPermission(getContext(), /*namespace=*/name.split("/")[0]);
Matt Pape1b31a332018-10-17 09:58:28 -07001078
1079 // Get the value.
1080 synchronized (mLock) {
1081 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_CONFIG,
1082 UserHandle.USER_SYSTEM, name);
1083 }
1084 }
1085
Matt Papec1323dc2018-12-11 12:32:42 -08001086 private boolean insertConfigSetting(String name, String value, boolean makeDefault) {
Matt Pape1b31a332018-10-17 09:58:28 -07001087 if (DEBUG) {
1088 Slog.v(LOG_TAG, "insertConfigSetting(" + name + ", " + value + ", "
Matt Papec1323dc2018-12-11 12:32:42 -08001089 + makeDefault + ")");
Matt Pape1b31a332018-10-17 09:58:28 -07001090 }
Matt Papec1323dc2018-12-11 12:32:42 -08001091 return mutateConfigSetting(name, value, null, makeDefault,
1092 MUTATION_OPERATION_INSERT, 0);
Matt Pape1b31a332018-10-17 09:58:28 -07001093 }
1094
Matt Papec1323dc2018-12-11 12:32:42 -08001095 private boolean deleteConfigSetting(String name) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08001096 if (DEBUG) {
Matt Papec1323dc2018-12-11 12:32:42 -08001097 Slog.v(LOG_TAG, "deleteConfigSetting(" + name + ")");
Matt Pape6bfc62e2018-11-28 13:16:03 -08001098 }
Matt Papec1323dc2018-12-11 12:32:42 -08001099 return mutateConfigSetting(name, null, null, false,
1100 MUTATION_OPERATION_DELETE, 0);
Matt Pape6bfc62e2018-11-28 13:16:03 -08001101 }
1102
Matt Papec1323dc2018-12-11 12:32:42 -08001103 private void resetConfigSetting(int mode, String prefix) {
Matt Pape1b31a332018-10-17 09:58:28 -07001104 if (DEBUG) {
Matt Papec1323dc2018-12-11 12:32:42 -08001105 Slog.v(LOG_TAG, "resetConfigSetting(" + mode + ", " + prefix + ")");
Matt Pape1b31a332018-10-17 09:58:28 -07001106 }
Matt Papec1323dc2018-12-11 12:32:42 -08001107 mutateConfigSetting(null, null, prefix, false,
1108 MUTATION_OPERATION_RESET, mode);
Matt Pape1b31a332018-10-17 09:58:28 -07001109 }
1110
Matt Pape6bfc62e2018-11-28 13:16:03 -08001111 private boolean mutateConfigSetting(String name, String value, String prefix,
Matt Papec1323dc2018-12-11 12:32:42 -08001112 boolean makeDefault, int operation, int mode) {
Stanislav Zholnin5a25a842019-03-13 14:43:26 +00001113 enforceWritePermission(Manifest.permission.WRITE_DEVICE_CONFIG);
Matt Pape1b31a332018-10-17 09:58:28 -07001114
Matt Pape1b31a332018-10-17 09:58:28 -07001115 // Perform the mutation.
1116 synchronized (mLock) {
1117 switch (operation) {
1118 case MUTATION_OPERATION_INSERT: {
1119 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_CONFIG,
Matt Papec1323dc2018-12-11 12:32:42 -08001120 UserHandle.USER_SYSTEM, name, value, null, makeDefault, true,
Matt Pape7b1c6cd2019-01-04 08:10:41 -08001121 resolveCallingPackage(), false, null);
Matt Pape1b31a332018-10-17 09:58:28 -07001122 }
1123
Matt Pape6bfc62e2018-11-28 13:16:03 -08001124 case MUTATION_OPERATION_DELETE: {
1125 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_CONFIG,
Matt Papec1323dc2018-12-11 12:32:42 -08001126 UserHandle.USER_SYSTEM, name, false, null);
Matt Pape6bfc62e2018-11-28 13:16:03 -08001127 }
1128
Matt Pape1b31a332018-10-17 09:58:28 -07001129 case MUTATION_OPERATION_RESET: {
1130 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_CONFIG,
Matt Pape7b1c6cd2019-01-04 08:10:41 -08001131 UserHandle.USER_SYSTEM, resolveCallingPackage(), mode, null, prefix);
Matt Pape1b31a332018-10-17 09:58:28 -07001132 } return true;
1133 }
1134 }
1135
1136 return false;
1137 }
1138
Matt Pape6bfc62e2018-11-28 13:16:03 -08001139 private Map<String, String> getAllConfigFlags(@Nullable String prefix) {
1140 if (DEBUG) {
1141 Slog.v(LOG_TAG, "getAllConfigFlags() for " + prefix);
1142 }
1143
1144 synchronized (mLock) {
1145 // Get the settings.
1146 SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
1147 SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM);
1148
1149 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_CONFIG,
1150 UserHandle.USER_SYSTEM);
1151
1152 final int nameCount = names.size();
1153 Map<String, String> flagsToValues = new HashMap<>(names.size());
1154
1155 for (int i = 0; i < nameCount; i++) {
1156 String name = names.get(i);
1157 Setting setting = settingsState.getSettingLocked(name);
1158 if (prefix == null || setting.getName().startsWith(prefix)) {
1159 flagsToValues.put(setting.getName(), setting.getValue());
1160 }
1161 }
1162
1163 return flagsToValues;
1164 }
1165 }
1166
Svetoslav7ec28e82015-05-20 17:01:10 -07001167 private Cursor getAllGlobalSettings(String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001168 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001169 Slog.v(LOG_TAG, "getAllGlobalSettings()");
Svetoslav683914b2015-01-15 14:22:26 -08001170 }
1171
Svetoslav7ec28e82015-05-20 17:01:10 -07001172 synchronized (mLock) {
1173 // Get the settings.
1174 SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001175 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001176
Chad Brubaker97bccee2017-01-05 15:51:41 -08001177 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_GLOBAL,
1178 UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001179
Svetoslav7ec28e82015-05-20 17:01:10 -07001180 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001181
Svetoslav7ec28e82015-05-20 17:01:10 -07001182 String[] normalizedProjection = normalizeProjection(projection);
1183 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001184
Svetoslav7ec28e82015-05-20 17:01:10 -07001185 // Anyone can get the global settings, so no security checks.
1186 for (int i = 0; i < nameCount; i++) {
1187 String name = names.get(i);
1188 Setting setting = settingsState.getSettingLocked(name);
1189 appendSettingToCursor(result, setting);
1190 }
1191
1192 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001193 }
Svetoslav683914b2015-01-15 14:22:26 -08001194 }
1195
Svetoslav7ec28e82015-05-20 17:01:10 -07001196 private Setting getGlobalSetting(String name) {
Svetoslav683914b2015-01-15 14:22:26 -08001197 if (DEBUG) {
1198 Slog.v(LOG_TAG, "getGlobalSetting(" + name + ")");
1199 }
1200
Chad Brubakera6830e72017-04-28 17:34:36 -07001201 // Ensure the caller can access the setting.
1202 enforceSettingReadable(name, SETTINGS_TYPE_GLOBAL, UserHandle.getCallingUserId());
1203
Svetoslav683914b2015-01-15 14:22:26 -08001204 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001205 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -07001206 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_GLOBAL,
Xiaohui Chen43765b72015-08-31 10:57:33 -07001207 UserHandle.USER_SYSTEM, name);
Svetoslav683914b2015-01-15 14:22:26 -08001208 }
Svetoslav683914b2015-01-15 14:22:26 -08001209 }
1210
Svetoslav Ganove080da92016-12-21 17:10:35 -08001211 private boolean updateGlobalSetting(String name, String value, String tag,
1212 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001213 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001214 Slog.v(LOG_TAG, "updateGlobalSetting(" + name + ", " + value + ", "
1215 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1216 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001217 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001218 return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId,
1219 MUTATION_OPERATION_UPDATE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001220 }
1221
Svetoslav Ganove080da92016-12-21 17:10:35 -08001222 private boolean insertGlobalSetting(String name, String value, String tag,
1223 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001224 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001225 Slog.v(LOG_TAG, "insertGlobalSetting(" + name + ", " + value + ", "
1226 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1227 + ", " + forceNotify + ")");
Svetoslav7ec28e82015-05-20 17:01:10 -07001228 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001229 return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId,
1230 MUTATION_OPERATION_INSERT, forceNotify, 0);
Svetoslav7ec28e82015-05-20 17:01:10 -07001231 }
1232
Svet Ganov53a441c2016-04-19 19:38:00 -07001233 private boolean deleteGlobalSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001234 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001235 Slog.v(LOG_TAG, "deleteGlobalSetting(" + name + ", " + requestingUserId
1236 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001237 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001238 return mutateGlobalSetting(name, null, null, false, requestingUserId,
1239 MUTATION_OPERATION_DELETE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001240 }
1241
Svetoslav Ganove080da92016-12-21 17:10:35 -08001242 private void resetGlobalSetting(int requestingUserId, int mode, String tag) {
1243 if (DEBUG) {
1244 Slog.v(LOG_TAG, "resetGlobalSetting(" + requestingUserId + ", "
1245 + mode + ", " + tag + ")");
1246 }
1247 mutateGlobalSetting(null, null, tag, false, requestingUserId,
1248 MUTATION_OPERATION_RESET, false, mode);
1249 }
1250
1251 private boolean mutateGlobalSetting(String name, String value, String tag,
1252 boolean makeDefault, int requestingUserId, int operation, boolean forceNotify,
1253 int mode) {
Svetoslav683914b2015-01-15 14:22:26 -08001254 // Make sure the caller can change the settings - treated as secure.
1255 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
1256
Svetoslav683914b2015-01-15 14:22:26 -08001257 // Resolve the userId on whose behalf the call is made.
1258 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1259
Makoto Onuki28da2e32015-11-20 11:30:44 -08001260 // If this is a setting that is currently restricted for this user, do not allow
1261 // unrestricting changes.
yuemingw1d13eae2018-01-30 17:27:54 +00001262 if (name != null && mUserManagerInternal.isSettingRestrictedForUser(
1263 name, callingUserId, value, Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -08001264 return false;
1265 }
1266
1267 // Perform the mutation.
Svetoslav7ec28e82015-05-20 17:01:10 -07001268 synchronized (mLock) {
1269 switch (operation) {
1270 case MUTATION_OPERATION_INSERT: {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001271 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_GLOBAL,
1272 UserHandle.USER_SYSTEM, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001273 getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001274 }
Svetoslav683914b2015-01-15 14:22:26 -08001275
Svetoslav7ec28e82015-05-20 17:01:10 -07001276 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001277 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_GLOBAL,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001278 UserHandle.USER_SYSTEM, name, forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001279 }
Svetoslav683914b2015-01-15 14:22:26 -08001280
Svetoslav7ec28e82015-05-20 17:01:10 -07001281 case MUTATION_OPERATION_UPDATE: {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001282 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_GLOBAL,
1283 UserHandle.USER_SYSTEM, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001284 getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001285 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001286
1287 case MUTATION_OPERATION_RESET: {
1288 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_GLOBAL,
1289 UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag);
1290 } return true;
Svetoslav683914b2015-01-15 14:22:26 -08001291 }
1292 }
1293
1294 return false;
1295 }
1296
Christopher Tateb218e762017-04-05 16:34:07 -07001297 private PackageInfo getCallingPackageInfo(int userId) {
1298 try {
1299 return mPackageManager.getPackageInfo(getCallingPackage(),
1300 PackageManager.GET_SIGNATURES, userId);
1301 } catch (RemoteException e) {
1302 throw new IllegalStateException("Package " + getCallingPackage() + " doesn't exist");
1303 }
1304 }
1305
Svetoslav7ec28e82015-05-20 17:01:10 -07001306 private Cursor getAllSecureSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001307 if (DEBUG) {
1308 Slog.v(LOG_TAG, "getAllSecureSettings(" + userId + ")");
1309 }
1310
1311 // Resolve the userId on whose behalf the call is made.
1312 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
1313
Christopher Tateb218e762017-04-05 16:34:07 -07001314 // The relevant "calling package" userId will be the owning userId for some
1315 // profiles, and we can't do the lookup inside our [lock held] loop, so work out
1316 // up front who the effective "new SSAID" user ID for that settings name will be.
1317 final int ssaidUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
1318 Settings.Secure.ANDROID_ID);
1319 final PackageInfo ssaidCallingPkg = getCallingPackageInfo(ssaidUserId);
1320
Svetoslav7ec28e82015-05-20 17:01:10 -07001321 synchronized (mLock) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001322 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SECURE, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -08001323
Svetoslav7ec28e82015-05-20 17:01:10 -07001324 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001325
Svetoslav7ec28e82015-05-20 17:01:10 -07001326 String[] normalizedProjection = normalizeProjection(projection);
1327 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001328
Svetoslav7ec28e82015-05-20 17:01:10 -07001329 for (int i = 0; i < nameCount; i++) {
1330 String name = names.get(i);
1331 // Determine the owning user as some profile settings are cloned from the parent.
1332 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
1333 name);
Svetoslav683914b2015-01-15 14:22:26 -08001334
Alex Klyubin1991f572017-03-03 14:08:36 -08001335 if (!isSecureSettingAccessible(name, callingUserId, owningUserId)) {
1336 // This caller is not permitted to access this setting. Pretend the setting
1337 // doesn't exist.
Svetoslav Ganov83a1f7f2016-04-27 13:50:49 -07001338 continue;
Svetoslav7ec28e82015-05-20 17:01:10 -07001339 }
Svetoslav683914b2015-01-15 14:22:26 -08001340
Mark Rathjen7599f132017-01-23 14:15:54 -08001341 // As of Android O, the SSAID is read from an app-specific entry in table
Mark Rathjend891f012017-01-19 04:10:37 +00001342 // SETTINGS_FILE_SSAID, unless accessed by a system process.
1343 final Setting setting;
1344 if (isNewSsaidSetting(name)) {
Christopher Tateb218e762017-04-05 16:34:07 -07001345 setting = getSsaidSettingLocked(ssaidCallingPkg, owningUserId);
Mark Rathjend891f012017-01-19 04:10:37 +00001346 } else {
1347 setting = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE, owningUserId,
1348 name);
1349 }
Svetoslav7ec28e82015-05-20 17:01:10 -07001350 appendSettingToCursor(result, setting);
Svetoslav683914b2015-01-15 14:22:26 -08001351 }
1352
Svetoslav7ec28e82015-05-20 17:01:10 -07001353 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001354 }
Svetoslav683914b2015-01-15 14:22:26 -08001355 }
1356
Svetoslav7ec28e82015-05-20 17:01:10 -07001357 private Setting getSecureSetting(String name, int requestingUserId) {
Makoto Onuki0000d322017-11-28 16:31:47 -08001358 return getSecureSetting(name, requestingUserId, /*enableOverride=*/ false);
1359 }
1360
1361 private Setting getSecureSetting(String name, int requestingUserId, boolean enableOverride) {
Svetoslav683914b2015-01-15 14:22:26 -08001362 if (DEBUG) {
1363 Slog.v(LOG_TAG, "getSecureSetting(" + name + ", " + requestingUserId + ")");
1364 }
1365
1366 // Resolve the userId on whose behalf the call is made.
1367 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1368
Chad Brubakera6830e72017-04-28 17:34:36 -07001369 // Ensure the caller can access the setting.
1370 enforceSettingReadable(name, SETTINGS_TYPE_SECURE, UserHandle.getCallingUserId());
1371
Svetoslav683914b2015-01-15 14:22:26 -08001372 // Determine the owning user as some profile settings are cloned from the parent.
1373 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
1374
Alex Klyubin1991f572017-03-03 14:08:36 -08001375 if (!isSecureSettingAccessible(name, callingUserId, owningUserId)) {
1376 // This caller is not permitted to access this setting. Pretend the setting doesn't
1377 // exist.
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001378 SettingsState settings = mSettingsRegistry.getSettingsLocked(SETTINGS_TYPE_SECURE,
1379 owningUserId);
1380 return settings != null ? settings.getNullSetting() : null;
Svetoslav683914b2015-01-15 14:22:26 -08001381 }
1382
Christopher Tateb218e762017-04-05 16:34:07 -07001383 // As of Android O, the SSAID is read from an app-specific entry in table
1384 // SETTINGS_FILE_SSAID, unless accessed by a system process.
1385 if (isNewSsaidSetting(name)) {
1386 PackageInfo callingPkg = getCallingPackageInfo(owningUserId);
1387 synchronized (mLock) {
1388 return getSsaidSettingLocked(callingPkg, owningUserId);
Mark Rathjend891f012017-01-19 04:10:37 +00001389 }
Christopher Tateb218e762017-04-05 16:34:07 -07001390 }
Makoto Onuki0000d322017-11-28 16:31:47 -08001391 if (enableOverride) {
Kweku Adams5e0052b2019-02-22 15:17:52 -08001392 if (Secure.LOCATION_MODE.equals(name)) {
1393 final Setting overridden = getLocationModeSetting(owningUserId);
Makoto Onuki0000d322017-11-28 16:31:47 -08001394 if (overridden != null) {
1395 return overridden;
1396 }
1397 }
1398 }
Mark Rathjend891f012017-01-19 04:10:37 +00001399
Christopher Tateb218e762017-04-05 16:34:07 -07001400 // Not the SSAID; do a straight lookup
1401 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -07001402 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav7ec28e82015-05-20 17:01:10 -07001403 owningUserId, name);
1404 }
Svetoslav683914b2015-01-15 14:22:26 -08001405 }
1406
Mark Rathjend891f012017-01-19 04:10:37 +00001407 private boolean isNewSsaidSetting(String name) {
1408 return Settings.Secure.ANDROID_ID.equals(name)
1409 && UserHandle.getAppId(Binder.getCallingUid()) >= Process.FIRST_APPLICATION_UID;
1410 }
1411
Andreas Gampeb58893072018-09-05 16:52:31 -07001412 @GuardedBy("mLock")
Christopher Tateb218e762017-04-05 16:34:07 -07001413 private Setting getSsaidSettingLocked(PackageInfo callingPkg, int owningUserId) {
Mark Rathjend891f012017-01-19 04:10:37 +00001414 // Get uid of caller (key) used to store ssaid value
1415 String name = Integer.toString(
1416 UserHandle.getUid(owningUserId, UserHandle.getAppId(Binder.getCallingUid())));
1417
1418 if (DEBUG) {
1419 Slog.v(LOG_TAG, "getSsaidSettingLocked(" + name + "," + owningUserId + ")");
1420 }
1421
1422 // Retrieve the ssaid from the table if present.
1423 final Setting ssaid = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SSAID, owningUserId,
1424 name);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001425 // If the app is an Instant App use its stored SSAID instead of our own.
1426 final String instantSsaid;
1427 final long token = Binder.clearCallingIdentity();
1428 try {
1429 instantSsaid = mPackageManager.getInstantAppAndroidId(callingPkg.packageName,
1430 owningUserId);
1431 } catch (RemoteException e) {
1432 Slog.e(LOG_TAG, "Failed to get Instant App Android ID", e);
1433 return null;
1434 } finally {
1435 Binder.restoreCallingIdentity(token);
1436 }
Svet Ganov96c99462017-05-05 14:27:13 -07001437
1438 final SettingsState ssaidSettings = mSettingsRegistry.getSettingsLocked(
1439 SETTINGS_TYPE_SSAID, owningUserId);
1440
Chad Brubaker0d277a72017-04-12 16:56:53 -07001441 if (instantSsaid != null) {
1442 // Use the stored value if it is still valid.
1443 if (ssaid != null && instantSsaid.equals(ssaid.getValue())) {
Svet Ganov96c99462017-05-05 14:27:13 -07001444 return mascaradeSsaidSetting(ssaidSettings, ssaid);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001445 }
1446 // The value has changed, update the stored value.
Chad Brubaker0d277a72017-04-12 16:56:53 -07001447 final boolean success = ssaidSettings.insertSettingLocked(name, instantSsaid, null,
1448 true, callingPkg.packageName);
1449 if (!success) {
1450 throw new IllegalStateException("Failed to update instant app android id");
1451 }
Svet Ganov96c99462017-05-05 14:27:13 -07001452 Setting setting = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SSAID,
1453 owningUserId, name);
1454 return mascaradeSsaidSetting(ssaidSettings, setting);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001455 }
Mark Rathjend891f012017-01-19 04:10:37 +00001456
1457 // Lazy initialize ssaid if not yet present in ssaid table.
Mark Rathjenea617592017-01-18 23:03:41 -08001458 if (ssaid == null || ssaid.isNull() || ssaid.getValue() == null) {
Svet Ganov96c99462017-05-05 14:27:13 -07001459 Setting setting = mSettingsRegistry.generateSsaidLocked(callingPkg, owningUserId);
1460 return mascaradeSsaidSetting(ssaidSettings, setting);
Mark Rathjend891f012017-01-19 04:10:37 +00001461 }
1462
Svet Ganov96c99462017-05-05 14:27:13 -07001463 return mascaradeSsaidSetting(ssaidSettings, ssaid);
1464 }
1465
1466 private Setting mascaradeSsaidSetting(SettingsState settingsState, Setting ssaidSetting) {
1467 // SSAID settings are located in a dedicated table for internal bookkeeping
1468 // but for the world they reside in the secure table, so adjust the key here.
1469 // We have a special name when looking it up but want the world to see it as
1470 // "android_id".
1471 if (ssaidSetting != null) {
1472 return settingsState.new Setting(ssaidSetting) {
1473 @Override
1474 public int getKey() {
1475 final int userId = getUserIdFromKey(super.getKey());
1476 return makeKey(SETTINGS_TYPE_SECURE, userId);
1477 }
1478
1479 @Override
1480 public String getName() {
1481 return Settings.Secure.ANDROID_ID;
1482 }
1483 };
1484 }
1485 return null;
Mark Rathjend891f012017-01-19 04:10:37 +00001486 }
1487
Kweku Adams5e0052b2019-02-22 15:17:52 -08001488 private Setting getLocationModeSetting(int owningUserId) {
Makoto Onuki0000d322017-11-28 16:31:47 -08001489 synchronized (mLock) {
1490 final Setting setting = getGlobalSetting(
1491 Global.LOCATION_GLOBAL_KILL_SWITCH);
1492 if (!"1".equals(setting.getValue())) {
1493 return null;
1494 }
1495 // Global kill-switch is enabled. Return an empty value.
1496 final SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
1497 SETTINGS_TYPE_SECURE, owningUserId);
1498 return settingsState.new Setting(
Kweku Adams5e0052b2019-02-22 15:17:52 -08001499 Secure.LOCATION_MODE,
Makoto Onuki0000d322017-11-28 16:31:47 -08001500 "", // value
1501 "", // tag
1502 "", // default value
1503 "", // package name
1504 false, // from system
1505 "0" // id
1506 ) {
1507 @Override
1508 public boolean update(String value, boolean setDefault, String packageName,
1509 String tag, boolean forceNonSystemPackage) {
Kweku Adams5e0052b2019-02-22 15:17:52 -08001510 Slog.wtf(LOG_TAG, "update shouldn't be called on this instance.");
Makoto Onuki0000d322017-11-28 16:31:47 -08001511 return false;
1512 }
1513 };
1514 }
1515 }
1516
Svetoslav Ganove080da92016-12-21 17:10:35 -08001517 private boolean insertSecureSetting(String name, String value, String tag,
1518 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001519 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001520 Slog.v(LOG_TAG, "insertSecureSetting(" + name + ", " + value + ", "
Svetoslav Ganove080da92016-12-21 17:10:35 -08001521 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1522 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001523 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001524 return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId,
1525 MUTATION_OPERATION_INSERT, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001526 }
1527
Svet Ganov53a441c2016-04-19 19:38:00 -07001528 private boolean deleteSecureSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001529 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001530 Slog.v(LOG_TAG, "deleteSecureSetting(" + name + ", " + requestingUserId
1531 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001532 }
1533
Svetoslav Ganove080da92016-12-21 17:10:35 -08001534 return mutateSecureSetting(name, null, null, false, requestingUserId,
1535 MUTATION_OPERATION_DELETE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001536 }
1537
Svetoslav Ganove080da92016-12-21 17:10:35 -08001538 private boolean updateSecureSetting(String name, String value, String tag,
1539 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001540 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001541 Slog.v(LOG_TAG, "updateSecureSetting(" + name + ", " + value + ", "
Svetoslav Ganove080da92016-12-21 17:10:35 -08001542 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1543 + ", " + forceNotify +")");
Svetoslav683914b2015-01-15 14:22:26 -08001544 }
1545
Svetoslav Ganove080da92016-12-21 17:10:35 -08001546 return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId,
1547 MUTATION_OPERATION_UPDATE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001548 }
1549
Svetoslav Ganove080da92016-12-21 17:10:35 -08001550 private void resetSecureSetting(int requestingUserId, int mode, String tag) {
1551 if (DEBUG) {
1552 Slog.v(LOG_TAG, "resetSecureSetting(" + requestingUserId + ", "
1553 + mode + ", " + tag + ")");
1554 }
1555
1556 mutateSecureSetting(null, null, tag, false, requestingUserId,
1557 MUTATION_OPERATION_RESET, false, mode);
1558 }
1559
1560 private boolean mutateSecureSetting(String name, String value, String tag,
1561 boolean makeDefault, int requestingUserId, int operation, boolean forceNotify,
1562 int mode) {
Svetoslav683914b2015-01-15 14:22:26 -08001563 // Make sure the caller can change the settings.
1564 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
1565
Svetoslav683914b2015-01-15 14:22:26 -08001566 // Resolve the userId on whose behalf the call is made.
1567 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1568
Makoto Onuki28da2e32015-11-20 11:30:44 -08001569 // If this is a setting that is currently restricted for this user, do not allow
1570 // unrestricting changes.
yuemingw1d13eae2018-01-30 17:27:54 +00001571 if (name != null && mUserManagerInternal.isSettingRestrictedForUser(
1572 name, callingUserId, value, Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -08001573 return false;
1574 }
1575
1576 // Determine the owning user as some profile settings are cloned from the parent.
1577 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
1578
1579 // Only the owning user can change the setting.
1580 if (owningUserId != callingUserId) {
1581 return false;
1582 }
1583
1584 // Special cases for location providers (sigh).
1585 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001586 return updateLocationProvidersAllowedLocked(value, tag, owningUserId, makeDefault,
1587 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -08001588 }
1589
1590 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001591 synchronized (mLock) {
1592 switch (operation) {
1593 case MUTATION_OPERATION_INSERT: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001594 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001595 owningUserId, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001596 getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001597 }
Svetoslav683914b2015-01-15 14:22:26 -08001598
Svetoslav7ec28e82015-05-20 17:01:10 -07001599 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001600 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001601 owningUserId, name, forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001602 }
Svetoslav683914b2015-01-15 14:22:26 -08001603
Svetoslav7ec28e82015-05-20 17:01:10 -07001604 case MUTATION_OPERATION_UPDATE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001605 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001606 owningUserId, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001607 getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001608 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001609
1610 case MUTATION_OPERATION_RESET: {
1611 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_SECURE,
1612 UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag);
1613 } return true;
Svetoslav683914b2015-01-15 14:22:26 -08001614 }
1615 }
1616
1617 return false;
1618 }
1619
Svetoslav7ec28e82015-05-20 17:01:10 -07001620 private Cursor getAllSystemSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001621 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001622 Slog.v(LOG_TAG, "getAllSecureSystem(" + userId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001623 }
1624
1625 // Resolve the userId on whose behalf the call is made.
1626 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
1627
Svetoslav7ec28e82015-05-20 17:01:10 -07001628 synchronized (mLock) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001629 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SYSTEM, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -08001630
Svetoslav7ec28e82015-05-20 17:01:10 -07001631 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001632
Svetoslav7ec28e82015-05-20 17:01:10 -07001633 String[] normalizedProjection = normalizeProjection(projection);
1634 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001635
Svetoslav7ec28e82015-05-20 17:01:10 -07001636 for (int i = 0; i < nameCount; i++) {
1637 String name = names.get(i);
Svetoslav683914b2015-01-15 14:22:26 -08001638
Svetoslav7ec28e82015-05-20 17:01:10 -07001639 // Determine the owning user as some profile settings are cloned from the parent.
1640 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId,
1641 name);
Svetoslav683914b2015-01-15 14:22:26 -08001642
Svetoslav7ec28e82015-05-20 17:01:10 -07001643 Setting setting = mSettingsRegistry.getSettingLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001644 SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001645 appendSettingToCursor(result, setting);
1646 }
1647
1648 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001649 }
Svetoslav683914b2015-01-15 14:22:26 -08001650 }
1651
Svetoslav7ec28e82015-05-20 17:01:10 -07001652 private Setting getSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001653 if (DEBUG) {
1654 Slog.v(LOG_TAG, "getSystemSetting(" + name + ", " + requestingUserId + ")");
1655 }
1656
1657 // Resolve the userId on whose behalf the call is made.
1658 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1659
Chad Brubakera6830e72017-04-28 17:34:36 -07001660 // Ensure the caller can access the setting.
1661 enforceSettingReadable(name, SETTINGS_TYPE_SYSTEM, UserHandle.getCallingUserId());
Chad Brubaker97bccee2017-01-05 15:51:41 -08001662
Svetoslav683914b2015-01-15 14:22:26 -08001663 // Determine the owning user as some profile settings are cloned from the parent.
1664 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1665
1666 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001667 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -07001668 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001669 }
Svetoslav683914b2015-01-15 14:22:26 -08001670 }
1671
Svetoslav7ec28e82015-05-20 17:01:10 -07001672 private boolean insertSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001673 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001674 Slog.v(LOG_TAG, "insertSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001675 + requestingUserId + ")");
1676 }
1677
Svetoslav7ec28e82015-05-20 17:01:10 -07001678 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT);
Svetoslav683914b2015-01-15 14:22:26 -08001679 }
1680
Svetoslav7ec28e82015-05-20 17:01:10 -07001681 private boolean deleteSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001682 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001683 Slog.v(LOG_TAG, "deleteSystemSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001684 }
1685
Svetoslav7ec28e82015-05-20 17:01:10 -07001686 return mutateSystemSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE);
Svetoslav683914b2015-01-15 14:22:26 -08001687 }
1688
Svetoslav7ec28e82015-05-20 17:01:10 -07001689 private boolean updateSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001690 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001691 Slog.v(LOG_TAG, "updateSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001692 + requestingUserId + ")");
1693 }
1694
Svetoslav7ec28e82015-05-20 17:01:10 -07001695 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE);
Svetoslav683914b2015-01-15 14:22:26 -08001696 }
1697
Svetoslav7ec28e82015-05-20 17:01:10 -07001698 private boolean mutateSystemSetting(String name, String value, int runAsUserId,
Svetoslav683914b2015-01-15 14:22:26 -08001699 int operation) {
Billy Lau6ad2d662015-07-18 00:26:58 +01001700 if (!hasWriteSecureSettingsPermission()) {
1701 // If the caller doesn't hold WRITE_SECURE_SETTINGS, we verify whether this
1702 // operation is allowed for the calling package through appops.
1703 if (!Settings.checkAndNoteWriteSettingsOperation(getContext(),
1704 Binder.getCallingUid(), getCallingPackage(), true)) {
1705 return false;
1706 }
Svetoslav683914b2015-01-15 14:22:26 -08001707 }
1708
Svetoslav683914b2015-01-15 14:22:26 -08001709 // Resolve the userId on whose behalf the call is made.
1710 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId);
1711
yuemingw1d13eae2018-01-30 17:27:54 +00001712 if (name != null && mUserManagerInternal.isSettingRestrictedForUser(
1713 name, callingUserId, value, Binder.getCallingUid())) {
1714 return false;
1715 }
1716
Svetoslavd8d25e02015-11-20 13:09:26 -08001717 // Enforce what the calling package can mutate the system settings.
1718 enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name, callingUserId);
1719
Svetoslav683914b2015-01-15 14:22:26 -08001720 // Determine the owning user as some profile settings are cloned from the parent.
1721 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1722
1723 // Only the owning user id can change the setting.
1724 if (owningUserId != callingUserId) {
1725 return false;
1726 }
1727
Jeff Sharkey413573a2016-02-22 17:52:45 -07001728 // Invalidate any relevant cache files
1729 String cacheName = null;
1730 if (Settings.System.RINGTONE.equals(name)) {
1731 cacheName = Settings.System.RINGTONE_CACHE;
1732 } else if (Settings.System.NOTIFICATION_SOUND.equals(name)) {
1733 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
1734 } else if (Settings.System.ALARM_ALERT.equals(name)) {
1735 cacheName = Settings.System.ALARM_ALERT_CACHE;
1736 }
1737 if (cacheName != null) {
1738 final File cacheFile = new File(
Andre Lago3fa139c2016-08-04 13:53:44 +01001739 getRingtoneCacheDir(owningUserId), cacheName);
Jeff Sharkey413573a2016-02-22 17:52:45 -07001740 cacheFile.delete();
1741 }
1742
Svetoslav683914b2015-01-15 14:22:26 -08001743 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001744 synchronized (mLock) {
1745 switch (operation) {
1746 case MUTATION_OPERATION_INSERT: {
1747 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001748 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001749 owningUserId, name, value, null, false, getCallingPackage(),
1750 false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001751 }
1752
1753 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001754 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001755 owningUserId, name, false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001756 }
1757
1758 case MUTATION_OPERATION_UPDATE: {
1759 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001760 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001761 owningUserId, name, value, null, false, getCallingPackage(),
1762 false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001763 }
Svetoslav683914b2015-01-15 14:22:26 -08001764 }
1765
Svetoslav7ec28e82015-05-20 17:01:10 -07001766 return false;
Svetoslav683914b2015-01-15 14:22:26 -08001767 }
Svetoslav683914b2015-01-15 14:22:26 -08001768 }
1769
Billy Lau6ad2d662015-07-18 00:26:58 +01001770 private boolean hasWriteSecureSettingsPermission() {
Svetoslavf41334b2015-06-23 12:06:03 -07001771 // Write secure settings is a more protected permission. If caller has it we are good.
1772 if (getContext().checkCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS)
1773 == PackageManager.PERMISSION_GRANTED) {
1774 return true;
1775 }
1776
Svetoslavf41334b2015-06-23 12:06:03 -07001777 return false;
1778 }
1779
Svetoslav683914b2015-01-15 14:22:26 -08001780 private void validateSystemSettingValue(String name, String value) {
Michal Karpinski2c37b082018-01-18 16:14:27 +00001781 SettingsValidators.Validator validator = Settings.System.VALIDATORS.get(name);
Svetoslav683914b2015-01-15 14:22:26 -08001782 if (validator != null && !validator.validate(value)) {
1783 throw new IllegalArgumentException("Invalid value: " + value
1784 + " for setting: " + name);
1785 }
1786 }
1787
Alex Klyubin1991f572017-03-03 14:08:36 -08001788 /**
1789 * Returns {@code true} if the specified secure setting should be accessible to the caller.
1790 */
1791 private boolean isSecureSettingAccessible(String name, int callingUserId,
1792 int owningUserId) {
1793 // Special case for location (sigh).
1794 // This check is not inside the name-based checks below because this method performs checks
1795 // only if the calling user ID is not the same as the owning user ID.
1796 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
1797 return false;
1798 }
1799
1800 switch (name) {
1801 case "bluetooth_address":
1802 // BluetoothManagerService for some reason stores the Android's Bluetooth MAC
1803 // address in this secure setting. Secure settings can normally be read by any app,
1804 // which thus enables them to bypass the recently introduced restrictions on access
1805 // to device identifiers.
1806 // To mitigate this we make this setting available only to callers privileged to see
1807 // this device's MAC addresses, same as through public API
1808 // BluetoothAdapter.getAddress() (see BluetoothManagerService for details).
1809 return getContext().checkCallingOrSelfPermission(
1810 Manifest.permission.LOCAL_MAC_ADDRESS) == PackageManager.PERMISSION_GRANTED;
1811 default:
1812 return true;
1813 }
1814 }
1815
Svetoslav683914b2015-01-15 14:22:26 -08001816 private boolean isLocationProvidersAllowedRestricted(String name, int callingUserId,
1817 int owningUserId) {
1818 // Optimization - location providers are restricted only for managed profiles.
1819 if (callingUserId == owningUserId) {
1820 return false;
1821 }
1822 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)
1823 && mUserManager.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION,
1824 new UserHandle(callingUserId))) {
1825 return true;
1826 }
1827 return false;
1828 }
1829
Svetoslav683914b2015-01-15 14:22:26 -08001830 private int resolveOwningUserIdForSecureSettingLocked(int userId, String setting) {
1831 return resolveOwningUserIdLocked(userId, sSecureCloneToManagedSettings, setting);
1832 }
1833
1834 private int resolveOwningUserIdForSystemSettingLocked(int userId, String setting) {
Andre Lago3fa139c2016-08-04 13:53:44 +01001835 final int parentId;
1836 // Resolves dependency if setting has a dependency and the calling user has a parent
1837 if (sSystemCloneFromParentOnDependency.containsKey(setting)
1838 && (parentId = getGroupParentLocked(userId)) != userId) {
1839 // The setting has a dependency and the profile has a parent
1840 String dependency = sSystemCloneFromParentOnDependency.get(setting);
Chad Brubaker97bccee2017-01-05 15:51:41 -08001841 // Lookup the dependency setting as ourselves, some callers may not have access to it.
1842 final long token = Binder.clearCallingIdentity();
1843 try {
1844 Setting settingObj = getSecureSetting(dependency, userId);
1845 if (settingObj != null && settingObj.getValue().equals("1")) {
1846 return parentId;
1847 }
1848 } finally {
1849 Binder.restoreCallingIdentity(token);
Andre Lago3fa139c2016-08-04 13:53:44 +01001850 }
1851 }
Svetoslav683914b2015-01-15 14:22:26 -08001852 return resolveOwningUserIdLocked(userId, sSystemCloneToManagedSettings, setting);
1853 }
1854
1855 private int resolveOwningUserIdLocked(int userId, Set<String> keys, String name) {
1856 final int parentId = getGroupParentLocked(userId);
1857 if (parentId != userId && keys.contains(name)) {
1858 return parentId;
1859 }
1860 return userId;
1861 }
1862
Svetoslavf41334b2015-06-23 12:06:03 -07001863 private void enforceRestrictedSystemSettingsMutationForCallingPackage(int operation,
Xiaohui Chen43765b72015-08-31 10:57:33 -07001864 String name, int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001865 // System/root/shell can mutate whatever secure settings they want.
1866 final int callingUid = Binder.getCallingUid();
Svetoslav Ganove080da92016-12-21 17:10:35 -08001867 final int appId = UserHandle.getAppId(callingUid);
1868 if (appId == android.os.Process.SYSTEM_UID
1869 || appId == Process.SHELL_UID
1870 || appId == Process.ROOT_UID) {
Svetoslav683914b2015-01-15 14:22:26 -08001871 return;
1872 }
1873
1874 switch (operation) {
1875 case MUTATION_OPERATION_INSERT:
1876 // Insert updates.
1877 case MUTATION_OPERATION_UPDATE: {
1878 if (Settings.System.PUBLIC_SETTINGS.contains(name)) {
1879 return;
1880 }
1881
1882 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001883 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001884
1885 // Privileged apps can do whatever they want.
1886 if ((packageInfo.applicationInfo.privateFlags
1887 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1888 return;
1889 }
1890
1891 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1892 packageInfo.applicationInfo.targetSdkVersion, name);
1893 } break;
1894
1895 case MUTATION_OPERATION_DELETE: {
1896 if (Settings.System.PUBLIC_SETTINGS.contains(name)
1897 || Settings.System.PRIVATE_SETTINGS.contains(name)) {
1898 throw new IllegalArgumentException("You cannot delete system defined"
1899 + " secure settings.");
1900 }
1901
1902 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001903 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001904
1905 // Privileged apps can do whatever they want.
1906 if ((packageInfo.applicationInfo.privateFlags &
1907 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1908 return;
1909 }
1910
1911 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1912 packageInfo.applicationInfo.targetSdkVersion, name);
1913 } break;
1914 }
1915 }
1916
Todd Kennedybe0b8892017-02-15 14:13:52 -08001917 private Set<String> getInstantAppAccessibleSettings(int settingsType) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001918 switch (settingsType) {
1919 case SETTINGS_TYPE_GLOBAL:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001920 return Settings.Global.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001921 case SETTINGS_TYPE_SECURE:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001922 return Settings.Secure.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001923 case SETTINGS_TYPE_SYSTEM:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001924 return Settings.System.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001925 default:
1926 throw new IllegalArgumentException("Invalid settings type: " + settingsType);
1927 }
1928 }
1929
Chad Brubaker20e0dc32017-04-28 18:24:55 -07001930 private Set<String> getOverlayInstantAppAccessibleSettings(int settingsType) {
1931 switch (settingsType) {
1932 case SETTINGS_TYPE_GLOBAL:
1933 return OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS;
1934 case SETTINGS_TYPE_SYSTEM:
1935 return OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS;
1936 case SETTINGS_TYPE_SECURE:
1937 return OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS;
1938 default:
1939 throw new IllegalArgumentException("Invalid settings type: " + settingsType);
1940 }
1941 }
1942
Andreas Gampeb58893072018-09-05 16:52:31 -07001943 @GuardedBy("mLock")
Chad Brubaker97bccee2017-01-05 15:51:41 -08001944 private List<String> getSettingsNamesLocked(int settingsType, int userId) {
Chad Brubakerb6108d62017-12-23 20:06:44 -08001945 // Don't enforce the instant app whitelist for now -- its too prone to unintended breakage
1946 // in the current form.
1947 return mSettingsRegistry.getSettingsNamesLocked(settingsType, userId);
Chad Brubaker97bccee2017-01-05 15:51:41 -08001948 }
1949
Chad Brubakera6830e72017-04-28 17:34:36 -07001950 private void enforceSettingReadable(String settingName, int settingsType, int userId) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001951 if (UserHandle.getAppId(Binder.getCallingUid()) < Process.FIRST_APPLICATION_UID) {
1952 return;
1953 }
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001954 ApplicationInfo ai = getCallingApplicationInfoOrThrow();
Svetoslav Ganov096d3042017-01-30 16:34:13 -08001955 if (!ai.isInstantApp()) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001956 return;
1957 }
Chad Brubaker20e0dc32017-04-28 18:24:55 -07001958 if (!getInstantAppAccessibleSettings(settingsType).contains(settingName)
1959 && !getOverlayInstantAppAccessibleSettings(settingsType).contains(settingName)) {
Chad Brubakerb6108d62017-12-23 20:06:44 -08001960 // Don't enforce the instant app whitelist for now -- its too prone to unintended
1961 // breakage in the current form.
1962 Slog.w(LOG_TAG, "Instant App " + ai.packageName
1963 + " trying to access unexposed setting, this will be an error in the future.");
Chad Brubaker97bccee2017-01-05 15:51:41 -08001964 }
1965 }
1966
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001967 private ApplicationInfo getCallingApplicationInfoOrThrow() {
1968 // We always use the callingUid for this lookup. This means that if hypothetically an
1969 // app was installed in user A with cross user and in user B as an Instant App
1970 // the app in A would be able to see all the settings in user B. However since cross
1971 // user is a system permission and the app must be uninstalled in B and then installed as
1972 // an Instant App that situation is not realistic or supported.
Chad Brubaker97bccee2017-01-05 15:51:41 -08001973 ApplicationInfo ai = null;
1974 try {
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001975 ai = mPackageManager.getApplicationInfo(getCallingPackage(), 0
1976 , UserHandle.getCallingUserId());
Chad Brubaker97bccee2017-01-05 15:51:41 -08001977 } catch (RemoteException ignored) {
1978 }
1979 if (ai == null) {
1980 throw new IllegalStateException("Failed to lookup info for package "
1981 + getCallingPackage());
1982 }
1983 return ai;
1984 }
1985
Xiaohui Chen43765b72015-08-31 10:57:33 -07001986 private PackageInfo getCallingPackageInfoOrThrow(int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001987 try {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001988 PackageInfo packageInfo = mPackageManager.getPackageInfo(
1989 getCallingPackage(), 0, userId);
1990 if (packageInfo != null) {
1991 return packageInfo;
1992 }
Xiaohui Chen43765b72015-08-31 10:57:33 -07001993 } catch (RemoteException e) {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001994 /* ignore */
Svetoslav683914b2015-01-15 14:22:26 -08001995 }
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001996 throw new IllegalStateException("Calling package doesn't exist");
Svetoslav683914b2015-01-15 14:22:26 -08001997 }
1998
1999 private int getGroupParentLocked(int userId) {
2000 // Most frequent use case.
Xiaohui Chen43765b72015-08-31 10:57:33 -07002001 if (userId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08002002 return userId;
2003 }
2004 // We are in the same process with the user manager and the returned
2005 // user info is a cached instance, so just look up instead of cache.
2006 final long identity = Binder.clearCallingIdentity();
2007 try {
Svetoslav7ec28e82015-05-20 17:01:10 -07002008 // Just a lookup and not reentrant, so holding a lock is fine.
Svetoslav683914b2015-01-15 14:22:26 -08002009 UserInfo userInfo = mUserManager.getProfileParent(userId);
2010 return (userInfo != null) ? userInfo.id : userId;
2011 } finally {
2012 Binder.restoreCallingIdentity(identity);
2013 }
2014 }
2015
Svetoslav683914b2015-01-15 14:22:26 -08002016 private void enforceWritePermission(String permission) {
2017 if (getContext().checkCallingOrSelfPermission(permission)
2018 != PackageManager.PERMISSION_GRANTED) {
2019 throw new SecurityException("Permission denial: writing to settings requires:"
2020 + permission);
2021 }
2022 }
2023
2024 /*
2025 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
2026 * This setting contains a list of the currently enabled location providers.
2027 * But helper functions in android.providers.Settings can enable or disable
2028 * a single provider by using a "+" or "-" prefix before the provider name.
2029 *
yuemingw1d13eae2018-01-30 17:27:54 +00002030 * <p>See also {@link com.android.server.pm.UserRestrictionsUtils#isSettingRestrictedForUser()}.
2031 * If DISALLOW_SHARE_LOCATION is set, the said method will only allow values with
2032 * the "-" prefix.
Makoto Onuki28da2e32015-11-20 11:30:44 -08002033 *
Svetoslav683914b2015-01-15 14:22:26 -08002034 * @returns whether the enabled location providers changed.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002035 */
Andreas Gampeb58893072018-09-05 16:52:31 -07002036 @GuardedBy("mLock")
Svetoslav Ganove080da92016-12-21 17:10:35 -08002037 private boolean updateLocationProvidersAllowedLocked(String value, String tag,
2038 int owningUserId, boolean makeDefault, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08002039 if (TextUtils.isEmpty(value)) {
2040 return false;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002041 }
Maggie83e03f52018-03-16 12:22:20 -07002042 Setting oldSetting = getSecureSetting(
2043 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, owningUserId);
2044 if (oldSetting == null) {
Svetoslav683914b2015-01-15 14:22:26 -08002045 return false;
2046 }
Maggie83e03f52018-03-16 12:22:20 -07002047 String oldProviders = oldSetting.getValue();
2048 List<String> oldProvidersList = TextUtils.isEmpty(oldProviders)
2049 ? new ArrayList<>() : new ArrayList<>(Arrays.asList(oldProviders.split(",")));
2050 Set<String> newProvidersSet = new ArraySet<>();
2051 newProvidersSet.addAll(oldProvidersList);
Svetoslav683914b2015-01-15 14:22:26 -08002052
Maggie83e03f52018-03-16 12:22:20 -07002053 String[] providerUpdates = value.split(",");
2054 boolean inputError = false;
2055 for (String provider : providerUpdates) {
2056 // do not update location_providers_allowed when input is invalid
2057 if (TextUtils.isEmpty(provider)) {
2058 inputError = true;
2059 break;
Svetoslav683914b2015-01-15 14:22:26 -08002060 }
Maggie83e03f52018-03-16 12:22:20 -07002061 final char prefix = provider.charAt(0);
2062 // do not update location_providers_allowed when input is invalid
2063 if (prefix != '+' && prefix != '-') {
2064 inputError = true;
2065 break;
Svetoslav683914b2015-01-15 14:22:26 -08002066 }
Maggie83e03f52018-03-16 12:22:20 -07002067 // skip prefix
2068 provider = provider.substring(1);
2069 if (prefix == '+') {
2070 newProvidersSet.add(provider);
2071 } else if (prefix == '-') {
2072 newProvidersSet.remove(provider);
Svetoslav683914b2015-01-15 14:22:26 -08002073 }
Maggie83e03f52018-03-16 12:22:20 -07002074 }
2075 String newProviders = TextUtils.join(",", newProvidersSet.toArray());
2076 if (inputError == true || newProviders.equals(oldProviders)) {
Svetoslav683914b2015-01-15 14:22:26 -08002077 // nothing changed, so no need to update the database
Svet Ganov53a441c2016-04-19 19:38:00 -07002078 if (forceNotify) {
Maggie83e03f52018-03-16 12:22:20 -07002079 mSettingsRegistry.notifyForSettingsChange(
2080 makeKey(SETTINGS_TYPE_SECURE, owningUserId),
Svet Ganov53a441c2016-04-19 19:38:00 -07002081 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
2082 }
Svetoslav683914b2015-01-15 14:22:26 -08002083 return false;
2084 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002085 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Maggie83e03f52018-03-16 12:22:20 -07002086 owningUserId, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, newProviders, tag,
2087 makeDefault, getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav683914b2015-01-15 14:22:26 -08002088 }
2089
Svetoslav683914b2015-01-15 14:22:26 -08002090 private static void warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
2091 int targetSdkVersion, String name) {
2092 // If the app targets Lollipop MR1 or older SDK we warn, otherwise crash.
2093 if (targetSdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) {
2094 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
2095 Slog.w(LOG_TAG, "You shouldn't not change private system settings."
2096 + " This will soon become an error.");
2097 } else {
2098 Slog.w(LOG_TAG, "You shouldn't keep your settings in the secure settings."
2099 + " This will soon become an error.");
2100 }
2101 } else {
2102 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
2103 throw new IllegalArgumentException("You cannot change private secure settings.");
2104 } else {
2105 throw new IllegalArgumentException("You cannot keep your settings in"
2106 + " the secure settings.");
2107 }
2108 }
2109 }
2110
2111 private static int resolveCallingUserIdEnforcingPermissionsLocked(int requestingUserId) {
2112 if (requestingUserId == UserHandle.getCallingUserId()) {
2113 return requestingUserId;
2114 }
2115 return ActivityManager.handleIncomingUser(Binder.getCallingPid(),
2116 Binder.getCallingUid(), requestingUserId, false, true,
2117 "get/set setting for user", null);
2118 }
2119
Svet Ganov53a441c2016-04-19 19:38:00 -07002120 private Bundle packageValueForCallResult(Setting setting,
2121 boolean trackingGeneration) {
2122 if (!trackingGeneration) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002123 if (setting == null || setting.isNull()) {
Svet Ganov53a441c2016-04-19 19:38:00 -07002124 return NULL_SETTING_BUNDLE;
2125 }
2126 return Bundle.forPair(Settings.NameValueTable.VALUE, setting.getValue());
Svetoslav683914b2015-01-15 14:22:26 -08002127 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002128 Bundle result = new Bundle();
2129 result.putString(Settings.NameValueTable.VALUE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002130 !setting.isNull() ? setting.getValue() : null);
Svet Ganov96c99462017-05-05 14:27:13 -07002131
Svetoslav Ganove080da92016-12-21 17:10:35 -08002132 mSettingsRegistry.mGenerationRegistry.addGenerationData(result, setting.getKey());
Svet Ganov53a441c2016-04-19 19:38:00 -07002133 return result;
Svetoslav683914b2015-01-15 14:22:26 -08002134 }
2135
2136 private static int getRequestingUserId(Bundle args) {
2137 final int callingUserId = UserHandle.getCallingUserId();
2138 return (args != null) ? args.getInt(Settings.CALL_METHOD_USER_KEY, callingUserId)
2139 : callingUserId;
2140 }
2141
Svet Ganov53a441c2016-04-19 19:38:00 -07002142 private boolean isTrackingGeneration(Bundle args) {
2143 return args != null && args.containsKey(Settings.CALL_METHOD_TRACK_GENERATION_KEY);
2144 }
2145
Svetoslav683914b2015-01-15 14:22:26 -08002146 private static String getSettingValue(Bundle args) {
2147 return (args != null) ? args.getString(Settings.NameValueTable.VALUE) : null;
2148 }
2149
Svetoslav Ganove080da92016-12-21 17:10:35 -08002150 private static String getSettingTag(Bundle args) {
2151 return (args != null) ? args.getString(Settings.CALL_METHOD_TAG_KEY) : null;
2152 }
2153
Matt Pape6bfc62e2018-11-28 13:16:03 -08002154 private static String getSettingPrefix(Bundle args) {
2155 String prefix = (args != null) ? args.getString(Settings.CALL_METHOD_PREFIX_KEY) : null;
2156 // Append '/' to ensure we only match properties with this exact prefix.
2157 // i.e. "foo" should match "foo/property" but not "foobar/property"
2158 return prefix != null ? prefix + "/" : null;
2159 }
2160
Svetoslav Ganove080da92016-12-21 17:10:35 -08002161 private static boolean getSettingMakeDefault(Bundle args) {
2162 return (args != null) && args.getBoolean(Settings.CALL_METHOD_MAKE_DEFAULT_KEY);
2163 }
2164
2165 private static int getResetModeEnforcingPermission(Bundle args) {
2166 final int mode = (args != null) ? args.getInt(Settings.CALL_METHOD_RESET_MODE_KEY) : 0;
2167 switch (mode) {
2168 case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: {
2169 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
2170 throw new SecurityException("Only system, shell/root on a "
2171 + "debuggable build can reset to untrusted defaults");
2172 }
2173 return mode;
2174 }
2175 case Settings.RESET_MODE_UNTRUSTED_CHANGES: {
2176 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
2177 throw new SecurityException("Only system, shell/root on a "
2178 + "debuggable build can reset untrusted changes");
2179 }
2180 return mode;
2181 }
2182 case Settings.RESET_MODE_TRUSTED_DEFAULTS: {
2183 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
2184 throw new SecurityException("Only system, shell/root on a "
2185 + "debuggable build can reset to trusted defaults");
2186 }
2187 return mode;
2188 }
2189 case Settings.RESET_MODE_PACKAGE_DEFAULTS: {
2190 return mode;
2191 }
2192 }
2193 throw new IllegalArgumentException("Invalid reset mode: " + mode);
2194 }
2195
2196 private static boolean isCallerSystemOrShellOrRootOnDebuggableBuild() {
2197 final int appId = UserHandle.getAppId(Binder.getCallingUid());
2198 return appId == SYSTEM_UID || (Build.IS_DEBUGGABLE
2199 && (appId == SHELL_UID || appId == ROOT_UID));
2200 }
2201
Svetoslav683914b2015-01-15 14:22:26 -08002202 private static String getValidTableOrThrow(Uri uri) {
2203 if (uri.getPathSegments().size() > 0) {
2204 String table = uri.getPathSegments().get(0);
2205 if (DatabaseHelper.isValidTable(table)) {
2206 return table;
2207 }
2208 throw new IllegalArgumentException("Bad root path: " + table);
2209 }
2210 throw new IllegalArgumentException("Invalid URI:" + uri);
2211 }
2212
2213 private static MatrixCursor packageSettingForQuery(Setting setting, String[] projection) {
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002214 if (setting.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08002215 return new MatrixCursor(projection, 0);
2216 }
2217 MatrixCursor cursor = new MatrixCursor(projection, 1);
2218 appendSettingToCursor(cursor, setting);
2219 return cursor;
2220 }
2221
2222 private static String[] normalizeProjection(String[] projection) {
2223 if (projection == null) {
2224 return ALL_COLUMNS;
2225 }
2226
2227 final int columnCount = projection.length;
2228 for (int i = 0; i < columnCount; i++) {
2229 String column = projection[i];
2230 if (!ArrayUtils.contains(ALL_COLUMNS, column)) {
2231 throw new IllegalArgumentException("Invalid column: " + column);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002232 }
2233 }
2234
Svetoslav683914b2015-01-15 14:22:26 -08002235 return projection;
2236 }
2237
2238 private static void appendSettingToCursor(MatrixCursor cursor, Setting setting) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002239 if (setting == null || setting.isNull()) {
Suprabh Shuklac9d064a2016-04-12 18:45:34 -07002240 return;
2241 }
Svetoslav683914b2015-01-15 14:22:26 -08002242 final int columnCount = cursor.getColumnCount();
2243
2244 String[] values = new String[columnCount];
2245
2246 for (int i = 0; i < columnCount; i++) {
2247 String column = cursor.getColumnName(i);
2248
2249 switch (column) {
2250 case Settings.NameValueTable._ID: {
2251 values[i] = setting.getId();
2252 } break;
2253
2254 case Settings.NameValueTable.NAME: {
2255 values[i] = setting.getName();
2256 } break;
2257
2258 case Settings.NameValueTable.VALUE: {
2259 values[i] = setting.getValue();
2260 } break;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002261 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002262 }
2263
Svetoslav683914b2015-01-15 14:22:26 -08002264 cursor.addRow(values);
2265 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002266
Makoto Onuki3a2c35782015-06-18 11:21:58 -07002267 private static boolean isKeyValid(String key) {
2268 return !(TextUtils.isEmpty(key) || SettingsState.isBinary(key));
2269 }
2270
Matt Pape7b1c6cd2019-01-04 08:10:41 -08002271 private String resolveCallingPackage() {
2272 switch (Binder.getCallingUid()) {
2273 case Process.ROOT_UID: {
2274 return "root";
2275 }
2276
2277 case Process.SHELL_UID: {
2278 return "com.android.shell";
2279 }
2280
2281 default: {
2282 return getCallingPackage();
2283 }
2284 }
2285 }
2286
Svetoslav683914b2015-01-15 14:22:26 -08002287 private static final class Arguments {
2288 private static final Pattern WHERE_PATTERN_WITH_PARAM_NO_BRACKETS =
2289 Pattern.compile("[\\s]*name[\\s]*=[\\s]*\\?[\\s]*");
2290
2291 private static final Pattern WHERE_PATTERN_WITH_PARAM_IN_BRACKETS =
2292 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*\\?[\\s]*\\)[\\s]*");
2293
2294 private static final Pattern WHERE_PATTERN_NO_PARAM_IN_BRACKETS =
2295 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*\\)[\\s]*");
2296
2297 private static final Pattern WHERE_PATTERN_NO_PARAM_NO_BRACKETS =
2298 Pattern.compile("[\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*");
2299
2300 public final String table;
2301 public final String name;
2302
2303 public Arguments(Uri uri, String where, String[] whereArgs, boolean supportAll) {
2304 final int segmentSize = uri.getPathSegments().size();
2305 switch (segmentSize) {
2306 case 1: {
2307 if (where != null
2308 && (WHERE_PATTERN_WITH_PARAM_NO_BRACKETS.matcher(where).matches()
2309 || WHERE_PATTERN_WITH_PARAM_IN_BRACKETS.matcher(where).matches())
2310 && whereArgs.length == 1) {
2311 name = whereArgs[0];
2312 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08002313 return;
Svetoslav683914b2015-01-15 14:22:26 -08002314 } else if (where != null
2315 && (WHERE_PATTERN_NO_PARAM_NO_BRACKETS.matcher(where).matches()
2316 || WHERE_PATTERN_NO_PARAM_IN_BRACKETS.matcher(where).matches())) {
2317 final int startIndex = Math.max(where.indexOf("'"),
2318 where.indexOf("\"")) + 1;
2319 final int endIndex = Math.max(where.lastIndexOf("'"),
2320 where.lastIndexOf("\""));
2321 name = where.substring(startIndex, endIndex);
2322 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08002323 return;
Svetoslav683914b2015-01-15 14:22:26 -08002324 } else if (supportAll && where == null && whereArgs == null) {
2325 name = null;
2326 table = computeTableForSetting(uri, null);
Svetoslav28494652015-02-12 14:11:42 -08002327 return;
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002328 }
Svetoslav683914b2015-01-15 14:22:26 -08002329 } break;
2330
Svetoslav28494652015-02-12 14:11:42 -08002331 case 2: {
2332 if (where == null && whereArgs == null) {
2333 name = uri.getPathSegments().get(1);
2334 table = computeTableForSetting(uri, name);
2335 return;
2336 }
2337 } break;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002338 }
Svetoslav28494652015-02-12 14:11:42 -08002339
2340 EventLogTags.writeUnsupportedSettingsQuery(
2341 uri.toSafeString(), where, Arrays.toString(whereArgs));
2342 String message = String.format( "Supported SQL:\n"
2343 + " uri content://some_table/some_property with null where and where args\n"
2344 + " uri content://some_table with query name=? and single name as arg\n"
2345 + " uri content://some_table with query name=some_name and null args\n"
2346 + " but got - uri:%1s, where:%2s whereArgs:%3s", uri, where,
2347 Arrays.toString(whereArgs));
2348 throw new IllegalArgumentException(message);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002349 }
2350
Svetoslav28494652015-02-12 14:11:42 -08002351 private static String computeTableForSetting(Uri uri, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08002352 String table = getValidTableOrThrow(uri);
2353
2354 if (name != null) {
2355 if (sSystemMovedToSecureSettings.contains(name)) {
2356 table = TABLE_SECURE;
2357 }
2358
2359 if (sSystemMovedToGlobalSettings.contains(name)) {
2360 table = TABLE_GLOBAL;
2361 }
2362
2363 if (sSecureMovedToGlobalSettings.contains(name)) {
2364 table = TABLE_GLOBAL;
2365 }
2366
2367 if (sGlobalMovedToSecureSettings.contains(name)) {
2368 table = TABLE_SECURE;
2369 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002370 }
Svetoslav683914b2015-01-15 14:22:26 -08002371
2372 return table;
2373 }
2374 }
2375
2376 final class SettingsRegistry {
2377 private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid";
2378
Svetoslav683914b2015-01-15 14:22:26 -08002379 private static final String SETTINGS_FILE_GLOBAL = "settings_global.xml";
2380 private static final String SETTINGS_FILE_SYSTEM = "settings_system.xml";
2381 private static final String SETTINGS_FILE_SECURE = "settings_secure.xml";
Mark Rathjend891f012017-01-19 04:10:37 +00002382 private static final String SETTINGS_FILE_SSAID = "settings_ssaid.xml";
Matt Pape1b31a332018-10-17 09:58:28 -07002383 private static final String SETTINGS_FILE_CONFIG = "settings_config.xml";
Mark Rathjend891f012017-01-19 04:10:37 +00002384
2385 private static final String SSAID_USER_KEY = "userkey";
Svetoslav683914b2015-01-15 14:22:26 -08002386
2387 private final SparseArray<SettingsState> mSettingsStates = new SparseArray<>();
2388
Svet Ganov53a441c2016-04-19 19:38:00 -07002389 private GenerationRegistry mGenerationRegistry;
Svetoslav683914b2015-01-15 14:22:26 -08002390
Svetoslav7e0683b2015-08-03 16:02:52 -07002391 private final Handler mHandler;
2392
Svet Ganov53a441c2016-04-19 19:38:00 -07002393 private final BackupManager mBackupManager;
2394
Amith Yamasani39452022017-03-21 15:23:47 -07002395 private String mSettingsCreationBuildId;
2396
Svetoslav683914b2015-01-15 14:22:26 -08002397 public SettingsRegistry() {
Svetoslav7e0683b2015-08-03 16:02:52 -07002398 mHandler = new MyHandler(getContext().getMainLooper());
Svet Ganov53a441c2016-04-19 19:38:00 -07002399 mGenerationRegistry = new GenerationRegistry(mLock);
2400 mBackupManager = new BackupManager(getContext());
Svetoslav683914b2015-01-15 14:22:26 -08002401 migrateAllLegacySettingsIfNeeded();
Mark Rathjend891f012017-01-19 04:10:37 +00002402 syncSsaidTableOnStart();
2403 }
2404
2405 private void generateUserKeyLocked(int userId) {
2406 // Generate a random key for each user used for creating a new ssaid.
Mark Rathjen7599f132017-01-23 14:15:54 -08002407 final byte[] keyBytes = new byte[32];
Mark Rathjend891f012017-01-19 04:10:37 +00002408 final SecureRandom rand = new SecureRandom();
2409 rand.nextBytes(keyBytes);
2410
2411 // Convert to string for storage in settings table.
Mark Rathjen7599f132017-01-23 14:15:54 -08002412 final String userKey = ByteStringUtils.toHexString(keyBytes);
Mark Rathjend891f012017-01-19 04:10:37 +00002413
2414 // Store the key in the ssaid table.
2415 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
2416 final boolean success = ssaidSettings.insertSettingLocked(SSAID_USER_KEY, userKey, null,
2417 true, SettingsState.SYSTEM_PACKAGE_NAME);
2418
2419 if (!success) {
2420 throw new IllegalStateException("Ssaid settings not accessible");
2421 }
2422 }
2423
Mark Rathjen7599f132017-01-23 14:15:54 -08002424 private byte[] getLengthPrefix(byte[] data) {
2425 return ByteBuffer.allocate(4).putInt(data.length).array();
2426 }
2427
Christopher Tateb218e762017-04-05 16:34:07 -07002428 public Setting generateSsaidLocked(PackageInfo callingPkg, int userId) {
Mark Rathjend891f012017-01-19 04:10:37 +00002429 // Read the user's key from the ssaid table.
2430 Setting userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY);
Mark Rathjenea617592017-01-18 23:03:41 -08002431 if (userKeySetting == null || userKeySetting.isNull()
2432 || userKeySetting.getValue() == null) {
Mark Rathjend891f012017-01-19 04:10:37 +00002433 // Lazy initialize and store the user key.
2434 generateUserKeyLocked(userId);
2435 userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY);
Mark Rathjenea617592017-01-18 23:03:41 -08002436 if (userKeySetting == null || userKeySetting.isNull()
2437 || userKeySetting.getValue() == null) {
Mark Rathjend891f012017-01-19 04:10:37 +00002438 throw new IllegalStateException("User key not accessible");
2439 }
2440 }
2441 final String userKey = userKeySetting.getValue();
2442
2443 // Convert the user's key back to a byte array.
Mark Rathjen7599f132017-01-23 14:15:54 -08002444 final byte[] keyBytes = ByteStringUtils.fromHexToByteArray(userKey);
2445
2446 // Validate that the key is of expected length.
2447 // Keys are currently 32 bytes, but were once 16 bytes during Android O development.
2448 if (keyBytes == null || (keyBytes.length != 16 && keyBytes.length != 32)) {
Mark Rathjend891f012017-01-19 04:10:37 +00002449 throw new IllegalStateException("User key invalid");
2450 }
2451
Mark Rathjen7599f132017-01-23 14:15:54 -08002452 final Mac m;
Mark Rathjend891f012017-01-19 04:10:37 +00002453 try {
Mark Rathjen7599f132017-01-23 14:15:54 -08002454 m = Mac.getInstance("HmacSHA256");
2455 m.init(new SecretKeySpec(keyBytes, m.getAlgorithm()));
Mark Rathjend891f012017-01-19 04:10:37 +00002456 } catch (NoSuchAlgorithmException e) {
Mark Rathjen7599f132017-01-23 14:15:54 -08002457 throw new IllegalStateException("HmacSHA256 is not available", e);
2458 } catch (InvalidKeyException e) {
2459 throw new IllegalStateException("Key is corrupted", e);
Mark Rathjend891f012017-01-19 04:10:37 +00002460 }
Mark Rathjen7599f132017-01-23 14:15:54 -08002461
Mark Rathjenf42dd912017-06-05 19:04:34 -07002462 // Mac each of the developer signatures.
Christopher Tateb218e762017-04-05 16:34:07 -07002463 for (int i = 0; i < callingPkg.signatures.length; i++) {
2464 byte[] sig = callingPkg.signatures[i].toByteArray();
Mark Rathjen7599f132017-01-23 14:15:54 -08002465 m.update(getLengthPrefix(sig), 0, 4);
2466 m.update(sig);
2467 }
Mark Rathjend891f012017-01-19 04:10:37 +00002468
2469 // Convert result to a string for storage in settings table. Only want first 64 bits.
Mark Rathjen7599f132017-01-23 14:15:54 -08002470 final String ssaid = ByteStringUtils.toHexString(m.doFinal()).substring(0, 16)
2471 .toLowerCase(Locale.US);
Mark Rathjend891f012017-01-19 04:10:37 +00002472
2473 // Save the ssaid in the ssaid table.
Christopher Tateb218e762017-04-05 16:34:07 -07002474 final String uid = Integer.toString(callingPkg.applicationInfo.uid);
Mark Rathjend891f012017-01-19 04:10:37 +00002475 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
2476 final boolean success = ssaidSettings.insertSettingLocked(uid, ssaid, null, true,
Mark Rathjenf42dd912017-06-05 19:04:34 -07002477 callingPkg.packageName);
Mark Rathjend891f012017-01-19 04:10:37 +00002478
2479 if (!success) {
2480 throw new IllegalStateException("Ssaid settings not accessible");
2481 }
2482
2483 return getSettingLocked(SETTINGS_TYPE_SSAID, userId, uid);
2484 }
2485
2486 public void syncSsaidTableOnStart() {
2487 synchronized (mLock) {
2488 // Verify that each user's packages and ssaid's are in sync.
2489 for (UserInfo user : mUserManager.getUsers(true)) {
2490 // Get all uids for the user's packages.
2491 final List<PackageInfo> packages;
2492 try {
Tetsutoki Shiozawaebe0e5f2018-01-17 11:07:09 +09002493 packages = mPackageManager.getInstalledPackages(
2494 PackageManager.MATCH_UNINSTALLED_PACKAGES,
2495 user.id).getList();
Mark Rathjend891f012017-01-19 04:10:37 +00002496 } catch (RemoteException e) {
2497 throw new IllegalStateException("Package manager not available");
2498 }
2499 final Set<String> appUids = new HashSet<>();
2500 for (PackageInfo info : packages) {
2501 appUids.add(Integer.toString(info.applicationInfo.uid));
2502 }
2503
2504 // Get all uids currently stored in the user's ssaid table.
2505 final Set<String> ssaidUids = new HashSet<>(
2506 getSettingsNamesLocked(SETTINGS_TYPE_SSAID, user.id));
2507 ssaidUids.remove(SSAID_USER_KEY);
2508
2509 // Perform a set difference for the appUids and ssaidUids.
2510 ssaidUids.removeAll(appUids);
2511
2512 // If there are ssaidUids left over they need to be removed from the table.
2513 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID,
2514 user.id);
2515 for (String uid : ssaidUids) {
2516 ssaidSettings.deleteSettingLocked(uid);
2517 }
2518 }
2519 }
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002520 }
2521
Svetoslav683914b2015-01-15 14:22:26 -08002522 public List<String> getSettingsNamesLocked(int type, int userId) {
2523 final int key = makeKey(type, userId);
2524 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002525 if (settingsState == null) {
2526 return new ArrayList<String>();
2527 }
Svetoslav683914b2015-01-15 14:22:26 -08002528 return settingsState.getSettingNamesLocked();
2529 }
2530
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002531 public SparseBooleanArray getKnownUsersLocked() {
2532 SparseBooleanArray users = new SparseBooleanArray();
2533 for (int i = mSettingsStates.size()-1; i >= 0; i--) {
2534 users.put(getUserIdFromKey(mSettingsStates.keyAt(i)), true);
2535 }
2536 return users;
2537 }
2538
Kweku Adamsb0886f32017-10-31 15:32:09 -07002539 @Nullable
Svetoslav683914b2015-01-15 14:22:26 -08002540 public SettingsState getSettingsLocked(int type, int userId) {
2541 final int key = makeKey(type, userId);
2542 return peekSettingsStateLocked(key);
2543 }
2544
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002545 public boolean ensureSettingsForUserLocked(int userId) {
2546 // First make sure this user actually exists.
2547 if (mUserManager.getUserInfo(userId) == null) {
2548 Slog.wtf(LOG_TAG, "Requested user " + userId + " does not exist");
2549 return false;
2550 }
2551
Svetoslav683914b2015-01-15 14:22:26 -08002552 // Migrate the setting for this user if needed.
2553 migrateLegacySettingsForUserIfNeededLocked(userId);
2554
Matt Pape1b31a332018-10-17 09:58:28 -07002555 // Ensure config settings loaded if owner.
2556 if (userId == UserHandle.USER_SYSTEM) {
2557 final int configKey
2558 = makeKey(SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM);
2559 ensureSettingsStateLocked(configKey);
2560 }
2561
Svetoslav683914b2015-01-15 14:22:26 -08002562 // Ensure global settings loaded if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07002563 if (userId == UserHandle.USER_SYSTEM) {
2564 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002565 ensureSettingsStateLocked(globalKey);
2566 }
2567
2568 // Ensure secure settings loaded.
2569 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2570 ensureSettingsStateLocked(secureKey);
2571
2572 // Make sure the secure settings have an Android id set.
2573 SettingsState secureSettings = getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
2574 ensureSecureSettingAndroidIdSetLocked(secureSettings);
2575
2576 // Ensure system settings loaded.
2577 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2578 ensureSettingsStateLocked(systemKey);
2579
Mark Rathjend891f012017-01-19 04:10:37 +00002580 // Ensure secure settings loaded.
2581 final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId);
2582 ensureSettingsStateLocked(ssaidKey);
2583
Svetoslav683914b2015-01-15 14:22:26 -08002584 // Upgrade the settings to the latest version.
2585 UpgradeController upgrader = new UpgradeController(userId);
2586 upgrader.upgradeIfNeededLocked();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002587 return true;
Svetoslav683914b2015-01-15 14:22:26 -08002588 }
2589
2590 private void ensureSettingsStateLocked(int key) {
2591 if (mSettingsStates.get(key) == null) {
2592 final int maxBytesPerPackage = getMaxBytesPerPackageForType(getTypeFromKey(key));
Svetoslav Ganove080da92016-12-21 17:10:35 -08002593 SettingsState settingsState = new SettingsState(getContext(), mLock,
2594 getSettingsFile(key), key, maxBytesPerPackage, mHandlerThread.getLooper());
Svetoslav683914b2015-01-15 14:22:26 -08002595 mSettingsStates.put(key, settingsState);
2596 }
2597 }
2598
2599 public void removeUserStateLocked(int userId, boolean permanently) {
2600 // We always keep the global settings in memory.
2601
2602 // Nuke system settings.
2603 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2604 final SettingsState systemSettingsState = mSettingsStates.get(systemKey);
2605 if (systemSettingsState != null) {
2606 if (permanently) {
2607 mSettingsStates.remove(systemKey);
2608 systemSettingsState.destroyLocked(null);
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002609 } else {
Svetoslav683914b2015-01-15 14:22:26 -08002610 systemSettingsState.destroyLocked(new Runnable() {
2611 @Override
2612 public void run() {
2613 mSettingsStates.remove(systemKey);
2614 }
2615 });
2616 }
2617 }
2618
2619 // Nuke secure settings.
2620 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2621 final SettingsState secureSettingsState = mSettingsStates.get(secureKey);
2622 if (secureSettingsState != null) {
2623 if (permanently) {
2624 mSettingsStates.remove(secureKey);
2625 secureSettingsState.destroyLocked(null);
2626 } else {
2627 secureSettingsState.destroyLocked(new Runnable() {
2628 @Override
2629 public void run() {
2630 mSettingsStates.remove(secureKey);
2631 }
2632 });
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002633 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002634 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002635
Mark Rathjend891f012017-01-19 04:10:37 +00002636 // Nuke ssaid settings.
2637 final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId);
2638 final SettingsState ssaidSettingsState = mSettingsStates.get(ssaidKey);
2639 if (ssaidSettingsState != null) {
2640 if (permanently) {
2641 mSettingsStates.remove(ssaidKey);
2642 ssaidSettingsState.destroyLocked(null);
2643 } else {
2644 ssaidSettingsState.destroyLocked(new Runnable() {
2645 @Override
2646 public void run() {
2647 mSettingsStates.remove(ssaidKey);
2648 }
2649 });
2650 }
2651 }
2652
Svet Ganov53a441c2016-04-19 19:38:00 -07002653 // Nuke generation tracking data
2654 mGenerationRegistry.onUserRemoved(userId);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002655 }
2656
Svetoslav683914b2015-01-15 14:22:26 -08002657 public boolean insertSettingLocked(int type, int userId, String name, String value,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002658 String tag, boolean makeDefault, String packageName, boolean forceNotify,
2659 Set<String> criticalSettings) {
Matt Papec1323dc2018-12-11 12:32:42 -08002660 return insertSettingLocked(type, userId, name, value, tag, makeDefault, false,
2661 packageName, forceNotify, criticalSettings);
2662 }
2663
2664 public boolean insertSettingLocked(int type, int userId, String name, String value,
2665 String tag, boolean makeDefault, boolean forceNonSystemPackage, String packageName,
2666 boolean forceNotify, Set<String> criticalSettings) {
Svetoslav683914b2015-01-15 14:22:26 -08002667 final int key = makeKey(type, userId);
2668
Svetoslav Ganove080da92016-12-21 17:10:35 -08002669 boolean success = false;
Svetoslav683914b2015-01-15 14:22:26 -08002670 SettingsState settingsState = peekSettingsStateLocked(key);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002671 if (settingsState != null) {
2672 success = settingsState.insertSettingLocked(name, value,
Matt Papec1323dc2018-12-11 12:32:42 -08002673 tag, makeDefault, forceNonSystemPackage, packageName);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002674 }
Svetoslav683914b2015-01-15 14:22:26 -08002675
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002676 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2677 settingsState.persistSyncLocked();
2678 }
2679
Svet Ganov53a441c2016-04-19 19:38:00 -07002680 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08002681 notifyForSettingsChange(key, name);
2682 }
2683 return success;
2684 }
2685
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002686 public boolean deleteSettingLocked(int type, int userId, String name, boolean forceNotify,
2687 Set<String> criticalSettings) {
Svetoslav683914b2015-01-15 14:22:26 -08002688 final int key = makeKey(type, userId);
2689
Svetoslav Ganove080da92016-12-21 17:10:35 -08002690 boolean success = false;
Svetoslav683914b2015-01-15 14:22:26 -08002691 SettingsState settingsState = peekSettingsStateLocked(key);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002692 if (settingsState != null) {
2693 success = settingsState.deleteSettingLocked(name);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002694 }
Svetoslav683914b2015-01-15 14:22:26 -08002695
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002696 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2697 settingsState.persistSyncLocked();
2698 }
2699
Svet Ganov53a441c2016-04-19 19:38:00 -07002700 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08002701 notifyForSettingsChange(key, name);
2702 }
2703 return success;
2704 }
2705
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002706 public boolean updateSettingLocked(int type, int userId, String name, String value,
2707 String tag, boolean makeDefault, String packageName, boolean forceNotify,
2708 Set<String> criticalSettings) {
2709 final int key = makeKey(type, userId);
2710
2711 boolean success = false;
2712 SettingsState settingsState = peekSettingsStateLocked(key);
2713 if (settingsState != null) {
2714 success = settingsState.updateSettingLocked(name, value, tag,
2715 makeDefault, packageName);
2716 }
2717
2718 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2719 settingsState.persistSyncLocked();
2720 }
2721
2722 if (forceNotify || success) {
2723 notifyForSettingsChange(key, name);
2724 }
2725
2726 return success;
2727 }
2728
Svetoslav683914b2015-01-15 14:22:26 -08002729 public Setting getSettingLocked(int type, int userId, String name) {
2730 final int key = makeKey(type, userId);
2731
2732 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002733 if (settingsState == null) {
Mark Rathjenea617592017-01-18 23:03:41 -08002734 return null;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002735 }
Mark Rathjend891f012017-01-19 04:10:37 +00002736
2737 // getSettingLocked will return non-null result
Svetoslav683914b2015-01-15 14:22:26 -08002738 return settingsState.getSettingLocked(name);
2739 }
2740
Svetoslav Ganove080da92016-12-21 17:10:35 -08002741 public void resetSettingsLocked(int type, int userId, String packageName, int mode,
2742 String tag) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002743 resetSettingsLocked(type, userId, packageName, mode, tag, null);
2744 }
2745
2746 public void resetSettingsLocked(int type, int userId, String packageName, int mode,
2747 String tag, @Nullable String prefix) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08002748 final int key = makeKey(type, userId);
2749 SettingsState settingsState = peekSettingsStateLocked(key);
2750 if (settingsState == null) {
2751 return;
2752 }
2753
2754 switch (mode) {
2755 case Settings.RESET_MODE_PACKAGE_DEFAULTS: {
2756 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002757 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002758 Setting setting = settingsState.getSettingLocked(name);
2759 if (packageName.equals(setting.getPackageName())) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002760 if ((tag != null && !tag.equals(setting.getTag()))
2761 || (prefix != null && !setting.getName().startsWith(prefix))) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08002762 continue;
2763 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002764 if (settingsState.resetSettingLocked(name)) {
2765 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002766 notifyForSettingsChange(key, name);
2767 }
2768 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002769 if (someSettingChanged) {
2770 settingsState.persistSyncLocked();
2771 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002772 }
2773 } break;
2774
2775 case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: {
2776 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002777 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002778 Setting setting = settingsState.getSettingLocked(name);
2779 if (!SettingsState.isSystemPackage(getContext(),
2780 setting.getPackageName())) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002781 if (prefix != null && !setting.getName().startsWith(prefix)) {
2782 continue;
2783 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002784 if (settingsState.resetSettingLocked(name)) {
2785 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002786 notifyForSettingsChange(key, name);
2787 }
2788 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002789 if (someSettingChanged) {
2790 settingsState.persistSyncLocked();
2791 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002792 }
2793 } break;
2794
2795 case Settings.RESET_MODE_UNTRUSTED_CHANGES: {
2796 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002797 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002798 Setting setting = settingsState.getSettingLocked(name);
2799 if (!SettingsState.isSystemPackage(getContext(),
2800 setting.getPackageName())) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002801 if (prefix != null && !setting.getName().startsWith(prefix)) {
2802 continue;
2803 }
Eugene Suslad72c3972016-12-27 15:49:30 -08002804 if (setting.isDefaultFromSystem()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002805 if (settingsState.resetSettingLocked(name)) {
2806 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002807 notifyForSettingsChange(key, name);
2808 }
2809 } else if (settingsState.deleteSettingLocked(name)) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002810 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002811 notifyForSettingsChange(key, name);
2812 }
2813 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002814 if (someSettingChanged) {
2815 settingsState.persistSyncLocked();
2816 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002817 }
2818 } break;
2819
2820 case Settings.RESET_MODE_TRUSTED_DEFAULTS: {
2821 for (String name : settingsState.getSettingNamesLocked()) {
2822 Setting setting = settingsState.getSettingLocked(name);
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002823 boolean someSettingChanged = false;
Matt Pape6bfc62e2018-11-28 13:16:03 -08002824 if (prefix != null && !setting.getName().startsWith(prefix)) {
2825 continue;
2826 }
Eugene Suslad72c3972016-12-27 15:49:30 -08002827 if (setting.isDefaultFromSystem()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002828 if (settingsState.resetSettingLocked(name)) {
2829 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002830 notifyForSettingsChange(key, name);
2831 }
2832 } else if (settingsState.deleteSettingLocked(name)) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002833 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002834 notifyForSettingsChange(key, name);
2835 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002836 if (someSettingChanged) {
2837 settingsState.persistSyncLocked();
2838 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002839 }
2840 } break;
2841 }
2842 }
2843
Zimuzoc56192c2018-07-25 10:40:01 +01002844 public void removeSettingsForPackageLocked(String packageName, int userId) {
Svet Ganov8de34802015-04-27 09:33:40 -07002845 // Global and secure settings are signature protected. Apps signed
2846 // by the platform certificate are generally not uninstalled and
2847 // the main exception is tests. We trust components signed
2848 // by the platform certificate and do not do a clean up after them.
Svetoslav683914b2015-01-15 14:22:26 -08002849
2850 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2851 SettingsState systemSettings = mSettingsStates.get(systemKey);
Svet Ganov8de34802015-04-27 09:33:40 -07002852 if (systemSettings != null) {
Zimuzoc56192c2018-07-25 10:40:01 +01002853 systemSettings.removeSettingsForPackageLocked(packageName);
Svet Ganov8de34802015-04-27 09:33:40 -07002854 }
Svetoslav683914b2015-01-15 14:22:26 -08002855 }
2856
Mark Rathjend891f012017-01-19 04:10:37 +00002857 public void onUidRemovedLocked(int uid) {
2858 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID,
2859 UserHandle.getUserId(uid));
Tony Mak24c6ab42018-01-08 14:35:34 +00002860 if (ssaidSettings != null) {
2861 ssaidSettings.deleteSettingLocked(Integer.toString(uid));
2862 }
Mark Rathjend891f012017-01-19 04:10:37 +00002863 }
2864
Kweku Adamsb0886f32017-10-31 15:32:09 -07002865 @Nullable
Svetoslav683914b2015-01-15 14:22:26 -08002866 private SettingsState peekSettingsStateLocked(int key) {
2867 SettingsState settingsState = mSettingsStates.get(key);
2868 if (settingsState != null) {
2869 return settingsState;
2870 }
2871
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002872 if (!ensureSettingsForUserLocked(getUserIdFromKey(key))) {
2873 return null;
2874 }
Svetoslav683914b2015-01-15 14:22:26 -08002875 return mSettingsStates.get(key);
2876 }
2877
2878 private void migrateAllLegacySettingsIfNeeded() {
2879 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07002880 final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002881 File globalFile = getSettingsFile(key);
Amith Yamasani74bf71b2017-07-19 16:49:52 -07002882 if (SettingsState.stateFileExists(globalFile)) {
Svetoslav683914b2015-01-15 14:22:26 -08002883 return;
2884 }
2885
Amith Yamasani39452022017-03-21 15:23:47 -07002886 mSettingsCreationBuildId = Build.ID;
2887
Svetoslav683914b2015-01-15 14:22:26 -08002888 final long identity = Binder.clearCallingIdentity();
2889 try {
2890 List<UserInfo> users = mUserManager.getUsers(true);
2891
2892 final int userCount = users.size();
2893 for (int i = 0; i < userCount; i++) {
2894 final int userId = users.get(i).id;
2895
2896 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
2897 SQLiteDatabase database = dbHelper.getWritableDatabase();
2898 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
2899
2900 // Upgrade to the latest version.
2901 UpgradeController upgrader = new UpgradeController(userId);
2902 upgrader.upgradeIfNeededLocked();
2903
2904 // Drop from memory if not a running user.
2905 if (!mUserManager.isUserRunning(new UserHandle(userId))) {
2906 removeUserStateLocked(userId, false);
2907 }
2908 }
2909 } finally {
2910 Binder.restoreCallingIdentity(identity);
2911 }
2912 }
2913 }
2914
2915 private void migrateLegacySettingsForUserIfNeededLocked(int userId) {
2916 // Every user has secure settings and if no file we need to migrate.
2917 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2918 File secureFile = getSettingsFile(secureKey);
Amith Yamasani74bf71b2017-07-19 16:49:52 -07002919 if (SettingsState.stateFileExists(secureFile)) {
Svetoslav683914b2015-01-15 14:22:26 -08002920 return;
2921 }
2922
2923 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
2924 SQLiteDatabase database = dbHelper.getWritableDatabase();
2925
2926 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
2927 }
2928
2929 private void migrateLegacySettingsForUserLocked(DatabaseHelper dbHelper,
2930 SQLiteDatabase database, int userId) {
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002931 // Move over the system settings.
2932 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2933 ensureSettingsStateLocked(systemKey);
2934 SettingsState systemSettings = mSettingsStates.get(systemKey);
2935 migrateLegacySettingsLocked(systemSettings, database, TABLE_SYSTEM);
2936 systemSettings.persistSyncLocked();
Svetoslav683914b2015-01-15 14:22:26 -08002937
2938 // Move over the secure settings.
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002939 // Do this after System settings, since this is the first thing we check when deciding
2940 // to skip over migration from db to xml for a secondary user.
Svetoslav683914b2015-01-15 14:22:26 -08002941 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2942 ensureSettingsStateLocked(secureKey);
2943 SettingsState secureSettings = mSettingsStates.get(secureKey);
2944 migrateLegacySettingsLocked(secureSettings, database, TABLE_SECURE);
2945 ensureSecureSettingAndroidIdSetLocked(secureSettings);
2946 secureSettings.persistSyncLocked();
2947
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002948 // Move over the global settings if owner.
2949 // Do this last, since this is the first thing we check when deciding
2950 // to skip over migration from db to xml for owner user.
2951 if (userId == UserHandle.USER_SYSTEM) {
2952 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, userId);
2953 ensureSettingsStateLocked(globalKey);
2954 SettingsState globalSettings = mSettingsStates.get(globalKey);
2955 migrateLegacySettingsLocked(globalSettings, database, TABLE_GLOBAL);
Amith Yamasani39452022017-03-21 15:23:47 -07002956 // If this was just created
2957 if (mSettingsCreationBuildId != null) {
2958 globalSettings.insertSettingLocked(Settings.Global.DATABASE_CREATION_BUILDID,
2959 mSettingsCreationBuildId, null, true,
2960 SettingsState.SYSTEM_PACKAGE_NAME);
2961 }
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002962 globalSettings.persistSyncLocked();
2963 }
Svetoslav683914b2015-01-15 14:22:26 -08002964
2965 // Drop the database as now all is moved and persisted.
2966 if (DROP_DATABASE_ON_MIGRATION) {
2967 dbHelper.dropDatabase();
2968 } else {
2969 dbHelper.backupDatabase();
2970 }
2971 }
2972
2973 private void migrateLegacySettingsLocked(SettingsState settingsState,
2974 SQLiteDatabase database, String table) {
2975 SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
2976 queryBuilder.setTables(table);
2977
2978 Cursor cursor = queryBuilder.query(database, ALL_COLUMNS,
2979 null, null, null, null, null);
2980
2981 if (cursor == null) {
2982 return;
2983 }
2984
2985 try {
2986 if (!cursor.moveToFirst()) {
2987 return;
2988 }
2989
2990 final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME);
2991 final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE);
2992
2993 settingsState.setVersionLocked(database.getVersion());
2994
2995 while (!cursor.isAfterLast()) {
2996 String name = cursor.getString(nameColumnIdx);
2997 String value = cursor.getString(valueColumnIdx);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002998 settingsState.insertSettingLocked(name, value, null, true,
Svetoslav683914b2015-01-15 14:22:26 -08002999 SettingsState.SYSTEM_PACKAGE_NAME);
3000 cursor.moveToNext();
3001 }
3002 } finally {
3003 cursor.close();
3004 }
3005 }
3006
Andreas Gampeb58893072018-09-05 16:52:31 -07003007 @GuardedBy("secureSettings.mLock")
Svetoslav683914b2015-01-15 14:22:26 -08003008 private void ensureSecureSettingAndroidIdSetLocked(SettingsState secureSettings) {
3009 Setting value = secureSettings.getSettingLocked(Settings.Secure.ANDROID_ID);
3010
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09003011 if (!value.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08003012 return;
3013 }
3014
3015 final int userId = getUserIdFromKey(secureSettings.mKey);
3016
3017 final UserInfo user;
3018 final long identity = Binder.clearCallingIdentity();
3019 try {
3020 user = mUserManager.getUserInfo(userId);
3021 } finally {
3022 Binder.restoreCallingIdentity(identity);
3023 }
3024 if (user == null) {
3025 // Can happen due to races when deleting users - treat as benign.
3026 return;
3027 }
3028
3029 String androidId = Long.toHexString(new SecureRandom().nextLong());
3030 secureSettings.insertSettingLocked(Settings.Secure.ANDROID_ID, androidId,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003031 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Svetoslav683914b2015-01-15 14:22:26 -08003032
3033 Slog.d(LOG_TAG, "Generated and saved new ANDROID_ID [" + androidId
3034 + "] for user " + userId);
3035
3036 // Write a drop box entry if it's a restricted profile
3037 if (user.isRestricted()) {
3038 DropBoxManager dbm = (DropBoxManager) getContext().getSystemService(
3039 Context.DROPBOX_SERVICE);
3040 if (dbm != null && dbm.isTagEnabled(DROPBOX_TAG_USERLOG)) {
3041 dbm.addText(DROPBOX_TAG_USERLOG, System.currentTimeMillis()
3042 + "," + DROPBOX_TAG_USERLOG + "," + androidId + "\n");
3043 }
3044 }
3045 }
3046
3047 private void notifyForSettingsChange(int key, String name) {
Svet Ganov945864c2018-03-22 21:49:10 -07003048 // Increment the generation first, so observers always see the new value
Phil Weaver83fec002016-05-11 10:55:29 -07003049 mGenerationRegistry.incrementGeneration(key);
3050
Svet Ganov945864c2018-03-22 21:49:10 -07003051 if (isGlobalSettingsKey(key)) {
Jeff Sharkey308093f2018-03-25 22:53:29 -06003052 final long token = Binder.clearCallingIdentity();
3053 try {
3054 if (Global.LOCATION_GLOBAL_KILL_SWITCH.equals(name)) {
3055 // When the global kill switch is updated, send the
3056 // change notification for the location setting.
3057 notifyLocationChangeForRunningUsers();
3058 }
3059 notifyGlobalSettingChangeForRunningUsers(key, name);
3060 } finally {
3061 Binder.restoreCallingIdentity(token);
Svet Ganov945864c2018-03-22 21:49:10 -07003062 }
Svet Ganov945864c2018-03-22 21:49:10 -07003063 } else {
3064 final int userId = getUserIdFromKey(key);
3065 final Uri uri = getNotificationUriFor(key, name);
3066 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
3067 userId, 0, uri).sendToTarget();
3068 if (isSecureSettingsKey(key)) {
3069 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
3070 sSecureCloneToManagedSettings);
3071 maybeNotifyProfiles(SETTINGS_TYPE_SYSTEM, userId, uri, name,
3072 sSystemCloneFromParentOnDependency.values());
3073 } else if (isSystemSettingsKey(key)) {
3074 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
3075 sSystemCloneToManagedSettings);
3076 }
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003077 }
Svet Ganov53a441c2016-04-19 19:38:00 -07003078
Svet Ganov945864c2018-03-22 21:49:10 -07003079 // Always notify that our data changed
Svet Ganov53a441c2016-04-19 19:38:00 -07003080 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003081 }
3082
Svet Ganov53a441c2016-04-19 19:38:00 -07003083 private void maybeNotifyProfiles(int type, int userId, Uri uri, String name,
Robin Lee7af9a742017-02-20 14:47:30 +00003084 Collection<String> keysCloned) {
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003085 if (keysCloned.contains(name)) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003086 for (int profileId : mUserManager.getProfileIdsWithDisabled(userId)) {
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003087 // the notification for userId has already been sent.
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003088 if (profileId != userId) {
Svet Ganov945864c2018-03-22 21:49:10 -07003089 final int key = makeKey(type, profileId);
3090 // Increment the generation first, so observers always see the new value
3091 mGenerationRegistry.incrementGeneration(key);
Svetoslav7e0683b2015-08-03 16:02:52 -07003092 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003093 profileId, 0, uri).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003094 }
3095 }
3096 }
Svetoslav683914b2015-01-15 14:22:26 -08003097 }
3098
Svet Ganov945864c2018-03-22 21:49:10 -07003099 private void notifyGlobalSettingChangeForRunningUsers(int key, String name) {
3100 // Important: No need to update generation for each user as there
3101 // is a singleton generation entry for the global settings which
3102 // is already incremented be the caller.
3103 final Uri uri = getNotificationUriFor(key, name);
3104 final List<UserInfo> users = mUserManager.getUsers(/*excludeDying*/ true);
3105 for (int i = 0; i < users.size(); i++) {
3106 final int userId = users.get(i).id;
3107 if (mUserManager.isUserRunning(UserHandle.of(userId))) {
3108 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
3109 userId, 0, uri).sendToTarget();
3110 }
3111 }
3112 }
3113
Makoto Onuki0000d322017-11-28 16:31:47 -08003114 private void notifyLocationChangeForRunningUsers() {
3115 final List<UserInfo> users = mUserManager.getUsers(/*excludeDying=*/ true);
3116
3117 for (int i = 0; i < users.size(); i++) {
3118 final int userId = users.get(i).id;
3119
3120 if (!mUserManager.isUserRunning(UserHandle.of(userId))) {
3121 continue;
3122 }
Makoto Onuki0000d322017-11-28 16:31:47 -08003123
Svet Ganov945864c2018-03-22 21:49:10 -07003124 // Increment the generation first, so observers always see the new value
3125 final int key = makeKey(SETTINGS_TYPE_SECURE, userId);
3126 mGenerationRegistry.incrementGeneration(key);
3127
Kweku Adams5e0052b2019-02-22 15:17:52 -08003128 final Uri uri = getNotificationUriFor(key, Secure.LOCATION_MODE);
Makoto Onuki0000d322017-11-28 16:31:47 -08003129 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
3130 userId, 0, uri).sendToTarget();
3131 }
3132 }
3133
Matt Pape1b31a332018-10-17 09:58:28 -07003134 private boolean isConfigSettingsKey(int key) {
3135 return getTypeFromKey(key) == SETTINGS_TYPE_CONFIG;
3136 }
3137
Svetoslav683914b2015-01-15 14:22:26 -08003138 private boolean isGlobalSettingsKey(int key) {
3139 return getTypeFromKey(key) == SETTINGS_TYPE_GLOBAL;
3140 }
3141
3142 private boolean isSystemSettingsKey(int key) {
3143 return getTypeFromKey(key) == SETTINGS_TYPE_SYSTEM;
3144 }
3145
3146 private boolean isSecureSettingsKey(int key) {
3147 return getTypeFromKey(key) == SETTINGS_TYPE_SECURE;
3148 }
3149
Mark Rathjend891f012017-01-19 04:10:37 +00003150 private boolean isSsaidSettingsKey(int key) {
3151 return getTypeFromKey(key) == SETTINGS_TYPE_SSAID;
3152 }
3153
Svetoslav683914b2015-01-15 14:22:26 -08003154 private File getSettingsFile(int key) {
Matt Pape1b31a332018-10-17 09:58:28 -07003155 if (isConfigSettingsKey(key)) {
3156 final int userId = getUserIdFromKey(key);
3157 return new File(Environment.getUserSystemDirectory(userId),
3158 SETTINGS_FILE_CONFIG);
3159 } else if (isGlobalSettingsKey(key)) {
Svetoslav683914b2015-01-15 14:22:26 -08003160 final int userId = getUserIdFromKey(key);
3161 return new File(Environment.getUserSystemDirectory(userId),
3162 SETTINGS_FILE_GLOBAL);
3163 } else if (isSystemSettingsKey(key)) {
3164 final int userId = getUserIdFromKey(key);
3165 return new File(Environment.getUserSystemDirectory(userId),
3166 SETTINGS_FILE_SYSTEM);
3167 } else if (isSecureSettingsKey(key)) {
3168 final int userId = getUserIdFromKey(key);
3169 return new File(Environment.getUserSystemDirectory(userId),
3170 SETTINGS_FILE_SECURE);
Mark Rathjend891f012017-01-19 04:10:37 +00003171 } else if (isSsaidSettingsKey(key)) {
3172 final int userId = getUserIdFromKey(key);
3173 return new File(Environment.getUserSystemDirectory(userId),
3174 SETTINGS_FILE_SSAID);
Svetoslav683914b2015-01-15 14:22:26 -08003175 } else {
3176 throw new IllegalArgumentException("Invalid settings key:" + key);
3177 }
3178 }
3179
3180 private Uri getNotificationUriFor(int key, String name) {
Matt Pape1b31a332018-10-17 09:58:28 -07003181 if (isConfigSettingsKey(key)) {
Matt Pape1278d1c2018-12-11 13:03:49 -08003182 return (name != null) ? Uri.withAppendedPath(DeviceConfig.CONTENT_URI, name)
3183 : DeviceConfig.CONTENT_URI;
Matt Pape1b31a332018-10-17 09:58:28 -07003184 } else if (isGlobalSettingsKey(key)) {
Svetoslav683914b2015-01-15 14:22:26 -08003185 return (name != null) ? Uri.withAppendedPath(Settings.Global.CONTENT_URI, name)
3186 : Settings.Global.CONTENT_URI;
3187 } else if (isSecureSettingsKey(key)) {
3188 return (name != null) ? Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name)
3189 : Settings.Secure.CONTENT_URI;
3190 } else if (isSystemSettingsKey(key)) {
3191 return (name != null) ? Uri.withAppendedPath(Settings.System.CONTENT_URI, name)
3192 : Settings.System.CONTENT_URI;
3193 } else {
3194 throw new IllegalArgumentException("Invalid settings key:" + key);
3195 }
3196 }
3197
3198 private int getMaxBytesPerPackageForType(int type) {
3199 switch (type) {
Matt Pape1b31a332018-10-17 09:58:28 -07003200 case SETTINGS_TYPE_CONFIG:
Svetoslav683914b2015-01-15 14:22:26 -08003201 case SETTINGS_TYPE_GLOBAL:
Mark Rathjend891f012017-01-19 04:10:37 +00003202 case SETTINGS_TYPE_SECURE:
3203 case SETTINGS_TYPE_SSAID: {
Svetoslav683914b2015-01-15 14:22:26 -08003204 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED;
3205 }
3206
3207 default: {
3208 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_LIMITED;
3209 }
3210 }
3211 }
3212
Svetoslav7e0683b2015-08-03 16:02:52 -07003213 private final class MyHandler extends Handler {
3214 private static final int MSG_NOTIFY_URI_CHANGED = 1;
3215 private static final int MSG_NOTIFY_DATA_CHANGED = 2;
3216
3217 public MyHandler(Looper looper) {
3218 super(looper);
3219 }
3220
3221 @Override
3222 public void handleMessage(Message msg) {
3223 switch (msg.what) {
3224 case MSG_NOTIFY_URI_CHANGED: {
3225 final int userId = msg.arg1;
3226 Uri uri = (Uri) msg.obj;
Jeff Sharkey88f9d0b2017-08-11 15:29:40 -06003227 try {
3228 getContext().getContentResolver().notifyChange(uri, null, true, userId);
3229 } catch (SecurityException e) {
3230 Slog.w(LOG_TAG, "Failed to notify for " + userId + ": " + uri, e);
3231 }
Makoto Onuki0000d322017-11-28 16:31:47 -08003232 if (DEBUG || true) {
Svetoslav7e0683b2015-08-03 16:02:52 -07003233 Slog.v(LOG_TAG, "Notifying for " + userId + ": " + uri);
3234 }
3235 } break;
3236
3237 case MSG_NOTIFY_DATA_CHANGED: {
3238 mBackupManager.dataChanged();
3239 } break;
3240 }
3241 }
3242 }
3243
Svetoslav683914b2015-01-15 14:22:26 -08003244 private final class UpgradeController {
Winson Chungd9f2fb32019-03-05 11:10:12 -08003245 private static final int SETTINGS_VERSION = 177;
Svetoslav683914b2015-01-15 14:22:26 -08003246
3247 private final int mUserId;
3248
3249 public UpgradeController(int userId) {
3250 mUserId = userId;
3251 }
3252
3253 public void upgradeIfNeededLocked() {
3254 // The version of all settings for a user is the same (all users have secure).
3255 SettingsState secureSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07003256 SETTINGS_TYPE_SECURE, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08003257
3258 // Try an update from the current state.
3259 final int oldVersion = secureSettings.getVersionLocked();
3260 final int newVersion = SETTINGS_VERSION;
3261
Svet Ganovc9755bc2015-03-28 13:21:22 -07003262 // If up do date - done.
Svetoslav683914b2015-01-15 14:22:26 -08003263 if (oldVersion == newVersion) {
3264 return;
3265 }
3266
3267 // Try to upgrade.
3268 final int curVersion = onUpgradeLocked(mUserId, oldVersion, newVersion);
3269
3270 // If upgrade failed start from scratch and upgrade.
3271 if (curVersion != newVersion) {
3272 // Drop state we have for this user.
3273 removeUserStateLocked(mUserId, true);
3274
3275 // Recreate the database.
3276 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), mUserId);
3277 SQLiteDatabase database = dbHelper.getWritableDatabase();
3278 dbHelper.recreateDatabase(database, newVersion, curVersion, oldVersion);
3279
3280 // Migrate the settings for this user.
3281 migrateLegacySettingsForUserLocked(dbHelper, database, mUserId);
3282
3283 // Now upgrade should work fine.
3284 onUpgradeLocked(mUserId, oldVersion, newVersion);
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07003285
3286 // Make a note what happened, so we don't wonder why data was lost
3287 String reason = "Settings rebuilt! Current version: "
3288 + curVersion + " while expected: " + newVersion;
3289 getGlobalSettingsLocked().insertSettingLocked(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003290 Settings.Global.DATABASE_DOWNGRADE_REASON,
3291 reason, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Svetoslav683914b2015-01-15 14:22:26 -08003292 }
3293
3294 // Set the global settings version if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07003295 if (mUserId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08003296 SettingsState globalSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07003297 SETTINGS_TYPE_GLOBAL, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08003298 globalSettings.setVersionLocked(newVersion);
3299 }
3300
3301 // Set the secure settings version.
3302 secureSettings.setVersionLocked(newVersion);
3303
3304 // Set the system settings version.
3305 SettingsState systemSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07003306 SETTINGS_TYPE_SYSTEM, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08003307 systemSettings.setVersionLocked(newVersion);
3308 }
3309
3310 private SettingsState getGlobalSettingsLocked() {
Xiaohui Chen43765b72015-08-31 10:57:33 -07003311 return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08003312 }
3313
3314 private SettingsState getSecureSettingsLocked(int userId) {
3315 return getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
3316 }
3317
Mark Rathjend891f012017-01-19 04:10:37 +00003318 private SettingsState getSsaidSettingsLocked(int userId) {
3319 return getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
3320 }
3321
Svetoslav683914b2015-01-15 14:22:26 -08003322 private SettingsState getSystemSettingsLocked(int userId) {
3323 return getSettingsLocked(SETTINGS_TYPE_SYSTEM, userId);
3324 }
3325
Jeff Brown503cffc2015-03-26 18:08:51 -07003326 /**
3327 * You must perform all necessary mutations to bring the settings
3328 * for this user from the old to the new version. When you add a new
3329 * upgrade step you *must* update SETTINGS_VERSION.
3330 *
3331 * This is an example of moving a setting from secure to global.
3332 *
3333 * // v119: Example settings changes.
3334 * if (currentVersion == 118) {
3335 * if (userId == UserHandle.USER_OWNER) {
3336 * // Remove from the secure settings.
3337 * SettingsState secureSettings = getSecureSettingsLocked(userId);
3338 * String name = "example_setting_to_move";
3339 * String value = secureSettings.getSetting(name);
3340 * secureSettings.deleteSetting(name);
3341 *
3342 * // Add to the global settings.
3343 * SettingsState globalSettings = getGlobalSettingsLocked();
3344 * globalSettings.insertSetting(name, value, SettingsState.SYSTEM_PACKAGE_NAME);
3345 * }
3346 *
3347 * // Update the current version.
3348 * currentVersion = 119;
3349 * }
3350 */
Svetoslav683914b2015-01-15 14:22:26 -08003351 private int onUpgradeLocked(int userId, int oldVersion, int newVersion) {
3352 if (DEBUG) {
3353 Slog.w(LOG_TAG, "Upgrading settings for user: " + userId + " from version: "
3354 + oldVersion + " to version: " + newVersion);
3355 }
3356
Jeff Brown503cffc2015-03-26 18:08:51 -07003357 int currentVersion = oldVersion;
Svetoslav683914b2015-01-15 14:22:26 -08003358
John Spurlocke11ae112015-05-11 16:09:03 -04003359 // v119: Reset zen + ringer mode.
3360 if (currentVersion == 118) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07003361 if (userId == UserHandle.USER_SYSTEM) {
John Spurlocke11ae112015-05-11 16:09:03 -04003362 final SettingsState globalSettings = getGlobalSettingsLocked();
3363 globalSettings.updateSettingLocked(Settings.Global.ZEN_MODE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003364 Integer.toString(Settings.Global.ZEN_MODE_OFF), null,
3365 true, SettingsState.SYSTEM_PACKAGE_NAME);
John Spurlocke11ae112015-05-11 16:09:03 -04003366 globalSettings.updateSettingLocked(Settings.Global.MODE_RINGER,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003367 Integer.toString(AudioManager.RINGER_MODE_NORMAL), null,
3368 true, SettingsState.SYSTEM_PACKAGE_NAME);
John Spurlocke11ae112015-05-11 16:09:03 -04003369 }
3370 currentVersion = 119;
3371 }
3372
Jason Monk27bbb2d2015-03-31 16:46:39 -04003373 // v120: Add double tap to wake setting.
3374 if (currentVersion == 119) {
3375 SettingsState secureSettings = getSecureSettingsLocked(userId);
3376 secureSettings.insertSettingLocked(Settings.Secure.DOUBLE_TAP_TO_WAKE,
3377 getContext().getResources().getBoolean(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003378 R.bool.def_double_tap_to_wake) ? "1" : "0", null, true,
Jason Monk27bbb2d2015-03-31 16:46:39 -04003379 SettingsState.SYSTEM_PACKAGE_NAME);
3380
3381 currentVersion = 120;
3382 }
3383
Svetoslav7e0683b2015-08-03 16:02:52 -07003384 if (currentVersion == 120) {
3385 // Before 121, we used a different string encoding logic. We just bump the
3386 // version here; SettingsState knows how to handle pre-version 120 files.
3387 currentVersion = 121;
3388 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -07003389
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003390 if (currentVersion == 121) {
3391 // Version 122: allow OEMs to set a default payment component in resources.
3392 // Note that we only write the default if no default has been set;
3393 // if there is, we just leave the default at whatever it currently is.
3394 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3395 String defaultComponent = (getContext().getResources().getString(
3396 R.string.def_nfc_payment_component));
3397 Setting currentSetting = secureSettings.getSettingLocked(
3398 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT);
3399 if (defaultComponent != null && !defaultComponent.isEmpty() &&
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003400 currentSetting.isNull()) {
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003401 secureSettings.insertSettingLocked(
3402 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003403 defaultComponent, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003404 }
3405 currentVersion = 122;
3406 }
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003407
3408 if (currentVersion == 122) {
3409 // Version 123: Adding a default value for the ability to add a user from
3410 // the lock screen.
3411 if (userId == UserHandle.USER_SYSTEM) {
3412 final SettingsState globalSettings = getGlobalSettingsLocked();
3413 Setting currentSetting = globalSettings.getSettingLocked(
3414 Settings.Global.ADD_USERS_WHEN_LOCKED);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003415 if (currentSetting.isNull()) {
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003416 globalSettings.insertSettingLocked(
3417 Settings.Global.ADD_USERS_WHEN_LOCKED,
3418 getContext().getResources().getBoolean(
3419 R.bool.def_add_users_from_lockscreen) ? "1" : "0",
Svetoslav Ganove080da92016-12-21 17:10:35 -08003420 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003421 }
3422 }
3423 currentVersion = 123;
3424 }
Bryce Leebd179282015-12-17 19:01:37 -08003425
3426 if (currentVersion == 123) {
Bryce Leeec85f342015-12-16 13:32:28 -08003427 final SettingsState globalSettings = getGlobalSettingsLocked();
3428 String defaultDisabledProfiles = (getContext().getResources().getString(
3429 R.string.def_bluetooth_disabled_profiles));
3430 globalSettings.insertSettingLocked(Settings.Global.BLUETOOTH_DISABLED_PROFILES,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003431 defaultDisabledProfiles, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Bryce Leebd179282015-12-17 19:01:37 -08003432 currentVersion = 124;
Bryce Leeec85f342015-12-16 13:32:28 -08003433 }
3434
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003435 if (currentVersion == 124) {
3436 // Version 124: allow OEMs to set a default value for whether IME should be
3437 // shown when a physical keyboard is connected.
3438 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3439 Setting currentSetting = secureSettings.getSettingLocked(
3440 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003441 if (currentSetting.isNull()) {
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003442 secureSettings.insertSettingLocked(
3443 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD,
3444 getContext().getResources().getBoolean(
3445 R.bool.def_show_ime_with_hard_keyboard) ? "1" : "0",
Svetoslav Ganove080da92016-12-21 17:10:35 -08003446 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003447 }
3448 currentVersion = 125;
3449 }
3450
Ruben Brunk98576cf2016-03-07 18:54:28 -08003451 if (currentVersion == 125) {
3452 // Version 125: Allow OEMs to set the default VR service.
3453 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3454
3455 Setting currentSetting = secureSettings.getSettingLocked(
3456 Settings.Secure.ENABLED_VR_LISTENERS);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003457 if (currentSetting.isNull()) {
Ruben Brunk98576cf2016-03-07 18:54:28 -08003458 ArraySet<ComponentName> l =
3459 SystemConfig.getInstance().getDefaultVrComponents();
3460
3461 if (l != null && !l.isEmpty()) {
3462 StringBuilder b = new StringBuilder();
3463 boolean start = true;
3464 for (ComponentName c : l) {
3465 if (!start) {
3466 b.append(':');
3467 }
3468 b.append(c.flattenToString());
3469 start = false;
3470 }
3471 secureSettings.insertSettingLocked(
3472 Settings.Secure.ENABLED_VR_LISTENERS, b.toString(),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003473 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Ruben Brunk98576cf2016-03-07 18:54:28 -08003474 }
3475
3476 }
3477 currentVersion = 126;
3478 }
3479
Daniel U02ba6122016-04-01 18:41:42 +01003480 if (currentVersion == 126) {
3481 // Version 126: copy the primary values of LOCK_SCREEN_SHOW_NOTIFICATIONS and
3482 // LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS into managed profile.
3483 if (mUserManager.isManagedProfile(userId)) {
3484 final SettingsState systemSecureSettings =
3485 getSecureSettingsLocked(UserHandle.USER_SYSTEM);
3486
3487 final Setting showNotifications = systemSecureSettings.getSettingLocked(
3488 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09003489 if (!showNotifications.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01003490 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3491 secureSettings.insertSettingLocked(
3492 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003493 showNotifications.getValue(), null, true,
Daniel U02ba6122016-04-01 18:41:42 +01003494 SettingsState.SYSTEM_PACKAGE_NAME);
3495 }
3496
3497 final Setting allowPrivate = systemSecureSettings.getSettingLocked(
3498 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09003499 if (!allowPrivate.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01003500 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3501 secureSettings.insertSettingLocked(
3502 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003503 allowPrivate.getValue(), null, true,
Daniel U02ba6122016-04-01 18:41:42 +01003504 SettingsState.SYSTEM_PACKAGE_NAME);
3505 }
3506 }
3507 currentVersion = 127;
3508 }
3509
Steven Ngdc20ba62016-04-26 18:19:04 +01003510 if (currentVersion == 127) {
Mahaver Chopra3d9805d2016-07-07 16:25:05 +01003511 // version 127 is no longer used.
Steven Ngdc20ba62016-04-26 18:19:04 +01003512 currentVersion = 128;
3513 }
3514
Julia Reynolds1f721e12016-07-11 08:50:58 -04003515 if (currentVersion == 128) {
Julia Reynoldsb852e562017-06-06 16:14:18 -04003516 // Version 128: Removed
Julia Reynolds1f721e12016-07-11 08:50:58 -04003517 currentVersion = 129;
3518 }
3519
Dan Sandler71f85e92016-07-20 13:46:05 -04003520 if (currentVersion == 129) {
3521 // default longpress timeout changed from 500 to 400. If unchanged from the old
3522 // default, update to the new default.
3523 final SettingsState systemSecureSettings =
3524 getSecureSettingsLocked(userId);
3525 final String oldValue = systemSecureSettings.getSettingLocked(
3526 Settings.Secure.LONG_PRESS_TIMEOUT).getValue();
3527 if (TextUtils.equals("500", oldValue)) {
3528 systemSecureSettings.insertSettingLocked(
3529 Settings.Secure.LONG_PRESS_TIMEOUT,
3530 String.valueOf(getContext().getResources().getInteger(
3531 R.integer.def_long_press_timeout_millis)),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003532 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Dan Sandler71f85e92016-07-20 13:46:05 -04003533 }
3534 currentVersion = 130;
3535 }
3536
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003537 if (currentVersion == 130) {
Adrian Roos69741a22016-10-21 14:49:17 -07003538 // Split Ambient settings
3539 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3540 boolean dozeExplicitlyDisabled = "0".equals(secureSettings.
3541 getSettingLocked(Settings.Secure.DOZE_ENABLED).getValue());
3542
3543 if (dozeExplicitlyDisabled) {
Lucas Dupin4359b552018-08-09 15:07:54 -07003544 secureSettings.insertSettingLocked(Settings.Secure.DOZE_PICK_UP_GESTURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003545 "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Lucas Dupin4359b552018-08-09 15:07:54 -07003546 secureSettings.insertSettingLocked(Settings.Secure.DOZE_DOUBLE_TAP_GESTURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003547 "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Adrian Roos69741a22016-10-21 14:49:17 -07003548 }
3549 currentVersion = 131;
3550 }
3551
3552 if (currentVersion == 131) {
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003553 // Initialize new multi-press timeout to default value
3554 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3555 final String oldValue = systemSecureSettings.getSettingLocked(
3556 Settings.Secure.MULTI_PRESS_TIMEOUT).getValue();
3557 if (TextUtils.equals(null, oldValue)) {
3558 systemSecureSettings.insertSettingLocked(
3559 Settings.Secure.MULTI_PRESS_TIMEOUT,
3560 String.valueOf(getContext().getResources().getInteger(
3561 R.integer.def_multi_press_timeout_millis)),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003562 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003563 }
3564
Adrian Roos69741a22016-10-21 14:49:17 -07003565 currentVersion = 132;
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003566 }
3567
Adrian Roos69741a22016-10-21 14:49:17 -07003568 if (currentVersion == 132) {
3569 // Version 132: Allow managed profile to optionally use the parent's ringtones
Andre Lagoea35e072016-08-04 13:41:13 +01003570 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3571 String defaultSyncParentSounds = (getContext().getResources()
3572 .getBoolean(R.bool.def_sync_parent_sounds) ? "1" : "0");
3573 systemSecureSettings.insertSettingLocked(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003574 Settings.Secure.SYNC_PARENT_SOUNDS, defaultSyncParentSounds,
3575 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Adrian Roos69741a22016-10-21 14:49:17 -07003576 currentVersion = 133;
Andre Lagoea35e072016-08-04 13:41:13 +01003577 }
3578
Adrian Roos69741a22016-10-21 14:49:17 -07003579 if (currentVersion == 133) {
3580 // Version 133: Add default end button behavior
Keun-young Parkec7a1182016-10-18 11:52:38 -07003581 final SettingsState systemSettings = getSystemSettingsLocked(userId);
davidlnedb31e12018-09-25 10:21:20 -07003582 if (systemSettings.getSettingLocked(Settings.System.END_BUTTON_BEHAVIOR)
3583 .isNull()) {
Keun-young Parkec7a1182016-10-18 11:52:38 -07003584 String defaultEndButtonBehavior = Integer.toString(getContext()
3585 .getResources().getInteger(R.integer.def_end_button_behavior));
3586 systemSettings.insertSettingLocked(Settings.System.END_BUTTON_BEHAVIOR,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003587 defaultEndButtonBehavior, null, true,
3588 SettingsState.SYSTEM_PACKAGE_NAME);
Keun-young Parkec7a1182016-10-18 11:52:38 -07003589 }
Adrian Roos69741a22016-10-21 14:49:17 -07003590 currentVersion = 134;
Keun-young Parkec7a1182016-10-18 11:52:38 -07003591 }
3592
Phil Weaver89e3ffc2016-09-19 13:51:10 -07003593 if (currentVersion == 134) {
3594 // Remove setting that specifies if magnification values should be preserved.
3595 // This setting defaulted to true and never has a UI.
3596 getSecureSettingsLocked(userId).deleteSettingLocked(
3597 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_AUTO_UPDATE);
3598 currentVersion = 135;
3599 }
3600
Jeremy Joslin8bdad342016-12-14 11:46:47 -08003601 if (currentVersion == 135) {
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003602 // Version 135 no longer used.
Jeremy Joslin8bdad342016-12-14 11:46:47 -08003603 currentVersion = 136;
3604 }
3605
Mark Rathjend891f012017-01-19 04:10:37 +00003606 if (currentVersion == 136) {
3607 // Version 136: Store legacy SSAID for all apps currently installed on the
3608 // device as first step in migrating SSAID to be unique per application.
3609
3610 final boolean isUpgrade;
3611 try {
3612 isUpgrade = mPackageManager.isUpgrade();
3613 } catch (RemoteException e) {
3614 throw new IllegalStateException("Package manager not available");
3615 }
3616 // Only retain legacy ssaid if the device is performing an OTA. After wiping
3617 // user data or first boot on a new device should use new ssaid generation.
3618 if (isUpgrade) {
3619 // Retrieve the legacy ssaid from the secure settings table.
Mark Rathjenea617592017-01-18 23:03:41 -08003620 final Setting legacySsaidSetting = getSettingLocked(SETTINGS_TYPE_SECURE,
3621 userId, Settings.Secure.ANDROID_ID);
3622 if (legacySsaidSetting == null || legacySsaidSetting.isNull()
3623 || legacySsaidSetting.getValue() == null) {
3624 throw new IllegalStateException("Legacy ssaid not accessible");
3625 }
3626 final String legacySsaid = legacySsaidSetting.getValue();
Mark Rathjend891f012017-01-19 04:10:37 +00003627
3628 // Fill each uid with the legacy ssaid to be backwards compatible.
3629 final List<PackageInfo> packages;
3630 try {
Tetsutoki Shiozawaebe0e5f2018-01-17 11:07:09 +09003631 packages = mPackageManager.getInstalledPackages(
3632 PackageManager.MATCH_UNINSTALLED_PACKAGES,
3633 userId).getList();
Mark Rathjend891f012017-01-19 04:10:37 +00003634 } catch (RemoteException e) {
3635 throw new IllegalStateException("Package manager not available");
3636 }
3637
3638 final SettingsState ssaidSettings = getSsaidSettingsLocked(userId);
3639 for (PackageInfo info : packages) {
3640 // Check if the UID already has an entry in the table.
3641 final String uid = Integer.toString(info.applicationInfo.uid);
3642 final Setting ssaid = ssaidSettings.getSettingLocked(uid);
3643
3644 if (ssaid.isNull() || ssaid.getValue() == null) {
3645 // Android Id doesn't exist for this package so create it.
3646 ssaidSettings.insertSettingLocked(uid, legacySsaid, null, true,
3647 info.packageName);
Tetsutoki Shiozawaebe0e5f2018-01-17 11:07:09 +09003648 if (DEBUG) {
3649 Slog.d(LOG_TAG, "Keep the legacy ssaid for uid=" + uid);
3650 }
Mark Rathjend891f012017-01-19 04:10:37 +00003651 }
3652 }
3653 }
3654
3655 currentVersion = 137;
3656 }
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003657 if (currentVersion == 137) {
3658 // Version 138: Settings.Secure#INSTALL_NON_MARKET_APPS is deprecated and its
3659 // default value set to 1. The user can no longer change the value of this
3660 // setting through the UI.
3661 final SettingsState secureSetting = getSecureSettingsLocked(userId);
3662 if (!mUserManager.hasUserRestriction(
Suprabh Shukla0b1356f2017-02-21 14:33:50 -08003663 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, UserHandle.of(userId))
3664 && secureSetting.getSettingLocked(
3665 Settings.Secure.INSTALL_NON_MARKET_APPS).getValue().equals("0")) {
3666
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003667 secureSetting.insertSettingLocked(Settings.Secure.INSTALL_NON_MARKET_APPS,
3668 "1", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shukla0b1356f2017-02-21 14:33:50 -08003669 // For managed profiles with profile owners, DevicePolicyManagerService
3670 // may want to set the user restriction in this case
3671 secureSetting.insertSettingLocked(
3672 Settings.Secure.UNKNOWN_SOURCES_DEFAULT_REVERSED, "1", null, true,
3673 SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003674 }
3675 currentVersion = 138;
3676 }
Mark Rathjend891f012017-01-19 04:10:37 +00003677
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003678 if (currentVersion == 138) {
Jeremy Joslin27d14c42017-02-15 12:02:03 -08003679 // Version 139: Removed.
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003680 currentVersion = 139;
3681 }
3682
Phil Weaver385912e2017-02-10 10:06:56 -08003683 if (currentVersion == 139) {
3684 // Version 140: Settings.Secure#ACCESSIBILITY_SPEAK_PASSWORD is deprecated and
3685 // the user can no longer change the value of this setting through the UI.
3686 // Force to true.
3687 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3688 secureSettings.updateSettingLocked(Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD,
3689 "1", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3690 currentVersion = 140;
3691 }
3692
Julia Reynoldsad0d9e02017-02-15 08:41:48 -05003693 if (currentVersion == 140) {
Julia Reynoldsb852e562017-06-06 16:14:18 -04003694 // Version 141: Removed
Julia Reynoldsad0d9e02017-02-15 08:41:48 -05003695 currentVersion = 141;
3696 }
3697
Svet Ganov13701552017-02-23 12:45:17 -08003698 if (currentVersion == 141) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003699 // This implementation was incorrectly setting the current value of
3700 // settings changed by non-system packages as the default which default
3701 // is set by the system. We add a new upgrade step at the end to properly
3702 // handle this case which would also fix incorrect changes made by the
3703 // old implementation of this step.
Svet Ganov13701552017-02-23 12:45:17 -08003704 currentVersion = 142;
3705 }
3706
Stephen Chen5d0922f2017-03-27 10:28:04 -07003707 if (currentVersion == 142) {
Felipe Lemeff355092017-04-03 12:55:02 -07003708 // Version 143: Set a default value for Wi-Fi wakeup feature.
Stephen Chen5d0922f2017-03-27 10:28:04 -07003709 if (userId == UserHandle.USER_SYSTEM) {
3710 final SettingsState globalSettings = getGlobalSettingsLocked();
3711 Setting currentSetting = globalSettings.getSettingLocked(
3712 Settings.Global.WIFI_WAKEUP_ENABLED);
3713 if (currentSetting.isNull()) {
3714 globalSettings.insertSettingLocked(
3715 Settings.Global.WIFI_WAKEUP_ENABLED,
3716 getContext().getResources().getBoolean(
3717 R.bool.def_wifi_wakeup_enabled) ? "1" : "0",
3718 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3719 }
3720 }
3721
3722 currentVersion = 143;
3723 }
3724
Felipe Lemeff355092017-04-03 12:55:02 -07003725 if (currentVersion == 143) {
3726 // Version 144: Set a default value for Autofill service.
3727 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3728 final Setting currentSetting = secureSettings
3729 .getSettingLocked(Settings.Secure.AUTOFILL_SERVICE);
3730 if (currentSetting.isNull()) {
3731 final String defaultValue = getContext().getResources().getString(
3732 com.android.internal.R.string.config_defaultAutofillService);
3733 if (defaultValue != null) {
3734 Slog.d(LOG_TAG, "Setting [" + defaultValue + "] as Autofill Service "
3735 + "for user " + userId);
3736 secureSettings.insertSettingLocked(Settings.Secure.AUTOFILL_SERVICE,
3737 defaultValue, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3738 }
3739 }
3740
3741 currentVersion = 144;
3742 }
3743
Jeremy Joslin45caa252017-05-04 11:22:46 -07003744 if (currentVersion == 144) {
Amin Shaikh86367962017-06-07 08:58:22 -07003745 // Version 145: Removed
3746 currentVersion = 145;
3747 }
3748
3749 if (currentVersion == 145) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003750 // Version 146: In step 142 we had a bug where incorrectly
3751 // some settings were considered system set and as a result
3752 // made the default and marked as the default being set by
3753 // the system. Here reevaluate the default and default system
3754 // set flags. This would both fix corruption by the old impl
3755 // of step 142 and also properly handle devices which never
3756 // run 142.
Jeremy Joslin45caa252017-05-04 11:22:46 -07003757 if (userId == UserHandle.USER_SYSTEM) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003758 SettingsState globalSettings = getGlobalSettingsLocked();
3759 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(globalSettings, userId);
3760 globalSettings.persistSyncLocked();
Jeremy Joslin45caa252017-05-04 11:22:46 -07003761 }
3762
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003763 SettingsState secureSettings = getSecureSettingsLocked(mUserId);
3764 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(secureSettings, userId);
3765 secureSettings.persistSyncLocked();
3766
3767 SettingsState systemSettings = getSystemSettingsLocked(mUserId);
3768 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(systemSettings, userId);
3769 systemSettings.persistSyncLocked();
3770
Amin Shaikh86367962017-06-07 08:58:22 -07003771 currentVersion = 146;
Jeremy Joslin45caa252017-05-04 11:22:46 -07003772 }
3773
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003774 if (currentVersion == 146) {
Joe LaPenna250d7842018-01-25 10:19:42 -08003775 // Version 147: Removed. (This version previously allowed showing the
3776 // "wifi_wakeup_available" setting).
3777 // The setting that was added here is deleted in 153.
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003778 currentVersion = 147;
3779 }
3780
3781 if (currentVersion == 147) {
3782 // Version 148: Set the default value for DEFAULT_RESTRICT_BACKGROUND_DATA.
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003783 if (userId == UserHandle.USER_SYSTEM) {
3784 final SettingsState globalSettings = getGlobalSettingsLocked();
3785 final Setting currentSetting = globalSettings.getSettingLocked(
3786 Global.DEFAULT_RESTRICT_BACKGROUND_DATA);
3787 if (currentSetting.isNull()) {
3788 globalSettings.insertSettingLocked(
3789 Global.DEFAULT_RESTRICT_BACKGROUND_DATA,
3790 getContext().getResources().getBoolean(
3791 R.bool.def_restrict_background_data) ? "1" : "0",
3792 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3793 }
3794 }
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003795 currentVersion = 148;
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003796 }
3797
Tim Zhengcc1e76a2017-08-30 17:46:19 -07003798 if (currentVersion == 148) {
3799 // Version 149: Set the default value for BACKUP_MANAGER_CONSTANTS.
3800 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3801 final String oldValue = systemSecureSettings.getSettingLocked(
3802 Settings.Secure.BACKUP_MANAGER_CONSTANTS).getValue();
3803 if (TextUtils.equals(null, oldValue)) {
3804 final String defaultValue = getContext().getResources().getString(
3805 R.string.def_backup_manager_constants);
3806 if (!TextUtils.isEmpty(defaultValue)) {
3807 systemSecureSettings.insertSettingLocked(
3808 Settings.Secure.BACKUP_MANAGER_CONSTANTS, defaultValue, null,
3809 true, SettingsState.SYSTEM_PACKAGE_NAME);
3810 }
3811 }
Tim Zhengcc1e76a2017-08-30 17:46:19 -07003812 currentVersion = 149;
3813 }
Jacky Cheung7076a312017-10-02 10:40:48 -07003814
3815 if (currentVersion == 149) {
3816 // Version 150: Set a default value for mobile data always on
3817 final SettingsState globalSettings = getGlobalSettingsLocked();
3818 final Setting currentSetting = globalSettings.getSettingLocked(
3819 Settings.Global.MOBILE_DATA_ALWAYS_ON);
3820 if (currentSetting.isNull()) {
3821 globalSettings.insertSettingLocked(
3822 Settings.Global.MOBILE_DATA_ALWAYS_ON,
3823 getContext().getResources().getBoolean(
3824 R.bool.def_mobile_data_always_on) ? "1" : "0",
3825 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3826 }
3827
3828 currentVersion = 150;
3829 }
3830
Mike Digman4af4a6f2018-01-16 14:49:38 -08003831 if (currentVersion == 150) {
Mike Digman32e03312018-05-16 16:43:23 -07003832 // Version 151: Removed.
Mike Digman4af4a6f2018-01-16 14:49:38 -08003833 currentVersion = 151;
3834 }
3835
Joe LaPenna4f50ed42018-01-22 14:54:45 -08003836 if (currentVersion == 151) {
Joe LaPenna250d7842018-01-25 10:19:42 -08003837 // Version 152: Removed. (This version made the setting for wifi_wakeup enabled
3838 // by default but it is now no longer configurable).
3839 // The setting updated here is deleted in 153.
Joe LaPenna4f50ed42018-01-22 14:54:45 -08003840 currentVersion = 152;
3841 }
3842
Joe LaPenna250d7842018-01-25 10:19:42 -08003843 if (currentVersion == 152) {
3844 getGlobalSettingsLocked().deleteSettingLocked("wifi_wakeup_available");
3845 currentVersion = 153;
3846 }
3847
Ben Linb4df8bc2018-01-29 11:48:20 -08003848 if (currentVersion == 153) {
3849 // Version 154: Read notification badge configuration from config.
3850 // If user has already set the value, don't do anything.
3851 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3852 final Setting showNotificationBadges = systemSecureSettings.getSettingLocked(
3853 Settings.Secure.NOTIFICATION_BADGING);
3854 if (showNotificationBadges.isNull()) {
3855 final boolean defaultValue = getContext().getResources().getBoolean(
3856 com.android.internal.R.bool.config_notificationBadging);
3857 systemSecureSettings.insertSettingLocked(
3858 Secure.NOTIFICATION_BADGING,
3859 defaultValue ? "1" : "0",
3860 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3861 }
3862 currentVersion = 154;
3863 }
3864
Bernardo Rufinoeaa78b92018-01-26 11:25:37 +00003865 if (currentVersion == 154) {
3866 // Version 155: Set the default value for BACKUP_LOCAL_TRANSPORT_PARAMETERS.
3867 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3868 final String oldValue = systemSecureSettings.getSettingLocked(
3869 Settings.Secure.BACKUP_LOCAL_TRANSPORT_PARAMETERS).getValue();
3870 if (TextUtils.equals(null, oldValue)) {
3871 final String defaultValue = getContext().getResources().getString(
3872 R.string.def_backup_local_transport_parameters);
3873 if (!TextUtils.isEmpty(defaultValue)) {
3874 systemSecureSettings.insertSettingLocked(
3875 Settings.Secure.BACKUP_LOCAL_TRANSPORT_PARAMETERS, defaultValue,
3876 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3877 }
3878
3879 }
3880 currentVersion = 155;
3881 }
3882
Beverlyda904812018-03-02 09:55:30 -05003883 if (currentVersion == 155) {
Annie Mengd069a882018-03-13 15:31:40 +00003884 // Version 156: Set the default value for CHARGING_STARTED_SOUND.
Beverlyda904812018-03-02 09:55:30 -05003885 final SettingsState globalSettings = getGlobalSettingsLocked();
3886 final String oldValue = globalSettings.getSettingLocked(
3887 Global.CHARGING_STARTED_SOUND).getValue();
3888 final String oldDefault = getContext().getResources().getString(
3889 R.string.def_wireless_charging_started_sound);
3890 if (TextUtils.equals(null, oldValue)
3891 || TextUtils.equals(oldValue, oldDefault)) {
3892 final String defaultValue = getContext().getResources().getString(
3893 R.string.def_charging_started_sound);
3894 if (!TextUtils.isEmpty(defaultValue)) {
3895 globalSettings.insertSettingLocked(
3896 Settings.Global.CHARGING_STARTED_SOUND, defaultValue,
3897 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3898 }
3899
3900 }
3901 currentVersion = 156;
3902 }
3903
Beverly09da25f2018-02-26 09:17:07 -05003904 if (currentVersion == 156) {
Beverly91d0a632018-07-02 16:45:00 -04003905 // Version 157: Set a default value for zen duration,
3906 // in version 169, zen duration is moved to secure settings
Beverly09da25f2018-02-26 09:17:07 -05003907 final SettingsState globalSettings = getGlobalSettingsLocked();
3908 final Setting currentSetting = globalSettings.getSettingLocked(
3909 Global.ZEN_DURATION);
3910 if (currentSetting.isNull()) {
3911 String defaultZenDuration = Integer.toString(getContext()
3912 .getResources().getInteger(R.integer.def_zen_duration));
3913 globalSettings.insertSettingLocked(
3914 Global.ZEN_DURATION, defaultZenDuration,
3915 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3916 }
Beverly09da25f2018-02-26 09:17:07 -05003917 currentVersion = 157;
3918 }
Annie Mengd069a882018-03-13 15:31:40 +00003919
3920 if (currentVersion == 157) {
3921 // Version 158: Set default value for BACKUP_AGENT_TIMEOUT_PARAMETERS.
3922 final SettingsState globalSettings = getGlobalSettingsLocked();
3923 final String oldValue = globalSettings.getSettingLocked(
3924 Settings.Global.BACKUP_AGENT_TIMEOUT_PARAMETERS).getValue();
3925 if (TextUtils.equals(null, oldValue)) {
3926 final String defaultValue = getContext().getResources().getString(
3927 R.string.def_backup_agent_timeout_parameters);
3928 if (!TextUtils.isEmpty(defaultValue)) {
3929 globalSettings.insertSettingLocked(
3930 Settings.Global.BACKUP_AGENT_TIMEOUT_PARAMETERS, defaultValue,
3931 null, true,
3932 SettingsState.SYSTEM_PACKAGE_NAME);
3933 }
3934 }
3935 currentVersion = 158;
3936 }
Roshan Pius9c396672018-03-02 14:54:13 -08003937
3938 if (currentVersion == 158) {
3939 // Remove setting that specifies wifi bgscan throttling params
3940 getGlobalSettingsLocked().deleteSettingLocked(
3941 "wifi_scan_background_throttle_interval_ms");
3942 getGlobalSettingsLocked().deleteSettingLocked(
3943 "wifi_scan_background_throttle_package_whitelist");
3944 currentVersion = 159;
3945 }
3946
Pavel Grafovc5c97302018-03-19 13:37:15 +00003947 if (currentVersion == 159) {
3948 // Version 160: Hiding notifications from the lockscreen is only available as
3949 // primary user option, profiles can only make them redacted. If a profile was
3950 // configured to not show lockscreen notifications, ensure that at the very
3951 // least these will be come hidden.
3952 if (mUserManager.isManagedProfile(userId)) {
3953 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3954 Setting showNotifications = secureSettings.getSettingLocked(
3955 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
3956 // The default value is "1", check if user has turned it off.
3957 if ("0".equals(showNotifications.getValue())) {
3958 secureSettings.insertSettingLocked(
3959 Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, "0",
3960 null /* tag */, false /* makeDefault */,
3961 SettingsState.SYSTEM_PACKAGE_NAME);
3962 }
3963 // The setting is no longer valid for managed profiles, it should be
3964 // treated as if it was set to "1".
3965 secureSettings.deleteSettingLocked(Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
3966 }
3967 currentVersion = 160;
3968 }
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08003969
3970 if (currentVersion == 160) {
3971 // Version 161: Set the default value for
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08003972 // MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY and
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08003973 // SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT
3974 final SettingsState globalSettings = getGlobalSettingsLocked();
3975
3976 String oldValue = globalSettings.getSettingLocked(
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08003977 Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY).getValue();
3978 if (TextUtils.equals(null, oldValue)) {
3979 globalSettings.insertSettingLocked(
3980 Settings.Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY,
3981 Integer.toString(getContext().getResources().getInteger(
3982 R.integer.def_max_sound_trigger_detection_service_ops_per_day)),
3983 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3984 }
3985
3986 oldValue = globalSettings.getSettingLocked(
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08003987 Global.SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT).getValue();
3988 if (TextUtils.equals(null, oldValue)) {
3989 globalSettings.insertSettingLocked(
3990 Settings.Global.SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT,
3991 Integer.toString(getContext().getResources().getInteger(
3992 R.integer.def_sound_trigger_detection_service_op_timeout)),
3993 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3994 }
3995 currentVersion = 161;
3996 }
3997
Mike Digman55272862018-02-20 14:35:17 -08003998 if (currentVersion == 161) {
3999 // Version 161: Add a gesture for silencing phones
4000 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4001 final Setting currentSetting = secureSettings.getSettingLocked(
4002 Secure.VOLUME_HUSH_GESTURE);
4003 if (currentSetting.isNull()) {
4004 secureSettings.insertSettingLocked(
4005 Secure.VOLUME_HUSH_GESTURE,
4006 Integer.toString(Secure.VOLUME_HUSH_VIBRATE),
4007 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4008 }
4009
4010 currentVersion = 162;
4011 }
4012
Julia Reynoldsc4e5ecf2018-04-11 11:33:32 -04004013 if (currentVersion == 162) {
Julia Reynolds76bfa602018-04-23 09:38:47 -04004014 // Version 162: REMOVED: Add a gesture for silencing phones
Julia Reynoldsc4e5ecf2018-04-11 11:33:32 -04004015 currentVersion = 163;
4016 }
4017
Philip P. Moltmanncb58a832018-04-16 09:19:42 -07004018 if (currentVersion == 163) {
4019 // Version 163: Update default value of
4020 // MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY from old to new default
4021 final SettingsState settings = getGlobalSettingsLocked();
4022 final Setting currentSetting = settings.getSettingLocked(
4023 Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY);
4024 if (currentSetting.isDefaultFromSystem()) {
4025 settings.insertSettingLocked(
4026 Settings.Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY,
4027 Integer.toString(getContext().getResources().getInteger(
4028 R.integer
4029 .def_max_sound_trigger_detection_service_ops_per_day)),
4030 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4031 }
4032
4033 currentVersion = 164;
4034 }
4035
Julia Reynolds76bfa602018-04-23 09:38:47 -04004036 if (currentVersion == 164) {
Julia Reynolds24836c52018-06-06 14:36:03 -04004037 // Version 164: REMOVED: show zen upgrade notification
Julia Reynolds76bfa602018-04-23 09:38:47 -04004038 currentVersion = 165;
4039 }
4040
Beverly301e92a2018-04-27 09:43:05 -04004041 if (currentVersion == 165) {
Beverly91d0a632018-07-02 16:45:00 -04004042 // Version 165: MOVED: Show zen settings suggestion and zen updated settings
4043 // moved to secure settings and are set in version 169
Beverly301e92a2018-04-27 09:43:05 -04004044 currentVersion = 166;
4045 }
4046
Beverly38fcfd02018-05-18 17:33:40 -04004047 if (currentVersion == 166) {
4048 // Version 166: add default values for hush gesture used and manual ringer
4049 // toggle
4050 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4051 Setting currentHushUsedSetting = secureSettings.getSettingLocked(
4052 Secure.HUSH_GESTURE_USED);
4053 if (currentHushUsedSetting.isNull()) {
4054 secureSettings.insertSettingLocked(
4055 Settings.Secure.HUSH_GESTURE_USED, "0", null, true,
4056 SettingsState.SYSTEM_PACKAGE_NAME);
4057 }
4058
4059 Setting currentRingerToggleCountSetting = secureSettings.getSettingLocked(
4060 Secure.MANUAL_RINGER_TOGGLE_COUNT);
4061 if (currentRingerToggleCountSetting.isNull()) {
4062 secureSettings.insertSettingLocked(
4063 Settings.Secure.MANUAL_RINGER_TOGGLE_COUNT, "0", null, true,
4064 SettingsState.SYSTEM_PACKAGE_NAME);
4065 }
4066 currentVersion = 167;
4067 }
4068
Beverly155c9d22018-05-23 18:03:23 -04004069 if (currentVersion == 167) {
Beverly91d0a632018-07-02 16:45:00 -04004070 // Version 167: MOVED - Settings.Global.CHARGING_VIBRATION_ENABLED moved to
4071 // Settings.Secure.CHARGING_VIBRATION_ENABLED, set in version 170
Beverly155c9d22018-05-23 18:03:23 -04004072 currentVersion = 168;
4073 }
4074
Michael Wright0e9eeee2018-05-26 00:31:20 +01004075 if (currentVersion == 168) {
4076 // Version 168: by default, vibrate for phone calls
4077 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4078 final Setting currentSetting = systemSettings.getSettingLocked(
4079 Settings.System.VIBRATE_WHEN_RINGING);
4080 if (currentSetting.isNull()) {
4081 systemSettings.insertSettingLocked(
4082 Settings.System.VIBRATE_WHEN_RINGING,
4083 getContext().getResources().getBoolean(
4084 R.bool.def_vibrate_when_ringing) ? "1" : "0",
4085 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4086 }
4087 currentVersion = 169;
4088 }
4089
Nadav Barf9f115d2018-06-24 10:06:50 +03004090 if (currentVersion == 169) {
Beverly91d0a632018-07-02 16:45:00 -04004091 // Version 169: Set the default value for Secure Settings ZEN_DURATION,
4092 // SHOW_ZEN_SETTINGS_SUGGESTION, ZEN_SETTINGS_UPDATE and
4093 // ZEN_SETTINGS_SUGGESTION_VIEWED
Nadav Barf9f115d2018-06-24 10:06:50 +03004094
Beverly91d0a632018-07-02 16:45:00 -04004095 final SettingsState globalSettings = getGlobalSettingsLocked();
4096 final Setting globalZenDuration = globalSettings.getSettingLocked(
4097 Global.ZEN_DURATION);
4098
4099 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4100 final Setting secureZenDuration = secureSettings.getSettingLocked(
4101 Secure.ZEN_DURATION);
4102
4103 // ZEN_DURATION
4104 if (!globalZenDuration.isNull()) {
4105 secureSettings.insertSettingLocked(
4106 Secure.ZEN_DURATION, globalZenDuration.getValue(), null, false,
4107 SettingsState.SYSTEM_PACKAGE_NAME);
4108
4109 // set global zen duration setting to null since it's deprecated
4110 globalSettings.insertSettingLocked(
4111 Global.ZEN_DURATION, null, null, true,
4112 SettingsState.SYSTEM_PACKAGE_NAME);
4113 } else if (secureZenDuration.isNull()) {
4114 String defaultZenDuration = Integer.toString(getContext()
4115 .getResources().getInteger(R.integer.def_zen_duration));
4116 secureSettings.insertSettingLocked(
4117 Secure.ZEN_DURATION, defaultZenDuration, null, true,
4118 SettingsState.SYSTEM_PACKAGE_NAME);
Nadav Barf9f115d2018-06-24 10:06:50 +03004119 }
Beverly91d0a632018-07-02 16:45:00 -04004120
4121 // SHOW_ZEN_SETTINGS_SUGGESTION
4122 final Setting currentShowZenSettingSuggestion = secureSettings.getSettingLocked(
4123 Secure.SHOW_ZEN_SETTINGS_SUGGESTION);
4124 if (currentShowZenSettingSuggestion.isNull()) {
4125 secureSettings.insertSettingLocked(
4126 Secure.SHOW_ZEN_SETTINGS_SUGGESTION, "1",
4127 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4128 }
4129
4130 // ZEN_SETTINGS_UPDATED
4131 final Setting currentUpdatedSetting = secureSettings.getSettingLocked(
4132 Secure.ZEN_SETTINGS_UPDATED);
4133 if (currentUpdatedSetting.isNull()) {
4134 secureSettings.insertSettingLocked(
4135 Secure.ZEN_SETTINGS_UPDATED, "0",
4136 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4137 }
4138
4139 // ZEN_SETTINGS_SUGGESTION_VIEWED
4140 final Setting currentSettingSuggestionViewed = secureSettings.getSettingLocked(
4141 Secure.ZEN_SETTINGS_SUGGESTION_VIEWED);
4142 if (currentSettingSuggestionViewed.isNull()) {
4143 secureSettings.insertSettingLocked(
4144 Secure.ZEN_SETTINGS_SUGGESTION_VIEWED, "0",
4145 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4146 }
4147
Nadav Barf9f115d2018-06-24 10:06:50 +03004148 currentVersion = 170;
4149 }
4150
Beverly91d0a632018-07-02 16:45:00 -04004151 if (currentVersion == 170) {
4152 // Version 170: Set the default value for Secure Settings:
4153 // CHARGING_SOUNDS_ENABLED and CHARGING_VIBRATION_ENABLED
4154
4155 final SettingsState globalSettings = getGlobalSettingsLocked();
4156 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4157
4158 // CHARGING_SOUNDS_ENABLED
4159 final Setting globalChargingSoundEnabled = globalSettings.getSettingLocked(
4160 Global.CHARGING_SOUNDS_ENABLED);
4161 final Setting secureChargingSoundsEnabled = secureSettings.getSettingLocked(
4162 Secure.CHARGING_SOUNDS_ENABLED);
4163
4164 if (!globalChargingSoundEnabled.isNull()) {
Beverlyff9c5872019-01-02 15:44:00 -05004165 if (secureChargingSoundsEnabled.isNull()) {
4166 secureSettings.insertSettingLocked(
4167 Secure.CHARGING_SOUNDS_ENABLED,
4168 globalChargingSoundEnabled.getValue(), null, false,
4169 SettingsState.SYSTEM_PACKAGE_NAME);
4170 }
Beverly91d0a632018-07-02 16:45:00 -04004171
4172 // set global charging_sounds_enabled setting to null since it's deprecated
4173 globalSettings.insertSettingLocked(
4174 Global.CHARGING_SOUNDS_ENABLED, null, null, true,
4175 SettingsState.SYSTEM_PACKAGE_NAME);
4176 } else if (secureChargingSoundsEnabled.isNull()) {
4177 String defChargingSoundsEnabled = getContext().getResources()
4178 .getBoolean(R.bool.def_charging_sounds_enabled) ? "1" : "0";
4179 secureSettings.insertSettingLocked(
4180 Secure.CHARGING_SOUNDS_ENABLED, defChargingSoundsEnabled, null,
4181 true, SettingsState.SYSTEM_PACKAGE_NAME);
4182 }
4183
4184 // CHARGING_VIBRATION_ENABLED
4185 final Setting secureChargingVibrationEnabled = secureSettings.getSettingLocked(
4186 Secure.CHARGING_VIBRATION_ENABLED);
4187
4188 if (secureChargingVibrationEnabled.isNull()) {
4189 String defChargingVibrationEnabled = getContext().getResources()
4190 .getBoolean(R.bool.def_charging_vibration_enabled) ? "1" : "0";
4191 secureSettings.insertSettingLocked(
4192 Secure.CHARGING_VIBRATION_ENABLED, defChargingVibrationEnabled,
4193 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4194 }
4195
4196 currentVersion = 171;
4197 }
4198
Nadav Bar8bc8c9e2018-09-12 15:41:51 +03004199 if (currentVersion == 171) {
4200 // Version 171: by default, add STREAM_VOICE_CALL to list of streams that can
4201 // be muted.
4202 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4203 final Setting currentSetting = systemSettings.getSettingLocked(
4204 Settings.System.MUTE_STREAMS_AFFECTED);
4205 if (!currentSetting.isNull()) {
4206 try {
4207 int currentSettingIntegerValue = Integer.parseInt(
4208 currentSetting.getValue());
4209 if ((currentSettingIntegerValue
4210 & (1 << AudioManager.STREAM_VOICE_CALL)) == 0) {
4211 systemSettings.insertSettingLocked(
4212 Settings.System.MUTE_STREAMS_AFFECTED,
4213 Integer.toString(
4214 currentSettingIntegerValue
4215 | (1 << AudioManager.STREAM_VOICE_CALL)),
4216 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4217 }
4218 } catch (NumberFormatException e) {
4219 // remove the setting in case it is not a valid integer
4220 Slog.w("Failed to parse integer value of MUTE_STREAMS_AFFECTED"
4221 + "setting, removing setting", e);
4222 systemSettings.deleteSettingLocked(
4223 Settings.System.MUTE_STREAMS_AFFECTED);
4224 }
4225
4226 }
4227 currentVersion = 172;
4228 }
4229
Soonil Nagarkar42da1b12019-01-22 11:29:27 -08004230 if (currentVersion == 172) {
4231 // Version 172: Set the default value for Secure Settings: LOCATION_MODE
4232
4233 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4234
4235 final Setting locationMode = secureSettings.getSettingLocked(
4236 Secure.LOCATION_MODE);
4237
4238 if (locationMode.isNull()) {
4239 final Setting locationProvidersAllowed = secureSettings.getSettingLocked(
4240 Secure.LOCATION_PROVIDERS_ALLOWED);
4241
Soonil Nagarkar4ee3ac22019-02-08 19:19:24 -08004242 final int defLocationMode;
4243 if (locationProvidersAllowed.isNull()) {
4244 defLocationMode = getContext().getResources().getInteger(
4245 R.integer.def_location_mode);
4246 } else {
4247 defLocationMode =
4248 !TextUtils.isEmpty(locationProvidersAllowed.getValue())
4249 ? Secure.LOCATION_MODE_ON
4250 : Secure.LOCATION_MODE_OFF;
4251 }
Soonil Nagarkar42da1b12019-01-22 11:29:27 -08004252 secureSettings.insertSettingLocked(
Soonil Nagarkar4ee3ac22019-02-08 19:19:24 -08004253 Secure.LOCATION_MODE, Integer.toString(defLocationMode),
Soonil Nagarkar42da1b12019-01-22 11:29:27 -08004254 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4255 }
4256
4257 currentVersion = 173;
4258 }
4259
Beverly4179f992019-02-08 11:34:16 -05004260 if (currentVersion == 173) {
4261 // Version 173: Set the default value for Secure Settings: NOTIFICATION_BUBBLES
4262
4263 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4264
4265 final Setting bubblesSetting = secureSettings.getSettingLocked(
4266 Secure.NOTIFICATION_BUBBLES);
4267
4268 if (bubblesSetting.isNull()) {
4269 secureSettings.insertSettingLocked(Secure.NOTIFICATION_BUBBLES,
4270 getContext().getResources().getBoolean(
4271 R.bool.def_notification_bubbles) ? "1" : "0", null,
4272 true, SettingsState.SYSTEM_PACKAGE_NAME);
4273 }
4274
4275 currentVersion = 174;
4276 }
4277
Yiwen Chen0305b572019-03-05 11:26:59 -08004278 if (currentVersion == 174) {
4279 // Version 174: Set the default value for Global Settings: APPLY_RAMPING_RINGER
4280
4281 final SettingsState globalSettings = getGlobalSettingsLocked();
4282
4283 Setting currentRampingRingerSetting = globalSettings.getSettingLocked(
4284 Settings.Global.APPLY_RAMPING_RINGER);
4285 if (currentRampingRingerSetting.isNull()) {
4286 globalSettings.insertSettingLocked(
4287 Settings.Global.APPLY_RAMPING_RINGER,
4288 getContext().getResources().getBoolean(
4289 R.bool.def_apply_ramping_ringer) ? "1" : "0", null,
4290 true, SettingsState.SYSTEM_PACKAGE_NAME);
4291 }
4292
4293 currentVersion = 175;
4294 }
4295
wilsonshih5d999e22019-03-20 11:50:42 +08004296 if (currentVersion == 175) {
4297 // Version 175: Set the default value for System Settings:
4298 // RING_VIBRATION_INTENSITY. If the notification vibration intensity has been
4299 // set and ring vibration intensity hasn't, the ring vibration intensity should
4300 // followed notification vibration intensity.
4301
4302 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4303
4304 Setting notificationVibrationIntensity = systemSettings.getSettingLocked(
4305 Settings.System.NOTIFICATION_VIBRATION_INTENSITY);
4306
4307 Setting ringVibrationIntensity = systemSettings.getSettingLocked(
4308 Settings.System.RING_VIBRATION_INTENSITY);
4309
4310 if (!notificationVibrationIntensity.isNull()
4311 && ringVibrationIntensity.isNull()) {
4312 systemSettings.insertSettingLocked(
4313 Settings.System.RING_VIBRATION_INTENSITY,
4314 notificationVibrationIntensity.getValue(),
4315 null , true, SettingsState.SYSTEM_PACKAGE_NAME);
4316 }
4317
4318 currentVersion = 176;
4319 }
4320
Winson Chungd9f2fb32019-03-05 11:10:12 -08004321 if (currentVersion == 176) {
4322 // Version 176: Migrate the existing swipe up setting into the resource overlay
4323 // for the navigation bar interaction mode.
4324
4325 final IOverlayManager overlayManager = IOverlayManager.Stub.asInterface(
4326 ServiceManager.getService(Context.OVERLAY_SERVICE));
4327 int navBarMode = -1;
4328
4329 // Migrate the swipe up setting only if it is set
4330 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4331 final Setting swipeUpSetting = secureSettings.getSettingLocked(
4332 Secure.SWIPE_UP_TO_SWITCH_APPS_ENABLED);
4333 if (swipeUpSetting != null && !swipeUpSetting.isNull()) {
4334 navBarMode = swipeUpSetting.getValue().equals("1")
4335 ? NAV_BAR_MODE_2BUTTON
4336 : NAV_BAR_MODE_3BUTTON;
4337 }
4338
4339 // Temporary: Only for migration for dogfooders, to be removed
4340 try {
4341 final OverlayInfo info = overlayManager.getOverlayInfo(
4342 "com.android.internal.experiment.navbar.type.inset",
4343 UserHandle.USER_CURRENT);
4344 if (info != null && info.isEnabled()) {
4345 navBarMode = NAV_BAR_MODE_GESTURAL;
4346 }
4347 } catch (RemoteException e) {
4348 // Ingore, fall through
4349 }
4350
4351 if (navBarMode != -1) {
4352 try {
4353 overlayManager.setEnabled(NAV_BAR_MODE_3BUTTON_OVERLAY,
4354 navBarMode == NAV_BAR_MODE_3BUTTON,
4355 UserHandle.USER_CURRENT);
4356 overlayManager.setEnabled(NAV_BAR_MODE_2BUTTON_OVERLAY,
4357 navBarMode == NAV_BAR_MODE_2BUTTON,
4358 UserHandle.USER_CURRENT);
4359 overlayManager.setEnabled(NAV_BAR_MODE_GESTURAL_OVERLAY,
4360 navBarMode == NAV_BAR_MODE_GESTURAL,
4361 UserHandle.USER_CURRENT);
4362 } catch (RemoteException e) {
4363 throw new IllegalStateException(
4364 "Failed to set nav bar interaction mode overlay");
4365 }
4366 }
4367
4368 currentVersion = 177;
4369 }
4370
4371
Felipe Lemeff355092017-04-03 12:55:02 -07004372 // vXXX: Add new settings above this point.
4373
Dan Sandler71f85e92016-07-20 13:46:05 -04004374 if (currentVersion != newVersion) {
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07004375 Slog.wtf("SettingsProvider", "warning: upgrading settings database to version "
Dan Sandler71f85e92016-07-20 13:46:05 -04004376 + newVersion + " left it at "
Stephen Chen5d0922f2017-03-27 10:28:04 -07004377 + currentVersion +
4378 " instead; this is probably a bug. Did you update SETTINGS_VERSION?",
4379 new Throwable());
Dan Sandler71f85e92016-07-20 13:46:05 -04004380 if (DEBUG) {
4381 throw new RuntimeException("db upgrade error");
4382 }
4383 }
4384
Jeff Brown503cffc2015-03-26 18:08:51 -07004385 // Return the current version.
4386 return currentVersion;
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -08004387 }
4388 }
Svet Ganov13701552017-02-23 12:45:17 -08004389
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004390 private void ensureLegacyDefaultValueAndSystemSetUpdatedLocked(SettingsState settings,
4391 int userId) {
Svet Ganov13701552017-02-23 12:45:17 -08004392 List<String> names = settings.getSettingNamesLocked();
4393 final int nameCount = names.size();
4394 for (int i = 0; i < nameCount; i++) {
4395 String name = names.get(i);
4396 Setting setting = settings.getSettingLocked(name);
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004397
4398 // In the upgrade case we pretend the call is made from the app
4399 // that made the last change to the setting to properly determine
4400 // whether the call has been made by a system component.
4401 int callingUid = -1;
4402 try {
4403 callingUid = mPackageManager.getPackageUid(setting.getPackageName(), 0, userId);
4404 } catch (RemoteException e) {
4405 /* ignore - handled below */
4406 }
4407 if (callingUid < 0) {
4408 Slog.e(LOG_TAG, "Unknown package: " + setting.getPackageName());
4409 continue;
4410 }
4411 try {
4412 final boolean systemSet = SettingsState.isSystemPackage(getContext(),
4413 setting.getPackageName(), callingUid);
Svet Ganov13701552017-02-23 12:45:17 -08004414 if (systemSet) {
4415 settings.insertSettingLocked(name, setting.getValue(),
4416 setting.getTag(), true, setting.getPackageName());
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004417 } else if (setting.getDefaultValue() != null && setting.isDefaultFromSystem()) {
4418 // We had a bug where changes by non-system packages were marked
4419 // as system made and as a result set as the default. Therefore, if
4420 // the package changed the setting last is not a system one but the
4421 // setting is marked as its default coming from the system we clear
4422 // the default and clear the system set flag.
4423 settings.resetSettingDefaultValueLocked(name);
Svet Ganov13701552017-02-23 12:45:17 -08004424 }
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004425 } catch (IllegalStateException e) {
4426 // If the package goes over its quota during the upgrade, don't
4427 // crash but just log the error as the system does the upgrade.
4428 Slog.e(LOG_TAG, "Error upgrading setting: " + setting.getName(), e);
4429
Svet Ganov13701552017-02-23 12:45:17 -08004430 }
4431 }
4432 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08004433 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004434}