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