blob: 4d71e72b59d6fa95e8e5357298f41a622d2a8e0e [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_OVERLAY;
yuemingw1d13eae2018-01-30 17:27:54 +000024
Svetoslav683914b2015-01-15 14:22:26 -080025import android.Manifest;
Eugene Suslad72c3972016-12-27 15:49:30 -080026import android.annotation.NonNull;
Kweku Adamsb0886f32017-10-31 15:32:09 -070027import android.annotation.Nullable;
Christopher Tated5fe1472012-09-10 15:48:38 -070028import android.app.ActivityManager;
Xiaohui Chen43765b72015-08-31 10:57:33 -070029import android.app.AppGlobals;
Christopher Tate45281862010-03-05 15:46:30 -080030import android.app.backup.BackupManager;
Christopher Tate06efb532012-08-24 15:29:27 -070031import android.content.BroadcastReceiver;
Ruben Brunk98576cf2016-03-07 18:54:28 -080032import android.content.ComponentName;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070033import android.content.ContentProvider;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070034import android.content.ContentValues;
35import android.content.Context;
Christopher Tate06efb532012-08-24 15:29:27 -070036import android.content.Intent;
37import android.content.IntentFilter;
Winson Chungd9f2fb32019-03-05 11:10:12 -080038import android.content.om.IOverlayManager;
Svetoslav683914b2015-01-15 14:22:26 -080039import android.content.pm.ApplicationInfo;
Xiaohui Chen43765b72015-08-31 10:57:33 -070040import android.content.pm.IPackageManager;
Svetoslav683914b2015-01-15 14:22:26 -080041import android.content.pm.PackageInfo;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070042import android.content.pm.PackageManager;
Christopher Tate38e7a602013-09-03 16:57:34 -070043import android.content.pm.UserInfo;
Chad Brubaker20e0dc32017-04-28 18:24:55 -070044import android.content.res.Resources;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070045import android.database.Cursor;
Svetoslav683914b2015-01-15 14:22:26 -080046import android.database.MatrixCursor;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070047import android.database.sqlite.SQLiteDatabase;
48import android.database.sqlite.SQLiteQueryBuilder;
Svetoslav683914b2015-01-15 14:22:26 -080049import android.hardware.camera2.utils.ArrayUtils;
John Spurlocke11ae112015-05-11 16:09:03 -040050import android.media.AudioManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070051import android.net.Uri;
Christopher Tate06efb532012-08-24 15:29:27 -070052import android.os.Binder;
Svetoslav683914b2015-01-15 14:22:26 -080053import android.os.Build;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080054import android.os.Bundle;
Amith Yamasani5cdf7f52013-06-27 15:12:01 -070055import android.os.DropBoxManager;
Svetoslav683914b2015-01-15 14:22:26 -080056import android.os.Environment;
Svetoslav7e0683b2015-08-03 16:02:52 -070057import android.os.Handler;
Svet Ganova8f90262016-05-10 08:44:48 -070058import android.os.HandlerThread;
Svetoslav7e0683b2015-08-03 16:02:52 -070059import android.os.Looper;
60import android.os.Message;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070061import android.os.ParcelFileDescriptor;
Christopher Tate0da13572013-10-13 17:34:49 -070062import android.os.Process;
Xiaohui Chen43765b72015-08-31 10:57:33 -070063import android.os.RemoteException;
Jeff Sharkey413573a2016-02-22 17:52:45 -070064import android.os.SELinux;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -070065import android.os.ServiceManager;
Christopher Tate06efb532012-08-24 15:29:27 -070066import android.os.UserHandle;
67import android.os.UserManager;
Svet Ganov53a441c2016-04-19 19:38:00 -070068import android.os.UserManagerInternal;
Matt Pape1278d1c2018-12-11 13:03:49 -080069import android.provider.DeviceConfig;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070070import android.provider.Settings;
Narayan Kamath94bcdbc2017-07-17 15:32:53 +010071import android.provider.Settings.Global;
Makoto Onuki0000d322017-11-28 16:31:47 -080072import android.provider.Settings.Secure;
Al Sutton91f89d02019-08-16 12:56:57 +010073import android.provider.settings.validators.SystemSettingsValidators;
Al Suttonb0067fb2019-08-16 10:34:46 +010074import android.provider.settings.validators.Validator;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070075import android.text.TextUtils;
Andre Lago3fa139c2016-08-04 13:53:44 +010076import android.util.ArrayMap;
Svetoslav683914b2015-01-15 14:22:26 -080077import android.util.ArraySet;
Christopher Tate06efb532012-08-24 15:29:27 -070078import android.util.Slog;
79import android.util.SparseArray;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -070080import android.util.SparseBooleanArray;
Eugene Suslad72c3972016-12-27 15:49:30 -080081import android.util.proto.ProtoOutputStream;
John Spurlocke11ae112015-05-11 16:09:03 -040082
Svetoslav683914b2015-01-15 14:22:26 -080083import com.android.internal.annotations.GuardedBy;
84import com.android.internal.content.PackageMonitor;
85import com.android.internal.os.BackgroundThread;
Suprabh Shukla269c11e2015-12-02 16:51:16 -080086import com.android.providers.settings.SettingsState.Setting;
Svet Ganov53a441c2016-04-19 19:38:00 -070087import com.android.server.LocalServices;
Ruben Brunk98576cf2016-03-07 18:54:28 -080088import com.android.server.SystemConfig;
John Spurlocke11ae112015-05-11 16:09:03 -040089
Irina Dumitrescue3696872019-01-09 16:07:59 +000090import com.google.android.collect.Sets;
91
Neil Fuller3447fb42019-04-08 22:03:14 +010092import libcore.util.HexEncoding;
93
Svetoslav683914b2015-01-15 14:22:26 -080094import java.io.File;
Svetoslavb505ccc2015-02-17 12:41:04 -080095import java.io.FileDescriptor;
Svetoslav683914b2015-01-15 14:22:26 -080096import java.io.FileNotFoundException;
Svetoslavb505ccc2015-02-17 12:41:04 -080097import java.io.PrintWriter;
Mark Rathjen7599f132017-01-23 14:15:54 -080098import java.nio.ByteBuffer;
99import java.security.InvalidKeyException;
Mark Rathjend891f012017-01-19 04:10:37 +0000100import java.security.NoSuchAlgorithmException;
Svetoslav683914b2015-01-15 14:22:26 -0800101import java.security.SecureRandom;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700102import java.util.ArrayList;
Svetoslav683914b2015-01-15 14:22:26 -0800103import java.util.Arrays;
Robin Lee7af9a742017-02-20 14:47:30 +0000104import java.util.Collection;
Matt Pape6bfc62e2018-11-28 13:16:03 -0800105import java.util.HashMap;
Mark Rathjend891f012017-01-19 04:10:37 +0000106import java.util.HashSet;
Svetoslav683914b2015-01-15 14:22:26 -0800107import java.util.List;
Andre Lago3fa139c2016-08-04 13:53:44 +0100108import java.util.Map;
Svetoslav683914b2015-01-15 14:22:26 -0800109import java.util.Set;
110import java.util.regex.Pattern;
yuemingw1d13eae2018-01-30 17:27:54 +0000111
Mark Rathjen7599f132017-01-23 14:15:54 -0800112import javax.crypto.Mac;
113import javax.crypto.spec.SecretKeySpec;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700114
Svetoslav Ganove080da92016-12-21 17:10:35 -0800115
Svetoslav683914b2015-01-15 14:22:26 -0800116/**
117 * <p>
118 * This class is a content provider that publishes the system settings.
119 * It can be accessed via the content provider APIs or via custom call
120 * commands. The latter is a bit faster and is the preferred way to access
121 * the platform settings.
122 * </p>
123 * <p>
124 * There are three settings types, global (with signature level protection
125 * and shared across users), secure (with signature permission level
126 * protection and per user), and system (with dangerous permission level
127 * protection and per user). Global settings are stored under the device owner.
128 * Each of these settings is represented by a {@link
129 * com.android.providers.settings.SettingsState} object mapped to an integer
130 * key derived from the setting type in the most significant bits and user
131 * id in the least significant bits. Settings are synchronously loaded on
132 * instantiation of a SettingsState and asynchronously persisted on mutation.
133 * Settings are stored in the user specific system directory.
134 * </p>
135 * <p>
136 * Apps targeting APIs Lollipop MR1 and lower can add custom settings entries
137 * and get a warning. Targeting higher API version prohibits this as the
138 * system settings are not a place for apps to save their state. When a package
139 * is removed the settings it added are deleted. Apps cannot delete system
140 * settings added by the platform. System settings values are validated to
141 * ensure the clients do not put bad values. Global and secure settings are
142 * changed only by trusted parties, therefore no validation is performed. Also
143 * there is a limit on the amount of app specific settings that can be added
144 * to prevent unlimited growth of the system process memory footprint.
145 * </p>
146 */
147@SuppressWarnings("deprecation")
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700148public class SettingsProvider extends ContentProvider {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700149 static final boolean DEBUG = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700150
Svetoslav Ganov264c7a92016-08-24 17:31:14 -0700151 private static final boolean DROP_DATABASE_ON_MIGRATION = true;
Svetoslav683914b2015-01-15 14:22:26 -0800152
153 private static final String LOG_TAG = "SettingsProvider";
Christopher Tate0da13572013-10-13 17:34:49 -0700154
Christopher Tate06efb532012-08-24 15:29:27 -0700155 private static final String TABLE_SYSTEM = "system";
156 private static final String TABLE_SECURE = "secure";
157 private static final String TABLE_GLOBAL = "global";
Matt Pape6bfc62e2018-11-28 13:16:03 -0800158 private static final String TABLE_CONFIG = "config";
Svetoslav683914b2015-01-15 14:22:26 -0800159
160 // Old tables no longer exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 private static final String TABLE_FAVORITES = "favorites";
162 private static final String TABLE_OLD_FAVORITES = "old_favorites";
Svetoslav683914b2015-01-15 14:22:26 -0800163 private static final String TABLE_BLUETOOTH_DEVICES = "bluetooth_devices";
164 private static final String TABLE_BOOKMARKS = "bookmarks";
165 private static final String TABLE_ANDROID_METADATA = "android_metadata";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166
Svetoslav683914b2015-01-15 14:22:26 -0800167 // The set of removed legacy tables.
168 private static final Set<String> REMOVED_LEGACY_TABLES = new ArraySet<>();
Christopher Tate06efb532012-08-24 15:29:27 -0700169 static {
Svetoslav683914b2015-01-15 14:22:26 -0800170 REMOVED_LEGACY_TABLES.add(TABLE_FAVORITES);
171 REMOVED_LEGACY_TABLES.add(TABLE_OLD_FAVORITES);
172 REMOVED_LEGACY_TABLES.add(TABLE_BLUETOOTH_DEVICES);
173 REMOVED_LEGACY_TABLES.add(TABLE_BOOKMARKS);
174 REMOVED_LEGACY_TABLES.add(TABLE_ANDROID_METADATA);
175 }
Christopher Tate06efb532012-08-24 15:29:27 -0700176
Svetoslav683914b2015-01-15 14:22:26 -0800177 private static final int MUTATION_OPERATION_INSERT = 1;
178 private static final int MUTATION_OPERATION_DELETE = 2;
179 private static final int MUTATION_OPERATION_UPDATE = 3;
Svetoslav Ganove080da92016-12-21 17:10:35 -0800180 private static final int MUTATION_OPERATION_RESET = 4;
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400181
Svetoslav683914b2015-01-15 14:22:26 -0800182 private static final String[] ALL_COLUMNS = new String[] {
183 Settings.NameValueTable._ID,
184 Settings.NameValueTable.NAME,
185 Settings.NameValueTable.VALUE
186 };
187
Makoto Onuki53f0e022017-11-29 13:51:01 -0800188 public static final int SETTINGS_TYPE_GLOBAL = SettingsState.SETTINGS_TYPE_GLOBAL;
189 public static final int SETTINGS_TYPE_SYSTEM = SettingsState.SETTINGS_TYPE_SYSTEM;
190 public static final int SETTINGS_TYPE_SECURE = SettingsState.SETTINGS_TYPE_SECURE;
191 public static final int SETTINGS_TYPE_SSAID = SettingsState.SETTINGS_TYPE_SSAID;
Matt Pape1b31a332018-10-17 09:58:28 -0700192 public static final int SETTINGS_TYPE_CONFIG = SettingsState.SETTINGS_TYPE_CONFIG;
Svet Ganov53a441c2016-04-19 19:38:00 -0700193
194 private static final Bundle NULL_SETTING_BUNDLE = Bundle.forPair(
195 Settings.NameValueTable.VALUE, null);
Christopher Tate06efb532012-08-24 15:29:27 -0700196
Nicholas Sauer3d87d1e2018-11-06 08:38:39 -0800197 public static final String RESULT_ROWS_DELETED = "result_rows_deleted";
Nicholas Sauer72500532018-11-21 10:30:58 -0800198 public static final String RESULT_SETTINGS_LIST = "result_settings_list";
Nicholas Sauer3d87d1e2018-11-06 08:38:39 -0800199
Chad Brubaker20e0dc32017-04-28 18:24:55 -0700200 // Overlay specified settings whitelisted for Instant Apps
201 private static final Set<String> OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS = new ArraySet<>();
202 private static final Set<String> OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS = new ArraySet<>();
203 private static final Set<String> OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS = new ArraySet<>();
204
205 static {
206 for (String name : Resources.getSystem().getStringArray(
207 com.android.internal.R.array.config_allowedGlobalInstantAppSettings)) {
208 OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS.add(name);
209 }
210 for (String name : Resources.getSystem().getStringArray(
211 com.android.internal.R.array.config_allowedSystemInstantAppSettings)) {
212 OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS.add(name);
213 }
214 for (String name : Resources.getSystem().getStringArray(
215 com.android.internal.R.array.config_allowedSecureInstantAppSettings)) {
216 OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS.add(name);
217 }
218 }
219
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -0800220 // Changes to these global settings are synchronously persisted
221 private static final Set<String> CRITICAL_GLOBAL_SETTINGS = new ArraySet<>();
222 static {
223 CRITICAL_GLOBAL_SETTINGS.add(Settings.Global.DEVICE_PROVISIONED);
224 }
225
226 // Changes to these secure settings are synchronously persisted
227 private static final Set<String> CRITICAL_SECURE_SETTINGS = new ArraySet<>();
228 static {
229 CRITICAL_SECURE_SETTINGS.add(Settings.Secure.USER_SETUP_COMPLETE);
230 }
231
Svetoslav683914b2015-01-15 14:22:26 -0800232 // Per user secure settings that moved to the for all users global settings.
233 static final Set<String> sSecureMovedToGlobalSettings = new ArraySet<>();
234 static {
235 Settings.Secure.getMovedToGlobalSettings(sSecureMovedToGlobalSettings);
Christopher Tate06efb532012-08-24 15:29:27 -0700236 }
237
Svetoslav683914b2015-01-15 14:22:26 -0800238 // Per user system settings that moved to the for all users global settings.
239 static final Set<String> sSystemMovedToGlobalSettings = new ArraySet<>();
240 static {
241 Settings.System.getMovedToGlobalSettings(sSystemMovedToGlobalSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700242 }
243
Svetoslav683914b2015-01-15 14:22:26 -0800244 // Per user system settings that moved to the per user secure settings.
245 static final Set<String> sSystemMovedToSecureSettings = new ArraySet<>();
246 static {
247 Settings.System.getMovedToSecureSettings(sSystemMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700248 }
249
Svetoslav683914b2015-01-15 14:22:26 -0800250 // Per all users global settings that moved to the per user secure settings.
251 static final Set<String> sGlobalMovedToSecureSettings = new ArraySet<>();
252 static {
253 Settings.Global.getMovedToSecureSettings(sGlobalMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700254 }
255
Svetoslav683914b2015-01-15 14:22:26 -0800256 // Per user secure settings that are cloned for the managed profiles of the user.
257 private static final Set<String> sSecureCloneToManagedSettings = new ArraySet<>();
258 static {
259 Settings.Secure.getCloneToManagedProfileSettings(sSecureCloneToManagedSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700260 }
261
Svetoslav683914b2015-01-15 14:22:26 -0800262 // Per user system settings that are cloned for the managed profiles of the user.
263 private static final Set<String> sSystemCloneToManagedSettings = new ArraySet<>();
264 static {
265 Settings.System.getCloneToManagedProfileSettings(sSystemCloneToManagedSettings);
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400266 }
267
Andre Lago3fa139c2016-08-04 13:53:44 +0100268 // Per user system settings that are cloned from the profile's parent when a dependency
269 // in {@link Settings.Secure} is set to "1".
270 public static final Map<String, String> sSystemCloneFromParentOnDependency = new ArrayMap<>();
271 static {
272 Settings.System.getCloneFromParentOnValueSettings(sSystemCloneFromParentOnDependency);
273 }
274
Svetoslav683914b2015-01-15 14:22:26 -0800275 private final Object mLock = new Object();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700276
Svetoslav683914b2015-01-15 14:22:26 -0800277 @GuardedBy("mLock")
278 private SettingsRegistry mSettingsRegistry;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700279
Svet Ganova8f90262016-05-10 08:44:48 -0700280 @GuardedBy("mLock")
281 private HandlerThread mHandlerThread;
282
Makoto Onuki73360ab2017-03-17 11:50:13 -0700283 @GuardedBy("mLock")
284 private Handler mHandler;
285
Svetoslav7ec28e82015-05-20 17:01:10 -0700286 // We have to call in the user manager with no lock held,
287 private volatile UserManager mUserManager;
Svetoslav683914b2015-01-15 14:22:26 -0800288
yuemingw1d13eae2018-01-30 17:27:54 +0000289 private UserManagerInternal mUserManagerInternal;
290
Svetoslav7ec28e82015-05-20 17:01:10 -0700291 // We have to call in the package manager with no lock held,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700292 private volatile IPackageManager mPackageManager;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700293
Svet Ganov53a441c2016-04-19 19:38:00 -0700294 public static int makeKey(int type, int userId) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800295 return SettingsState.makeKey(type, userId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700296 }
297
298 public static int getTypeFromKey(int key) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800299 return SettingsState.getTypeFromKey(key);
Svet Ganov53a441c2016-04-19 19:38:00 -0700300 }
301
302 public static int getUserIdFromKey(int key) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800303 return SettingsState.getUserIdFromKey(key);
Svet Ganov53a441c2016-04-19 19:38:00 -0700304 }
305
306 public static String settingTypeToString(int type) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800307 return SettingsState.settingTypeToString(type);
Svet Ganov53a441c2016-04-19 19:38:00 -0700308 }
309
310 public static String keyToString(int key) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800311 return SettingsState.keyToString(key);
Svet Ganov53a441c2016-04-19 19:38:00 -0700312 }
313
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700314 @Override
315 public boolean onCreate() {
Chad Brubaker97bccee2017-01-05 15:51:41 -0800316 Settings.setInSystemServer();
Michal Karpinski2c37b082018-01-18 16:14:27 +0000317
Svetoslav683914b2015-01-15 14:22:26 -0800318 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700319 mUserManager = UserManager.get(getContext());
yuemingw1d13eae2018-01-30 17:27:54 +0000320 mUserManagerInternal = LocalServices.getService(UserManagerInternal.class);
Xiaohui Chen43765b72015-08-31 10:57:33 -0700321 mPackageManager = AppGlobals.getPackageManager();
Svet Ganova8f90262016-05-10 08:44:48 -0700322 mHandlerThread = new HandlerThread(LOG_TAG,
323 Process.THREAD_PRIORITY_BACKGROUND);
324 mHandlerThread.start();
Makoto Onuki73360ab2017-03-17 11:50:13 -0700325 mHandler = new Handler(mHandlerThread.getLooper());
Svetoslav683914b2015-01-15 14:22:26 -0800326 mSettingsRegistry = new SettingsRegistry();
327 }
Makoto Onuki73360ab2017-03-17 11:50:13 -0700328 mHandler.post(() -> {
329 registerBroadcastReceivers();
330 startWatchingUserRestrictionChanges();
331 });
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700332 ServiceManager.addService("settings", new SettingsService(this));
Matt Papeabb67892018-12-07 09:13:26 -0800333 ServiceManager.addService("device_config", new DeviceConfigService(this));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700334 return true;
335 }
336
Svetoslav683914b2015-01-15 14:22:26 -0800337 @Override
338 public Bundle call(String method, String name, Bundle args) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700339 final int requestingUserId = getRequestingUserId(args);
340 switch (method) {
Matt Pape1b31a332018-10-17 09:58:28 -0700341 case Settings.CALL_METHOD_GET_CONFIG: {
342 Setting setting = getConfigSetting(name);
343 return packageValueForCallResult(setting, isTrackingGeneration(args));
344 }
345
Svetoslav7ec28e82015-05-20 17:01:10 -0700346 case Settings.CALL_METHOD_GET_GLOBAL: {
347 Setting setting = getGlobalSetting(name);
Svet Ganov53a441c2016-04-19 19:38:00 -0700348 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav683914b2015-01-15 14:22:26 -0800349 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700350
351 case Settings.CALL_METHOD_GET_SECURE: {
Makoto Onuki0000d322017-11-28 16:31:47 -0800352 Setting setting = getSecureSetting(name, requestingUserId,
353 /*enableOverride=*/ true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700354 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700355 }
356
357 case Settings.CALL_METHOD_GET_SYSTEM: {
358 Setting setting = getSystemSetting(name, requestingUserId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700359 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700360 }
361
Matt Pape1b31a332018-10-17 09:58:28 -0700362 case Settings.CALL_METHOD_PUT_CONFIG: {
363 String value = getSettingValue(args);
Matt Pape1b31a332018-10-17 09:58:28 -0700364 final boolean makeDefault = getSettingMakeDefault(args);
Matt Papec1323dc2018-12-11 12:32:42 -0800365 insertConfigSetting(name, value, makeDefault);
Matt Pape1b31a332018-10-17 09:58:28 -0700366 break;
367 }
368
Svetoslav7ec28e82015-05-20 17:01:10 -0700369 case Settings.CALL_METHOD_PUT_GLOBAL: {
370 String value = getSettingValue(args);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800371 String tag = getSettingTag(args);
372 final boolean makeDefault = getSettingMakeDefault(args);
373 insertGlobalSetting(name, value, tag, makeDefault, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700374 break;
375 }
376
377 case Settings.CALL_METHOD_PUT_SECURE: {
378 String value = getSettingValue(args);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800379 String tag = getSettingTag(args);
380 final boolean makeDefault = getSettingMakeDefault(args);
381 insertSecureSetting(name, value, tag, makeDefault, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700382 break;
383 }
384
385 case Settings.CALL_METHOD_PUT_SYSTEM: {
386 String value = getSettingValue(args);
387 insertSystemSetting(name, value, requestingUserId);
388 break;
389 }
390
Matt Pape1b31a332018-10-17 09:58:28 -0700391 case Settings.CALL_METHOD_RESET_CONFIG: {
392 final int mode = getResetModeEnforcingPermission(args);
Matt Pape6bfc62e2018-11-28 13:16:03 -0800393 String prefix = getSettingPrefix(args);
Matt Papec1323dc2018-12-11 12:32:42 -0800394 resetConfigSetting(mode, prefix);
Matt Pape1b31a332018-10-17 09:58:28 -0700395 break;
396 }
397
Svetoslav Ganove080da92016-12-21 17:10:35 -0800398 case Settings.CALL_METHOD_RESET_GLOBAL: {
399 final int mode = getResetModeEnforcingPermission(args);
400 String tag = getSettingTag(args);
401 resetGlobalSetting(requestingUserId, mode, tag);
402 break;
403 }
404
405 case Settings.CALL_METHOD_RESET_SECURE: {
406 final int mode = getResetModeEnforcingPermission(args);
407 String tag = getSettingTag(args);
408 resetSecureSetting(requestingUserId, mode, tag);
409 break;
410 }
411
Matt Pape6bfc62e2018-11-28 13:16:03 -0800412 case Settings.CALL_METHOD_DELETE_CONFIG: {
Matt Papec1323dc2018-12-11 12:32:42 -0800413 int rows = deleteConfigSetting(name) ? 1 : 0;
Nicholas Sauer3d87d1e2018-11-06 08:38:39 -0800414 Bundle result = new Bundle();
415 result.putInt(RESULT_ROWS_DELETED, rows);
416 return result;
417 }
418
419 case Settings.CALL_METHOD_DELETE_GLOBAL: {
420 int rows = deleteGlobalSetting(name, requestingUserId, false) ? 1 : 0;
421 Bundle result = new Bundle();
422 result.putInt(RESULT_ROWS_DELETED, rows);
423 return result;
424 }
425
Matt Pape6bfc62e2018-11-28 13:16:03 -0800426 case Settings.CALL_METHOD_DELETE_SECURE: {
427 int rows = deleteSecureSetting(name, requestingUserId, false) ? 1 : 0;
428 Bundle result = new Bundle();
429 result.putInt(RESULT_ROWS_DELETED, rows);
430 return result;
431 }
432
433 case Settings.CALL_METHOD_DELETE_SYSTEM: {
434 int rows = deleteSystemSetting(name, requestingUserId) ? 1 : 0;
435 Bundle result = new Bundle();
436 result.putInt(RESULT_ROWS_DELETED, rows);
437 return result;
438 }
439
440 case Settings.CALL_METHOD_LIST_CONFIG: {
441 String prefix = getSettingPrefix(args);
442 Bundle result = new Bundle();
443 result.putSerializable(
444 Settings.NameValueTable.VALUE, (HashMap) getAllConfigFlags(prefix));
445 return result;
446 }
447
448 case Settings.CALL_METHOD_LIST_GLOBAL: {
Nicholas Sauer72500532018-11-21 10:30:58 -0800449 Bundle result = new Bundle();
450 result.putStringArrayList(RESULT_SETTINGS_LIST,
Matt Pape6bfc62e2018-11-28 13:16:03 -0800451 buildSettingsList(getAllGlobalSettings(null)));
Nicholas Sauer72500532018-11-21 10:30:58 -0800452 return result;
453 }
454
455 case Settings.CALL_METHOD_LIST_SECURE: {
456 Bundle result = new Bundle();
457 result.putStringArrayList(RESULT_SETTINGS_LIST,
458 buildSettingsList(getAllSecureSettings(requestingUserId, null)));
459 return result;
460 }
461
Matt Pape6bfc62e2018-11-28 13:16:03 -0800462 case Settings.CALL_METHOD_LIST_SYSTEM: {
Nicholas Sauer72500532018-11-21 10:30:58 -0800463 Bundle result = new Bundle();
464 result.putStringArrayList(RESULT_SETTINGS_LIST,
Matt Pape6bfc62e2018-11-28 13:16:03 -0800465 buildSettingsList(getAllSystemSettings(requestingUserId, null)));
Nicholas Sauer72500532018-11-21 10:30:58 -0800466 return result;
467 }
468
Svetoslav7ec28e82015-05-20 17:01:10 -0700469 default: {
470 Slog.w(LOG_TAG, "call() with invalid method: " + method);
471 } break;
Christopher Tate06efb532012-08-24 15:29:27 -0700472 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700473
Christopher Tate06efb532012-08-24 15:29:27 -0700474 return null;
475 }
476
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800477 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800478 public String getType(Uri uri) {
479 Arguments args = new Arguments(uri, null, null, true);
480 if (TextUtils.isEmpty(args.name)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700481 return "vnd.android.cursor.dir/" + args.table;
482 } else {
Svetoslav7ec28e82015-05-20 17:01:10 -0700483 return "vnd.android.cursor.item/" + args.table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700484 }
485 }
486
487 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800488 public Cursor query(Uri uri, String[] projection, String where, String[] whereArgs,
489 String order) {
490 if (DEBUG) {
491 Slog.v(LOG_TAG, "query() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700492 }
493
Svetoslav683914b2015-01-15 14:22:26 -0800494 Arguments args = new Arguments(uri, where, whereArgs, true);
495 String[] normalizedProjection = normalizeProjection(projection);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700496
Svetoslav683914b2015-01-15 14:22:26 -0800497 // If a legacy table that is gone, done.
498 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
499 return new MatrixCursor(normalizedProjection, 0);
500 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700501
Svetoslav7ec28e82015-05-20 17:01:10 -0700502 switch (args.table) {
503 case TABLE_GLOBAL: {
504 if (args.name != null) {
505 Setting setting = getGlobalSetting(args.name);
506 return packageSettingForQuery(setting, normalizedProjection);
507 } else {
508 return getAllGlobalSettings(projection);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700509 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700510 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700511
Svetoslav7ec28e82015-05-20 17:01:10 -0700512 case TABLE_SECURE: {
513 final int userId = UserHandle.getCallingUserId();
514 if (args.name != null) {
515 Setting setting = getSecureSetting(args.name, userId);
516 return packageSettingForQuery(setting, normalizedProjection);
517 } else {
518 return getAllSecureSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800519 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700520 }
Svetoslav683914b2015-01-15 14:22:26 -0800521
Svetoslav7ec28e82015-05-20 17:01:10 -0700522 case TABLE_SYSTEM: {
523 final int userId = UserHandle.getCallingUserId();
524 if (args.name != null) {
525 Setting setting = getSystemSetting(args.name, userId);
526 return packageSettingForQuery(setting, normalizedProjection);
527 } else {
528 return getAllSystemSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800529 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700530 }
Svetoslav683914b2015-01-15 14:22:26 -0800531
Svetoslav7ec28e82015-05-20 17:01:10 -0700532 default: {
533 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700534 }
535 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700536 }
537
Nicholas Sauer72500532018-11-21 10:30:58 -0800538 private ArrayList<String> buildSettingsList(Cursor cursor) {
539 final ArrayList<String> lines = new ArrayList<String>();
540 try {
541 while (cursor != null && cursor.moveToNext()) {
542 lines.add(cursor.getString(1) + "=" + cursor.getString(2));
543 }
544 } finally {
545 if (cursor != null) {
546 cursor.close();
547 }
548 }
549 return lines;
550 }
551
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700552 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800553 public Uri insert(Uri uri, ContentValues values) {
554 if (DEBUG) {
555 Slog.v(LOG_TAG, "insert() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700556 }
557
Svetoslav683914b2015-01-15 14:22:26 -0800558 String table = getValidTableOrThrow(uri);
Christopher Tate06efb532012-08-24 15:29:27 -0700559
Svetoslav683914b2015-01-15 14:22:26 -0800560 // If a legacy table that is gone, done.
561 if (REMOVED_LEGACY_TABLES.contains(table)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 return null;
563 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700564
Svetoslav683914b2015-01-15 14:22:26 -0800565 String name = values.getAsString(Settings.Secure.NAME);
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700566 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800567 return null;
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700568 }
569
Svetoslav683914b2015-01-15 14:22:26 -0800570 String value = values.getAsString(Settings.Secure.VALUE);
571
Svetoslav7ec28e82015-05-20 17:01:10 -0700572 switch (table) {
573 case TABLE_GLOBAL: {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800574 if (insertGlobalSetting(name, value, null, false,
575 UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700576 return Uri.withAppendedPath(Settings.Global.CONTENT_URI, name);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700577 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700578 } break;
579
580 case TABLE_SECURE: {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800581 if (insertSecureSetting(name, value, null, false,
582 UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700583 return Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name);
584 }
585 } break;
586
587 case TABLE_SYSTEM: {
588 if (insertSystemSetting(name, value, UserHandle.getCallingUserId())) {
589 return Uri.withAppendedPath(Settings.System.CONTENT_URI, name);
590 }
591 } break;
592
593 default: {
594 throw new IllegalArgumentException("Bad Uri path:" + uri);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700595 }
596 }
597
Svetoslav683914b2015-01-15 14:22:26 -0800598 return null;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700599 }
600
601 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800602 public int bulkInsert(Uri uri, ContentValues[] allValues) {
603 if (DEBUG) {
604 Slog.v(LOG_TAG, "bulkInsert() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700606
Svetoslav683914b2015-01-15 14:22:26 -0800607 int insertionCount = 0;
608 final int valuesCount = allValues.length;
609 for (int i = 0; i < valuesCount; i++) {
610 ContentValues values = allValues[i];
611 if (insert(uri, values) != null) {
612 insertionCount++;
613 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700614 }
Svetoslav683914b2015-01-15 14:22:26 -0800615
616 return insertionCount;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700617 }
618
619 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800620 public int delete(Uri uri, String where, String[] whereArgs) {
621 if (DEBUG) {
622 Slog.v(LOG_TAG, "delete() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700624
Svetoslav683914b2015-01-15 14:22:26 -0800625 Arguments args = new Arguments(uri, where, whereArgs, false);
626
627 // If a legacy table that is gone, done.
628 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
629 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700630 }
Svetoslav683914b2015-01-15 14:22:26 -0800631
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700632 if (!isKeyValid(args.name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800633 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700634 }
Svetoslav683914b2015-01-15 14:22:26 -0800635
Svetoslav7ec28e82015-05-20 17:01:10 -0700636 switch (args.table) {
637 case TABLE_GLOBAL: {
638 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700639 return deleteGlobalSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700640 }
Svetoslav683914b2015-01-15 14:22:26 -0800641
Svetoslav7ec28e82015-05-20 17:01:10 -0700642 case TABLE_SECURE: {
643 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700644 return deleteSecureSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700645 }
Svetoslav683914b2015-01-15 14:22:26 -0800646
Svetoslav7ec28e82015-05-20 17:01:10 -0700647 case TABLE_SYSTEM: {
648 final int userId = UserHandle.getCallingUserId();
649 return deleteSystemSetting(args.name, userId) ? 1 : 0;
650 }
651
652 default: {
653 throw new IllegalArgumentException("Bad Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800654 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700655 }
Svetoslav683914b2015-01-15 14:22:26 -0800656 }
657
658 @Override
659 public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
660 if (DEBUG) {
661 Slog.v(LOG_TAG, "update() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700662 }
Svetoslav683914b2015-01-15 14:22:26 -0800663
664 Arguments args = new Arguments(uri, where, whereArgs, false);
665
666 // If a legacy table that is gone, done.
667 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
668 return 0;
669 }
670
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700671 String name = values.getAsString(Settings.Secure.NAME);
672 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800673 return 0;
674 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700675 String value = values.getAsString(Settings.Secure.VALUE);
Svetoslav683914b2015-01-15 14:22:26 -0800676
Svetoslav7ec28e82015-05-20 17:01:10 -0700677 switch (args.table) {
678 case TABLE_GLOBAL: {
679 final int userId = UserHandle.getCallingUserId();
Svetoslav Ganove080da92016-12-21 17:10:35 -0800680 return updateGlobalSetting(args.name, value, null, false,
681 userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700682 }
Svetoslav683914b2015-01-15 14:22:26 -0800683
Svetoslav7ec28e82015-05-20 17:01:10 -0700684 case TABLE_SECURE: {
685 final int userId = UserHandle.getCallingUserId();
Svetoslav Ganove080da92016-12-21 17:10:35 -0800686 return updateSecureSetting(args.name, value, null, false,
687 userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700688 }
Svetoslav683914b2015-01-15 14:22:26 -0800689
Svetoslav7ec28e82015-05-20 17:01:10 -0700690 case TABLE_SYSTEM: {
691 final int userId = UserHandle.getCallingUserId();
692 return updateSystemSetting(args.name, value, userId) ? 1 : 0;
693 }
Svetoslav683914b2015-01-15 14:22:26 -0800694
Svetoslav7ec28e82015-05-20 17:01:10 -0700695 default: {
696 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800697 }
698 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700699 }
700
701 @Override
702 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
Robin Lee2ab02e22016-07-28 18:41:23 +0100703 final int userId = getUserIdFromUri(uri, UserHandle.getCallingUserId());
704 if (userId != UserHandle.getCallingUserId()) {
705 getContext().enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS,
706 "Access files from the settings of another user");
707 }
708 uri = ContentProvider.getUriWithoutUserId(uri);
709
Andre Lago3fa139c2016-08-04 13:53:44 +0100710 final String cacheRingtoneSetting;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700711 final String cacheName;
712 if (Settings.System.RINGTONE_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100713 cacheRingtoneSetting = Settings.System.RINGTONE;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700714 cacheName = Settings.System.RINGTONE_CACHE;
715 } else if (Settings.System.NOTIFICATION_SOUND_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100716 cacheRingtoneSetting = Settings.System.NOTIFICATION_SOUND;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700717 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
718 } else if (Settings.System.ALARM_ALERT_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100719 cacheRingtoneSetting = Settings.System.ALARM_ALERT;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700720 cacheName = Settings.System.ALARM_ALERT_CACHE;
721 } else {
722 throw new FileNotFoundException("Direct file access no longer supported; "
723 + "ringtone playback is available through android.media.Ringtone");
724 }
725
Andre Lago3fa139c2016-08-04 13:53:44 +0100726 int actualCacheOwner;
727 // Redirect cache to parent if ringtone setting is owned by profile parent
728 synchronized (mLock) {
729 actualCacheOwner = resolveOwningUserIdForSystemSettingLocked(userId,
730 cacheRingtoneSetting);
731 }
732 final File cacheFile = new File(getRingtoneCacheDir(actualCacheOwner), cacheName);
Jeff Sharkey413573a2016-02-22 17:52:45 -0700733 return ParcelFileDescriptor.open(cacheFile, ParcelFileDescriptor.parseMode(mode));
734 }
735
736 private File getRingtoneCacheDir(int userId) {
737 final File cacheDir = new File(Environment.getDataSystemDeDirectory(userId), "ringtones");
738 cacheDir.mkdir();
739 SELinux.restorecon(cacheDir);
740 return cacheDir;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700741 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800742
Eugene Suslad72c3972016-12-27 15:49:30 -0800743 /**
744 * Dump all settings as a proto buf.
745 *
746 * @param fd The file to dump to
747 */
748 void dumpProto(@NonNull FileDescriptor fd) {
749 ProtoOutputStream proto = new ProtoOutputStream(fd);
750
751 synchronized (mLock) {
752 SettingsProtoDumpUtil.dumpProtoLocked(mSettingsRegistry, proto);
Eugene Suslad72c3972016-12-27 15:49:30 -0800753 }
754
755 proto.flush();
756 }
757
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700758 public void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800759 synchronized (mLock) {
760 final long identity = Binder.clearCallingIdentity();
761 try {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700762 SparseBooleanArray users = mSettingsRegistry.getKnownUsersLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800763 final int userCount = users.size();
764 for (int i = 0; i < userCount; i++) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700765 dumpForUserLocked(users.keyAt(i), pw);
Svetoslavb505ccc2015-02-17 12:41:04 -0800766 }
767 } finally {
768 Binder.restoreCallingIdentity(identity);
769 }
770 }
771 }
772
Andreas Gampeb58893072018-09-05 16:52:31 -0700773 @GuardedBy("mLock")
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700774 private void dumpForUserLocked(int userId, PrintWriter pw) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700775 if (userId == UserHandle.USER_SYSTEM) {
Matt Pape1b31a332018-10-17 09:58:28 -0700776 pw.println("CONFIG SETTINGS (user " + userId + ")");
777 SettingsState configSettings = mSettingsRegistry.getSettingsLocked(
778 SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM);
779 if (configSettings != null) {
780 dumpSettingsLocked(configSettings, pw);
781 pw.println();
782 configSettings.dumpHistoricalOperations(pw);
783 }
784
Svetoslavb505ccc2015-02-17 12:41:04 -0800785 pw.println("GLOBAL SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700786 SettingsState globalSettings = mSettingsRegistry.getSettingsLocked(
787 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700788 if (globalSettings != null) {
789 dumpSettingsLocked(globalSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800790 pw.println();
791 globalSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700792 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800793 }
794
795 pw.println("SECURE SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700796 SettingsState secureSettings = mSettingsRegistry.getSettingsLocked(
797 SETTINGS_TYPE_SECURE, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700798 if (secureSettings != null) {
799 dumpSettingsLocked(secureSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800800 pw.println();
801 secureSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700802 }
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700803
Svetoslavb505ccc2015-02-17 12:41:04 -0800804 pw.println("SYSTEM SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700805 SettingsState systemSettings = mSettingsRegistry.getSettingsLocked(
806 SETTINGS_TYPE_SYSTEM, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700807 if (systemSettings != null) {
808 dumpSettingsLocked(systemSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800809 pw.println();
810 systemSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700811 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800812 }
813
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700814 private void dumpSettingsLocked(SettingsState settingsState, PrintWriter pw) {
815 List<String> names = settingsState.getSettingNamesLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800816
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700817 final int nameCount = names.size();
Svetoslavb505ccc2015-02-17 12:41:04 -0800818
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700819 for (int i = 0; i < nameCount; i++) {
820 String name = names.get(i);
821 Setting setting = settingsState.getSettingLocked(name);
822 pw.print("_id:"); pw.print(toDumpString(setting.getId()));
823 pw.print(" name:"); pw.print(toDumpString(name));
824 if (setting.getPackageName() != null) {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800825 pw.print(" pkg:"); pw.print(setting.getPackageName());
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700826 }
827 pw.print(" value:"); pw.print(toDumpString(setting.getValue()));
Svetoslav Ganove080da92016-12-21 17:10:35 -0800828 if (setting.getDefaultValue() != null) {
829 pw.print(" default:"); pw.print(setting.getDefaultValue());
Eugene Suslad72c3972016-12-27 15:49:30 -0800830 pw.print(" defaultSystemSet:"); pw.print(setting.isDefaultFromSystem());
Svetoslav Ganove080da92016-12-21 17:10:35 -0800831 }
832 if (setting.getTag() != null) {
833 pw.print(" tag:"); pw.print(setting.getTag());
834 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800835 pw.println();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700836 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800837 }
838
Svetoslav7e0683b2015-08-03 16:02:52 -0700839 private static String toDumpString(String s) {
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700840 if (s != null) {
841 return s;
842 }
843 return "{null}";
844 }
845
Svetoslav683914b2015-01-15 14:22:26 -0800846 private void registerBroadcastReceivers() {
847 IntentFilter userFilter = new IntentFilter();
848 userFilter.addAction(Intent.ACTION_USER_REMOVED);
849 userFilter.addAction(Intent.ACTION_USER_STOPPED);
850
851 getContext().registerReceiver(new BroadcastReceiver() {
852 @Override
853 public void onReceive(Context context, Intent intent) {
854 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700855 UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800856
857 switch (intent.getAction()) {
858 case Intent.ACTION_USER_REMOVED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700859 synchronized (mLock) {
860 mSettingsRegistry.removeUserStateLocked(userId, true);
861 }
Svetoslav683914b2015-01-15 14:22:26 -0800862 } break;
863
864 case Intent.ACTION_USER_STOPPED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700865 synchronized (mLock) {
866 mSettingsRegistry.removeUserStateLocked(userId, false);
867 }
Svetoslav683914b2015-01-15 14:22:26 -0800868 } break;
869 }
870 }
871 }, userFilter);
872
873 PackageMonitor monitor = new PackageMonitor() {
874 @Override
875 public void onPackageRemoved(String packageName, int uid) {
876 synchronized (mLock) {
Zimuzoc56192c2018-07-25 10:40:01 +0100877 mSettingsRegistry.removeSettingsForPackageLocked(packageName,
Svetoslav683914b2015-01-15 14:22:26 -0800878 UserHandle.getUserId(uid));
879 }
880 }
Mark Rathjend891f012017-01-19 04:10:37 +0000881
882 @Override
883 public void onUidRemoved(int uid) {
884 synchronized (mLock) {
885 mSettingsRegistry.onUidRemovedLocked(uid);
886 }
887 }
Zimuzoc56192c2018-07-25 10:40:01 +0100888
889 @Override
890 public void onPackageDataCleared(String packageName, int uid) {
891 synchronized (mLock) {
892 mSettingsRegistry.removeSettingsForPackageLocked(packageName,
893 UserHandle.getUserId(uid));
894 }
895 }
Svetoslav683914b2015-01-15 14:22:26 -0800896 };
897
898 // package changes
899 monitor.register(getContext(), BackgroundThread.getHandler().getLooper(),
900 UserHandle.ALL, true);
901 }
902
Svet Ganov53a441c2016-04-19 19:38:00 -0700903 private void startWatchingUserRestrictionChanges() {
904 // TODO: The current design of settings looking different based on user restrictions
905 // should be reworked to keep them separate and system code should check the setting
906 // first followed by checking the user restriction before performing an operation.
907 UserManagerInternal userManager = LocalServices.getService(UserManagerInternal.class);
908 userManager.addUserRestrictionsListener((int userId, Bundle newRestrictions,
909 Bundle prevRestrictions) -> {
Irina Dumitrescue3696872019-01-09 16:07:59 +0000910 Set<String> changedRestrictions = getRestrictionDiff(prevRestrictions, newRestrictions);
Svet Ganov53a441c2016-04-19 19:38:00 -0700911 // We are changing the settings affected by restrictions to their current
912 // value with a forced update to ensure that all cross profile dependencies
913 // are taken into account. Also make sure the settings update to.. the same
914 // value passes the security checks, so clear binder calling id.
Irina Dumitrescue3696872019-01-09 16:07:59 +0000915 if (changedRestrictions.contains(UserManager.DISALLOW_SHARE_LOCATION)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700916 final long identity = Binder.clearCallingIdentity();
917 try {
918 synchronized (mLock) {
919 Setting setting = getSecureSetting(
Soonil Nagarkar4ee3ac22019-02-08 19:19:24 -0800920 Settings.Secure.LOCATION_MODE, userId);
921 updateSecureSetting(Settings.Secure.LOCATION_MODE,
922 setting != null ? setting.getValue() : null, null,
923 true, userId, true);
924 setting = getSecureSetting(
Svet Ganov53a441c2016-04-19 19:38:00 -0700925 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, userId);
926 updateSecureSetting(Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800927 setting != null ? setting.getValue() : null, null,
928 true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700929 }
930 } finally {
931 Binder.restoreCallingIdentity(identity);
932 }
933 }
Irina Dumitrescue3696872019-01-09 16:07:59 +0000934 if (changedRestrictions.contains(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)
935 || changedRestrictions.contains(
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100936 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700937 final long identity = Binder.clearCallingIdentity();
938 try {
939 synchronized (mLock) {
940 Setting setting = getGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800941 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700942 updateGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800943 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700944 }
945 } finally {
946 Binder.restoreCallingIdentity(identity);
947 }
948 }
Irina Dumitrescue3696872019-01-09 16:07:59 +0000949 if (changedRestrictions.contains(UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700950 final long identity = Binder.clearCallingIdentity();
951 try {
952 synchronized (mLock) {
953 Setting setting = getGlobalSetting(Settings.Global.ADB_ENABLED);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800954 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700955 updateGlobalSetting(Settings.Global.ADB_ENABLED,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800956 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700957 }
958 } finally {
959 Binder.restoreCallingIdentity(identity);
960 }
961 }
Irina Dumitrescue3696872019-01-09 16:07:59 +0000962 if (changedRestrictions.contains(UserManager.ENSURE_VERIFY_APPS)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700963 final long identity = Binder.clearCallingIdentity();
964 try {
965 synchronized (mLock) {
966 Setting enable = getGlobalSetting(
967 Settings.Global.PACKAGE_VERIFIER_ENABLE);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800968 String enableValue = enable != null ? enable.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700969 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_ENABLE,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800970 enableValue, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700971 Setting include = getGlobalSetting(
972 Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800973 String includeValue = include != null ? include.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700974 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800975 includeValue, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700976 }
977 } finally {
978 Binder.restoreCallingIdentity(identity);
979 }
980 }
Irina Dumitrescue3696872019-01-09 16:07:59 +0000981 if (changedRestrictions.contains(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700982 final long identity = Binder.clearCallingIdentity();
983 try {
984 synchronized (mLock) {
985 Setting setting = getGlobalSetting(
986 Settings.Global.PREFERRED_NETWORK_MODE);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800987 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700988 updateGlobalSetting(Settings.Global.PREFERRED_NETWORK_MODE,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800989 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700990 }
991 } finally {
992 Binder.restoreCallingIdentity(identity);
993 }
994 }
995 });
996 }
997
Irina Dumitrescue3696872019-01-09 16:07:59 +0000998 private static Set<String> getRestrictionDiff(Bundle prevRestrictions, Bundle newRestrictions) {
999 Set<String> restrictionNames = Sets.newArraySet();
1000 restrictionNames.addAll(prevRestrictions.keySet());
1001 restrictionNames.addAll(newRestrictions.keySet());
1002 Set<String> diff = Sets.newArraySet();
1003 for (String restrictionName : restrictionNames) {
1004 if (prevRestrictions.getBoolean(restrictionName) != newRestrictions.getBoolean(
1005 restrictionName)) {
1006 diff.add(restrictionName);
1007 }
1008 }
1009 return diff;
1010 }
1011
Matt Pape1b31a332018-10-17 09:58:28 -07001012 private Setting getConfigSetting(String name) {
1013 if (DEBUG) {
1014 Slog.v(LOG_TAG, "getConfigSetting(" + name + ")");
1015 }
1016
Stanislav Zholnin55799502019-03-08 14:54:55 +00001017 DeviceConfig.enforceReadPermission(getContext(), /*namespace=*/name.split("/")[0]);
Matt Pape1b31a332018-10-17 09:58:28 -07001018
1019 // Get the value.
1020 synchronized (mLock) {
1021 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_CONFIG,
1022 UserHandle.USER_SYSTEM, name);
1023 }
1024 }
1025
Matt Papec1323dc2018-12-11 12:32:42 -08001026 private boolean insertConfigSetting(String name, String value, boolean makeDefault) {
Matt Pape1b31a332018-10-17 09:58:28 -07001027 if (DEBUG) {
1028 Slog.v(LOG_TAG, "insertConfigSetting(" + name + ", " + value + ", "
Matt Papec1323dc2018-12-11 12:32:42 -08001029 + makeDefault + ")");
Matt Pape1b31a332018-10-17 09:58:28 -07001030 }
Matt Papec1323dc2018-12-11 12:32:42 -08001031 return mutateConfigSetting(name, value, null, makeDefault,
1032 MUTATION_OPERATION_INSERT, 0);
Matt Pape1b31a332018-10-17 09:58:28 -07001033 }
1034
Matt Papec1323dc2018-12-11 12:32:42 -08001035 private boolean deleteConfigSetting(String name) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08001036 if (DEBUG) {
Matt Papec1323dc2018-12-11 12:32:42 -08001037 Slog.v(LOG_TAG, "deleteConfigSetting(" + name + ")");
Matt Pape6bfc62e2018-11-28 13:16:03 -08001038 }
Matt Papec1323dc2018-12-11 12:32:42 -08001039 return mutateConfigSetting(name, null, null, false,
1040 MUTATION_OPERATION_DELETE, 0);
Matt Pape6bfc62e2018-11-28 13:16:03 -08001041 }
1042
Matt Papec1323dc2018-12-11 12:32:42 -08001043 private void resetConfigSetting(int mode, String prefix) {
Matt Pape1b31a332018-10-17 09:58:28 -07001044 if (DEBUG) {
Matt Papec1323dc2018-12-11 12:32:42 -08001045 Slog.v(LOG_TAG, "resetConfigSetting(" + mode + ", " + prefix + ")");
Matt Pape1b31a332018-10-17 09:58:28 -07001046 }
Matt Papec1323dc2018-12-11 12:32:42 -08001047 mutateConfigSetting(null, null, prefix, false,
1048 MUTATION_OPERATION_RESET, mode);
Matt Pape1b31a332018-10-17 09:58:28 -07001049 }
1050
Matt Pape6bfc62e2018-11-28 13:16:03 -08001051 private boolean mutateConfigSetting(String name, String value, String prefix,
Matt Papec1323dc2018-12-11 12:32:42 -08001052 boolean makeDefault, int operation, int mode) {
Stanislav Zholnin5a25a842019-03-13 14:43:26 +00001053 enforceWritePermission(Manifest.permission.WRITE_DEVICE_CONFIG);
Matt Pape1b31a332018-10-17 09:58:28 -07001054
Matt Pape1b31a332018-10-17 09:58:28 -07001055 // Perform the mutation.
1056 synchronized (mLock) {
1057 switch (operation) {
1058 case MUTATION_OPERATION_INSERT: {
1059 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_CONFIG,
Matt Papec1323dc2018-12-11 12:32:42 -08001060 UserHandle.USER_SYSTEM, name, value, null, makeDefault, true,
Matt Pape7b1c6cd2019-01-04 08:10:41 -08001061 resolveCallingPackage(), false, null);
Matt Pape1b31a332018-10-17 09:58:28 -07001062 }
1063
Matt Pape6bfc62e2018-11-28 13:16:03 -08001064 case MUTATION_OPERATION_DELETE: {
1065 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_CONFIG,
Matt Papec1323dc2018-12-11 12:32:42 -08001066 UserHandle.USER_SYSTEM, name, false, null);
Matt Pape6bfc62e2018-11-28 13:16:03 -08001067 }
1068
Matt Pape1b31a332018-10-17 09:58:28 -07001069 case MUTATION_OPERATION_RESET: {
1070 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_CONFIG,
Matt Pape7b1c6cd2019-01-04 08:10:41 -08001071 UserHandle.USER_SYSTEM, resolveCallingPackage(), mode, null, prefix);
Matt Pape1b31a332018-10-17 09:58:28 -07001072 } return true;
1073 }
1074 }
1075
1076 return false;
1077 }
1078
Matt Pape6bfc62e2018-11-28 13:16:03 -08001079 private Map<String, String> getAllConfigFlags(@Nullable String prefix) {
1080 if (DEBUG) {
1081 Slog.v(LOG_TAG, "getAllConfigFlags() for " + prefix);
1082 }
1083
1084 synchronized (mLock) {
1085 // Get the settings.
1086 SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
1087 SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM);
1088
1089 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_CONFIG,
1090 UserHandle.USER_SYSTEM);
1091
1092 final int nameCount = names.size();
1093 Map<String, String> flagsToValues = new HashMap<>(names.size());
1094
1095 for (int i = 0; i < nameCount; i++) {
1096 String name = names.get(i);
1097 Setting setting = settingsState.getSettingLocked(name);
1098 if (prefix == null || setting.getName().startsWith(prefix)) {
1099 flagsToValues.put(setting.getName(), setting.getValue());
1100 }
1101 }
1102
1103 return flagsToValues;
1104 }
1105 }
1106
Svetoslav7ec28e82015-05-20 17:01:10 -07001107 private Cursor getAllGlobalSettings(String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001108 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001109 Slog.v(LOG_TAG, "getAllGlobalSettings()");
Svetoslav683914b2015-01-15 14:22:26 -08001110 }
1111
Svetoslav7ec28e82015-05-20 17:01:10 -07001112 synchronized (mLock) {
1113 // Get the settings.
1114 SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001115 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001116
Chad Brubaker97bccee2017-01-05 15:51:41 -08001117 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_GLOBAL,
1118 UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001119
Svetoslav7ec28e82015-05-20 17:01:10 -07001120 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001121
Svetoslav7ec28e82015-05-20 17:01:10 -07001122 String[] normalizedProjection = normalizeProjection(projection);
1123 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001124
Svetoslav7ec28e82015-05-20 17:01:10 -07001125 // Anyone can get the global settings, so no security checks.
1126 for (int i = 0; i < nameCount; i++) {
1127 String name = names.get(i);
1128 Setting setting = settingsState.getSettingLocked(name);
1129 appendSettingToCursor(result, setting);
1130 }
1131
1132 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001133 }
Svetoslav683914b2015-01-15 14:22:26 -08001134 }
1135
Svetoslav7ec28e82015-05-20 17:01:10 -07001136 private Setting getGlobalSetting(String name) {
Svetoslav683914b2015-01-15 14:22:26 -08001137 if (DEBUG) {
1138 Slog.v(LOG_TAG, "getGlobalSetting(" + name + ")");
1139 }
1140
Chad Brubakera6830e72017-04-28 17:34:36 -07001141 // Ensure the caller can access the setting.
1142 enforceSettingReadable(name, SETTINGS_TYPE_GLOBAL, UserHandle.getCallingUserId());
1143
Svetoslav683914b2015-01-15 14:22:26 -08001144 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001145 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -07001146 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_GLOBAL,
Xiaohui Chen43765b72015-08-31 10:57:33 -07001147 UserHandle.USER_SYSTEM, name);
Svetoslav683914b2015-01-15 14:22:26 -08001148 }
Svetoslav683914b2015-01-15 14:22:26 -08001149 }
1150
Svetoslav Ganove080da92016-12-21 17:10:35 -08001151 private boolean updateGlobalSetting(String name, String value, String tag,
1152 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001153 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001154 Slog.v(LOG_TAG, "updateGlobalSetting(" + name + ", " + value + ", "
1155 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1156 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001157 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001158 return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId,
1159 MUTATION_OPERATION_UPDATE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001160 }
1161
Svetoslav Ganove080da92016-12-21 17:10:35 -08001162 private boolean insertGlobalSetting(String name, String value, String tag,
1163 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001164 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001165 Slog.v(LOG_TAG, "insertGlobalSetting(" + name + ", " + value + ", "
1166 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1167 + ", " + forceNotify + ")");
Svetoslav7ec28e82015-05-20 17:01:10 -07001168 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001169 return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId,
1170 MUTATION_OPERATION_INSERT, forceNotify, 0);
Svetoslav7ec28e82015-05-20 17:01:10 -07001171 }
1172
Svet Ganov53a441c2016-04-19 19:38:00 -07001173 private boolean deleteGlobalSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001174 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001175 Slog.v(LOG_TAG, "deleteGlobalSetting(" + name + ", " + requestingUserId
1176 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001177 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001178 return mutateGlobalSetting(name, null, null, false, requestingUserId,
1179 MUTATION_OPERATION_DELETE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001180 }
1181
Svetoslav Ganove080da92016-12-21 17:10:35 -08001182 private void resetGlobalSetting(int requestingUserId, int mode, String tag) {
1183 if (DEBUG) {
1184 Slog.v(LOG_TAG, "resetGlobalSetting(" + requestingUserId + ", "
1185 + mode + ", " + tag + ")");
1186 }
1187 mutateGlobalSetting(null, null, tag, false, requestingUserId,
1188 MUTATION_OPERATION_RESET, false, mode);
1189 }
1190
1191 private boolean mutateGlobalSetting(String name, String value, String tag,
1192 boolean makeDefault, int requestingUserId, int operation, boolean forceNotify,
1193 int mode) {
Svetoslav683914b2015-01-15 14:22:26 -08001194 // Make sure the caller can change the settings - treated as secure.
1195 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
1196
Svetoslav683914b2015-01-15 14:22:26 -08001197 // Resolve the userId on whose behalf the call is made.
1198 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1199
Makoto Onuki28da2e32015-11-20 11:30:44 -08001200 // If this is a setting that is currently restricted for this user, do not allow
1201 // unrestricting changes.
yuemingw1d13eae2018-01-30 17:27:54 +00001202 if (name != null && mUserManagerInternal.isSettingRestrictedForUser(
1203 name, callingUserId, value, Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -08001204 return false;
1205 }
1206
1207 // Perform the mutation.
Svetoslav7ec28e82015-05-20 17:01:10 -07001208 synchronized (mLock) {
1209 switch (operation) {
1210 case MUTATION_OPERATION_INSERT: {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001211 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_GLOBAL,
1212 UserHandle.USER_SYSTEM, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001213 getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001214 }
Svetoslav683914b2015-01-15 14:22:26 -08001215
Svetoslav7ec28e82015-05-20 17:01:10 -07001216 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001217 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_GLOBAL,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001218 UserHandle.USER_SYSTEM, name, forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001219 }
Svetoslav683914b2015-01-15 14:22:26 -08001220
Svetoslav7ec28e82015-05-20 17:01:10 -07001221 case MUTATION_OPERATION_UPDATE: {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001222 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_GLOBAL,
1223 UserHandle.USER_SYSTEM, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001224 getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001225 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001226
1227 case MUTATION_OPERATION_RESET: {
1228 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_GLOBAL,
1229 UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag);
1230 } return true;
Svetoslav683914b2015-01-15 14:22:26 -08001231 }
1232 }
1233
1234 return false;
1235 }
1236
Christopher Tateb218e762017-04-05 16:34:07 -07001237 private PackageInfo getCallingPackageInfo(int userId) {
1238 try {
1239 return mPackageManager.getPackageInfo(getCallingPackage(),
1240 PackageManager.GET_SIGNATURES, userId);
1241 } catch (RemoteException e) {
1242 throw new IllegalStateException("Package " + getCallingPackage() + " doesn't exist");
1243 }
1244 }
1245
Svetoslav7ec28e82015-05-20 17:01:10 -07001246 private Cursor getAllSecureSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001247 if (DEBUG) {
1248 Slog.v(LOG_TAG, "getAllSecureSettings(" + userId + ")");
1249 }
1250
1251 // Resolve the userId on whose behalf the call is made.
1252 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
1253
Christopher Tateb218e762017-04-05 16:34:07 -07001254 // The relevant "calling package" userId will be the owning userId for some
1255 // profiles, and we can't do the lookup inside our [lock held] loop, so work out
1256 // up front who the effective "new SSAID" user ID for that settings name will be.
1257 final int ssaidUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
1258 Settings.Secure.ANDROID_ID);
1259 final PackageInfo ssaidCallingPkg = getCallingPackageInfo(ssaidUserId);
1260
Svetoslav7ec28e82015-05-20 17:01:10 -07001261 synchronized (mLock) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001262 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SECURE, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -08001263
Svetoslav7ec28e82015-05-20 17:01:10 -07001264 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001265
Svetoslav7ec28e82015-05-20 17:01:10 -07001266 String[] normalizedProjection = normalizeProjection(projection);
1267 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001268
Svetoslav7ec28e82015-05-20 17:01:10 -07001269 for (int i = 0; i < nameCount; i++) {
1270 String name = names.get(i);
1271 // Determine the owning user as some profile settings are cloned from the parent.
1272 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
1273 name);
Svetoslav683914b2015-01-15 14:22:26 -08001274
Alex Klyubin1991f572017-03-03 14:08:36 -08001275 if (!isSecureSettingAccessible(name, callingUserId, owningUserId)) {
1276 // This caller is not permitted to access this setting. Pretend the setting
1277 // doesn't exist.
Svetoslav Ganov83a1f7f2016-04-27 13:50:49 -07001278 continue;
Svetoslav7ec28e82015-05-20 17:01:10 -07001279 }
Svetoslav683914b2015-01-15 14:22:26 -08001280
Mark Rathjen7599f132017-01-23 14:15:54 -08001281 // As of Android O, the SSAID is read from an app-specific entry in table
Mark Rathjend891f012017-01-19 04:10:37 +00001282 // SETTINGS_FILE_SSAID, unless accessed by a system process.
1283 final Setting setting;
1284 if (isNewSsaidSetting(name)) {
Christopher Tateb218e762017-04-05 16:34:07 -07001285 setting = getSsaidSettingLocked(ssaidCallingPkg, owningUserId);
Mark Rathjend891f012017-01-19 04:10:37 +00001286 } else {
1287 setting = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE, owningUserId,
1288 name);
1289 }
Svetoslav7ec28e82015-05-20 17:01:10 -07001290 appendSettingToCursor(result, setting);
Svetoslav683914b2015-01-15 14:22:26 -08001291 }
1292
Svetoslav7ec28e82015-05-20 17:01:10 -07001293 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001294 }
Svetoslav683914b2015-01-15 14:22:26 -08001295 }
1296
Svetoslav7ec28e82015-05-20 17:01:10 -07001297 private Setting getSecureSetting(String name, int requestingUserId) {
Makoto Onuki0000d322017-11-28 16:31:47 -08001298 return getSecureSetting(name, requestingUserId, /*enableOverride=*/ false);
1299 }
1300
1301 private Setting getSecureSetting(String name, int requestingUserId, boolean enableOverride) {
Svetoslav683914b2015-01-15 14:22:26 -08001302 if (DEBUG) {
1303 Slog.v(LOG_TAG, "getSecureSetting(" + name + ", " + requestingUserId + ")");
1304 }
1305
1306 // Resolve the userId on whose behalf the call is made.
1307 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1308
Chad Brubakera6830e72017-04-28 17:34:36 -07001309 // Ensure the caller can access the setting.
1310 enforceSettingReadable(name, SETTINGS_TYPE_SECURE, UserHandle.getCallingUserId());
1311
Svetoslav683914b2015-01-15 14:22:26 -08001312 // Determine the owning user as some profile settings are cloned from the parent.
1313 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
1314
Alex Klyubin1991f572017-03-03 14:08:36 -08001315 if (!isSecureSettingAccessible(name, callingUserId, owningUserId)) {
1316 // This caller is not permitted to access this setting. Pretend the setting doesn't
1317 // exist.
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001318 SettingsState settings = mSettingsRegistry.getSettingsLocked(SETTINGS_TYPE_SECURE,
1319 owningUserId);
1320 return settings != null ? settings.getNullSetting() : null;
Svetoslav683914b2015-01-15 14:22:26 -08001321 }
1322
Christopher Tateb218e762017-04-05 16:34:07 -07001323 // As of Android O, the SSAID is read from an app-specific entry in table
1324 // SETTINGS_FILE_SSAID, unless accessed by a system process.
1325 if (isNewSsaidSetting(name)) {
1326 PackageInfo callingPkg = getCallingPackageInfo(owningUserId);
1327 synchronized (mLock) {
1328 return getSsaidSettingLocked(callingPkg, owningUserId);
Mark Rathjend891f012017-01-19 04:10:37 +00001329 }
Christopher Tateb218e762017-04-05 16:34:07 -07001330 }
Makoto Onuki0000d322017-11-28 16:31:47 -08001331 if (enableOverride) {
Kweku Adams5e0052b2019-02-22 15:17:52 -08001332 if (Secure.LOCATION_MODE.equals(name)) {
1333 final Setting overridden = getLocationModeSetting(owningUserId);
Makoto Onuki0000d322017-11-28 16:31:47 -08001334 if (overridden != null) {
1335 return overridden;
1336 }
1337 }
1338 }
Mark Rathjend891f012017-01-19 04:10:37 +00001339
Christopher Tateb218e762017-04-05 16:34:07 -07001340 // Not the SSAID; do a straight lookup
1341 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -07001342 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav7ec28e82015-05-20 17:01:10 -07001343 owningUserId, name);
1344 }
Svetoslav683914b2015-01-15 14:22:26 -08001345 }
1346
Mark Rathjend891f012017-01-19 04:10:37 +00001347 private boolean isNewSsaidSetting(String name) {
1348 return Settings.Secure.ANDROID_ID.equals(name)
1349 && UserHandle.getAppId(Binder.getCallingUid()) >= Process.FIRST_APPLICATION_UID;
1350 }
1351
Andreas Gampeb58893072018-09-05 16:52:31 -07001352 @GuardedBy("mLock")
Christopher Tateb218e762017-04-05 16:34:07 -07001353 private Setting getSsaidSettingLocked(PackageInfo callingPkg, int owningUserId) {
Mark Rathjend891f012017-01-19 04:10:37 +00001354 // Get uid of caller (key) used to store ssaid value
1355 String name = Integer.toString(
1356 UserHandle.getUid(owningUserId, UserHandle.getAppId(Binder.getCallingUid())));
1357
1358 if (DEBUG) {
1359 Slog.v(LOG_TAG, "getSsaidSettingLocked(" + name + "," + owningUserId + ")");
1360 }
1361
1362 // Retrieve the ssaid from the table if present.
1363 final Setting ssaid = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SSAID, owningUserId,
1364 name);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001365 // If the app is an Instant App use its stored SSAID instead of our own.
1366 final String instantSsaid;
1367 final long token = Binder.clearCallingIdentity();
1368 try {
1369 instantSsaid = mPackageManager.getInstantAppAndroidId(callingPkg.packageName,
1370 owningUserId);
1371 } catch (RemoteException e) {
1372 Slog.e(LOG_TAG, "Failed to get Instant App Android ID", e);
1373 return null;
1374 } finally {
1375 Binder.restoreCallingIdentity(token);
1376 }
Svet Ganov96c99462017-05-05 14:27:13 -07001377
1378 final SettingsState ssaidSettings = mSettingsRegistry.getSettingsLocked(
1379 SETTINGS_TYPE_SSAID, owningUserId);
1380
Chad Brubaker0d277a72017-04-12 16:56:53 -07001381 if (instantSsaid != null) {
1382 // Use the stored value if it is still valid.
1383 if (ssaid != null && instantSsaid.equals(ssaid.getValue())) {
Svet Ganov96c99462017-05-05 14:27:13 -07001384 return mascaradeSsaidSetting(ssaidSettings, ssaid);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001385 }
1386 // The value has changed, update the stored value.
Chad Brubaker0d277a72017-04-12 16:56:53 -07001387 final boolean success = ssaidSettings.insertSettingLocked(name, instantSsaid, null,
1388 true, callingPkg.packageName);
1389 if (!success) {
1390 throw new IllegalStateException("Failed to update instant app android id");
1391 }
Svet Ganov96c99462017-05-05 14:27:13 -07001392 Setting setting = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SSAID,
1393 owningUserId, name);
1394 return mascaradeSsaidSetting(ssaidSettings, setting);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001395 }
Mark Rathjend891f012017-01-19 04:10:37 +00001396
1397 // Lazy initialize ssaid if not yet present in ssaid table.
Mark Rathjenea617592017-01-18 23:03:41 -08001398 if (ssaid == null || ssaid.isNull() || ssaid.getValue() == null) {
Svet Ganov96c99462017-05-05 14:27:13 -07001399 Setting setting = mSettingsRegistry.generateSsaidLocked(callingPkg, owningUserId);
1400 return mascaradeSsaidSetting(ssaidSettings, setting);
Mark Rathjend891f012017-01-19 04:10:37 +00001401 }
1402
Svet Ganov96c99462017-05-05 14:27:13 -07001403 return mascaradeSsaidSetting(ssaidSettings, ssaid);
1404 }
1405
1406 private Setting mascaradeSsaidSetting(SettingsState settingsState, Setting ssaidSetting) {
1407 // SSAID settings are located in a dedicated table for internal bookkeeping
1408 // but for the world they reside in the secure table, so adjust the key here.
1409 // We have a special name when looking it up but want the world to see it as
1410 // "android_id".
1411 if (ssaidSetting != null) {
1412 return settingsState.new Setting(ssaidSetting) {
1413 @Override
1414 public int getKey() {
1415 final int userId = getUserIdFromKey(super.getKey());
1416 return makeKey(SETTINGS_TYPE_SECURE, userId);
1417 }
1418
1419 @Override
1420 public String getName() {
1421 return Settings.Secure.ANDROID_ID;
1422 }
1423 };
1424 }
1425 return null;
Mark Rathjend891f012017-01-19 04:10:37 +00001426 }
1427
Kweku Adams5e0052b2019-02-22 15:17:52 -08001428 private Setting getLocationModeSetting(int owningUserId) {
Makoto Onuki0000d322017-11-28 16:31:47 -08001429 synchronized (mLock) {
1430 final Setting setting = getGlobalSetting(
1431 Global.LOCATION_GLOBAL_KILL_SWITCH);
1432 if (!"1".equals(setting.getValue())) {
1433 return null;
1434 }
1435 // Global kill-switch is enabled. Return an empty value.
1436 final SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
1437 SETTINGS_TYPE_SECURE, owningUserId);
1438 return settingsState.new Setting(
Kweku Adams5e0052b2019-02-22 15:17:52 -08001439 Secure.LOCATION_MODE,
Makoto Onuki0000d322017-11-28 16:31:47 -08001440 "", // value
1441 "", // tag
1442 "", // default value
1443 "", // package name
1444 false, // from system
1445 "0" // id
1446 ) {
1447 @Override
1448 public boolean update(String value, boolean setDefault, String packageName,
1449 String tag, boolean forceNonSystemPackage) {
Kweku Adams5e0052b2019-02-22 15:17:52 -08001450 Slog.wtf(LOG_TAG, "update shouldn't be called on this instance.");
Makoto Onuki0000d322017-11-28 16:31:47 -08001451 return false;
1452 }
1453 };
1454 }
1455 }
1456
Svetoslav Ganove080da92016-12-21 17:10:35 -08001457 private boolean insertSecureSetting(String name, String value, String tag,
1458 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001459 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001460 Slog.v(LOG_TAG, "insertSecureSetting(" + name + ", " + value + ", "
Svetoslav Ganove080da92016-12-21 17:10:35 -08001461 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1462 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001463 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001464 return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId,
1465 MUTATION_OPERATION_INSERT, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001466 }
1467
Svet Ganov53a441c2016-04-19 19:38:00 -07001468 private boolean deleteSecureSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001469 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001470 Slog.v(LOG_TAG, "deleteSecureSetting(" + name + ", " + requestingUserId
1471 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001472 }
1473
Svetoslav Ganove080da92016-12-21 17:10:35 -08001474 return mutateSecureSetting(name, null, null, false, requestingUserId,
1475 MUTATION_OPERATION_DELETE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001476 }
1477
Svetoslav Ganove080da92016-12-21 17:10:35 -08001478 private boolean updateSecureSetting(String name, String value, String tag,
1479 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001480 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001481 Slog.v(LOG_TAG, "updateSecureSetting(" + name + ", " + value + ", "
Svetoslav Ganove080da92016-12-21 17:10:35 -08001482 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1483 + ", " + forceNotify +")");
Svetoslav683914b2015-01-15 14:22:26 -08001484 }
1485
Svetoslav Ganove080da92016-12-21 17:10:35 -08001486 return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId,
1487 MUTATION_OPERATION_UPDATE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001488 }
1489
Svetoslav Ganove080da92016-12-21 17:10:35 -08001490 private void resetSecureSetting(int requestingUserId, int mode, String tag) {
1491 if (DEBUG) {
1492 Slog.v(LOG_TAG, "resetSecureSetting(" + requestingUserId + ", "
1493 + mode + ", " + tag + ")");
1494 }
1495
1496 mutateSecureSetting(null, null, tag, false, requestingUserId,
1497 MUTATION_OPERATION_RESET, false, mode);
1498 }
1499
1500 private boolean mutateSecureSetting(String name, String value, String tag,
1501 boolean makeDefault, int requestingUserId, int operation, boolean forceNotify,
1502 int mode) {
Svetoslav683914b2015-01-15 14:22:26 -08001503 // Make sure the caller can change the settings.
1504 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
1505
Svetoslav683914b2015-01-15 14:22:26 -08001506 // Resolve the userId on whose behalf the call is made.
1507 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1508
Makoto Onuki28da2e32015-11-20 11:30:44 -08001509 // If this is a setting that is currently restricted for this user, do not allow
1510 // unrestricting changes.
yuemingw1d13eae2018-01-30 17:27:54 +00001511 if (name != null && mUserManagerInternal.isSettingRestrictedForUser(
1512 name, callingUserId, value, Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -08001513 return false;
1514 }
1515
1516 // Determine the owning user as some profile settings are cloned from the parent.
1517 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
1518
1519 // Only the owning user can change the setting.
1520 if (owningUserId != callingUserId) {
1521 return false;
1522 }
1523
1524 // Special cases for location providers (sigh).
1525 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001526 return updateLocationProvidersAllowedLocked(value, tag, owningUserId, makeDefault,
1527 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -08001528 }
1529
1530 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001531 synchronized (mLock) {
1532 switch (operation) {
1533 case MUTATION_OPERATION_INSERT: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001534 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001535 owningUserId, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001536 getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001537 }
Svetoslav683914b2015-01-15 14:22:26 -08001538
Svetoslav7ec28e82015-05-20 17:01:10 -07001539 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001540 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001541 owningUserId, name, forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001542 }
Svetoslav683914b2015-01-15 14:22:26 -08001543
Svetoslav7ec28e82015-05-20 17:01:10 -07001544 case MUTATION_OPERATION_UPDATE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001545 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001546 owningUserId, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001547 getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001548 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001549
1550 case MUTATION_OPERATION_RESET: {
1551 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_SECURE,
1552 UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag);
1553 } return true;
Svetoslav683914b2015-01-15 14:22:26 -08001554 }
1555 }
1556
1557 return false;
1558 }
1559
Svetoslav7ec28e82015-05-20 17:01:10 -07001560 private Cursor getAllSystemSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001561 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001562 Slog.v(LOG_TAG, "getAllSecureSystem(" + userId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001563 }
1564
1565 // Resolve the userId on whose behalf the call is made.
1566 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
1567
Svetoslav7ec28e82015-05-20 17:01:10 -07001568 synchronized (mLock) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001569 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SYSTEM, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -08001570
Svetoslav7ec28e82015-05-20 17:01:10 -07001571 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001572
Svetoslav7ec28e82015-05-20 17:01:10 -07001573 String[] normalizedProjection = normalizeProjection(projection);
1574 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001575
Svetoslav7ec28e82015-05-20 17:01:10 -07001576 for (int i = 0; i < nameCount; i++) {
1577 String name = names.get(i);
Svetoslav683914b2015-01-15 14:22:26 -08001578
Svetoslav7ec28e82015-05-20 17:01:10 -07001579 // Determine the owning user as some profile settings are cloned from the parent.
1580 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId,
1581 name);
Svetoslav683914b2015-01-15 14:22:26 -08001582
Svetoslav7ec28e82015-05-20 17:01:10 -07001583 Setting setting = mSettingsRegistry.getSettingLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001584 SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001585 appendSettingToCursor(result, setting);
1586 }
1587
1588 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001589 }
Svetoslav683914b2015-01-15 14:22:26 -08001590 }
1591
Svetoslav7ec28e82015-05-20 17:01:10 -07001592 private Setting getSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001593 if (DEBUG) {
1594 Slog.v(LOG_TAG, "getSystemSetting(" + name + ", " + requestingUserId + ")");
1595 }
1596
1597 // Resolve the userId on whose behalf the call is made.
1598 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1599
Chad Brubakera6830e72017-04-28 17:34:36 -07001600 // Ensure the caller can access the setting.
1601 enforceSettingReadable(name, SETTINGS_TYPE_SYSTEM, UserHandle.getCallingUserId());
Chad Brubaker97bccee2017-01-05 15:51:41 -08001602
Svetoslav683914b2015-01-15 14:22:26 -08001603 // Determine the owning user as some profile settings are cloned from the parent.
1604 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1605
1606 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001607 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -07001608 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001609 }
Svetoslav683914b2015-01-15 14:22:26 -08001610 }
1611
Svetoslav7ec28e82015-05-20 17:01:10 -07001612 private boolean insertSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001613 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001614 Slog.v(LOG_TAG, "insertSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001615 + requestingUserId + ")");
1616 }
1617
Svetoslav7ec28e82015-05-20 17:01:10 -07001618 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT);
Svetoslav683914b2015-01-15 14:22:26 -08001619 }
1620
Svetoslav7ec28e82015-05-20 17:01:10 -07001621 private boolean deleteSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001622 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001623 Slog.v(LOG_TAG, "deleteSystemSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001624 }
1625
Svetoslav7ec28e82015-05-20 17:01:10 -07001626 return mutateSystemSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE);
Svetoslav683914b2015-01-15 14:22:26 -08001627 }
1628
Svetoslav7ec28e82015-05-20 17:01:10 -07001629 private boolean updateSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001630 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001631 Slog.v(LOG_TAG, "updateSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001632 + requestingUserId + ")");
1633 }
1634
Svetoslav7ec28e82015-05-20 17:01:10 -07001635 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE);
Svetoslav683914b2015-01-15 14:22:26 -08001636 }
1637
Svetoslav7ec28e82015-05-20 17:01:10 -07001638 private boolean mutateSystemSetting(String name, String value, int runAsUserId,
Svetoslav683914b2015-01-15 14:22:26 -08001639 int operation) {
Billy Lau6ad2d662015-07-18 00:26:58 +01001640 if (!hasWriteSecureSettingsPermission()) {
1641 // If the caller doesn't hold WRITE_SECURE_SETTINGS, we verify whether this
1642 // operation is allowed for the calling package through appops.
1643 if (!Settings.checkAndNoteWriteSettingsOperation(getContext(),
1644 Binder.getCallingUid(), getCallingPackage(), true)) {
1645 return false;
1646 }
Svetoslav683914b2015-01-15 14:22:26 -08001647 }
1648
Svetoslav683914b2015-01-15 14:22:26 -08001649 // Resolve the userId on whose behalf the call is made.
1650 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId);
1651
yuemingw1d13eae2018-01-30 17:27:54 +00001652 if (name != null && mUserManagerInternal.isSettingRestrictedForUser(
1653 name, callingUserId, value, Binder.getCallingUid())) {
1654 return false;
1655 }
1656
Svetoslavd8d25e02015-11-20 13:09:26 -08001657 // Enforce what the calling package can mutate the system settings.
1658 enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name, callingUserId);
1659
Svetoslav683914b2015-01-15 14:22:26 -08001660 // Determine the owning user as some profile settings are cloned from the parent.
1661 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1662
1663 // Only the owning user id can change the setting.
1664 if (owningUserId != callingUserId) {
1665 return false;
1666 }
1667
Jeff Sharkey413573a2016-02-22 17:52:45 -07001668 // Invalidate any relevant cache files
1669 String cacheName = null;
1670 if (Settings.System.RINGTONE.equals(name)) {
1671 cacheName = Settings.System.RINGTONE_CACHE;
1672 } else if (Settings.System.NOTIFICATION_SOUND.equals(name)) {
1673 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
1674 } else if (Settings.System.ALARM_ALERT.equals(name)) {
1675 cacheName = Settings.System.ALARM_ALERT_CACHE;
1676 }
1677 if (cacheName != null) {
1678 final File cacheFile = new File(
Andre Lago3fa139c2016-08-04 13:53:44 +01001679 getRingtoneCacheDir(owningUserId), cacheName);
Jeff Sharkey413573a2016-02-22 17:52:45 -07001680 cacheFile.delete();
1681 }
1682
Svetoslav683914b2015-01-15 14:22:26 -08001683 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001684 synchronized (mLock) {
1685 switch (operation) {
1686 case MUTATION_OPERATION_INSERT: {
1687 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001688 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001689 owningUserId, name, value, null, false, getCallingPackage(),
1690 false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001691 }
1692
1693 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001694 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001695 owningUserId, name, false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001696 }
1697
1698 case MUTATION_OPERATION_UPDATE: {
1699 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001700 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001701 owningUserId, name, value, null, false, getCallingPackage(),
1702 false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001703 }
Svetoslav683914b2015-01-15 14:22:26 -08001704 }
1705
Svetoslav7ec28e82015-05-20 17:01:10 -07001706 return false;
Svetoslav683914b2015-01-15 14:22:26 -08001707 }
Svetoslav683914b2015-01-15 14:22:26 -08001708 }
1709
Billy Lau6ad2d662015-07-18 00:26:58 +01001710 private boolean hasWriteSecureSettingsPermission() {
Svetoslavf41334b2015-06-23 12:06:03 -07001711 // Write secure settings is a more protected permission. If caller has it we are good.
1712 if (getContext().checkCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS)
1713 == PackageManager.PERMISSION_GRANTED) {
1714 return true;
1715 }
1716
Svetoslavf41334b2015-06-23 12:06:03 -07001717 return false;
1718 }
1719
Svetoslav683914b2015-01-15 14:22:26 -08001720 private void validateSystemSettingValue(String name, String value) {
Al Sutton91f89d02019-08-16 12:56:57 +01001721 Validator validator = SystemSettingsValidators.VALIDATORS.get(name);
Svetoslav683914b2015-01-15 14:22:26 -08001722 if (validator != null && !validator.validate(value)) {
1723 throw new IllegalArgumentException("Invalid value: " + value
1724 + " for setting: " + name);
1725 }
1726 }
1727
Alex Klyubin1991f572017-03-03 14:08:36 -08001728 /**
1729 * Returns {@code true} if the specified secure setting should be accessible to the caller.
1730 */
1731 private boolean isSecureSettingAccessible(String name, int callingUserId,
1732 int owningUserId) {
1733 // Special case for location (sigh).
1734 // This check is not inside the name-based checks below because this method performs checks
1735 // only if the calling user ID is not the same as the owning user ID.
1736 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
1737 return false;
1738 }
1739
1740 switch (name) {
1741 case "bluetooth_address":
1742 // BluetoothManagerService for some reason stores the Android's Bluetooth MAC
1743 // address in this secure setting. Secure settings can normally be read by any app,
1744 // which thus enables them to bypass the recently introduced restrictions on access
1745 // to device identifiers.
1746 // To mitigate this we make this setting available only to callers privileged to see
1747 // this device's MAC addresses, same as through public API
1748 // BluetoothAdapter.getAddress() (see BluetoothManagerService for details).
1749 return getContext().checkCallingOrSelfPermission(
1750 Manifest.permission.LOCAL_MAC_ADDRESS) == PackageManager.PERMISSION_GRANTED;
1751 default:
1752 return true;
1753 }
1754 }
1755
Svetoslav683914b2015-01-15 14:22:26 -08001756 private boolean isLocationProvidersAllowedRestricted(String name, int callingUserId,
1757 int owningUserId) {
1758 // Optimization - location providers are restricted only for managed profiles.
1759 if (callingUserId == owningUserId) {
1760 return false;
1761 }
1762 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)
1763 && mUserManager.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION,
1764 new UserHandle(callingUserId))) {
1765 return true;
1766 }
1767 return false;
1768 }
1769
Svetoslav683914b2015-01-15 14:22:26 -08001770 private int resolveOwningUserIdForSecureSettingLocked(int userId, String setting) {
1771 return resolveOwningUserIdLocked(userId, sSecureCloneToManagedSettings, setting);
1772 }
1773
1774 private int resolveOwningUserIdForSystemSettingLocked(int userId, String setting) {
Andre Lago3fa139c2016-08-04 13:53:44 +01001775 final int parentId;
1776 // Resolves dependency if setting has a dependency and the calling user has a parent
1777 if (sSystemCloneFromParentOnDependency.containsKey(setting)
1778 && (parentId = getGroupParentLocked(userId)) != userId) {
1779 // The setting has a dependency and the profile has a parent
1780 String dependency = sSystemCloneFromParentOnDependency.get(setting);
Chad Brubaker97bccee2017-01-05 15:51:41 -08001781 // Lookup the dependency setting as ourselves, some callers may not have access to it.
1782 final long token = Binder.clearCallingIdentity();
1783 try {
1784 Setting settingObj = getSecureSetting(dependency, userId);
1785 if (settingObj != null && settingObj.getValue().equals("1")) {
1786 return parentId;
1787 }
1788 } finally {
1789 Binder.restoreCallingIdentity(token);
Andre Lago3fa139c2016-08-04 13:53:44 +01001790 }
1791 }
Svetoslav683914b2015-01-15 14:22:26 -08001792 return resolveOwningUserIdLocked(userId, sSystemCloneToManagedSettings, setting);
1793 }
1794
1795 private int resolveOwningUserIdLocked(int userId, Set<String> keys, String name) {
1796 final int parentId = getGroupParentLocked(userId);
1797 if (parentId != userId && keys.contains(name)) {
1798 return parentId;
1799 }
1800 return userId;
1801 }
1802
Svetoslavf41334b2015-06-23 12:06:03 -07001803 private void enforceRestrictedSystemSettingsMutationForCallingPackage(int operation,
Xiaohui Chen43765b72015-08-31 10:57:33 -07001804 String name, int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001805 // System/root/shell can mutate whatever secure settings they want.
1806 final int callingUid = Binder.getCallingUid();
Svetoslav Ganove080da92016-12-21 17:10:35 -08001807 final int appId = UserHandle.getAppId(callingUid);
1808 if (appId == android.os.Process.SYSTEM_UID
1809 || appId == Process.SHELL_UID
1810 || appId == Process.ROOT_UID) {
Svetoslav683914b2015-01-15 14:22:26 -08001811 return;
1812 }
1813
1814 switch (operation) {
1815 case MUTATION_OPERATION_INSERT:
1816 // Insert updates.
1817 case MUTATION_OPERATION_UPDATE: {
1818 if (Settings.System.PUBLIC_SETTINGS.contains(name)) {
1819 return;
1820 }
1821
1822 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001823 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001824
1825 // Privileged apps can do whatever they want.
1826 if ((packageInfo.applicationInfo.privateFlags
1827 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1828 return;
1829 }
1830
1831 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1832 packageInfo.applicationInfo.targetSdkVersion, name);
1833 } break;
1834
1835 case MUTATION_OPERATION_DELETE: {
1836 if (Settings.System.PUBLIC_SETTINGS.contains(name)
1837 || Settings.System.PRIVATE_SETTINGS.contains(name)) {
1838 throw new IllegalArgumentException("You cannot delete system defined"
1839 + " secure settings.");
1840 }
1841
1842 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001843 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001844
1845 // Privileged apps can do whatever they want.
1846 if ((packageInfo.applicationInfo.privateFlags &
1847 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1848 return;
1849 }
1850
1851 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1852 packageInfo.applicationInfo.targetSdkVersion, name);
1853 } break;
1854 }
1855 }
1856
Todd Kennedybe0b8892017-02-15 14:13:52 -08001857 private Set<String> getInstantAppAccessibleSettings(int settingsType) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001858 switch (settingsType) {
1859 case SETTINGS_TYPE_GLOBAL:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001860 return Settings.Global.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001861 case SETTINGS_TYPE_SECURE:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001862 return Settings.Secure.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001863 case SETTINGS_TYPE_SYSTEM:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001864 return Settings.System.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001865 default:
1866 throw new IllegalArgumentException("Invalid settings type: " + settingsType);
1867 }
1868 }
1869
Chad Brubaker20e0dc32017-04-28 18:24:55 -07001870 private Set<String> getOverlayInstantAppAccessibleSettings(int settingsType) {
1871 switch (settingsType) {
1872 case SETTINGS_TYPE_GLOBAL:
1873 return OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS;
1874 case SETTINGS_TYPE_SYSTEM:
1875 return OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS;
1876 case SETTINGS_TYPE_SECURE:
1877 return OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS;
1878 default:
1879 throw new IllegalArgumentException("Invalid settings type: " + settingsType);
1880 }
1881 }
1882
Andreas Gampeb58893072018-09-05 16:52:31 -07001883 @GuardedBy("mLock")
Chad Brubaker97bccee2017-01-05 15:51:41 -08001884 private List<String> getSettingsNamesLocked(int settingsType, int userId) {
Chad Brubakerb6108d62017-12-23 20:06:44 -08001885 // Don't enforce the instant app whitelist for now -- its too prone to unintended breakage
1886 // in the current form.
1887 return mSettingsRegistry.getSettingsNamesLocked(settingsType, userId);
Chad Brubaker97bccee2017-01-05 15:51:41 -08001888 }
1889
Chad Brubakera6830e72017-04-28 17:34:36 -07001890 private void enforceSettingReadable(String settingName, int settingsType, int userId) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001891 if (UserHandle.getAppId(Binder.getCallingUid()) < Process.FIRST_APPLICATION_UID) {
1892 return;
1893 }
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001894 ApplicationInfo ai = getCallingApplicationInfoOrThrow();
Svetoslav Ganov096d3042017-01-30 16:34:13 -08001895 if (!ai.isInstantApp()) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001896 return;
1897 }
Chad Brubaker20e0dc32017-04-28 18:24:55 -07001898 if (!getInstantAppAccessibleSettings(settingsType).contains(settingName)
1899 && !getOverlayInstantAppAccessibleSettings(settingsType).contains(settingName)) {
Chad Brubakerb6108d62017-12-23 20:06:44 -08001900 // Don't enforce the instant app whitelist for now -- its too prone to unintended
1901 // breakage in the current form.
1902 Slog.w(LOG_TAG, "Instant App " + ai.packageName
1903 + " trying to access unexposed setting, this will be an error in the future.");
Chad Brubaker97bccee2017-01-05 15:51:41 -08001904 }
1905 }
1906
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001907 private ApplicationInfo getCallingApplicationInfoOrThrow() {
1908 // We always use the callingUid for this lookup. This means that if hypothetically an
1909 // app was installed in user A with cross user and in user B as an Instant App
1910 // the app in A would be able to see all the settings in user B. However since cross
1911 // user is a system permission and the app must be uninstalled in B and then installed as
1912 // an Instant App that situation is not realistic or supported.
Chad Brubaker97bccee2017-01-05 15:51:41 -08001913 ApplicationInfo ai = null;
1914 try {
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001915 ai = mPackageManager.getApplicationInfo(getCallingPackage(), 0
1916 , UserHandle.getCallingUserId());
Chad Brubaker97bccee2017-01-05 15:51:41 -08001917 } catch (RemoteException ignored) {
1918 }
1919 if (ai == null) {
1920 throw new IllegalStateException("Failed to lookup info for package "
1921 + getCallingPackage());
1922 }
1923 return ai;
1924 }
1925
Xiaohui Chen43765b72015-08-31 10:57:33 -07001926 private PackageInfo getCallingPackageInfoOrThrow(int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001927 try {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001928 PackageInfo packageInfo = mPackageManager.getPackageInfo(
1929 getCallingPackage(), 0, userId);
1930 if (packageInfo != null) {
1931 return packageInfo;
1932 }
Xiaohui Chen43765b72015-08-31 10:57:33 -07001933 } catch (RemoteException e) {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001934 /* ignore */
Svetoslav683914b2015-01-15 14:22:26 -08001935 }
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001936 throw new IllegalStateException("Calling package doesn't exist");
Svetoslav683914b2015-01-15 14:22:26 -08001937 }
1938
1939 private int getGroupParentLocked(int userId) {
1940 // Most frequent use case.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001941 if (userId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08001942 return userId;
1943 }
1944 // We are in the same process with the user manager and the returned
1945 // user info is a cached instance, so just look up instead of cache.
1946 final long identity = Binder.clearCallingIdentity();
1947 try {
Svetoslav7ec28e82015-05-20 17:01:10 -07001948 // Just a lookup and not reentrant, so holding a lock is fine.
Svetoslav683914b2015-01-15 14:22:26 -08001949 UserInfo userInfo = mUserManager.getProfileParent(userId);
1950 return (userInfo != null) ? userInfo.id : userId;
1951 } finally {
1952 Binder.restoreCallingIdentity(identity);
1953 }
1954 }
1955
Svetoslav683914b2015-01-15 14:22:26 -08001956 private void enforceWritePermission(String permission) {
1957 if (getContext().checkCallingOrSelfPermission(permission)
1958 != PackageManager.PERMISSION_GRANTED) {
1959 throw new SecurityException("Permission denial: writing to settings requires:"
1960 + permission);
1961 }
1962 }
1963
1964 /*
1965 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
1966 * This setting contains a list of the currently enabled location providers.
1967 * But helper functions in android.providers.Settings can enable or disable
1968 * a single provider by using a "+" or "-" prefix before the provider name.
1969 *
yuemingw1d13eae2018-01-30 17:27:54 +00001970 * <p>See also {@link com.android.server.pm.UserRestrictionsUtils#isSettingRestrictedForUser()}.
1971 * If DISALLOW_SHARE_LOCATION is set, the said method will only allow values with
1972 * the "-" prefix.
Makoto Onuki28da2e32015-11-20 11:30:44 -08001973 *
Svetoslav683914b2015-01-15 14:22:26 -08001974 * @returns whether the enabled location providers changed.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001975 */
Andreas Gampeb58893072018-09-05 16:52:31 -07001976 @GuardedBy("mLock")
Svetoslav Ganove080da92016-12-21 17:10:35 -08001977 private boolean updateLocationProvidersAllowedLocked(String value, String tag,
1978 int owningUserId, boolean makeDefault, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001979 if (TextUtils.isEmpty(value)) {
1980 return false;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001981 }
Maggie83e03f52018-03-16 12:22:20 -07001982 Setting oldSetting = getSecureSetting(
1983 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, owningUserId);
1984 if (oldSetting == null) {
Svetoslav683914b2015-01-15 14:22:26 -08001985 return false;
1986 }
Maggie83e03f52018-03-16 12:22:20 -07001987 String oldProviders = oldSetting.getValue();
1988 List<String> oldProvidersList = TextUtils.isEmpty(oldProviders)
1989 ? new ArrayList<>() : new ArrayList<>(Arrays.asList(oldProviders.split(",")));
1990 Set<String> newProvidersSet = new ArraySet<>();
1991 newProvidersSet.addAll(oldProvidersList);
Svetoslav683914b2015-01-15 14:22:26 -08001992
Maggie83e03f52018-03-16 12:22:20 -07001993 String[] providerUpdates = value.split(",");
1994 boolean inputError = false;
1995 for (String provider : providerUpdates) {
1996 // do not update location_providers_allowed when input is invalid
1997 if (TextUtils.isEmpty(provider)) {
1998 inputError = true;
1999 break;
Svetoslav683914b2015-01-15 14:22:26 -08002000 }
Maggie83e03f52018-03-16 12:22:20 -07002001 final char prefix = provider.charAt(0);
2002 // do not update location_providers_allowed when input is invalid
2003 if (prefix != '+' && prefix != '-') {
2004 inputError = true;
2005 break;
Svetoslav683914b2015-01-15 14:22:26 -08002006 }
Maggie83e03f52018-03-16 12:22:20 -07002007 // skip prefix
2008 provider = provider.substring(1);
2009 if (prefix == '+') {
2010 newProvidersSet.add(provider);
2011 } else if (prefix == '-') {
2012 newProvidersSet.remove(provider);
Svetoslav683914b2015-01-15 14:22:26 -08002013 }
Maggie83e03f52018-03-16 12:22:20 -07002014 }
2015 String newProviders = TextUtils.join(",", newProvidersSet.toArray());
2016 if (inputError == true || newProviders.equals(oldProviders)) {
Svetoslav683914b2015-01-15 14:22:26 -08002017 // nothing changed, so no need to update the database
Svet Ganov53a441c2016-04-19 19:38:00 -07002018 if (forceNotify) {
Maggie83e03f52018-03-16 12:22:20 -07002019 mSettingsRegistry.notifyForSettingsChange(
2020 makeKey(SETTINGS_TYPE_SECURE, owningUserId),
Svet Ganov53a441c2016-04-19 19:38:00 -07002021 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
2022 }
Svetoslav683914b2015-01-15 14:22:26 -08002023 return false;
2024 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002025 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Maggie83e03f52018-03-16 12:22:20 -07002026 owningUserId, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, newProviders, tag,
2027 makeDefault, getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav683914b2015-01-15 14:22:26 -08002028 }
2029
Svetoslav683914b2015-01-15 14:22:26 -08002030 private static void warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
2031 int targetSdkVersion, String name) {
2032 // If the app targets Lollipop MR1 or older SDK we warn, otherwise crash.
2033 if (targetSdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) {
2034 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
2035 Slog.w(LOG_TAG, "You shouldn't not change private system settings."
2036 + " This will soon become an error.");
2037 } else {
2038 Slog.w(LOG_TAG, "You shouldn't keep your settings in the secure settings."
2039 + " This will soon become an error.");
2040 }
2041 } else {
2042 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
2043 throw new IllegalArgumentException("You cannot change private secure settings.");
2044 } else {
2045 throw new IllegalArgumentException("You cannot keep your settings in"
2046 + " the secure settings.");
2047 }
2048 }
2049 }
2050
2051 private static int resolveCallingUserIdEnforcingPermissionsLocked(int requestingUserId) {
2052 if (requestingUserId == UserHandle.getCallingUserId()) {
2053 return requestingUserId;
2054 }
2055 return ActivityManager.handleIncomingUser(Binder.getCallingPid(),
2056 Binder.getCallingUid(), requestingUserId, false, true,
2057 "get/set setting for user", null);
2058 }
2059
Svet Ganov53a441c2016-04-19 19:38:00 -07002060 private Bundle packageValueForCallResult(Setting setting,
2061 boolean trackingGeneration) {
2062 if (!trackingGeneration) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002063 if (setting == null || setting.isNull()) {
Svet Ganov53a441c2016-04-19 19:38:00 -07002064 return NULL_SETTING_BUNDLE;
2065 }
2066 return Bundle.forPair(Settings.NameValueTable.VALUE, setting.getValue());
Svetoslav683914b2015-01-15 14:22:26 -08002067 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002068 Bundle result = new Bundle();
2069 result.putString(Settings.NameValueTable.VALUE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002070 !setting.isNull() ? setting.getValue() : null);
Svet Ganov96c99462017-05-05 14:27:13 -07002071
Svetoslav Ganove080da92016-12-21 17:10:35 -08002072 mSettingsRegistry.mGenerationRegistry.addGenerationData(result, setting.getKey());
Svet Ganov53a441c2016-04-19 19:38:00 -07002073 return result;
Svetoslav683914b2015-01-15 14:22:26 -08002074 }
2075
2076 private static int getRequestingUserId(Bundle args) {
2077 final int callingUserId = UserHandle.getCallingUserId();
2078 return (args != null) ? args.getInt(Settings.CALL_METHOD_USER_KEY, callingUserId)
2079 : callingUserId;
2080 }
2081
Svet Ganov53a441c2016-04-19 19:38:00 -07002082 private boolean isTrackingGeneration(Bundle args) {
2083 return args != null && args.containsKey(Settings.CALL_METHOD_TRACK_GENERATION_KEY);
2084 }
2085
Svetoslav683914b2015-01-15 14:22:26 -08002086 private static String getSettingValue(Bundle args) {
2087 return (args != null) ? args.getString(Settings.NameValueTable.VALUE) : null;
2088 }
2089
Svetoslav Ganove080da92016-12-21 17:10:35 -08002090 private static String getSettingTag(Bundle args) {
2091 return (args != null) ? args.getString(Settings.CALL_METHOD_TAG_KEY) : null;
2092 }
2093
Matt Pape6bfc62e2018-11-28 13:16:03 -08002094 private static String getSettingPrefix(Bundle args) {
2095 String prefix = (args != null) ? args.getString(Settings.CALL_METHOD_PREFIX_KEY) : null;
2096 // Append '/' to ensure we only match properties with this exact prefix.
2097 // i.e. "foo" should match "foo/property" but not "foobar/property"
2098 return prefix != null ? prefix + "/" : null;
2099 }
2100
Svetoslav Ganove080da92016-12-21 17:10:35 -08002101 private static boolean getSettingMakeDefault(Bundle args) {
2102 return (args != null) && args.getBoolean(Settings.CALL_METHOD_MAKE_DEFAULT_KEY);
2103 }
2104
2105 private static int getResetModeEnforcingPermission(Bundle args) {
2106 final int mode = (args != null) ? args.getInt(Settings.CALL_METHOD_RESET_MODE_KEY) : 0;
2107 switch (mode) {
2108 case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: {
2109 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
2110 throw new SecurityException("Only system, shell/root on a "
2111 + "debuggable build can reset to untrusted defaults");
2112 }
2113 return mode;
2114 }
2115 case Settings.RESET_MODE_UNTRUSTED_CHANGES: {
2116 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
2117 throw new SecurityException("Only system, shell/root on a "
2118 + "debuggable build can reset untrusted changes");
2119 }
2120 return mode;
2121 }
2122 case Settings.RESET_MODE_TRUSTED_DEFAULTS: {
2123 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
2124 throw new SecurityException("Only system, shell/root on a "
2125 + "debuggable build can reset to trusted defaults");
2126 }
2127 return mode;
2128 }
2129 case Settings.RESET_MODE_PACKAGE_DEFAULTS: {
2130 return mode;
2131 }
2132 }
2133 throw new IllegalArgumentException("Invalid reset mode: " + mode);
2134 }
2135
2136 private static boolean isCallerSystemOrShellOrRootOnDebuggableBuild() {
2137 final int appId = UserHandle.getAppId(Binder.getCallingUid());
2138 return appId == SYSTEM_UID || (Build.IS_DEBUGGABLE
2139 && (appId == SHELL_UID || appId == ROOT_UID));
2140 }
2141
Svetoslav683914b2015-01-15 14:22:26 -08002142 private static String getValidTableOrThrow(Uri uri) {
2143 if (uri.getPathSegments().size() > 0) {
2144 String table = uri.getPathSegments().get(0);
2145 if (DatabaseHelper.isValidTable(table)) {
2146 return table;
2147 }
2148 throw new IllegalArgumentException("Bad root path: " + table);
2149 }
2150 throw new IllegalArgumentException("Invalid URI:" + uri);
2151 }
2152
2153 private static MatrixCursor packageSettingForQuery(Setting setting, String[] projection) {
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002154 if (setting.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08002155 return new MatrixCursor(projection, 0);
2156 }
2157 MatrixCursor cursor = new MatrixCursor(projection, 1);
2158 appendSettingToCursor(cursor, setting);
2159 return cursor;
2160 }
2161
2162 private static String[] normalizeProjection(String[] projection) {
2163 if (projection == null) {
2164 return ALL_COLUMNS;
2165 }
2166
2167 final int columnCount = projection.length;
2168 for (int i = 0; i < columnCount; i++) {
2169 String column = projection[i];
2170 if (!ArrayUtils.contains(ALL_COLUMNS, column)) {
2171 throw new IllegalArgumentException("Invalid column: " + column);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002172 }
2173 }
2174
Svetoslav683914b2015-01-15 14:22:26 -08002175 return projection;
2176 }
2177
2178 private static void appendSettingToCursor(MatrixCursor cursor, Setting setting) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002179 if (setting == null || setting.isNull()) {
Suprabh Shuklac9d064a2016-04-12 18:45:34 -07002180 return;
2181 }
Svetoslav683914b2015-01-15 14:22:26 -08002182 final int columnCount = cursor.getColumnCount();
2183
2184 String[] values = new String[columnCount];
2185
2186 for (int i = 0; i < columnCount; i++) {
2187 String column = cursor.getColumnName(i);
2188
2189 switch (column) {
2190 case Settings.NameValueTable._ID: {
2191 values[i] = setting.getId();
2192 } break;
2193
2194 case Settings.NameValueTable.NAME: {
2195 values[i] = setting.getName();
2196 } break;
2197
2198 case Settings.NameValueTable.VALUE: {
2199 values[i] = setting.getValue();
2200 } break;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002201 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002202 }
2203
Svetoslav683914b2015-01-15 14:22:26 -08002204 cursor.addRow(values);
2205 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002206
Makoto Onuki3a2c35782015-06-18 11:21:58 -07002207 private static boolean isKeyValid(String key) {
2208 return !(TextUtils.isEmpty(key) || SettingsState.isBinary(key));
2209 }
2210
Matt Pape7b1c6cd2019-01-04 08:10:41 -08002211 private String resolveCallingPackage() {
2212 switch (Binder.getCallingUid()) {
2213 case Process.ROOT_UID: {
2214 return "root";
2215 }
2216
2217 case Process.SHELL_UID: {
2218 return "com.android.shell";
2219 }
2220
2221 default: {
2222 return getCallingPackage();
2223 }
2224 }
2225 }
2226
Svetoslav683914b2015-01-15 14:22:26 -08002227 private static final class Arguments {
2228 private static final Pattern WHERE_PATTERN_WITH_PARAM_NO_BRACKETS =
2229 Pattern.compile("[\\s]*name[\\s]*=[\\s]*\\?[\\s]*");
2230
2231 private static final Pattern WHERE_PATTERN_WITH_PARAM_IN_BRACKETS =
2232 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*\\?[\\s]*\\)[\\s]*");
2233
2234 private static final Pattern WHERE_PATTERN_NO_PARAM_IN_BRACKETS =
2235 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*\\)[\\s]*");
2236
2237 private static final Pattern WHERE_PATTERN_NO_PARAM_NO_BRACKETS =
2238 Pattern.compile("[\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*");
2239
2240 public final String table;
2241 public final String name;
2242
2243 public Arguments(Uri uri, String where, String[] whereArgs, boolean supportAll) {
2244 final int segmentSize = uri.getPathSegments().size();
2245 switch (segmentSize) {
2246 case 1: {
2247 if (where != null
2248 && (WHERE_PATTERN_WITH_PARAM_NO_BRACKETS.matcher(where).matches()
2249 || WHERE_PATTERN_WITH_PARAM_IN_BRACKETS.matcher(where).matches())
2250 && whereArgs.length == 1) {
2251 name = whereArgs[0];
2252 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08002253 return;
Svetoslav683914b2015-01-15 14:22:26 -08002254 } else if (where != null
2255 && (WHERE_PATTERN_NO_PARAM_NO_BRACKETS.matcher(where).matches()
2256 || WHERE_PATTERN_NO_PARAM_IN_BRACKETS.matcher(where).matches())) {
2257 final int startIndex = Math.max(where.indexOf("'"),
2258 where.indexOf("\"")) + 1;
2259 final int endIndex = Math.max(where.lastIndexOf("'"),
2260 where.lastIndexOf("\""));
2261 name = where.substring(startIndex, endIndex);
2262 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08002263 return;
Svetoslav683914b2015-01-15 14:22:26 -08002264 } else if (supportAll && where == null && whereArgs == null) {
2265 name = null;
2266 table = computeTableForSetting(uri, null);
Svetoslav28494652015-02-12 14:11:42 -08002267 return;
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002268 }
Svetoslav683914b2015-01-15 14:22:26 -08002269 } break;
2270
Svetoslav28494652015-02-12 14:11:42 -08002271 case 2: {
2272 if (where == null && whereArgs == null) {
2273 name = uri.getPathSegments().get(1);
2274 table = computeTableForSetting(uri, name);
2275 return;
2276 }
2277 } break;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002278 }
Svetoslav28494652015-02-12 14:11:42 -08002279
2280 EventLogTags.writeUnsupportedSettingsQuery(
2281 uri.toSafeString(), where, Arrays.toString(whereArgs));
2282 String message = String.format( "Supported SQL:\n"
2283 + " uri content://some_table/some_property with null where and where args\n"
2284 + " uri content://some_table with query name=? and single name as arg\n"
2285 + " uri content://some_table with query name=some_name and null args\n"
2286 + " but got - uri:%1s, where:%2s whereArgs:%3s", uri, where,
2287 Arrays.toString(whereArgs));
2288 throw new IllegalArgumentException(message);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002289 }
2290
Svetoslav28494652015-02-12 14:11:42 -08002291 private static String computeTableForSetting(Uri uri, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08002292 String table = getValidTableOrThrow(uri);
2293
2294 if (name != null) {
2295 if (sSystemMovedToSecureSettings.contains(name)) {
2296 table = TABLE_SECURE;
2297 }
2298
2299 if (sSystemMovedToGlobalSettings.contains(name)) {
2300 table = TABLE_GLOBAL;
2301 }
2302
2303 if (sSecureMovedToGlobalSettings.contains(name)) {
2304 table = TABLE_GLOBAL;
2305 }
2306
2307 if (sGlobalMovedToSecureSettings.contains(name)) {
2308 table = TABLE_SECURE;
2309 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002310 }
Svetoslav683914b2015-01-15 14:22:26 -08002311
2312 return table;
2313 }
2314 }
2315
2316 final class SettingsRegistry {
2317 private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid";
2318
Svetoslav683914b2015-01-15 14:22:26 -08002319 private static final String SETTINGS_FILE_GLOBAL = "settings_global.xml";
2320 private static final String SETTINGS_FILE_SYSTEM = "settings_system.xml";
2321 private static final String SETTINGS_FILE_SECURE = "settings_secure.xml";
Mark Rathjend891f012017-01-19 04:10:37 +00002322 private static final String SETTINGS_FILE_SSAID = "settings_ssaid.xml";
Matt Pape1b31a332018-10-17 09:58:28 -07002323 private static final String SETTINGS_FILE_CONFIG = "settings_config.xml";
Mark Rathjend891f012017-01-19 04:10:37 +00002324
2325 private static final String SSAID_USER_KEY = "userkey";
Svetoslav683914b2015-01-15 14:22:26 -08002326
2327 private final SparseArray<SettingsState> mSettingsStates = new SparseArray<>();
2328
Svet Ganov53a441c2016-04-19 19:38:00 -07002329 private GenerationRegistry mGenerationRegistry;
Svetoslav683914b2015-01-15 14:22:26 -08002330
Svetoslav7e0683b2015-08-03 16:02:52 -07002331 private final Handler mHandler;
2332
Svet Ganov53a441c2016-04-19 19:38:00 -07002333 private final BackupManager mBackupManager;
2334
Amith Yamasani39452022017-03-21 15:23:47 -07002335 private String mSettingsCreationBuildId;
2336
Svetoslav683914b2015-01-15 14:22:26 -08002337 public SettingsRegistry() {
Svetoslav7e0683b2015-08-03 16:02:52 -07002338 mHandler = new MyHandler(getContext().getMainLooper());
Svet Ganov53a441c2016-04-19 19:38:00 -07002339 mGenerationRegistry = new GenerationRegistry(mLock);
2340 mBackupManager = new BackupManager(getContext());
Svetoslav683914b2015-01-15 14:22:26 -08002341 migrateAllLegacySettingsIfNeeded();
Mark Rathjend891f012017-01-19 04:10:37 +00002342 syncSsaidTableOnStart();
2343 }
2344
2345 private void generateUserKeyLocked(int userId) {
2346 // Generate a random key for each user used for creating a new ssaid.
Mark Rathjen7599f132017-01-23 14:15:54 -08002347 final byte[] keyBytes = new byte[32];
Mark Rathjend891f012017-01-19 04:10:37 +00002348 final SecureRandom rand = new SecureRandom();
2349 rand.nextBytes(keyBytes);
2350
2351 // Convert to string for storage in settings table.
Neil Fuller3447fb42019-04-08 22:03:14 +01002352 final String userKey = HexEncoding.encodeToString(keyBytes, true /* upperCase */);
Mark Rathjend891f012017-01-19 04:10:37 +00002353
2354 // Store the key in the ssaid table.
2355 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
2356 final boolean success = ssaidSettings.insertSettingLocked(SSAID_USER_KEY, userKey, null,
2357 true, SettingsState.SYSTEM_PACKAGE_NAME);
2358
2359 if (!success) {
2360 throw new IllegalStateException("Ssaid settings not accessible");
2361 }
2362 }
2363
Mark Rathjen7599f132017-01-23 14:15:54 -08002364 private byte[] getLengthPrefix(byte[] data) {
2365 return ByteBuffer.allocate(4).putInt(data.length).array();
2366 }
2367
Christopher Tateb218e762017-04-05 16:34:07 -07002368 public Setting generateSsaidLocked(PackageInfo callingPkg, int userId) {
Mark Rathjend891f012017-01-19 04:10:37 +00002369 // Read the user's key from the ssaid table.
2370 Setting userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY);
Mark Rathjenea617592017-01-18 23:03:41 -08002371 if (userKeySetting == null || userKeySetting.isNull()
2372 || userKeySetting.getValue() == null) {
Mark Rathjend891f012017-01-19 04:10:37 +00002373 // Lazy initialize and store the user key.
2374 generateUserKeyLocked(userId);
2375 userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY);
Mark Rathjenea617592017-01-18 23:03:41 -08002376 if (userKeySetting == null || userKeySetting.isNull()
2377 || userKeySetting.getValue() == null) {
Mark Rathjend891f012017-01-19 04:10:37 +00002378 throw new IllegalStateException("User key not accessible");
2379 }
2380 }
2381 final String userKey = userKeySetting.getValue();
Neil Fuller3447fb42019-04-08 22:03:14 +01002382 if (userKey == null || userKey.length() % 2 != 0) {
2383 throw new IllegalStateException("User key invalid");
2384 }
Mark Rathjend891f012017-01-19 04:10:37 +00002385
2386 // Convert the user's key back to a byte array.
Neil Fuller3447fb42019-04-08 22:03:14 +01002387 final byte[] keyBytes = HexEncoding.decode(userKey);
Mark Rathjen7599f132017-01-23 14:15:54 -08002388
2389 // Validate that the key is of expected length.
2390 // Keys are currently 32 bytes, but were once 16 bytes during Android O development.
Neil Fuller3447fb42019-04-08 22:03:14 +01002391 if (keyBytes.length != 16 && keyBytes.length != 32) {
Mark Rathjend891f012017-01-19 04:10:37 +00002392 throw new IllegalStateException("User key invalid");
2393 }
2394
Mark Rathjen7599f132017-01-23 14:15:54 -08002395 final Mac m;
Mark Rathjend891f012017-01-19 04:10:37 +00002396 try {
Mark Rathjen7599f132017-01-23 14:15:54 -08002397 m = Mac.getInstance("HmacSHA256");
2398 m.init(new SecretKeySpec(keyBytes, m.getAlgorithm()));
Mark Rathjend891f012017-01-19 04:10:37 +00002399 } catch (NoSuchAlgorithmException e) {
Mark Rathjen7599f132017-01-23 14:15:54 -08002400 throw new IllegalStateException("HmacSHA256 is not available", e);
2401 } catch (InvalidKeyException e) {
2402 throw new IllegalStateException("Key is corrupted", e);
Mark Rathjend891f012017-01-19 04:10:37 +00002403 }
Mark Rathjen7599f132017-01-23 14:15:54 -08002404
Mark Rathjenf42dd912017-06-05 19:04:34 -07002405 // Mac each of the developer signatures.
Christopher Tateb218e762017-04-05 16:34:07 -07002406 for (int i = 0; i < callingPkg.signatures.length; i++) {
2407 byte[] sig = callingPkg.signatures[i].toByteArray();
Mark Rathjen7599f132017-01-23 14:15:54 -08002408 m.update(getLengthPrefix(sig), 0, 4);
2409 m.update(sig);
2410 }
Mark Rathjend891f012017-01-19 04:10:37 +00002411
2412 // Convert result to a string for storage in settings table. Only want first 64 bits.
Neil Fuller3447fb42019-04-08 22:03:14 +01002413 final String ssaid = HexEncoding.encodeToString(m.doFinal(), false /* upperCase */)
2414 .substring(0, 16);
Mark Rathjend891f012017-01-19 04:10:37 +00002415
2416 // Save the ssaid in the ssaid table.
Christopher Tateb218e762017-04-05 16:34:07 -07002417 final String uid = Integer.toString(callingPkg.applicationInfo.uid);
Mark Rathjend891f012017-01-19 04:10:37 +00002418 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
2419 final boolean success = ssaidSettings.insertSettingLocked(uid, ssaid, null, true,
Mark Rathjenf42dd912017-06-05 19:04:34 -07002420 callingPkg.packageName);
Mark Rathjend891f012017-01-19 04:10:37 +00002421
2422 if (!success) {
2423 throw new IllegalStateException("Ssaid settings not accessible");
2424 }
2425
2426 return getSettingLocked(SETTINGS_TYPE_SSAID, userId, uid);
2427 }
2428
2429 public void syncSsaidTableOnStart() {
2430 synchronized (mLock) {
2431 // Verify that each user's packages and ssaid's are in sync.
2432 for (UserInfo user : mUserManager.getUsers(true)) {
2433 // Get all uids for the user's packages.
2434 final List<PackageInfo> packages;
2435 try {
Tetsutoki Shiozawaebe0e5f2018-01-17 11:07:09 +09002436 packages = mPackageManager.getInstalledPackages(
2437 PackageManager.MATCH_UNINSTALLED_PACKAGES,
2438 user.id).getList();
Mark Rathjend891f012017-01-19 04:10:37 +00002439 } catch (RemoteException e) {
2440 throw new IllegalStateException("Package manager not available");
2441 }
2442 final Set<String> appUids = new HashSet<>();
2443 for (PackageInfo info : packages) {
2444 appUids.add(Integer.toString(info.applicationInfo.uid));
2445 }
2446
2447 // Get all uids currently stored in the user's ssaid table.
2448 final Set<String> ssaidUids = new HashSet<>(
2449 getSettingsNamesLocked(SETTINGS_TYPE_SSAID, user.id));
2450 ssaidUids.remove(SSAID_USER_KEY);
2451
2452 // Perform a set difference for the appUids and ssaidUids.
2453 ssaidUids.removeAll(appUids);
2454
2455 // If there are ssaidUids left over they need to be removed from the table.
2456 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID,
2457 user.id);
2458 for (String uid : ssaidUids) {
2459 ssaidSettings.deleteSettingLocked(uid);
2460 }
2461 }
2462 }
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002463 }
2464
Svetoslav683914b2015-01-15 14:22:26 -08002465 public List<String> getSettingsNamesLocked(int type, int userId) {
2466 final int key = makeKey(type, userId);
2467 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002468 if (settingsState == null) {
2469 return new ArrayList<String>();
2470 }
Svetoslav683914b2015-01-15 14:22:26 -08002471 return settingsState.getSettingNamesLocked();
2472 }
2473
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002474 public SparseBooleanArray getKnownUsersLocked() {
2475 SparseBooleanArray users = new SparseBooleanArray();
2476 for (int i = mSettingsStates.size()-1; i >= 0; i--) {
2477 users.put(getUserIdFromKey(mSettingsStates.keyAt(i)), true);
2478 }
2479 return users;
2480 }
2481
Kweku Adamsb0886f32017-10-31 15:32:09 -07002482 @Nullable
Svetoslav683914b2015-01-15 14:22:26 -08002483 public SettingsState getSettingsLocked(int type, int userId) {
2484 final int key = makeKey(type, userId);
2485 return peekSettingsStateLocked(key);
2486 }
2487
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002488 public boolean ensureSettingsForUserLocked(int userId) {
2489 // First make sure this user actually exists.
2490 if (mUserManager.getUserInfo(userId) == null) {
2491 Slog.wtf(LOG_TAG, "Requested user " + userId + " does not exist");
2492 return false;
2493 }
2494
Svetoslav683914b2015-01-15 14:22:26 -08002495 // Migrate the setting for this user if needed.
2496 migrateLegacySettingsForUserIfNeededLocked(userId);
2497
Matt Pape1b31a332018-10-17 09:58:28 -07002498 // Ensure config settings loaded if owner.
2499 if (userId == UserHandle.USER_SYSTEM) {
2500 final int configKey
2501 = makeKey(SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM);
2502 ensureSettingsStateLocked(configKey);
2503 }
2504
Svetoslav683914b2015-01-15 14:22:26 -08002505 // Ensure global settings loaded if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07002506 if (userId == UserHandle.USER_SYSTEM) {
2507 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002508 ensureSettingsStateLocked(globalKey);
2509 }
2510
2511 // Ensure secure settings loaded.
2512 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2513 ensureSettingsStateLocked(secureKey);
2514
2515 // Make sure the secure settings have an Android id set.
2516 SettingsState secureSettings = getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
2517 ensureSecureSettingAndroidIdSetLocked(secureSettings);
2518
2519 // Ensure system settings loaded.
2520 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2521 ensureSettingsStateLocked(systemKey);
2522
Mark Rathjend891f012017-01-19 04:10:37 +00002523 // Ensure secure settings loaded.
2524 final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId);
2525 ensureSettingsStateLocked(ssaidKey);
2526
Svetoslav683914b2015-01-15 14:22:26 -08002527 // Upgrade the settings to the latest version.
2528 UpgradeController upgrader = new UpgradeController(userId);
2529 upgrader.upgradeIfNeededLocked();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002530 return true;
Svetoslav683914b2015-01-15 14:22:26 -08002531 }
2532
2533 private void ensureSettingsStateLocked(int key) {
2534 if (mSettingsStates.get(key) == null) {
2535 final int maxBytesPerPackage = getMaxBytesPerPackageForType(getTypeFromKey(key));
Svetoslav Ganove080da92016-12-21 17:10:35 -08002536 SettingsState settingsState = new SettingsState(getContext(), mLock,
2537 getSettingsFile(key), key, maxBytesPerPackage, mHandlerThread.getLooper());
Svetoslav683914b2015-01-15 14:22:26 -08002538 mSettingsStates.put(key, settingsState);
2539 }
2540 }
2541
2542 public void removeUserStateLocked(int userId, boolean permanently) {
2543 // We always keep the global settings in memory.
2544
2545 // Nuke system settings.
2546 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2547 final SettingsState systemSettingsState = mSettingsStates.get(systemKey);
2548 if (systemSettingsState != null) {
2549 if (permanently) {
2550 mSettingsStates.remove(systemKey);
2551 systemSettingsState.destroyLocked(null);
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002552 } else {
Svetoslav683914b2015-01-15 14:22:26 -08002553 systemSettingsState.destroyLocked(new Runnable() {
2554 @Override
2555 public void run() {
2556 mSettingsStates.remove(systemKey);
2557 }
2558 });
2559 }
2560 }
2561
2562 // Nuke secure settings.
2563 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2564 final SettingsState secureSettingsState = mSettingsStates.get(secureKey);
2565 if (secureSettingsState != null) {
2566 if (permanently) {
2567 mSettingsStates.remove(secureKey);
2568 secureSettingsState.destroyLocked(null);
2569 } else {
2570 secureSettingsState.destroyLocked(new Runnable() {
2571 @Override
2572 public void run() {
2573 mSettingsStates.remove(secureKey);
2574 }
2575 });
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002576 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002577 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002578
Mark Rathjend891f012017-01-19 04:10:37 +00002579 // Nuke ssaid settings.
2580 final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId);
2581 final SettingsState ssaidSettingsState = mSettingsStates.get(ssaidKey);
2582 if (ssaidSettingsState != null) {
2583 if (permanently) {
2584 mSettingsStates.remove(ssaidKey);
2585 ssaidSettingsState.destroyLocked(null);
2586 } else {
2587 ssaidSettingsState.destroyLocked(new Runnable() {
2588 @Override
2589 public void run() {
2590 mSettingsStates.remove(ssaidKey);
2591 }
2592 });
2593 }
2594 }
2595
Svet Ganov53a441c2016-04-19 19:38:00 -07002596 // Nuke generation tracking data
2597 mGenerationRegistry.onUserRemoved(userId);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002598 }
2599
Svetoslav683914b2015-01-15 14:22:26 -08002600 public boolean insertSettingLocked(int type, int userId, String name, String value,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002601 String tag, boolean makeDefault, String packageName, boolean forceNotify,
2602 Set<String> criticalSettings) {
Matt Papec1323dc2018-12-11 12:32:42 -08002603 return insertSettingLocked(type, userId, name, value, tag, makeDefault, false,
2604 packageName, forceNotify, criticalSettings);
2605 }
2606
2607 public boolean insertSettingLocked(int type, int userId, String name, String value,
2608 String tag, boolean makeDefault, boolean forceNonSystemPackage, String packageName,
2609 boolean forceNotify, Set<String> criticalSettings) {
Svetoslav683914b2015-01-15 14:22:26 -08002610 final int key = makeKey(type, userId);
2611
Svetoslav Ganove080da92016-12-21 17:10:35 -08002612 boolean success = false;
Svetoslav683914b2015-01-15 14:22:26 -08002613 SettingsState settingsState = peekSettingsStateLocked(key);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002614 if (settingsState != null) {
2615 success = settingsState.insertSettingLocked(name, value,
Matt Papec1323dc2018-12-11 12:32:42 -08002616 tag, makeDefault, forceNonSystemPackage, packageName);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002617 }
Svetoslav683914b2015-01-15 14:22:26 -08002618
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002619 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2620 settingsState.persistSyncLocked();
2621 }
2622
Svet Ganov53a441c2016-04-19 19:38:00 -07002623 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08002624 notifyForSettingsChange(key, name);
2625 }
2626 return success;
2627 }
2628
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002629 public boolean deleteSettingLocked(int type, int userId, String name, boolean forceNotify,
2630 Set<String> criticalSettings) {
Svetoslav683914b2015-01-15 14:22:26 -08002631 final int key = makeKey(type, userId);
2632
Svetoslav Ganove080da92016-12-21 17:10:35 -08002633 boolean success = false;
Svetoslav683914b2015-01-15 14:22:26 -08002634 SettingsState settingsState = peekSettingsStateLocked(key);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002635 if (settingsState != null) {
2636 success = settingsState.deleteSettingLocked(name);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002637 }
Svetoslav683914b2015-01-15 14:22:26 -08002638
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002639 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2640 settingsState.persistSyncLocked();
2641 }
2642
Svet Ganov53a441c2016-04-19 19:38:00 -07002643 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08002644 notifyForSettingsChange(key, name);
2645 }
2646 return success;
2647 }
2648
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002649 public boolean updateSettingLocked(int type, int userId, String name, String value,
2650 String tag, boolean makeDefault, String packageName, boolean forceNotify,
2651 Set<String> criticalSettings) {
2652 final int key = makeKey(type, userId);
2653
2654 boolean success = false;
2655 SettingsState settingsState = peekSettingsStateLocked(key);
2656 if (settingsState != null) {
2657 success = settingsState.updateSettingLocked(name, value, tag,
2658 makeDefault, packageName);
2659 }
2660
2661 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2662 settingsState.persistSyncLocked();
2663 }
2664
2665 if (forceNotify || success) {
2666 notifyForSettingsChange(key, name);
2667 }
2668
2669 return success;
2670 }
2671
Svetoslav683914b2015-01-15 14:22:26 -08002672 public Setting getSettingLocked(int type, int userId, String name) {
2673 final int key = makeKey(type, userId);
2674
2675 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002676 if (settingsState == null) {
Mark Rathjenea617592017-01-18 23:03:41 -08002677 return null;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002678 }
Mark Rathjend891f012017-01-19 04:10:37 +00002679
2680 // getSettingLocked will return non-null result
Svetoslav683914b2015-01-15 14:22:26 -08002681 return settingsState.getSettingLocked(name);
2682 }
2683
Svetoslav Ganove080da92016-12-21 17:10:35 -08002684 public void resetSettingsLocked(int type, int userId, String packageName, int mode,
2685 String tag) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002686 resetSettingsLocked(type, userId, packageName, mode, tag, null);
2687 }
2688
2689 public void resetSettingsLocked(int type, int userId, String packageName, int mode,
2690 String tag, @Nullable String prefix) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08002691 final int key = makeKey(type, userId);
2692 SettingsState settingsState = peekSettingsStateLocked(key);
2693 if (settingsState == null) {
2694 return;
2695 }
2696
2697 switch (mode) {
2698 case Settings.RESET_MODE_PACKAGE_DEFAULTS: {
2699 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002700 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002701 Setting setting = settingsState.getSettingLocked(name);
2702 if (packageName.equals(setting.getPackageName())) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002703 if ((tag != null && !tag.equals(setting.getTag()))
2704 || (prefix != null && !setting.getName().startsWith(prefix))) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08002705 continue;
2706 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002707 if (settingsState.resetSettingLocked(name)) {
2708 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002709 notifyForSettingsChange(key, name);
2710 }
2711 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002712 if (someSettingChanged) {
2713 settingsState.persistSyncLocked();
2714 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002715 }
2716 } break;
2717
2718 case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: {
2719 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002720 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002721 Setting setting = settingsState.getSettingLocked(name);
2722 if (!SettingsState.isSystemPackage(getContext(),
Matt Pape25c940d2019-03-26 12:14:33 -07002723 setting.getPackageName(), INVALID_UID, userId)) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002724 if (prefix != null && !setting.getName().startsWith(prefix)) {
2725 continue;
2726 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002727 if (settingsState.resetSettingLocked(name)) {
2728 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002729 notifyForSettingsChange(key, name);
2730 }
2731 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002732 if (someSettingChanged) {
2733 settingsState.persistSyncLocked();
2734 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002735 }
2736 } break;
2737
2738 case Settings.RESET_MODE_UNTRUSTED_CHANGES: {
2739 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002740 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002741 Setting setting = settingsState.getSettingLocked(name);
2742 if (!SettingsState.isSystemPackage(getContext(),
Matt Pape25c940d2019-03-26 12:14:33 -07002743 setting.getPackageName(), INVALID_UID, userId)) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002744 if (prefix != null && !setting.getName().startsWith(prefix)) {
2745 continue;
2746 }
Eugene Suslad72c3972016-12-27 15:49:30 -08002747 if (setting.isDefaultFromSystem()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002748 if (settingsState.resetSettingLocked(name)) {
2749 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002750 notifyForSettingsChange(key, name);
2751 }
2752 } else if (settingsState.deleteSettingLocked(name)) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002753 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002754 notifyForSettingsChange(key, name);
2755 }
2756 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002757 if (someSettingChanged) {
2758 settingsState.persistSyncLocked();
2759 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002760 }
2761 } break;
2762
2763 case Settings.RESET_MODE_TRUSTED_DEFAULTS: {
2764 for (String name : settingsState.getSettingNamesLocked()) {
2765 Setting setting = settingsState.getSettingLocked(name);
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002766 boolean someSettingChanged = false;
Matt Pape6bfc62e2018-11-28 13:16:03 -08002767 if (prefix != null && !setting.getName().startsWith(prefix)) {
2768 continue;
2769 }
Eugene Suslad72c3972016-12-27 15:49:30 -08002770 if (setting.isDefaultFromSystem()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002771 if (settingsState.resetSettingLocked(name)) {
2772 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002773 notifyForSettingsChange(key, name);
2774 }
2775 } else if (settingsState.deleteSettingLocked(name)) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002776 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002777 notifyForSettingsChange(key, name);
2778 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002779 if (someSettingChanged) {
2780 settingsState.persistSyncLocked();
2781 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002782 }
2783 } break;
2784 }
2785 }
2786
Zimuzoc56192c2018-07-25 10:40:01 +01002787 public void removeSettingsForPackageLocked(String packageName, int userId) {
Svet Ganov8de34802015-04-27 09:33:40 -07002788 // Global and secure settings are signature protected. Apps signed
2789 // by the platform certificate are generally not uninstalled and
2790 // the main exception is tests. We trust components signed
2791 // by the platform certificate and do not do a clean up after them.
Svetoslav683914b2015-01-15 14:22:26 -08002792
2793 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2794 SettingsState systemSettings = mSettingsStates.get(systemKey);
Svet Ganov8de34802015-04-27 09:33:40 -07002795 if (systemSettings != null) {
Zimuzoc56192c2018-07-25 10:40:01 +01002796 systemSettings.removeSettingsForPackageLocked(packageName);
Svet Ganov8de34802015-04-27 09:33:40 -07002797 }
Svetoslav683914b2015-01-15 14:22:26 -08002798 }
2799
Mark Rathjend891f012017-01-19 04:10:37 +00002800 public void onUidRemovedLocked(int uid) {
2801 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID,
2802 UserHandle.getUserId(uid));
Tony Mak24c6ab42018-01-08 14:35:34 +00002803 if (ssaidSettings != null) {
2804 ssaidSettings.deleteSettingLocked(Integer.toString(uid));
2805 }
Mark Rathjend891f012017-01-19 04:10:37 +00002806 }
2807
Kweku Adamsb0886f32017-10-31 15:32:09 -07002808 @Nullable
Svetoslav683914b2015-01-15 14:22:26 -08002809 private SettingsState peekSettingsStateLocked(int key) {
2810 SettingsState settingsState = mSettingsStates.get(key);
2811 if (settingsState != null) {
2812 return settingsState;
2813 }
2814
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002815 if (!ensureSettingsForUserLocked(getUserIdFromKey(key))) {
2816 return null;
2817 }
Svetoslav683914b2015-01-15 14:22:26 -08002818 return mSettingsStates.get(key);
2819 }
2820
2821 private void migrateAllLegacySettingsIfNeeded() {
2822 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07002823 final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002824 File globalFile = getSettingsFile(key);
Amith Yamasani74bf71b2017-07-19 16:49:52 -07002825 if (SettingsState.stateFileExists(globalFile)) {
Svetoslav683914b2015-01-15 14:22:26 -08002826 return;
2827 }
2828
Amith Yamasani39452022017-03-21 15:23:47 -07002829 mSettingsCreationBuildId = Build.ID;
2830
Svetoslav683914b2015-01-15 14:22:26 -08002831 final long identity = Binder.clearCallingIdentity();
2832 try {
2833 List<UserInfo> users = mUserManager.getUsers(true);
2834
2835 final int userCount = users.size();
2836 for (int i = 0; i < userCount; i++) {
2837 final int userId = users.get(i).id;
2838
2839 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
2840 SQLiteDatabase database = dbHelper.getWritableDatabase();
2841 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
2842
2843 // Upgrade to the latest version.
2844 UpgradeController upgrader = new UpgradeController(userId);
2845 upgrader.upgradeIfNeededLocked();
2846
2847 // Drop from memory if not a running user.
2848 if (!mUserManager.isUserRunning(new UserHandle(userId))) {
2849 removeUserStateLocked(userId, false);
2850 }
2851 }
2852 } finally {
2853 Binder.restoreCallingIdentity(identity);
2854 }
2855 }
2856 }
2857
2858 private void migrateLegacySettingsForUserIfNeededLocked(int userId) {
2859 // Every user has secure settings and if no file we need to migrate.
2860 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2861 File secureFile = getSettingsFile(secureKey);
Amith Yamasani74bf71b2017-07-19 16:49:52 -07002862 if (SettingsState.stateFileExists(secureFile)) {
Svetoslav683914b2015-01-15 14:22:26 -08002863 return;
2864 }
2865
2866 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
2867 SQLiteDatabase database = dbHelper.getWritableDatabase();
2868
2869 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
2870 }
2871
2872 private void migrateLegacySettingsForUserLocked(DatabaseHelper dbHelper,
2873 SQLiteDatabase database, int userId) {
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002874 // Move over the system settings.
2875 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2876 ensureSettingsStateLocked(systemKey);
2877 SettingsState systemSettings = mSettingsStates.get(systemKey);
2878 migrateLegacySettingsLocked(systemSettings, database, TABLE_SYSTEM);
2879 systemSettings.persistSyncLocked();
Svetoslav683914b2015-01-15 14:22:26 -08002880
2881 // Move over the secure settings.
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002882 // Do this after System settings, since this is the first thing we check when deciding
2883 // to skip over migration from db to xml for a secondary user.
Svetoslav683914b2015-01-15 14:22:26 -08002884 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2885 ensureSettingsStateLocked(secureKey);
2886 SettingsState secureSettings = mSettingsStates.get(secureKey);
2887 migrateLegacySettingsLocked(secureSettings, database, TABLE_SECURE);
2888 ensureSecureSettingAndroidIdSetLocked(secureSettings);
2889 secureSettings.persistSyncLocked();
2890
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002891 // Move over the global settings if owner.
2892 // Do this last, since this is the first thing we check when deciding
2893 // to skip over migration from db to xml for owner user.
2894 if (userId == UserHandle.USER_SYSTEM) {
2895 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, userId);
2896 ensureSettingsStateLocked(globalKey);
2897 SettingsState globalSettings = mSettingsStates.get(globalKey);
2898 migrateLegacySettingsLocked(globalSettings, database, TABLE_GLOBAL);
Amith Yamasani39452022017-03-21 15:23:47 -07002899 // If this was just created
2900 if (mSettingsCreationBuildId != null) {
2901 globalSettings.insertSettingLocked(Settings.Global.DATABASE_CREATION_BUILDID,
2902 mSettingsCreationBuildId, null, true,
2903 SettingsState.SYSTEM_PACKAGE_NAME);
2904 }
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002905 globalSettings.persistSyncLocked();
2906 }
Svetoslav683914b2015-01-15 14:22:26 -08002907
2908 // Drop the database as now all is moved and persisted.
2909 if (DROP_DATABASE_ON_MIGRATION) {
2910 dbHelper.dropDatabase();
2911 } else {
2912 dbHelper.backupDatabase();
2913 }
2914 }
2915
2916 private void migrateLegacySettingsLocked(SettingsState settingsState,
2917 SQLiteDatabase database, String table) {
2918 SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
2919 queryBuilder.setTables(table);
2920
2921 Cursor cursor = queryBuilder.query(database, ALL_COLUMNS,
2922 null, null, null, null, null);
2923
2924 if (cursor == null) {
2925 return;
2926 }
2927
2928 try {
2929 if (!cursor.moveToFirst()) {
2930 return;
2931 }
2932
2933 final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME);
2934 final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE);
2935
2936 settingsState.setVersionLocked(database.getVersion());
2937
2938 while (!cursor.isAfterLast()) {
2939 String name = cursor.getString(nameColumnIdx);
2940 String value = cursor.getString(valueColumnIdx);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002941 settingsState.insertSettingLocked(name, value, null, true,
Svetoslav683914b2015-01-15 14:22:26 -08002942 SettingsState.SYSTEM_PACKAGE_NAME);
2943 cursor.moveToNext();
2944 }
2945 } finally {
2946 cursor.close();
2947 }
2948 }
2949
Andreas Gampeb58893072018-09-05 16:52:31 -07002950 @GuardedBy("secureSettings.mLock")
Svetoslav683914b2015-01-15 14:22:26 -08002951 private void ensureSecureSettingAndroidIdSetLocked(SettingsState secureSettings) {
2952 Setting value = secureSettings.getSettingLocked(Settings.Secure.ANDROID_ID);
2953
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09002954 if (!value.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08002955 return;
2956 }
2957
2958 final int userId = getUserIdFromKey(secureSettings.mKey);
2959
2960 final UserInfo user;
2961 final long identity = Binder.clearCallingIdentity();
2962 try {
2963 user = mUserManager.getUserInfo(userId);
2964 } finally {
2965 Binder.restoreCallingIdentity(identity);
2966 }
2967 if (user == null) {
2968 // Can happen due to races when deleting users - treat as benign.
2969 return;
2970 }
2971
2972 String androidId = Long.toHexString(new SecureRandom().nextLong());
2973 secureSettings.insertSettingLocked(Settings.Secure.ANDROID_ID, androidId,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002974 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Svetoslav683914b2015-01-15 14:22:26 -08002975
2976 Slog.d(LOG_TAG, "Generated and saved new ANDROID_ID [" + androidId
2977 + "] for user " + userId);
2978
2979 // Write a drop box entry if it's a restricted profile
2980 if (user.isRestricted()) {
2981 DropBoxManager dbm = (DropBoxManager) getContext().getSystemService(
2982 Context.DROPBOX_SERVICE);
2983 if (dbm != null && dbm.isTagEnabled(DROPBOX_TAG_USERLOG)) {
2984 dbm.addText(DROPBOX_TAG_USERLOG, System.currentTimeMillis()
2985 + "," + DROPBOX_TAG_USERLOG + "," + androidId + "\n");
2986 }
2987 }
2988 }
2989
2990 private void notifyForSettingsChange(int key, String name) {
Svet Ganov945864c2018-03-22 21:49:10 -07002991 // Increment the generation first, so observers always see the new value
Phil Weaver83fec002016-05-11 10:55:29 -07002992 mGenerationRegistry.incrementGeneration(key);
2993
Linus Tufvesson93c38552019-05-30 12:47:39 +01002994 if (isGlobalSettingsKey(key) || isConfigSettingsKey(key)) {
Jeff Sharkey308093f2018-03-25 22:53:29 -06002995 final long token = Binder.clearCallingIdentity();
2996 try {
Linus Tufvesson93c38552019-05-30 12:47:39 +01002997 if (Global.LOCATION_GLOBAL_KILL_SWITCH.equals(name)
2998 && isGlobalSettingsKey(key)) {
Jeff Sharkey308093f2018-03-25 22:53:29 -06002999 // When the global kill switch is updated, send the
3000 // change notification for the location setting.
3001 notifyLocationChangeForRunningUsers();
3002 }
Linus Tufvesson93c38552019-05-30 12:47:39 +01003003 notifySettingChangeForRunningUsers(key, name);
Jeff Sharkey308093f2018-03-25 22:53:29 -06003004 } finally {
3005 Binder.restoreCallingIdentity(token);
Svet Ganov945864c2018-03-22 21:49:10 -07003006 }
Svet Ganov945864c2018-03-22 21:49:10 -07003007 } else {
3008 final int userId = getUserIdFromKey(key);
3009 final Uri uri = getNotificationUriFor(key, name);
3010 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
3011 userId, 0, uri).sendToTarget();
3012 if (isSecureSettingsKey(key)) {
3013 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
3014 sSecureCloneToManagedSettings);
3015 maybeNotifyProfiles(SETTINGS_TYPE_SYSTEM, userId, uri, name,
3016 sSystemCloneFromParentOnDependency.values());
3017 } else if (isSystemSettingsKey(key)) {
3018 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
3019 sSystemCloneToManagedSettings);
3020 }
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003021 }
Svet Ganov53a441c2016-04-19 19:38:00 -07003022
Svet Ganov945864c2018-03-22 21:49:10 -07003023 // Always notify that our data changed
Svet Ganov53a441c2016-04-19 19:38:00 -07003024 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003025 }
3026
Svet Ganov53a441c2016-04-19 19:38:00 -07003027 private void maybeNotifyProfiles(int type, int userId, Uri uri, String name,
Robin Lee7af9a742017-02-20 14:47:30 +00003028 Collection<String> keysCloned) {
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003029 if (keysCloned.contains(name)) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003030 for (int profileId : mUserManager.getProfileIdsWithDisabled(userId)) {
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003031 // the notification for userId has already been sent.
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003032 if (profileId != userId) {
Svet Ganov945864c2018-03-22 21:49:10 -07003033 final int key = makeKey(type, profileId);
3034 // Increment the generation first, so observers always see the new value
3035 mGenerationRegistry.incrementGeneration(key);
Svetoslav7e0683b2015-08-03 16:02:52 -07003036 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003037 profileId, 0, uri).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003038 }
3039 }
3040 }
Svetoslav683914b2015-01-15 14:22:26 -08003041 }
3042
Linus Tufvesson93c38552019-05-30 12:47:39 +01003043 private void notifySettingChangeForRunningUsers(int key, String name) {
Svet Ganov945864c2018-03-22 21:49:10 -07003044 // Important: No need to update generation for each user as there
3045 // is a singleton generation entry for the global settings which
3046 // is already incremented be the caller.
3047 final Uri uri = getNotificationUriFor(key, name);
3048 final List<UserInfo> users = mUserManager.getUsers(/*excludeDying*/ true);
3049 for (int i = 0; i < users.size(); i++) {
3050 final int userId = users.get(i).id;
3051 if (mUserManager.isUserRunning(UserHandle.of(userId))) {
3052 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
3053 userId, 0, uri).sendToTarget();
3054 }
3055 }
3056 }
3057
Makoto Onuki0000d322017-11-28 16:31:47 -08003058 private void notifyLocationChangeForRunningUsers() {
3059 final List<UserInfo> users = mUserManager.getUsers(/*excludeDying=*/ true);
3060
3061 for (int i = 0; i < users.size(); i++) {
3062 final int userId = users.get(i).id;
3063
3064 if (!mUserManager.isUserRunning(UserHandle.of(userId))) {
3065 continue;
3066 }
Makoto Onuki0000d322017-11-28 16:31:47 -08003067
Svet Ganov945864c2018-03-22 21:49:10 -07003068 // Increment the generation first, so observers always see the new value
3069 final int key = makeKey(SETTINGS_TYPE_SECURE, userId);
3070 mGenerationRegistry.incrementGeneration(key);
3071
Kweku Adams5e0052b2019-02-22 15:17:52 -08003072 final Uri uri = getNotificationUriFor(key, Secure.LOCATION_MODE);
Makoto Onuki0000d322017-11-28 16:31:47 -08003073 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
3074 userId, 0, uri).sendToTarget();
3075 }
3076 }
3077
Matt Pape1b31a332018-10-17 09:58:28 -07003078 private boolean isConfigSettingsKey(int key) {
3079 return getTypeFromKey(key) == SETTINGS_TYPE_CONFIG;
3080 }
3081
Svetoslav683914b2015-01-15 14:22:26 -08003082 private boolean isGlobalSettingsKey(int key) {
3083 return getTypeFromKey(key) == SETTINGS_TYPE_GLOBAL;
3084 }
3085
3086 private boolean isSystemSettingsKey(int key) {
3087 return getTypeFromKey(key) == SETTINGS_TYPE_SYSTEM;
3088 }
3089
3090 private boolean isSecureSettingsKey(int key) {
3091 return getTypeFromKey(key) == SETTINGS_TYPE_SECURE;
3092 }
3093
Mark Rathjend891f012017-01-19 04:10:37 +00003094 private boolean isSsaidSettingsKey(int key) {
3095 return getTypeFromKey(key) == SETTINGS_TYPE_SSAID;
3096 }
3097
Svetoslav683914b2015-01-15 14:22:26 -08003098 private File getSettingsFile(int key) {
Matt Pape1b31a332018-10-17 09:58:28 -07003099 if (isConfigSettingsKey(key)) {
3100 final int userId = getUserIdFromKey(key);
3101 return new File(Environment.getUserSystemDirectory(userId),
3102 SETTINGS_FILE_CONFIG);
3103 } else if (isGlobalSettingsKey(key)) {
Svetoslav683914b2015-01-15 14:22:26 -08003104 final int userId = getUserIdFromKey(key);
3105 return new File(Environment.getUserSystemDirectory(userId),
3106 SETTINGS_FILE_GLOBAL);
3107 } else if (isSystemSettingsKey(key)) {
3108 final int userId = getUserIdFromKey(key);
3109 return new File(Environment.getUserSystemDirectory(userId),
3110 SETTINGS_FILE_SYSTEM);
3111 } else if (isSecureSettingsKey(key)) {
3112 final int userId = getUserIdFromKey(key);
3113 return new File(Environment.getUserSystemDirectory(userId),
3114 SETTINGS_FILE_SECURE);
Mark Rathjend891f012017-01-19 04:10:37 +00003115 } else if (isSsaidSettingsKey(key)) {
3116 final int userId = getUserIdFromKey(key);
3117 return new File(Environment.getUserSystemDirectory(userId),
3118 SETTINGS_FILE_SSAID);
Svetoslav683914b2015-01-15 14:22:26 -08003119 } else {
3120 throw new IllegalArgumentException("Invalid settings key:" + key);
3121 }
3122 }
3123
3124 private Uri getNotificationUriFor(int key, String name) {
Matt Pape1b31a332018-10-17 09:58:28 -07003125 if (isConfigSettingsKey(key)) {
Matt Pape1278d1c2018-12-11 13:03:49 -08003126 return (name != null) ? Uri.withAppendedPath(DeviceConfig.CONTENT_URI, name)
3127 : DeviceConfig.CONTENT_URI;
Matt Pape1b31a332018-10-17 09:58:28 -07003128 } else if (isGlobalSettingsKey(key)) {
Svetoslav683914b2015-01-15 14:22:26 -08003129 return (name != null) ? Uri.withAppendedPath(Settings.Global.CONTENT_URI, name)
3130 : Settings.Global.CONTENT_URI;
3131 } else if (isSecureSettingsKey(key)) {
3132 return (name != null) ? Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name)
3133 : Settings.Secure.CONTENT_URI;
3134 } else if (isSystemSettingsKey(key)) {
3135 return (name != null) ? Uri.withAppendedPath(Settings.System.CONTENT_URI, name)
3136 : Settings.System.CONTENT_URI;
3137 } else {
3138 throw new IllegalArgumentException("Invalid settings key:" + key);
3139 }
3140 }
3141
3142 private int getMaxBytesPerPackageForType(int type) {
3143 switch (type) {
Matt Pape1b31a332018-10-17 09:58:28 -07003144 case SETTINGS_TYPE_CONFIG:
Svetoslav683914b2015-01-15 14:22:26 -08003145 case SETTINGS_TYPE_GLOBAL:
Mark Rathjend891f012017-01-19 04:10:37 +00003146 case SETTINGS_TYPE_SECURE:
3147 case SETTINGS_TYPE_SSAID: {
Svetoslav683914b2015-01-15 14:22:26 -08003148 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED;
3149 }
3150
3151 default: {
3152 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_LIMITED;
3153 }
3154 }
3155 }
3156
Svetoslav7e0683b2015-08-03 16:02:52 -07003157 private final class MyHandler extends Handler {
3158 private static final int MSG_NOTIFY_URI_CHANGED = 1;
3159 private static final int MSG_NOTIFY_DATA_CHANGED = 2;
3160
3161 public MyHandler(Looper looper) {
3162 super(looper);
3163 }
3164
3165 @Override
3166 public void handleMessage(Message msg) {
3167 switch (msg.what) {
3168 case MSG_NOTIFY_URI_CHANGED: {
3169 final int userId = msg.arg1;
3170 Uri uri = (Uri) msg.obj;
Jeff Sharkey88f9d0b2017-08-11 15:29:40 -06003171 try {
3172 getContext().getContentResolver().notifyChange(uri, null, true, userId);
3173 } catch (SecurityException e) {
3174 Slog.w(LOG_TAG, "Failed to notify for " + userId + ": " + uri, e);
3175 }
Makoto Onuki0000d322017-11-28 16:31:47 -08003176 if (DEBUG || true) {
Svetoslav7e0683b2015-08-03 16:02:52 -07003177 Slog.v(LOG_TAG, "Notifying for " + userId + ": " + uri);
3178 }
3179 } break;
3180
3181 case MSG_NOTIFY_DATA_CHANGED: {
3182 mBackupManager.dataChanged();
3183 } break;
3184 }
3185 }
3186 }
3187
Svetoslav683914b2015-01-15 14:22:26 -08003188 private final class UpgradeController {
Eric Laurent02153e62019-06-12 17:20:03 -07003189 private static final int SETTINGS_VERSION = 182;
Svetoslav683914b2015-01-15 14:22:26 -08003190
3191 private final int mUserId;
3192
3193 public UpgradeController(int userId) {
3194 mUserId = userId;
3195 }
3196
3197 public void upgradeIfNeededLocked() {
3198 // The version of all settings for a user is the same (all users have secure).
3199 SettingsState secureSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07003200 SETTINGS_TYPE_SECURE, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08003201
3202 // Try an update from the current state.
3203 final int oldVersion = secureSettings.getVersionLocked();
3204 final int newVersion = SETTINGS_VERSION;
3205
Svet Ganovc9755bc2015-03-28 13:21:22 -07003206 // If up do date - done.
Svetoslav683914b2015-01-15 14:22:26 -08003207 if (oldVersion == newVersion) {
3208 return;
3209 }
3210
3211 // Try to upgrade.
3212 final int curVersion = onUpgradeLocked(mUserId, oldVersion, newVersion);
3213
3214 // If upgrade failed start from scratch and upgrade.
3215 if (curVersion != newVersion) {
3216 // Drop state we have for this user.
3217 removeUserStateLocked(mUserId, true);
3218
3219 // Recreate the database.
3220 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), mUserId);
3221 SQLiteDatabase database = dbHelper.getWritableDatabase();
3222 dbHelper.recreateDatabase(database, newVersion, curVersion, oldVersion);
3223
3224 // Migrate the settings for this user.
3225 migrateLegacySettingsForUserLocked(dbHelper, database, mUserId);
3226
3227 // Now upgrade should work fine.
3228 onUpgradeLocked(mUserId, oldVersion, newVersion);
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07003229
3230 // Make a note what happened, so we don't wonder why data was lost
3231 String reason = "Settings rebuilt! Current version: "
3232 + curVersion + " while expected: " + newVersion;
3233 getGlobalSettingsLocked().insertSettingLocked(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003234 Settings.Global.DATABASE_DOWNGRADE_REASON,
3235 reason, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Svetoslav683914b2015-01-15 14:22:26 -08003236 }
3237
3238 // Set the global settings version if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07003239 if (mUserId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08003240 SettingsState globalSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07003241 SETTINGS_TYPE_GLOBAL, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08003242 globalSettings.setVersionLocked(newVersion);
3243 }
3244
3245 // Set the secure settings version.
3246 secureSettings.setVersionLocked(newVersion);
3247
3248 // Set the system settings version.
3249 SettingsState systemSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07003250 SETTINGS_TYPE_SYSTEM, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08003251 systemSettings.setVersionLocked(newVersion);
3252 }
3253
3254 private SettingsState getGlobalSettingsLocked() {
Xiaohui Chen43765b72015-08-31 10:57:33 -07003255 return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08003256 }
3257
3258 private SettingsState getSecureSettingsLocked(int userId) {
3259 return getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
3260 }
3261
Mark Rathjend891f012017-01-19 04:10:37 +00003262 private SettingsState getSsaidSettingsLocked(int userId) {
3263 return getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
3264 }
3265
Svetoslav683914b2015-01-15 14:22:26 -08003266 private SettingsState getSystemSettingsLocked(int userId) {
3267 return getSettingsLocked(SETTINGS_TYPE_SYSTEM, userId);
3268 }
3269
Jeff Brown503cffc2015-03-26 18:08:51 -07003270 /**
3271 * You must perform all necessary mutations to bring the settings
3272 * for this user from the old to the new version. When you add a new
3273 * upgrade step you *must* update SETTINGS_VERSION.
3274 *
3275 * This is an example of moving a setting from secure to global.
3276 *
3277 * // v119: Example settings changes.
3278 * if (currentVersion == 118) {
3279 * if (userId == UserHandle.USER_OWNER) {
3280 * // Remove from the secure settings.
3281 * SettingsState secureSettings = getSecureSettingsLocked(userId);
3282 * String name = "example_setting_to_move";
3283 * String value = secureSettings.getSetting(name);
3284 * secureSettings.deleteSetting(name);
3285 *
3286 * // Add to the global settings.
3287 * SettingsState globalSettings = getGlobalSettingsLocked();
3288 * globalSettings.insertSetting(name, value, SettingsState.SYSTEM_PACKAGE_NAME);
3289 * }
3290 *
3291 * // Update the current version.
3292 * currentVersion = 119;
3293 * }
3294 */
Svetoslav683914b2015-01-15 14:22:26 -08003295 private int onUpgradeLocked(int userId, int oldVersion, int newVersion) {
3296 if (DEBUG) {
3297 Slog.w(LOG_TAG, "Upgrading settings for user: " + userId + " from version: "
3298 + oldVersion + " to version: " + newVersion);
3299 }
3300
Jeff Brown503cffc2015-03-26 18:08:51 -07003301 int currentVersion = oldVersion;
Svetoslav683914b2015-01-15 14:22:26 -08003302
John Spurlocke11ae112015-05-11 16:09:03 -04003303 // v119: Reset zen + ringer mode.
3304 if (currentVersion == 118) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07003305 if (userId == UserHandle.USER_SYSTEM) {
John Spurlocke11ae112015-05-11 16:09:03 -04003306 final SettingsState globalSettings = getGlobalSettingsLocked();
3307 globalSettings.updateSettingLocked(Settings.Global.ZEN_MODE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003308 Integer.toString(Settings.Global.ZEN_MODE_OFF), null,
3309 true, SettingsState.SYSTEM_PACKAGE_NAME);
John Spurlocke11ae112015-05-11 16:09:03 -04003310 globalSettings.updateSettingLocked(Settings.Global.MODE_RINGER,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003311 Integer.toString(AudioManager.RINGER_MODE_NORMAL), null,
3312 true, SettingsState.SYSTEM_PACKAGE_NAME);
John Spurlocke11ae112015-05-11 16:09:03 -04003313 }
3314 currentVersion = 119;
3315 }
3316
Jason Monk27bbb2d2015-03-31 16:46:39 -04003317 // v120: Add double tap to wake setting.
3318 if (currentVersion == 119) {
3319 SettingsState secureSettings = getSecureSettingsLocked(userId);
3320 secureSettings.insertSettingLocked(Settings.Secure.DOUBLE_TAP_TO_WAKE,
3321 getContext().getResources().getBoolean(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003322 R.bool.def_double_tap_to_wake) ? "1" : "0", null, true,
Jason Monk27bbb2d2015-03-31 16:46:39 -04003323 SettingsState.SYSTEM_PACKAGE_NAME);
3324
3325 currentVersion = 120;
3326 }
3327
Svetoslav7e0683b2015-08-03 16:02:52 -07003328 if (currentVersion == 120) {
3329 // Before 121, we used a different string encoding logic. We just bump the
3330 // version here; SettingsState knows how to handle pre-version 120 files.
3331 currentVersion = 121;
3332 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -07003333
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003334 if (currentVersion == 121) {
3335 // Version 122: allow OEMs to set a default payment component in resources.
3336 // Note that we only write the default if no default has been set;
3337 // if there is, we just leave the default at whatever it currently is.
3338 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3339 String defaultComponent = (getContext().getResources().getString(
3340 R.string.def_nfc_payment_component));
3341 Setting currentSetting = secureSettings.getSettingLocked(
3342 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT);
3343 if (defaultComponent != null && !defaultComponent.isEmpty() &&
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003344 currentSetting.isNull()) {
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003345 secureSettings.insertSettingLocked(
3346 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003347 defaultComponent, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003348 }
3349 currentVersion = 122;
3350 }
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003351
3352 if (currentVersion == 122) {
3353 // Version 123: Adding a default value for the ability to add a user from
3354 // the lock screen.
3355 if (userId == UserHandle.USER_SYSTEM) {
3356 final SettingsState globalSettings = getGlobalSettingsLocked();
3357 Setting currentSetting = globalSettings.getSettingLocked(
3358 Settings.Global.ADD_USERS_WHEN_LOCKED);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003359 if (currentSetting.isNull()) {
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003360 globalSettings.insertSettingLocked(
3361 Settings.Global.ADD_USERS_WHEN_LOCKED,
3362 getContext().getResources().getBoolean(
3363 R.bool.def_add_users_from_lockscreen) ? "1" : "0",
Svetoslav Ganove080da92016-12-21 17:10:35 -08003364 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003365 }
3366 }
3367 currentVersion = 123;
3368 }
Bryce Leebd179282015-12-17 19:01:37 -08003369
3370 if (currentVersion == 123) {
Bryce Leeec85f342015-12-16 13:32:28 -08003371 final SettingsState globalSettings = getGlobalSettingsLocked();
3372 String defaultDisabledProfiles = (getContext().getResources().getString(
3373 R.string.def_bluetooth_disabled_profiles));
3374 globalSettings.insertSettingLocked(Settings.Global.BLUETOOTH_DISABLED_PROFILES,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003375 defaultDisabledProfiles, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Bryce Leebd179282015-12-17 19:01:37 -08003376 currentVersion = 124;
Bryce Leeec85f342015-12-16 13:32:28 -08003377 }
3378
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003379 if (currentVersion == 124) {
3380 // Version 124: allow OEMs to set a default value for whether IME should be
3381 // shown when a physical keyboard is connected.
3382 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3383 Setting currentSetting = secureSettings.getSettingLocked(
3384 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003385 if (currentSetting.isNull()) {
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003386 secureSettings.insertSettingLocked(
3387 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD,
3388 getContext().getResources().getBoolean(
3389 R.bool.def_show_ime_with_hard_keyboard) ? "1" : "0",
Svetoslav Ganove080da92016-12-21 17:10:35 -08003390 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003391 }
3392 currentVersion = 125;
3393 }
3394
Ruben Brunk98576cf2016-03-07 18:54:28 -08003395 if (currentVersion == 125) {
3396 // Version 125: Allow OEMs to set the default VR service.
3397 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3398
3399 Setting currentSetting = secureSettings.getSettingLocked(
3400 Settings.Secure.ENABLED_VR_LISTENERS);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003401 if (currentSetting.isNull()) {
Ruben Brunk98576cf2016-03-07 18:54:28 -08003402 ArraySet<ComponentName> l =
3403 SystemConfig.getInstance().getDefaultVrComponents();
3404
3405 if (l != null && !l.isEmpty()) {
3406 StringBuilder b = new StringBuilder();
3407 boolean start = true;
3408 for (ComponentName c : l) {
3409 if (!start) {
3410 b.append(':');
3411 }
3412 b.append(c.flattenToString());
3413 start = false;
3414 }
3415 secureSettings.insertSettingLocked(
3416 Settings.Secure.ENABLED_VR_LISTENERS, b.toString(),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003417 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Ruben Brunk98576cf2016-03-07 18:54:28 -08003418 }
3419
3420 }
3421 currentVersion = 126;
3422 }
3423
Daniel U02ba6122016-04-01 18:41:42 +01003424 if (currentVersion == 126) {
3425 // Version 126: copy the primary values of LOCK_SCREEN_SHOW_NOTIFICATIONS and
3426 // LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS into managed profile.
3427 if (mUserManager.isManagedProfile(userId)) {
3428 final SettingsState systemSecureSettings =
3429 getSecureSettingsLocked(UserHandle.USER_SYSTEM);
3430
3431 final Setting showNotifications = systemSecureSettings.getSettingLocked(
3432 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09003433 if (!showNotifications.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01003434 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3435 secureSettings.insertSettingLocked(
3436 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003437 showNotifications.getValue(), null, true,
Daniel U02ba6122016-04-01 18:41:42 +01003438 SettingsState.SYSTEM_PACKAGE_NAME);
3439 }
3440
3441 final Setting allowPrivate = systemSecureSettings.getSettingLocked(
3442 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09003443 if (!allowPrivate.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01003444 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3445 secureSettings.insertSettingLocked(
3446 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003447 allowPrivate.getValue(), null, true,
Daniel U02ba6122016-04-01 18:41:42 +01003448 SettingsState.SYSTEM_PACKAGE_NAME);
3449 }
3450 }
3451 currentVersion = 127;
3452 }
3453
Steven Ngdc20ba62016-04-26 18:19:04 +01003454 if (currentVersion == 127) {
Mahaver Chopra3d9805d2016-07-07 16:25:05 +01003455 // version 127 is no longer used.
Steven Ngdc20ba62016-04-26 18:19:04 +01003456 currentVersion = 128;
3457 }
3458
Julia Reynolds1f721e12016-07-11 08:50:58 -04003459 if (currentVersion == 128) {
Julia Reynoldsb852e562017-06-06 16:14:18 -04003460 // Version 128: Removed
Julia Reynolds1f721e12016-07-11 08:50:58 -04003461 currentVersion = 129;
3462 }
3463
Dan Sandler71f85e92016-07-20 13:46:05 -04003464 if (currentVersion == 129) {
3465 // default longpress timeout changed from 500 to 400. If unchanged from the old
3466 // default, update to the new default.
3467 final SettingsState systemSecureSettings =
3468 getSecureSettingsLocked(userId);
3469 final String oldValue = systemSecureSettings.getSettingLocked(
3470 Settings.Secure.LONG_PRESS_TIMEOUT).getValue();
3471 if (TextUtils.equals("500", oldValue)) {
3472 systemSecureSettings.insertSettingLocked(
3473 Settings.Secure.LONG_PRESS_TIMEOUT,
3474 String.valueOf(getContext().getResources().getInteger(
3475 R.integer.def_long_press_timeout_millis)),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003476 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Dan Sandler71f85e92016-07-20 13:46:05 -04003477 }
3478 currentVersion = 130;
3479 }
3480
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003481 if (currentVersion == 130) {
Adrian Roos69741a22016-10-21 14:49:17 -07003482 // Split Ambient settings
3483 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3484 boolean dozeExplicitlyDisabled = "0".equals(secureSettings.
3485 getSettingLocked(Settings.Secure.DOZE_ENABLED).getValue());
3486
3487 if (dozeExplicitlyDisabled) {
Lucas Dupin4359b552018-08-09 15:07:54 -07003488 secureSettings.insertSettingLocked(Settings.Secure.DOZE_PICK_UP_GESTURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003489 "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Lucas Dupin4359b552018-08-09 15:07:54 -07003490 secureSettings.insertSettingLocked(Settings.Secure.DOZE_DOUBLE_TAP_GESTURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003491 "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Adrian Roos69741a22016-10-21 14:49:17 -07003492 }
3493 currentVersion = 131;
3494 }
3495
3496 if (currentVersion == 131) {
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003497 // Initialize new multi-press timeout to default value
3498 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3499 final String oldValue = systemSecureSettings.getSettingLocked(
3500 Settings.Secure.MULTI_PRESS_TIMEOUT).getValue();
3501 if (TextUtils.equals(null, oldValue)) {
3502 systemSecureSettings.insertSettingLocked(
3503 Settings.Secure.MULTI_PRESS_TIMEOUT,
3504 String.valueOf(getContext().getResources().getInteger(
3505 R.integer.def_multi_press_timeout_millis)),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003506 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003507 }
3508
Adrian Roos69741a22016-10-21 14:49:17 -07003509 currentVersion = 132;
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003510 }
3511
Adrian Roos69741a22016-10-21 14:49:17 -07003512 if (currentVersion == 132) {
3513 // Version 132: Allow managed profile to optionally use the parent's ringtones
Andre Lagoea35e072016-08-04 13:41:13 +01003514 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3515 String defaultSyncParentSounds = (getContext().getResources()
3516 .getBoolean(R.bool.def_sync_parent_sounds) ? "1" : "0");
3517 systemSecureSettings.insertSettingLocked(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003518 Settings.Secure.SYNC_PARENT_SOUNDS, defaultSyncParentSounds,
3519 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Adrian Roos69741a22016-10-21 14:49:17 -07003520 currentVersion = 133;
Andre Lagoea35e072016-08-04 13:41:13 +01003521 }
3522
Adrian Roos69741a22016-10-21 14:49:17 -07003523 if (currentVersion == 133) {
3524 // Version 133: Add default end button behavior
Keun-young Parkec7a1182016-10-18 11:52:38 -07003525 final SettingsState systemSettings = getSystemSettingsLocked(userId);
davidlnedb31e12018-09-25 10:21:20 -07003526 if (systemSettings.getSettingLocked(Settings.System.END_BUTTON_BEHAVIOR)
3527 .isNull()) {
Keun-young Parkec7a1182016-10-18 11:52:38 -07003528 String defaultEndButtonBehavior = Integer.toString(getContext()
3529 .getResources().getInteger(R.integer.def_end_button_behavior));
3530 systemSettings.insertSettingLocked(Settings.System.END_BUTTON_BEHAVIOR,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003531 defaultEndButtonBehavior, null, true,
3532 SettingsState.SYSTEM_PACKAGE_NAME);
Keun-young Parkec7a1182016-10-18 11:52:38 -07003533 }
Adrian Roos69741a22016-10-21 14:49:17 -07003534 currentVersion = 134;
Keun-young Parkec7a1182016-10-18 11:52:38 -07003535 }
3536
Phil Weaver89e3ffc2016-09-19 13:51:10 -07003537 if (currentVersion == 134) {
3538 // Remove setting that specifies if magnification values should be preserved.
3539 // This setting defaulted to true and never has a UI.
3540 getSecureSettingsLocked(userId).deleteSettingLocked(
3541 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_AUTO_UPDATE);
3542 currentVersion = 135;
3543 }
3544
Jeremy Joslin8bdad342016-12-14 11:46:47 -08003545 if (currentVersion == 135) {
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003546 // Version 135 no longer used.
Jeremy Joslin8bdad342016-12-14 11:46:47 -08003547 currentVersion = 136;
3548 }
3549
Mark Rathjend891f012017-01-19 04:10:37 +00003550 if (currentVersion == 136) {
3551 // Version 136: Store legacy SSAID for all apps currently installed on the
3552 // device as first step in migrating SSAID to be unique per application.
3553
3554 final boolean isUpgrade;
3555 try {
Svet Ganovd8eb8b22019-04-05 18:52:08 -07003556 isUpgrade = mPackageManager.isDeviceUpgrading();
Mark Rathjend891f012017-01-19 04:10:37 +00003557 } catch (RemoteException e) {
3558 throw new IllegalStateException("Package manager not available");
3559 }
3560 // Only retain legacy ssaid if the device is performing an OTA. After wiping
3561 // user data or first boot on a new device should use new ssaid generation.
3562 if (isUpgrade) {
3563 // Retrieve the legacy ssaid from the secure settings table.
Mark Rathjenea617592017-01-18 23:03:41 -08003564 final Setting legacySsaidSetting = getSettingLocked(SETTINGS_TYPE_SECURE,
3565 userId, Settings.Secure.ANDROID_ID);
3566 if (legacySsaidSetting == null || legacySsaidSetting.isNull()
3567 || legacySsaidSetting.getValue() == null) {
3568 throw new IllegalStateException("Legacy ssaid not accessible");
3569 }
3570 final String legacySsaid = legacySsaidSetting.getValue();
Mark Rathjend891f012017-01-19 04:10:37 +00003571
3572 // Fill each uid with the legacy ssaid to be backwards compatible.
3573 final List<PackageInfo> packages;
3574 try {
Tetsutoki Shiozawaebe0e5f2018-01-17 11:07:09 +09003575 packages = mPackageManager.getInstalledPackages(
3576 PackageManager.MATCH_UNINSTALLED_PACKAGES,
3577 userId).getList();
Mark Rathjend891f012017-01-19 04:10:37 +00003578 } catch (RemoteException e) {
3579 throw new IllegalStateException("Package manager not available");
3580 }
3581
3582 final SettingsState ssaidSettings = getSsaidSettingsLocked(userId);
3583 for (PackageInfo info : packages) {
3584 // Check if the UID already has an entry in the table.
3585 final String uid = Integer.toString(info.applicationInfo.uid);
3586 final Setting ssaid = ssaidSettings.getSettingLocked(uid);
3587
3588 if (ssaid.isNull() || ssaid.getValue() == null) {
3589 // Android Id doesn't exist for this package so create it.
3590 ssaidSettings.insertSettingLocked(uid, legacySsaid, null, true,
3591 info.packageName);
Tetsutoki Shiozawaebe0e5f2018-01-17 11:07:09 +09003592 if (DEBUG) {
3593 Slog.d(LOG_TAG, "Keep the legacy ssaid for uid=" + uid);
3594 }
Mark Rathjend891f012017-01-19 04:10:37 +00003595 }
3596 }
3597 }
3598
3599 currentVersion = 137;
3600 }
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003601 if (currentVersion == 137) {
3602 // Version 138: Settings.Secure#INSTALL_NON_MARKET_APPS is deprecated and its
3603 // default value set to 1. The user can no longer change the value of this
3604 // setting through the UI.
3605 final SettingsState secureSetting = getSecureSettingsLocked(userId);
3606 if (!mUserManager.hasUserRestriction(
Suprabh Shukla0b1356f2017-02-21 14:33:50 -08003607 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, UserHandle.of(userId))
3608 && secureSetting.getSettingLocked(
3609 Settings.Secure.INSTALL_NON_MARKET_APPS).getValue().equals("0")) {
3610
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003611 secureSetting.insertSettingLocked(Settings.Secure.INSTALL_NON_MARKET_APPS,
3612 "1", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shukla0b1356f2017-02-21 14:33:50 -08003613 // For managed profiles with profile owners, DevicePolicyManagerService
3614 // may want to set the user restriction in this case
3615 secureSetting.insertSettingLocked(
3616 Settings.Secure.UNKNOWN_SOURCES_DEFAULT_REVERSED, "1", null, true,
3617 SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003618 }
3619 currentVersion = 138;
3620 }
Mark Rathjend891f012017-01-19 04:10:37 +00003621
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003622 if (currentVersion == 138) {
Jeremy Joslin27d14c42017-02-15 12:02:03 -08003623 // Version 139: Removed.
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003624 currentVersion = 139;
3625 }
3626
Phil Weaver385912e2017-02-10 10:06:56 -08003627 if (currentVersion == 139) {
3628 // Version 140: Settings.Secure#ACCESSIBILITY_SPEAK_PASSWORD is deprecated and
3629 // the user can no longer change the value of this setting through the UI.
3630 // Force to true.
3631 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3632 secureSettings.updateSettingLocked(Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD,
3633 "1", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3634 currentVersion = 140;
3635 }
3636
Julia Reynoldsad0d9e02017-02-15 08:41:48 -05003637 if (currentVersion == 140) {
Julia Reynoldsb852e562017-06-06 16:14:18 -04003638 // Version 141: Removed
Julia Reynoldsad0d9e02017-02-15 08:41:48 -05003639 currentVersion = 141;
3640 }
3641
Svet Ganov13701552017-02-23 12:45:17 -08003642 if (currentVersion == 141) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003643 // This implementation was incorrectly setting the current value of
3644 // settings changed by non-system packages as the default which default
3645 // is set by the system. We add a new upgrade step at the end to properly
3646 // handle this case which would also fix incorrect changes made by the
3647 // old implementation of this step.
Svet Ganov13701552017-02-23 12:45:17 -08003648 currentVersion = 142;
3649 }
3650
Stephen Chen5d0922f2017-03-27 10:28:04 -07003651 if (currentVersion == 142) {
Felipe Lemeff355092017-04-03 12:55:02 -07003652 // Version 143: Set a default value for Wi-Fi wakeup feature.
Stephen Chen5d0922f2017-03-27 10:28:04 -07003653 if (userId == UserHandle.USER_SYSTEM) {
3654 final SettingsState globalSettings = getGlobalSettingsLocked();
3655 Setting currentSetting = globalSettings.getSettingLocked(
3656 Settings.Global.WIFI_WAKEUP_ENABLED);
3657 if (currentSetting.isNull()) {
3658 globalSettings.insertSettingLocked(
3659 Settings.Global.WIFI_WAKEUP_ENABLED,
3660 getContext().getResources().getBoolean(
3661 R.bool.def_wifi_wakeup_enabled) ? "1" : "0",
3662 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3663 }
3664 }
3665
3666 currentVersion = 143;
3667 }
3668
Felipe Lemeff355092017-04-03 12:55:02 -07003669 if (currentVersion == 143) {
3670 // Version 144: Set a default value for Autofill service.
3671 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3672 final Setting currentSetting = secureSettings
3673 .getSettingLocked(Settings.Secure.AUTOFILL_SERVICE);
3674 if (currentSetting.isNull()) {
3675 final String defaultValue = getContext().getResources().getString(
3676 com.android.internal.R.string.config_defaultAutofillService);
3677 if (defaultValue != null) {
3678 Slog.d(LOG_TAG, "Setting [" + defaultValue + "] as Autofill Service "
3679 + "for user " + userId);
3680 secureSettings.insertSettingLocked(Settings.Secure.AUTOFILL_SERVICE,
3681 defaultValue, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3682 }
3683 }
3684
3685 currentVersion = 144;
3686 }
3687
Jeremy Joslin45caa252017-05-04 11:22:46 -07003688 if (currentVersion == 144) {
Amin Shaikh86367962017-06-07 08:58:22 -07003689 // Version 145: Removed
3690 currentVersion = 145;
3691 }
3692
3693 if (currentVersion == 145) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003694 // Version 146: In step 142 we had a bug where incorrectly
3695 // some settings were considered system set and as a result
3696 // made the default and marked as the default being set by
3697 // the system. Here reevaluate the default and default system
3698 // set flags. This would both fix corruption by the old impl
3699 // of step 142 and also properly handle devices which never
3700 // run 142.
Jeremy Joslin45caa252017-05-04 11:22:46 -07003701 if (userId == UserHandle.USER_SYSTEM) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003702 SettingsState globalSettings = getGlobalSettingsLocked();
3703 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(globalSettings, userId);
3704 globalSettings.persistSyncLocked();
Jeremy Joslin45caa252017-05-04 11:22:46 -07003705 }
3706
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003707 SettingsState secureSettings = getSecureSettingsLocked(mUserId);
3708 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(secureSettings, userId);
3709 secureSettings.persistSyncLocked();
3710
3711 SettingsState systemSettings = getSystemSettingsLocked(mUserId);
3712 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(systemSettings, userId);
3713 systemSettings.persistSyncLocked();
3714
Amin Shaikh86367962017-06-07 08:58:22 -07003715 currentVersion = 146;
Jeremy Joslin45caa252017-05-04 11:22:46 -07003716 }
3717
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003718 if (currentVersion == 146) {
Joe LaPenna250d7842018-01-25 10:19:42 -08003719 // Version 147: Removed. (This version previously allowed showing the
3720 // "wifi_wakeup_available" setting).
3721 // The setting that was added here is deleted in 153.
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003722 currentVersion = 147;
3723 }
3724
3725 if (currentVersion == 147) {
3726 // Version 148: Set the default value for DEFAULT_RESTRICT_BACKGROUND_DATA.
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003727 if (userId == UserHandle.USER_SYSTEM) {
3728 final SettingsState globalSettings = getGlobalSettingsLocked();
3729 final Setting currentSetting = globalSettings.getSettingLocked(
3730 Global.DEFAULT_RESTRICT_BACKGROUND_DATA);
3731 if (currentSetting.isNull()) {
3732 globalSettings.insertSettingLocked(
3733 Global.DEFAULT_RESTRICT_BACKGROUND_DATA,
3734 getContext().getResources().getBoolean(
3735 R.bool.def_restrict_background_data) ? "1" : "0",
3736 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3737 }
3738 }
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003739 currentVersion = 148;
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003740 }
3741
Tim Zhengcc1e76a2017-08-30 17:46:19 -07003742 if (currentVersion == 148) {
3743 // Version 149: Set the default value for BACKUP_MANAGER_CONSTANTS.
3744 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3745 final String oldValue = systemSecureSettings.getSettingLocked(
3746 Settings.Secure.BACKUP_MANAGER_CONSTANTS).getValue();
3747 if (TextUtils.equals(null, oldValue)) {
3748 final String defaultValue = getContext().getResources().getString(
3749 R.string.def_backup_manager_constants);
3750 if (!TextUtils.isEmpty(defaultValue)) {
3751 systemSecureSettings.insertSettingLocked(
3752 Settings.Secure.BACKUP_MANAGER_CONSTANTS, defaultValue, null,
3753 true, SettingsState.SYSTEM_PACKAGE_NAME);
3754 }
3755 }
Tim Zhengcc1e76a2017-08-30 17:46:19 -07003756 currentVersion = 149;
3757 }
Jacky Cheung7076a312017-10-02 10:40:48 -07003758
3759 if (currentVersion == 149) {
3760 // Version 150: Set a default value for mobile data always on
3761 final SettingsState globalSettings = getGlobalSettingsLocked();
3762 final Setting currentSetting = globalSettings.getSettingLocked(
3763 Settings.Global.MOBILE_DATA_ALWAYS_ON);
3764 if (currentSetting.isNull()) {
3765 globalSettings.insertSettingLocked(
3766 Settings.Global.MOBILE_DATA_ALWAYS_ON,
3767 getContext().getResources().getBoolean(
3768 R.bool.def_mobile_data_always_on) ? "1" : "0",
3769 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3770 }
3771
3772 currentVersion = 150;
3773 }
3774
Mike Digman4af4a6f2018-01-16 14:49:38 -08003775 if (currentVersion == 150) {
Mike Digman32e03312018-05-16 16:43:23 -07003776 // Version 151: Removed.
Mike Digman4af4a6f2018-01-16 14:49:38 -08003777 currentVersion = 151;
3778 }
3779
Joe LaPenna4f50ed42018-01-22 14:54:45 -08003780 if (currentVersion == 151) {
Joe LaPenna250d7842018-01-25 10:19:42 -08003781 // Version 152: Removed. (This version made the setting for wifi_wakeup enabled
3782 // by default but it is now no longer configurable).
3783 // The setting updated here is deleted in 153.
Joe LaPenna4f50ed42018-01-22 14:54:45 -08003784 currentVersion = 152;
3785 }
3786
Joe LaPenna250d7842018-01-25 10:19:42 -08003787 if (currentVersion == 152) {
3788 getGlobalSettingsLocked().deleteSettingLocked("wifi_wakeup_available");
3789 currentVersion = 153;
3790 }
3791
Ben Linb4df8bc2018-01-29 11:48:20 -08003792 if (currentVersion == 153) {
3793 // Version 154: Read notification badge configuration from config.
3794 // If user has already set the value, don't do anything.
3795 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3796 final Setting showNotificationBadges = systemSecureSettings.getSettingLocked(
3797 Settings.Secure.NOTIFICATION_BADGING);
3798 if (showNotificationBadges.isNull()) {
3799 final boolean defaultValue = getContext().getResources().getBoolean(
3800 com.android.internal.R.bool.config_notificationBadging);
3801 systemSecureSettings.insertSettingLocked(
3802 Secure.NOTIFICATION_BADGING,
3803 defaultValue ? "1" : "0",
3804 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3805 }
3806 currentVersion = 154;
3807 }
3808
Bernardo Rufinoeaa78b92018-01-26 11:25:37 +00003809 if (currentVersion == 154) {
3810 // Version 155: Set the default value for BACKUP_LOCAL_TRANSPORT_PARAMETERS.
3811 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3812 final String oldValue = systemSecureSettings.getSettingLocked(
3813 Settings.Secure.BACKUP_LOCAL_TRANSPORT_PARAMETERS).getValue();
3814 if (TextUtils.equals(null, oldValue)) {
3815 final String defaultValue = getContext().getResources().getString(
3816 R.string.def_backup_local_transport_parameters);
3817 if (!TextUtils.isEmpty(defaultValue)) {
3818 systemSecureSettings.insertSettingLocked(
3819 Settings.Secure.BACKUP_LOCAL_TRANSPORT_PARAMETERS, defaultValue,
3820 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3821 }
3822
3823 }
3824 currentVersion = 155;
3825 }
3826
Beverlyda904812018-03-02 09:55:30 -05003827 if (currentVersion == 155) {
Annie Mengd069a882018-03-13 15:31:40 +00003828 // Version 156: Set the default value for CHARGING_STARTED_SOUND.
Beverlyda904812018-03-02 09:55:30 -05003829 final SettingsState globalSettings = getGlobalSettingsLocked();
3830 final String oldValue = globalSettings.getSettingLocked(
3831 Global.CHARGING_STARTED_SOUND).getValue();
3832 final String oldDefault = getContext().getResources().getString(
3833 R.string.def_wireless_charging_started_sound);
3834 if (TextUtils.equals(null, oldValue)
3835 || TextUtils.equals(oldValue, oldDefault)) {
3836 final String defaultValue = getContext().getResources().getString(
3837 R.string.def_charging_started_sound);
3838 if (!TextUtils.isEmpty(defaultValue)) {
3839 globalSettings.insertSettingLocked(
3840 Settings.Global.CHARGING_STARTED_SOUND, defaultValue,
3841 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3842 }
3843
3844 }
3845 currentVersion = 156;
3846 }
3847
Beverly09da25f2018-02-26 09:17:07 -05003848 if (currentVersion == 156) {
Beverly91d0a632018-07-02 16:45:00 -04003849 // Version 157: Set a default value for zen duration,
3850 // in version 169, zen duration is moved to secure settings
Beverly09da25f2018-02-26 09:17:07 -05003851 final SettingsState globalSettings = getGlobalSettingsLocked();
3852 final Setting currentSetting = globalSettings.getSettingLocked(
3853 Global.ZEN_DURATION);
3854 if (currentSetting.isNull()) {
3855 String defaultZenDuration = Integer.toString(getContext()
3856 .getResources().getInteger(R.integer.def_zen_duration));
3857 globalSettings.insertSettingLocked(
3858 Global.ZEN_DURATION, defaultZenDuration,
3859 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3860 }
Beverly09da25f2018-02-26 09:17:07 -05003861 currentVersion = 157;
3862 }
Annie Mengd069a882018-03-13 15:31:40 +00003863
3864 if (currentVersion == 157) {
3865 // Version 158: Set default value for BACKUP_AGENT_TIMEOUT_PARAMETERS.
3866 final SettingsState globalSettings = getGlobalSettingsLocked();
3867 final String oldValue = globalSettings.getSettingLocked(
3868 Settings.Global.BACKUP_AGENT_TIMEOUT_PARAMETERS).getValue();
3869 if (TextUtils.equals(null, oldValue)) {
3870 final String defaultValue = getContext().getResources().getString(
3871 R.string.def_backup_agent_timeout_parameters);
3872 if (!TextUtils.isEmpty(defaultValue)) {
3873 globalSettings.insertSettingLocked(
3874 Settings.Global.BACKUP_AGENT_TIMEOUT_PARAMETERS, defaultValue,
3875 null, true,
3876 SettingsState.SYSTEM_PACKAGE_NAME);
3877 }
3878 }
3879 currentVersion = 158;
3880 }
Roshan Pius9c396672018-03-02 14:54:13 -08003881
3882 if (currentVersion == 158) {
3883 // Remove setting that specifies wifi bgscan throttling params
3884 getGlobalSettingsLocked().deleteSettingLocked(
3885 "wifi_scan_background_throttle_interval_ms");
3886 getGlobalSettingsLocked().deleteSettingLocked(
3887 "wifi_scan_background_throttle_package_whitelist");
3888 currentVersion = 159;
3889 }
3890
Pavel Grafovc5c97302018-03-19 13:37:15 +00003891 if (currentVersion == 159) {
3892 // Version 160: Hiding notifications from the lockscreen is only available as
3893 // primary user option, profiles can only make them redacted. If a profile was
3894 // configured to not show lockscreen notifications, ensure that at the very
3895 // least these will be come hidden.
3896 if (mUserManager.isManagedProfile(userId)) {
3897 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3898 Setting showNotifications = secureSettings.getSettingLocked(
3899 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
3900 // The default value is "1", check if user has turned it off.
3901 if ("0".equals(showNotifications.getValue())) {
3902 secureSettings.insertSettingLocked(
3903 Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, "0",
3904 null /* tag */, false /* makeDefault */,
3905 SettingsState.SYSTEM_PACKAGE_NAME);
3906 }
3907 // The setting is no longer valid for managed profiles, it should be
3908 // treated as if it was set to "1".
3909 secureSettings.deleteSettingLocked(Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
3910 }
3911 currentVersion = 160;
3912 }
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08003913
3914 if (currentVersion == 160) {
3915 // Version 161: Set the default value for
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08003916 // MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY and
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08003917 // SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT
3918 final SettingsState globalSettings = getGlobalSettingsLocked();
3919
3920 String oldValue = globalSettings.getSettingLocked(
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08003921 Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY).getValue();
3922 if (TextUtils.equals(null, oldValue)) {
3923 globalSettings.insertSettingLocked(
3924 Settings.Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY,
3925 Integer.toString(getContext().getResources().getInteger(
3926 R.integer.def_max_sound_trigger_detection_service_ops_per_day)),
3927 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3928 }
3929
3930 oldValue = globalSettings.getSettingLocked(
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08003931 Global.SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT).getValue();
3932 if (TextUtils.equals(null, oldValue)) {
3933 globalSettings.insertSettingLocked(
3934 Settings.Global.SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT,
3935 Integer.toString(getContext().getResources().getInteger(
3936 R.integer.def_sound_trigger_detection_service_op_timeout)),
3937 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3938 }
3939 currentVersion = 161;
3940 }
3941
Mike Digman55272862018-02-20 14:35:17 -08003942 if (currentVersion == 161) {
3943 // Version 161: Add a gesture for silencing phones
3944 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3945 final Setting currentSetting = secureSettings.getSettingLocked(
3946 Secure.VOLUME_HUSH_GESTURE);
3947 if (currentSetting.isNull()) {
3948 secureSettings.insertSettingLocked(
3949 Secure.VOLUME_HUSH_GESTURE,
3950 Integer.toString(Secure.VOLUME_HUSH_VIBRATE),
3951 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3952 }
3953
3954 currentVersion = 162;
3955 }
3956
Julia Reynoldsc4e5ecf2018-04-11 11:33:32 -04003957 if (currentVersion == 162) {
Julia Reynolds76bfa602018-04-23 09:38:47 -04003958 // Version 162: REMOVED: Add a gesture for silencing phones
Julia Reynoldsc4e5ecf2018-04-11 11:33:32 -04003959 currentVersion = 163;
3960 }
3961
Philip P. Moltmanncb58a832018-04-16 09:19:42 -07003962 if (currentVersion == 163) {
3963 // Version 163: Update default value of
3964 // MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY from old to new default
3965 final SettingsState settings = getGlobalSettingsLocked();
3966 final Setting currentSetting = settings.getSettingLocked(
3967 Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY);
3968 if (currentSetting.isDefaultFromSystem()) {
3969 settings.insertSettingLocked(
3970 Settings.Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY,
3971 Integer.toString(getContext().getResources().getInteger(
3972 R.integer
3973 .def_max_sound_trigger_detection_service_ops_per_day)),
3974 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3975 }
3976
3977 currentVersion = 164;
3978 }
3979
Julia Reynolds76bfa602018-04-23 09:38:47 -04003980 if (currentVersion == 164) {
Julia Reynolds24836c52018-06-06 14:36:03 -04003981 // Version 164: REMOVED: show zen upgrade notification
Julia Reynolds76bfa602018-04-23 09:38:47 -04003982 currentVersion = 165;
3983 }
3984
Beverly301e92a2018-04-27 09:43:05 -04003985 if (currentVersion == 165) {
Beverly91d0a632018-07-02 16:45:00 -04003986 // Version 165: MOVED: Show zen settings suggestion and zen updated settings
3987 // moved to secure settings and are set in version 169
Beverly301e92a2018-04-27 09:43:05 -04003988 currentVersion = 166;
3989 }
3990
Beverly38fcfd02018-05-18 17:33:40 -04003991 if (currentVersion == 166) {
3992 // Version 166: add default values for hush gesture used and manual ringer
3993 // toggle
3994 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3995 Setting currentHushUsedSetting = secureSettings.getSettingLocked(
3996 Secure.HUSH_GESTURE_USED);
3997 if (currentHushUsedSetting.isNull()) {
3998 secureSettings.insertSettingLocked(
3999 Settings.Secure.HUSH_GESTURE_USED, "0", null, true,
4000 SettingsState.SYSTEM_PACKAGE_NAME);
4001 }
4002
4003 Setting currentRingerToggleCountSetting = secureSettings.getSettingLocked(
4004 Secure.MANUAL_RINGER_TOGGLE_COUNT);
4005 if (currentRingerToggleCountSetting.isNull()) {
4006 secureSettings.insertSettingLocked(
4007 Settings.Secure.MANUAL_RINGER_TOGGLE_COUNT, "0", null, true,
4008 SettingsState.SYSTEM_PACKAGE_NAME);
4009 }
4010 currentVersion = 167;
4011 }
4012
Beverly155c9d22018-05-23 18:03:23 -04004013 if (currentVersion == 167) {
Beverly91d0a632018-07-02 16:45:00 -04004014 // Version 167: MOVED - Settings.Global.CHARGING_VIBRATION_ENABLED moved to
4015 // Settings.Secure.CHARGING_VIBRATION_ENABLED, set in version 170
Beverly155c9d22018-05-23 18:03:23 -04004016 currentVersion = 168;
4017 }
4018
Michael Wright0e9eeee2018-05-26 00:31:20 +01004019 if (currentVersion == 168) {
4020 // Version 168: by default, vibrate for phone calls
4021 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4022 final Setting currentSetting = systemSettings.getSettingLocked(
4023 Settings.System.VIBRATE_WHEN_RINGING);
4024 if (currentSetting.isNull()) {
4025 systemSettings.insertSettingLocked(
4026 Settings.System.VIBRATE_WHEN_RINGING,
4027 getContext().getResources().getBoolean(
4028 R.bool.def_vibrate_when_ringing) ? "1" : "0",
4029 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4030 }
4031 currentVersion = 169;
4032 }
4033
Nadav Barf9f115d2018-06-24 10:06:50 +03004034 if (currentVersion == 169) {
Beverly91d0a632018-07-02 16:45:00 -04004035 // Version 169: Set the default value for Secure Settings ZEN_DURATION,
4036 // SHOW_ZEN_SETTINGS_SUGGESTION, ZEN_SETTINGS_UPDATE and
4037 // ZEN_SETTINGS_SUGGESTION_VIEWED
Nadav Barf9f115d2018-06-24 10:06:50 +03004038
Beverly91d0a632018-07-02 16:45:00 -04004039 final SettingsState globalSettings = getGlobalSettingsLocked();
4040 final Setting globalZenDuration = globalSettings.getSettingLocked(
4041 Global.ZEN_DURATION);
4042
4043 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4044 final Setting secureZenDuration = secureSettings.getSettingLocked(
4045 Secure.ZEN_DURATION);
4046
4047 // ZEN_DURATION
4048 if (!globalZenDuration.isNull()) {
4049 secureSettings.insertSettingLocked(
4050 Secure.ZEN_DURATION, globalZenDuration.getValue(), null, false,
4051 SettingsState.SYSTEM_PACKAGE_NAME);
4052
4053 // set global zen duration setting to null since it's deprecated
4054 globalSettings.insertSettingLocked(
4055 Global.ZEN_DURATION, null, null, true,
4056 SettingsState.SYSTEM_PACKAGE_NAME);
4057 } else if (secureZenDuration.isNull()) {
4058 String defaultZenDuration = Integer.toString(getContext()
4059 .getResources().getInteger(R.integer.def_zen_duration));
4060 secureSettings.insertSettingLocked(
4061 Secure.ZEN_DURATION, defaultZenDuration, null, true,
4062 SettingsState.SYSTEM_PACKAGE_NAME);
Nadav Barf9f115d2018-06-24 10:06:50 +03004063 }
Beverly91d0a632018-07-02 16:45:00 -04004064
4065 // SHOW_ZEN_SETTINGS_SUGGESTION
4066 final Setting currentShowZenSettingSuggestion = secureSettings.getSettingLocked(
4067 Secure.SHOW_ZEN_SETTINGS_SUGGESTION);
4068 if (currentShowZenSettingSuggestion.isNull()) {
4069 secureSettings.insertSettingLocked(
4070 Secure.SHOW_ZEN_SETTINGS_SUGGESTION, "1",
4071 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4072 }
4073
4074 // ZEN_SETTINGS_UPDATED
4075 final Setting currentUpdatedSetting = secureSettings.getSettingLocked(
4076 Secure.ZEN_SETTINGS_UPDATED);
4077 if (currentUpdatedSetting.isNull()) {
4078 secureSettings.insertSettingLocked(
4079 Secure.ZEN_SETTINGS_UPDATED, "0",
4080 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4081 }
4082
4083 // ZEN_SETTINGS_SUGGESTION_VIEWED
4084 final Setting currentSettingSuggestionViewed = secureSettings.getSettingLocked(
4085 Secure.ZEN_SETTINGS_SUGGESTION_VIEWED);
4086 if (currentSettingSuggestionViewed.isNull()) {
4087 secureSettings.insertSettingLocked(
4088 Secure.ZEN_SETTINGS_SUGGESTION_VIEWED, "0",
4089 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4090 }
4091
Nadav Barf9f115d2018-06-24 10:06:50 +03004092 currentVersion = 170;
4093 }
4094
Beverly91d0a632018-07-02 16:45:00 -04004095 if (currentVersion == 170) {
4096 // Version 170: Set the default value for Secure Settings:
4097 // CHARGING_SOUNDS_ENABLED and CHARGING_VIBRATION_ENABLED
4098
4099 final SettingsState globalSettings = getGlobalSettingsLocked();
4100 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4101
4102 // CHARGING_SOUNDS_ENABLED
4103 final Setting globalChargingSoundEnabled = globalSettings.getSettingLocked(
4104 Global.CHARGING_SOUNDS_ENABLED);
4105 final Setting secureChargingSoundsEnabled = secureSettings.getSettingLocked(
4106 Secure.CHARGING_SOUNDS_ENABLED);
4107
4108 if (!globalChargingSoundEnabled.isNull()) {
Beverlyff9c5872019-01-02 15:44:00 -05004109 if (secureChargingSoundsEnabled.isNull()) {
4110 secureSettings.insertSettingLocked(
4111 Secure.CHARGING_SOUNDS_ENABLED,
4112 globalChargingSoundEnabled.getValue(), null, false,
4113 SettingsState.SYSTEM_PACKAGE_NAME);
4114 }
Beverly91d0a632018-07-02 16:45:00 -04004115
4116 // set global charging_sounds_enabled setting to null since it's deprecated
4117 globalSettings.insertSettingLocked(
4118 Global.CHARGING_SOUNDS_ENABLED, null, null, true,
4119 SettingsState.SYSTEM_PACKAGE_NAME);
4120 } else if (secureChargingSoundsEnabled.isNull()) {
4121 String defChargingSoundsEnabled = getContext().getResources()
4122 .getBoolean(R.bool.def_charging_sounds_enabled) ? "1" : "0";
4123 secureSettings.insertSettingLocked(
4124 Secure.CHARGING_SOUNDS_ENABLED, defChargingSoundsEnabled, null,
4125 true, SettingsState.SYSTEM_PACKAGE_NAME);
4126 }
4127
4128 // CHARGING_VIBRATION_ENABLED
4129 final Setting secureChargingVibrationEnabled = secureSettings.getSettingLocked(
4130 Secure.CHARGING_VIBRATION_ENABLED);
4131
4132 if (secureChargingVibrationEnabled.isNull()) {
4133 String defChargingVibrationEnabled = getContext().getResources()
4134 .getBoolean(R.bool.def_charging_vibration_enabled) ? "1" : "0";
4135 secureSettings.insertSettingLocked(
4136 Secure.CHARGING_VIBRATION_ENABLED, defChargingVibrationEnabled,
4137 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4138 }
4139
4140 currentVersion = 171;
4141 }
4142
Nadav Bar8bc8c9e2018-09-12 15:41:51 +03004143 if (currentVersion == 171) {
4144 // Version 171: by default, add STREAM_VOICE_CALL to list of streams that can
4145 // be muted.
4146 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4147 final Setting currentSetting = systemSettings.getSettingLocked(
4148 Settings.System.MUTE_STREAMS_AFFECTED);
4149 if (!currentSetting.isNull()) {
4150 try {
4151 int currentSettingIntegerValue = Integer.parseInt(
4152 currentSetting.getValue());
4153 if ((currentSettingIntegerValue
4154 & (1 << AudioManager.STREAM_VOICE_CALL)) == 0) {
4155 systemSettings.insertSettingLocked(
4156 Settings.System.MUTE_STREAMS_AFFECTED,
4157 Integer.toString(
4158 currentSettingIntegerValue
4159 | (1 << AudioManager.STREAM_VOICE_CALL)),
4160 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4161 }
4162 } catch (NumberFormatException e) {
4163 // remove the setting in case it is not a valid integer
4164 Slog.w("Failed to parse integer value of MUTE_STREAMS_AFFECTED"
4165 + "setting, removing setting", e);
4166 systemSettings.deleteSettingLocked(
4167 Settings.System.MUTE_STREAMS_AFFECTED);
4168 }
4169
4170 }
4171 currentVersion = 172;
4172 }
4173
Soonil Nagarkar42da1b12019-01-22 11:29:27 -08004174 if (currentVersion == 172) {
4175 // Version 172: Set the default value for Secure Settings: LOCATION_MODE
4176
4177 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4178
4179 final Setting locationMode = secureSettings.getSettingLocked(
4180 Secure.LOCATION_MODE);
4181
4182 if (locationMode.isNull()) {
4183 final Setting locationProvidersAllowed = secureSettings.getSettingLocked(
4184 Secure.LOCATION_PROVIDERS_ALLOWED);
4185
Soonil Nagarkar4ee3ac22019-02-08 19:19:24 -08004186 final int defLocationMode;
4187 if (locationProvidersAllowed.isNull()) {
4188 defLocationMode = getContext().getResources().getInteger(
4189 R.integer.def_location_mode);
4190 } else {
4191 defLocationMode =
4192 !TextUtils.isEmpty(locationProvidersAllowed.getValue())
4193 ? Secure.LOCATION_MODE_ON
4194 : Secure.LOCATION_MODE_OFF;
4195 }
Soonil Nagarkar42da1b12019-01-22 11:29:27 -08004196 secureSettings.insertSettingLocked(
Soonil Nagarkar4ee3ac22019-02-08 19:19:24 -08004197 Secure.LOCATION_MODE, Integer.toString(defLocationMode),
Soonil Nagarkar42da1b12019-01-22 11:29:27 -08004198 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4199 }
4200
4201 currentVersion = 173;
4202 }
4203
Beverly4179f992019-02-08 11:34:16 -05004204 if (currentVersion == 173) {
4205 // Version 173: Set the default value for Secure Settings: NOTIFICATION_BUBBLES
4206
4207 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4208
4209 final Setting bubblesSetting = secureSettings.getSettingLocked(
4210 Secure.NOTIFICATION_BUBBLES);
4211
4212 if (bubblesSetting.isNull()) {
4213 secureSettings.insertSettingLocked(Secure.NOTIFICATION_BUBBLES,
4214 getContext().getResources().getBoolean(
4215 R.bool.def_notification_bubbles) ? "1" : "0", null,
4216 true, SettingsState.SYSTEM_PACKAGE_NAME);
4217 }
4218
4219 currentVersion = 174;
4220 }
4221
Yiwen Chen0305b572019-03-05 11:26:59 -08004222 if (currentVersion == 174) {
4223 // Version 174: Set the default value for Global Settings: APPLY_RAMPING_RINGER
4224
4225 final SettingsState globalSettings = getGlobalSettingsLocked();
4226
4227 Setting currentRampingRingerSetting = globalSettings.getSettingLocked(
4228 Settings.Global.APPLY_RAMPING_RINGER);
4229 if (currentRampingRingerSetting.isNull()) {
4230 globalSettings.insertSettingLocked(
4231 Settings.Global.APPLY_RAMPING_RINGER,
4232 getContext().getResources().getBoolean(
4233 R.bool.def_apply_ramping_ringer) ? "1" : "0", null,
4234 true, SettingsState.SYSTEM_PACKAGE_NAME);
4235 }
4236
4237 currentVersion = 175;
4238 }
4239
wilsonshih5d999e22019-03-20 11:50:42 +08004240 if (currentVersion == 175) {
4241 // Version 175: Set the default value for System Settings:
4242 // RING_VIBRATION_INTENSITY. If the notification vibration intensity has been
4243 // set and ring vibration intensity hasn't, the ring vibration intensity should
4244 // followed notification vibration intensity.
4245
4246 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4247
4248 Setting notificationVibrationIntensity = systemSettings.getSettingLocked(
4249 Settings.System.NOTIFICATION_VIBRATION_INTENSITY);
4250
4251 Setting ringVibrationIntensity = systemSettings.getSettingLocked(
4252 Settings.System.RING_VIBRATION_INTENSITY);
4253
4254 if (!notificationVibrationIntensity.isNull()
4255 && ringVibrationIntensity.isNull()) {
4256 systemSettings.insertSettingLocked(
4257 Settings.System.RING_VIBRATION_INTENSITY,
4258 notificationVibrationIntensity.getValue(),
4259 null , true, SettingsState.SYSTEM_PACKAGE_NAME);
4260 }
4261
4262 currentVersion = 176;
4263 }
4264
Winson Chungd9f2fb32019-03-05 11:10:12 -08004265 if (currentVersion == 176) {
4266 // Version 176: Migrate the existing swipe up setting into the resource overlay
Winson Chung3d5d3b32019-04-24 16:52:47 -07004267 // for the navigation bar interaction mode. We do so only if the
4268 // setting is set.
Winson Chungd9f2fb32019-03-05 11:10:12 -08004269
Winson Chungd9f2fb32019-03-05 11:10:12 -08004270 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4271 final Setting swipeUpSetting = secureSettings.getSettingLocked(
Winson Chung85e74592019-03-06 11:49:22 -08004272 "swipe_up_to_switch_apps_enabled");
Winson Chung3d5d3b32019-04-24 16:52:47 -07004273 if (swipeUpSetting != null && !swipeUpSetting.isNull()
4274 && swipeUpSetting.getValue().equals("1")) {
4275 final IOverlayManager overlayManager = IOverlayManager.Stub.asInterface(
4276 ServiceManager.getService(Context.OVERLAY_SERVICE));
Winson Chungd9f2fb32019-03-05 11:10:12 -08004277 try {
Winson Chung3d5d3b32019-04-24 16:52:47 -07004278 overlayManager.setEnabledExclusiveInCategory(
4279 NAV_BAR_MODE_2BUTTON_OVERLAY, UserHandle.USER_CURRENT);
Winson Chungd9f2fb32019-03-05 11:10:12 -08004280 } catch (RemoteException e) {
4281 throw new IllegalStateException(
4282 "Failed to set nav bar interaction mode overlay");
4283 }
4284 }
4285
4286 currentVersion = 177;
4287 }
4288
Edgar Wangeb8bddb2019-04-19 17:29:49 +08004289 if (currentVersion == 177) {
4290 // Version 177: Set the default value for Secure Settings: AWARE_ENABLED
4291
4292 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4293
4294 final Setting awareEnabled = secureSettings.getSettingLocked(
4295 Secure.AWARE_ENABLED);
4296
4297 if (awareEnabled.isNull()) {
4298 final boolean defAwareEnabled = getContext().getResources().getBoolean(
4299 R.bool.def_aware_enabled);
4300 secureSettings.insertSettingLocked(
4301 Secure.AWARE_ENABLED, defAwareEnabled ? "1" : "0",
4302 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4303 }
4304
4305 currentVersion = 178;
4306 }
4307
Edgar Wangdabc41e2019-05-07 18:53:31 +08004308 if (currentVersion == 178) {
4309 // Version 178: Set the default value for Secure Settings:
4310 // SKIP_GESTURE & SILENCE_GESTURE
4311
4312 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4313
4314 final Setting skipGesture = secureSettings.getSettingLocked(
4315 Secure.SKIP_GESTURE);
4316
4317 if (skipGesture.isNull()) {
4318 final boolean defSkipGesture = getContext().getResources().getBoolean(
4319 R.bool.def_skip_gesture);
4320 secureSettings.insertSettingLocked(
4321 Secure.SKIP_GESTURE, defSkipGesture ? "1" : "0",
4322 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4323 }
4324
4325 final Setting silenceGesture = secureSettings.getSettingLocked(
4326 Secure.SILENCE_GESTURE);
4327
4328 if (silenceGesture.isNull()) {
4329 final boolean defSilenceGesture = getContext().getResources().getBoolean(
4330 R.bool.def_silence_gesture);
4331 secureSettings.insertSettingLocked(
4332 Secure.SILENCE_GESTURE, defSilenceGesture ? "1" : "0",
4333 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4334 }
4335
4336 currentVersion = 179;
4337 }
4338
Mady Mellor95e879a2019-05-06 08:50:06 -07004339 if (currentVersion == 179) {
4340 // Version 178: Reset the default for Secure Settings: NOTIFICATION_BUBBLES
4341 // This is originally set in version 173, however, the default value changed
4342 // so this step is to ensure the value is updated to the correct defaulte
4343 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4344
4345 secureSettings.insertSettingLocked(Secure.NOTIFICATION_BUBBLES,
4346 getContext().getResources().getBoolean(
4347 R.bool.def_notification_bubbles) ? "1" : "0", null,
4348 true, SettingsState.SYSTEM_PACKAGE_NAME);
4349
4350 currentVersion = 180;
4351 }
Winson Chungd9f2fb32019-03-05 11:10:12 -08004352
Edgar Wang4870e4b2019-05-15 19:14:43 +08004353 if (currentVersion == 180) {
4354 // Version 180: Set the default value for Secure Settings: AWARE_LOCK_ENABLED
4355
4356 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4357
4358 final Setting awareLockEnabled = secureSettings.getSettingLocked(
4359 Secure.AWARE_LOCK_ENABLED);
4360
4361 if (awareLockEnabled.isNull()) {
4362 final boolean defAwareLockEnabled = getContext().getResources().getBoolean(
4363 R.bool.def_aware_lock_enabled);
4364 secureSettings.insertSettingLocked(
4365 Secure.AWARE_LOCK_ENABLED, defAwareLockEnabled ? "1" : "0",
4366 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4367 }
4368
4369 currentVersion = 181;
4370 }
4371
Eric Laurent02153e62019-06-12 17:20:03 -07004372 if (currentVersion == 181) {
4373 // Version cd : by default, add STREAM_BLUETOOTH_SCO to list of streams that can
4374 // be muted.
4375 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4376 final Setting currentSetting = systemSettings.getSettingLocked(
4377 Settings.System.MUTE_STREAMS_AFFECTED);
4378 if (!currentSetting.isNull()) {
4379 try {
4380 int currentSettingIntegerValue = Integer.parseInt(
4381 currentSetting.getValue());
4382 if ((currentSettingIntegerValue
4383 & (1 << AudioManager.STREAM_BLUETOOTH_SCO)) == 0) {
4384 systemSettings.insertSettingLocked(
4385 Settings.System.MUTE_STREAMS_AFFECTED,
4386 Integer.toString(
4387 currentSettingIntegerValue
4388 | (1 << AudioManager.STREAM_BLUETOOTH_SCO)),
4389 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4390 }
4391 } catch (NumberFormatException e) {
4392 // remove the setting in case it is not a valid integer
4393 Slog.w("Failed to parse integer value of MUTE_STREAMS_AFFECTED"
4394 + "setting, removing setting", e);
4395 systemSettings.deleteSettingLocked(
4396 Settings.System.MUTE_STREAMS_AFFECTED);
4397 }
4398
4399 }
4400 currentVersion = 182;
4401 }
4402
Felipe Lemeff355092017-04-03 12:55:02 -07004403 // vXXX: Add new settings above this point.
4404
Dan Sandler71f85e92016-07-20 13:46:05 -04004405 if (currentVersion != newVersion) {
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07004406 Slog.wtf("SettingsProvider", "warning: upgrading settings database to version "
Dan Sandler71f85e92016-07-20 13:46:05 -04004407 + newVersion + " left it at "
Stephen Chen5d0922f2017-03-27 10:28:04 -07004408 + currentVersion +
4409 " instead; this is probably a bug. Did you update SETTINGS_VERSION?",
4410 new Throwable());
Dan Sandler71f85e92016-07-20 13:46:05 -04004411 if (DEBUG) {
4412 throw new RuntimeException("db upgrade error");
4413 }
4414 }
4415
Jeff Brown503cffc2015-03-26 18:08:51 -07004416 // Return the current version.
4417 return currentVersion;
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -08004418 }
4419 }
Svet Ganov13701552017-02-23 12:45:17 -08004420
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004421 private void ensureLegacyDefaultValueAndSystemSetUpdatedLocked(SettingsState settings,
4422 int userId) {
Svet Ganov13701552017-02-23 12:45:17 -08004423 List<String> names = settings.getSettingNamesLocked();
4424 final int nameCount = names.size();
4425 for (int i = 0; i < nameCount; i++) {
4426 String name = names.get(i);
4427 Setting setting = settings.getSettingLocked(name);
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004428
4429 // In the upgrade case we pretend the call is made from the app
4430 // that made the last change to the setting to properly determine
4431 // whether the call has been made by a system component.
4432 int callingUid = -1;
4433 try {
4434 callingUid = mPackageManager.getPackageUid(setting.getPackageName(), 0, userId);
4435 } catch (RemoteException e) {
4436 /* ignore - handled below */
4437 }
4438 if (callingUid < 0) {
4439 Slog.e(LOG_TAG, "Unknown package: " + setting.getPackageName());
4440 continue;
4441 }
4442 try {
4443 final boolean systemSet = SettingsState.isSystemPackage(getContext(),
Matt Pape25c940d2019-03-26 12:14:33 -07004444 setting.getPackageName(), callingUid, userId);
Svet Ganov13701552017-02-23 12:45:17 -08004445 if (systemSet) {
4446 settings.insertSettingLocked(name, setting.getValue(),
4447 setting.getTag(), true, setting.getPackageName());
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004448 } else if (setting.getDefaultValue() != null && setting.isDefaultFromSystem()) {
4449 // We had a bug where changes by non-system packages were marked
4450 // as system made and as a result set as the default. Therefore, if
4451 // the package changed the setting last is not a system one but the
4452 // setting is marked as its default coming from the system we clear
4453 // the default and clear the system set flag.
4454 settings.resetSettingDefaultValueLocked(name);
Svet Ganov13701552017-02-23 12:45:17 -08004455 }
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004456 } catch (IllegalStateException e) {
4457 // If the package goes over its quota during the upgrade, don't
4458 // crash but just log the error as the system does the upgrade.
4459 Slog.e(LOG_TAG, "Error upgrading setting: " + setting.getName(), e);
4460
Svet Ganov13701552017-02-23 12:45:17 -08004461 }
4462 }
4463 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08004464 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004465}