blob: b8d0997554c3b2f67d3eaa198e4e84950fcfd1e7 [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);
Matt Pape793b15c2019-10-07 13:17:20 -0700442 return packageValuesForCallResult(getAllConfigFlags(prefix),
443 isTrackingGeneration(args));
Matt Pape6bfc62e2018-11-28 13:16:03 -0800444 }
445
446 case Settings.CALL_METHOD_LIST_GLOBAL: {
Nicholas Sauer72500532018-11-21 10:30:58 -0800447 Bundle result = new Bundle();
448 result.putStringArrayList(RESULT_SETTINGS_LIST,
Matt Pape6bfc62e2018-11-28 13:16:03 -0800449 buildSettingsList(getAllGlobalSettings(null)));
Nicholas Sauer72500532018-11-21 10:30:58 -0800450 return result;
451 }
452
453 case Settings.CALL_METHOD_LIST_SECURE: {
454 Bundle result = new Bundle();
455 result.putStringArrayList(RESULT_SETTINGS_LIST,
456 buildSettingsList(getAllSecureSettings(requestingUserId, null)));
457 return result;
458 }
459
Matt Pape6bfc62e2018-11-28 13:16:03 -0800460 case Settings.CALL_METHOD_LIST_SYSTEM: {
Nicholas Sauer72500532018-11-21 10:30:58 -0800461 Bundle result = new Bundle();
462 result.putStringArrayList(RESULT_SETTINGS_LIST,
Matt Pape6bfc62e2018-11-28 13:16:03 -0800463 buildSettingsList(getAllSystemSettings(requestingUserId, null)));
Nicholas Sauer72500532018-11-21 10:30:58 -0800464 return result;
465 }
466
Svetoslav7ec28e82015-05-20 17:01:10 -0700467 default: {
468 Slog.w(LOG_TAG, "call() with invalid method: " + method);
469 } break;
Christopher Tate06efb532012-08-24 15:29:27 -0700470 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700471
Christopher Tate06efb532012-08-24 15:29:27 -0700472 return null;
473 }
474
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800475 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800476 public String getType(Uri uri) {
477 Arguments args = new Arguments(uri, null, null, true);
478 if (TextUtils.isEmpty(args.name)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700479 return "vnd.android.cursor.dir/" + args.table;
480 } else {
Svetoslav7ec28e82015-05-20 17:01:10 -0700481 return "vnd.android.cursor.item/" + args.table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700482 }
483 }
484
485 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800486 public Cursor query(Uri uri, String[] projection, String where, String[] whereArgs,
487 String order) {
488 if (DEBUG) {
489 Slog.v(LOG_TAG, "query() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700490 }
491
Svetoslav683914b2015-01-15 14:22:26 -0800492 Arguments args = new Arguments(uri, where, whereArgs, true);
493 String[] normalizedProjection = normalizeProjection(projection);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700494
Svetoslav683914b2015-01-15 14:22:26 -0800495 // If a legacy table that is gone, done.
496 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
497 return new MatrixCursor(normalizedProjection, 0);
498 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700499
Svetoslav7ec28e82015-05-20 17:01:10 -0700500 switch (args.table) {
501 case TABLE_GLOBAL: {
502 if (args.name != null) {
503 Setting setting = getGlobalSetting(args.name);
504 return packageSettingForQuery(setting, normalizedProjection);
505 } else {
506 return getAllGlobalSettings(projection);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700507 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700508 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700509
Svetoslav7ec28e82015-05-20 17:01:10 -0700510 case TABLE_SECURE: {
511 final int userId = UserHandle.getCallingUserId();
512 if (args.name != null) {
513 Setting setting = getSecureSetting(args.name, userId);
514 return packageSettingForQuery(setting, normalizedProjection);
515 } else {
516 return getAllSecureSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800517 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700518 }
Svetoslav683914b2015-01-15 14:22:26 -0800519
Svetoslav7ec28e82015-05-20 17:01:10 -0700520 case TABLE_SYSTEM: {
521 final int userId = UserHandle.getCallingUserId();
522 if (args.name != null) {
523 Setting setting = getSystemSetting(args.name, userId);
524 return packageSettingForQuery(setting, normalizedProjection);
525 } else {
526 return getAllSystemSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800527 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700528 }
Svetoslav683914b2015-01-15 14:22:26 -0800529
Svetoslav7ec28e82015-05-20 17:01:10 -0700530 default: {
531 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700532 }
533 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700534 }
535
Nicholas Sauer72500532018-11-21 10:30:58 -0800536 private ArrayList<String> buildSettingsList(Cursor cursor) {
537 final ArrayList<String> lines = new ArrayList<String>();
538 try {
539 while (cursor != null && cursor.moveToNext()) {
540 lines.add(cursor.getString(1) + "=" + cursor.getString(2));
541 }
542 } finally {
543 if (cursor != null) {
544 cursor.close();
545 }
546 }
547 return lines;
548 }
549
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700550 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800551 public Uri insert(Uri uri, ContentValues values) {
552 if (DEBUG) {
553 Slog.v(LOG_TAG, "insert() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700554 }
555
Svetoslav683914b2015-01-15 14:22:26 -0800556 String table = getValidTableOrThrow(uri);
Christopher Tate06efb532012-08-24 15:29:27 -0700557
Svetoslav683914b2015-01-15 14:22:26 -0800558 // If a legacy table that is gone, done.
559 if (REMOVED_LEGACY_TABLES.contains(table)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 return null;
561 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700562
Svetoslav683914b2015-01-15 14:22:26 -0800563 String name = values.getAsString(Settings.Secure.NAME);
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700564 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800565 return null;
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700566 }
567
Svetoslav683914b2015-01-15 14:22:26 -0800568 String value = values.getAsString(Settings.Secure.VALUE);
569
Svetoslav7ec28e82015-05-20 17:01:10 -0700570 switch (table) {
571 case TABLE_GLOBAL: {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800572 if (insertGlobalSetting(name, value, null, false,
573 UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700574 return Uri.withAppendedPath(Settings.Global.CONTENT_URI, name);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700575 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700576 } break;
577
578 case TABLE_SECURE: {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800579 if (insertSecureSetting(name, value, null, false,
580 UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700581 return Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name);
582 }
583 } break;
584
585 case TABLE_SYSTEM: {
586 if (insertSystemSetting(name, value, UserHandle.getCallingUserId())) {
587 return Uri.withAppendedPath(Settings.System.CONTENT_URI, name);
588 }
589 } break;
590
591 default: {
592 throw new IllegalArgumentException("Bad Uri path:" + uri);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700593 }
594 }
595
Svetoslav683914b2015-01-15 14:22:26 -0800596 return null;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700597 }
598
599 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800600 public int bulkInsert(Uri uri, ContentValues[] allValues) {
601 if (DEBUG) {
602 Slog.v(LOG_TAG, "bulkInsert() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700604
Svetoslav683914b2015-01-15 14:22:26 -0800605 int insertionCount = 0;
606 final int valuesCount = allValues.length;
607 for (int i = 0; i < valuesCount; i++) {
608 ContentValues values = allValues[i];
609 if (insert(uri, values) != null) {
610 insertionCount++;
611 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700612 }
Svetoslav683914b2015-01-15 14:22:26 -0800613
614 return insertionCount;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700615 }
616
617 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800618 public int delete(Uri uri, String where, String[] whereArgs) {
619 if (DEBUG) {
620 Slog.v(LOG_TAG, "delete() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700622
Svetoslav683914b2015-01-15 14:22:26 -0800623 Arguments args = new Arguments(uri, where, whereArgs, false);
624
625 // If a legacy table that is gone, done.
626 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
627 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700628 }
Svetoslav683914b2015-01-15 14:22:26 -0800629
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700630 if (!isKeyValid(args.name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800631 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700632 }
Svetoslav683914b2015-01-15 14:22:26 -0800633
Svetoslav7ec28e82015-05-20 17:01:10 -0700634 switch (args.table) {
635 case TABLE_GLOBAL: {
636 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700637 return deleteGlobalSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700638 }
Svetoslav683914b2015-01-15 14:22:26 -0800639
Svetoslav7ec28e82015-05-20 17:01:10 -0700640 case TABLE_SECURE: {
641 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700642 return deleteSecureSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700643 }
Svetoslav683914b2015-01-15 14:22:26 -0800644
Svetoslav7ec28e82015-05-20 17:01:10 -0700645 case TABLE_SYSTEM: {
646 final int userId = UserHandle.getCallingUserId();
647 return deleteSystemSetting(args.name, userId) ? 1 : 0;
648 }
649
650 default: {
651 throw new IllegalArgumentException("Bad Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800652 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700653 }
Svetoslav683914b2015-01-15 14:22:26 -0800654 }
655
656 @Override
657 public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
658 if (DEBUG) {
659 Slog.v(LOG_TAG, "update() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700660 }
Svetoslav683914b2015-01-15 14:22:26 -0800661
662 Arguments args = new Arguments(uri, where, whereArgs, false);
663
664 // If a legacy table that is gone, done.
665 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
666 return 0;
667 }
668
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700669 String name = values.getAsString(Settings.Secure.NAME);
670 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800671 return 0;
672 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700673 String value = values.getAsString(Settings.Secure.VALUE);
Svetoslav683914b2015-01-15 14:22:26 -0800674
Svetoslav7ec28e82015-05-20 17:01:10 -0700675 switch (args.table) {
676 case TABLE_GLOBAL: {
677 final int userId = UserHandle.getCallingUserId();
Svetoslav Ganove080da92016-12-21 17:10:35 -0800678 return updateGlobalSetting(args.name, value, null, false,
679 userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700680 }
Svetoslav683914b2015-01-15 14:22:26 -0800681
Svetoslav7ec28e82015-05-20 17:01:10 -0700682 case TABLE_SECURE: {
683 final int userId = UserHandle.getCallingUserId();
Svetoslav Ganove080da92016-12-21 17:10:35 -0800684 return updateSecureSetting(args.name, value, null, false,
685 userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700686 }
Svetoslav683914b2015-01-15 14:22:26 -0800687
Svetoslav7ec28e82015-05-20 17:01:10 -0700688 case TABLE_SYSTEM: {
689 final int userId = UserHandle.getCallingUserId();
690 return updateSystemSetting(args.name, value, userId) ? 1 : 0;
691 }
Svetoslav683914b2015-01-15 14:22:26 -0800692
Svetoslav7ec28e82015-05-20 17:01:10 -0700693 default: {
694 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800695 }
696 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700697 }
698
699 @Override
700 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
Robin Lee2ab02e22016-07-28 18:41:23 +0100701 final int userId = getUserIdFromUri(uri, UserHandle.getCallingUserId());
702 if (userId != UserHandle.getCallingUserId()) {
703 getContext().enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS,
704 "Access files from the settings of another user");
705 }
706 uri = ContentProvider.getUriWithoutUserId(uri);
707
Andre Lago3fa139c2016-08-04 13:53:44 +0100708 final String cacheRingtoneSetting;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700709 final String cacheName;
710 if (Settings.System.RINGTONE_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100711 cacheRingtoneSetting = Settings.System.RINGTONE;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700712 cacheName = Settings.System.RINGTONE_CACHE;
713 } else if (Settings.System.NOTIFICATION_SOUND_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100714 cacheRingtoneSetting = Settings.System.NOTIFICATION_SOUND;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700715 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
716 } else if (Settings.System.ALARM_ALERT_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100717 cacheRingtoneSetting = Settings.System.ALARM_ALERT;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700718 cacheName = Settings.System.ALARM_ALERT_CACHE;
719 } else {
720 throw new FileNotFoundException("Direct file access no longer supported; "
721 + "ringtone playback is available through android.media.Ringtone");
722 }
723
Andre Lago3fa139c2016-08-04 13:53:44 +0100724 int actualCacheOwner;
725 // Redirect cache to parent if ringtone setting is owned by profile parent
726 synchronized (mLock) {
727 actualCacheOwner = resolveOwningUserIdForSystemSettingLocked(userId,
728 cacheRingtoneSetting);
729 }
730 final File cacheFile = new File(getRingtoneCacheDir(actualCacheOwner), cacheName);
Jeff Sharkey413573a2016-02-22 17:52:45 -0700731 return ParcelFileDescriptor.open(cacheFile, ParcelFileDescriptor.parseMode(mode));
732 }
733
734 private File getRingtoneCacheDir(int userId) {
735 final File cacheDir = new File(Environment.getDataSystemDeDirectory(userId), "ringtones");
736 cacheDir.mkdir();
737 SELinux.restorecon(cacheDir);
738 return cacheDir;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700739 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800740
Eugene Suslad72c3972016-12-27 15:49:30 -0800741 /**
742 * Dump all settings as a proto buf.
743 *
744 * @param fd The file to dump to
745 */
746 void dumpProto(@NonNull FileDescriptor fd) {
747 ProtoOutputStream proto = new ProtoOutputStream(fd);
748
749 synchronized (mLock) {
750 SettingsProtoDumpUtil.dumpProtoLocked(mSettingsRegistry, proto);
Eugene Suslad72c3972016-12-27 15:49:30 -0800751 }
752
753 proto.flush();
754 }
755
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700756 public void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800757 synchronized (mLock) {
758 final long identity = Binder.clearCallingIdentity();
759 try {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700760 SparseBooleanArray users = mSettingsRegistry.getKnownUsersLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800761 final int userCount = users.size();
762 for (int i = 0; i < userCount; i++) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700763 dumpForUserLocked(users.keyAt(i), pw);
Svetoslavb505ccc2015-02-17 12:41:04 -0800764 }
765 } finally {
766 Binder.restoreCallingIdentity(identity);
767 }
768 }
769 }
770
Andreas Gampeb58893072018-09-05 16:52:31 -0700771 @GuardedBy("mLock")
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700772 private void dumpForUserLocked(int userId, PrintWriter pw) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700773 if (userId == UserHandle.USER_SYSTEM) {
Matt Pape1b31a332018-10-17 09:58:28 -0700774 pw.println("CONFIG SETTINGS (user " + userId + ")");
775 SettingsState configSettings = mSettingsRegistry.getSettingsLocked(
776 SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM);
777 if (configSettings != null) {
778 dumpSettingsLocked(configSettings, pw);
779 pw.println();
780 configSettings.dumpHistoricalOperations(pw);
781 }
782
Svetoslavb505ccc2015-02-17 12:41:04 -0800783 pw.println("GLOBAL SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700784 SettingsState globalSettings = mSettingsRegistry.getSettingsLocked(
785 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700786 if (globalSettings != null) {
787 dumpSettingsLocked(globalSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800788 pw.println();
789 globalSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700790 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800791 }
792
793 pw.println("SECURE SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700794 SettingsState secureSettings = mSettingsRegistry.getSettingsLocked(
795 SETTINGS_TYPE_SECURE, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700796 if (secureSettings != null) {
797 dumpSettingsLocked(secureSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800798 pw.println();
799 secureSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700800 }
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700801
Svetoslavb505ccc2015-02-17 12:41:04 -0800802 pw.println("SYSTEM SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700803 SettingsState systemSettings = mSettingsRegistry.getSettingsLocked(
804 SETTINGS_TYPE_SYSTEM, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700805 if (systemSettings != null) {
806 dumpSettingsLocked(systemSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800807 pw.println();
808 systemSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700809 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800810 }
811
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700812 private void dumpSettingsLocked(SettingsState settingsState, PrintWriter pw) {
813 List<String> names = settingsState.getSettingNamesLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800814
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700815 final int nameCount = names.size();
Svetoslavb505ccc2015-02-17 12:41:04 -0800816
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700817 for (int i = 0; i < nameCount; i++) {
818 String name = names.get(i);
819 Setting setting = settingsState.getSettingLocked(name);
820 pw.print("_id:"); pw.print(toDumpString(setting.getId()));
821 pw.print(" name:"); pw.print(toDumpString(name));
822 if (setting.getPackageName() != null) {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800823 pw.print(" pkg:"); pw.print(setting.getPackageName());
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700824 }
825 pw.print(" value:"); pw.print(toDumpString(setting.getValue()));
Svetoslav Ganove080da92016-12-21 17:10:35 -0800826 if (setting.getDefaultValue() != null) {
827 pw.print(" default:"); pw.print(setting.getDefaultValue());
Eugene Suslad72c3972016-12-27 15:49:30 -0800828 pw.print(" defaultSystemSet:"); pw.print(setting.isDefaultFromSystem());
Svetoslav Ganove080da92016-12-21 17:10:35 -0800829 }
830 if (setting.getTag() != null) {
831 pw.print(" tag:"); pw.print(setting.getTag());
832 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800833 pw.println();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700834 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800835 }
836
Svetoslav7e0683b2015-08-03 16:02:52 -0700837 private static String toDumpString(String s) {
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700838 if (s != null) {
839 return s;
840 }
841 return "{null}";
842 }
843
Svetoslav683914b2015-01-15 14:22:26 -0800844 private void registerBroadcastReceivers() {
845 IntentFilter userFilter = new IntentFilter();
846 userFilter.addAction(Intent.ACTION_USER_REMOVED);
847 userFilter.addAction(Intent.ACTION_USER_STOPPED);
848
849 getContext().registerReceiver(new BroadcastReceiver() {
850 @Override
851 public void onReceive(Context context, Intent intent) {
852 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700853 UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800854
855 switch (intent.getAction()) {
856 case Intent.ACTION_USER_REMOVED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700857 synchronized (mLock) {
858 mSettingsRegistry.removeUserStateLocked(userId, true);
859 }
Svetoslav683914b2015-01-15 14:22:26 -0800860 } break;
861
862 case Intent.ACTION_USER_STOPPED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700863 synchronized (mLock) {
864 mSettingsRegistry.removeUserStateLocked(userId, false);
865 }
Svetoslav683914b2015-01-15 14:22:26 -0800866 } break;
867 }
868 }
869 }, userFilter);
870
871 PackageMonitor monitor = new PackageMonitor() {
872 @Override
873 public void onPackageRemoved(String packageName, int uid) {
874 synchronized (mLock) {
Zimuzoc56192c2018-07-25 10:40:01 +0100875 mSettingsRegistry.removeSettingsForPackageLocked(packageName,
Svetoslav683914b2015-01-15 14:22:26 -0800876 UserHandle.getUserId(uid));
877 }
878 }
Mark Rathjend891f012017-01-19 04:10:37 +0000879
880 @Override
881 public void onUidRemoved(int uid) {
882 synchronized (mLock) {
883 mSettingsRegistry.onUidRemovedLocked(uid);
884 }
885 }
Zimuzoc56192c2018-07-25 10:40:01 +0100886
887 @Override
888 public void onPackageDataCleared(String packageName, int uid) {
889 synchronized (mLock) {
890 mSettingsRegistry.removeSettingsForPackageLocked(packageName,
891 UserHandle.getUserId(uid));
892 }
893 }
Svetoslav683914b2015-01-15 14:22:26 -0800894 };
895
896 // package changes
897 monitor.register(getContext(), BackgroundThread.getHandler().getLooper(),
898 UserHandle.ALL, true);
899 }
900
Svet Ganov53a441c2016-04-19 19:38:00 -0700901 private void startWatchingUserRestrictionChanges() {
902 // TODO: The current design of settings looking different based on user restrictions
903 // should be reworked to keep them separate and system code should check the setting
904 // first followed by checking the user restriction before performing an operation.
905 UserManagerInternal userManager = LocalServices.getService(UserManagerInternal.class);
906 userManager.addUserRestrictionsListener((int userId, Bundle newRestrictions,
907 Bundle prevRestrictions) -> {
Irina Dumitrescue3696872019-01-09 16:07:59 +0000908 Set<String> changedRestrictions = getRestrictionDiff(prevRestrictions, newRestrictions);
Svet Ganov53a441c2016-04-19 19:38:00 -0700909 // We are changing the settings affected by restrictions to their current
910 // value with a forced update to ensure that all cross profile dependencies
911 // are taken into account. Also make sure the settings update to.. the same
912 // value passes the security checks, so clear binder calling id.
Irina Dumitrescue3696872019-01-09 16:07:59 +0000913 if (changedRestrictions.contains(UserManager.DISALLOW_SHARE_LOCATION)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700914 final long identity = Binder.clearCallingIdentity();
915 try {
916 synchronized (mLock) {
917 Setting setting = getSecureSetting(
Soonil Nagarkar4ee3ac22019-02-08 19:19:24 -0800918 Settings.Secure.LOCATION_MODE, userId);
919 updateSecureSetting(Settings.Secure.LOCATION_MODE,
920 setting != null ? setting.getValue() : null, null,
921 true, userId, true);
922 setting = getSecureSetting(
Svet Ganov53a441c2016-04-19 19:38:00 -0700923 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, userId);
924 updateSecureSetting(Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800925 setting != null ? setting.getValue() : null, null,
926 true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700927 }
928 } finally {
929 Binder.restoreCallingIdentity(identity);
930 }
931 }
Irina Dumitrescue3696872019-01-09 16:07:59 +0000932 if (changedRestrictions.contains(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)
933 || changedRestrictions.contains(
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100934 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700935 final long identity = Binder.clearCallingIdentity();
936 try {
937 synchronized (mLock) {
938 Setting setting = getGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800939 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700940 updateGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800941 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700942 }
943 } finally {
944 Binder.restoreCallingIdentity(identity);
945 }
946 }
Irina Dumitrescue3696872019-01-09 16:07:59 +0000947 if (changedRestrictions.contains(UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700948 final long identity = Binder.clearCallingIdentity();
949 try {
950 synchronized (mLock) {
951 Setting setting = getGlobalSetting(Settings.Global.ADB_ENABLED);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800952 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700953 updateGlobalSetting(Settings.Global.ADB_ENABLED,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800954 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700955 }
956 } finally {
957 Binder.restoreCallingIdentity(identity);
958 }
959 }
Irina Dumitrescue3696872019-01-09 16:07:59 +0000960 if (changedRestrictions.contains(UserManager.ENSURE_VERIFY_APPS)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700961 final long identity = Binder.clearCallingIdentity();
962 try {
963 synchronized (mLock) {
964 Setting enable = getGlobalSetting(
965 Settings.Global.PACKAGE_VERIFIER_ENABLE);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800966 String enableValue = enable != null ? enable.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700967 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_ENABLE,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800968 enableValue, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700969 Setting include = getGlobalSetting(
970 Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800971 String includeValue = include != null ? include.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700972 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800973 includeValue, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700974 }
975 } finally {
976 Binder.restoreCallingIdentity(identity);
977 }
978 }
Irina Dumitrescue3696872019-01-09 16:07:59 +0000979 if (changedRestrictions.contains(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700980 final long identity = Binder.clearCallingIdentity();
981 try {
982 synchronized (mLock) {
983 Setting setting = getGlobalSetting(
984 Settings.Global.PREFERRED_NETWORK_MODE);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800985 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700986 updateGlobalSetting(Settings.Global.PREFERRED_NETWORK_MODE,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800987 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700988 }
989 } finally {
990 Binder.restoreCallingIdentity(identity);
991 }
992 }
993 });
994 }
995
Irina Dumitrescue3696872019-01-09 16:07:59 +0000996 private static Set<String> getRestrictionDiff(Bundle prevRestrictions, Bundle newRestrictions) {
997 Set<String> restrictionNames = Sets.newArraySet();
998 restrictionNames.addAll(prevRestrictions.keySet());
999 restrictionNames.addAll(newRestrictions.keySet());
1000 Set<String> diff = Sets.newArraySet();
1001 for (String restrictionName : restrictionNames) {
1002 if (prevRestrictions.getBoolean(restrictionName) != newRestrictions.getBoolean(
1003 restrictionName)) {
1004 diff.add(restrictionName);
1005 }
1006 }
1007 return diff;
1008 }
1009
Matt Pape1b31a332018-10-17 09:58:28 -07001010 private Setting getConfigSetting(String name) {
1011 if (DEBUG) {
1012 Slog.v(LOG_TAG, "getConfigSetting(" + name + ")");
1013 }
1014
Stanislav Zholnin55799502019-03-08 14:54:55 +00001015 DeviceConfig.enforceReadPermission(getContext(), /*namespace=*/name.split("/")[0]);
Matt Pape1b31a332018-10-17 09:58:28 -07001016
1017 // Get the value.
1018 synchronized (mLock) {
1019 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_CONFIG,
1020 UserHandle.USER_SYSTEM, name);
1021 }
1022 }
1023
Matt Papec1323dc2018-12-11 12:32:42 -08001024 private boolean insertConfigSetting(String name, String value, boolean makeDefault) {
Matt Pape1b31a332018-10-17 09:58:28 -07001025 if (DEBUG) {
1026 Slog.v(LOG_TAG, "insertConfigSetting(" + name + ", " + value + ", "
Matt Papec1323dc2018-12-11 12:32:42 -08001027 + makeDefault + ")");
Matt Pape1b31a332018-10-17 09:58:28 -07001028 }
Matt Papec1323dc2018-12-11 12:32:42 -08001029 return mutateConfigSetting(name, value, null, makeDefault,
1030 MUTATION_OPERATION_INSERT, 0);
Matt Pape1b31a332018-10-17 09:58:28 -07001031 }
1032
Matt Papec1323dc2018-12-11 12:32:42 -08001033 private boolean deleteConfigSetting(String name) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08001034 if (DEBUG) {
Matt Papec1323dc2018-12-11 12:32:42 -08001035 Slog.v(LOG_TAG, "deleteConfigSetting(" + name + ")");
Matt Pape6bfc62e2018-11-28 13:16:03 -08001036 }
Matt Papec1323dc2018-12-11 12:32:42 -08001037 return mutateConfigSetting(name, null, null, false,
1038 MUTATION_OPERATION_DELETE, 0);
Matt Pape6bfc62e2018-11-28 13:16:03 -08001039 }
1040
Matt Papec1323dc2018-12-11 12:32:42 -08001041 private void resetConfigSetting(int mode, String prefix) {
Matt Pape1b31a332018-10-17 09:58:28 -07001042 if (DEBUG) {
Matt Papec1323dc2018-12-11 12:32:42 -08001043 Slog.v(LOG_TAG, "resetConfigSetting(" + mode + ", " + prefix + ")");
Matt Pape1b31a332018-10-17 09:58:28 -07001044 }
Matt Papec1323dc2018-12-11 12:32:42 -08001045 mutateConfigSetting(null, null, prefix, false,
1046 MUTATION_OPERATION_RESET, mode);
Matt Pape1b31a332018-10-17 09:58:28 -07001047 }
1048
Matt Pape6bfc62e2018-11-28 13:16:03 -08001049 private boolean mutateConfigSetting(String name, String value, String prefix,
Matt Papec1323dc2018-12-11 12:32:42 -08001050 boolean makeDefault, int operation, int mode) {
Stanislav Zholnin5a25a842019-03-13 14:43:26 +00001051 enforceWritePermission(Manifest.permission.WRITE_DEVICE_CONFIG);
Matt Pape1b31a332018-10-17 09:58:28 -07001052
Matt Pape1b31a332018-10-17 09:58:28 -07001053 // Perform the mutation.
1054 synchronized (mLock) {
1055 switch (operation) {
1056 case MUTATION_OPERATION_INSERT: {
1057 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_CONFIG,
Matt Papec1323dc2018-12-11 12:32:42 -08001058 UserHandle.USER_SYSTEM, name, value, null, makeDefault, true,
Matt Pape7b1c6cd2019-01-04 08:10:41 -08001059 resolveCallingPackage(), false, null);
Matt Pape1b31a332018-10-17 09:58:28 -07001060 }
1061
Matt Pape6bfc62e2018-11-28 13:16:03 -08001062 case MUTATION_OPERATION_DELETE: {
1063 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_CONFIG,
Matt Papec1323dc2018-12-11 12:32:42 -08001064 UserHandle.USER_SYSTEM, name, false, null);
Matt Pape6bfc62e2018-11-28 13:16:03 -08001065 }
1066
Matt Pape1b31a332018-10-17 09:58:28 -07001067 case MUTATION_OPERATION_RESET: {
1068 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_CONFIG,
Matt Pape7b1c6cd2019-01-04 08:10:41 -08001069 UserHandle.USER_SYSTEM, resolveCallingPackage(), mode, null, prefix);
Matt Pape1b31a332018-10-17 09:58:28 -07001070 } return true;
1071 }
1072 }
1073
1074 return false;
1075 }
1076
Matt Pape793b15c2019-10-07 13:17:20 -07001077 private HashMap<String, String> getAllConfigFlags(@Nullable String prefix) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08001078 if (DEBUG) {
1079 Slog.v(LOG_TAG, "getAllConfigFlags() for " + prefix);
1080 }
1081
1082 synchronized (mLock) {
1083 // Get the settings.
1084 SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
1085 SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM);
Matt Pape6bfc62e2018-11-28 13:16:03 -08001086 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_CONFIG,
1087 UserHandle.USER_SYSTEM);
1088
1089 final int nameCount = names.size();
Matt Pape793b15c2019-10-07 13:17:20 -07001090 HashMap<String, String> flagsToValues = new HashMap<>(names.size());
Matt Pape6bfc62e2018-11-28 13:16:03 -08001091
1092 for (int i = 0; i < nameCount; i++) {
1093 String name = names.get(i);
1094 Setting setting = settingsState.getSettingLocked(name);
1095 if (prefix == null || setting.getName().startsWith(prefix)) {
1096 flagsToValues.put(setting.getName(), setting.getValue());
1097 }
1098 }
1099
1100 return flagsToValues;
1101 }
1102 }
1103
Svetoslav7ec28e82015-05-20 17:01:10 -07001104 private Cursor getAllGlobalSettings(String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001105 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001106 Slog.v(LOG_TAG, "getAllGlobalSettings()");
Svetoslav683914b2015-01-15 14:22:26 -08001107 }
1108
Svetoslav7ec28e82015-05-20 17:01:10 -07001109 synchronized (mLock) {
1110 // Get the settings.
1111 SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001112 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001113
Chad Brubaker97bccee2017-01-05 15:51:41 -08001114 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_GLOBAL,
1115 UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001116
Svetoslav7ec28e82015-05-20 17:01:10 -07001117 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001118
Svetoslav7ec28e82015-05-20 17:01:10 -07001119 String[] normalizedProjection = normalizeProjection(projection);
1120 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001121
Svetoslav7ec28e82015-05-20 17:01:10 -07001122 // Anyone can get the global settings, so no security checks.
1123 for (int i = 0; i < nameCount; i++) {
1124 String name = names.get(i);
1125 Setting setting = settingsState.getSettingLocked(name);
1126 appendSettingToCursor(result, setting);
1127 }
1128
1129 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001130 }
Svetoslav683914b2015-01-15 14:22:26 -08001131 }
1132
Svetoslav7ec28e82015-05-20 17:01:10 -07001133 private Setting getGlobalSetting(String name) {
Svetoslav683914b2015-01-15 14:22:26 -08001134 if (DEBUG) {
1135 Slog.v(LOG_TAG, "getGlobalSetting(" + name + ")");
1136 }
1137
Chad Brubakera6830e72017-04-28 17:34:36 -07001138 // Ensure the caller can access the setting.
1139 enforceSettingReadable(name, SETTINGS_TYPE_GLOBAL, UserHandle.getCallingUserId());
1140
Svetoslav683914b2015-01-15 14:22:26 -08001141 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001142 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -07001143 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_GLOBAL,
Xiaohui Chen43765b72015-08-31 10:57:33 -07001144 UserHandle.USER_SYSTEM, name);
Svetoslav683914b2015-01-15 14:22:26 -08001145 }
Svetoslav683914b2015-01-15 14:22:26 -08001146 }
1147
Svetoslav Ganove080da92016-12-21 17:10:35 -08001148 private boolean updateGlobalSetting(String name, String value, String tag,
1149 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001150 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001151 Slog.v(LOG_TAG, "updateGlobalSetting(" + name + ", " + value + ", "
1152 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1153 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001154 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001155 return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId,
1156 MUTATION_OPERATION_UPDATE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001157 }
1158
Svetoslav Ganove080da92016-12-21 17:10:35 -08001159 private boolean insertGlobalSetting(String name, String value, String tag,
1160 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001161 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001162 Slog.v(LOG_TAG, "insertGlobalSetting(" + name + ", " + value + ", "
1163 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1164 + ", " + forceNotify + ")");
Svetoslav7ec28e82015-05-20 17:01:10 -07001165 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001166 return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId,
1167 MUTATION_OPERATION_INSERT, forceNotify, 0);
Svetoslav7ec28e82015-05-20 17:01:10 -07001168 }
1169
Svet Ganov53a441c2016-04-19 19:38:00 -07001170 private boolean deleteGlobalSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001171 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001172 Slog.v(LOG_TAG, "deleteGlobalSetting(" + name + ", " + requestingUserId
1173 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001174 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001175 return mutateGlobalSetting(name, null, null, false, requestingUserId,
1176 MUTATION_OPERATION_DELETE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001177 }
1178
Svetoslav Ganove080da92016-12-21 17:10:35 -08001179 private void resetGlobalSetting(int requestingUserId, int mode, String tag) {
1180 if (DEBUG) {
1181 Slog.v(LOG_TAG, "resetGlobalSetting(" + requestingUserId + ", "
1182 + mode + ", " + tag + ")");
1183 }
1184 mutateGlobalSetting(null, null, tag, false, requestingUserId,
1185 MUTATION_OPERATION_RESET, false, mode);
1186 }
1187
1188 private boolean mutateGlobalSetting(String name, String value, String tag,
1189 boolean makeDefault, int requestingUserId, int operation, boolean forceNotify,
1190 int mode) {
Svetoslav683914b2015-01-15 14:22:26 -08001191 // Make sure the caller can change the settings - treated as secure.
1192 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
1193
Svetoslav683914b2015-01-15 14:22:26 -08001194 // Resolve the userId on whose behalf the call is made.
1195 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1196
Makoto Onuki28da2e32015-11-20 11:30:44 -08001197 // If this is a setting that is currently restricted for this user, do not allow
1198 // unrestricting changes.
yuemingw1d13eae2018-01-30 17:27:54 +00001199 if (name != null && mUserManagerInternal.isSettingRestrictedForUser(
1200 name, callingUserId, value, Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -08001201 return false;
1202 }
1203
1204 // Perform the mutation.
Svetoslav7ec28e82015-05-20 17:01:10 -07001205 synchronized (mLock) {
1206 switch (operation) {
1207 case MUTATION_OPERATION_INSERT: {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001208 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_GLOBAL,
1209 UserHandle.USER_SYSTEM, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001210 getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001211 }
Svetoslav683914b2015-01-15 14:22:26 -08001212
Svetoslav7ec28e82015-05-20 17:01:10 -07001213 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001214 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_GLOBAL,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001215 UserHandle.USER_SYSTEM, name, forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001216 }
Svetoslav683914b2015-01-15 14:22:26 -08001217
Svetoslav7ec28e82015-05-20 17:01:10 -07001218 case MUTATION_OPERATION_UPDATE: {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001219 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_GLOBAL,
1220 UserHandle.USER_SYSTEM, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001221 getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001222 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001223
1224 case MUTATION_OPERATION_RESET: {
1225 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_GLOBAL,
1226 UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag);
1227 } return true;
Svetoslav683914b2015-01-15 14:22:26 -08001228 }
1229 }
1230
1231 return false;
1232 }
1233
Christopher Tateb218e762017-04-05 16:34:07 -07001234 private PackageInfo getCallingPackageInfo(int userId) {
1235 try {
1236 return mPackageManager.getPackageInfo(getCallingPackage(),
1237 PackageManager.GET_SIGNATURES, userId);
1238 } catch (RemoteException e) {
1239 throw new IllegalStateException("Package " + getCallingPackage() + " doesn't exist");
1240 }
1241 }
1242
Svetoslav7ec28e82015-05-20 17:01:10 -07001243 private Cursor getAllSecureSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001244 if (DEBUG) {
1245 Slog.v(LOG_TAG, "getAllSecureSettings(" + userId + ")");
1246 }
1247
1248 // Resolve the userId on whose behalf the call is made.
1249 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
1250
Christopher Tateb218e762017-04-05 16:34:07 -07001251 // The relevant "calling package" userId will be the owning userId for some
1252 // profiles, and we can't do the lookup inside our [lock held] loop, so work out
1253 // up front who the effective "new SSAID" user ID for that settings name will be.
1254 final int ssaidUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
1255 Settings.Secure.ANDROID_ID);
1256 final PackageInfo ssaidCallingPkg = getCallingPackageInfo(ssaidUserId);
1257
Svetoslav7ec28e82015-05-20 17:01:10 -07001258 synchronized (mLock) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001259 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SECURE, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -08001260
Svetoslav7ec28e82015-05-20 17:01:10 -07001261 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001262
Svetoslav7ec28e82015-05-20 17:01:10 -07001263 String[] normalizedProjection = normalizeProjection(projection);
1264 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001265
Svetoslav7ec28e82015-05-20 17:01:10 -07001266 for (int i = 0; i < nameCount; i++) {
1267 String name = names.get(i);
1268 // Determine the owning user as some profile settings are cloned from the parent.
1269 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
1270 name);
Svetoslav683914b2015-01-15 14:22:26 -08001271
Alex Klyubin1991f572017-03-03 14:08:36 -08001272 if (!isSecureSettingAccessible(name, callingUserId, owningUserId)) {
1273 // This caller is not permitted to access this setting. Pretend the setting
1274 // doesn't exist.
Svetoslav Ganov83a1f7f2016-04-27 13:50:49 -07001275 continue;
Svetoslav7ec28e82015-05-20 17:01:10 -07001276 }
Svetoslav683914b2015-01-15 14:22:26 -08001277
Mark Rathjen7599f132017-01-23 14:15:54 -08001278 // As of Android O, the SSAID is read from an app-specific entry in table
Mark Rathjend891f012017-01-19 04:10:37 +00001279 // SETTINGS_FILE_SSAID, unless accessed by a system process.
1280 final Setting setting;
1281 if (isNewSsaidSetting(name)) {
Christopher Tateb218e762017-04-05 16:34:07 -07001282 setting = getSsaidSettingLocked(ssaidCallingPkg, owningUserId);
Mark Rathjend891f012017-01-19 04:10:37 +00001283 } else {
1284 setting = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE, owningUserId,
1285 name);
1286 }
Svetoslav7ec28e82015-05-20 17:01:10 -07001287 appendSettingToCursor(result, setting);
Svetoslav683914b2015-01-15 14:22:26 -08001288 }
1289
Svetoslav7ec28e82015-05-20 17:01:10 -07001290 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001291 }
Svetoslav683914b2015-01-15 14:22:26 -08001292 }
1293
Svetoslav7ec28e82015-05-20 17:01:10 -07001294 private Setting getSecureSetting(String name, int requestingUserId) {
Makoto Onuki0000d322017-11-28 16:31:47 -08001295 return getSecureSetting(name, requestingUserId, /*enableOverride=*/ false);
1296 }
1297
1298 private Setting getSecureSetting(String name, int requestingUserId, boolean enableOverride) {
Svetoslav683914b2015-01-15 14:22:26 -08001299 if (DEBUG) {
1300 Slog.v(LOG_TAG, "getSecureSetting(" + name + ", " + requestingUserId + ")");
1301 }
1302
1303 // Resolve the userId on whose behalf the call is made.
1304 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1305
Chad Brubakera6830e72017-04-28 17:34:36 -07001306 // Ensure the caller can access the setting.
1307 enforceSettingReadable(name, SETTINGS_TYPE_SECURE, UserHandle.getCallingUserId());
1308
Svetoslav683914b2015-01-15 14:22:26 -08001309 // Determine the owning user as some profile settings are cloned from the parent.
1310 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
1311
Alex Klyubin1991f572017-03-03 14:08:36 -08001312 if (!isSecureSettingAccessible(name, callingUserId, owningUserId)) {
1313 // This caller is not permitted to access this setting. Pretend the setting doesn't
1314 // exist.
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001315 SettingsState settings = mSettingsRegistry.getSettingsLocked(SETTINGS_TYPE_SECURE,
1316 owningUserId);
1317 return settings != null ? settings.getNullSetting() : null;
Svetoslav683914b2015-01-15 14:22:26 -08001318 }
1319
Christopher Tateb218e762017-04-05 16:34:07 -07001320 // As of Android O, the SSAID is read from an app-specific entry in table
1321 // SETTINGS_FILE_SSAID, unless accessed by a system process.
1322 if (isNewSsaidSetting(name)) {
1323 PackageInfo callingPkg = getCallingPackageInfo(owningUserId);
1324 synchronized (mLock) {
1325 return getSsaidSettingLocked(callingPkg, owningUserId);
Mark Rathjend891f012017-01-19 04:10:37 +00001326 }
Christopher Tateb218e762017-04-05 16:34:07 -07001327 }
Makoto Onuki0000d322017-11-28 16:31:47 -08001328 if (enableOverride) {
Kweku Adams5e0052b2019-02-22 15:17:52 -08001329 if (Secure.LOCATION_MODE.equals(name)) {
1330 final Setting overridden = getLocationModeSetting(owningUserId);
Makoto Onuki0000d322017-11-28 16:31:47 -08001331 if (overridden != null) {
1332 return overridden;
1333 }
1334 }
1335 }
Mark Rathjend891f012017-01-19 04:10:37 +00001336
Christopher Tateb218e762017-04-05 16:34:07 -07001337 // Not the SSAID; do a straight lookup
1338 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -07001339 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav7ec28e82015-05-20 17:01:10 -07001340 owningUserId, name);
1341 }
Svetoslav683914b2015-01-15 14:22:26 -08001342 }
1343
Mark Rathjend891f012017-01-19 04:10:37 +00001344 private boolean isNewSsaidSetting(String name) {
1345 return Settings.Secure.ANDROID_ID.equals(name)
1346 && UserHandle.getAppId(Binder.getCallingUid()) >= Process.FIRST_APPLICATION_UID;
1347 }
1348
Andreas Gampeb58893072018-09-05 16:52:31 -07001349 @GuardedBy("mLock")
Christopher Tateb218e762017-04-05 16:34:07 -07001350 private Setting getSsaidSettingLocked(PackageInfo callingPkg, int owningUserId) {
Mark Rathjend891f012017-01-19 04:10:37 +00001351 // Get uid of caller (key) used to store ssaid value
1352 String name = Integer.toString(
1353 UserHandle.getUid(owningUserId, UserHandle.getAppId(Binder.getCallingUid())));
1354
1355 if (DEBUG) {
1356 Slog.v(LOG_TAG, "getSsaidSettingLocked(" + name + "," + owningUserId + ")");
1357 }
1358
1359 // Retrieve the ssaid from the table if present.
1360 final Setting ssaid = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SSAID, owningUserId,
1361 name);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001362 // If the app is an Instant App use its stored SSAID instead of our own.
1363 final String instantSsaid;
1364 final long token = Binder.clearCallingIdentity();
1365 try {
1366 instantSsaid = mPackageManager.getInstantAppAndroidId(callingPkg.packageName,
1367 owningUserId);
1368 } catch (RemoteException e) {
1369 Slog.e(LOG_TAG, "Failed to get Instant App Android ID", e);
1370 return null;
1371 } finally {
1372 Binder.restoreCallingIdentity(token);
1373 }
Svet Ganov96c99462017-05-05 14:27:13 -07001374
1375 final SettingsState ssaidSettings = mSettingsRegistry.getSettingsLocked(
1376 SETTINGS_TYPE_SSAID, owningUserId);
1377
Chad Brubaker0d277a72017-04-12 16:56:53 -07001378 if (instantSsaid != null) {
1379 // Use the stored value if it is still valid.
1380 if (ssaid != null && instantSsaid.equals(ssaid.getValue())) {
Svet Ganov96c99462017-05-05 14:27:13 -07001381 return mascaradeSsaidSetting(ssaidSettings, ssaid);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001382 }
1383 // The value has changed, update the stored value.
Chad Brubaker0d277a72017-04-12 16:56:53 -07001384 final boolean success = ssaidSettings.insertSettingLocked(name, instantSsaid, null,
1385 true, callingPkg.packageName);
1386 if (!success) {
1387 throw new IllegalStateException("Failed to update instant app android id");
1388 }
Svet Ganov96c99462017-05-05 14:27:13 -07001389 Setting setting = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SSAID,
1390 owningUserId, name);
1391 return mascaradeSsaidSetting(ssaidSettings, setting);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001392 }
Mark Rathjend891f012017-01-19 04:10:37 +00001393
1394 // Lazy initialize ssaid if not yet present in ssaid table.
Mark Rathjenea617592017-01-18 23:03:41 -08001395 if (ssaid == null || ssaid.isNull() || ssaid.getValue() == null) {
Svet Ganov96c99462017-05-05 14:27:13 -07001396 Setting setting = mSettingsRegistry.generateSsaidLocked(callingPkg, owningUserId);
1397 return mascaradeSsaidSetting(ssaidSettings, setting);
Mark Rathjend891f012017-01-19 04:10:37 +00001398 }
1399
Svet Ganov96c99462017-05-05 14:27:13 -07001400 return mascaradeSsaidSetting(ssaidSettings, ssaid);
1401 }
1402
1403 private Setting mascaradeSsaidSetting(SettingsState settingsState, Setting ssaidSetting) {
1404 // SSAID settings are located in a dedicated table for internal bookkeeping
1405 // but for the world they reside in the secure table, so adjust the key here.
1406 // We have a special name when looking it up but want the world to see it as
1407 // "android_id".
1408 if (ssaidSetting != null) {
1409 return settingsState.new Setting(ssaidSetting) {
1410 @Override
1411 public int getKey() {
1412 final int userId = getUserIdFromKey(super.getKey());
1413 return makeKey(SETTINGS_TYPE_SECURE, userId);
1414 }
1415
1416 @Override
1417 public String getName() {
1418 return Settings.Secure.ANDROID_ID;
1419 }
1420 };
1421 }
1422 return null;
Mark Rathjend891f012017-01-19 04:10:37 +00001423 }
1424
Kweku Adams5e0052b2019-02-22 15:17:52 -08001425 private Setting getLocationModeSetting(int owningUserId) {
Makoto Onuki0000d322017-11-28 16:31:47 -08001426 synchronized (mLock) {
1427 final Setting setting = getGlobalSetting(
1428 Global.LOCATION_GLOBAL_KILL_SWITCH);
1429 if (!"1".equals(setting.getValue())) {
1430 return null;
1431 }
1432 // Global kill-switch is enabled. Return an empty value.
1433 final SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
1434 SETTINGS_TYPE_SECURE, owningUserId);
1435 return settingsState.new Setting(
Kweku Adams5e0052b2019-02-22 15:17:52 -08001436 Secure.LOCATION_MODE,
Makoto Onuki0000d322017-11-28 16:31:47 -08001437 "", // value
1438 "", // tag
1439 "", // default value
1440 "", // package name
1441 false, // from system
1442 "0" // id
1443 ) {
1444 @Override
1445 public boolean update(String value, boolean setDefault, String packageName,
1446 String tag, boolean forceNonSystemPackage) {
Kweku Adams5e0052b2019-02-22 15:17:52 -08001447 Slog.wtf(LOG_TAG, "update shouldn't be called on this instance.");
Makoto Onuki0000d322017-11-28 16:31:47 -08001448 return false;
1449 }
1450 };
1451 }
1452 }
1453
Svetoslav Ganove080da92016-12-21 17:10:35 -08001454 private boolean insertSecureSetting(String name, String value, String tag,
1455 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001456 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001457 Slog.v(LOG_TAG, "insertSecureSetting(" + name + ", " + value + ", "
Svetoslav Ganove080da92016-12-21 17:10:35 -08001458 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1459 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001460 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001461 return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId,
1462 MUTATION_OPERATION_INSERT, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001463 }
1464
Svet Ganov53a441c2016-04-19 19:38:00 -07001465 private boolean deleteSecureSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001466 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001467 Slog.v(LOG_TAG, "deleteSecureSetting(" + name + ", " + requestingUserId
1468 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001469 }
1470
Svetoslav Ganove080da92016-12-21 17:10:35 -08001471 return mutateSecureSetting(name, null, null, false, requestingUserId,
1472 MUTATION_OPERATION_DELETE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001473 }
1474
Svetoslav Ganove080da92016-12-21 17:10:35 -08001475 private boolean updateSecureSetting(String name, String value, String tag,
1476 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001477 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001478 Slog.v(LOG_TAG, "updateSecureSetting(" + name + ", " + value + ", "
Svetoslav Ganove080da92016-12-21 17:10:35 -08001479 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1480 + ", " + forceNotify +")");
Svetoslav683914b2015-01-15 14:22:26 -08001481 }
1482
Svetoslav Ganove080da92016-12-21 17:10:35 -08001483 return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId,
1484 MUTATION_OPERATION_UPDATE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001485 }
1486
Svetoslav Ganove080da92016-12-21 17:10:35 -08001487 private void resetSecureSetting(int requestingUserId, int mode, String tag) {
1488 if (DEBUG) {
1489 Slog.v(LOG_TAG, "resetSecureSetting(" + requestingUserId + ", "
1490 + mode + ", " + tag + ")");
1491 }
1492
1493 mutateSecureSetting(null, null, tag, false, requestingUserId,
1494 MUTATION_OPERATION_RESET, false, mode);
1495 }
1496
1497 private boolean mutateSecureSetting(String name, String value, String tag,
1498 boolean makeDefault, int requestingUserId, int operation, boolean forceNotify,
1499 int mode) {
Svetoslav683914b2015-01-15 14:22:26 -08001500 // Make sure the caller can change the settings.
1501 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
1502
Svetoslav683914b2015-01-15 14:22:26 -08001503 // Resolve the userId on whose behalf the call is made.
1504 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1505
Makoto Onuki28da2e32015-11-20 11:30:44 -08001506 // If this is a setting that is currently restricted for this user, do not allow
1507 // unrestricting changes.
yuemingw1d13eae2018-01-30 17:27:54 +00001508 if (name != null && mUserManagerInternal.isSettingRestrictedForUser(
1509 name, callingUserId, value, Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -08001510 return false;
1511 }
1512
1513 // Determine the owning user as some profile settings are cloned from the parent.
1514 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
1515
1516 // Only the owning user can change the setting.
1517 if (owningUserId != callingUserId) {
1518 return false;
1519 }
1520
Svetoslav683914b2015-01-15 14:22:26 -08001521 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001522 synchronized (mLock) {
Soonil Nagarkar10b19602019-09-10 15:46:32 -07001523 // Special cases for location providers (sigh).
1524 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
1525 return updateLocationProvidersAllowedLocked(value, tag, owningUserId, makeDefault,
1526 forceNotify);
1527 }
1528
Svetoslav7ec28e82015-05-20 17:01:10 -07001529 switch (operation) {
1530 case MUTATION_OPERATION_INSERT: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001531 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001532 owningUserId, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001533 getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001534 }
Svetoslav683914b2015-01-15 14:22:26 -08001535
Svetoslav7ec28e82015-05-20 17:01:10 -07001536 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001537 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001538 owningUserId, name, forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001539 }
Svetoslav683914b2015-01-15 14:22:26 -08001540
Svetoslav7ec28e82015-05-20 17:01:10 -07001541 case MUTATION_OPERATION_UPDATE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001542 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001543 owningUserId, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001544 getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001545 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001546
1547 case MUTATION_OPERATION_RESET: {
1548 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_SECURE,
1549 UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag);
1550 } return true;
Svetoslav683914b2015-01-15 14:22:26 -08001551 }
1552 }
1553
1554 return false;
1555 }
1556
Svetoslav7ec28e82015-05-20 17:01:10 -07001557 private Cursor getAllSystemSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001558 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001559 Slog.v(LOG_TAG, "getAllSecureSystem(" + userId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001560 }
1561
1562 // Resolve the userId on whose behalf the call is made.
1563 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
1564
Svetoslav7ec28e82015-05-20 17:01:10 -07001565 synchronized (mLock) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001566 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SYSTEM, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -08001567
Svetoslav7ec28e82015-05-20 17:01:10 -07001568 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001569
Svetoslav7ec28e82015-05-20 17:01:10 -07001570 String[] normalizedProjection = normalizeProjection(projection);
1571 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001572
Svetoslav7ec28e82015-05-20 17:01:10 -07001573 for (int i = 0; i < nameCount; i++) {
1574 String name = names.get(i);
Svetoslav683914b2015-01-15 14:22:26 -08001575
Svetoslav7ec28e82015-05-20 17:01:10 -07001576 // Determine the owning user as some profile settings are cloned from the parent.
1577 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId,
1578 name);
Svetoslav683914b2015-01-15 14:22:26 -08001579
Svetoslav7ec28e82015-05-20 17:01:10 -07001580 Setting setting = mSettingsRegistry.getSettingLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001581 SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001582 appendSettingToCursor(result, setting);
1583 }
1584
1585 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001586 }
Svetoslav683914b2015-01-15 14:22:26 -08001587 }
1588
Svetoslav7ec28e82015-05-20 17:01:10 -07001589 private Setting getSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001590 if (DEBUG) {
1591 Slog.v(LOG_TAG, "getSystemSetting(" + name + ", " + requestingUserId + ")");
1592 }
1593
1594 // Resolve the userId on whose behalf the call is made.
1595 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1596
Chad Brubakera6830e72017-04-28 17:34:36 -07001597 // Ensure the caller can access the setting.
1598 enforceSettingReadable(name, SETTINGS_TYPE_SYSTEM, UserHandle.getCallingUserId());
Chad Brubaker97bccee2017-01-05 15:51:41 -08001599
Svetoslav683914b2015-01-15 14:22:26 -08001600 // Determine the owning user as some profile settings are cloned from the parent.
1601 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1602
1603 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001604 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -07001605 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001606 }
Svetoslav683914b2015-01-15 14:22:26 -08001607 }
1608
Svetoslav7ec28e82015-05-20 17:01:10 -07001609 private boolean insertSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001610 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001611 Slog.v(LOG_TAG, "insertSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001612 + requestingUserId + ")");
1613 }
1614
Svetoslav7ec28e82015-05-20 17:01:10 -07001615 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT);
Svetoslav683914b2015-01-15 14:22:26 -08001616 }
1617
Svetoslav7ec28e82015-05-20 17:01:10 -07001618 private boolean deleteSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001619 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001620 Slog.v(LOG_TAG, "deleteSystemSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001621 }
1622
Svetoslav7ec28e82015-05-20 17:01:10 -07001623 return mutateSystemSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE);
Svetoslav683914b2015-01-15 14:22:26 -08001624 }
1625
Svetoslav7ec28e82015-05-20 17:01:10 -07001626 private boolean updateSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001627 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001628 Slog.v(LOG_TAG, "updateSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001629 + requestingUserId + ")");
1630 }
1631
Svetoslav7ec28e82015-05-20 17:01:10 -07001632 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE);
Svetoslav683914b2015-01-15 14:22:26 -08001633 }
1634
Svetoslav7ec28e82015-05-20 17:01:10 -07001635 private boolean mutateSystemSetting(String name, String value, int runAsUserId,
Svetoslav683914b2015-01-15 14:22:26 -08001636 int operation) {
Billy Lau6ad2d662015-07-18 00:26:58 +01001637 if (!hasWriteSecureSettingsPermission()) {
1638 // If the caller doesn't hold WRITE_SECURE_SETTINGS, we verify whether this
1639 // operation is allowed for the calling package through appops.
1640 if (!Settings.checkAndNoteWriteSettingsOperation(getContext(),
1641 Binder.getCallingUid(), getCallingPackage(), true)) {
1642 return false;
1643 }
Svetoslav683914b2015-01-15 14:22:26 -08001644 }
1645
Svetoslav683914b2015-01-15 14:22:26 -08001646 // Resolve the userId on whose behalf the call is made.
1647 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId);
1648
yuemingw1d13eae2018-01-30 17:27:54 +00001649 if (name != null && mUserManagerInternal.isSettingRestrictedForUser(
1650 name, callingUserId, value, Binder.getCallingUid())) {
1651 return false;
1652 }
1653
Svetoslavd8d25e02015-11-20 13:09:26 -08001654 // Enforce what the calling package can mutate the system settings.
1655 enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name, callingUserId);
1656
Svetoslav683914b2015-01-15 14:22:26 -08001657 // Determine the owning user as some profile settings are cloned from the parent.
1658 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1659
1660 // Only the owning user id can change the setting.
1661 if (owningUserId != callingUserId) {
1662 return false;
1663 }
1664
Jeff Sharkey413573a2016-02-22 17:52:45 -07001665 // Invalidate any relevant cache files
1666 String cacheName = null;
1667 if (Settings.System.RINGTONE.equals(name)) {
1668 cacheName = Settings.System.RINGTONE_CACHE;
1669 } else if (Settings.System.NOTIFICATION_SOUND.equals(name)) {
1670 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
1671 } else if (Settings.System.ALARM_ALERT.equals(name)) {
1672 cacheName = Settings.System.ALARM_ALERT_CACHE;
1673 }
1674 if (cacheName != null) {
1675 final File cacheFile = new File(
Andre Lago3fa139c2016-08-04 13:53:44 +01001676 getRingtoneCacheDir(owningUserId), cacheName);
Jeff Sharkey413573a2016-02-22 17:52:45 -07001677 cacheFile.delete();
1678 }
1679
Svetoslav683914b2015-01-15 14:22:26 -08001680 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001681 synchronized (mLock) {
1682 switch (operation) {
1683 case MUTATION_OPERATION_INSERT: {
1684 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001685 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001686 owningUserId, name, value, null, false, getCallingPackage(),
1687 false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001688 }
1689
1690 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001691 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001692 owningUserId, name, false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001693 }
1694
1695 case MUTATION_OPERATION_UPDATE: {
1696 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001697 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001698 owningUserId, name, value, null, false, getCallingPackage(),
1699 false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001700 }
Svetoslav683914b2015-01-15 14:22:26 -08001701 }
1702
Svetoslav7ec28e82015-05-20 17:01:10 -07001703 return false;
Svetoslav683914b2015-01-15 14:22:26 -08001704 }
Svetoslav683914b2015-01-15 14:22:26 -08001705 }
1706
Billy Lau6ad2d662015-07-18 00:26:58 +01001707 private boolean hasWriteSecureSettingsPermission() {
Svetoslavf41334b2015-06-23 12:06:03 -07001708 // Write secure settings is a more protected permission. If caller has it we are good.
1709 if (getContext().checkCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS)
1710 == PackageManager.PERMISSION_GRANTED) {
1711 return true;
1712 }
1713
Svetoslavf41334b2015-06-23 12:06:03 -07001714 return false;
1715 }
1716
Svetoslav683914b2015-01-15 14:22:26 -08001717 private void validateSystemSettingValue(String name, String value) {
Al Sutton91f89d02019-08-16 12:56:57 +01001718 Validator validator = SystemSettingsValidators.VALIDATORS.get(name);
Svetoslav683914b2015-01-15 14:22:26 -08001719 if (validator != null && !validator.validate(value)) {
1720 throw new IllegalArgumentException("Invalid value: " + value
1721 + " for setting: " + name);
1722 }
1723 }
1724
Alex Klyubin1991f572017-03-03 14:08:36 -08001725 /**
1726 * Returns {@code true} if the specified secure setting should be accessible to the caller.
1727 */
1728 private boolean isSecureSettingAccessible(String name, int callingUserId,
1729 int owningUserId) {
1730 // Special case for location (sigh).
1731 // This check is not inside the name-based checks below because this method performs checks
1732 // only if the calling user ID is not the same as the owning user ID.
1733 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
1734 return false;
1735 }
1736
1737 switch (name) {
1738 case "bluetooth_address":
1739 // BluetoothManagerService for some reason stores the Android's Bluetooth MAC
1740 // address in this secure setting. Secure settings can normally be read by any app,
1741 // which thus enables them to bypass the recently introduced restrictions on access
1742 // to device identifiers.
1743 // To mitigate this we make this setting available only to callers privileged to see
1744 // this device's MAC addresses, same as through public API
1745 // BluetoothAdapter.getAddress() (see BluetoothManagerService for details).
1746 return getContext().checkCallingOrSelfPermission(
1747 Manifest.permission.LOCAL_MAC_ADDRESS) == PackageManager.PERMISSION_GRANTED;
1748 default:
1749 return true;
1750 }
1751 }
1752
Svetoslav683914b2015-01-15 14:22:26 -08001753 private boolean isLocationProvidersAllowedRestricted(String name, int callingUserId,
1754 int owningUserId) {
1755 // Optimization - location providers are restricted only for managed profiles.
1756 if (callingUserId == owningUserId) {
1757 return false;
1758 }
1759 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)
1760 && mUserManager.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION,
1761 new UserHandle(callingUserId))) {
1762 return true;
1763 }
1764 return false;
1765 }
1766
Svetoslav683914b2015-01-15 14:22:26 -08001767 private int resolveOwningUserIdForSecureSettingLocked(int userId, String setting) {
1768 return resolveOwningUserIdLocked(userId, sSecureCloneToManagedSettings, setting);
1769 }
1770
1771 private int resolveOwningUserIdForSystemSettingLocked(int userId, String setting) {
Andre Lago3fa139c2016-08-04 13:53:44 +01001772 final int parentId;
1773 // Resolves dependency if setting has a dependency and the calling user has a parent
1774 if (sSystemCloneFromParentOnDependency.containsKey(setting)
1775 && (parentId = getGroupParentLocked(userId)) != userId) {
1776 // The setting has a dependency and the profile has a parent
1777 String dependency = sSystemCloneFromParentOnDependency.get(setting);
Chad Brubaker97bccee2017-01-05 15:51:41 -08001778 // Lookup the dependency setting as ourselves, some callers may not have access to it.
1779 final long token = Binder.clearCallingIdentity();
1780 try {
1781 Setting settingObj = getSecureSetting(dependency, userId);
1782 if (settingObj != null && settingObj.getValue().equals("1")) {
1783 return parentId;
1784 }
1785 } finally {
1786 Binder.restoreCallingIdentity(token);
Andre Lago3fa139c2016-08-04 13:53:44 +01001787 }
1788 }
Svetoslav683914b2015-01-15 14:22:26 -08001789 return resolveOwningUserIdLocked(userId, sSystemCloneToManagedSettings, setting);
1790 }
1791
1792 private int resolveOwningUserIdLocked(int userId, Set<String> keys, String name) {
1793 final int parentId = getGroupParentLocked(userId);
1794 if (parentId != userId && keys.contains(name)) {
1795 return parentId;
1796 }
1797 return userId;
1798 }
1799
Svetoslavf41334b2015-06-23 12:06:03 -07001800 private void enforceRestrictedSystemSettingsMutationForCallingPackage(int operation,
Xiaohui Chen43765b72015-08-31 10:57:33 -07001801 String name, int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001802 // System/root/shell can mutate whatever secure settings they want.
1803 final int callingUid = Binder.getCallingUid();
Svetoslav Ganove080da92016-12-21 17:10:35 -08001804 final int appId = UserHandle.getAppId(callingUid);
1805 if (appId == android.os.Process.SYSTEM_UID
1806 || appId == Process.SHELL_UID
1807 || appId == Process.ROOT_UID) {
Svetoslav683914b2015-01-15 14:22:26 -08001808 return;
1809 }
1810
1811 switch (operation) {
1812 case MUTATION_OPERATION_INSERT:
1813 // Insert updates.
1814 case MUTATION_OPERATION_UPDATE: {
1815 if (Settings.System.PUBLIC_SETTINGS.contains(name)) {
1816 return;
1817 }
1818
1819 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001820 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001821
1822 // Privileged apps can do whatever they want.
1823 if ((packageInfo.applicationInfo.privateFlags
1824 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1825 return;
1826 }
1827
1828 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1829 packageInfo.applicationInfo.targetSdkVersion, name);
1830 } break;
1831
1832 case MUTATION_OPERATION_DELETE: {
1833 if (Settings.System.PUBLIC_SETTINGS.contains(name)
1834 || Settings.System.PRIVATE_SETTINGS.contains(name)) {
1835 throw new IllegalArgumentException("You cannot delete system defined"
1836 + " secure settings.");
1837 }
1838
1839 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001840 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001841
1842 // Privileged apps can do whatever they want.
1843 if ((packageInfo.applicationInfo.privateFlags &
1844 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1845 return;
1846 }
1847
1848 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1849 packageInfo.applicationInfo.targetSdkVersion, name);
1850 } break;
1851 }
1852 }
1853
Todd Kennedybe0b8892017-02-15 14:13:52 -08001854 private Set<String> getInstantAppAccessibleSettings(int settingsType) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001855 switch (settingsType) {
1856 case SETTINGS_TYPE_GLOBAL:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001857 return Settings.Global.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001858 case SETTINGS_TYPE_SECURE:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001859 return Settings.Secure.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001860 case SETTINGS_TYPE_SYSTEM:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001861 return Settings.System.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001862 default:
1863 throw new IllegalArgumentException("Invalid settings type: " + settingsType);
1864 }
1865 }
1866
Chad Brubaker20e0dc32017-04-28 18:24:55 -07001867 private Set<String> getOverlayInstantAppAccessibleSettings(int settingsType) {
1868 switch (settingsType) {
1869 case SETTINGS_TYPE_GLOBAL:
1870 return OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS;
1871 case SETTINGS_TYPE_SYSTEM:
1872 return OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS;
1873 case SETTINGS_TYPE_SECURE:
1874 return OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS;
1875 default:
1876 throw new IllegalArgumentException("Invalid settings type: " + settingsType);
1877 }
1878 }
1879
Andreas Gampeb58893072018-09-05 16:52:31 -07001880 @GuardedBy("mLock")
Chad Brubaker97bccee2017-01-05 15:51:41 -08001881 private List<String> getSettingsNamesLocked(int settingsType, int userId) {
Chad Brubakerb6108d62017-12-23 20:06:44 -08001882 // Don't enforce the instant app whitelist for now -- its too prone to unintended breakage
1883 // in the current form.
1884 return mSettingsRegistry.getSettingsNamesLocked(settingsType, userId);
Chad Brubaker97bccee2017-01-05 15:51:41 -08001885 }
1886
Chad Brubakera6830e72017-04-28 17:34:36 -07001887 private void enforceSettingReadable(String settingName, int settingsType, int userId) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001888 if (UserHandle.getAppId(Binder.getCallingUid()) < Process.FIRST_APPLICATION_UID) {
1889 return;
1890 }
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001891 ApplicationInfo ai = getCallingApplicationInfoOrThrow();
Svetoslav Ganov096d3042017-01-30 16:34:13 -08001892 if (!ai.isInstantApp()) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001893 return;
1894 }
Chad Brubaker20e0dc32017-04-28 18:24:55 -07001895 if (!getInstantAppAccessibleSettings(settingsType).contains(settingName)
1896 && !getOverlayInstantAppAccessibleSettings(settingsType).contains(settingName)) {
Chad Brubakerb6108d62017-12-23 20:06:44 -08001897 // Don't enforce the instant app whitelist for now -- its too prone to unintended
1898 // breakage in the current form.
1899 Slog.w(LOG_TAG, "Instant App " + ai.packageName
1900 + " trying to access unexposed setting, this will be an error in the future.");
Chad Brubaker97bccee2017-01-05 15:51:41 -08001901 }
1902 }
1903
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001904 private ApplicationInfo getCallingApplicationInfoOrThrow() {
1905 // We always use the callingUid for this lookup. This means that if hypothetically an
1906 // app was installed in user A with cross user and in user B as an Instant App
1907 // the app in A would be able to see all the settings in user B. However since cross
1908 // user is a system permission and the app must be uninstalled in B and then installed as
1909 // an Instant App that situation is not realistic or supported.
Chad Brubaker97bccee2017-01-05 15:51:41 -08001910 ApplicationInfo ai = null;
1911 try {
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001912 ai = mPackageManager.getApplicationInfo(getCallingPackage(), 0
1913 , UserHandle.getCallingUserId());
Chad Brubaker97bccee2017-01-05 15:51:41 -08001914 } catch (RemoteException ignored) {
1915 }
1916 if (ai == null) {
1917 throw new IllegalStateException("Failed to lookup info for package "
1918 + getCallingPackage());
1919 }
1920 return ai;
1921 }
1922
Xiaohui Chen43765b72015-08-31 10:57:33 -07001923 private PackageInfo getCallingPackageInfoOrThrow(int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001924 try {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001925 PackageInfo packageInfo = mPackageManager.getPackageInfo(
1926 getCallingPackage(), 0, userId);
1927 if (packageInfo != null) {
1928 return packageInfo;
1929 }
Xiaohui Chen43765b72015-08-31 10:57:33 -07001930 } catch (RemoteException e) {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001931 /* ignore */
Svetoslav683914b2015-01-15 14:22:26 -08001932 }
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001933 throw new IllegalStateException("Calling package doesn't exist");
Svetoslav683914b2015-01-15 14:22:26 -08001934 }
1935
1936 private int getGroupParentLocked(int userId) {
1937 // Most frequent use case.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001938 if (userId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08001939 return userId;
1940 }
1941 // We are in the same process with the user manager and the returned
1942 // user info is a cached instance, so just look up instead of cache.
1943 final long identity = Binder.clearCallingIdentity();
1944 try {
Svetoslav7ec28e82015-05-20 17:01:10 -07001945 // Just a lookup and not reentrant, so holding a lock is fine.
Svetoslav683914b2015-01-15 14:22:26 -08001946 UserInfo userInfo = mUserManager.getProfileParent(userId);
1947 return (userInfo != null) ? userInfo.id : userId;
1948 } finally {
1949 Binder.restoreCallingIdentity(identity);
1950 }
1951 }
1952
Svetoslav683914b2015-01-15 14:22:26 -08001953 private void enforceWritePermission(String permission) {
1954 if (getContext().checkCallingOrSelfPermission(permission)
1955 != PackageManager.PERMISSION_GRANTED) {
1956 throw new SecurityException("Permission denial: writing to settings requires:"
1957 + permission);
1958 }
1959 }
1960
1961 /*
1962 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
1963 * This setting contains a list of the currently enabled location providers.
1964 * But helper functions in android.providers.Settings can enable or disable
1965 * a single provider by using a "+" or "-" prefix before the provider name.
1966 *
yuemingw1d13eae2018-01-30 17:27:54 +00001967 * <p>See also {@link com.android.server.pm.UserRestrictionsUtils#isSettingRestrictedForUser()}.
1968 * If DISALLOW_SHARE_LOCATION is set, the said method will only allow values with
1969 * the "-" prefix.
Makoto Onuki28da2e32015-11-20 11:30:44 -08001970 *
Svetoslav683914b2015-01-15 14:22:26 -08001971 * @returns whether the enabled location providers changed.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001972 */
Andreas Gampeb58893072018-09-05 16:52:31 -07001973 @GuardedBy("mLock")
Svetoslav Ganove080da92016-12-21 17:10:35 -08001974 private boolean updateLocationProvidersAllowedLocked(String value, String tag,
1975 int owningUserId, boolean makeDefault, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001976 if (TextUtils.isEmpty(value)) {
1977 return false;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001978 }
Maggie83e03f52018-03-16 12:22:20 -07001979 Setting oldSetting = getSecureSetting(
1980 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, owningUserId);
1981 if (oldSetting == null) {
Svetoslav683914b2015-01-15 14:22:26 -08001982 return false;
1983 }
Maggie83e03f52018-03-16 12:22:20 -07001984 String oldProviders = oldSetting.getValue();
1985 List<String> oldProvidersList = TextUtils.isEmpty(oldProviders)
1986 ? new ArrayList<>() : new ArrayList<>(Arrays.asList(oldProviders.split(",")));
1987 Set<String> newProvidersSet = new ArraySet<>();
1988 newProvidersSet.addAll(oldProvidersList);
Svetoslav683914b2015-01-15 14:22:26 -08001989
Maggie83e03f52018-03-16 12:22:20 -07001990 String[] providerUpdates = value.split(",");
1991 boolean inputError = false;
1992 for (String provider : providerUpdates) {
1993 // do not update location_providers_allowed when input is invalid
1994 if (TextUtils.isEmpty(provider)) {
1995 inputError = true;
1996 break;
Svetoslav683914b2015-01-15 14:22:26 -08001997 }
Maggie83e03f52018-03-16 12:22:20 -07001998 final char prefix = provider.charAt(0);
1999 // do not update location_providers_allowed when input is invalid
2000 if (prefix != '+' && prefix != '-') {
2001 inputError = true;
2002 break;
Svetoslav683914b2015-01-15 14:22:26 -08002003 }
Maggie83e03f52018-03-16 12:22:20 -07002004 // skip prefix
2005 provider = provider.substring(1);
2006 if (prefix == '+') {
2007 newProvidersSet.add(provider);
2008 } else if (prefix == '-') {
2009 newProvidersSet.remove(provider);
Svetoslav683914b2015-01-15 14:22:26 -08002010 }
Maggie83e03f52018-03-16 12:22:20 -07002011 }
2012 String newProviders = TextUtils.join(",", newProvidersSet.toArray());
2013 if (inputError == true || newProviders.equals(oldProviders)) {
Svetoslav683914b2015-01-15 14:22:26 -08002014 // nothing changed, so no need to update the database
Svet Ganov53a441c2016-04-19 19:38:00 -07002015 if (forceNotify) {
Maggie83e03f52018-03-16 12:22:20 -07002016 mSettingsRegistry.notifyForSettingsChange(
2017 makeKey(SETTINGS_TYPE_SECURE, owningUserId),
Svet Ganov53a441c2016-04-19 19:38:00 -07002018 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
2019 }
Svetoslav683914b2015-01-15 14:22:26 -08002020 return false;
2021 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002022 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Maggie83e03f52018-03-16 12:22:20 -07002023 owningUserId, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, newProviders, tag,
2024 makeDefault, getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav683914b2015-01-15 14:22:26 -08002025 }
2026
Svetoslav683914b2015-01-15 14:22:26 -08002027 private static void warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
2028 int targetSdkVersion, String name) {
2029 // If the app targets Lollipop MR1 or older SDK we warn, otherwise crash.
2030 if (targetSdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) {
2031 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
2032 Slog.w(LOG_TAG, "You shouldn't not change private system settings."
2033 + " This will soon become an error.");
2034 } else {
2035 Slog.w(LOG_TAG, "You shouldn't keep your settings in the secure settings."
2036 + " This will soon become an error.");
2037 }
2038 } else {
2039 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
2040 throw new IllegalArgumentException("You cannot change private secure settings.");
2041 } else {
2042 throw new IllegalArgumentException("You cannot keep your settings in"
2043 + " the secure settings.");
2044 }
2045 }
2046 }
2047
2048 private static int resolveCallingUserIdEnforcingPermissionsLocked(int requestingUserId) {
2049 if (requestingUserId == UserHandle.getCallingUserId()) {
2050 return requestingUserId;
2051 }
2052 return ActivityManager.handleIncomingUser(Binder.getCallingPid(),
2053 Binder.getCallingUid(), requestingUserId, false, true,
2054 "get/set setting for user", null);
2055 }
2056
Matt Pape793b15c2019-10-07 13:17:20 -07002057 private Bundle packageValueForCallResult(Setting setting, boolean trackingGeneration) {
Svet Ganov53a441c2016-04-19 19:38:00 -07002058 if (!trackingGeneration) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002059 if (setting == null || setting.isNull()) {
Svet Ganov53a441c2016-04-19 19:38:00 -07002060 return NULL_SETTING_BUNDLE;
2061 }
2062 return Bundle.forPair(Settings.NameValueTable.VALUE, setting.getValue());
Svetoslav683914b2015-01-15 14:22:26 -08002063 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002064 Bundle result = new Bundle();
2065 result.putString(Settings.NameValueTable.VALUE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002066 !setting.isNull() ? setting.getValue() : null);
Svet Ganov96c99462017-05-05 14:27:13 -07002067
Svetoslav Ganove080da92016-12-21 17:10:35 -08002068 mSettingsRegistry.mGenerationRegistry.addGenerationData(result, setting.getKey());
Svet Ganov53a441c2016-04-19 19:38:00 -07002069 return result;
Svetoslav683914b2015-01-15 14:22:26 -08002070 }
2071
Matt Pape793b15c2019-10-07 13:17:20 -07002072 private Bundle packageValuesForCallResult(HashMap<String, String> keyValues,
2073 boolean trackingGeneration) {
2074 Bundle result = new Bundle();
2075 result.putSerializable(Settings.NameValueTable.VALUE, keyValues);
2076 if (trackingGeneration) {
2077 synchronized (mLock) {
2078 mSettingsRegistry.mGenerationRegistry.addGenerationData(result,
2079 mSettingsRegistry.getSettingsLocked(
2080 SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM).mKey);
2081 }
2082 }
2083
2084 return result;
2085 }
2086
Svetoslav683914b2015-01-15 14:22:26 -08002087 private static int getRequestingUserId(Bundle args) {
2088 final int callingUserId = UserHandle.getCallingUserId();
2089 return (args != null) ? args.getInt(Settings.CALL_METHOD_USER_KEY, callingUserId)
2090 : callingUserId;
2091 }
2092
Svet Ganov53a441c2016-04-19 19:38:00 -07002093 private boolean isTrackingGeneration(Bundle args) {
2094 return args != null && args.containsKey(Settings.CALL_METHOD_TRACK_GENERATION_KEY);
2095 }
2096
Svetoslav683914b2015-01-15 14:22:26 -08002097 private static String getSettingValue(Bundle args) {
2098 return (args != null) ? args.getString(Settings.NameValueTable.VALUE) : null;
2099 }
2100
Svetoslav Ganove080da92016-12-21 17:10:35 -08002101 private static String getSettingTag(Bundle args) {
2102 return (args != null) ? args.getString(Settings.CALL_METHOD_TAG_KEY) : null;
2103 }
2104
Matt Pape6bfc62e2018-11-28 13:16:03 -08002105 private static String getSettingPrefix(Bundle args) {
2106 String prefix = (args != null) ? args.getString(Settings.CALL_METHOD_PREFIX_KEY) : null;
2107 // Append '/' to ensure we only match properties with this exact prefix.
2108 // i.e. "foo" should match "foo/property" but not "foobar/property"
2109 return prefix != null ? prefix + "/" : null;
2110 }
2111
Svetoslav Ganove080da92016-12-21 17:10:35 -08002112 private static boolean getSettingMakeDefault(Bundle args) {
2113 return (args != null) && args.getBoolean(Settings.CALL_METHOD_MAKE_DEFAULT_KEY);
2114 }
2115
2116 private static int getResetModeEnforcingPermission(Bundle args) {
2117 final int mode = (args != null) ? args.getInt(Settings.CALL_METHOD_RESET_MODE_KEY) : 0;
2118 switch (mode) {
2119 case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: {
2120 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
2121 throw new SecurityException("Only system, shell/root on a "
2122 + "debuggable build can reset to untrusted defaults");
2123 }
2124 return mode;
2125 }
2126 case Settings.RESET_MODE_UNTRUSTED_CHANGES: {
2127 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
2128 throw new SecurityException("Only system, shell/root on a "
2129 + "debuggable build can reset untrusted changes");
2130 }
2131 return mode;
2132 }
2133 case Settings.RESET_MODE_TRUSTED_DEFAULTS: {
2134 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
2135 throw new SecurityException("Only system, shell/root on a "
2136 + "debuggable build can reset to trusted defaults");
2137 }
2138 return mode;
2139 }
2140 case Settings.RESET_MODE_PACKAGE_DEFAULTS: {
2141 return mode;
2142 }
2143 }
2144 throw new IllegalArgumentException("Invalid reset mode: " + mode);
2145 }
2146
2147 private static boolean isCallerSystemOrShellOrRootOnDebuggableBuild() {
2148 final int appId = UserHandle.getAppId(Binder.getCallingUid());
2149 return appId == SYSTEM_UID || (Build.IS_DEBUGGABLE
2150 && (appId == SHELL_UID || appId == ROOT_UID));
2151 }
2152
Svetoslav683914b2015-01-15 14:22:26 -08002153 private static String getValidTableOrThrow(Uri uri) {
2154 if (uri.getPathSegments().size() > 0) {
2155 String table = uri.getPathSegments().get(0);
2156 if (DatabaseHelper.isValidTable(table)) {
2157 return table;
2158 }
2159 throw new IllegalArgumentException("Bad root path: " + table);
2160 }
2161 throw new IllegalArgumentException("Invalid URI:" + uri);
2162 }
2163
2164 private static MatrixCursor packageSettingForQuery(Setting setting, String[] projection) {
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002165 if (setting.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08002166 return new MatrixCursor(projection, 0);
2167 }
2168 MatrixCursor cursor = new MatrixCursor(projection, 1);
2169 appendSettingToCursor(cursor, setting);
2170 return cursor;
2171 }
2172
2173 private static String[] normalizeProjection(String[] projection) {
2174 if (projection == null) {
2175 return ALL_COLUMNS;
2176 }
2177
2178 final int columnCount = projection.length;
2179 for (int i = 0; i < columnCount; i++) {
2180 String column = projection[i];
2181 if (!ArrayUtils.contains(ALL_COLUMNS, column)) {
2182 throw new IllegalArgumentException("Invalid column: " + column);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002183 }
2184 }
2185
Svetoslav683914b2015-01-15 14:22:26 -08002186 return projection;
2187 }
2188
2189 private static void appendSettingToCursor(MatrixCursor cursor, Setting setting) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002190 if (setting == null || setting.isNull()) {
Suprabh Shuklac9d064a2016-04-12 18:45:34 -07002191 return;
2192 }
Svetoslav683914b2015-01-15 14:22:26 -08002193 final int columnCount = cursor.getColumnCount();
2194
2195 String[] values = new String[columnCount];
2196
2197 for (int i = 0; i < columnCount; i++) {
2198 String column = cursor.getColumnName(i);
2199
2200 switch (column) {
2201 case Settings.NameValueTable._ID: {
2202 values[i] = setting.getId();
2203 } break;
2204
2205 case Settings.NameValueTable.NAME: {
2206 values[i] = setting.getName();
2207 } break;
2208
2209 case Settings.NameValueTable.VALUE: {
2210 values[i] = setting.getValue();
2211 } break;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002212 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002213 }
2214
Svetoslav683914b2015-01-15 14:22:26 -08002215 cursor.addRow(values);
2216 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002217
Makoto Onuki3a2c35782015-06-18 11:21:58 -07002218 private static boolean isKeyValid(String key) {
2219 return !(TextUtils.isEmpty(key) || SettingsState.isBinary(key));
2220 }
2221
Matt Pape7b1c6cd2019-01-04 08:10:41 -08002222 private String resolveCallingPackage() {
2223 switch (Binder.getCallingUid()) {
2224 case Process.ROOT_UID: {
2225 return "root";
2226 }
2227
2228 case Process.SHELL_UID: {
2229 return "com.android.shell";
2230 }
2231
2232 default: {
2233 return getCallingPackage();
2234 }
2235 }
2236 }
2237
Svetoslav683914b2015-01-15 14:22:26 -08002238 private static final class Arguments {
2239 private static final Pattern WHERE_PATTERN_WITH_PARAM_NO_BRACKETS =
2240 Pattern.compile("[\\s]*name[\\s]*=[\\s]*\\?[\\s]*");
2241
2242 private static final Pattern WHERE_PATTERN_WITH_PARAM_IN_BRACKETS =
2243 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*\\?[\\s]*\\)[\\s]*");
2244
2245 private static final Pattern WHERE_PATTERN_NO_PARAM_IN_BRACKETS =
2246 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*\\)[\\s]*");
2247
2248 private static final Pattern WHERE_PATTERN_NO_PARAM_NO_BRACKETS =
2249 Pattern.compile("[\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*");
2250
2251 public final String table;
2252 public final String name;
2253
2254 public Arguments(Uri uri, String where, String[] whereArgs, boolean supportAll) {
2255 final int segmentSize = uri.getPathSegments().size();
2256 switch (segmentSize) {
2257 case 1: {
2258 if (where != null
2259 && (WHERE_PATTERN_WITH_PARAM_NO_BRACKETS.matcher(where).matches()
2260 || WHERE_PATTERN_WITH_PARAM_IN_BRACKETS.matcher(where).matches())
2261 && whereArgs.length == 1) {
2262 name = whereArgs[0];
2263 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08002264 return;
Svetoslav683914b2015-01-15 14:22:26 -08002265 } else if (where != null
2266 && (WHERE_PATTERN_NO_PARAM_NO_BRACKETS.matcher(where).matches()
2267 || WHERE_PATTERN_NO_PARAM_IN_BRACKETS.matcher(where).matches())) {
2268 final int startIndex = Math.max(where.indexOf("'"),
2269 where.indexOf("\"")) + 1;
2270 final int endIndex = Math.max(where.lastIndexOf("'"),
2271 where.lastIndexOf("\""));
2272 name = where.substring(startIndex, endIndex);
2273 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08002274 return;
Svetoslav683914b2015-01-15 14:22:26 -08002275 } else if (supportAll && where == null && whereArgs == null) {
2276 name = null;
2277 table = computeTableForSetting(uri, null);
Svetoslav28494652015-02-12 14:11:42 -08002278 return;
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002279 }
Svetoslav683914b2015-01-15 14:22:26 -08002280 } break;
2281
Svetoslav28494652015-02-12 14:11:42 -08002282 case 2: {
2283 if (where == null && whereArgs == null) {
2284 name = uri.getPathSegments().get(1);
2285 table = computeTableForSetting(uri, name);
2286 return;
2287 }
2288 } break;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002289 }
Svetoslav28494652015-02-12 14:11:42 -08002290
2291 EventLogTags.writeUnsupportedSettingsQuery(
2292 uri.toSafeString(), where, Arrays.toString(whereArgs));
2293 String message = String.format( "Supported SQL:\n"
2294 + " uri content://some_table/some_property with null where and where args\n"
2295 + " uri content://some_table with query name=? and single name as arg\n"
2296 + " uri content://some_table with query name=some_name and null args\n"
2297 + " but got - uri:%1s, where:%2s whereArgs:%3s", uri, where,
2298 Arrays.toString(whereArgs));
2299 throw new IllegalArgumentException(message);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002300 }
2301
Svetoslav28494652015-02-12 14:11:42 -08002302 private static String computeTableForSetting(Uri uri, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08002303 String table = getValidTableOrThrow(uri);
2304
2305 if (name != null) {
2306 if (sSystemMovedToSecureSettings.contains(name)) {
2307 table = TABLE_SECURE;
2308 }
2309
2310 if (sSystemMovedToGlobalSettings.contains(name)) {
2311 table = TABLE_GLOBAL;
2312 }
2313
2314 if (sSecureMovedToGlobalSettings.contains(name)) {
2315 table = TABLE_GLOBAL;
2316 }
2317
2318 if (sGlobalMovedToSecureSettings.contains(name)) {
2319 table = TABLE_SECURE;
2320 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002321 }
Svetoslav683914b2015-01-15 14:22:26 -08002322
2323 return table;
2324 }
2325 }
2326
2327 final class SettingsRegistry {
2328 private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid";
2329
Svetoslav683914b2015-01-15 14:22:26 -08002330 private static final String SETTINGS_FILE_GLOBAL = "settings_global.xml";
2331 private static final String SETTINGS_FILE_SYSTEM = "settings_system.xml";
2332 private static final String SETTINGS_FILE_SECURE = "settings_secure.xml";
Mark Rathjend891f012017-01-19 04:10:37 +00002333 private static final String SETTINGS_FILE_SSAID = "settings_ssaid.xml";
Matt Pape1b31a332018-10-17 09:58:28 -07002334 private static final String SETTINGS_FILE_CONFIG = "settings_config.xml";
Mark Rathjend891f012017-01-19 04:10:37 +00002335
2336 private static final String SSAID_USER_KEY = "userkey";
Svetoslav683914b2015-01-15 14:22:26 -08002337
2338 private final SparseArray<SettingsState> mSettingsStates = new SparseArray<>();
2339
Svet Ganov53a441c2016-04-19 19:38:00 -07002340 private GenerationRegistry mGenerationRegistry;
Svetoslav683914b2015-01-15 14:22:26 -08002341
Svetoslav7e0683b2015-08-03 16:02:52 -07002342 private final Handler mHandler;
2343
Svet Ganov53a441c2016-04-19 19:38:00 -07002344 private final BackupManager mBackupManager;
2345
Amith Yamasani39452022017-03-21 15:23:47 -07002346 private String mSettingsCreationBuildId;
2347
Svetoslav683914b2015-01-15 14:22:26 -08002348 public SettingsRegistry() {
Svetoslav7e0683b2015-08-03 16:02:52 -07002349 mHandler = new MyHandler(getContext().getMainLooper());
Svet Ganov53a441c2016-04-19 19:38:00 -07002350 mGenerationRegistry = new GenerationRegistry(mLock);
2351 mBackupManager = new BackupManager(getContext());
Svetoslav683914b2015-01-15 14:22:26 -08002352 migrateAllLegacySettingsIfNeeded();
Mark Rathjend891f012017-01-19 04:10:37 +00002353 syncSsaidTableOnStart();
2354 }
2355
2356 private void generateUserKeyLocked(int userId) {
2357 // Generate a random key for each user used for creating a new ssaid.
Mark Rathjen7599f132017-01-23 14:15:54 -08002358 final byte[] keyBytes = new byte[32];
Mark Rathjend891f012017-01-19 04:10:37 +00002359 final SecureRandom rand = new SecureRandom();
2360 rand.nextBytes(keyBytes);
2361
2362 // Convert to string for storage in settings table.
Neil Fuller3447fb42019-04-08 22:03:14 +01002363 final String userKey = HexEncoding.encodeToString(keyBytes, true /* upperCase */);
Mark Rathjend891f012017-01-19 04:10:37 +00002364
2365 // Store the key in the ssaid table.
2366 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
2367 final boolean success = ssaidSettings.insertSettingLocked(SSAID_USER_KEY, userKey, null,
2368 true, SettingsState.SYSTEM_PACKAGE_NAME);
2369
2370 if (!success) {
2371 throw new IllegalStateException("Ssaid settings not accessible");
2372 }
2373 }
2374
Mark Rathjen7599f132017-01-23 14:15:54 -08002375 private byte[] getLengthPrefix(byte[] data) {
2376 return ByteBuffer.allocate(4).putInt(data.length).array();
2377 }
2378
Christopher Tateb218e762017-04-05 16:34:07 -07002379 public Setting generateSsaidLocked(PackageInfo callingPkg, int userId) {
Mark Rathjend891f012017-01-19 04:10:37 +00002380 // Read the user's key from the ssaid table.
2381 Setting userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY);
Mark Rathjenea617592017-01-18 23:03:41 -08002382 if (userKeySetting == null || userKeySetting.isNull()
2383 || userKeySetting.getValue() == null) {
Mark Rathjend891f012017-01-19 04:10:37 +00002384 // Lazy initialize and store the user key.
2385 generateUserKeyLocked(userId);
2386 userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY);
Mark Rathjenea617592017-01-18 23:03:41 -08002387 if (userKeySetting == null || userKeySetting.isNull()
2388 || userKeySetting.getValue() == null) {
Mark Rathjend891f012017-01-19 04:10:37 +00002389 throw new IllegalStateException("User key not accessible");
2390 }
2391 }
2392 final String userKey = userKeySetting.getValue();
Neil Fuller3447fb42019-04-08 22:03:14 +01002393 if (userKey == null || userKey.length() % 2 != 0) {
2394 throw new IllegalStateException("User key invalid");
2395 }
Mark Rathjend891f012017-01-19 04:10:37 +00002396
2397 // Convert the user's key back to a byte array.
Neil Fuller3447fb42019-04-08 22:03:14 +01002398 final byte[] keyBytes = HexEncoding.decode(userKey);
Mark Rathjen7599f132017-01-23 14:15:54 -08002399
2400 // Validate that the key is of expected length.
2401 // Keys are currently 32 bytes, but were once 16 bytes during Android O development.
Neil Fuller3447fb42019-04-08 22:03:14 +01002402 if (keyBytes.length != 16 && keyBytes.length != 32) {
Mark Rathjend891f012017-01-19 04:10:37 +00002403 throw new IllegalStateException("User key invalid");
2404 }
2405
Mark Rathjen7599f132017-01-23 14:15:54 -08002406 final Mac m;
Mark Rathjend891f012017-01-19 04:10:37 +00002407 try {
Mark Rathjen7599f132017-01-23 14:15:54 -08002408 m = Mac.getInstance("HmacSHA256");
2409 m.init(new SecretKeySpec(keyBytes, m.getAlgorithm()));
Mark Rathjend891f012017-01-19 04:10:37 +00002410 } catch (NoSuchAlgorithmException e) {
Mark Rathjen7599f132017-01-23 14:15:54 -08002411 throw new IllegalStateException("HmacSHA256 is not available", e);
2412 } catch (InvalidKeyException e) {
2413 throw new IllegalStateException("Key is corrupted", e);
Mark Rathjend891f012017-01-19 04:10:37 +00002414 }
Mark Rathjen7599f132017-01-23 14:15:54 -08002415
Mark Rathjenf42dd912017-06-05 19:04:34 -07002416 // Mac each of the developer signatures.
Christopher Tateb218e762017-04-05 16:34:07 -07002417 for (int i = 0; i < callingPkg.signatures.length; i++) {
2418 byte[] sig = callingPkg.signatures[i].toByteArray();
Mark Rathjen7599f132017-01-23 14:15:54 -08002419 m.update(getLengthPrefix(sig), 0, 4);
2420 m.update(sig);
2421 }
Mark Rathjend891f012017-01-19 04:10:37 +00002422
2423 // Convert result to a string for storage in settings table. Only want first 64 bits.
Neil Fuller3447fb42019-04-08 22:03:14 +01002424 final String ssaid = HexEncoding.encodeToString(m.doFinal(), false /* upperCase */)
2425 .substring(0, 16);
Mark Rathjend891f012017-01-19 04:10:37 +00002426
2427 // Save the ssaid in the ssaid table.
Christopher Tateb218e762017-04-05 16:34:07 -07002428 final String uid = Integer.toString(callingPkg.applicationInfo.uid);
Mark Rathjend891f012017-01-19 04:10:37 +00002429 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
2430 final boolean success = ssaidSettings.insertSettingLocked(uid, ssaid, null, true,
Mark Rathjenf42dd912017-06-05 19:04:34 -07002431 callingPkg.packageName);
Mark Rathjend891f012017-01-19 04:10:37 +00002432
2433 if (!success) {
2434 throw new IllegalStateException("Ssaid settings not accessible");
2435 }
2436
2437 return getSettingLocked(SETTINGS_TYPE_SSAID, userId, uid);
2438 }
2439
2440 public void syncSsaidTableOnStart() {
2441 synchronized (mLock) {
2442 // Verify that each user's packages and ssaid's are in sync.
2443 for (UserInfo user : mUserManager.getUsers(true)) {
2444 // Get all uids for the user's packages.
2445 final List<PackageInfo> packages;
2446 try {
Tetsutoki Shiozawaebe0e5f2018-01-17 11:07:09 +09002447 packages = mPackageManager.getInstalledPackages(
2448 PackageManager.MATCH_UNINSTALLED_PACKAGES,
2449 user.id).getList();
Mark Rathjend891f012017-01-19 04:10:37 +00002450 } catch (RemoteException e) {
2451 throw new IllegalStateException("Package manager not available");
2452 }
2453 final Set<String> appUids = new HashSet<>();
2454 for (PackageInfo info : packages) {
2455 appUids.add(Integer.toString(info.applicationInfo.uid));
2456 }
2457
2458 // Get all uids currently stored in the user's ssaid table.
2459 final Set<String> ssaidUids = new HashSet<>(
2460 getSettingsNamesLocked(SETTINGS_TYPE_SSAID, user.id));
2461 ssaidUids.remove(SSAID_USER_KEY);
2462
2463 // Perform a set difference for the appUids and ssaidUids.
2464 ssaidUids.removeAll(appUids);
2465
2466 // If there are ssaidUids left over they need to be removed from the table.
2467 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID,
2468 user.id);
2469 for (String uid : ssaidUids) {
2470 ssaidSettings.deleteSettingLocked(uid);
2471 }
2472 }
2473 }
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002474 }
2475
Svetoslav683914b2015-01-15 14:22:26 -08002476 public List<String> getSettingsNamesLocked(int type, int userId) {
2477 final int key = makeKey(type, userId);
2478 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002479 if (settingsState == null) {
2480 return new ArrayList<String>();
2481 }
Svetoslav683914b2015-01-15 14:22:26 -08002482 return settingsState.getSettingNamesLocked();
2483 }
2484
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002485 public SparseBooleanArray getKnownUsersLocked() {
2486 SparseBooleanArray users = new SparseBooleanArray();
2487 for (int i = mSettingsStates.size()-1; i >= 0; i--) {
2488 users.put(getUserIdFromKey(mSettingsStates.keyAt(i)), true);
2489 }
2490 return users;
2491 }
2492
Kweku Adamsb0886f32017-10-31 15:32:09 -07002493 @Nullable
Svetoslav683914b2015-01-15 14:22:26 -08002494 public SettingsState getSettingsLocked(int type, int userId) {
2495 final int key = makeKey(type, userId);
2496 return peekSettingsStateLocked(key);
2497 }
2498
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002499 public boolean ensureSettingsForUserLocked(int userId) {
2500 // First make sure this user actually exists.
2501 if (mUserManager.getUserInfo(userId) == null) {
2502 Slog.wtf(LOG_TAG, "Requested user " + userId + " does not exist");
2503 return false;
2504 }
2505
Svetoslav683914b2015-01-15 14:22:26 -08002506 // Migrate the setting for this user if needed.
2507 migrateLegacySettingsForUserIfNeededLocked(userId);
2508
Matt Pape1b31a332018-10-17 09:58:28 -07002509 // Ensure config settings loaded if owner.
2510 if (userId == UserHandle.USER_SYSTEM) {
2511 final int configKey
2512 = makeKey(SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM);
2513 ensureSettingsStateLocked(configKey);
2514 }
2515
Svetoslav683914b2015-01-15 14:22:26 -08002516 // Ensure global settings loaded if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07002517 if (userId == UserHandle.USER_SYSTEM) {
2518 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002519 ensureSettingsStateLocked(globalKey);
2520 }
2521
2522 // Ensure secure settings loaded.
2523 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2524 ensureSettingsStateLocked(secureKey);
2525
2526 // Make sure the secure settings have an Android id set.
2527 SettingsState secureSettings = getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
2528 ensureSecureSettingAndroidIdSetLocked(secureSettings);
2529
2530 // Ensure system settings loaded.
2531 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2532 ensureSettingsStateLocked(systemKey);
2533
Mark Rathjend891f012017-01-19 04:10:37 +00002534 // Ensure secure settings loaded.
2535 final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId);
2536 ensureSettingsStateLocked(ssaidKey);
2537
Svetoslav683914b2015-01-15 14:22:26 -08002538 // Upgrade the settings to the latest version.
2539 UpgradeController upgrader = new UpgradeController(userId);
2540 upgrader.upgradeIfNeededLocked();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002541 return true;
Svetoslav683914b2015-01-15 14:22:26 -08002542 }
2543
2544 private void ensureSettingsStateLocked(int key) {
2545 if (mSettingsStates.get(key) == null) {
2546 final int maxBytesPerPackage = getMaxBytesPerPackageForType(getTypeFromKey(key));
Svetoslav Ganove080da92016-12-21 17:10:35 -08002547 SettingsState settingsState = new SettingsState(getContext(), mLock,
2548 getSettingsFile(key), key, maxBytesPerPackage, mHandlerThread.getLooper());
Svetoslav683914b2015-01-15 14:22:26 -08002549 mSettingsStates.put(key, settingsState);
2550 }
2551 }
2552
2553 public void removeUserStateLocked(int userId, boolean permanently) {
2554 // We always keep the global settings in memory.
2555
2556 // Nuke system settings.
2557 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2558 final SettingsState systemSettingsState = mSettingsStates.get(systemKey);
2559 if (systemSettingsState != null) {
2560 if (permanently) {
2561 mSettingsStates.remove(systemKey);
2562 systemSettingsState.destroyLocked(null);
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002563 } else {
Svetoslav683914b2015-01-15 14:22:26 -08002564 systemSettingsState.destroyLocked(new Runnable() {
2565 @Override
2566 public void run() {
2567 mSettingsStates.remove(systemKey);
2568 }
2569 });
2570 }
2571 }
2572
2573 // Nuke secure settings.
2574 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2575 final SettingsState secureSettingsState = mSettingsStates.get(secureKey);
2576 if (secureSettingsState != null) {
2577 if (permanently) {
2578 mSettingsStates.remove(secureKey);
2579 secureSettingsState.destroyLocked(null);
2580 } else {
2581 secureSettingsState.destroyLocked(new Runnable() {
2582 @Override
2583 public void run() {
2584 mSettingsStates.remove(secureKey);
2585 }
2586 });
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002587 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002588 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002589
Mark Rathjend891f012017-01-19 04:10:37 +00002590 // Nuke ssaid settings.
2591 final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId);
2592 final SettingsState ssaidSettingsState = mSettingsStates.get(ssaidKey);
2593 if (ssaidSettingsState != null) {
2594 if (permanently) {
2595 mSettingsStates.remove(ssaidKey);
2596 ssaidSettingsState.destroyLocked(null);
2597 } else {
2598 ssaidSettingsState.destroyLocked(new Runnable() {
2599 @Override
2600 public void run() {
2601 mSettingsStates.remove(ssaidKey);
2602 }
2603 });
2604 }
2605 }
2606
Svet Ganov53a441c2016-04-19 19:38:00 -07002607 // Nuke generation tracking data
2608 mGenerationRegistry.onUserRemoved(userId);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002609 }
2610
Svetoslav683914b2015-01-15 14:22:26 -08002611 public boolean insertSettingLocked(int type, int userId, String name, String value,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002612 String tag, boolean makeDefault, String packageName, boolean forceNotify,
2613 Set<String> criticalSettings) {
Matt Papec1323dc2018-12-11 12:32:42 -08002614 return insertSettingLocked(type, userId, name, value, tag, makeDefault, false,
2615 packageName, forceNotify, criticalSettings);
2616 }
2617
2618 public boolean insertSettingLocked(int type, int userId, String name, String value,
2619 String tag, boolean makeDefault, boolean forceNonSystemPackage, String packageName,
2620 boolean forceNotify, Set<String> criticalSettings) {
Svetoslav683914b2015-01-15 14:22:26 -08002621 final int key = makeKey(type, userId);
2622
Svetoslav Ganove080da92016-12-21 17:10:35 -08002623 boolean success = false;
Svetoslav683914b2015-01-15 14:22:26 -08002624 SettingsState settingsState = peekSettingsStateLocked(key);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002625 if (settingsState != null) {
2626 success = settingsState.insertSettingLocked(name, value,
Matt Papec1323dc2018-12-11 12:32:42 -08002627 tag, makeDefault, forceNonSystemPackage, packageName);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002628 }
Svetoslav683914b2015-01-15 14:22:26 -08002629
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002630 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2631 settingsState.persistSyncLocked();
2632 }
2633
Svet Ganov53a441c2016-04-19 19:38:00 -07002634 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08002635 notifyForSettingsChange(key, name);
2636 }
2637 return success;
2638 }
2639
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002640 public boolean deleteSettingLocked(int type, int userId, String name, boolean forceNotify,
2641 Set<String> criticalSettings) {
Svetoslav683914b2015-01-15 14:22:26 -08002642 final int key = makeKey(type, userId);
2643
Svetoslav Ganove080da92016-12-21 17:10:35 -08002644 boolean success = false;
Svetoslav683914b2015-01-15 14:22:26 -08002645 SettingsState settingsState = peekSettingsStateLocked(key);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002646 if (settingsState != null) {
2647 success = settingsState.deleteSettingLocked(name);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002648 }
Svetoslav683914b2015-01-15 14:22:26 -08002649
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002650 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2651 settingsState.persistSyncLocked();
2652 }
2653
Svet Ganov53a441c2016-04-19 19:38:00 -07002654 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08002655 notifyForSettingsChange(key, name);
2656 }
2657 return success;
2658 }
2659
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002660 public boolean updateSettingLocked(int type, int userId, String name, String value,
2661 String tag, boolean makeDefault, String packageName, boolean forceNotify,
2662 Set<String> criticalSettings) {
2663 final int key = makeKey(type, userId);
2664
2665 boolean success = false;
2666 SettingsState settingsState = peekSettingsStateLocked(key);
2667 if (settingsState != null) {
2668 success = settingsState.updateSettingLocked(name, value, tag,
2669 makeDefault, packageName);
2670 }
2671
2672 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2673 settingsState.persistSyncLocked();
2674 }
2675
2676 if (forceNotify || success) {
2677 notifyForSettingsChange(key, name);
2678 }
2679
2680 return success;
2681 }
2682
Svetoslav683914b2015-01-15 14:22:26 -08002683 public Setting getSettingLocked(int type, int userId, String name) {
2684 final int key = makeKey(type, userId);
2685
2686 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002687 if (settingsState == null) {
Mark Rathjenea617592017-01-18 23:03:41 -08002688 return null;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002689 }
Mark Rathjend891f012017-01-19 04:10:37 +00002690
2691 // getSettingLocked will return non-null result
Svetoslav683914b2015-01-15 14:22:26 -08002692 return settingsState.getSettingLocked(name);
2693 }
2694
Svetoslav Ganove080da92016-12-21 17:10:35 -08002695 public void resetSettingsLocked(int type, int userId, String packageName, int mode,
2696 String tag) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002697 resetSettingsLocked(type, userId, packageName, mode, tag, null);
2698 }
2699
2700 public void resetSettingsLocked(int type, int userId, String packageName, int mode,
2701 String tag, @Nullable String prefix) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08002702 final int key = makeKey(type, userId);
2703 SettingsState settingsState = peekSettingsStateLocked(key);
2704 if (settingsState == null) {
2705 return;
2706 }
2707
2708 switch (mode) {
2709 case Settings.RESET_MODE_PACKAGE_DEFAULTS: {
2710 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002711 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002712 Setting setting = settingsState.getSettingLocked(name);
2713 if (packageName.equals(setting.getPackageName())) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002714 if ((tag != null && !tag.equals(setting.getTag()))
2715 || (prefix != null && !setting.getName().startsWith(prefix))) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08002716 continue;
2717 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002718 if (settingsState.resetSettingLocked(name)) {
2719 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002720 notifyForSettingsChange(key, name);
2721 }
2722 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002723 if (someSettingChanged) {
2724 settingsState.persistSyncLocked();
2725 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002726 }
2727 } break;
2728
2729 case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: {
2730 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002731 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002732 Setting setting = settingsState.getSettingLocked(name);
2733 if (!SettingsState.isSystemPackage(getContext(),
Matt Pape25c940d2019-03-26 12:14:33 -07002734 setting.getPackageName(), INVALID_UID, userId)) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002735 if (prefix != null && !setting.getName().startsWith(prefix)) {
2736 continue;
2737 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002738 if (settingsState.resetSettingLocked(name)) {
2739 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002740 notifyForSettingsChange(key, name);
2741 }
2742 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002743 if (someSettingChanged) {
2744 settingsState.persistSyncLocked();
2745 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002746 }
2747 } break;
2748
2749 case Settings.RESET_MODE_UNTRUSTED_CHANGES: {
2750 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002751 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002752 Setting setting = settingsState.getSettingLocked(name);
2753 if (!SettingsState.isSystemPackage(getContext(),
Matt Pape25c940d2019-03-26 12:14:33 -07002754 setting.getPackageName(), INVALID_UID, userId)) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002755 if (prefix != null && !setting.getName().startsWith(prefix)) {
2756 continue;
2757 }
Eugene Suslad72c3972016-12-27 15:49:30 -08002758 if (setting.isDefaultFromSystem()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002759 if (settingsState.resetSettingLocked(name)) {
2760 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002761 notifyForSettingsChange(key, name);
2762 }
2763 } else if (settingsState.deleteSettingLocked(name)) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002764 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002765 notifyForSettingsChange(key, name);
2766 }
2767 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002768 if (someSettingChanged) {
2769 settingsState.persistSyncLocked();
2770 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002771 }
2772 } break;
2773
2774 case Settings.RESET_MODE_TRUSTED_DEFAULTS: {
2775 for (String name : settingsState.getSettingNamesLocked()) {
2776 Setting setting = settingsState.getSettingLocked(name);
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002777 boolean someSettingChanged = false;
Matt Pape6bfc62e2018-11-28 13:16:03 -08002778 if (prefix != null && !setting.getName().startsWith(prefix)) {
2779 continue;
2780 }
Eugene Suslad72c3972016-12-27 15:49:30 -08002781 if (setting.isDefaultFromSystem()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002782 if (settingsState.resetSettingLocked(name)) {
2783 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002784 notifyForSettingsChange(key, name);
2785 }
2786 } else if (settingsState.deleteSettingLocked(name)) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002787 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002788 notifyForSettingsChange(key, name);
2789 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002790 if (someSettingChanged) {
2791 settingsState.persistSyncLocked();
2792 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002793 }
2794 } break;
2795 }
2796 }
2797
Zimuzoc56192c2018-07-25 10:40:01 +01002798 public void removeSettingsForPackageLocked(String packageName, int userId) {
Svet Ganov8de34802015-04-27 09:33:40 -07002799 // Global and secure settings are signature protected. Apps signed
2800 // by the platform certificate are generally not uninstalled and
2801 // the main exception is tests. We trust components signed
2802 // by the platform certificate and do not do a clean up after them.
Svetoslav683914b2015-01-15 14:22:26 -08002803
2804 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2805 SettingsState systemSettings = mSettingsStates.get(systemKey);
Svet Ganov8de34802015-04-27 09:33:40 -07002806 if (systemSettings != null) {
Zimuzoc56192c2018-07-25 10:40:01 +01002807 systemSettings.removeSettingsForPackageLocked(packageName);
Svet Ganov8de34802015-04-27 09:33:40 -07002808 }
Svetoslav683914b2015-01-15 14:22:26 -08002809 }
2810
Mark Rathjend891f012017-01-19 04:10:37 +00002811 public void onUidRemovedLocked(int uid) {
2812 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID,
2813 UserHandle.getUserId(uid));
Tony Mak24c6ab42018-01-08 14:35:34 +00002814 if (ssaidSettings != null) {
2815 ssaidSettings.deleteSettingLocked(Integer.toString(uid));
2816 }
Mark Rathjend891f012017-01-19 04:10:37 +00002817 }
2818
Kweku Adamsb0886f32017-10-31 15:32:09 -07002819 @Nullable
Svetoslav683914b2015-01-15 14:22:26 -08002820 private SettingsState peekSettingsStateLocked(int key) {
2821 SettingsState settingsState = mSettingsStates.get(key);
2822 if (settingsState != null) {
2823 return settingsState;
2824 }
2825
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002826 if (!ensureSettingsForUserLocked(getUserIdFromKey(key))) {
2827 return null;
2828 }
Svetoslav683914b2015-01-15 14:22:26 -08002829 return mSettingsStates.get(key);
2830 }
2831
2832 private void migrateAllLegacySettingsIfNeeded() {
2833 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07002834 final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002835 File globalFile = getSettingsFile(key);
Amith Yamasani74bf71b2017-07-19 16:49:52 -07002836 if (SettingsState.stateFileExists(globalFile)) {
Svetoslav683914b2015-01-15 14:22:26 -08002837 return;
2838 }
2839
Amith Yamasani39452022017-03-21 15:23:47 -07002840 mSettingsCreationBuildId = Build.ID;
2841
Svetoslav683914b2015-01-15 14:22:26 -08002842 final long identity = Binder.clearCallingIdentity();
2843 try {
2844 List<UserInfo> users = mUserManager.getUsers(true);
2845
2846 final int userCount = users.size();
2847 for (int i = 0; i < userCount; i++) {
2848 final int userId = users.get(i).id;
2849
2850 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
2851 SQLiteDatabase database = dbHelper.getWritableDatabase();
2852 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
2853
2854 // Upgrade to the latest version.
2855 UpgradeController upgrader = new UpgradeController(userId);
2856 upgrader.upgradeIfNeededLocked();
2857
2858 // Drop from memory if not a running user.
2859 if (!mUserManager.isUserRunning(new UserHandle(userId))) {
2860 removeUserStateLocked(userId, false);
2861 }
2862 }
2863 } finally {
2864 Binder.restoreCallingIdentity(identity);
2865 }
2866 }
2867 }
2868
2869 private void migrateLegacySettingsForUserIfNeededLocked(int userId) {
2870 // Every user has secure settings and if no file we need to migrate.
2871 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2872 File secureFile = getSettingsFile(secureKey);
Amith Yamasani74bf71b2017-07-19 16:49:52 -07002873 if (SettingsState.stateFileExists(secureFile)) {
Svetoslav683914b2015-01-15 14:22:26 -08002874 return;
2875 }
2876
2877 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
2878 SQLiteDatabase database = dbHelper.getWritableDatabase();
2879
2880 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
2881 }
2882
2883 private void migrateLegacySettingsForUserLocked(DatabaseHelper dbHelper,
2884 SQLiteDatabase database, int userId) {
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002885 // Move over the system settings.
2886 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2887 ensureSettingsStateLocked(systemKey);
2888 SettingsState systemSettings = mSettingsStates.get(systemKey);
2889 migrateLegacySettingsLocked(systemSettings, database, TABLE_SYSTEM);
2890 systemSettings.persistSyncLocked();
Svetoslav683914b2015-01-15 14:22:26 -08002891
2892 // Move over the secure settings.
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002893 // Do this after System settings, since this is the first thing we check when deciding
2894 // to skip over migration from db to xml for a secondary user.
Svetoslav683914b2015-01-15 14:22:26 -08002895 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2896 ensureSettingsStateLocked(secureKey);
2897 SettingsState secureSettings = mSettingsStates.get(secureKey);
2898 migrateLegacySettingsLocked(secureSettings, database, TABLE_SECURE);
2899 ensureSecureSettingAndroidIdSetLocked(secureSettings);
2900 secureSettings.persistSyncLocked();
2901
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002902 // Move over the global settings if owner.
2903 // Do this last, since this is the first thing we check when deciding
2904 // to skip over migration from db to xml for owner user.
2905 if (userId == UserHandle.USER_SYSTEM) {
2906 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, userId);
2907 ensureSettingsStateLocked(globalKey);
2908 SettingsState globalSettings = mSettingsStates.get(globalKey);
2909 migrateLegacySettingsLocked(globalSettings, database, TABLE_GLOBAL);
Amith Yamasani39452022017-03-21 15:23:47 -07002910 // If this was just created
2911 if (mSettingsCreationBuildId != null) {
2912 globalSettings.insertSettingLocked(Settings.Global.DATABASE_CREATION_BUILDID,
2913 mSettingsCreationBuildId, null, true,
2914 SettingsState.SYSTEM_PACKAGE_NAME);
2915 }
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002916 globalSettings.persistSyncLocked();
2917 }
Svetoslav683914b2015-01-15 14:22:26 -08002918
2919 // Drop the database as now all is moved and persisted.
2920 if (DROP_DATABASE_ON_MIGRATION) {
2921 dbHelper.dropDatabase();
2922 } else {
2923 dbHelper.backupDatabase();
2924 }
2925 }
2926
2927 private void migrateLegacySettingsLocked(SettingsState settingsState,
2928 SQLiteDatabase database, String table) {
2929 SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
2930 queryBuilder.setTables(table);
2931
2932 Cursor cursor = queryBuilder.query(database, ALL_COLUMNS,
2933 null, null, null, null, null);
2934
2935 if (cursor == null) {
2936 return;
2937 }
2938
2939 try {
2940 if (!cursor.moveToFirst()) {
2941 return;
2942 }
2943
2944 final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME);
2945 final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE);
2946
2947 settingsState.setVersionLocked(database.getVersion());
2948
2949 while (!cursor.isAfterLast()) {
2950 String name = cursor.getString(nameColumnIdx);
2951 String value = cursor.getString(valueColumnIdx);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002952 settingsState.insertSettingLocked(name, value, null, true,
Svetoslav683914b2015-01-15 14:22:26 -08002953 SettingsState.SYSTEM_PACKAGE_NAME);
2954 cursor.moveToNext();
2955 }
2956 } finally {
2957 cursor.close();
2958 }
2959 }
2960
Andreas Gampeb58893072018-09-05 16:52:31 -07002961 @GuardedBy("secureSettings.mLock")
Svetoslav683914b2015-01-15 14:22:26 -08002962 private void ensureSecureSettingAndroidIdSetLocked(SettingsState secureSettings) {
2963 Setting value = secureSettings.getSettingLocked(Settings.Secure.ANDROID_ID);
2964
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09002965 if (!value.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08002966 return;
2967 }
2968
2969 final int userId = getUserIdFromKey(secureSettings.mKey);
2970
2971 final UserInfo user;
2972 final long identity = Binder.clearCallingIdentity();
2973 try {
2974 user = mUserManager.getUserInfo(userId);
2975 } finally {
2976 Binder.restoreCallingIdentity(identity);
2977 }
2978 if (user == null) {
2979 // Can happen due to races when deleting users - treat as benign.
2980 return;
2981 }
2982
2983 String androidId = Long.toHexString(new SecureRandom().nextLong());
2984 secureSettings.insertSettingLocked(Settings.Secure.ANDROID_ID, androidId,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002985 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Svetoslav683914b2015-01-15 14:22:26 -08002986
2987 Slog.d(LOG_TAG, "Generated and saved new ANDROID_ID [" + androidId
2988 + "] for user " + userId);
2989
2990 // Write a drop box entry if it's a restricted profile
2991 if (user.isRestricted()) {
2992 DropBoxManager dbm = (DropBoxManager) getContext().getSystemService(
2993 Context.DROPBOX_SERVICE);
2994 if (dbm != null && dbm.isTagEnabled(DROPBOX_TAG_USERLOG)) {
2995 dbm.addText(DROPBOX_TAG_USERLOG, System.currentTimeMillis()
2996 + "," + DROPBOX_TAG_USERLOG + "," + androidId + "\n");
2997 }
2998 }
2999 }
3000
3001 private void notifyForSettingsChange(int key, String name) {
Svet Ganov945864c2018-03-22 21:49:10 -07003002 // Increment the generation first, so observers always see the new value
Phil Weaver83fec002016-05-11 10:55:29 -07003003 mGenerationRegistry.incrementGeneration(key);
3004
Linus Tufvesson93c38552019-05-30 12:47:39 +01003005 if (isGlobalSettingsKey(key) || isConfigSettingsKey(key)) {
Jeff Sharkey308093f2018-03-25 22:53:29 -06003006 final long token = Binder.clearCallingIdentity();
3007 try {
Linus Tufvesson93c38552019-05-30 12:47:39 +01003008 if (Global.LOCATION_GLOBAL_KILL_SWITCH.equals(name)
3009 && isGlobalSettingsKey(key)) {
Jeff Sharkey308093f2018-03-25 22:53:29 -06003010 // When the global kill switch is updated, send the
3011 // change notification for the location setting.
3012 notifyLocationChangeForRunningUsers();
3013 }
Linus Tufvesson93c38552019-05-30 12:47:39 +01003014 notifySettingChangeForRunningUsers(key, name);
Jeff Sharkey308093f2018-03-25 22:53:29 -06003015 } finally {
3016 Binder.restoreCallingIdentity(token);
Svet Ganov945864c2018-03-22 21:49:10 -07003017 }
Svet Ganov945864c2018-03-22 21:49:10 -07003018 } else {
3019 final int userId = getUserIdFromKey(key);
3020 final Uri uri = getNotificationUriFor(key, name);
3021 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
3022 userId, 0, uri).sendToTarget();
3023 if (isSecureSettingsKey(key)) {
3024 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
3025 sSecureCloneToManagedSettings);
3026 maybeNotifyProfiles(SETTINGS_TYPE_SYSTEM, userId, uri, name,
3027 sSystemCloneFromParentOnDependency.values());
3028 } else if (isSystemSettingsKey(key)) {
3029 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
3030 sSystemCloneToManagedSettings);
3031 }
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003032 }
Svet Ganov53a441c2016-04-19 19:38:00 -07003033
Svet Ganov945864c2018-03-22 21:49:10 -07003034 // Always notify that our data changed
Svet Ganov53a441c2016-04-19 19:38:00 -07003035 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003036 }
3037
Svet Ganov53a441c2016-04-19 19:38:00 -07003038 private void maybeNotifyProfiles(int type, int userId, Uri uri, String name,
Robin Lee7af9a742017-02-20 14:47:30 +00003039 Collection<String> keysCloned) {
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003040 if (keysCloned.contains(name)) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003041 for (int profileId : mUserManager.getProfileIdsWithDisabled(userId)) {
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003042 // the notification for userId has already been sent.
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003043 if (profileId != userId) {
Svet Ganov945864c2018-03-22 21:49:10 -07003044 final int key = makeKey(type, profileId);
3045 // Increment the generation first, so observers always see the new value
3046 mGenerationRegistry.incrementGeneration(key);
Svetoslav7e0683b2015-08-03 16:02:52 -07003047 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003048 profileId, 0, uri).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003049 }
3050 }
3051 }
Svetoslav683914b2015-01-15 14:22:26 -08003052 }
3053
Linus Tufvesson93c38552019-05-30 12:47:39 +01003054 private void notifySettingChangeForRunningUsers(int key, String name) {
Svet Ganov945864c2018-03-22 21:49:10 -07003055 // Important: No need to update generation for each user as there
3056 // is a singleton generation entry for the global settings which
3057 // is already incremented be the caller.
3058 final Uri uri = getNotificationUriFor(key, name);
3059 final List<UserInfo> users = mUserManager.getUsers(/*excludeDying*/ true);
3060 for (int i = 0; i < users.size(); i++) {
3061 final int userId = users.get(i).id;
3062 if (mUserManager.isUserRunning(UserHandle.of(userId))) {
3063 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
3064 userId, 0, uri).sendToTarget();
3065 }
3066 }
3067 }
3068
Makoto Onuki0000d322017-11-28 16:31:47 -08003069 private void notifyLocationChangeForRunningUsers() {
3070 final List<UserInfo> users = mUserManager.getUsers(/*excludeDying=*/ true);
3071
3072 for (int i = 0; i < users.size(); i++) {
3073 final int userId = users.get(i).id;
3074
3075 if (!mUserManager.isUserRunning(UserHandle.of(userId))) {
3076 continue;
3077 }
Makoto Onuki0000d322017-11-28 16:31:47 -08003078
Svet Ganov945864c2018-03-22 21:49:10 -07003079 // Increment the generation first, so observers always see the new value
3080 final int key = makeKey(SETTINGS_TYPE_SECURE, userId);
3081 mGenerationRegistry.incrementGeneration(key);
3082
Kweku Adams5e0052b2019-02-22 15:17:52 -08003083 final Uri uri = getNotificationUriFor(key, Secure.LOCATION_MODE);
Makoto Onuki0000d322017-11-28 16:31:47 -08003084 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
3085 userId, 0, uri).sendToTarget();
3086 }
3087 }
3088
Matt Pape1b31a332018-10-17 09:58:28 -07003089 private boolean isConfigSettingsKey(int key) {
3090 return getTypeFromKey(key) == SETTINGS_TYPE_CONFIG;
3091 }
3092
Svetoslav683914b2015-01-15 14:22:26 -08003093 private boolean isGlobalSettingsKey(int key) {
3094 return getTypeFromKey(key) == SETTINGS_TYPE_GLOBAL;
3095 }
3096
3097 private boolean isSystemSettingsKey(int key) {
3098 return getTypeFromKey(key) == SETTINGS_TYPE_SYSTEM;
3099 }
3100
3101 private boolean isSecureSettingsKey(int key) {
3102 return getTypeFromKey(key) == SETTINGS_TYPE_SECURE;
3103 }
3104
Mark Rathjend891f012017-01-19 04:10:37 +00003105 private boolean isSsaidSettingsKey(int key) {
3106 return getTypeFromKey(key) == SETTINGS_TYPE_SSAID;
3107 }
3108
Svetoslav683914b2015-01-15 14:22:26 -08003109 private File getSettingsFile(int key) {
Matt Pape1b31a332018-10-17 09:58:28 -07003110 if (isConfigSettingsKey(key)) {
3111 final int userId = getUserIdFromKey(key);
3112 return new File(Environment.getUserSystemDirectory(userId),
3113 SETTINGS_FILE_CONFIG);
3114 } else if (isGlobalSettingsKey(key)) {
Svetoslav683914b2015-01-15 14:22:26 -08003115 final int userId = getUserIdFromKey(key);
3116 return new File(Environment.getUserSystemDirectory(userId),
3117 SETTINGS_FILE_GLOBAL);
3118 } else if (isSystemSettingsKey(key)) {
3119 final int userId = getUserIdFromKey(key);
3120 return new File(Environment.getUserSystemDirectory(userId),
3121 SETTINGS_FILE_SYSTEM);
3122 } else if (isSecureSettingsKey(key)) {
3123 final int userId = getUserIdFromKey(key);
3124 return new File(Environment.getUserSystemDirectory(userId),
3125 SETTINGS_FILE_SECURE);
Mark Rathjend891f012017-01-19 04:10:37 +00003126 } else if (isSsaidSettingsKey(key)) {
3127 final int userId = getUserIdFromKey(key);
3128 return new File(Environment.getUserSystemDirectory(userId),
3129 SETTINGS_FILE_SSAID);
Svetoslav683914b2015-01-15 14:22:26 -08003130 } else {
3131 throw new IllegalArgumentException("Invalid settings key:" + key);
3132 }
3133 }
3134
3135 private Uri getNotificationUriFor(int key, String name) {
Matt Pape1b31a332018-10-17 09:58:28 -07003136 if (isConfigSettingsKey(key)) {
Matt Pape1278d1c2018-12-11 13:03:49 -08003137 return (name != null) ? Uri.withAppendedPath(DeviceConfig.CONTENT_URI, name)
3138 : DeviceConfig.CONTENT_URI;
Matt Pape1b31a332018-10-17 09:58:28 -07003139 } else if (isGlobalSettingsKey(key)) {
Svetoslav683914b2015-01-15 14:22:26 -08003140 return (name != null) ? Uri.withAppendedPath(Settings.Global.CONTENT_URI, name)
3141 : Settings.Global.CONTENT_URI;
3142 } else if (isSecureSettingsKey(key)) {
3143 return (name != null) ? Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name)
3144 : Settings.Secure.CONTENT_URI;
3145 } else if (isSystemSettingsKey(key)) {
3146 return (name != null) ? Uri.withAppendedPath(Settings.System.CONTENT_URI, name)
3147 : Settings.System.CONTENT_URI;
3148 } else {
3149 throw new IllegalArgumentException("Invalid settings key:" + key);
3150 }
3151 }
3152
3153 private int getMaxBytesPerPackageForType(int type) {
3154 switch (type) {
Matt Pape1b31a332018-10-17 09:58:28 -07003155 case SETTINGS_TYPE_CONFIG:
Svetoslav683914b2015-01-15 14:22:26 -08003156 case SETTINGS_TYPE_GLOBAL:
Mark Rathjend891f012017-01-19 04:10:37 +00003157 case SETTINGS_TYPE_SECURE:
3158 case SETTINGS_TYPE_SSAID: {
Svetoslav683914b2015-01-15 14:22:26 -08003159 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED;
3160 }
3161
3162 default: {
3163 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_LIMITED;
3164 }
3165 }
3166 }
3167
Svetoslav7e0683b2015-08-03 16:02:52 -07003168 private final class MyHandler extends Handler {
3169 private static final int MSG_NOTIFY_URI_CHANGED = 1;
3170 private static final int MSG_NOTIFY_DATA_CHANGED = 2;
3171
3172 public MyHandler(Looper looper) {
3173 super(looper);
3174 }
3175
3176 @Override
3177 public void handleMessage(Message msg) {
3178 switch (msg.what) {
3179 case MSG_NOTIFY_URI_CHANGED: {
3180 final int userId = msg.arg1;
3181 Uri uri = (Uri) msg.obj;
Jeff Sharkey88f9d0b2017-08-11 15:29:40 -06003182 try {
3183 getContext().getContentResolver().notifyChange(uri, null, true, userId);
3184 } catch (SecurityException e) {
3185 Slog.w(LOG_TAG, "Failed to notify for " + userId + ": " + uri, e);
3186 }
Makoto Onuki0000d322017-11-28 16:31:47 -08003187 if (DEBUG || true) {
Svetoslav7e0683b2015-08-03 16:02:52 -07003188 Slog.v(LOG_TAG, "Notifying for " + userId + ": " + uri);
3189 }
3190 } break;
3191
3192 case MSG_NOTIFY_DATA_CHANGED: {
3193 mBackupManager.dataChanged();
3194 } break;
3195 }
3196 }
3197 }
3198
Svetoslav683914b2015-01-15 14:22:26 -08003199 private final class UpgradeController {
Beverlyf364d7c2019-10-10 16:44:43 -04003200 private static final int SETTINGS_VERSION = 184;
Svetoslav683914b2015-01-15 14:22:26 -08003201
3202 private final int mUserId;
3203
3204 public UpgradeController(int userId) {
3205 mUserId = userId;
3206 }
3207
3208 public void upgradeIfNeededLocked() {
3209 // The version of all settings for a user is the same (all users have secure).
3210 SettingsState secureSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07003211 SETTINGS_TYPE_SECURE, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08003212
3213 // Try an update from the current state.
3214 final int oldVersion = secureSettings.getVersionLocked();
3215 final int newVersion = SETTINGS_VERSION;
3216
Svet Ganovc9755bc2015-03-28 13:21:22 -07003217 // If up do date - done.
Svetoslav683914b2015-01-15 14:22:26 -08003218 if (oldVersion == newVersion) {
3219 return;
3220 }
3221
3222 // Try to upgrade.
3223 final int curVersion = onUpgradeLocked(mUserId, oldVersion, newVersion);
3224
3225 // If upgrade failed start from scratch and upgrade.
3226 if (curVersion != newVersion) {
3227 // Drop state we have for this user.
3228 removeUserStateLocked(mUserId, true);
3229
3230 // Recreate the database.
3231 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), mUserId);
3232 SQLiteDatabase database = dbHelper.getWritableDatabase();
3233 dbHelper.recreateDatabase(database, newVersion, curVersion, oldVersion);
3234
3235 // Migrate the settings for this user.
3236 migrateLegacySettingsForUserLocked(dbHelper, database, mUserId);
3237
3238 // Now upgrade should work fine.
3239 onUpgradeLocked(mUserId, oldVersion, newVersion);
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07003240
3241 // Make a note what happened, so we don't wonder why data was lost
3242 String reason = "Settings rebuilt! Current version: "
3243 + curVersion + " while expected: " + newVersion;
3244 getGlobalSettingsLocked().insertSettingLocked(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003245 Settings.Global.DATABASE_DOWNGRADE_REASON,
3246 reason, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Svetoslav683914b2015-01-15 14:22:26 -08003247 }
3248
3249 // Set the global settings version if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07003250 if (mUserId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08003251 SettingsState globalSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07003252 SETTINGS_TYPE_GLOBAL, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08003253 globalSettings.setVersionLocked(newVersion);
3254 }
3255
3256 // Set the secure settings version.
3257 secureSettings.setVersionLocked(newVersion);
3258
3259 // Set the system settings version.
3260 SettingsState systemSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07003261 SETTINGS_TYPE_SYSTEM, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08003262 systemSettings.setVersionLocked(newVersion);
3263 }
3264
3265 private SettingsState getGlobalSettingsLocked() {
Xiaohui Chen43765b72015-08-31 10:57:33 -07003266 return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08003267 }
3268
3269 private SettingsState getSecureSettingsLocked(int userId) {
3270 return getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
3271 }
3272
Mark Rathjend891f012017-01-19 04:10:37 +00003273 private SettingsState getSsaidSettingsLocked(int userId) {
3274 return getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
3275 }
3276
Svetoslav683914b2015-01-15 14:22:26 -08003277 private SettingsState getSystemSettingsLocked(int userId) {
3278 return getSettingsLocked(SETTINGS_TYPE_SYSTEM, userId);
3279 }
3280
Jeff Brown503cffc2015-03-26 18:08:51 -07003281 /**
3282 * You must perform all necessary mutations to bring the settings
3283 * for this user from the old to the new version. When you add a new
3284 * upgrade step you *must* update SETTINGS_VERSION.
3285 *
3286 * This is an example of moving a setting from secure to global.
3287 *
3288 * // v119: Example settings changes.
3289 * if (currentVersion == 118) {
3290 * if (userId == UserHandle.USER_OWNER) {
3291 * // Remove from the secure settings.
3292 * SettingsState secureSettings = getSecureSettingsLocked(userId);
3293 * String name = "example_setting_to_move";
3294 * String value = secureSettings.getSetting(name);
3295 * secureSettings.deleteSetting(name);
3296 *
3297 * // Add to the global settings.
3298 * SettingsState globalSettings = getGlobalSettingsLocked();
3299 * globalSettings.insertSetting(name, value, SettingsState.SYSTEM_PACKAGE_NAME);
3300 * }
3301 *
3302 * // Update the current version.
3303 * currentVersion = 119;
3304 * }
3305 */
Svetoslav683914b2015-01-15 14:22:26 -08003306 private int onUpgradeLocked(int userId, int oldVersion, int newVersion) {
3307 if (DEBUG) {
3308 Slog.w(LOG_TAG, "Upgrading settings for user: " + userId + " from version: "
3309 + oldVersion + " to version: " + newVersion);
3310 }
3311
Jeff Brown503cffc2015-03-26 18:08:51 -07003312 int currentVersion = oldVersion;
Svetoslav683914b2015-01-15 14:22:26 -08003313
John Spurlocke11ae112015-05-11 16:09:03 -04003314 // v119: Reset zen + ringer mode.
3315 if (currentVersion == 118) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07003316 if (userId == UserHandle.USER_SYSTEM) {
John Spurlocke11ae112015-05-11 16:09:03 -04003317 final SettingsState globalSettings = getGlobalSettingsLocked();
3318 globalSettings.updateSettingLocked(Settings.Global.ZEN_MODE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003319 Integer.toString(Settings.Global.ZEN_MODE_OFF), null,
3320 true, SettingsState.SYSTEM_PACKAGE_NAME);
John Spurlocke11ae112015-05-11 16:09:03 -04003321 globalSettings.updateSettingLocked(Settings.Global.MODE_RINGER,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003322 Integer.toString(AudioManager.RINGER_MODE_NORMAL), null,
3323 true, SettingsState.SYSTEM_PACKAGE_NAME);
John Spurlocke11ae112015-05-11 16:09:03 -04003324 }
3325 currentVersion = 119;
3326 }
3327
Jason Monk27bbb2d2015-03-31 16:46:39 -04003328 // v120: Add double tap to wake setting.
3329 if (currentVersion == 119) {
3330 SettingsState secureSettings = getSecureSettingsLocked(userId);
3331 secureSettings.insertSettingLocked(Settings.Secure.DOUBLE_TAP_TO_WAKE,
3332 getContext().getResources().getBoolean(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003333 R.bool.def_double_tap_to_wake) ? "1" : "0", null, true,
Jason Monk27bbb2d2015-03-31 16:46:39 -04003334 SettingsState.SYSTEM_PACKAGE_NAME);
3335
3336 currentVersion = 120;
3337 }
3338
Svetoslav7e0683b2015-08-03 16:02:52 -07003339 if (currentVersion == 120) {
3340 // Before 121, we used a different string encoding logic. We just bump the
3341 // version here; SettingsState knows how to handle pre-version 120 files.
3342 currentVersion = 121;
3343 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -07003344
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003345 if (currentVersion == 121) {
3346 // Version 122: allow OEMs to set a default payment component in resources.
3347 // Note that we only write the default if no default has been set;
3348 // if there is, we just leave the default at whatever it currently is.
3349 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3350 String defaultComponent = (getContext().getResources().getString(
3351 R.string.def_nfc_payment_component));
3352 Setting currentSetting = secureSettings.getSettingLocked(
3353 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT);
3354 if (defaultComponent != null && !defaultComponent.isEmpty() &&
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003355 currentSetting.isNull()) {
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003356 secureSettings.insertSettingLocked(
3357 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003358 defaultComponent, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003359 }
3360 currentVersion = 122;
3361 }
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003362
3363 if (currentVersion == 122) {
3364 // Version 123: Adding a default value for the ability to add a user from
3365 // the lock screen.
3366 if (userId == UserHandle.USER_SYSTEM) {
3367 final SettingsState globalSettings = getGlobalSettingsLocked();
3368 Setting currentSetting = globalSettings.getSettingLocked(
3369 Settings.Global.ADD_USERS_WHEN_LOCKED);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003370 if (currentSetting.isNull()) {
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003371 globalSettings.insertSettingLocked(
3372 Settings.Global.ADD_USERS_WHEN_LOCKED,
3373 getContext().getResources().getBoolean(
3374 R.bool.def_add_users_from_lockscreen) ? "1" : "0",
Svetoslav Ganove080da92016-12-21 17:10:35 -08003375 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003376 }
3377 }
3378 currentVersion = 123;
3379 }
Bryce Leebd179282015-12-17 19:01:37 -08003380
3381 if (currentVersion == 123) {
Bryce Leeec85f342015-12-16 13:32:28 -08003382 final SettingsState globalSettings = getGlobalSettingsLocked();
3383 String defaultDisabledProfiles = (getContext().getResources().getString(
3384 R.string.def_bluetooth_disabled_profiles));
3385 globalSettings.insertSettingLocked(Settings.Global.BLUETOOTH_DISABLED_PROFILES,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003386 defaultDisabledProfiles, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Bryce Leebd179282015-12-17 19:01:37 -08003387 currentVersion = 124;
Bryce Leeec85f342015-12-16 13:32:28 -08003388 }
3389
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003390 if (currentVersion == 124) {
3391 // Version 124: allow OEMs to set a default value for whether IME should be
3392 // shown when a physical keyboard is connected.
3393 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3394 Setting currentSetting = secureSettings.getSettingLocked(
3395 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003396 if (currentSetting.isNull()) {
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003397 secureSettings.insertSettingLocked(
3398 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD,
3399 getContext().getResources().getBoolean(
3400 R.bool.def_show_ime_with_hard_keyboard) ? "1" : "0",
Svetoslav Ganove080da92016-12-21 17:10:35 -08003401 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003402 }
3403 currentVersion = 125;
3404 }
3405
Ruben Brunk98576cf2016-03-07 18:54:28 -08003406 if (currentVersion == 125) {
3407 // Version 125: Allow OEMs to set the default VR service.
3408 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3409
3410 Setting currentSetting = secureSettings.getSettingLocked(
3411 Settings.Secure.ENABLED_VR_LISTENERS);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003412 if (currentSetting.isNull()) {
Ruben Brunk98576cf2016-03-07 18:54:28 -08003413 ArraySet<ComponentName> l =
3414 SystemConfig.getInstance().getDefaultVrComponents();
3415
3416 if (l != null && !l.isEmpty()) {
3417 StringBuilder b = new StringBuilder();
3418 boolean start = true;
3419 for (ComponentName c : l) {
3420 if (!start) {
3421 b.append(':');
3422 }
3423 b.append(c.flattenToString());
3424 start = false;
3425 }
3426 secureSettings.insertSettingLocked(
3427 Settings.Secure.ENABLED_VR_LISTENERS, b.toString(),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003428 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Ruben Brunk98576cf2016-03-07 18:54:28 -08003429 }
3430
3431 }
3432 currentVersion = 126;
3433 }
3434
Daniel U02ba6122016-04-01 18:41:42 +01003435 if (currentVersion == 126) {
3436 // Version 126: copy the primary values of LOCK_SCREEN_SHOW_NOTIFICATIONS and
3437 // LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS into managed profile.
3438 if (mUserManager.isManagedProfile(userId)) {
3439 final SettingsState systemSecureSettings =
3440 getSecureSettingsLocked(UserHandle.USER_SYSTEM);
3441
3442 final Setting showNotifications = systemSecureSettings.getSettingLocked(
3443 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09003444 if (!showNotifications.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01003445 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3446 secureSettings.insertSettingLocked(
3447 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003448 showNotifications.getValue(), null, true,
Daniel U02ba6122016-04-01 18:41:42 +01003449 SettingsState.SYSTEM_PACKAGE_NAME);
3450 }
3451
3452 final Setting allowPrivate = systemSecureSettings.getSettingLocked(
3453 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09003454 if (!allowPrivate.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01003455 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3456 secureSettings.insertSettingLocked(
3457 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003458 allowPrivate.getValue(), null, true,
Daniel U02ba6122016-04-01 18:41:42 +01003459 SettingsState.SYSTEM_PACKAGE_NAME);
3460 }
3461 }
3462 currentVersion = 127;
3463 }
3464
Steven Ngdc20ba62016-04-26 18:19:04 +01003465 if (currentVersion == 127) {
Mahaver Chopra3d9805d2016-07-07 16:25:05 +01003466 // version 127 is no longer used.
Steven Ngdc20ba62016-04-26 18:19:04 +01003467 currentVersion = 128;
3468 }
3469
Julia Reynolds1f721e12016-07-11 08:50:58 -04003470 if (currentVersion == 128) {
Julia Reynoldsb852e562017-06-06 16:14:18 -04003471 // Version 128: Removed
Julia Reynolds1f721e12016-07-11 08:50:58 -04003472 currentVersion = 129;
3473 }
3474
Dan Sandler71f85e92016-07-20 13:46:05 -04003475 if (currentVersion == 129) {
3476 // default longpress timeout changed from 500 to 400. If unchanged from the old
3477 // default, update to the new default.
3478 final SettingsState systemSecureSettings =
3479 getSecureSettingsLocked(userId);
3480 final String oldValue = systemSecureSettings.getSettingLocked(
3481 Settings.Secure.LONG_PRESS_TIMEOUT).getValue();
3482 if (TextUtils.equals("500", oldValue)) {
3483 systemSecureSettings.insertSettingLocked(
3484 Settings.Secure.LONG_PRESS_TIMEOUT,
3485 String.valueOf(getContext().getResources().getInteger(
3486 R.integer.def_long_press_timeout_millis)),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003487 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Dan Sandler71f85e92016-07-20 13:46:05 -04003488 }
3489 currentVersion = 130;
3490 }
3491
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003492 if (currentVersion == 130) {
Adrian Roos69741a22016-10-21 14:49:17 -07003493 // Split Ambient settings
3494 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3495 boolean dozeExplicitlyDisabled = "0".equals(secureSettings.
3496 getSettingLocked(Settings.Secure.DOZE_ENABLED).getValue());
3497
3498 if (dozeExplicitlyDisabled) {
Lucas Dupin4359b552018-08-09 15:07:54 -07003499 secureSettings.insertSettingLocked(Settings.Secure.DOZE_PICK_UP_GESTURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003500 "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Lucas Dupin4359b552018-08-09 15:07:54 -07003501 secureSettings.insertSettingLocked(Settings.Secure.DOZE_DOUBLE_TAP_GESTURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003502 "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Adrian Roos69741a22016-10-21 14:49:17 -07003503 }
3504 currentVersion = 131;
3505 }
3506
3507 if (currentVersion == 131) {
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003508 // Initialize new multi-press timeout to default value
3509 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3510 final String oldValue = systemSecureSettings.getSettingLocked(
3511 Settings.Secure.MULTI_PRESS_TIMEOUT).getValue();
3512 if (TextUtils.equals(null, oldValue)) {
3513 systemSecureSettings.insertSettingLocked(
3514 Settings.Secure.MULTI_PRESS_TIMEOUT,
3515 String.valueOf(getContext().getResources().getInteger(
3516 R.integer.def_multi_press_timeout_millis)),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003517 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003518 }
3519
Adrian Roos69741a22016-10-21 14:49:17 -07003520 currentVersion = 132;
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003521 }
3522
Adrian Roos69741a22016-10-21 14:49:17 -07003523 if (currentVersion == 132) {
3524 // Version 132: Allow managed profile to optionally use the parent's ringtones
Andre Lagoea35e072016-08-04 13:41:13 +01003525 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3526 String defaultSyncParentSounds = (getContext().getResources()
3527 .getBoolean(R.bool.def_sync_parent_sounds) ? "1" : "0");
3528 systemSecureSettings.insertSettingLocked(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003529 Settings.Secure.SYNC_PARENT_SOUNDS, defaultSyncParentSounds,
3530 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Adrian Roos69741a22016-10-21 14:49:17 -07003531 currentVersion = 133;
Andre Lagoea35e072016-08-04 13:41:13 +01003532 }
3533
Adrian Roos69741a22016-10-21 14:49:17 -07003534 if (currentVersion == 133) {
3535 // Version 133: Add default end button behavior
Keun-young Parkec7a1182016-10-18 11:52:38 -07003536 final SettingsState systemSettings = getSystemSettingsLocked(userId);
davidlnedb31e12018-09-25 10:21:20 -07003537 if (systemSettings.getSettingLocked(Settings.System.END_BUTTON_BEHAVIOR)
3538 .isNull()) {
Keun-young Parkec7a1182016-10-18 11:52:38 -07003539 String defaultEndButtonBehavior = Integer.toString(getContext()
3540 .getResources().getInteger(R.integer.def_end_button_behavior));
3541 systemSettings.insertSettingLocked(Settings.System.END_BUTTON_BEHAVIOR,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003542 defaultEndButtonBehavior, null, true,
3543 SettingsState.SYSTEM_PACKAGE_NAME);
Keun-young Parkec7a1182016-10-18 11:52:38 -07003544 }
Adrian Roos69741a22016-10-21 14:49:17 -07003545 currentVersion = 134;
Keun-young Parkec7a1182016-10-18 11:52:38 -07003546 }
3547
Phil Weaver89e3ffc2016-09-19 13:51:10 -07003548 if (currentVersion == 134) {
3549 // Remove setting that specifies if magnification values should be preserved.
3550 // This setting defaulted to true and never has a UI.
3551 getSecureSettingsLocked(userId).deleteSettingLocked(
3552 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_AUTO_UPDATE);
3553 currentVersion = 135;
3554 }
3555
Jeremy Joslin8bdad342016-12-14 11:46:47 -08003556 if (currentVersion == 135) {
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003557 // Version 135 no longer used.
Jeremy Joslin8bdad342016-12-14 11:46:47 -08003558 currentVersion = 136;
3559 }
3560
Mark Rathjend891f012017-01-19 04:10:37 +00003561 if (currentVersion == 136) {
3562 // Version 136: Store legacy SSAID for all apps currently installed on the
3563 // device as first step in migrating SSAID to be unique per application.
3564
3565 final boolean isUpgrade;
3566 try {
Svet Ganovd8eb8b22019-04-05 18:52:08 -07003567 isUpgrade = mPackageManager.isDeviceUpgrading();
Mark Rathjend891f012017-01-19 04:10:37 +00003568 } catch (RemoteException e) {
3569 throw new IllegalStateException("Package manager not available");
3570 }
3571 // Only retain legacy ssaid if the device is performing an OTA. After wiping
3572 // user data or first boot on a new device should use new ssaid generation.
3573 if (isUpgrade) {
3574 // Retrieve the legacy ssaid from the secure settings table.
Mark Rathjenea617592017-01-18 23:03:41 -08003575 final Setting legacySsaidSetting = getSettingLocked(SETTINGS_TYPE_SECURE,
3576 userId, Settings.Secure.ANDROID_ID);
3577 if (legacySsaidSetting == null || legacySsaidSetting.isNull()
3578 || legacySsaidSetting.getValue() == null) {
3579 throw new IllegalStateException("Legacy ssaid not accessible");
3580 }
3581 final String legacySsaid = legacySsaidSetting.getValue();
Mark Rathjend891f012017-01-19 04:10:37 +00003582
3583 // Fill each uid with the legacy ssaid to be backwards compatible.
3584 final List<PackageInfo> packages;
3585 try {
Tetsutoki Shiozawaebe0e5f2018-01-17 11:07:09 +09003586 packages = mPackageManager.getInstalledPackages(
3587 PackageManager.MATCH_UNINSTALLED_PACKAGES,
3588 userId).getList();
Mark Rathjend891f012017-01-19 04:10:37 +00003589 } catch (RemoteException e) {
3590 throw new IllegalStateException("Package manager not available");
3591 }
3592
3593 final SettingsState ssaidSettings = getSsaidSettingsLocked(userId);
3594 for (PackageInfo info : packages) {
3595 // Check if the UID already has an entry in the table.
3596 final String uid = Integer.toString(info.applicationInfo.uid);
3597 final Setting ssaid = ssaidSettings.getSettingLocked(uid);
3598
3599 if (ssaid.isNull() || ssaid.getValue() == null) {
3600 // Android Id doesn't exist for this package so create it.
3601 ssaidSettings.insertSettingLocked(uid, legacySsaid, null, true,
3602 info.packageName);
Tetsutoki Shiozawaebe0e5f2018-01-17 11:07:09 +09003603 if (DEBUG) {
3604 Slog.d(LOG_TAG, "Keep the legacy ssaid for uid=" + uid);
3605 }
Mark Rathjend891f012017-01-19 04:10:37 +00003606 }
3607 }
3608 }
3609
3610 currentVersion = 137;
3611 }
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003612 if (currentVersion == 137) {
3613 // Version 138: Settings.Secure#INSTALL_NON_MARKET_APPS is deprecated and its
3614 // default value set to 1. The user can no longer change the value of this
3615 // setting through the UI.
3616 final SettingsState secureSetting = getSecureSettingsLocked(userId);
3617 if (!mUserManager.hasUserRestriction(
Suprabh Shukla0b1356f2017-02-21 14:33:50 -08003618 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, UserHandle.of(userId))
3619 && secureSetting.getSettingLocked(
3620 Settings.Secure.INSTALL_NON_MARKET_APPS).getValue().equals("0")) {
3621
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003622 secureSetting.insertSettingLocked(Settings.Secure.INSTALL_NON_MARKET_APPS,
3623 "1", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shukla0b1356f2017-02-21 14:33:50 -08003624 // For managed profiles with profile owners, DevicePolicyManagerService
3625 // may want to set the user restriction in this case
3626 secureSetting.insertSettingLocked(
3627 Settings.Secure.UNKNOWN_SOURCES_DEFAULT_REVERSED, "1", null, true,
3628 SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003629 }
3630 currentVersion = 138;
3631 }
Mark Rathjend891f012017-01-19 04:10:37 +00003632
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003633 if (currentVersion == 138) {
Jeremy Joslin27d14c42017-02-15 12:02:03 -08003634 // Version 139: Removed.
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003635 currentVersion = 139;
3636 }
3637
Phil Weaver385912e2017-02-10 10:06:56 -08003638 if (currentVersion == 139) {
3639 // Version 140: Settings.Secure#ACCESSIBILITY_SPEAK_PASSWORD is deprecated and
3640 // the user can no longer change the value of this setting through the UI.
3641 // Force to true.
3642 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3643 secureSettings.updateSettingLocked(Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD,
3644 "1", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3645 currentVersion = 140;
3646 }
3647
Julia Reynoldsad0d9e02017-02-15 08:41:48 -05003648 if (currentVersion == 140) {
Julia Reynoldsb852e562017-06-06 16:14:18 -04003649 // Version 141: Removed
Julia Reynoldsad0d9e02017-02-15 08:41:48 -05003650 currentVersion = 141;
3651 }
3652
Svet Ganov13701552017-02-23 12:45:17 -08003653 if (currentVersion == 141) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003654 // This implementation was incorrectly setting the current value of
3655 // settings changed by non-system packages as the default which default
3656 // is set by the system. We add a new upgrade step at the end to properly
3657 // handle this case which would also fix incorrect changes made by the
3658 // old implementation of this step.
Svet Ganov13701552017-02-23 12:45:17 -08003659 currentVersion = 142;
3660 }
3661
Stephen Chen5d0922f2017-03-27 10:28:04 -07003662 if (currentVersion == 142) {
Felipe Lemeff355092017-04-03 12:55:02 -07003663 // Version 143: Set a default value for Wi-Fi wakeup feature.
Stephen Chen5d0922f2017-03-27 10:28:04 -07003664 if (userId == UserHandle.USER_SYSTEM) {
3665 final SettingsState globalSettings = getGlobalSettingsLocked();
3666 Setting currentSetting = globalSettings.getSettingLocked(
3667 Settings.Global.WIFI_WAKEUP_ENABLED);
3668 if (currentSetting.isNull()) {
3669 globalSettings.insertSettingLocked(
3670 Settings.Global.WIFI_WAKEUP_ENABLED,
3671 getContext().getResources().getBoolean(
3672 R.bool.def_wifi_wakeup_enabled) ? "1" : "0",
3673 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3674 }
3675 }
3676
3677 currentVersion = 143;
3678 }
3679
Felipe Lemeff355092017-04-03 12:55:02 -07003680 if (currentVersion == 143) {
3681 // Version 144: Set a default value for Autofill service.
3682 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3683 final Setting currentSetting = secureSettings
3684 .getSettingLocked(Settings.Secure.AUTOFILL_SERVICE);
3685 if (currentSetting.isNull()) {
3686 final String defaultValue = getContext().getResources().getString(
3687 com.android.internal.R.string.config_defaultAutofillService);
3688 if (defaultValue != null) {
3689 Slog.d(LOG_TAG, "Setting [" + defaultValue + "] as Autofill Service "
3690 + "for user " + userId);
3691 secureSettings.insertSettingLocked(Settings.Secure.AUTOFILL_SERVICE,
3692 defaultValue, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3693 }
3694 }
3695
3696 currentVersion = 144;
3697 }
3698
Jeremy Joslin45caa252017-05-04 11:22:46 -07003699 if (currentVersion == 144) {
Amin Shaikh86367962017-06-07 08:58:22 -07003700 // Version 145: Removed
3701 currentVersion = 145;
3702 }
3703
3704 if (currentVersion == 145) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003705 // Version 146: In step 142 we had a bug where incorrectly
3706 // some settings were considered system set and as a result
3707 // made the default and marked as the default being set by
3708 // the system. Here reevaluate the default and default system
3709 // set flags. This would both fix corruption by the old impl
3710 // of step 142 and also properly handle devices which never
3711 // run 142.
Jeremy Joslin45caa252017-05-04 11:22:46 -07003712 if (userId == UserHandle.USER_SYSTEM) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003713 SettingsState globalSettings = getGlobalSettingsLocked();
3714 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(globalSettings, userId);
3715 globalSettings.persistSyncLocked();
Jeremy Joslin45caa252017-05-04 11:22:46 -07003716 }
3717
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003718 SettingsState secureSettings = getSecureSettingsLocked(mUserId);
3719 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(secureSettings, userId);
3720 secureSettings.persistSyncLocked();
3721
3722 SettingsState systemSettings = getSystemSettingsLocked(mUserId);
3723 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(systemSettings, userId);
3724 systemSettings.persistSyncLocked();
3725
Amin Shaikh86367962017-06-07 08:58:22 -07003726 currentVersion = 146;
Jeremy Joslin45caa252017-05-04 11:22:46 -07003727 }
3728
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003729 if (currentVersion == 146) {
Joe LaPenna250d7842018-01-25 10:19:42 -08003730 // Version 147: Removed. (This version previously allowed showing the
3731 // "wifi_wakeup_available" setting).
3732 // The setting that was added here is deleted in 153.
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003733 currentVersion = 147;
3734 }
3735
3736 if (currentVersion == 147) {
3737 // Version 148: Set the default value for DEFAULT_RESTRICT_BACKGROUND_DATA.
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003738 if (userId == UserHandle.USER_SYSTEM) {
3739 final SettingsState globalSettings = getGlobalSettingsLocked();
3740 final Setting currentSetting = globalSettings.getSettingLocked(
3741 Global.DEFAULT_RESTRICT_BACKGROUND_DATA);
3742 if (currentSetting.isNull()) {
3743 globalSettings.insertSettingLocked(
3744 Global.DEFAULT_RESTRICT_BACKGROUND_DATA,
3745 getContext().getResources().getBoolean(
3746 R.bool.def_restrict_background_data) ? "1" : "0",
3747 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3748 }
3749 }
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003750 currentVersion = 148;
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003751 }
3752
Tim Zhengcc1e76a2017-08-30 17:46:19 -07003753 if (currentVersion == 148) {
3754 // Version 149: Set the default value for BACKUP_MANAGER_CONSTANTS.
3755 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3756 final String oldValue = systemSecureSettings.getSettingLocked(
3757 Settings.Secure.BACKUP_MANAGER_CONSTANTS).getValue();
3758 if (TextUtils.equals(null, oldValue)) {
3759 final String defaultValue = getContext().getResources().getString(
3760 R.string.def_backup_manager_constants);
3761 if (!TextUtils.isEmpty(defaultValue)) {
3762 systemSecureSettings.insertSettingLocked(
3763 Settings.Secure.BACKUP_MANAGER_CONSTANTS, defaultValue, null,
3764 true, SettingsState.SYSTEM_PACKAGE_NAME);
3765 }
3766 }
Tim Zhengcc1e76a2017-08-30 17:46:19 -07003767 currentVersion = 149;
3768 }
Jacky Cheung7076a312017-10-02 10:40:48 -07003769
3770 if (currentVersion == 149) {
3771 // Version 150: Set a default value for mobile data always on
3772 final SettingsState globalSettings = getGlobalSettingsLocked();
3773 final Setting currentSetting = globalSettings.getSettingLocked(
3774 Settings.Global.MOBILE_DATA_ALWAYS_ON);
3775 if (currentSetting.isNull()) {
3776 globalSettings.insertSettingLocked(
3777 Settings.Global.MOBILE_DATA_ALWAYS_ON,
3778 getContext().getResources().getBoolean(
3779 R.bool.def_mobile_data_always_on) ? "1" : "0",
3780 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3781 }
3782
3783 currentVersion = 150;
3784 }
3785
Mike Digman4af4a6f2018-01-16 14:49:38 -08003786 if (currentVersion == 150) {
Mike Digman32e03312018-05-16 16:43:23 -07003787 // Version 151: Removed.
Mike Digman4af4a6f2018-01-16 14:49:38 -08003788 currentVersion = 151;
3789 }
3790
Joe LaPenna4f50ed42018-01-22 14:54:45 -08003791 if (currentVersion == 151) {
Joe LaPenna250d7842018-01-25 10:19:42 -08003792 // Version 152: Removed. (This version made the setting for wifi_wakeup enabled
3793 // by default but it is now no longer configurable).
3794 // The setting updated here is deleted in 153.
Joe LaPenna4f50ed42018-01-22 14:54:45 -08003795 currentVersion = 152;
3796 }
3797
Joe LaPenna250d7842018-01-25 10:19:42 -08003798 if (currentVersion == 152) {
3799 getGlobalSettingsLocked().deleteSettingLocked("wifi_wakeup_available");
3800 currentVersion = 153;
3801 }
3802
Ben Linb4df8bc2018-01-29 11:48:20 -08003803 if (currentVersion == 153) {
3804 // Version 154: Read notification badge configuration from config.
3805 // If user has already set the value, don't do anything.
3806 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3807 final Setting showNotificationBadges = systemSecureSettings.getSettingLocked(
3808 Settings.Secure.NOTIFICATION_BADGING);
3809 if (showNotificationBadges.isNull()) {
3810 final boolean defaultValue = getContext().getResources().getBoolean(
3811 com.android.internal.R.bool.config_notificationBadging);
3812 systemSecureSettings.insertSettingLocked(
3813 Secure.NOTIFICATION_BADGING,
3814 defaultValue ? "1" : "0",
3815 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3816 }
3817 currentVersion = 154;
3818 }
3819
Bernardo Rufinoeaa78b92018-01-26 11:25:37 +00003820 if (currentVersion == 154) {
3821 // Version 155: Set the default value for BACKUP_LOCAL_TRANSPORT_PARAMETERS.
3822 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3823 final String oldValue = systemSecureSettings.getSettingLocked(
3824 Settings.Secure.BACKUP_LOCAL_TRANSPORT_PARAMETERS).getValue();
3825 if (TextUtils.equals(null, oldValue)) {
3826 final String defaultValue = getContext().getResources().getString(
3827 R.string.def_backup_local_transport_parameters);
3828 if (!TextUtils.isEmpty(defaultValue)) {
3829 systemSecureSettings.insertSettingLocked(
3830 Settings.Secure.BACKUP_LOCAL_TRANSPORT_PARAMETERS, defaultValue,
3831 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3832 }
3833
3834 }
3835 currentVersion = 155;
3836 }
3837
Beverlyda904812018-03-02 09:55:30 -05003838 if (currentVersion == 155) {
Beverlyf364d7c2019-10-10 16:44:43 -04003839 // Version 156: migrated to version 184
Beverlyda904812018-03-02 09:55:30 -05003840 currentVersion = 156;
3841 }
3842
Beverly09da25f2018-02-26 09:17:07 -05003843 if (currentVersion == 156) {
Beverly91d0a632018-07-02 16:45:00 -04003844 // Version 157: Set a default value for zen duration,
3845 // in version 169, zen duration is moved to secure settings
Beverly09da25f2018-02-26 09:17:07 -05003846 final SettingsState globalSettings = getGlobalSettingsLocked();
3847 final Setting currentSetting = globalSettings.getSettingLocked(
3848 Global.ZEN_DURATION);
3849 if (currentSetting.isNull()) {
3850 String defaultZenDuration = Integer.toString(getContext()
3851 .getResources().getInteger(R.integer.def_zen_duration));
3852 globalSettings.insertSettingLocked(
3853 Global.ZEN_DURATION, defaultZenDuration,
3854 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3855 }
Beverly09da25f2018-02-26 09:17:07 -05003856 currentVersion = 157;
3857 }
Annie Mengd069a882018-03-13 15:31:40 +00003858
3859 if (currentVersion == 157) {
3860 // Version 158: Set default value for BACKUP_AGENT_TIMEOUT_PARAMETERS.
3861 final SettingsState globalSettings = getGlobalSettingsLocked();
3862 final String oldValue = globalSettings.getSettingLocked(
3863 Settings.Global.BACKUP_AGENT_TIMEOUT_PARAMETERS).getValue();
3864 if (TextUtils.equals(null, oldValue)) {
3865 final String defaultValue = getContext().getResources().getString(
3866 R.string.def_backup_agent_timeout_parameters);
3867 if (!TextUtils.isEmpty(defaultValue)) {
3868 globalSettings.insertSettingLocked(
3869 Settings.Global.BACKUP_AGENT_TIMEOUT_PARAMETERS, defaultValue,
3870 null, true,
3871 SettingsState.SYSTEM_PACKAGE_NAME);
3872 }
3873 }
3874 currentVersion = 158;
3875 }
Roshan Pius9c396672018-03-02 14:54:13 -08003876
3877 if (currentVersion == 158) {
3878 // Remove setting that specifies wifi bgscan throttling params
3879 getGlobalSettingsLocked().deleteSettingLocked(
3880 "wifi_scan_background_throttle_interval_ms");
3881 getGlobalSettingsLocked().deleteSettingLocked(
3882 "wifi_scan_background_throttle_package_whitelist");
3883 currentVersion = 159;
3884 }
3885
Pavel Grafovc5c97302018-03-19 13:37:15 +00003886 if (currentVersion == 159) {
3887 // Version 160: Hiding notifications from the lockscreen is only available as
3888 // primary user option, profiles can only make them redacted. If a profile was
3889 // configured to not show lockscreen notifications, ensure that at the very
3890 // least these will be come hidden.
3891 if (mUserManager.isManagedProfile(userId)) {
3892 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3893 Setting showNotifications = secureSettings.getSettingLocked(
3894 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
3895 // The default value is "1", check if user has turned it off.
3896 if ("0".equals(showNotifications.getValue())) {
3897 secureSettings.insertSettingLocked(
3898 Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, "0",
3899 null /* tag */, false /* makeDefault */,
3900 SettingsState.SYSTEM_PACKAGE_NAME);
3901 }
3902 // The setting is no longer valid for managed profiles, it should be
3903 // treated as if it was set to "1".
3904 secureSettings.deleteSettingLocked(Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
3905 }
3906 currentVersion = 160;
3907 }
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08003908
3909 if (currentVersion == 160) {
3910 // Version 161: Set the default value for
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08003911 // MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY and
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08003912 // SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT
3913 final SettingsState globalSettings = getGlobalSettingsLocked();
3914
3915 String oldValue = globalSettings.getSettingLocked(
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08003916 Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY).getValue();
3917 if (TextUtils.equals(null, oldValue)) {
3918 globalSettings.insertSettingLocked(
3919 Settings.Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY,
3920 Integer.toString(getContext().getResources().getInteger(
3921 R.integer.def_max_sound_trigger_detection_service_ops_per_day)),
3922 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3923 }
3924
3925 oldValue = globalSettings.getSettingLocked(
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08003926 Global.SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT).getValue();
3927 if (TextUtils.equals(null, oldValue)) {
3928 globalSettings.insertSettingLocked(
3929 Settings.Global.SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT,
3930 Integer.toString(getContext().getResources().getInteger(
3931 R.integer.def_sound_trigger_detection_service_op_timeout)),
3932 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3933 }
3934 currentVersion = 161;
3935 }
3936
Mike Digman55272862018-02-20 14:35:17 -08003937 if (currentVersion == 161) {
3938 // Version 161: Add a gesture for silencing phones
3939 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3940 final Setting currentSetting = secureSettings.getSettingLocked(
3941 Secure.VOLUME_HUSH_GESTURE);
3942 if (currentSetting.isNull()) {
3943 secureSettings.insertSettingLocked(
3944 Secure.VOLUME_HUSH_GESTURE,
3945 Integer.toString(Secure.VOLUME_HUSH_VIBRATE),
3946 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3947 }
3948
3949 currentVersion = 162;
3950 }
3951
Julia Reynoldsc4e5ecf2018-04-11 11:33:32 -04003952 if (currentVersion == 162) {
Julia Reynolds76bfa602018-04-23 09:38:47 -04003953 // Version 162: REMOVED: Add a gesture for silencing phones
Julia Reynoldsc4e5ecf2018-04-11 11:33:32 -04003954 currentVersion = 163;
3955 }
3956
Philip P. Moltmanncb58a832018-04-16 09:19:42 -07003957 if (currentVersion == 163) {
3958 // Version 163: Update default value of
3959 // MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY from old to new default
3960 final SettingsState settings = getGlobalSettingsLocked();
3961 final Setting currentSetting = settings.getSettingLocked(
3962 Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY);
3963 if (currentSetting.isDefaultFromSystem()) {
3964 settings.insertSettingLocked(
3965 Settings.Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY,
3966 Integer.toString(getContext().getResources().getInteger(
3967 R.integer
3968 .def_max_sound_trigger_detection_service_ops_per_day)),
3969 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3970 }
3971
3972 currentVersion = 164;
3973 }
3974
Julia Reynolds76bfa602018-04-23 09:38:47 -04003975 if (currentVersion == 164) {
Julia Reynolds24836c52018-06-06 14:36:03 -04003976 // Version 164: REMOVED: show zen upgrade notification
Julia Reynolds76bfa602018-04-23 09:38:47 -04003977 currentVersion = 165;
3978 }
3979
Beverly301e92a2018-04-27 09:43:05 -04003980 if (currentVersion == 165) {
Beverly91d0a632018-07-02 16:45:00 -04003981 // Version 165: MOVED: Show zen settings suggestion and zen updated settings
3982 // moved to secure settings and are set in version 169
Beverly301e92a2018-04-27 09:43:05 -04003983 currentVersion = 166;
3984 }
3985
Beverly38fcfd02018-05-18 17:33:40 -04003986 if (currentVersion == 166) {
3987 // Version 166: add default values for hush gesture used and manual ringer
3988 // toggle
3989 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3990 Setting currentHushUsedSetting = secureSettings.getSettingLocked(
3991 Secure.HUSH_GESTURE_USED);
3992 if (currentHushUsedSetting.isNull()) {
3993 secureSettings.insertSettingLocked(
3994 Settings.Secure.HUSH_GESTURE_USED, "0", null, true,
3995 SettingsState.SYSTEM_PACKAGE_NAME);
3996 }
3997
3998 Setting currentRingerToggleCountSetting = secureSettings.getSettingLocked(
3999 Secure.MANUAL_RINGER_TOGGLE_COUNT);
4000 if (currentRingerToggleCountSetting.isNull()) {
4001 secureSettings.insertSettingLocked(
4002 Settings.Secure.MANUAL_RINGER_TOGGLE_COUNT, "0", null, true,
4003 SettingsState.SYSTEM_PACKAGE_NAME);
4004 }
4005 currentVersion = 167;
4006 }
4007
Beverly155c9d22018-05-23 18:03:23 -04004008 if (currentVersion == 167) {
Beverly91d0a632018-07-02 16:45:00 -04004009 // Version 167: MOVED - Settings.Global.CHARGING_VIBRATION_ENABLED moved to
4010 // Settings.Secure.CHARGING_VIBRATION_ENABLED, set in version 170
Beverly155c9d22018-05-23 18:03:23 -04004011 currentVersion = 168;
4012 }
4013
Michael Wright0e9eeee2018-05-26 00:31:20 +01004014 if (currentVersion == 168) {
4015 // Version 168: by default, vibrate for phone calls
4016 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4017 final Setting currentSetting = systemSettings.getSettingLocked(
4018 Settings.System.VIBRATE_WHEN_RINGING);
4019 if (currentSetting.isNull()) {
4020 systemSettings.insertSettingLocked(
4021 Settings.System.VIBRATE_WHEN_RINGING,
4022 getContext().getResources().getBoolean(
4023 R.bool.def_vibrate_when_ringing) ? "1" : "0",
4024 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4025 }
4026 currentVersion = 169;
4027 }
4028
Nadav Barf9f115d2018-06-24 10:06:50 +03004029 if (currentVersion == 169) {
Beverly91d0a632018-07-02 16:45:00 -04004030 // Version 169: Set the default value for Secure Settings ZEN_DURATION,
4031 // SHOW_ZEN_SETTINGS_SUGGESTION, ZEN_SETTINGS_UPDATE and
4032 // ZEN_SETTINGS_SUGGESTION_VIEWED
Nadav Barf9f115d2018-06-24 10:06:50 +03004033
Beverly91d0a632018-07-02 16:45:00 -04004034 final SettingsState globalSettings = getGlobalSettingsLocked();
4035 final Setting globalZenDuration = globalSettings.getSettingLocked(
4036 Global.ZEN_DURATION);
4037
4038 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4039 final Setting secureZenDuration = secureSettings.getSettingLocked(
4040 Secure.ZEN_DURATION);
4041
4042 // ZEN_DURATION
4043 if (!globalZenDuration.isNull()) {
4044 secureSettings.insertSettingLocked(
4045 Secure.ZEN_DURATION, globalZenDuration.getValue(), null, false,
4046 SettingsState.SYSTEM_PACKAGE_NAME);
4047
4048 // set global zen duration setting to null since it's deprecated
4049 globalSettings.insertSettingLocked(
4050 Global.ZEN_DURATION, null, null, true,
4051 SettingsState.SYSTEM_PACKAGE_NAME);
4052 } else if (secureZenDuration.isNull()) {
4053 String defaultZenDuration = Integer.toString(getContext()
4054 .getResources().getInteger(R.integer.def_zen_duration));
4055 secureSettings.insertSettingLocked(
4056 Secure.ZEN_DURATION, defaultZenDuration, null, true,
4057 SettingsState.SYSTEM_PACKAGE_NAME);
Nadav Barf9f115d2018-06-24 10:06:50 +03004058 }
Beverly91d0a632018-07-02 16:45:00 -04004059
4060 // SHOW_ZEN_SETTINGS_SUGGESTION
4061 final Setting currentShowZenSettingSuggestion = secureSettings.getSettingLocked(
4062 Secure.SHOW_ZEN_SETTINGS_SUGGESTION);
4063 if (currentShowZenSettingSuggestion.isNull()) {
4064 secureSettings.insertSettingLocked(
4065 Secure.SHOW_ZEN_SETTINGS_SUGGESTION, "1",
4066 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4067 }
4068
4069 // ZEN_SETTINGS_UPDATED
4070 final Setting currentUpdatedSetting = secureSettings.getSettingLocked(
4071 Secure.ZEN_SETTINGS_UPDATED);
4072 if (currentUpdatedSetting.isNull()) {
4073 secureSettings.insertSettingLocked(
4074 Secure.ZEN_SETTINGS_UPDATED, "0",
4075 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4076 }
4077
4078 // ZEN_SETTINGS_SUGGESTION_VIEWED
4079 final Setting currentSettingSuggestionViewed = secureSettings.getSettingLocked(
4080 Secure.ZEN_SETTINGS_SUGGESTION_VIEWED);
4081 if (currentSettingSuggestionViewed.isNull()) {
4082 secureSettings.insertSettingLocked(
4083 Secure.ZEN_SETTINGS_SUGGESTION_VIEWED, "0",
4084 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4085 }
4086
Nadav Barf9f115d2018-06-24 10:06:50 +03004087 currentVersion = 170;
4088 }
4089
Beverly91d0a632018-07-02 16:45:00 -04004090 if (currentVersion == 170) {
4091 // Version 170: Set the default value for Secure Settings:
4092 // CHARGING_SOUNDS_ENABLED and CHARGING_VIBRATION_ENABLED
4093
4094 final SettingsState globalSettings = getGlobalSettingsLocked();
4095 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4096
4097 // CHARGING_SOUNDS_ENABLED
4098 final Setting globalChargingSoundEnabled = globalSettings.getSettingLocked(
4099 Global.CHARGING_SOUNDS_ENABLED);
4100 final Setting secureChargingSoundsEnabled = secureSettings.getSettingLocked(
4101 Secure.CHARGING_SOUNDS_ENABLED);
4102
4103 if (!globalChargingSoundEnabled.isNull()) {
Beverlyff9c5872019-01-02 15:44:00 -05004104 if (secureChargingSoundsEnabled.isNull()) {
4105 secureSettings.insertSettingLocked(
4106 Secure.CHARGING_SOUNDS_ENABLED,
4107 globalChargingSoundEnabled.getValue(), null, false,
4108 SettingsState.SYSTEM_PACKAGE_NAME);
4109 }
Beverly91d0a632018-07-02 16:45:00 -04004110
4111 // set global charging_sounds_enabled setting to null since it's deprecated
4112 globalSettings.insertSettingLocked(
4113 Global.CHARGING_SOUNDS_ENABLED, null, null, true,
4114 SettingsState.SYSTEM_PACKAGE_NAME);
4115 } else if (secureChargingSoundsEnabled.isNull()) {
4116 String defChargingSoundsEnabled = getContext().getResources()
4117 .getBoolean(R.bool.def_charging_sounds_enabled) ? "1" : "0";
4118 secureSettings.insertSettingLocked(
4119 Secure.CHARGING_SOUNDS_ENABLED, defChargingSoundsEnabled, null,
4120 true, SettingsState.SYSTEM_PACKAGE_NAME);
4121 }
4122
4123 // CHARGING_VIBRATION_ENABLED
4124 final Setting secureChargingVibrationEnabled = secureSettings.getSettingLocked(
4125 Secure.CHARGING_VIBRATION_ENABLED);
4126
4127 if (secureChargingVibrationEnabled.isNull()) {
4128 String defChargingVibrationEnabled = getContext().getResources()
4129 .getBoolean(R.bool.def_charging_vibration_enabled) ? "1" : "0";
4130 secureSettings.insertSettingLocked(
4131 Secure.CHARGING_VIBRATION_ENABLED, defChargingVibrationEnabled,
4132 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4133 }
4134
4135 currentVersion = 171;
4136 }
4137
Nadav Bar8bc8c9e2018-09-12 15:41:51 +03004138 if (currentVersion == 171) {
4139 // Version 171: by default, add STREAM_VOICE_CALL to list of streams that can
4140 // be muted.
4141 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4142 final Setting currentSetting = systemSettings.getSettingLocked(
4143 Settings.System.MUTE_STREAMS_AFFECTED);
4144 if (!currentSetting.isNull()) {
4145 try {
4146 int currentSettingIntegerValue = Integer.parseInt(
4147 currentSetting.getValue());
4148 if ((currentSettingIntegerValue
4149 & (1 << AudioManager.STREAM_VOICE_CALL)) == 0) {
4150 systemSettings.insertSettingLocked(
4151 Settings.System.MUTE_STREAMS_AFFECTED,
4152 Integer.toString(
4153 currentSettingIntegerValue
4154 | (1 << AudioManager.STREAM_VOICE_CALL)),
4155 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4156 }
4157 } catch (NumberFormatException e) {
4158 // remove the setting in case it is not a valid integer
4159 Slog.w("Failed to parse integer value of MUTE_STREAMS_AFFECTED"
4160 + "setting, removing setting", e);
4161 systemSettings.deleteSettingLocked(
4162 Settings.System.MUTE_STREAMS_AFFECTED);
4163 }
4164
4165 }
4166 currentVersion = 172;
4167 }
4168
Soonil Nagarkar42da1b12019-01-22 11:29:27 -08004169 if (currentVersion == 172) {
4170 // Version 172: Set the default value for Secure Settings: LOCATION_MODE
4171
4172 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4173
4174 final Setting locationMode = secureSettings.getSettingLocked(
4175 Secure.LOCATION_MODE);
4176
4177 if (locationMode.isNull()) {
4178 final Setting locationProvidersAllowed = secureSettings.getSettingLocked(
4179 Secure.LOCATION_PROVIDERS_ALLOWED);
4180
Soonil Nagarkar4ee3ac22019-02-08 19:19:24 -08004181 final int defLocationMode;
4182 if (locationProvidersAllowed.isNull()) {
4183 defLocationMode = getContext().getResources().getInteger(
4184 R.integer.def_location_mode);
4185 } else {
4186 defLocationMode =
4187 !TextUtils.isEmpty(locationProvidersAllowed.getValue())
4188 ? Secure.LOCATION_MODE_ON
4189 : Secure.LOCATION_MODE_OFF;
4190 }
Soonil Nagarkar42da1b12019-01-22 11:29:27 -08004191 secureSettings.insertSettingLocked(
Soonil Nagarkar4ee3ac22019-02-08 19:19:24 -08004192 Secure.LOCATION_MODE, Integer.toString(defLocationMode),
Soonil Nagarkar42da1b12019-01-22 11:29:27 -08004193 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4194 }
4195
4196 currentVersion = 173;
4197 }
4198
Beverly4179f992019-02-08 11:34:16 -05004199 if (currentVersion == 173) {
4200 // Version 173: Set the default value for Secure Settings: NOTIFICATION_BUBBLES
Lyn Hanb739fa62019-06-27 18:41:11 -07004201 // Removed. Moved NOTIFICATION_BUBBLES to Global Settings.
Beverly4179f992019-02-08 11:34:16 -05004202 currentVersion = 174;
4203 }
4204
Yiwen Chen0305b572019-03-05 11:26:59 -08004205 if (currentVersion == 174) {
4206 // Version 174: Set the default value for Global Settings: APPLY_RAMPING_RINGER
4207
4208 final SettingsState globalSettings = getGlobalSettingsLocked();
4209
4210 Setting currentRampingRingerSetting = globalSettings.getSettingLocked(
4211 Settings.Global.APPLY_RAMPING_RINGER);
4212 if (currentRampingRingerSetting.isNull()) {
4213 globalSettings.insertSettingLocked(
4214 Settings.Global.APPLY_RAMPING_RINGER,
4215 getContext().getResources().getBoolean(
4216 R.bool.def_apply_ramping_ringer) ? "1" : "0", null,
4217 true, SettingsState.SYSTEM_PACKAGE_NAME);
4218 }
4219
4220 currentVersion = 175;
4221 }
4222
wilsonshih5d999e22019-03-20 11:50:42 +08004223 if (currentVersion == 175) {
4224 // Version 175: Set the default value for System Settings:
4225 // RING_VIBRATION_INTENSITY. If the notification vibration intensity has been
4226 // set and ring vibration intensity hasn't, the ring vibration intensity should
4227 // followed notification vibration intensity.
4228
4229 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4230
4231 Setting notificationVibrationIntensity = systemSettings.getSettingLocked(
4232 Settings.System.NOTIFICATION_VIBRATION_INTENSITY);
4233
4234 Setting ringVibrationIntensity = systemSettings.getSettingLocked(
4235 Settings.System.RING_VIBRATION_INTENSITY);
4236
4237 if (!notificationVibrationIntensity.isNull()
4238 && ringVibrationIntensity.isNull()) {
4239 systemSettings.insertSettingLocked(
4240 Settings.System.RING_VIBRATION_INTENSITY,
4241 notificationVibrationIntensity.getValue(),
4242 null , true, SettingsState.SYSTEM_PACKAGE_NAME);
4243 }
4244
4245 currentVersion = 176;
4246 }
4247
Winson Chungd9f2fb32019-03-05 11:10:12 -08004248 if (currentVersion == 176) {
4249 // Version 176: Migrate the existing swipe up setting into the resource overlay
Winson Chung3d5d3b32019-04-24 16:52:47 -07004250 // for the navigation bar interaction mode. We do so only if the
4251 // setting is set.
Winson Chungd9f2fb32019-03-05 11:10:12 -08004252
Winson Chungd9f2fb32019-03-05 11:10:12 -08004253 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4254 final Setting swipeUpSetting = secureSettings.getSettingLocked(
Winson Chung85e74592019-03-06 11:49:22 -08004255 "swipe_up_to_switch_apps_enabled");
Winson Chung3d5d3b32019-04-24 16:52:47 -07004256 if (swipeUpSetting != null && !swipeUpSetting.isNull()
4257 && swipeUpSetting.getValue().equals("1")) {
4258 final IOverlayManager overlayManager = IOverlayManager.Stub.asInterface(
4259 ServiceManager.getService(Context.OVERLAY_SERVICE));
Winson Chungd9f2fb32019-03-05 11:10:12 -08004260 try {
Winson Chung3d5d3b32019-04-24 16:52:47 -07004261 overlayManager.setEnabledExclusiveInCategory(
4262 NAV_BAR_MODE_2BUTTON_OVERLAY, UserHandle.USER_CURRENT);
Winson Chungd9f2fb32019-03-05 11:10:12 -08004263 } catch (RemoteException e) {
4264 throw new IllegalStateException(
4265 "Failed to set nav bar interaction mode overlay");
4266 }
4267 }
4268
4269 currentVersion = 177;
4270 }
4271
Edgar Wangeb8bddb2019-04-19 17:29:49 +08004272 if (currentVersion == 177) {
4273 // Version 177: Set the default value for Secure Settings: AWARE_ENABLED
4274
4275 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4276
4277 final Setting awareEnabled = secureSettings.getSettingLocked(
4278 Secure.AWARE_ENABLED);
4279
4280 if (awareEnabled.isNull()) {
4281 final boolean defAwareEnabled = getContext().getResources().getBoolean(
4282 R.bool.def_aware_enabled);
4283 secureSettings.insertSettingLocked(
4284 Secure.AWARE_ENABLED, defAwareEnabled ? "1" : "0",
4285 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4286 }
4287
4288 currentVersion = 178;
4289 }
4290
Edgar Wangdabc41e2019-05-07 18:53:31 +08004291 if (currentVersion == 178) {
4292 // Version 178: Set the default value for Secure Settings:
4293 // SKIP_GESTURE & SILENCE_GESTURE
4294
4295 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4296
4297 final Setting skipGesture = secureSettings.getSettingLocked(
4298 Secure.SKIP_GESTURE);
4299
4300 if (skipGesture.isNull()) {
4301 final boolean defSkipGesture = getContext().getResources().getBoolean(
4302 R.bool.def_skip_gesture);
4303 secureSettings.insertSettingLocked(
4304 Secure.SKIP_GESTURE, defSkipGesture ? "1" : "0",
4305 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4306 }
4307
4308 final Setting silenceGesture = secureSettings.getSettingLocked(
4309 Secure.SILENCE_GESTURE);
4310
4311 if (silenceGesture.isNull()) {
4312 final boolean defSilenceGesture = getContext().getResources().getBoolean(
4313 R.bool.def_silence_gesture);
4314 secureSettings.insertSettingLocked(
4315 Secure.SILENCE_GESTURE, defSilenceGesture ? "1" : "0",
4316 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4317 }
4318
4319 currentVersion = 179;
4320 }
4321
Mady Mellor95e879a2019-05-06 08:50:06 -07004322 if (currentVersion == 179) {
4323 // Version 178: Reset the default for Secure Settings: NOTIFICATION_BUBBLES
4324 // This is originally set in version 173, however, the default value changed
Lyn Hanb739fa62019-06-27 18:41:11 -07004325 // so this step is to ensure the value is updated to the correct default.
Mady Mellor95e879a2019-05-06 08:50:06 -07004326
Lyn Hanb739fa62019-06-27 18:41:11 -07004327 // Removed. Moved NOTIFICATION_BUBBLES to Global Settings.
Mady Mellor95e879a2019-05-06 08:50:06 -07004328 currentVersion = 180;
4329 }
Winson Chungd9f2fb32019-03-05 11:10:12 -08004330
Edgar Wang4870e4b2019-05-15 19:14:43 +08004331 if (currentVersion == 180) {
4332 // Version 180: Set the default value for Secure Settings: AWARE_LOCK_ENABLED
4333
4334 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4335
4336 final Setting awareLockEnabled = secureSettings.getSettingLocked(
4337 Secure.AWARE_LOCK_ENABLED);
4338
4339 if (awareLockEnabled.isNull()) {
4340 final boolean defAwareLockEnabled = getContext().getResources().getBoolean(
4341 R.bool.def_aware_lock_enabled);
4342 secureSettings.insertSettingLocked(
4343 Secure.AWARE_LOCK_ENABLED, defAwareLockEnabled ? "1" : "0",
4344 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4345 }
4346
4347 currentVersion = 181;
4348 }
4349
Eric Laurent02153e62019-06-12 17:20:03 -07004350 if (currentVersion == 181) {
4351 // Version cd : by default, add STREAM_BLUETOOTH_SCO to list of streams that can
4352 // be muted.
4353 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4354 final Setting currentSetting = systemSettings.getSettingLocked(
4355 Settings.System.MUTE_STREAMS_AFFECTED);
4356 if (!currentSetting.isNull()) {
4357 try {
4358 int currentSettingIntegerValue = Integer.parseInt(
4359 currentSetting.getValue());
4360 if ((currentSettingIntegerValue
4361 & (1 << AudioManager.STREAM_BLUETOOTH_SCO)) == 0) {
4362 systemSettings.insertSettingLocked(
4363 Settings.System.MUTE_STREAMS_AFFECTED,
4364 Integer.toString(
4365 currentSettingIntegerValue
4366 | (1 << AudioManager.STREAM_BLUETOOTH_SCO)),
4367 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4368 }
4369 } catch (NumberFormatException e) {
4370 // remove the setting in case it is not a valid integer
4371 Slog.w("Failed to parse integer value of MUTE_STREAMS_AFFECTED"
4372 + "setting, removing setting", e);
4373 systemSettings.deleteSettingLocked(
4374 Settings.System.MUTE_STREAMS_AFFECTED);
4375 }
4376
4377 }
4378 currentVersion = 182;
4379 }
4380
Lyn Hanb739fa62019-06-27 18:41:11 -07004381 if (currentVersion == 182) {
4382 // Remove secure bubble settings.
Mady Mellorfa9556a2019-09-19 11:10:31 -07004383 getSecureSettingsLocked(userId).deleteSettingLocked("notification_bubbles");
Lyn Hanb739fa62019-06-27 18:41:11 -07004384
4385 // Add global bubble settings.
4386 getGlobalSettingsLocked().insertSettingLocked(Global.NOTIFICATION_BUBBLES,
4387 getContext().getResources().getBoolean(
4388 R.bool.def_notification_bubbles) ? "1" : "0", null /* tag */,
4389 true /* makeDefault */, SettingsState.SYSTEM_PACKAGE_NAME);
4390
4391 currentVersion = 183;
4392 }
4393
Beverlyf364d7c2019-10-10 16:44:43 -04004394 if (currentVersion == 183) {
4395 // Version 184: Set default values for WIRELESS_CHARGING_STARTED_SOUND
4396 // and CHARGING_STARTED_SOUND
4397 final SettingsState globalSettings = getGlobalSettingsLocked();
4398
4399 final String oldValueWireless = globalSettings.getSettingLocked(
4400 Global.WIRELESS_CHARGING_STARTED_SOUND).getValue();
4401 final String oldValueWired = globalSettings.getSettingLocked(
4402 Global.CHARGING_STARTED_SOUND).getValue();
4403
4404 final String defaultValueWireless = getContext().getResources().getString(
4405 R.string.def_wireless_charging_started_sound);
4406 final String defaultValueWired = getContext().getResources().getString(
4407 R.string.def_charging_started_sound);
4408
4409 // wireless charging sound
4410 if (oldValueWireless == null
4411 || TextUtils.equals(oldValueWireless, defaultValueWired)) {
4412 if (!TextUtils.isEmpty(defaultValueWireless)) {
4413 globalSettings.insertSettingLocked(
4414 Global.WIRELESS_CHARGING_STARTED_SOUND, defaultValueWireless,
4415 null /* tag */, true /* makeDefault */,
4416 SettingsState.SYSTEM_PACKAGE_NAME);
4417 } else if (!TextUtils.isEmpty(defaultValueWired)) {
4418 // if the wireless sound is empty, use the wired charging sound
4419 globalSettings.insertSettingLocked(
4420 Global.WIRELESS_CHARGING_STARTED_SOUND, defaultValueWired,
4421 null /* tag */, true /* makeDefault */,
4422 SettingsState.SYSTEM_PACKAGE_NAME);
4423 }
4424 }
4425
4426 // wired charging sound
4427 if (oldValueWired == null && !TextUtils.isEmpty(defaultValueWired)) {
4428 globalSettings.insertSettingLocked(
4429 Global.CHARGING_STARTED_SOUND, defaultValueWired,
4430 null /* tag */, true /* makeDefault */,
4431 SettingsState.SYSTEM_PACKAGE_NAME);
4432 }
4433 currentVersion = 184;
4434 }
4435
Felipe Lemeff355092017-04-03 12:55:02 -07004436 // vXXX: Add new settings above this point.
4437
Dan Sandler71f85e92016-07-20 13:46:05 -04004438 if (currentVersion != newVersion) {
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07004439 Slog.wtf("SettingsProvider", "warning: upgrading settings database to version "
Dan Sandler71f85e92016-07-20 13:46:05 -04004440 + newVersion + " left it at "
Stephen Chen5d0922f2017-03-27 10:28:04 -07004441 + currentVersion +
4442 " instead; this is probably a bug. Did you update SETTINGS_VERSION?",
4443 new Throwable());
Dan Sandler71f85e92016-07-20 13:46:05 -04004444 if (DEBUG) {
4445 throw new RuntimeException("db upgrade error");
4446 }
4447 }
4448
Jeff Brown503cffc2015-03-26 18:08:51 -07004449 // Return the current version.
4450 return currentVersion;
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -08004451 }
4452 }
Svet Ganov13701552017-02-23 12:45:17 -08004453
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004454 private void ensureLegacyDefaultValueAndSystemSetUpdatedLocked(SettingsState settings,
4455 int userId) {
Svet Ganov13701552017-02-23 12:45:17 -08004456 List<String> names = settings.getSettingNamesLocked();
4457 final int nameCount = names.size();
4458 for (int i = 0; i < nameCount; i++) {
4459 String name = names.get(i);
4460 Setting setting = settings.getSettingLocked(name);
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004461
4462 // In the upgrade case we pretend the call is made from the app
4463 // that made the last change to the setting to properly determine
4464 // whether the call has been made by a system component.
4465 int callingUid = -1;
4466 try {
4467 callingUid = mPackageManager.getPackageUid(setting.getPackageName(), 0, userId);
4468 } catch (RemoteException e) {
4469 /* ignore - handled below */
4470 }
4471 if (callingUid < 0) {
4472 Slog.e(LOG_TAG, "Unknown package: " + setting.getPackageName());
4473 continue;
4474 }
4475 try {
4476 final boolean systemSet = SettingsState.isSystemPackage(getContext(),
Matt Pape25c940d2019-03-26 12:14:33 -07004477 setting.getPackageName(), callingUid, userId);
Svet Ganov13701552017-02-23 12:45:17 -08004478 if (systemSet) {
4479 settings.insertSettingLocked(name, setting.getValue(),
4480 setting.getTag(), true, setting.getPackageName());
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004481 } else if (setting.getDefaultValue() != null && setting.isDefaultFromSystem()) {
4482 // We had a bug where changes by non-system packages were marked
4483 // as system made and as a result set as the default. Therefore, if
4484 // the package changed the setting last is not a system one but the
4485 // setting is marked as its default coming from the system we clear
4486 // the default and clear the system set flag.
4487 settings.resetSettingDefaultValueLocked(name);
Svet Ganov13701552017-02-23 12:45:17 -08004488 }
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004489 } catch (IllegalStateException e) {
4490 // If the package goes over its quota during the upgrade, don't
4491 // crash but just log the error as the system does the upgrade.
4492 Slog.e(LOG_TAG, "Error upgrading setting: " + setting.getName(), e);
4493
Svet Ganov13701552017-02-23 12:45:17 -08004494 }
4495 }
4496 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08004497 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004498}