blob: e492e28f61720c0d6d3a82a9b3e5ac79c405f3e3 [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 Suttonb0067fb2019-08-16 10:34:46 +010073import android.provider.settings.validators.Validator;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070074import android.text.TextUtils;
Andre Lago3fa139c2016-08-04 13:53:44 +010075import android.util.ArrayMap;
Svetoslav683914b2015-01-15 14:22:26 -080076import android.util.ArraySet;
Christopher Tate06efb532012-08-24 15:29:27 -070077import android.util.Slog;
78import android.util.SparseArray;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -070079import android.util.SparseBooleanArray;
Eugene Suslad72c3972016-12-27 15:49:30 -080080import android.util.proto.ProtoOutputStream;
John Spurlocke11ae112015-05-11 16:09:03 -040081
Svetoslav683914b2015-01-15 14:22:26 -080082import com.android.internal.annotations.GuardedBy;
83import com.android.internal.content.PackageMonitor;
84import com.android.internal.os.BackgroundThread;
Suprabh Shukla269c11e2015-12-02 16:51:16 -080085import com.android.providers.settings.SettingsState.Setting;
Svet Ganov53a441c2016-04-19 19:38:00 -070086import com.android.server.LocalServices;
Ruben Brunk98576cf2016-03-07 18:54:28 -080087import com.android.server.SystemConfig;
John Spurlocke11ae112015-05-11 16:09:03 -040088
Irina Dumitrescue3696872019-01-09 16:07:59 +000089import com.google.android.collect.Sets;
90
Neil Fuller3447fb42019-04-08 22:03:14 +010091import libcore.util.HexEncoding;
92
Svetoslav683914b2015-01-15 14:22:26 -080093import java.io.File;
Svetoslavb505ccc2015-02-17 12:41:04 -080094import java.io.FileDescriptor;
Svetoslav683914b2015-01-15 14:22:26 -080095import java.io.FileNotFoundException;
Svetoslavb505ccc2015-02-17 12:41:04 -080096import java.io.PrintWriter;
Mark Rathjen7599f132017-01-23 14:15:54 -080097import java.nio.ByteBuffer;
98import java.security.InvalidKeyException;
Mark Rathjend891f012017-01-19 04:10:37 +000099import java.security.NoSuchAlgorithmException;
Svetoslav683914b2015-01-15 14:22:26 -0800100import java.security.SecureRandom;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700101import java.util.ArrayList;
Svetoslav683914b2015-01-15 14:22:26 -0800102import java.util.Arrays;
Robin Lee7af9a742017-02-20 14:47:30 +0000103import java.util.Collection;
Matt Pape6bfc62e2018-11-28 13:16:03 -0800104import java.util.HashMap;
Mark Rathjend891f012017-01-19 04:10:37 +0000105import java.util.HashSet;
Svetoslav683914b2015-01-15 14:22:26 -0800106import java.util.List;
Andre Lago3fa139c2016-08-04 13:53:44 +0100107import java.util.Map;
Svetoslav683914b2015-01-15 14:22:26 -0800108import java.util.Set;
109import java.util.regex.Pattern;
yuemingw1d13eae2018-01-30 17:27:54 +0000110
Mark Rathjen7599f132017-01-23 14:15:54 -0800111import javax.crypto.Mac;
112import javax.crypto.spec.SecretKeySpec;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700113
Svetoslav Ganove080da92016-12-21 17:10:35 -0800114
Svetoslav683914b2015-01-15 14:22:26 -0800115/**
116 * <p>
117 * This class is a content provider that publishes the system settings.
118 * It can be accessed via the content provider APIs or via custom call
119 * commands. The latter is a bit faster and is the preferred way to access
120 * the platform settings.
121 * </p>
122 * <p>
123 * There are three settings types, global (with signature level protection
124 * and shared across users), secure (with signature permission level
125 * protection and per user), and system (with dangerous permission level
126 * protection and per user). Global settings are stored under the device owner.
127 * Each of these settings is represented by a {@link
128 * com.android.providers.settings.SettingsState} object mapped to an integer
129 * key derived from the setting type in the most significant bits and user
130 * id in the least significant bits. Settings are synchronously loaded on
131 * instantiation of a SettingsState and asynchronously persisted on mutation.
132 * Settings are stored in the user specific system directory.
133 * </p>
134 * <p>
135 * Apps targeting APIs Lollipop MR1 and lower can add custom settings entries
136 * and get a warning. Targeting higher API version prohibits this as the
137 * system settings are not a place for apps to save their state. When a package
138 * is removed the settings it added are deleted. Apps cannot delete system
139 * settings added by the platform. System settings values are validated to
140 * ensure the clients do not put bad values. Global and secure settings are
141 * changed only by trusted parties, therefore no validation is performed. Also
142 * there is a limit on the amount of app specific settings that can be added
143 * to prevent unlimited growth of the system process memory footprint.
144 * </p>
145 */
146@SuppressWarnings("deprecation")
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700147public class SettingsProvider extends ContentProvider {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700148 static final boolean DEBUG = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700149
Svetoslav Ganov264c7a92016-08-24 17:31:14 -0700150 private static final boolean DROP_DATABASE_ON_MIGRATION = true;
Svetoslav683914b2015-01-15 14:22:26 -0800151
152 private static final String LOG_TAG = "SettingsProvider";
Christopher Tate0da13572013-10-13 17:34:49 -0700153
Christopher Tate06efb532012-08-24 15:29:27 -0700154 private static final String TABLE_SYSTEM = "system";
155 private static final String TABLE_SECURE = "secure";
156 private static final String TABLE_GLOBAL = "global";
Matt Pape6bfc62e2018-11-28 13:16:03 -0800157 private static final String TABLE_CONFIG = "config";
Svetoslav683914b2015-01-15 14:22:26 -0800158
159 // Old tables no longer exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 private static final String TABLE_FAVORITES = "favorites";
161 private static final String TABLE_OLD_FAVORITES = "old_favorites";
Svetoslav683914b2015-01-15 14:22:26 -0800162 private static final String TABLE_BLUETOOTH_DEVICES = "bluetooth_devices";
163 private static final String TABLE_BOOKMARKS = "bookmarks";
164 private static final String TABLE_ANDROID_METADATA = "android_metadata";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
Svetoslav683914b2015-01-15 14:22:26 -0800166 // The set of removed legacy tables.
167 private static final Set<String> REMOVED_LEGACY_TABLES = new ArraySet<>();
Christopher Tate06efb532012-08-24 15:29:27 -0700168 static {
Svetoslav683914b2015-01-15 14:22:26 -0800169 REMOVED_LEGACY_TABLES.add(TABLE_FAVORITES);
170 REMOVED_LEGACY_TABLES.add(TABLE_OLD_FAVORITES);
171 REMOVED_LEGACY_TABLES.add(TABLE_BLUETOOTH_DEVICES);
172 REMOVED_LEGACY_TABLES.add(TABLE_BOOKMARKS);
173 REMOVED_LEGACY_TABLES.add(TABLE_ANDROID_METADATA);
174 }
Christopher Tate06efb532012-08-24 15:29:27 -0700175
Svetoslav683914b2015-01-15 14:22:26 -0800176 private static final int MUTATION_OPERATION_INSERT = 1;
177 private static final int MUTATION_OPERATION_DELETE = 2;
178 private static final int MUTATION_OPERATION_UPDATE = 3;
Svetoslav Ganove080da92016-12-21 17:10:35 -0800179 private static final int MUTATION_OPERATION_RESET = 4;
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400180
Svetoslav683914b2015-01-15 14:22:26 -0800181 private static final String[] ALL_COLUMNS = new String[] {
182 Settings.NameValueTable._ID,
183 Settings.NameValueTable.NAME,
184 Settings.NameValueTable.VALUE
185 };
186
Makoto Onuki53f0e022017-11-29 13:51:01 -0800187 public static final int SETTINGS_TYPE_GLOBAL = SettingsState.SETTINGS_TYPE_GLOBAL;
188 public static final int SETTINGS_TYPE_SYSTEM = SettingsState.SETTINGS_TYPE_SYSTEM;
189 public static final int SETTINGS_TYPE_SECURE = SettingsState.SETTINGS_TYPE_SECURE;
190 public static final int SETTINGS_TYPE_SSAID = SettingsState.SETTINGS_TYPE_SSAID;
Matt Pape1b31a332018-10-17 09:58:28 -0700191 public static final int SETTINGS_TYPE_CONFIG = SettingsState.SETTINGS_TYPE_CONFIG;
Svet Ganov53a441c2016-04-19 19:38:00 -0700192
193 private static final Bundle NULL_SETTING_BUNDLE = Bundle.forPair(
194 Settings.NameValueTable.VALUE, null);
Christopher Tate06efb532012-08-24 15:29:27 -0700195
Nicholas Sauer3d87d1e2018-11-06 08:38:39 -0800196 public static final String RESULT_ROWS_DELETED = "result_rows_deleted";
Nicholas Sauer72500532018-11-21 10:30:58 -0800197 public static final String RESULT_SETTINGS_LIST = "result_settings_list";
Nicholas Sauer3d87d1e2018-11-06 08:38:39 -0800198
Chad Brubaker20e0dc32017-04-28 18:24:55 -0700199 // Overlay specified settings whitelisted for Instant Apps
200 private static final Set<String> OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS = new ArraySet<>();
201 private static final Set<String> OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS = new ArraySet<>();
202 private static final Set<String> OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS = new ArraySet<>();
203
204 static {
205 for (String name : Resources.getSystem().getStringArray(
206 com.android.internal.R.array.config_allowedGlobalInstantAppSettings)) {
207 OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS.add(name);
208 }
209 for (String name : Resources.getSystem().getStringArray(
210 com.android.internal.R.array.config_allowedSystemInstantAppSettings)) {
211 OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS.add(name);
212 }
213 for (String name : Resources.getSystem().getStringArray(
214 com.android.internal.R.array.config_allowedSecureInstantAppSettings)) {
215 OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS.add(name);
216 }
217 }
218
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -0800219 // Changes to these global settings are synchronously persisted
220 private static final Set<String> CRITICAL_GLOBAL_SETTINGS = new ArraySet<>();
221 static {
222 CRITICAL_GLOBAL_SETTINGS.add(Settings.Global.DEVICE_PROVISIONED);
223 }
224
225 // Changes to these secure settings are synchronously persisted
226 private static final Set<String> CRITICAL_SECURE_SETTINGS = new ArraySet<>();
227 static {
228 CRITICAL_SECURE_SETTINGS.add(Settings.Secure.USER_SETUP_COMPLETE);
229 }
230
Svetoslav683914b2015-01-15 14:22:26 -0800231 // Per user secure settings that moved to the for all users global settings.
232 static final Set<String> sSecureMovedToGlobalSettings = new ArraySet<>();
233 static {
234 Settings.Secure.getMovedToGlobalSettings(sSecureMovedToGlobalSettings);
Christopher Tate06efb532012-08-24 15:29:27 -0700235 }
236
Svetoslav683914b2015-01-15 14:22:26 -0800237 // Per user system settings that moved to the for all users global settings.
238 static final Set<String> sSystemMovedToGlobalSettings = new ArraySet<>();
239 static {
240 Settings.System.getMovedToGlobalSettings(sSystemMovedToGlobalSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700241 }
242
Svetoslav683914b2015-01-15 14:22:26 -0800243 // Per user system settings that moved to the per user secure settings.
244 static final Set<String> sSystemMovedToSecureSettings = new ArraySet<>();
245 static {
246 Settings.System.getMovedToSecureSettings(sSystemMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700247 }
248
Svetoslav683914b2015-01-15 14:22:26 -0800249 // Per all users global settings that moved to the per user secure settings.
250 static final Set<String> sGlobalMovedToSecureSettings = new ArraySet<>();
251 static {
252 Settings.Global.getMovedToSecureSettings(sGlobalMovedToSecureSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700253 }
254
Svetoslav683914b2015-01-15 14:22:26 -0800255 // Per user secure settings that are cloned for the managed profiles of the user.
256 private static final Set<String> sSecureCloneToManagedSettings = new ArraySet<>();
257 static {
258 Settings.Secure.getCloneToManagedProfileSettings(sSecureCloneToManagedSettings);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700259 }
260
Svetoslav683914b2015-01-15 14:22:26 -0800261 // Per user system settings that are cloned for the managed profiles of the user.
262 private static final Set<String> sSystemCloneToManagedSettings = new ArraySet<>();
263 static {
264 Settings.System.getCloneToManagedProfileSettings(sSystemCloneToManagedSettings);
Julia Reynolds5e458dd2014-07-07 16:07:01 -0400265 }
266
Andre Lago3fa139c2016-08-04 13:53:44 +0100267 // Per user system settings that are cloned from the profile's parent when a dependency
268 // in {@link Settings.Secure} is set to "1".
269 public static final Map<String, String> sSystemCloneFromParentOnDependency = new ArrayMap<>();
270 static {
271 Settings.System.getCloneFromParentOnValueSettings(sSystemCloneFromParentOnDependency);
272 }
273
Svetoslav683914b2015-01-15 14:22:26 -0800274 private final Object mLock = new Object();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700275
Svetoslav683914b2015-01-15 14:22:26 -0800276 @GuardedBy("mLock")
277 private SettingsRegistry mSettingsRegistry;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700278
Svet Ganova8f90262016-05-10 08:44:48 -0700279 @GuardedBy("mLock")
280 private HandlerThread mHandlerThread;
281
Makoto Onuki73360ab2017-03-17 11:50:13 -0700282 @GuardedBy("mLock")
283 private Handler mHandler;
284
Svetoslav7ec28e82015-05-20 17:01:10 -0700285 // We have to call in the user manager with no lock held,
286 private volatile UserManager mUserManager;
Svetoslav683914b2015-01-15 14:22:26 -0800287
yuemingw1d13eae2018-01-30 17:27:54 +0000288 private UserManagerInternal mUserManagerInternal;
289
Svetoslav7ec28e82015-05-20 17:01:10 -0700290 // We have to call in the package manager with no lock held,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700291 private volatile IPackageManager mPackageManager;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700292
Svet Ganov53a441c2016-04-19 19:38:00 -0700293 public static int makeKey(int type, int userId) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800294 return SettingsState.makeKey(type, userId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700295 }
296
297 public static int getTypeFromKey(int key) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800298 return SettingsState.getTypeFromKey(key);
Svet Ganov53a441c2016-04-19 19:38:00 -0700299 }
300
301 public static int getUserIdFromKey(int key) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800302 return SettingsState.getUserIdFromKey(key);
Svet Ganov53a441c2016-04-19 19:38:00 -0700303 }
304
305 public static String settingTypeToString(int type) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800306 return SettingsState.settingTypeToString(type);
Svet Ganov53a441c2016-04-19 19:38:00 -0700307 }
308
309 public static String keyToString(int key) {
Makoto Onuki53f0e022017-11-29 13:51:01 -0800310 return SettingsState.keyToString(key);
Svet Ganov53a441c2016-04-19 19:38:00 -0700311 }
312
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700313 @Override
314 public boolean onCreate() {
Chad Brubaker97bccee2017-01-05 15:51:41 -0800315 Settings.setInSystemServer();
Michal Karpinski2c37b082018-01-18 16:14:27 +0000316
Svetoslav683914b2015-01-15 14:22:26 -0800317 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700318 mUserManager = UserManager.get(getContext());
yuemingw1d13eae2018-01-30 17:27:54 +0000319 mUserManagerInternal = LocalServices.getService(UserManagerInternal.class);
Xiaohui Chen43765b72015-08-31 10:57:33 -0700320 mPackageManager = AppGlobals.getPackageManager();
Svet Ganova8f90262016-05-10 08:44:48 -0700321 mHandlerThread = new HandlerThread(LOG_TAG,
322 Process.THREAD_PRIORITY_BACKGROUND);
323 mHandlerThread.start();
Makoto Onuki73360ab2017-03-17 11:50:13 -0700324 mHandler = new Handler(mHandlerThread.getLooper());
Svetoslav683914b2015-01-15 14:22:26 -0800325 mSettingsRegistry = new SettingsRegistry();
326 }
Makoto Onuki73360ab2017-03-17 11:50:13 -0700327 mHandler.post(() -> {
328 registerBroadcastReceivers();
329 startWatchingUserRestrictionChanges();
330 });
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700331 ServiceManager.addService("settings", new SettingsService(this));
Matt Papeabb67892018-12-07 09:13:26 -0800332 ServiceManager.addService("device_config", new DeviceConfigService(this));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700333 return true;
334 }
335
Svetoslav683914b2015-01-15 14:22:26 -0800336 @Override
337 public Bundle call(String method, String name, Bundle args) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700338 final int requestingUserId = getRequestingUserId(args);
339 switch (method) {
Matt Pape1b31a332018-10-17 09:58:28 -0700340 case Settings.CALL_METHOD_GET_CONFIG: {
341 Setting setting = getConfigSetting(name);
342 return packageValueForCallResult(setting, isTrackingGeneration(args));
343 }
344
Svetoslav7ec28e82015-05-20 17:01:10 -0700345 case Settings.CALL_METHOD_GET_GLOBAL: {
346 Setting setting = getGlobalSetting(name);
Svet Ganov53a441c2016-04-19 19:38:00 -0700347 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav683914b2015-01-15 14:22:26 -0800348 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700349
350 case Settings.CALL_METHOD_GET_SECURE: {
Makoto Onuki0000d322017-11-28 16:31:47 -0800351 Setting setting = getSecureSetting(name, requestingUserId,
352 /*enableOverride=*/ true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700353 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700354 }
355
356 case Settings.CALL_METHOD_GET_SYSTEM: {
357 Setting setting = getSystemSetting(name, requestingUserId);
Svet Ganov53a441c2016-04-19 19:38:00 -0700358 return packageValueForCallResult(setting, isTrackingGeneration(args));
Svetoslav7ec28e82015-05-20 17:01:10 -0700359 }
360
Matt Pape1b31a332018-10-17 09:58:28 -0700361 case Settings.CALL_METHOD_PUT_CONFIG: {
362 String value = getSettingValue(args);
Matt Pape1b31a332018-10-17 09:58:28 -0700363 final boolean makeDefault = getSettingMakeDefault(args);
Matt Papec1323dc2018-12-11 12:32:42 -0800364 insertConfigSetting(name, value, makeDefault);
Matt Pape1b31a332018-10-17 09:58:28 -0700365 break;
366 }
367
Svetoslav7ec28e82015-05-20 17:01:10 -0700368 case Settings.CALL_METHOD_PUT_GLOBAL: {
369 String value = getSettingValue(args);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800370 String tag = getSettingTag(args);
371 final boolean makeDefault = getSettingMakeDefault(args);
372 insertGlobalSetting(name, value, tag, makeDefault, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700373 break;
374 }
375
376 case Settings.CALL_METHOD_PUT_SECURE: {
377 String value = getSettingValue(args);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800378 String tag = getSettingTag(args);
379 final boolean makeDefault = getSettingMakeDefault(args);
380 insertSecureSetting(name, value, tag, makeDefault, requestingUserId, false);
Svetoslav7ec28e82015-05-20 17:01:10 -0700381 break;
382 }
383
384 case Settings.CALL_METHOD_PUT_SYSTEM: {
385 String value = getSettingValue(args);
386 insertSystemSetting(name, value, requestingUserId);
387 break;
388 }
389
Matt Pape1b31a332018-10-17 09:58:28 -0700390 case Settings.CALL_METHOD_RESET_CONFIG: {
391 final int mode = getResetModeEnforcingPermission(args);
Matt Pape6bfc62e2018-11-28 13:16:03 -0800392 String prefix = getSettingPrefix(args);
Matt Papec1323dc2018-12-11 12:32:42 -0800393 resetConfigSetting(mode, prefix);
Matt Pape1b31a332018-10-17 09:58:28 -0700394 break;
395 }
396
Svetoslav Ganove080da92016-12-21 17:10:35 -0800397 case Settings.CALL_METHOD_RESET_GLOBAL: {
398 final int mode = getResetModeEnforcingPermission(args);
399 String tag = getSettingTag(args);
400 resetGlobalSetting(requestingUserId, mode, tag);
401 break;
402 }
403
404 case Settings.CALL_METHOD_RESET_SECURE: {
405 final int mode = getResetModeEnforcingPermission(args);
406 String tag = getSettingTag(args);
407 resetSecureSetting(requestingUserId, mode, tag);
408 break;
409 }
410
Matt Pape6bfc62e2018-11-28 13:16:03 -0800411 case Settings.CALL_METHOD_DELETE_CONFIG: {
Matt Papec1323dc2018-12-11 12:32:42 -0800412 int rows = deleteConfigSetting(name) ? 1 : 0;
Nicholas Sauer3d87d1e2018-11-06 08:38:39 -0800413 Bundle result = new Bundle();
414 result.putInt(RESULT_ROWS_DELETED, rows);
415 return result;
416 }
417
418 case Settings.CALL_METHOD_DELETE_GLOBAL: {
419 int rows = deleteGlobalSetting(name, requestingUserId, false) ? 1 : 0;
420 Bundle result = new Bundle();
421 result.putInt(RESULT_ROWS_DELETED, rows);
422 return result;
423 }
424
Matt Pape6bfc62e2018-11-28 13:16:03 -0800425 case Settings.CALL_METHOD_DELETE_SECURE: {
426 int rows = deleteSecureSetting(name, requestingUserId, false) ? 1 : 0;
427 Bundle result = new Bundle();
428 result.putInt(RESULT_ROWS_DELETED, rows);
429 return result;
430 }
431
432 case Settings.CALL_METHOD_DELETE_SYSTEM: {
433 int rows = deleteSystemSetting(name, requestingUserId) ? 1 : 0;
434 Bundle result = new Bundle();
435 result.putInt(RESULT_ROWS_DELETED, rows);
436 return result;
437 }
438
439 case Settings.CALL_METHOD_LIST_CONFIG: {
440 String prefix = getSettingPrefix(args);
441 Bundle result = new Bundle();
442 result.putSerializable(
443 Settings.NameValueTable.VALUE, (HashMap) getAllConfigFlags(prefix));
444 return result;
445 }
446
447 case Settings.CALL_METHOD_LIST_GLOBAL: {
Nicholas Sauer72500532018-11-21 10:30:58 -0800448 Bundle result = new Bundle();
449 result.putStringArrayList(RESULT_SETTINGS_LIST,
Matt Pape6bfc62e2018-11-28 13:16:03 -0800450 buildSettingsList(getAllGlobalSettings(null)));
Nicholas Sauer72500532018-11-21 10:30:58 -0800451 return result;
452 }
453
454 case Settings.CALL_METHOD_LIST_SECURE: {
455 Bundle result = new Bundle();
456 result.putStringArrayList(RESULT_SETTINGS_LIST,
457 buildSettingsList(getAllSecureSettings(requestingUserId, null)));
458 return result;
459 }
460
Matt Pape6bfc62e2018-11-28 13:16:03 -0800461 case Settings.CALL_METHOD_LIST_SYSTEM: {
Nicholas Sauer72500532018-11-21 10:30:58 -0800462 Bundle result = new Bundle();
463 result.putStringArrayList(RESULT_SETTINGS_LIST,
Matt Pape6bfc62e2018-11-28 13:16:03 -0800464 buildSettingsList(getAllSystemSettings(requestingUserId, null)));
Nicholas Sauer72500532018-11-21 10:30:58 -0800465 return result;
466 }
467
Svetoslav7ec28e82015-05-20 17:01:10 -0700468 default: {
469 Slog.w(LOG_TAG, "call() with invalid method: " + method);
470 } break;
Christopher Tate06efb532012-08-24 15:29:27 -0700471 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700472
Christopher Tate06efb532012-08-24 15:29:27 -0700473 return null;
474 }
475
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800476 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800477 public String getType(Uri uri) {
478 Arguments args = new Arguments(uri, null, null, true);
479 if (TextUtils.isEmpty(args.name)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700480 return "vnd.android.cursor.dir/" + args.table;
481 } else {
Svetoslav7ec28e82015-05-20 17:01:10 -0700482 return "vnd.android.cursor.item/" + args.table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700483 }
484 }
485
486 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800487 public Cursor query(Uri uri, String[] projection, String where, String[] whereArgs,
488 String order) {
489 if (DEBUG) {
490 Slog.v(LOG_TAG, "query() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700491 }
492
Svetoslav683914b2015-01-15 14:22:26 -0800493 Arguments args = new Arguments(uri, where, whereArgs, true);
494 String[] normalizedProjection = normalizeProjection(projection);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700495
Svetoslav683914b2015-01-15 14:22:26 -0800496 // If a legacy table that is gone, done.
497 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
498 return new MatrixCursor(normalizedProjection, 0);
499 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700500
Svetoslav7ec28e82015-05-20 17:01:10 -0700501 switch (args.table) {
502 case TABLE_GLOBAL: {
503 if (args.name != null) {
504 Setting setting = getGlobalSetting(args.name);
505 return packageSettingForQuery(setting, normalizedProjection);
506 } else {
507 return getAllGlobalSettings(projection);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700508 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700509 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700510
Svetoslav7ec28e82015-05-20 17:01:10 -0700511 case TABLE_SECURE: {
512 final int userId = UserHandle.getCallingUserId();
513 if (args.name != null) {
514 Setting setting = getSecureSetting(args.name, userId);
515 return packageSettingForQuery(setting, normalizedProjection);
516 } else {
517 return getAllSecureSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800518 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700519 }
Svetoslav683914b2015-01-15 14:22:26 -0800520
Svetoslav7ec28e82015-05-20 17:01:10 -0700521 case TABLE_SYSTEM: {
522 final int userId = UserHandle.getCallingUserId();
523 if (args.name != null) {
524 Setting setting = getSystemSetting(args.name, userId);
525 return packageSettingForQuery(setting, normalizedProjection);
526 } else {
527 return getAllSystemSettings(userId, projection);
Svetoslav683914b2015-01-15 14:22:26 -0800528 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700529 }
Svetoslav683914b2015-01-15 14:22:26 -0800530
Svetoslav7ec28e82015-05-20 17:01:10 -0700531 default: {
532 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700533 }
534 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700535 }
536
Nicholas Sauer72500532018-11-21 10:30:58 -0800537 private ArrayList<String> buildSettingsList(Cursor cursor) {
538 final ArrayList<String> lines = new ArrayList<String>();
539 try {
540 while (cursor != null && cursor.moveToNext()) {
541 lines.add(cursor.getString(1) + "=" + cursor.getString(2));
542 }
543 } finally {
544 if (cursor != null) {
545 cursor.close();
546 }
547 }
548 return lines;
549 }
550
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700551 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800552 public Uri insert(Uri uri, ContentValues values) {
553 if (DEBUG) {
554 Slog.v(LOG_TAG, "insert() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700555 }
556
Svetoslav683914b2015-01-15 14:22:26 -0800557 String table = getValidTableOrThrow(uri);
Christopher Tate06efb532012-08-24 15:29:27 -0700558
Svetoslav683914b2015-01-15 14:22:26 -0800559 // If a legacy table that is gone, done.
560 if (REMOVED_LEGACY_TABLES.contains(table)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 return null;
562 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700563
Svetoslav683914b2015-01-15 14:22:26 -0800564 String name = values.getAsString(Settings.Secure.NAME);
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700565 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800566 return null;
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700567 }
568
Svetoslav683914b2015-01-15 14:22:26 -0800569 String value = values.getAsString(Settings.Secure.VALUE);
570
Svetoslav7ec28e82015-05-20 17:01:10 -0700571 switch (table) {
572 case TABLE_GLOBAL: {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800573 if (insertGlobalSetting(name, value, null, false,
574 UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700575 return Uri.withAppendedPath(Settings.Global.CONTENT_URI, name);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700576 }
Svetoslav7ec28e82015-05-20 17:01:10 -0700577 } break;
578
579 case TABLE_SECURE: {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800580 if (insertSecureSetting(name, value, null, false,
581 UserHandle.getCallingUserId(), false)) {
Svetoslav7ec28e82015-05-20 17:01:10 -0700582 return Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name);
583 }
584 } break;
585
586 case TABLE_SYSTEM: {
587 if (insertSystemSetting(name, value, UserHandle.getCallingUserId())) {
588 return Uri.withAppendedPath(Settings.System.CONTENT_URI, name);
589 }
590 } break;
591
592 default: {
593 throw new IllegalArgumentException("Bad Uri path:" + uri);
Christopher Tatec221d2b2012-10-03 18:33:52 -0700594 }
595 }
596
Svetoslav683914b2015-01-15 14:22:26 -0800597 return null;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700598 }
599
600 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800601 public int bulkInsert(Uri uri, ContentValues[] allValues) {
602 if (DEBUG) {
603 Slog.v(LOG_TAG, "bulkInsert() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700605
Svetoslav683914b2015-01-15 14:22:26 -0800606 int insertionCount = 0;
607 final int valuesCount = allValues.length;
608 for (int i = 0; i < valuesCount; i++) {
609 ContentValues values = allValues[i];
610 if (insert(uri, values) != null) {
611 insertionCount++;
612 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700613 }
Svetoslav683914b2015-01-15 14:22:26 -0800614
615 return insertionCount;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700616 }
617
618 @Override
Svetoslav683914b2015-01-15 14:22:26 -0800619 public int delete(Uri uri, String where, String[] whereArgs) {
620 if (DEBUG) {
621 Slog.v(LOG_TAG, "delete() for user: " + UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700623
Svetoslav683914b2015-01-15 14:22:26 -0800624 Arguments args = new Arguments(uri, where, whereArgs, false);
625
626 // If a legacy table that is gone, done.
627 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
628 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700629 }
Svetoslav683914b2015-01-15 14:22:26 -0800630
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700631 if (!isKeyValid(args.name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800632 return 0;
Dianne Hackborn8d051722014-10-01 14:59:58 -0700633 }
Svetoslav683914b2015-01-15 14:22:26 -0800634
Svetoslav7ec28e82015-05-20 17:01:10 -0700635 switch (args.table) {
636 case TABLE_GLOBAL: {
637 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700638 return deleteGlobalSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700639 }
Svetoslav683914b2015-01-15 14:22:26 -0800640
Svetoslav7ec28e82015-05-20 17:01:10 -0700641 case TABLE_SECURE: {
642 final int userId = UserHandle.getCallingUserId();
Svet Ganov53a441c2016-04-19 19:38:00 -0700643 return deleteSecureSetting(args.name, userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700644 }
Svetoslav683914b2015-01-15 14:22:26 -0800645
Svetoslav7ec28e82015-05-20 17:01:10 -0700646 case TABLE_SYSTEM: {
647 final int userId = UserHandle.getCallingUserId();
648 return deleteSystemSetting(args.name, userId) ? 1 : 0;
649 }
650
651 default: {
652 throw new IllegalArgumentException("Bad Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800653 }
Dianne Hackborn8d051722014-10-01 14:59:58 -0700654 }
Svetoslav683914b2015-01-15 14:22:26 -0800655 }
656
657 @Override
658 public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
659 if (DEBUG) {
660 Slog.v(LOG_TAG, "update() for user: " + UserHandle.getCallingUserId());
Christopher Tate06efb532012-08-24 15:29:27 -0700661 }
Svetoslav683914b2015-01-15 14:22:26 -0800662
663 Arguments args = new Arguments(uri, where, whereArgs, false);
664
665 // If a legacy table that is gone, done.
666 if (REMOVED_LEGACY_TABLES.contains(args.table)) {
667 return 0;
668 }
669
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700670 String name = values.getAsString(Settings.Secure.NAME);
671 if (!isKeyValid(name)) {
Svetoslav683914b2015-01-15 14:22:26 -0800672 return 0;
673 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700674 String value = values.getAsString(Settings.Secure.VALUE);
Svetoslav683914b2015-01-15 14:22:26 -0800675
Svetoslav7ec28e82015-05-20 17:01:10 -0700676 switch (args.table) {
677 case TABLE_GLOBAL: {
678 final int userId = UserHandle.getCallingUserId();
Svetoslav Ganove080da92016-12-21 17:10:35 -0800679 return updateGlobalSetting(args.name, value, null, false,
680 userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700681 }
Svetoslav683914b2015-01-15 14:22:26 -0800682
Svetoslav7ec28e82015-05-20 17:01:10 -0700683 case TABLE_SECURE: {
684 final int userId = UserHandle.getCallingUserId();
Svetoslav Ganove080da92016-12-21 17:10:35 -0800685 return updateSecureSetting(args.name, value, null, false,
686 userId, false) ? 1 : 0;
Svetoslav7ec28e82015-05-20 17:01:10 -0700687 }
Svetoslav683914b2015-01-15 14:22:26 -0800688
Svetoslav7ec28e82015-05-20 17:01:10 -0700689 case TABLE_SYSTEM: {
690 final int userId = UserHandle.getCallingUserId();
691 return updateSystemSetting(args.name, value, userId) ? 1 : 0;
692 }
Svetoslav683914b2015-01-15 14:22:26 -0800693
Svetoslav7ec28e82015-05-20 17:01:10 -0700694 default: {
695 throw new IllegalArgumentException("Invalid Uri path:" + uri);
Svetoslav683914b2015-01-15 14:22:26 -0800696 }
697 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700698 }
699
700 @Override
701 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
Robin Lee2ab02e22016-07-28 18:41:23 +0100702 final int userId = getUserIdFromUri(uri, UserHandle.getCallingUserId());
703 if (userId != UserHandle.getCallingUserId()) {
704 getContext().enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS,
705 "Access files from the settings of another user");
706 }
707 uri = ContentProvider.getUriWithoutUserId(uri);
708
Andre Lago3fa139c2016-08-04 13:53:44 +0100709 final String cacheRingtoneSetting;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700710 final String cacheName;
711 if (Settings.System.RINGTONE_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100712 cacheRingtoneSetting = Settings.System.RINGTONE;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700713 cacheName = Settings.System.RINGTONE_CACHE;
714 } else if (Settings.System.NOTIFICATION_SOUND_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100715 cacheRingtoneSetting = Settings.System.NOTIFICATION_SOUND;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700716 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
717 } else if (Settings.System.ALARM_ALERT_CACHE_URI.equals(uri)) {
Andre Lago3fa139c2016-08-04 13:53:44 +0100718 cacheRingtoneSetting = Settings.System.ALARM_ALERT;
Jeff Sharkey413573a2016-02-22 17:52:45 -0700719 cacheName = Settings.System.ALARM_ALERT_CACHE;
720 } else {
721 throw new FileNotFoundException("Direct file access no longer supported; "
722 + "ringtone playback is available through android.media.Ringtone");
723 }
724
Andre Lago3fa139c2016-08-04 13:53:44 +0100725 int actualCacheOwner;
726 // Redirect cache to parent if ringtone setting is owned by profile parent
727 synchronized (mLock) {
728 actualCacheOwner = resolveOwningUserIdForSystemSettingLocked(userId,
729 cacheRingtoneSetting);
730 }
731 final File cacheFile = new File(getRingtoneCacheDir(actualCacheOwner), cacheName);
Jeff Sharkey413573a2016-02-22 17:52:45 -0700732 return ParcelFileDescriptor.open(cacheFile, ParcelFileDescriptor.parseMode(mode));
733 }
734
735 private File getRingtoneCacheDir(int userId) {
736 final File cacheDir = new File(Environment.getDataSystemDeDirectory(userId), "ringtones");
737 cacheDir.mkdir();
738 SELinux.restorecon(cacheDir);
739 return cacheDir;
Marco Nelissen69f593c2009-07-28 09:55:04 -0700740 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800741
Eugene Suslad72c3972016-12-27 15:49:30 -0800742 /**
743 * Dump all settings as a proto buf.
744 *
745 * @param fd The file to dump to
746 */
747 void dumpProto(@NonNull FileDescriptor fd) {
748 ProtoOutputStream proto = new ProtoOutputStream(fd);
749
750 synchronized (mLock) {
751 SettingsProtoDumpUtil.dumpProtoLocked(mSettingsRegistry, proto);
Eugene Suslad72c3972016-12-27 15:49:30 -0800752 }
753
754 proto.flush();
755 }
756
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700757 public void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
Svetoslavb505ccc2015-02-17 12:41:04 -0800758 synchronized (mLock) {
759 final long identity = Binder.clearCallingIdentity();
760 try {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700761 SparseBooleanArray users = mSettingsRegistry.getKnownUsersLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800762 final int userCount = users.size();
763 for (int i = 0; i < userCount; i++) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700764 dumpForUserLocked(users.keyAt(i), pw);
Svetoslavb505ccc2015-02-17 12:41:04 -0800765 }
766 } finally {
767 Binder.restoreCallingIdentity(identity);
768 }
769 }
770 }
771
Andreas Gampeb58893072018-09-05 16:52:31 -0700772 @GuardedBy("mLock")
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700773 private void dumpForUserLocked(int userId, PrintWriter pw) {
Xiaohui Chen43765b72015-08-31 10:57:33 -0700774 if (userId == UserHandle.USER_SYSTEM) {
Matt Pape1b31a332018-10-17 09:58:28 -0700775 pw.println("CONFIG SETTINGS (user " + userId + ")");
776 SettingsState configSettings = mSettingsRegistry.getSettingsLocked(
777 SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM);
778 if (configSettings != null) {
779 dumpSettingsLocked(configSettings, pw);
780 pw.println();
781 configSettings.dumpHistoricalOperations(pw);
782 }
783
Svetoslavb505ccc2015-02-17 12:41:04 -0800784 pw.println("GLOBAL SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700785 SettingsState globalSettings = mSettingsRegistry.getSettingsLocked(
786 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700787 if (globalSettings != null) {
788 dumpSettingsLocked(globalSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800789 pw.println();
790 globalSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700791 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800792 }
793
794 pw.println("SECURE SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700795 SettingsState secureSettings = mSettingsRegistry.getSettingsLocked(
796 SETTINGS_TYPE_SECURE, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700797 if (secureSettings != null) {
798 dumpSettingsLocked(secureSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800799 pw.println();
800 secureSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700801 }
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700802
Svetoslavb505ccc2015-02-17 12:41:04 -0800803 pw.println("SYSTEM SETTINGS (user " + userId + ")");
Svetoslav Ganova340bfd2016-08-02 18:24:49 -0700804 SettingsState systemSettings = mSettingsRegistry.getSettingsLocked(
805 SETTINGS_TYPE_SYSTEM, userId);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700806 if (systemSettings != null) {
807 dumpSettingsLocked(systemSettings, pw);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800808 pw.println();
809 systemSettings.dumpHistoricalOperations(pw);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700810 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800811 }
812
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700813 private void dumpSettingsLocked(SettingsState settingsState, PrintWriter pw) {
814 List<String> names = settingsState.getSettingNamesLocked();
Svetoslavb505ccc2015-02-17 12:41:04 -0800815
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700816 final int nameCount = names.size();
Svetoslavb505ccc2015-02-17 12:41:04 -0800817
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700818 for (int i = 0; i < nameCount; i++) {
819 String name = names.get(i);
820 Setting setting = settingsState.getSettingLocked(name);
821 pw.print("_id:"); pw.print(toDumpString(setting.getId()));
822 pw.print(" name:"); pw.print(toDumpString(name));
823 if (setting.getPackageName() != null) {
Svetoslav Ganove080da92016-12-21 17:10:35 -0800824 pw.print(" pkg:"); pw.print(setting.getPackageName());
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700825 }
826 pw.print(" value:"); pw.print(toDumpString(setting.getValue()));
Svetoslav Ganove080da92016-12-21 17:10:35 -0800827 if (setting.getDefaultValue() != null) {
828 pw.print(" default:"); pw.print(setting.getDefaultValue());
Eugene Suslad72c3972016-12-27 15:49:30 -0800829 pw.print(" defaultSystemSet:"); pw.print(setting.isDefaultFromSystem());
Svetoslav Ganove080da92016-12-21 17:10:35 -0800830 }
831 if (setting.getTag() != null) {
832 pw.print(" tag:"); pw.print(setting.getTag());
833 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800834 pw.println();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -0700835 }
Svetoslavb505ccc2015-02-17 12:41:04 -0800836 }
837
Svetoslav7e0683b2015-08-03 16:02:52 -0700838 private static String toDumpString(String s) {
Makoto Onuki3a2c35782015-06-18 11:21:58 -0700839 if (s != null) {
840 return s;
841 }
842 return "{null}";
843 }
844
Svetoslav683914b2015-01-15 14:22:26 -0800845 private void registerBroadcastReceivers() {
846 IntentFilter userFilter = new IntentFilter();
847 userFilter.addAction(Intent.ACTION_USER_REMOVED);
848 userFilter.addAction(Intent.ACTION_USER_STOPPED);
849
850 getContext().registerReceiver(new BroadcastReceiver() {
851 @Override
852 public void onReceive(Context context, Intent intent) {
853 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
Xiaohui Chen43765b72015-08-31 10:57:33 -0700854 UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -0800855
856 switch (intent.getAction()) {
857 case Intent.ACTION_USER_REMOVED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700858 synchronized (mLock) {
859 mSettingsRegistry.removeUserStateLocked(userId, true);
860 }
Svetoslav683914b2015-01-15 14:22:26 -0800861 } break;
862
863 case Intent.ACTION_USER_STOPPED: {
Svet Ganov53a441c2016-04-19 19:38:00 -0700864 synchronized (mLock) {
865 mSettingsRegistry.removeUserStateLocked(userId, false);
866 }
Svetoslav683914b2015-01-15 14:22:26 -0800867 } break;
868 }
869 }
870 }, userFilter);
871
872 PackageMonitor monitor = new PackageMonitor() {
873 @Override
874 public void onPackageRemoved(String packageName, int uid) {
875 synchronized (mLock) {
Zimuzoc56192c2018-07-25 10:40:01 +0100876 mSettingsRegistry.removeSettingsForPackageLocked(packageName,
Svetoslav683914b2015-01-15 14:22:26 -0800877 UserHandle.getUserId(uid));
878 }
879 }
Mark Rathjend891f012017-01-19 04:10:37 +0000880
881 @Override
882 public void onUidRemoved(int uid) {
883 synchronized (mLock) {
884 mSettingsRegistry.onUidRemovedLocked(uid);
885 }
886 }
Zimuzoc56192c2018-07-25 10:40:01 +0100887
888 @Override
889 public void onPackageDataCleared(String packageName, int uid) {
890 synchronized (mLock) {
891 mSettingsRegistry.removeSettingsForPackageLocked(packageName,
892 UserHandle.getUserId(uid));
893 }
894 }
Svetoslav683914b2015-01-15 14:22:26 -0800895 };
896
897 // package changes
898 monitor.register(getContext(), BackgroundThread.getHandler().getLooper(),
899 UserHandle.ALL, true);
900 }
901
Svet Ganov53a441c2016-04-19 19:38:00 -0700902 private void startWatchingUserRestrictionChanges() {
903 // TODO: The current design of settings looking different based on user restrictions
904 // should be reworked to keep them separate and system code should check the setting
905 // first followed by checking the user restriction before performing an operation.
906 UserManagerInternal userManager = LocalServices.getService(UserManagerInternal.class);
907 userManager.addUserRestrictionsListener((int userId, Bundle newRestrictions,
908 Bundle prevRestrictions) -> {
Irina Dumitrescue3696872019-01-09 16:07:59 +0000909 Set<String> changedRestrictions = getRestrictionDiff(prevRestrictions, newRestrictions);
Svet Ganov53a441c2016-04-19 19:38:00 -0700910 // We are changing the settings affected by restrictions to their current
911 // value with a forced update to ensure that all cross profile dependencies
912 // are taken into account. Also make sure the settings update to.. the same
913 // value passes the security checks, so clear binder calling id.
Irina Dumitrescue3696872019-01-09 16:07:59 +0000914 if (changedRestrictions.contains(UserManager.DISALLOW_SHARE_LOCATION)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700915 final long identity = Binder.clearCallingIdentity();
916 try {
917 synchronized (mLock) {
918 Setting setting = getSecureSetting(
Soonil Nagarkar4ee3ac22019-02-08 19:19:24 -0800919 Settings.Secure.LOCATION_MODE, userId);
920 updateSecureSetting(Settings.Secure.LOCATION_MODE,
921 setting != null ? setting.getValue() : null, null,
922 true, userId, true);
923 setting = getSecureSetting(
Svet Ganov53a441c2016-04-19 19:38:00 -0700924 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, userId);
925 updateSecureSetting(Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800926 setting != null ? setting.getValue() : null, null,
927 true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700928 }
929 } finally {
930 Binder.restoreCallingIdentity(identity);
931 }
932 }
Irina Dumitrescue3696872019-01-09 16:07:59 +0000933 if (changedRestrictions.contains(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)
934 || changedRestrictions.contains(
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100935 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700936 final long identity = Binder.clearCallingIdentity();
937 try {
938 synchronized (mLock) {
939 Setting setting = getGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800940 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700941 updateGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800942 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700943 }
944 } finally {
945 Binder.restoreCallingIdentity(identity);
946 }
947 }
Irina Dumitrescue3696872019-01-09 16:07:59 +0000948 if (changedRestrictions.contains(UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700949 final long identity = Binder.clearCallingIdentity();
950 try {
951 synchronized (mLock) {
952 Setting setting = getGlobalSetting(Settings.Global.ADB_ENABLED);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800953 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700954 updateGlobalSetting(Settings.Global.ADB_ENABLED,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800955 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700956 }
957 } finally {
958 Binder.restoreCallingIdentity(identity);
959 }
960 }
Irina Dumitrescue3696872019-01-09 16:07:59 +0000961 if (changedRestrictions.contains(UserManager.ENSURE_VERIFY_APPS)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700962 final long identity = Binder.clearCallingIdentity();
963 try {
964 synchronized (mLock) {
965 Setting enable = getGlobalSetting(
966 Settings.Global.PACKAGE_VERIFIER_ENABLE);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800967 String enableValue = enable != null ? enable.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700968 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_ENABLE,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800969 enableValue, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700970 Setting include = getGlobalSetting(
971 Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800972 String includeValue = include != null ? include.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700973 updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800974 includeValue, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700975 }
976 } finally {
977 Binder.restoreCallingIdentity(identity);
978 }
979 }
Irina Dumitrescue3696872019-01-09 16:07:59 +0000980 if (changedRestrictions.contains(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700981 final long identity = Binder.clearCallingIdentity();
982 try {
983 synchronized (mLock) {
984 Setting setting = getGlobalSetting(
985 Settings.Global.PREFERRED_NETWORK_MODE);
Svetoslav Ganove080da92016-12-21 17:10:35 -0800986 String value = setting != null ? setting.getValue() : null;
Svet Ganov53a441c2016-04-19 19:38:00 -0700987 updateGlobalSetting(Settings.Global.PREFERRED_NETWORK_MODE,
Svetoslav Ganove080da92016-12-21 17:10:35 -0800988 value, null, true, userId, true);
Svet Ganov53a441c2016-04-19 19:38:00 -0700989 }
990 } finally {
991 Binder.restoreCallingIdentity(identity);
992 }
993 }
994 });
995 }
996
Irina Dumitrescue3696872019-01-09 16:07:59 +0000997 private static Set<String> getRestrictionDiff(Bundle prevRestrictions, Bundle newRestrictions) {
998 Set<String> restrictionNames = Sets.newArraySet();
999 restrictionNames.addAll(prevRestrictions.keySet());
1000 restrictionNames.addAll(newRestrictions.keySet());
1001 Set<String> diff = Sets.newArraySet();
1002 for (String restrictionName : restrictionNames) {
1003 if (prevRestrictions.getBoolean(restrictionName) != newRestrictions.getBoolean(
1004 restrictionName)) {
1005 diff.add(restrictionName);
1006 }
1007 }
1008 return diff;
1009 }
1010
Matt Pape1b31a332018-10-17 09:58:28 -07001011 private Setting getConfigSetting(String name) {
1012 if (DEBUG) {
1013 Slog.v(LOG_TAG, "getConfigSetting(" + name + ")");
1014 }
1015
Stanislav Zholnin55799502019-03-08 14:54:55 +00001016 DeviceConfig.enforceReadPermission(getContext(), /*namespace=*/name.split("/")[0]);
Matt Pape1b31a332018-10-17 09:58:28 -07001017
1018 // Get the value.
1019 synchronized (mLock) {
1020 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_CONFIG,
1021 UserHandle.USER_SYSTEM, name);
1022 }
1023 }
1024
Matt Papec1323dc2018-12-11 12:32:42 -08001025 private boolean insertConfigSetting(String name, String value, boolean makeDefault) {
Matt Pape1b31a332018-10-17 09:58:28 -07001026 if (DEBUG) {
1027 Slog.v(LOG_TAG, "insertConfigSetting(" + name + ", " + value + ", "
Matt Papec1323dc2018-12-11 12:32:42 -08001028 + makeDefault + ")");
Matt Pape1b31a332018-10-17 09:58:28 -07001029 }
Matt Papec1323dc2018-12-11 12:32:42 -08001030 return mutateConfigSetting(name, value, null, makeDefault,
1031 MUTATION_OPERATION_INSERT, 0);
Matt Pape1b31a332018-10-17 09:58:28 -07001032 }
1033
Matt Papec1323dc2018-12-11 12:32:42 -08001034 private boolean deleteConfigSetting(String name) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08001035 if (DEBUG) {
Matt Papec1323dc2018-12-11 12:32:42 -08001036 Slog.v(LOG_TAG, "deleteConfigSetting(" + name + ")");
Matt Pape6bfc62e2018-11-28 13:16:03 -08001037 }
Matt Papec1323dc2018-12-11 12:32:42 -08001038 return mutateConfigSetting(name, null, null, false,
1039 MUTATION_OPERATION_DELETE, 0);
Matt Pape6bfc62e2018-11-28 13:16:03 -08001040 }
1041
Matt Papec1323dc2018-12-11 12:32:42 -08001042 private void resetConfigSetting(int mode, String prefix) {
Matt Pape1b31a332018-10-17 09:58:28 -07001043 if (DEBUG) {
Matt Papec1323dc2018-12-11 12:32:42 -08001044 Slog.v(LOG_TAG, "resetConfigSetting(" + mode + ", " + prefix + ")");
Matt Pape1b31a332018-10-17 09:58:28 -07001045 }
Matt Papec1323dc2018-12-11 12:32:42 -08001046 mutateConfigSetting(null, null, prefix, false,
1047 MUTATION_OPERATION_RESET, mode);
Matt Pape1b31a332018-10-17 09:58:28 -07001048 }
1049
Matt Pape6bfc62e2018-11-28 13:16:03 -08001050 private boolean mutateConfigSetting(String name, String value, String prefix,
Matt Papec1323dc2018-12-11 12:32:42 -08001051 boolean makeDefault, int operation, int mode) {
Stanislav Zholnin5a25a842019-03-13 14:43:26 +00001052 enforceWritePermission(Manifest.permission.WRITE_DEVICE_CONFIG);
Matt Pape1b31a332018-10-17 09:58:28 -07001053
Matt Pape1b31a332018-10-17 09:58:28 -07001054 // Perform the mutation.
1055 synchronized (mLock) {
1056 switch (operation) {
1057 case MUTATION_OPERATION_INSERT: {
1058 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_CONFIG,
Matt Papec1323dc2018-12-11 12:32:42 -08001059 UserHandle.USER_SYSTEM, name, value, null, makeDefault, true,
Matt Pape7b1c6cd2019-01-04 08:10:41 -08001060 resolveCallingPackage(), false, null);
Matt Pape1b31a332018-10-17 09:58:28 -07001061 }
1062
Matt Pape6bfc62e2018-11-28 13:16:03 -08001063 case MUTATION_OPERATION_DELETE: {
1064 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_CONFIG,
Matt Papec1323dc2018-12-11 12:32:42 -08001065 UserHandle.USER_SYSTEM, name, false, null);
Matt Pape6bfc62e2018-11-28 13:16:03 -08001066 }
1067
Matt Pape1b31a332018-10-17 09:58:28 -07001068 case MUTATION_OPERATION_RESET: {
1069 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_CONFIG,
Matt Pape7b1c6cd2019-01-04 08:10:41 -08001070 UserHandle.USER_SYSTEM, resolveCallingPackage(), mode, null, prefix);
Matt Pape1b31a332018-10-17 09:58:28 -07001071 } return true;
1072 }
1073 }
1074
1075 return false;
1076 }
1077
Matt Pape6bfc62e2018-11-28 13:16:03 -08001078 private Map<String, String> getAllConfigFlags(@Nullable String prefix) {
1079 if (DEBUG) {
1080 Slog.v(LOG_TAG, "getAllConfigFlags() for " + prefix);
1081 }
1082
1083 synchronized (mLock) {
1084 // Get the settings.
1085 SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
1086 SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM);
1087
1088 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_CONFIG,
1089 UserHandle.USER_SYSTEM);
1090
1091 final int nameCount = names.size();
1092 Map<String, String> flagsToValues = new HashMap<>(names.size());
1093
1094 for (int i = 0; i < nameCount; i++) {
1095 String name = names.get(i);
1096 Setting setting = settingsState.getSettingLocked(name);
1097 if (prefix == null || setting.getName().startsWith(prefix)) {
1098 flagsToValues.put(setting.getName(), setting.getValue());
1099 }
1100 }
1101
1102 return flagsToValues;
1103 }
1104 }
1105
Svetoslav7ec28e82015-05-20 17:01:10 -07001106 private Cursor getAllGlobalSettings(String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001107 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001108 Slog.v(LOG_TAG, "getAllGlobalSettings()");
Svetoslav683914b2015-01-15 14:22:26 -08001109 }
1110
Svetoslav7ec28e82015-05-20 17:01:10 -07001111 synchronized (mLock) {
1112 // Get the settings.
1113 SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001114 SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001115
Chad Brubaker97bccee2017-01-05 15:51:41 -08001116 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_GLOBAL,
1117 UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08001118
Svetoslav7ec28e82015-05-20 17:01:10 -07001119 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001120
Svetoslav7ec28e82015-05-20 17:01:10 -07001121 String[] normalizedProjection = normalizeProjection(projection);
1122 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001123
Svetoslav7ec28e82015-05-20 17:01:10 -07001124 // Anyone can get the global settings, so no security checks.
1125 for (int i = 0; i < nameCount; i++) {
1126 String name = names.get(i);
1127 Setting setting = settingsState.getSettingLocked(name);
1128 appendSettingToCursor(result, setting);
1129 }
1130
1131 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001132 }
Svetoslav683914b2015-01-15 14:22:26 -08001133 }
1134
Svetoslav7ec28e82015-05-20 17:01:10 -07001135 private Setting getGlobalSetting(String name) {
Svetoslav683914b2015-01-15 14:22:26 -08001136 if (DEBUG) {
1137 Slog.v(LOG_TAG, "getGlobalSetting(" + name + ")");
1138 }
1139
Chad Brubakera6830e72017-04-28 17:34:36 -07001140 // Ensure the caller can access the setting.
1141 enforceSettingReadable(name, SETTINGS_TYPE_GLOBAL, UserHandle.getCallingUserId());
1142
Svetoslav683914b2015-01-15 14:22:26 -08001143 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001144 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -07001145 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_GLOBAL,
Xiaohui Chen43765b72015-08-31 10:57:33 -07001146 UserHandle.USER_SYSTEM, name);
Svetoslav683914b2015-01-15 14:22:26 -08001147 }
Svetoslav683914b2015-01-15 14:22:26 -08001148 }
1149
Svetoslav Ganove080da92016-12-21 17:10:35 -08001150 private boolean updateGlobalSetting(String name, String value, String tag,
1151 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001152 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001153 Slog.v(LOG_TAG, "updateGlobalSetting(" + name + ", " + value + ", "
1154 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1155 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001156 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001157 return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId,
1158 MUTATION_OPERATION_UPDATE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001159 }
1160
Svetoslav Ganove080da92016-12-21 17:10:35 -08001161 private boolean insertGlobalSetting(String name, String value, String tag,
1162 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001163 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001164 Slog.v(LOG_TAG, "insertGlobalSetting(" + name + ", " + value + ", "
1165 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1166 + ", " + forceNotify + ")");
Svetoslav7ec28e82015-05-20 17:01:10 -07001167 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001168 return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId,
1169 MUTATION_OPERATION_INSERT, forceNotify, 0);
Svetoslav7ec28e82015-05-20 17:01:10 -07001170 }
1171
Svet Ganov53a441c2016-04-19 19:38:00 -07001172 private boolean deleteGlobalSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001173 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001174 Slog.v(LOG_TAG, "deleteGlobalSetting(" + name + ", " + requestingUserId
1175 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001176 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001177 return mutateGlobalSetting(name, null, null, false, requestingUserId,
1178 MUTATION_OPERATION_DELETE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001179 }
1180
Svetoslav Ganove080da92016-12-21 17:10:35 -08001181 private void resetGlobalSetting(int requestingUserId, int mode, String tag) {
1182 if (DEBUG) {
1183 Slog.v(LOG_TAG, "resetGlobalSetting(" + requestingUserId + ", "
1184 + mode + ", " + tag + ")");
1185 }
1186 mutateGlobalSetting(null, null, tag, false, requestingUserId,
1187 MUTATION_OPERATION_RESET, false, mode);
1188 }
1189
1190 private boolean mutateGlobalSetting(String name, String value, String tag,
1191 boolean makeDefault, int requestingUserId, int operation, boolean forceNotify,
1192 int mode) {
Svetoslav683914b2015-01-15 14:22:26 -08001193 // Make sure the caller can change the settings - treated as secure.
1194 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
1195
Svetoslav683914b2015-01-15 14:22:26 -08001196 // Resolve the userId on whose behalf the call is made.
1197 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1198
Makoto Onuki28da2e32015-11-20 11:30:44 -08001199 // If this is a setting that is currently restricted for this user, do not allow
1200 // unrestricting changes.
yuemingw1d13eae2018-01-30 17:27:54 +00001201 if (name != null && mUserManagerInternal.isSettingRestrictedForUser(
1202 name, callingUserId, value, Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -08001203 return false;
1204 }
1205
1206 // Perform the mutation.
Svetoslav7ec28e82015-05-20 17:01:10 -07001207 synchronized (mLock) {
1208 switch (operation) {
1209 case MUTATION_OPERATION_INSERT: {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001210 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_GLOBAL,
1211 UserHandle.USER_SYSTEM, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001212 getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001213 }
Svetoslav683914b2015-01-15 14:22:26 -08001214
Svetoslav7ec28e82015-05-20 17:01:10 -07001215 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001216 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_GLOBAL,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001217 UserHandle.USER_SYSTEM, name, forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001218 }
Svetoslav683914b2015-01-15 14:22:26 -08001219
Svetoslav7ec28e82015-05-20 17:01:10 -07001220 case MUTATION_OPERATION_UPDATE: {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001221 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_GLOBAL,
1222 UserHandle.USER_SYSTEM, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001223 getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001224 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001225
1226 case MUTATION_OPERATION_RESET: {
1227 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_GLOBAL,
1228 UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag);
1229 } return true;
Svetoslav683914b2015-01-15 14:22:26 -08001230 }
1231 }
1232
1233 return false;
1234 }
1235
Christopher Tateb218e762017-04-05 16:34:07 -07001236 private PackageInfo getCallingPackageInfo(int userId) {
1237 try {
1238 return mPackageManager.getPackageInfo(getCallingPackage(),
1239 PackageManager.GET_SIGNATURES, userId);
1240 } catch (RemoteException e) {
1241 throw new IllegalStateException("Package " + getCallingPackage() + " doesn't exist");
1242 }
1243 }
1244
Svetoslav7ec28e82015-05-20 17:01:10 -07001245 private Cursor getAllSecureSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001246 if (DEBUG) {
1247 Slog.v(LOG_TAG, "getAllSecureSettings(" + userId + ")");
1248 }
1249
1250 // Resolve the userId on whose behalf the call is made.
1251 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
1252
Christopher Tateb218e762017-04-05 16:34:07 -07001253 // The relevant "calling package" userId will be the owning userId for some
1254 // profiles, and we can't do the lookup inside our [lock held] loop, so work out
1255 // up front who the effective "new SSAID" user ID for that settings name will be.
1256 final int ssaidUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
1257 Settings.Secure.ANDROID_ID);
1258 final PackageInfo ssaidCallingPkg = getCallingPackageInfo(ssaidUserId);
1259
Svetoslav7ec28e82015-05-20 17:01:10 -07001260 synchronized (mLock) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001261 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SECURE, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -08001262
Svetoslav7ec28e82015-05-20 17:01:10 -07001263 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001264
Svetoslav7ec28e82015-05-20 17:01:10 -07001265 String[] normalizedProjection = normalizeProjection(projection);
1266 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001267
Svetoslav7ec28e82015-05-20 17:01:10 -07001268 for (int i = 0; i < nameCount; i++) {
1269 String name = names.get(i);
1270 // Determine the owning user as some profile settings are cloned from the parent.
1271 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId,
1272 name);
Svetoslav683914b2015-01-15 14:22:26 -08001273
Alex Klyubin1991f572017-03-03 14:08:36 -08001274 if (!isSecureSettingAccessible(name, callingUserId, owningUserId)) {
1275 // This caller is not permitted to access this setting. Pretend the setting
1276 // doesn't exist.
Svetoslav Ganov83a1f7f2016-04-27 13:50:49 -07001277 continue;
Svetoslav7ec28e82015-05-20 17:01:10 -07001278 }
Svetoslav683914b2015-01-15 14:22:26 -08001279
Mark Rathjen7599f132017-01-23 14:15:54 -08001280 // As of Android O, the SSAID is read from an app-specific entry in table
Mark Rathjend891f012017-01-19 04:10:37 +00001281 // SETTINGS_FILE_SSAID, unless accessed by a system process.
1282 final Setting setting;
1283 if (isNewSsaidSetting(name)) {
Christopher Tateb218e762017-04-05 16:34:07 -07001284 setting = getSsaidSettingLocked(ssaidCallingPkg, owningUserId);
Mark Rathjend891f012017-01-19 04:10:37 +00001285 } else {
1286 setting = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE, owningUserId,
1287 name);
1288 }
Svetoslav7ec28e82015-05-20 17:01:10 -07001289 appendSettingToCursor(result, setting);
Svetoslav683914b2015-01-15 14:22:26 -08001290 }
1291
Svetoslav7ec28e82015-05-20 17:01:10 -07001292 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001293 }
Svetoslav683914b2015-01-15 14:22:26 -08001294 }
1295
Svetoslav7ec28e82015-05-20 17:01:10 -07001296 private Setting getSecureSetting(String name, int requestingUserId) {
Makoto Onuki0000d322017-11-28 16:31:47 -08001297 return getSecureSetting(name, requestingUserId, /*enableOverride=*/ false);
1298 }
1299
1300 private Setting getSecureSetting(String name, int requestingUserId, boolean enableOverride) {
Svetoslav683914b2015-01-15 14:22:26 -08001301 if (DEBUG) {
1302 Slog.v(LOG_TAG, "getSecureSetting(" + name + ", " + requestingUserId + ")");
1303 }
1304
1305 // Resolve the userId on whose behalf the call is made.
1306 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1307
Chad Brubakera6830e72017-04-28 17:34:36 -07001308 // Ensure the caller can access the setting.
1309 enforceSettingReadable(name, SETTINGS_TYPE_SECURE, UserHandle.getCallingUserId());
1310
Svetoslav683914b2015-01-15 14:22:26 -08001311 // Determine the owning user as some profile settings are cloned from the parent.
1312 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
1313
Alex Klyubin1991f572017-03-03 14:08:36 -08001314 if (!isSecureSettingAccessible(name, callingUserId, owningUserId)) {
1315 // This caller is not permitted to access this setting. Pretend the setting doesn't
1316 // exist.
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07001317 SettingsState settings = mSettingsRegistry.getSettingsLocked(SETTINGS_TYPE_SECURE,
1318 owningUserId);
1319 return settings != null ? settings.getNullSetting() : null;
Svetoslav683914b2015-01-15 14:22:26 -08001320 }
1321
Christopher Tateb218e762017-04-05 16:34:07 -07001322 // As of Android O, the SSAID is read from an app-specific entry in table
1323 // SETTINGS_FILE_SSAID, unless accessed by a system process.
1324 if (isNewSsaidSetting(name)) {
1325 PackageInfo callingPkg = getCallingPackageInfo(owningUserId);
1326 synchronized (mLock) {
1327 return getSsaidSettingLocked(callingPkg, owningUserId);
Mark Rathjend891f012017-01-19 04:10:37 +00001328 }
Christopher Tateb218e762017-04-05 16:34:07 -07001329 }
Makoto Onuki0000d322017-11-28 16:31:47 -08001330 if (enableOverride) {
Kweku Adams5e0052b2019-02-22 15:17:52 -08001331 if (Secure.LOCATION_MODE.equals(name)) {
1332 final Setting overridden = getLocationModeSetting(owningUserId);
Makoto Onuki0000d322017-11-28 16:31:47 -08001333 if (overridden != null) {
1334 return overridden;
1335 }
1336 }
1337 }
Mark Rathjend891f012017-01-19 04:10:37 +00001338
Christopher Tateb218e762017-04-05 16:34:07 -07001339 // Not the SSAID; do a straight lookup
1340 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -07001341 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav7ec28e82015-05-20 17:01:10 -07001342 owningUserId, name);
1343 }
Svetoslav683914b2015-01-15 14:22:26 -08001344 }
1345
Mark Rathjend891f012017-01-19 04:10:37 +00001346 private boolean isNewSsaidSetting(String name) {
1347 return Settings.Secure.ANDROID_ID.equals(name)
1348 && UserHandle.getAppId(Binder.getCallingUid()) >= Process.FIRST_APPLICATION_UID;
1349 }
1350
Andreas Gampeb58893072018-09-05 16:52:31 -07001351 @GuardedBy("mLock")
Christopher Tateb218e762017-04-05 16:34:07 -07001352 private Setting getSsaidSettingLocked(PackageInfo callingPkg, int owningUserId) {
Mark Rathjend891f012017-01-19 04:10:37 +00001353 // Get uid of caller (key) used to store ssaid value
1354 String name = Integer.toString(
1355 UserHandle.getUid(owningUserId, UserHandle.getAppId(Binder.getCallingUid())));
1356
1357 if (DEBUG) {
1358 Slog.v(LOG_TAG, "getSsaidSettingLocked(" + name + "," + owningUserId + ")");
1359 }
1360
1361 // Retrieve the ssaid from the table if present.
1362 final Setting ssaid = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SSAID, owningUserId,
1363 name);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001364 // If the app is an Instant App use its stored SSAID instead of our own.
1365 final String instantSsaid;
1366 final long token = Binder.clearCallingIdentity();
1367 try {
1368 instantSsaid = mPackageManager.getInstantAppAndroidId(callingPkg.packageName,
1369 owningUserId);
1370 } catch (RemoteException e) {
1371 Slog.e(LOG_TAG, "Failed to get Instant App Android ID", e);
1372 return null;
1373 } finally {
1374 Binder.restoreCallingIdentity(token);
1375 }
Svet Ganov96c99462017-05-05 14:27:13 -07001376
1377 final SettingsState ssaidSettings = mSettingsRegistry.getSettingsLocked(
1378 SETTINGS_TYPE_SSAID, owningUserId);
1379
Chad Brubaker0d277a72017-04-12 16:56:53 -07001380 if (instantSsaid != null) {
1381 // Use the stored value if it is still valid.
1382 if (ssaid != null && instantSsaid.equals(ssaid.getValue())) {
Svet Ganov96c99462017-05-05 14:27:13 -07001383 return mascaradeSsaidSetting(ssaidSettings, ssaid);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001384 }
1385 // The value has changed, update the stored value.
Chad Brubaker0d277a72017-04-12 16:56:53 -07001386 final boolean success = ssaidSettings.insertSettingLocked(name, instantSsaid, null,
1387 true, callingPkg.packageName);
1388 if (!success) {
1389 throw new IllegalStateException("Failed to update instant app android id");
1390 }
Svet Ganov96c99462017-05-05 14:27:13 -07001391 Setting setting = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SSAID,
1392 owningUserId, name);
1393 return mascaradeSsaidSetting(ssaidSettings, setting);
Chad Brubaker0d277a72017-04-12 16:56:53 -07001394 }
Mark Rathjend891f012017-01-19 04:10:37 +00001395
1396 // Lazy initialize ssaid if not yet present in ssaid table.
Mark Rathjenea617592017-01-18 23:03:41 -08001397 if (ssaid == null || ssaid.isNull() || ssaid.getValue() == null) {
Svet Ganov96c99462017-05-05 14:27:13 -07001398 Setting setting = mSettingsRegistry.generateSsaidLocked(callingPkg, owningUserId);
1399 return mascaradeSsaidSetting(ssaidSettings, setting);
Mark Rathjend891f012017-01-19 04:10:37 +00001400 }
1401
Svet Ganov96c99462017-05-05 14:27:13 -07001402 return mascaradeSsaidSetting(ssaidSettings, ssaid);
1403 }
1404
1405 private Setting mascaradeSsaidSetting(SettingsState settingsState, Setting ssaidSetting) {
1406 // SSAID settings are located in a dedicated table for internal bookkeeping
1407 // but for the world they reside in the secure table, so adjust the key here.
1408 // We have a special name when looking it up but want the world to see it as
1409 // "android_id".
1410 if (ssaidSetting != null) {
1411 return settingsState.new Setting(ssaidSetting) {
1412 @Override
1413 public int getKey() {
1414 final int userId = getUserIdFromKey(super.getKey());
1415 return makeKey(SETTINGS_TYPE_SECURE, userId);
1416 }
1417
1418 @Override
1419 public String getName() {
1420 return Settings.Secure.ANDROID_ID;
1421 }
1422 };
1423 }
1424 return null;
Mark Rathjend891f012017-01-19 04:10:37 +00001425 }
1426
Kweku Adams5e0052b2019-02-22 15:17:52 -08001427 private Setting getLocationModeSetting(int owningUserId) {
Makoto Onuki0000d322017-11-28 16:31:47 -08001428 synchronized (mLock) {
1429 final Setting setting = getGlobalSetting(
1430 Global.LOCATION_GLOBAL_KILL_SWITCH);
1431 if (!"1".equals(setting.getValue())) {
1432 return null;
1433 }
1434 // Global kill-switch is enabled. Return an empty value.
1435 final SettingsState settingsState = mSettingsRegistry.getSettingsLocked(
1436 SETTINGS_TYPE_SECURE, owningUserId);
1437 return settingsState.new Setting(
Kweku Adams5e0052b2019-02-22 15:17:52 -08001438 Secure.LOCATION_MODE,
Makoto Onuki0000d322017-11-28 16:31:47 -08001439 "", // value
1440 "", // tag
1441 "", // default value
1442 "", // package name
1443 false, // from system
1444 "0" // id
1445 ) {
1446 @Override
1447 public boolean update(String value, boolean setDefault, String packageName,
1448 String tag, boolean forceNonSystemPackage) {
Kweku Adams5e0052b2019-02-22 15:17:52 -08001449 Slog.wtf(LOG_TAG, "update shouldn't be called on this instance.");
Makoto Onuki0000d322017-11-28 16:31:47 -08001450 return false;
1451 }
1452 };
1453 }
1454 }
1455
Svetoslav Ganove080da92016-12-21 17:10:35 -08001456 private boolean insertSecureSetting(String name, String value, String tag,
1457 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001458 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001459 Slog.v(LOG_TAG, "insertSecureSetting(" + name + ", " + value + ", "
Svetoslav Ganove080da92016-12-21 17:10:35 -08001460 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1461 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001462 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001463 return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId,
1464 MUTATION_OPERATION_INSERT, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001465 }
1466
Svet Ganov53a441c2016-04-19 19:38:00 -07001467 private boolean deleteSecureSetting(String name, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001468 if (DEBUG) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001469 Slog.v(LOG_TAG, "deleteSecureSetting(" + name + ", " + requestingUserId
1470 + ", " + forceNotify + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001471 }
1472
Svetoslav Ganove080da92016-12-21 17:10:35 -08001473 return mutateSecureSetting(name, null, null, false, requestingUserId,
1474 MUTATION_OPERATION_DELETE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001475 }
1476
Svetoslav Ganove080da92016-12-21 17:10:35 -08001477 private boolean updateSecureSetting(String name, String value, String tag,
1478 boolean makeDefault, int requestingUserId, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001479 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001480 Slog.v(LOG_TAG, "updateSecureSetting(" + name + ", " + value + ", "
Svetoslav Ganove080da92016-12-21 17:10:35 -08001481 + ", " + tag + ", " + makeDefault + ", " + requestingUserId
1482 + ", " + forceNotify +")");
Svetoslav683914b2015-01-15 14:22:26 -08001483 }
1484
Svetoslav Ganove080da92016-12-21 17:10:35 -08001485 return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId,
1486 MUTATION_OPERATION_UPDATE, forceNotify, 0);
Svetoslav683914b2015-01-15 14:22:26 -08001487 }
1488
Svetoslav Ganove080da92016-12-21 17:10:35 -08001489 private void resetSecureSetting(int requestingUserId, int mode, String tag) {
1490 if (DEBUG) {
1491 Slog.v(LOG_TAG, "resetSecureSetting(" + requestingUserId + ", "
1492 + mode + ", " + tag + ")");
1493 }
1494
1495 mutateSecureSetting(null, null, tag, false, requestingUserId,
1496 MUTATION_OPERATION_RESET, false, mode);
1497 }
1498
1499 private boolean mutateSecureSetting(String name, String value, String tag,
1500 boolean makeDefault, int requestingUserId, int operation, boolean forceNotify,
1501 int mode) {
Svetoslav683914b2015-01-15 14:22:26 -08001502 // Make sure the caller can change the settings.
1503 enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS);
1504
Svetoslav683914b2015-01-15 14:22:26 -08001505 // Resolve the userId on whose behalf the call is made.
1506 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1507
Makoto Onuki28da2e32015-11-20 11:30:44 -08001508 // If this is a setting that is currently restricted for this user, do not allow
1509 // unrestricting changes.
yuemingw1d13eae2018-01-30 17:27:54 +00001510 if (name != null && mUserManagerInternal.isSettingRestrictedForUser(
1511 name, callingUserId, value, Binder.getCallingUid())) {
Svetoslav683914b2015-01-15 14:22:26 -08001512 return false;
1513 }
1514
1515 // Determine the owning user as some profile settings are cloned from the parent.
1516 final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name);
1517
1518 // Only the owning user can change the setting.
1519 if (owningUserId != callingUserId) {
1520 return false;
1521 }
1522
1523 // Special cases for location providers (sigh).
1524 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08001525 return updateLocationProvidersAllowedLocked(value, tag, owningUserId, makeDefault,
1526 forceNotify);
Svetoslav683914b2015-01-15 14:22:26 -08001527 }
1528
1529 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001530 synchronized (mLock) {
1531 switch (operation) {
1532 case MUTATION_OPERATION_INSERT: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001533 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001534 owningUserId, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001535 getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001536 }
Svetoslav683914b2015-01-15 14:22:26 -08001537
Svetoslav7ec28e82015-05-20 17:01:10 -07001538 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001539 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001540 owningUserId, name, forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001541 }
Svetoslav683914b2015-01-15 14:22:26 -08001542
Svetoslav7ec28e82015-05-20 17:01:10 -07001543 case MUTATION_OPERATION_UPDATE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001544 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SECURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08001545 owningUserId, name, value, tag, makeDefault,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001546 getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav7ec28e82015-05-20 17:01:10 -07001547 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08001548
1549 case MUTATION_OPERATION_RESET: {
1550 mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_SECURE,
1551 UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag);
1552 } return true;
Svetoslav683914b2015-01-15 14:22:26 -08001553 }
1554 }
1555
1556 return false;
1557 }
1558
Svetoslav7ec28e82015-05-20 17:01:10 -07001559 private Cursor getAllSystemSettings(int userId, String[] projection) {
Svetoslav683914b2015-01-15 14:22:26 -08001560 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001561 Slog.v(LOG_TAG, "getAllSecureSystem(" + userId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001562 }
1563
1564 // Resolve the userId on whose behalf the call is made.
1565 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId);
1566
Svetoslav7ec28e82015-05-20 17:01:10 -07001567 synchronized (mLock) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001568 List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SYSTEM, callingUserId);
Svetoslav683914b2015-01-15 14:22:26 -08001569
Svetoslav7ec28e82015-05-20 17:01:10 -07001570 final int nameCount = names.size();
Svetoslav683914b2015-01-15 14:22:26 -08001571
Svetoslav7ec28e82015-05-20 17:01:10 -07001572 String[] normalizedProjection = normalizeProjection(projection);
1573 MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount);
Svetoslav683914b2015-01-15 14:22:26 -08001574
Svetoslav7ec28e82015-05-20 17:01:10 -07001575 for (int i = 0; i < nameCount; i++) {
1576 String name = names.get(i);
Svetoslav683914b2015-01-15 14:22:26 -08001577
Svetoslav7ec28e82015-05-20 17:01:10 -07001578 // Determine the owning user as some profile settings are cloned from the parent.
1579 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId,
1580 name);
Svetoslav683914b2015-01-15 14:22:26 -08001581
Svetoslav7ec28e82015-05-20 17:01:10 -07001582 Setting setting = mSettingsRegistry.getSettingLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07001583 SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001584 appendSettingToCursor(result, setting);
1585 }
1586
1587 return result;
Svetoslav683914b2015-01-15 14:22:26 -08001588 }
Svetoslav683914b2015-01-15 14:22:26 -08001589 }
1590
Svetoslav7ec28e82015-05-20 17:01:10 -07001591 private Setting getSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001592 if (DEBUG) {
1593 Slog.v(LOG_TAG, "getSystemSetting(" + name + ", " + requestingUserId + ")");
1594 }
1595
1596 // Resolve the userId on whose behalf the call is made.
1597 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId);
1598
Chad Brubakera6830e72017-04-28 17:34:36 -07001599 // Ensure the caller can access the setting.
1600 enforceSettingReadable(name, SETTINGS_TYPE_SYSTEM, UserHandle.getCallingUserId());
Chad Brubaker97bccee2017-01-05 15:51:41 -08001601
Svetoslav683914b2015-01-15 14:22:26 -08001602 // Determine the owning user as some profile settings are cloned from the parent.
1603 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1604
1605 // Get the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001606 synchronized (mLock) {
Chad Brubakera6830e72017-04-28 17:34:36 -07001607 return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SYSTEM, owningUserId, name);
Svetoslav7ec28e82015-05-20 17:01:10 -07001608 }
Svetoslav683914b2015-01-15 14:22:26 -08001609 }
1610
Svetoslav7ec28e82015-05-20 17:01:10 -07001611 private boolean insertSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001612 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001613 Slog.v(LOG_TAG, "insertSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001614 + requestingUserId + ")");
1615 }
1616
Svetoslav7ec28e82015-05-20 17:01:10 -07001617 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT);
Svetoslav683914b2015-01-15 14:22:26 -08001618 }
1619
Svetoslav7ec28e82015-05-20 17:01:10 -07001620 private boolean deleteSystemSetting(String name, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001621 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001622 Slog.v(LOG_TAG, "deleteSystemSetting(" + name + ", " + requestingUserId + ")");
Svetoslav683914b2015-01-15 14:22:26 -08001623 }
1624
Svetoslav7ec28e82015-05-20 17:01:10 -07001625 return mutateSystemSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE);
Svetoslav683914b2015-01-15 14:22:26 -08001626 }
1627
Svetoslav7ec28e82015-05-20 17:01:10 -07001628 private boolean updateSystemSetting(String name, String value, int requestingUserId) {
Svetoslav683914b2015-01-15 14:22:26 -08001629 if (DEBUG) {
Svetoslav7ec28e82015-05-20 17:01:10 -07001630 Slog.v(LOG_TAG, "updateSystemSetting(" + name + ", " + value + ", "
Svetoslav683914b2015-01-15 14:22:26 -08001631 + requestingUserId + ")");
1632 }
1633
Svetoslav7ec28e82015-05-20 17:01:10 -07001634 return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE);
Svetoslav683914b2015-01-15 14:22:26 -08001635 }
1636
Svetoslav7ec28e82015-05-20 17:01:10 -07001637 private boolean mutateSystemSetting(String name, String value, int runAsUserId,
Svetoslav683914b2015-01-15 14:22:26 -08001638 int operation) {
Billy Lau6ad2d662015-07-18 00:26:58 +01001639 if (!hasWriteSecureSettingsPermission()) {
1640 // If the caller doesn't hold WRITE_SECURE_SETTINGS, we verify whether this
1641 // operation is allowed for the calling package through appops.
1642 if (!Settings.checkAndNoteWriteSettingsOperation(getContext(),
1643 Binder.getCallingUid(), getCallingPackage(), true)) {
1644 return false;
1645 }
Svetoslav683914b2015-01-15 14:22:26 -08001646 }
1647
Svetoslav683914b2015-01-15 14:22:26 -08001648 // Resolve the userId on whose behalf the call is made.
1649 final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId);
1650
yuemingw1d13eae2018-01-30 17:27:54 +00001651 if (name != null && mUserManagerInternal.isSettingRestrictedForUser(
1652 name, callingUserId, value, Binder.getCallingUid())) {
1653 return false;
1654 }
1655
Svetoslavd8d25e02015-11-20 13:09:26 -08001656 // Enforce what the calling package can mutate the system settings.
1657 enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name, callingUserId);
1658
Svetoslav683914b2015-01-15 14:22:26 -08001659 // Determine the owning user as some profile settings are cloned from the parent.
1660 final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);
1661
1662 // Only the owning user id can change the setting.
1663 if (owningUserId != callingUserId) {
1664 return false;
1665 }
1666
Jeff Sharkey413573a2016-02-22 17:52:45 -07001667 // Invalidate any relevant cache files
1668 String cacheName = null;
1669 if (Settings.System.RINGTONE.equals(name)) {
1670 cacheName = Settings.System.RINGTONE_CACHE;
1671 } else if (Settings.System.NOTIFICATION_SOUND.equals(name)) {
1672 cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
1673 } else if (Settings.System.ALARM_ALERT.equals(name)) {
1674 cacheName = Settings.System.ALARM_ALERT_CACHE;
1675 }
1676 if (cacheName != null) {
1677 final File cacheFile = new File(
Andre Lago3fa139c2016-08-04 13:53:44 +01001678 getRingtoneCacheDir(owningUserId), cacheName);
Jeff Sharkey413573a2016-02-22 17:52:45 -07001679 cacheFile.delete();
1680 }
1681
Svetoslav683914b2015-01-15 14:22:26 -08001682 // Mutate the value.
Svetoslav7ec28e82015-05-20 17:01:10 -07001683 synchronized (mLock) {
1684 switch (operation) {
1685 case MUTATION_OPERATION_INSERT: {
1686 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001687 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001688 owningUserId, name, value, null, false, getCallingPackage(),
1689 false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001690 }
1691
1692 case MUTATION_OPERATION_DELETE: {
Svet Ganov53a441c2016-04-19 19:38:00 -07001693 return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001694 owningUserId, name, false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001695 }
1696
1697 case MUTATION_OPERATION_UPDATE: {
1698 validateSystemSettingValue(name, value);
Svet Ganov53a441c2016-04-19 19:38:00 -07001699 return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SYSTEM,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08001700 owningUserId, name, value, null, false, getCallingPackage(),
1701 false, null);
Svetoslav7ec28e82015-05-20 17:01:10 -07001702 }
Svetoslav683914b2015-01-15 14:22:26 -08001703 }
1704
Svetoslav7ec28e82015-05-20 17:01:10 -07001705 return false;
Svetoslav683914b2015-01-15 14:22:26 -08001706 }
Svetoslav683914b2015-01-15 14:22:26 -08001707 }
1708
Billy Lau6ad2d662015-07-18 00:26:58 +01001709 private boolean hasWriteSecureSettingsPermission() {
Svetoslavf41334b2015-06-23 12:06:03 -07001710 // Write secure settings is a more protected permission. If caller has it we are good.
1711 if (getContext().checkCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS)
1712 == PackageManager.PERMISSION_GRANTED) {
1713 return true;
1714 }
1715
Svetoslavf41334b2015-06-23 12:06:03 -07001716 return false;
1717 }
1718
Svetoslav683914b2015-01-15 14:22:26 -08001719 private void validateSystemSettingValue(String name, String value) {
Al Suttonb0067fb2019-08-16 10:34:46 +01001720 Validator validator = Settings.System.VALIDATORS.get(name);
Svetoslav683914b2015-01-15 14:22:26 -08001721 if (validator != null && !validator.validate(value)) {
1722 throw new IllegalArgumentException("Invalid value: " + value
1723 + " for setting: " + name);
1724 }
1725 }
1726
Alex Klyubin1991f572017-03-03 14:08:36 -08001727 /**
1728 * Returns {@code true} if the specified secure setting should be accessible to the caller.
1729 */
1730 private boolean isSecureSettingAccessible(String name, int callingUserId,
1731 int owningUserId) {
1732 // Special case for location (sigh).
1733 // This check is not inside the name-based checks below because this method performs checks
1734 // only if the calling user ID is not the same as the owning user ID.
1735 if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) {
1736 return false;
1737 }
1738
1739 switch (name) {
1740 case "bluetooth_address":
1741 // BluetoothManagerService for some reason stores the Android's Bluetooth MAC
1742 // address in this secure setting. Secure settings can normally be read by any app,
1743 // which thus enables them to bypass the recently introduced restrictions on access
1744 // to device identifiers.
1745 // To mitigate this we make this setting available only to callers privileged to see
1746 // this device's MAC addresses, same as through public API
1747 // BluetoothAdapter.getAddress() (see BluetoothManagerService for details).
1748 return getContext().checkCallingOrSelfPermission(
1749 Manifest.permission.LOCAL_MAC_ADDRESS) == PackageManager.PERMISSION_GRANTED;
1750 default:
1751 return true;
1752 }
1753 }
1754
Svetoslav683914b2015-01-15 14:22:26 -08001755 private boolean isLocationProvidersAllowedRestricted(String name, int callingUserId,
1756 int owningUserId) {
1757 // Optimization - location providers are restricted only for managed profiles.
1758 if (callingUserId == owningUserId) {
1759 return false;
1760 }
1761 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)
1762 && mUserManager.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION,
1763 new UserHandle(callingUserId))) {
1764 return true;
1765 }
1766 return false;
1767 }
1768
Svetoslav683914b2015-01-15 14:22:26 -08001769 private int resolveOwningUserIdForSecureSettingLocked(int userId, String setting) {
1770 return resolveOwningUserIdLocked(userId, sSecureCloneToManagedSettings, setting);
1771 }
1772
1773 private int resolveOwningUserIdForSystemSettingLocked(int userId, String setting) {
Andre Lago3fa139c2016-08-04 13:53:44 +01001774 final int parentId;
1775 // Resolves dependency if setting has a dependency and the calling user has a parent
1776 if (sSystemCloneFromParentOnDependency.containsKey(setting)
1777 && (parentId = getGroupParentLocked(userId)) != userId) {
1778 // The setting has a dependency and the profile has a parent
1779 String dependency = sSystemCloneFromParentOnDependency.get(setting);
Chad Brubaker97bccee2017-01-05 15:51:41 -08001780 // Lookup the dependency setting as ourselves, some callers may not have access to it.
1781 final long token = Binder.clearCallingIdentity();
1782 try {
1783 Setting settingObj = getSecureSetting(dependency, userId);
1784 if (settingObj != null && settingObj.getValue().equals("1")) {
1785 return parentId;
1786 }
1787 } finally {
1788 Binder.restoreCallingIdentity(token);
Andre Lago3fa139c2016-08-04 13:53:44 +01001789 }
1790 }
Svetoslav683914b2015-01-15 14:22:26 -08001791 return resolveOwningUserIdLocked(userId, sSystemCloneToManagedSettings, setting);
1792 }
1793
1794 private int resolveOwningUserIdLocked(int userId, Set<String> keys, String name) {
1795 final int parentId = getGroupParentLocked(userId);
1796 if (parentId != userId && keys.contains(name)) {
1797 return parentId;
1798 }
1799 return userId;
1800 }
1801
Svetoslavf41334b2015-06-23 12:06:03 -07001802 private void enforceRestrictedSystemSettingsMutationForCallingPackage(int operation,
Xiaohui Chen43765b72015-08-31 10:57:33 -07001803 String name, int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001804 // System/root/shell can mutate whatever secure settings they want.
1805 final int callingUid = Binder.getCallingUid();
Svetoslav Ganove080da92016-12-21 17:10:35 -08001806 final int appId = UserHandle.getAppId(callingUid);
1807 if (appId == android.os.Process.SYSTEM_UID
1808 || appId == Process.SHELL_UID
1809 || appId == Process.ROOT_UID) {
Svetoslav683914b2015-01-15 14:22:26 -08001810 return;
1811 }
1812
1813 switch (operation) {
1814 case MUTATION_OPERATION_INSERT:
1815 // Insert updates.
1816 case MUTATION_OPERATION_UPDATE: {
1817 if (Settings.System.PUBLIC_SETTINGS.contains(name)) {
1818 return;
1819 }
1820
1821 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001822 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001823
1824 // Privileged apps can do whatever they want.
1825 if ((packageInfo.applicationInfo.privateFlags
1826 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1827 return;
1828 }
1829
1830 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1831 packageInfo.applicationInfo.targetSdkVersion, name);
1832 } break;
1833
1834 case MUTATION_OPERATION_DELETE: {
1835 if (Settings.System.PUBLIC_SETTINGS.contains(name)
1836 || Settings.System.PRIVATE_SETTINGS.contains(name)) {
1837 throw new IllegalArgumentException("You cannot delete system defined"
1838 + " secure settings.");
1839 }
1840
1841 // The calling package is already verified.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001842 PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId);
Svetoslav683914b2015-01-15 14:22:26 -08001843
1844 // Privileged apps can do whatever they want.
1845 if ((packageInfo.applicationInfo.privateFlags &
1846 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
1847 return;
1848 }
1849
1850 warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
1851 packageInfo.applicationInfo.targetSdkVersion, name);
1852 } break;
1853 }
1854 }
1855
Todd Kennedybe0b8892017-02-15 14:13:52 -08001856 private Set<String> getInstantAppAccessibleSettings(int settingsType) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001857 switch (settingsType) {
1858 case SETTINGS_TYPE_GLOBAL:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001859 return Settings.Global.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001860 case SETTINGS_TYPE_SECURE:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001861 return Settings.Secure.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001862 case SETTINGS_TYPE_SYSTEM:
Todd Kennedybe0b8892017-02-15 14:13:52 -08001863 return Settings.System.INSTANT_APP_SETTINGS;
Chad Brubaker97bccee2017-01-05 15:51:41 -08001864 default:
1865 throw new IllegalArgumentException("Invalid settings type: " + settingsType);
1866 }
1867 }
1868
Chad Brubaker20e0dc32017-04-28 18:24:55 -07001869 private Set<String> getOverlayInstantAppAccessibleSettings(int settingsType) {
1870 switch (settingsType) {
1871 case SETTINGS_TYPE_GLOBAL:
1872 return OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS;
1873 case SETTINGS_TYPE_SYSTEM:
1874 return OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS;
1875 case SETTINGS_TYPE_SECURE:
1876 return OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS;
1877 default:
1878 throw new IllegalArgumentException("Invalid settings type: " + settingsType);
1879 }
1880 }
1881
Andreas Gampeb58893072018-09-05 16:52:31 -07001882 @GuardedBy("mLock")
Chad Brubaker97bccee2017-01-05 15:51:41 -08001883 private List<String> getSettingsNamesLocked(int settingsType, int userId) {
Chad Brubakerb6108d62017-12-23 20:06:44 -08001884 // Don't enforce the instant app whitelist for now -- its too prone to unintended breakage
1885 // in the current form.
1886 return mSettingsRegistry.getSettingsNamesLocked(settingsType, userId);
Chad Brubaker97bccee2017-01-05 15:51:41 -08001887 }
1888
Chad Brubakera6830e72017-04-28 17:34:36 -07001889 private void enforceSettingReadable(String settingName, int settingsType, int userId) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001890 if (UserHandle.getAppId(Binder.getCallingUid()) < Process.FIRST_APPLICATION_UID) {
1891 return;
1892 }
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001893 ApplicationInfo ai = getCallingApplicationInfoOrThrow();
Svetoslav Ganov096d3042017-01-30 16:34:13 -08001894 if (!ai.isInstantApp()) {
Chad Brubaker97bccee2017-01-05 15:51:41 -08001895 return;
1896 }
Chad Brubaker20e0dc32017-04-28 18:24:55 -07001897 if (!getInstantAppAccessibleSettings(settingsType).contains(settingName)
1898 && !getOverlayInstantAppAccessibleSettings(settingsType).contains(settingName)) {
Chad Brubakerb6108d62017-12-23 20:06:44 -08001899 // Don't enforce the instant app whitelist for now -- its too prone to unintended
1900 // breakage in the current form.
1901 Slog.w(LOG_TAG, "Instant App " + ai.packageName
1902 + " trying to access unexposed setting, this will be an error in the future.");
Chad Brubaker97bccee2017-01-05 15:51:41 -08001903 }
1904 }
1905
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001906 private ApplicationInfo getCallingApplicationInfoOrThrow() {
1907 // We always use the callingUid for this lookup. This means that if hypothetically an
1908 // app was installed in user A with cross user and in user B as an Instant App
1909 // the app in A would be able to see all the settings in user B. However since cross
1910 // user is a system permission and the app must be uninstalled in B and then installed as
1911 // an Instant App that situation is not realistic or supported.
Chad Brubaker97bccee2017-01-05 15:51:41 -08001912 ApplicationInfo ai = null;
1913 try {
Chad Brubakerf0fa8532017-02-23 10:45:20 -08001914 ai = mPackageManager.getApplicationInfo(getCallingPackage(), 0
1915 , UserHandle.getCallingUserId());
Chad Brubaker97bccee2017-01-05 15:51:41 -08001916 } catch (RemoteException ignored) {
1917 }
1918 if (ai == null) {
1919 throw new IllegalStateException("Failed to lookup info for package "
1920 + getCallingPackage());
1921 }
1922 return ai;
1923 }
1924
Xiaohui Chen43765b72015-08-31 10:57:33 -07001925 private PackageInfo getCallingPackageInfoOrThrow(int userId) {
Svetoslav683914b2015-01-15 14:22:26 -08001926 try {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001927 PackageInfo packageInfo = mPackageManager.getPackageInfo(
1928 getCallingPackage(), 0, userId);
1929 if (packageInfo != null) {
1930 return packageInfo;
1931 }
Xiaohui Chen43765b72015-08-31 10:57:33 -07001932 } catch (RemoteException e) {
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001933 /* ignore */
Svetoslav683914b2015-01-15 14:22:26 -08001934 }
Svetoslav Ganov67a8d352016-03-02 13:26:40 -08001935 throw new IllegalStateException("Calling package doesn't exist");
Svetoslav683914b2015-01-15 14:22:26 -08001936 }
1937
1938 private int getGroupParentLocked(int userId) {
1939 // Most frequent use case.
Xiaohui Chen43765b72015-08-31 10:57:33 -07001940 if (userId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08001941 return userId;
1942 }
1943 // We are in the same process with the user manager and the returned
1944 // user info is a cached instance, so just look up instead of cache.
1945 final long identity = Binder.clearCallingIdentity();
1946 try {
Svetoslav7ec28e82015-05-20 17:01:10 -07001947 // Just a lookup and not reentrant, so holding a lock is fine.
Svetoslav683914b2015-01-15 14:22:26 -08001948 UserInfo userInfo = mUserManager.getProfileParent(userId);
1949 return (userInfo != null) ? userInfo.id : userId;
1950 } finally {
1951 Binder.restoreCallingIdentity(identity);
1952 }
1953 }
1954
Svetoslav683914b2015-01-15 14:22:26 -08001955 private void enforceWritePermission(String permission) {
1956 if (getContext().checkCallingOrSelfPermission(permission)
1957 != PackageManager.PERMISSION_GRANTED) {
1958 throw new SecurityException("Permission denial: writing to settings requires:"
1959 + permission);
1960 }
1961 }
1962
1963 /*
1964 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
1965 * This setting contains a list of the currently enabled location providers.
1966 * But helper functions in android.providers.Settings can enable or disable
1967 * a single provider by using a "+" or "-" prefix before the provider name.
1968 *
yuemingw1d13eae2018-01-30 17:27:54 +00001969 * <p>See also {@link com.android.server.pm.UserRestrictionsUtils#isSettingRestrictedForUser()}.
1970 * If DISALLOW_SHARE_LOCATION is set, the said method will only allow values with
1971 * the "-" prefix.
Makoto Onuki28da2e32015-11-20 11:30:44 -08001972 *
Svetoslav683914b2015-01-15 14:22:26 -08001973 * @returns whether the enabled location providers changed.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001974 */
Andreas Gampeb58893072018-09-05 16:52:31 -07001975 @GuardedBy("mLock")
Svetoslav Ganove080da92016-12-21 17:10:35 -08001976 private boolean updateLocationProvidersAllowedLocked(String value, String tag,
1977 int owningUserId, boolean makeDefault, boolean forceNotify) {
Svetoslav683914b2015-01-15 14:22:26 -08001978 if (TextUtils.isEmpty(value)) {
1979 return false;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001980 }
Maggie83e03f52018-03-16 12:22:20 -07001981 Setting oldSetting = getSecureSetting(
1982 Settings.Secure.LOCATION_PROVIDERS_ALLOWED, owningUserId);
1983 if (oldSetting == null) {
Svetoslav683914b2015-01-15 14:22:26 -08001984 return false;
1985 }
Maggie83e03f52018-03-16 12:22:20 -07001986 String oldProviders = oldSetting.getValue();
1987 List<String> oldProvidersList = TextUtils.isEmpty(oldProviders)
1988 ? new ArrayList<>() : new ArrayList<>(Arrays.asList(oldProviders.split(",")));
1989 Set<String> newProvidersSet = new ArraySet<>();
1990 newProvidersSet.addAll(oldProvidersList);
Svetoslav683914b2015-01-15 14:22:26 -08001991
Maggie83e03f52018-03-16 12:22:20 -07001992 String[] providerUpdates = value.split(",");
1993 boolean inputError = false;
1994 for (String provider : providerUpdates) {
1995 // do not update location_providers_allowed when input is invalid
1996 if (TextUtils.isEmpty(provider)) {
1997 inputError = true;
1998 break;
Svetoslav683914b2015-01-15 14:22:26 -08001999 }
Maggie83e03f52018-03-16 12:22:20 -07002000 final char prefix = provider.charAt(0);
2001 // do not update location_providers_allowed when input is invalid
2002 if (prefix != '+' && prefix != '-') {
2003 inputError = true;
2004 break;
Svetoslav683914b2015-01-15 14:22:26 -08002005 }
Maggie83e03f52018-03-16 12:22:20 -07002006 // skip prefix
2007 provider = provider.substring(1);
2008 if (prefix == '+') {
2009 newProvidersSet.add(provider);
2010 } else if (prefix == '-') {
2011 newProvidersSet.remove(provider);
Svetoslav683914b2015-01-15 14:22:26 -08002012 }
Maggie83e03f52018-03-16 12:22:20 -07002013 }
2014 String newProviders = TextUtils.join(",", newProvidersSet.toArray());
2015 if (inputError == true || newProviders.equals(oldProviders)) {
Svetoslav683914b2015-01-15 14:22:26 -08002016 // nothing changed, so no need to update the database
Svet Ganov53a441c2016-04-19 19:38:00 -07002017 if (forceNotify) {
Maggie83e03f52018-03-16 12:22:20 -07002018 mSettingsRegistry.notifyForSettingsChange(
2019 makeKey(SETTINGS_TYPE_SECURE, owningUserId),
Svet Ganov53a441c2016-04-19 19:38:00 -07002020 Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
2021 }
Svetoslav683914b2015-01-15 14:22:26 -08002022 return false;
2023 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002024 return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE,
Maggie83e03f52018-03-16 12:22:20 -07002025 owningUserId, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, newProviders, tag,
2026 makeDefault, getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS);
Svetoslav683914b2015-01-15 14:22:26 -08002027 }
2028
Svetoslav683914b2015-01-15 14:22:26 -08002029 private static void warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(
2030 int targetSdkVersion, String name) {
2031 // If the app targets Lollipop MR1 or older SDK we warn, otherwise crash.
2032 if (targetSdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) {
2033 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
2034 Slog.w(LOG_TAG, "You shouldn't not change private system settings."
2035 + " This will soon become an error.");
2036 } else {
2037 Slog.w(LOG_TAG, "You shouldn't keep your settings in the secure settings."
2038 + " This will soon become an error.");
2039 }
2040 } else {
2041 if (Settings.System.PRIVATE_SETTINGS.contains(name)) {
2042 throw new IllegalArgumentException("You cannot change private secure settings.");
2043 } else {
2044 throw new IllegalArgumentException("You cannot keep your settings in"
2045 + " the secure settings.");
2046 }
2047 }
2048 }
2049
2050 private static int resolveCallingUserIdEnforcingPermissionsLocked(int requestingUserId) {
2051 if (requestingUserId == UserHandle.getCallingUserId()) {
2052 return requestingUserId;
2053 }
2054 return ActivityManager.handleIncomingUser(Binder.getCallingPid(),
2055 Binder.getCallingUid(), requestingUserId, false, true,
2056 "get/set setting for user", null);
2057 }
2058
Svet Ganov53a441c2016-04-19 19:38:00 -07002059 private Bundle packageValueForCallResult(Setting setting,
2060 boolean trackingGeneration) {
2061 if (!trackingGeneration) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002062 if (setting == null || setting.isNull()) {
Svet Ganov53a441c2016-04-19 19:38:00 -07002063 return NULL_SETTING_BUNDLE;
2064 }
2065 return Bundle.forPair(Settings.NameValueTable.VALUE, setting.getValue());
Svetoslav683914b2015-01-15 14:22:26 -08002066 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002067 Bundle result = new Bundle();
2068 result.putString(Settings.NameValueTable.VALUE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002069 !setting.isNull() ? setting.getValue() : null);
Svet Ganov96c99462017-05-05 14:27:13 -07002070
Svetoslav Ganove080da92016-12-21 17:10:35 -08002071 mSettingsRegistry.mGenerationRegistry.addGenerationData(result, setting.getKey());
Svet Ganov53a441c2016-04-19 19:38:00 -07002072 return result;
Svetoslav683914b2015-01-15 14:22:26 -08002073 }
2074
2075 private static int getRequestingUserId(Bundle args) {
2076 final int callingUserId = UserHandle.getCallingUserId();
2077 return (args != null) ? args.getInt(Settings.CALL_METHOD_USER_KEY, callingUserId)
2078 : callingUserId;
2079 }
2080
Svet Ganov53a441c2016-04-19 19:38:00 -07002081 private boolean isTrackingGeneration(Bundle args) {
2082 return args != null && args.containsKey(Settings.CALL_METHOD_TRACK_GENERATION_KEY);
2083 }
2084
Svetoslav683914b2015-01-15 14:22:26 -08002085 private static String getSettingValue(Bundle args) {
2086 return (args != null) ? args.getString(Settings.NameValueTable.VALUE) : null;
2087 }
2088
Svetoslav Ganove080da92016-12-21 17:10:35 -08002089 private static String getSettingTag(Bundle args) {
2090 return (args != null) ? args.getString(Settings.CALL_METHOD_TAG_KEY) : null;
2091 }
2092
Matt Pape6bfc62e2018-11-28 13:16:03 -08002093 private static String getSettingPrefix(Bundle args) {
2094 String prefix = (args != null) ? args.getString(Settings.CALL_METHOD_PREFIX_KEY) : null;
2095 // Append '/' to ensure we only match properties with this exact prefix.
2096 // i.e. "foo" should match "foo/property" but not "foobar/property"
2097 return prefix != null ? prefix + "/" : null;
2098 }
2099
Svetoslav Ganove080da92016-12-21 17:10:35 -08002100 private static boolean getSettingMakeDefault(Bundle args) {
2101 return (args != null) && args.getBoolean(Settings.CALL_METHOD_MAKE_DEFAULT_KEY);
2102 }
2103
2104 private static int getResetModeEnforcingPermission(Bundle args) {
2105 final int mode = (args != null) ? args.getInt(Settings.CALL_METHOD_RESET_MODE_KEY) : 0;
2106 switch (mode) {
2107 case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: {
2108 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
2109 throw new SecurityException("Only system, shell/root on a "
2110 + "debuggable build can reset to untrusted defaults");
2111 }
2112 return mode;
2113 }
2114 case Settings.RESET_MODE_UNTRUSTED_CHANGES: {
2115 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
2116 throw new SecurityException("Only system, shell/root on a "
2117 + "debuggable build can reset untrusted changes");
2118 }
2119 return mode;
2120 }
2121 case Settings.RESET_MODE_TRUSTED_DEFAULTS: {
2122 if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) {
2123 throw new SecurityException("Only system, shell/root on a "
2124 + "debuggable build can reset to trusted defaults");
2125 }
2126 return mode;
2127 }
2128 case Settings.RESET_MODE_PACKAGE_DEFAULTS: {
2129 return mode;
2130 }
2131 }
2132 throw new IllegalArgumentException("Invalid reset mode: " + mode);
2133 }
2134
2135 private static boolean isCallerSystemOrShellOrRootOnDebuggableBuild() {
2136 final int appId = UserHandle.getAppId(Binder.getCallingUid());
2137 return appId == SYSTEM_UID || (Build.IS_DEBUGGABLE
2138 && (appId == SHELL_UID || appId == ROOT_UID));
2139 }
2140
Svetoslav683914b2015-01-15 14:22:26 -08002141 private static String getValidTableOrThrow(Uri uri) {
2142 if (uri.getPathSegments().size() > 0) {
2143 String table = uri.getPathSegments().get(0);
2144 if (DatabaseHelper.isValidTable(table)) {
2145 return table;
2146 }
2147 throw new IllegalArgumentException("Bad root path: " + table);
2148 }
2149 throw new IllegalArgumentException("Invalid URI:" + uri);
2150 }
2151
2152 private static MatrixCursor packageSettingForQuery(Setting setting, String[] projection) {
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07002153 if (setting.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08002154 return new MatrixCursor(projection, 0);
2155 }
2156 MatrixCursor cursor = new MatrixCursor(projection, 1);
2157 appendSettingToCursor(cursor, setting);
2158 return cursor;
2159 }
2160
2161 private static String[] normalizeProjection(String[] projection) {
2162 if (projection == null) {
2163 return ALL_COLUMNS;
2164 }
2165
2166 final int columnCount = projection.length;
2167 for (int i = 0; i < columnCount; i++) {
2168 String column = projection[i];
2169 if (!ArrayUtils.contains(ALL_COLUMNS, column)) {
2170 throw new IllegalArgumentException("Invalid column: " + column);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002171 }
2172 }
2173
Svetoslav683914b2015-01-15 14:22:26 -08002174 return projection;
2175 }
2176
2177 private static void appendSettingToCursor(MatrixCursor cursor, Setting setting) {
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002178 if (setting == null || setting.isNull()) {
Suprabh Shuklac9d064a2016-04-12 18:45:34 -07002179 return;
2180 }
Svetoslav683914b2015-01-15 14:22:26 -08002181 final int columnCount = cursor.getColumnCount();
2182
2183 String[] values = new String[columnCount];
2184
2185 for (int i = 0; i < columnCount; i++) {
2186 String column = cursor.getColumnName(i);
2187
2188 switch (column) {
2189 case Settings.NameValueTable._ID: {
2190 values[i] = setting.getId();
2191 } break;
2192
2193 case Settings.NameValueTable.NAME: {
2194 values[i] = setting.getName();
2195 } break;
2196
2197 case Settings.NameValueTable.VALUE: {
2198 values[i] = setting.getValue();
2199 } break;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002200 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002201 }
2202
Svetoslav683914b2015-01-15 14:22:26 -08002203 cursor.addRow(values);
2204 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002205
Makoto Onuki3a2c35782015-06-18 11:21:58 -07002206 private static boolean isKeyValid(String key) {
2207 return !(TextUtils.isEmpty(key) || SettingsState.isBinary(key));
2208 }
2209
Matt Pape7b1c6cd2019-01-04 08:10:41 -08002210 private String resolveCallingPackage() {
2211 switch (Binder.getCallingUid()) {
2212 case Process.ROOT_UID: {
2213 return "root";
2214 }
2215
2216 case Process.SHELL_UID: {
2217 return "com.android.shell";
2218 }
2219
2220 default: {
2221 return getCallingPackage();
2222 }
2223 }
2224 }
2225
Svetoslav683914b2015-01-15 14:22:26 -08002226 private static final class Arguments {
2227 private static final Pattern WHERE_PATTERN_WITH_PARAM_NO_BRACKETS =
2228 Pattern.compile("[\\s]*name[\\s]*=[\\s]*\\?[\\s]*");
2229
2230 private static final Pattern WHERE_PATTERN_WITH_PARAM_IN_BRACKETS =
2231 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*\\?[\\s]*\\)[\\s]*");
2232
2233 private static final Pattern WHERE_PATTERN_NO_PARAM_IN_BRACKETS =
2234 Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*\\)[\\s]*");
2235
2236 private static final Pattern WHERE_PATTERN_NO_PARAM_NO_BRACKETS =
2237 Pattern.compile("[\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*");
2238
2239 public final String table;
2240 public final String name;
2241
2242 public Arguments(Uri uri, String where, String[] whereArgs, boolean supportAll) {
2243 final int segmentSize = uri.getPathSegments().size();
2244 switch (segmentSize) {
2245 case 1: {
2246 if (where != null
2247 && (WHERE_PATTERN_WITH_PARAM_NO_BRACKETS.matcher(where).matches()
2248 || WHERE_PATTERN_WITH_PARAM_IN_BRACKETS.matcher(where).matches())
2249 && whereArgs.length == 1) {
2250 name = whereArgs[0];
2251 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08002252 return;
Svetoslav683914b2015-01-15 14:22:26 -08002253 } else if (where != null
2254 && (WHERE_PATTERN_NO_PARAM_NO_BRACKETS.matcher(where).matches()
2255 || WHERE_PATTERN_NO_PARAM_IN_BRACKETS.matcher(where).matches())) {
2256 final int startIndex = Math.max(where.indexOf("'"),
2257 where.indexOf("\"")) + 1;
2258 final int endIndex = Math.max(where.lastIndexOf("'"),
2259 where.lastIndexOf("\""));
2260 name = where.substring(startIndex, endIndex);
2261 table = computeTableForSetting(uri, name);
Svetoslav28494652015-02-12 14:11:42 -08002262 return;
Svetoslav683914b2015-01-15 14:22:26 -08002263 } else if (supportAll && where == null && whereArgs == null) {
2264 name = null;
2265 table = computeTableForSetting(uri, null);
Svetoslav28494652015-02-12 14:11:42 -08002266 return;
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002267 }
Svetoslav683914b2015-01-15 14:22:26 -08002268 } break;
2269
Svetoslav28494652015-02-12 14:11:42 -08002270 case 2: {
2271 if (where == null && whereArgs == null) {
2272 name = uri.getPathSegments().get(1);
2273 table = computeTableForSetting(uri, name);
2274 return;
2275 }
2276 } break;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002277 }
Svetoslav28494652015-02-12 14:11:42 -08002278
2279 EventLogTags.writeUnsupportedSettingsQuery(
2280 uri.toSafeString(), where, Arrays.toString(whereArgs));
2281 String message = String.format( "Supported SQL:\n"
2282 + " uri content://some_table/some_property with null where and where args\n"
2283 + " uri content://some_table with query name=? and single name as arg\n"
2284 + " uri content://some_table with query name=some_name and null args\n"
2285 + " but got - uri:%1s, where:%2s whereArgs:%3s", uri, where,
2286 Arrays.toString(whereArgs));
2287 throw new IllegalArgumentException(message);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002288 }
2289
Svetoslav28494652015-02-12 14:11:42 -08002290 private static String computeTableForSetting(Uri uri, String name) {
Svetoslav683914b2015-01-15 14:22:26 -08002291 String table = getValidTableOrThrow(uri);
2292
2293 if (name != null) {
2294 if (sSystemMovedToSecureSettings.contains(name)) {
2295 table = TABLE_SECURE;
2296 }
2297
2298 if (sSystemMovedToGlobalSettings.contains(name)) {
2299 table = TABLE_GLOBAL;
2300 }
2301
2302 if (sSecureMovedToGlobalSettings.contains(name)) {
2303 table = TABLE_GLOBAL;
2304 }
2305
2306 if (sGlobalMovedToSecureSettings.contains(name)) {
2307 table = TABLE_SECURE;
2308 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002309 }
Svetoslav683914b2015-01-15 14:22:26 -08002310
2311 return table;
2312 }
2313 }
2314
2315 final class SettingsRegistry {
2316 private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid";
2317
Svetoslav683914b2015-01-15 14:22:26 -08002318 private static final String SETTINGS_FILE_GLOBAL = "settings_global.xml";
2319 private static final String SETTINGS_FILE_SYSTEM = "settings_system.xml";
2320 private static final String SETTINGS_FILE_SECURE = "settings_secure.xml";
Mark Rathjend891f012017-01-19 04:10:37 +00002321 private static final String SETTINGS_FILE_SSAID = "settings_ssaid.xml";
Matt Pape1b31a332018-10-17 09:58:28 -07002322 private static final String SETTINGS_FILE_CONFIG = "settings_config.xml";
Mark Rathjend891f012017-01-19 04:10:37 +00002323
2324 private static final String SSAID_USER_KEY = "userkey";
Svetoslav683914b2015-01-15 14:22:26 -08002325
2326 private final SparseArray<SettingsState> mSettingsStates = new SparseArray<>();
2327
Svet Ganov53a441c2016-04-19 19:38:00 -07002328 private GenerationRegistry mGenerationRegistry;
Svetoslav683914b2015-01-15 14:22:26 -08002329
Svetoslav7e0683b2015-08-03 16:02:52 -07002330 private final Handler mHandler;
2331
Svet Ganov53a441c2016-04-19 19:38:00 -07002332 private final BackupManager mBackupManager;
2333
Amith Yamasani39452022017-03-21 15:23:47 -07002334 private String mSettingsCreationBuildId;
2335
Svetoslav683914b2015-01-15 14:22:26 -08002336 public SettingsRegistry() {
Svetoslav7e0683b2015-08-03 16:02:52 -07002337 mHandler = new MyHandler(getContext().getMainLooper());
Svet Ganov53a441c2016-04-19 19:38:00 -07002338 mGenerationRegistry = new GenerationRegistry(mLock);
2339 mBackupManager = new BackupManager(getContext());
Svetoslav683914b2015-01-15 14:22:26 -08002340 migrateAllLegacySettingsIfNeeded();
Mark Rathjend891f012017-01-19 04:10:37 +00002341 syncSsaidTableOnStart();
2342 }
2343
2344 private void generateUserKeyLocked(int userId) {
2345 // Generate a random key for each user used for creating a new ssaid.
Mark Rathjen7599f132017-01-23 14:15:54 -08002346 final byte[] keyBytes = new byte[32];
Mark Rathjend891f012017-01-19 04:10:37 +00002347 final SecureRandom rand = new SecureRandom();
2348 rand.nextBytes(keyBytes);
2349
2350 // Convert to string for storage in settings table.
Neil Fuller3447fb42019-04-08 22:03:14 +01002351 final String userKey = HexEncoding.encodeToString(keyBytes, true /* upperCase */);
Mark Rathjend891f012017-01-19 04:10:37 +00002352
2353 // Store the key in the ssaid table.
2354 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
2355 final boolean success = ssaidSettings.insertSettingLocked(SSAID_USER_KEY, userKey, null,
2356 true, SettingsState.SYSTEM_PACKAGE_NAME);
2357
2358 if (!success) {
2359 throw new IllegalStateException("Ssaid settings not accessible");
2360 }
2361 }
2362
Mark Rathjen7599f132017-01-23 14:15:54 -08002363 private byte[] getLengthPrefix(byte[] data) {
2364 return ByteBuffer.allocate(4).putInt(data.length).array();
2365 }
2366
Christopher Tateb218e762017-04-05 16:34:07 -07002367 public Setting generateSsaidLocked(PackageInfo callingPkg, int userId) {
Mark Rathjend891f012017-01-19 04:10:37 +00002368 // Read the user's key from the ssaid table.
2369 Setting userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY);
Mark Rathjenea617592017-01-18 23:03:41 -08002370 if (userKeySetting == null || userKeySetting.isNull()
2371 || userKeySetting.getValue() == null) {
Mark Rathjend891f012017-01-19 04:10:37 +00002372 // Lazy initialize and store the user key.
2373 generateUserKeyLocked(userId);
2374 userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY);
Mark Rathjenea617592017-01-18 23:03:41 -08002375 if (userKeySetting == null || userKeySetting.isNull()
2376 || userKeySetting.getValue() == null) {
Mark Rathjend891f012017-01-19 04:10:37 +00002377 throw new IllegalStateException("User key not accessible");
2378 }
2379 }
2380 final String userKey = userKeySetting.getValue();
Neil Fuller3447fb42019-04-08 22:03:14 +01002381 if (userKey == null || userKey.length() % 2 != 0) {
2382 throw new IllegalStateException("User key invalid");
2383 }
Mark Rathjend891f012017-01-19 04:10:37 +00002384
2385 // Convert the user's key back to a byte array.
Neil Fuller3447fb42019-04-08 22:03:14 +01002386 final byte[] keyBytes = HexEncoding.decode(userKey);
Mark Rathjen7599f132017-01-23 14:15:54 -08002387
2388 // Validate that the key is of expected length.
2389 // Keys are currently 32 bytes, but were once 16 bytes during Android O development.
Neil Fuller3447fb42019-04-08 22:03:14 +01002390 if (keyBytes.length != 16 && keyBytes.length != 32) {
Mark Rathjend891f012017-01-19 04:10:37 +00002391 throw new IllegalStateException("User key invalid");
2392 }
2393
Mark Rathjen7599f132017-01-23 14:15:54 -08002394 final Mac m;
Mark Rathjend891f012017-01-19 04:10:37 +00002395 try {
Mark Rathjen7599f132017-01-23 14:15:54 -08002396 m = Mac.getInstance("HmacSHA256");
2397 m.init(new SecretKeySpec(keyBytes, m.getAlgorithm()));
Mark Rathjend891f012017-01-19 04:10:37 +00002398 } catch (NoSuchAlgorithmException e) {
Mark Rathjen7599f132017-01-23 14:15:54 -08002399 throw new IllegalStateException("HmacSHA256 is not available", e);
2400 } catch (InvalidKeyException e) {
2401 throw new IllegalStateException("Key is corrupted", e);
Mark Rathjend891f012017-01-19 04:10:37 +00002402 }
Mark Rathjen7599f132017-01-23 14:15:54 -08002403
Mark Rathjenf42dd912017-06-05 19:04:34 -07002404 // Mac each of the developer signatures.
Christopher Tateb218e762017-04-05 16:34:07 -07002405 for (int i = 0; i < callingPkg.signatures.length; i++) {
2406 byte[] sig = callingPkg.signatures[i].toByteArray();
Mark Rathjen7599f132017-01-23 14:15:54 -08002407 m.update(getLengthPrefix(sig), 0, 4);
2408 m.update(sig);
2409 }
Mark Rathjend891f012017-01-19 04:10:37 +00002410
2411 // Convert result to a string for storage in settings table. Only want first 64 bits.
Neil Fuller3447fb42019-04-08 22:03:14 +01002412 final String ssaid = HexEncoding.encodeToString(m.doFinal(), false /* upperCase */)
2413 .substring(0, 16);
Mark Rathjend891f012017-01-19 04:10:37 +00002414
2415 // Save the ssaid in the ssaid table.
Christopher Tateb218e762017-04-05 16:34:07 -07002416 final String uid = Integer.toString(callingPkg.applicationInfo.uid);
Mark Rathjend891f012017-01-19 04:10:37 +00002417 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
2418 final boolean success = ssaidSettings.insertSettingLocked(uid, ssaid, null, true,
Mark Rathjenf42dd912017-06-05 19:04:34 -07002419 callingPkg.packageName);
Mark Rathjend891f012017-01-19 04:10:37 +00002420
2421 if (!success) {
2422 throw new IllegalStateException("Ssaid settings not accessible");
2423 }
2424
2425 return getSettingLocked(SETTINGS_TYPE_SSAID, userId, uid);
2426 }
2427
2428 public void syncSsaidTableOnStart() {
2429 synchronized (mLock) {
2430 // Verify that each user's packages and ssaid's are in sync.
2431 for (UserInfo user : mUserManager.getUsers(true)) {
2432 // Get all uids for the user's packages.
2433 final List<PackageInfo> packages;
2434 try {
Tetsutoki Shiozawaebe0e5f2018-01-17 11:07:09 +09002435 packages = mPackageManager.getInstalledPackages(
2436 PackageManager.MATCH_UNINSTALLED_PACKAGES,
2437 user.id).getList();
Mark Rathjend891f012017-01-19 04:10:37 +00002438 } catch (RemoteException e) {
2439 throw new IllegalStateException("Package manager not available");
2440 }
2441 final Set<String> appUids = new HashSet<>();
2442 for (PackageInfo info : packages) {
2443 appUids.add(Integer.toString(info.applicationInfo.uid));
2444 }
2445
2446 // Get all uids currently stored in the user's ssaid table.
2447 final Set<String> ssaidUids = new HashSet<>(
2448 getSettingsNamesLocked(SETTINGS_TYPE_SSAID, user.id));
2449 ssaidUids.remove(SSAID_USER_KEY);
2450
2451 // Perform a set difference for the appUids and ssaidUids.
2452 ssaidUids.removeAll(appUids);
2453
2454 // If there are ssaidUids left over they need to be removed from the table.
2455 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID,
2456 user.id);
2457 for (String uid : ssaidUids) {
2458 ssaidSettings.deleteSettingLocked(uid);
2459 }
2460 }
2461 }
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07002462 }
2463
Svetoslav683914b2015-01-15 14:22:26 -08002464 public List<String> getSettingsNamesLocked(int type, int userId) {
2465 final int key = makeKey(type, userId);
2466 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002467 if (settingsState == null) {
2468 return new ArrayList<String>();
2469 }
Svetoslav683914b2015-01-15 14:22:26 -08002470 return settingsState.getSettingNamesLocked();
2471 }
2472
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002473 public SparseBooleanArray getKnownUsersLocked() {
2474 SparseBooleanArray users = new SparseBooleanArray();
2475 for (int i = mSettingsStates.size()-1; i >= 0; i--) {
2476 users.put(getUserIdFromKey(mSettingsStates.keyAt(i)), true);
2477 }
2478 return users;
2479 }
2480
Kweku Adamsb0886f32017-10-31 15:32:09 -07002481 @Nullable
Svetoslav683914b2015-01-15 14:22:26 -08002482 public SettingsState getSettingsLocked(int type, int userId) {
2483 final int key = makeKey(type, userId);
2484 return peekSettingsStateLocked(key);
2485 }
2486
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002487 public boolean ensureSettingsForUserLocked(int userId) {
2488 // First make sure this user actually exists.
2489 if (mUserManager.getUserInfo(userId) == null) {
2490 Slog.wtf(LOG_TAG, "Requested user " + userId + " does not exist");
2491 return false;
2492 }
2493
Svetoslav683914b2015-01-15 14:22:26 -08002494 // Migrate the setting for this user if needed.
2495 migrateLegacySettingsForUserIfNeededLocked(userId);
2496
Matt Pape1b31a332018-10-17 09:58:28 -07002497 // Ensure config settings loaded if owner.
2498 if (userId == UserHandle.USER_SYSTEM) {
2499 final int configKey
2500 = makeKey(SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM);
2501 ensureSettingsStateLocked(configKey);
2502 }
2503
Svetoslav683914b2015-01-15 14:22:26 -08002504 // Ensure global settings loaded if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07002505 if (userId == UserHandle.USER_SYSTEM) {
2506 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002507 ensureSettingsStateLocked(globalKey);
2508 }
2509
2510 // Ensure secure settings loaded.
2511 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2512 ensureSettingsStateLocked(secureKey);
2513
2514 // Make sure the secure settings have an Android id set.
2515 SettingsState secureSettings = getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
2516 ensureSecureSettingAndroidIdSetLocked(secureSettings);
2517
2518 // Ensure system settings loaded.
2519 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2520 ensureSettingsStateLocked(systemKey);
2521
Mark Rathjend891f012017-01-19 04:10:37 +00002522 // Ensure secure settings loaded.
2523 final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId);
2524 ensureSettingsStateLocked(ssaidKey);
2525
Svetoslav683914b2015-01-15 14:22:26 -08002526 // Upgrade the settings to the latest version.
2527 UpgradeController upgrader = new UpgradeController(userId);
2528 upgrader.upgradeIfNeededLocked();
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002529 return true;
Svetoslav683914b2015-01-15 14:22:26 -08002530 }
2531
2532 private void ensureSettingsStateLocked(int key) {
2533 if (mSettingsStates.get(key) == null) {
2534 final int maxBytesPerPackage = getMaxBytesPerPackageForType(getTypeFromKey(key));
Svetoslav Ganove080da92016-12-21 17:10:35 -08002535 SettingsState settingsState = new SettingsState(getContext(), mLock,
2536 getSettingsFile(key), key, maxBytesPerPackage, mHandlerThread.getLooper());
Svetoslav683914b2015-01-15 14:22:26 -08002537 mSettingsStates.put(key, settingsState);
2538 }
2539 }
2540
2541 public void removeUserStateLocked(int userId, boolean permanently) {
2542 // We always keep the global settings in memory.
2543
2544 // Nuke system settings.
2545 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2546 final SettingsState systemSettingsState = mSettingsStates.get(systemKey);
2547 if (systemSettingsState != null) {
2548 if (permanently) {
2549 mSettingsStates.remove(systemKey);
2550 systemSettingsState.destroyLocked(null);
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002551 } else {
Svetoslav683914b2015-01-15 14:22:26 -08002552 systemSettingsState.destroyLocked(new Runnable() {
2553 @Override
2554 public void run() {
2555 mSettingsStates.remove(systemKey);
2556 }
2557 });
2558 }
2559 }
2560
2561 // Nuke secure settings.
2562 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2563 final SettingsState secureSettingsState = mSettingsStates.get(secureKey);
2564 if (secureSettingsState != null) {
2565 if (permanently) {
2566 mSettingsStates.remove(secureKey);
2567 secureSettingsState.destroyLocked(null);
2568 } else {
2569 secureSettingsState.destroyLocked(new Runnable() {
2570 @Override
2571 public void run() {
2572 mSettingsStates.remove(secureKey);
2573 }
2574 });
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08002575 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002576 }
Svet Ganov53a441c2016-04-19 19:38:00 -07002577
Mark Rathjend891f012017-01-19 04:10:37 +00002578 // Nuke ssaid settings.
2579 final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId);
2580 final SettingsState ssaidSettingsState = mSettingsStates.get(ssaidKey);
2581 if (ssaidSettingsState != null) {
2582 if (permanently) {
2583 mSettingsStates.remove(ssaidKey);
2584 ssaidSettingsState.destroyLocked(null);
2585 } else {
2586 ssaidSettingsState.destroyLocked(new Runnable() {
2587 @Override
2588 public void run() {
2589 mSettingsStates.remove(ssaidKey);
2590 }
2591 });
2592 }
2593 }
2594
Svet Ganov53a441c2016-04-19 19:38:00 -07002595 // Nuke generation tracking data
2596 mGenerationRegistry.onUserRemoved(userId);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08002597 }
2598
Svetoslav683914b2015-01-15 14:22:26 -08002599 public boolean insertSettingLocked(int type, int userId, String name, String value,
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002600 String tag, boolean makeDefault, String packageName, boolean forceNotify,
2601 Set<String> criticalSettings) {
Matt Papec1323dc2018-12-11 12:32:42 -08002602 return insertSettingLocked(type, userId, name, value, tag, makeDefault, false,
2603 packageName, forceNotify, criticalSettings);
2604 }
2605
2606 public boolean insertSettingLocked(int type, int userId, String name, String value,
2607 String tag, boolean makeDefault, boolean forceNonSystemPackage, String packageName,
2608 boolean forceNotify, Set<String> criticalSettings) {
Svetoslav683914b2015-01-15 14:22:26 -08002609 final int key = makeKey(type, userId);
2610
Svetoslav Ganove080da92016-12-21 17:10:35 -08002611 boolean success = false;
Svetoslav683914b2015-01-15 14:22:26 -08002612 SettingsState settingsState = peekSettingsStateLocked(key);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002613 if (settingsState != null) {
2614 success = settingsState.insertSettingLocked(name, value,
Matt Papec1323dc2018-12-11 12:32:42 -08002615 tag, makeDefault, forceNonSystemPackage, packageName);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002616 }
Svetoslav683914b2015-01-15 14:22:26 -08002617
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002618 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2619 settingsState.persistSyncLocked();
2620 }
2621
Svet Ganov53a441c2016-04-19 19:38:00 -07002622 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08002623 notifyForSettingsChange(key, name);
2624 }
2625 return success;
2626 }
2627
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002628 public boolean deleteSettingLocked(int type, int userId, String name, boolean forceNotify,
2629 Set<String> criticalSettings) {
Svetoslav683914b2015-01-15 14:22:26 -08002630 final int key = makeKey(type, userId);
2631
Svetoslav Ganove080da92016-12-21 17:10:35 -08002632 boolean success = false;
Svetoslav683914b2015-01-15 14:22:26 -08002633 SettingsState settingsState = peekSettingsStateLocked(key);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002634 if (settingsState != null) {
2635 success = settingsState.deleteSettingLocked(name);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002636 }
Svetoslav683914b2015-01-15 14:22:26 -08002637
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002638 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2639 settingsState.persistSyncLocked();
2640 }
2641
Svet Ganov53a441c2016-04-19 19:38:00 -07002642 if (forceNotify || success) {
Svetoslav683914b2015-01-15 14:22:26 -08002643 notifyForSettingsChange(key, name);
2644 }
2645 return success;
2646 }
2647
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002648 public boolean updateSettingLocked(int type, int userId, String name, String value,
2649 String tag, boolean makeDefault, String packageName, boolean forceNotify,
2650 Set<String> criticalSettings) {
2651 final int key = makeKey(type, userId);
2652
2653 boolean success = false;
2654 SettingsState settingsState = peekSettingsStateLocked(key);
2655 if (settingsState != null) {
2656 success = settingsState.updateSettingLocked(name, value, tag,
2657 makeDefault, packageName);
2658 }
2659
2660 if (success && criticalSettings != null && criticalSettings.contains(name)) {
2661 settingsState.persistSyncLocked();
2662 }
2663
2664 if (forceNotify || success) {
2665 notifyForSettingsChange(key, name);
2666 }
2667
2668 return success;
2669 }
2670
Svetoslav683914b2015-01-15 14:22:26 -08002671 public Setting getSettingLocked(int type, int userId, String name) {
2672 final int key = makeKey(type, userId);
2673
2674 SettingsState settingsState = peekSettingsStateLocked(key);
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002675 if (settingsState == null) {
Mark Rathjenea617592017-01-18 23:03:41 -08002676 return null;
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002677 }
Mark Rathjend891f012017-01-19 04:10:37 +00002678
2679 // getSettingLocked will return non-null result
Svetoslav683914b2015-01-15 14:22:26 -08002680 return settingsState.getSettingLocked(name);
2681 }
2682
Svetoslav Ganove080da92016-12-21 17:10:35 -08002683 public void resetSettingsLocked(int type, int userId, String packageName, int mode,
2684 String tag) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002685 resetSettingsLocked(type, userId, packageName, mode, tag, null);
2686 }
2687
2688 public void resetSettingsLocked(int type, int userId, String packageName, int mode,
2689 String tag, @Nullable String prefix) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08002690 final int key = makeKey(type, userId);
2691 SettingsState settingsState = peekSettingsStateLocked(key);
2692 if (settingsState == null) {
2693 return;
2694 }
2695
2696 switch (mode) {
2697 case Settings.RESET_MODE_PACKAGE_DEFAULTS: {
2698 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002699 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002700 Setting setting = settingsState.getSettingLocked(name);
2701 if (packageName.equals(setting.getPackageName())) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002702 if ((tag != null && !tag.equals(setting.getTag()))
2703 || (prefix != null && !setting.getName().startsWith(prefix))) {
Svetoslav Ganove080da92016-12-21 17:10:35 -08002704 continue;
2705 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002706 if (settingsState.resetSettingLocked(name)) {
2707 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002708 notifyForSettingsChange(key, name);
2709 }
2710 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002711 if (someSettingChanged) {
2712 settingsState.persistSyncLocked();
2713 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002714 }
2715 } break;
2716
2717 case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: {
2718 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002719 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002720 Setting setting = settingsState.getSettingLocked(name);
2721 if (!SettingsState.isSystemPackage(getContext(),
Matt Pape25c940d2019-03-26 12:14:33 -07002722 setting.getPackageName(), INVALID_UID, userId)) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002723 if (prefix != null && !setting.getName().startsWith(prefix)) {
2724 continue;
2725 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002726 if (settingsState.resetSettingLocked(name)) {
2727 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002728 notifyForSettingsChange(key, name);
2729 }
2730 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002731 if (someSettingChanged) {
2732 settingsState.persistSyncLocked();
2733 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002734 }
2735 } break;
2736
2737 case Settings.RESET_MODE_UNTRUSTED_CHANGES: {
2738 for (String name : settingsState.getSettingNamesLocked()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002739 boolean someSettingChanged = false;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002740 Setting setting = settingsState.getSettingLocked(name);
2741 if (!SettingsState.isSystemPackage(getContext(),
Matt Pape25c940d2019-03-26 12:14:33 -07002742 setting.getPackageName(), INVALID_UID, userId)) {
Matt Pape6bfc62e2018-11-28 13:16:03 -08002743 if (prefix != null && !setting.getName().startsWith(prefix)) {
2744 continue;
2745 }
Eugene Suslad72c3972016-12-27 15:49:30 -08002746 if (setting.isDefaultFromSystem()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002747 if (settingsState.resetSettingLocked(name)) {
2748 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002749 notifyForSettingsChange(key, name);
2750 }
2751 } else if (settingsState.deleteSettingLocked(name)) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002752 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002753 notifyForSettingsChange(key, name);
2754 }
2755 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002756 if (someSettingChanged) {
2757 settingsState.persistSyncLocked();
2758 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002759 }
2760 } break;
2761
2762 case Settings.RESET_MODE_TRUSTED_DEFAULTS: {
2763 for (String name : settingsState.getSettingNamesLocked()) {
2764 Setting setting = settingsState.getSettingLocked(name);
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002765 boolean someSettingChanged = false;
Matt Pape6bfc62e2018-11-28 13:16:03 -08002766 if (prefix != null && !setting.getName().startsWith(prefix)) {
2767 continue;
2768 }
Eugene Suslad72c3972016-12-27 15:49:30 -08002769 if (setting.isDefaultFromSystem()) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002770 if (settingsState.resetSettingLocked(name)) {
2771 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002772 notifyForSettingsChange(key, name);
2773 }
2774 } else if (settingsState.deleteSettingLocked(name)) {
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002775 someSettingChanged = true;
Svetoslav Ganove080da92016-12-21 17:10:35 -08002776 notifyForSettingsChange(key, name);
2777 }
Svetoslav Ganov5fb405b2017-01-26 22:43:09 -08002778 if (someSettingChanged) {
2779 settingsState.persistSyncLocked();
2780 }
Svetoslav Ganove080da92016-12-21 17:10:35 -08002781 }
2782 } break;
2783 }
2784 }
2785
Zimuzoc56192c2018-07-25 10:40:01 +01002786 public void removeSettingsForPackageLocked(String packageName, int userId) {
Svet Ganov8de34802015-04-27 09:33:40 -07002787 // Global and secure settings are signature protected. Apps signed
2788 // by the platform certificate are generally not uninstalled and
2789 // the main exception is tests. We trust components signed
2790 // by the platform certificate and do not do a clean up after them.
Svetoslav683914b2015-01-15 14:22:26 -08002791
2792 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2793 SettingsState systemSettings = mSettingsStates.get(systemKey);
Svet Ganov8de34802015-04-27 09:33:40 -07002794 if (systemSettings != null) {
Zimuzoc56192c2018-07-25 10:40:01 +01002795 systemSettings.removeSettingsForPackageLocked(packageName);
Svet Ganov8de34802015-04-27 09:33:40 -07002796 }
Svetoslav683914b2015-01-15 14:22:26 -08002797 }
2798
Mark Rathjend891f012017-01-19 04:10:37 +00002799 public void onUidRemovedLocked(int uid) {
2800 final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID,
2801 UserHandle.getUserId(uid));
Tony Mak24c6ab42018-01-08 14:35:34 +00002802 if (ssaidSettings != null) {
2803 ssaidSettings.deleteSettingLocked(Integer.toString(uid));
2804 }
Mark Rathjend891f012017-01-19 04:10:37 +00002805 }
2806
Kweku Adamsb0886f32017-10-31 15:32:09 -07002807 @Nullable
Svetoslav683914b2015-01-15 14:22:26 -08002808 private SettingsState peekSettingsStateLocked(int key) {
2809 SettingsState settingsState = mSettingsStates.get(key);
2810 if (settingsState != null) {
2811 return settingsState;
2812 }
2813
Dianne Hackborn32f40ee2016-10-20 15:54:14 -07002814 if (!ensureSettingsForUserLocked(getUserIdFromKey(key))) {
2815 return null;
2816 }
Svetoslav683914b2015-01-15 14:22:26 -08002817 return mSettingsStates.get(key);
2818 }
2819
2820 private void migrateAllLegacySettingsIfNeeded() {
2821 synchronized (mLock) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07002822 final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08002823 File globalFile = getSettingsFile(key);
Amith Yamasani74bf71b2017-07-19 16:49:52 -07002824 if (SettingsState.stateFileExists(globalFile)) {
Svetoslav683914b2015-01-15 14:22:26 -08002825 return;
2826 }
2827
Amith Yamasani39452022017-03-21 15:23:47 -07002828 mSettingsCreationBuildId = Build.ID;
2829
Svetoslav683914b2015-01-15 14:22:26 -08002830 final long identity = Binder.clearCallingIdentity();
2831 try {
2832 List<UserInfo> users = mUserManager.getUsers(true);
2833
2834 final int userCount = users.size();
2835 for (int i = 0; i < userCount; i++) {
2836 final int userId = users.get(i).id;
2837
2838 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
2839 SQLiteDatabase database = dbHelper.getWritableDatabase();
2840 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
2841
2842 // Upgrade to the latest version.
2843 UpgradeController upgrader = new UpgradeController(userId);
2844 upgrader.upgradeIfNeededLocked();
2845
2846 // Drop from memory if not a running user.
2847 if (!mUserManager.isUserRunning(new UserHandle(userId))) {
2848 removeUserStateLocked(userId, false);
2849 }
2850 }
2851 } finally {
2852 Binder.restoreCallingIdentity(identity);
2853 }
2854 }
2855 }
2856
2857 private void migrateLegacySettingsForUserIfNeededLocked(int userId) {
2858 // Every user has secure settings and if no file we need to migrate.
2859 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2860 File secureFile = getSettingsFile(secureKey);
Amith Yamasani74bf71b2017-07-19 16:49:52 -07002861 if (SettingsState.stateFileExists(secureFile)) {
Svetoslav683914b2015-01-15 14:22:26 -08002862 return;
2863 }
2864
2865 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId);
2866 SQLiteDatabase database = dbHelper.getWritableDatabase();
2867
2868 migrateLegacySettingsForUserLocked(dbHelper, database, userId);
2869 }
2870
2871 private void migrateLegacySettingsForUserLocked(DatabaseHelper dbHelper,
2872 SQLiteDatabase database, int userId) {
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002873 // Move over the system settings.
2874 final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId);
2875 ensureSettingsStateLocked(systemKey);
2876 SettingsState systemSettings = mSettingsStates.get(systemKey);
2877 migrateLegacySettingsLocked(systemSettings, database, TABLE_SYSTEM);
2878 systemSettings.persistSyncLocked();
Svetoslav683914b2015-01-15 14:22:26 -08002879
2880 // Move over the secure settings.
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002881 // Do this after System settings, since this is the first thing we check when deciding
2882 // to skip over migration from db to xml for a secondary user.
Svetoslav683914b2015-01-15 14:22:26 -08002883 final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId);
2884 ensureSettingsStateLocked(secureKey);
2885 SettingsState secureSettings = mSettingsStates.get(secureKey);
2886 migrateLegacySettingsLocked(secureSettings, database, TABLE_SECURE);
2887 ensureSecureSettingAndroidIdSetLocked(secureSettings);
2888 secureSettings.persistSyncLocked();
2889
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002890 // Move over the global settings if owner.
2891 // Do this last, since this is the first thing we check when deciding
2892 // to skip over migration from db to xml for owner user.
2893 if (userId == UserHandle.USER_SYSTEM) {
2894 final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, userId);
2895 ensureSettingsStateLocked(globalKey);
2896 SettingsState globalSettings = mSettingsStates.get(globalKey);
2897 migrateLegacySettingsLocked(globalSettings, database, TABLE_GLOBAL);
Amith Yamasani39452022017-03-21 15:23:47 -07002898 // If this was just created
2899 if (mSettingsCreationBuildId != null) {
2900 globalSettings.insertSettingLocked(Settings.Global.DATABASE_CREATION_BUILDID,
2901 mSettingsCreationBuildId, null, true,
2902 SettingsState.SYSTEM_PACKAGE_NAME);
2903 }
Amith Yamasanibf2ef612016-03-07 16:37:18 -08002904 globalSettings.persistSyncLocked();
2905 }
Svetoslav683914b2015-01-15 14:22:26 -08002906
2907 // Drop the database as now all is moved and persisted.
2908 if (DROP_DATABASE_ON_MIGRATION) {
2909 dbHelper.dropDatabase();
2910 } else {
2911 dbHelper.backupDatabase();
2912 }
2913 }
2914
2915 private void migrateLegacySettingsLocked(SettingsState settingsState,
2916 SQLiteDatabase database, String table) {
2917 SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
2918 queryBuilder.setTables(table);
2919
2920 Cursor cursor = queryBuilder.query(database, ALL_COLUMNS,
2921 null, null, null, null, null);
2922
2923 if (cursor == null) {
2924 return;
2925 }
2926
2927 try {
2928 if (!cursor.moveToFirst()) {
2929 return;
2930 }
2931
2932 final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME);
2933 final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE);
2934
2935 settingsState.setVersionLocked(database.getVersion());
2936
2937 while (!cursor.isAfterLast()) {
2938 String name = cursor.getString(nameColumnIdx);
2939 String value = cursor.getString(valueColumnIdx);
Svetoslav Ganove080da92016-12-21 17:10:35 -08002940 settingsState.insertSettingLocked(name, value, null, true,
Svetoslav683914b2015-01-15 14:22:26 -08002941 SettingsState.SYSTEM_PACKAGE_NAME);
2942 cursor.moveToNext();
2943 }
2944 } finally {
2945 cursor.close();
2946 }
2947 }
2948
Andreas Gampeb58893072018-09-05 16:52:31 -07002949 @GuardedBy("secureSettings.mLock")
Svetoslav683914b2015-01-15 14:22:26 -08002950 private void ensureSecureSettingAndroidIdSetLocked(SettingsState secureSettings) {
2951 Setting value = secureSettings.getSettingLocked(Settings.Secure.ANDROID_ID);
2952
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09002953 if (!value.isNull()) {
Svetoslav683914b2015-01-15 14:22:26 -08002954 return;
2955 }
2956
2957 final int userId = getUserIdFromKey(secureSettings.mKey);
2958
2959 final UserInfo user;
2960 final long identity = Binder.clearCallingIdentity();
2961 try {
2962 user = mUserManager.getUserInfo(userId);
2963 } finally {
2964 Binder.restoreCallingIdentity(identity);
2965 }
2966 if (user == null) {
2967 // Can happen due to races when deleting users - treat as benign.
2968 return;
2969 }
2970
2971 String androidId = Long.toHexString(new SecureRandom().nextLong());
2972 secureSettings.insertSettingLocked(Settings.Secure.ANDROID_ID, androidId,
Svetoslav Ganove080da92016-12-21 17:10:35 -08002973 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Svetoslav683914b2015-01-15 14:22:26 -08002974
2975 Slog.d(LOG_TAG, "Generated and saved new ANDROID_ID [" + androidId
2976 + "] for user " + userId);
2977
2978 // Write a drop box entry if it's a restricted profile
2979 if (user.isRestricted()) {
2980 DropBoxManager dbm = (DropBoxManager) getContext().getSystemService(
2981 Context.DROPBOX_SERVICE);
2982 if (dbm != null && dbm.isTagEnabled(DROPBOX_TAG_USERLOG)) {
2983 dbm.addText(DROPBOX_TAG_USERLOG, System.currentTimeMillis()
2984 + "," + DROPBOX_TAG_USERLOG + "," + androidId + "\n");
2985 }
2986 }
2987 }
2988
2989 private void notifyForSettingsChange(int key, String name) {
Svet Ganov945864c2018-03-22 21:49:10 -07002990 // Increment the generation first, so observers always see the new value
Phil Weaver83fec002016-05-11 10:55:29 -07002991 mGenerationRegistry.incrementGeneration(key);
2992
Linus Tufvesson93c38552019-05-30 12:47:39 +01002993 if (isGlobalSettingsKey(key) || isConfigSettingsKey(key)) {
Jeff Sharkey308093f2018-03-25 22:53:29 -06002994 final long token = Binder.clearCallingIdentity();
2995 try {
Linus Tufvesson93c38552019-05-30 12:47:39 +01002996 if (Global.LOCATION_GLOBAL_KILL_SWITCH.equals(name)
2997 && isGlobalSettingsKey(key)) {
Jeff Sharkey308093f2018-03-25 22:53:29 -06002998 // When the global kill switch is updated, send the
2999 // change notification for the location setting.
3000 notifyLocationChangeForRunningUsers();
3001 }
Linus Tufvesson93c38552019-05-30 12:47:39 +01003002 notifySettingChangeForRunningUsers(key, name);
Jeff Sharkey308093f2018-03-25 22:53:29 -06003003 } finally {
3004 Binder.restoreCallingIdentity(token);
Svet Ganov945864c2018-03-22 21:49:10 -07003005 }
Svet Ganov945864c2018-03-22 21:49:10 -07003006 } else {
3007 final int userId = getUserIdFromKey(key);
3008 final Uri uri = getNotificationUriFor(key, name);
3009 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
3010 userId, 0, uri).sendToTarget();
3011 if (isSecureSettingsKey(key)) {
3012 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
3013 sSecureCloneToManagedSettings);
3014 maybeNotifyProfiles(SETTINGS_TYPE_SYSTEM, userId, uri, name,
3015 sSystemCloneFromParentOnDependency.values());
3016 } else if (isSystemSettingsKey(key)) {
3017 maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name,
3018 sSystemCloneToManagedSettings);
3019 }
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003020 }
Svet Ganov53a441c2016-04-19 19:38:00 -07003021
Svet Ganov945864c2018-03-22 21:49:10 -07003022 // Always notify that our data changed
Svet Ganov53a441c2016-04-19 19:38:00 -07003023 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003024 }
3025
Svet Ganov53a441c2016-04-19 19:38:00 -07003026 private void maybeNotifyProfiles(int type, int userId, Uri uri, String name,
Robin Lee7af9a742017-02-20 14:47:30 +00003027 Collection<String> keysCloned) {
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003028 if (keysCloned.contains(name)) {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003029 for (int profileId : mUserManager.getProfileIdsWithDisabled(userId)) {
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003030 // the notification for userId has already been sent.
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003031 if (profileId != userId) {
Svet Ganov945864c2018-03-22 21:49:10 -07003032 final int key = makeKey(type, profileId);
3033 // Increment the generation first, so observers always see the new value
3034 mGenerationRegistry.incrementGeneration(key);
Svetoslav7e0683b2015-08-03 16:02:52 -07003035 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07003036 profileId, 0, uri).sendToTarget();
Nicolas Prevot310e1ee2015-07-02 14:03:06 +01003037 }
3038 }
3039 }
Svetoslav683914b2015-01-15 14:22:26 -08003040 }
3041
Linus Tufvesson93c38552019-05-30 12:47:39 +01003042 private void notifySettingChangeForRunningUsers(int key, String name) {
Svet Ganov945864c2018-03-22 21:49:10 -07003043 // Important: No need to update generation for each user as there
3044 // is a singleton generation entry for the global settings which
3045 // is already incremented be the caller.
3046 final Uri uri = getNotificationUriFor(key, name);
3047 final List<UserInfo> users = mUserManager.getUsers(/*excludeDying*/ true);
3048 for (int i = 0; i < users.size(); i++) {
3049 final int userId = users.get(i).id;
3050 if (mUserManager.isUserRunning(UserHandle.of(userId))) {
3051 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
3052 userId, 0, uri).sendToTarget();
3053 }
3054 }
3055 }
3056
Makoto Onuki0000d322017-11-28 16:31:47 -08003057 private void notifyLocationChangeForRunningUsers() {
3058 final List<UserInfo> users = mUserManager.getUsers(/*excludeDying=*/ true);
3059
3060 for (int i = 0; i < users.size(); i++) {
3061 final int userId = users.get(i).id;
3062
3063 if (!mUserManager.isUserRunning(UserHandle.of(userId))) {
3064 continue;
3065 }
Makoto Onuki0000d322017-11-28 16:31:47 -08003066
Svet Ganov945864c2018-03-22 21:49:10 -07003067 // Increment the generation first, so observers always see the new value
3068 final int key = makeKey(SETTINGS_TYPE_SECURE, userId);
3069 mGenerationRegistry.incrementGeneration(key);
3070
Kweku Adams5e0052b2019-02-22 15:17:52 -08003071 final Uri uri = getNotificationUriFor(key, Secure.LOCATION_MODE);
Makoto Onuki0000d322017-11-28 16:31:47 -08003072 mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED,
3073 userId, 0, uri).sendToTarget();
3074 }
3075 }
3076
Matt Pape1b31a332018-10-17 09:58:28 -07003077 private boolean isConfigSettingsKey(int key) {
3078 return getTypeFromKey(key) == SETTINGS_TYPE_CONFIG;
3079 }
3080
Svetoslav683914b2015-01-15 14:22:26 -08003081 private boolean isGlobalSettingsKey(int key) {
3082 return getTypeFromKey(key) == SETTINGS_TYPE_GLOBAL;
3083 }
3084
3085 private boolean isSystemSettingsKey(int key) {
3086 return getTypeFromKey(key) == SETTINGS_TYPE_SYSTEM;
3087 }
3088
3089 private boolean isSecureSettingsKey(int key) {
3090 return getTypeFromKey(key) == SETTINGS_TYPE_SECURE;
3091 }
3092
Mark Rathjend891f012017-01-19 04:10:37 +00003093 private boolean isSsaidSettingsKey(int key) {
3094 return getTypeFromKey(key) == SETTINGS_TYPE_SSAID;
3095 }
3096
Svetoslav683914b2015-01-15 14:22:26 -08003097 private File getSettingsFile(int key) {
Matt Pape1b31a332018-10-17 09:58:28 -07003098 if (isConfigSettingsKey(key)) {
3099 final int userId = getUserIdFromKey(key);
3100 return new File(Environment.getUserSystemDirectory(userId),
3101 SETTINGS_FILE_CONFIG);
3102 } else if (isGlobalSettingsKey(key)) {
Svetoslav683914b2015-01-15 14:22:26 -08003103 final int userId = getUserIdFromKey(key);
3104 return new File(Environment.getUserSystemDirectory(userId),
3105 SETTINGS_FILE_GLOBAL);
3106 } else if (isSystemSettingsKey(key)) {
3107 final int userId = getUserIdFromKey(key);
3108 return new File(Environment.getUserSystemDirectory(userId),
3109 SETTINGS_FILE_SYSTEM);
3110 } else if (isSecureSettingsKey(key)) {
3111 final int userId = getUserIdFromKey(key);
3112 return new File(Environment.getUserSystemDirectory(userId),
3113 SETTINGS_FILE_SECURE);
Mark Rathjend891f012017-01-19 04:10:37 +00003114 } else if (isSsaidSettingsKey(key)) {
3115 final int userId = getUserIdFromKey(key);
3116 return new File(Environment.getUserSystemDirectory(userId),
3117 SETTINGS_FILE_SSAID);
Svetoslav683914b2015-01-15 14:22:26 -08003118 } else {
3119 throw new IllegalArgumentException("Invalid settings key:" + key);
3120 }
3121 }
3122
3123 private Uri getNotificationUriFor(int key, String name) {
Matt Pape1b31a332018-10-17 09:58:28 -07003124 if (isConfigSettingsKey(key)) {
Matt Pape1278d1c2018-12-11 13:03:49 -08003125 return (name != null) ? Uri.withAppendedPath(DeviceConfig.CONTENT_URI, name)
3126 : DeviceConfig.CONTENT_URI;
Matt Pape1b31a332018-10-17 09:58:28 -07003127 } else if (isGlobalSettingsKey(key)) {
Svetoslav683914b2015-01-15 14:22:26 -08003128 return (name != null) ? Uri.withAppendedPath(Settings.Global.CONTENT_URI, name)
3129 : Settings.Global.CONTENT_URI;
3130 } else if (isSecureSettingsKey(key)) {
3131 return (name != null) ? Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name)
3132 : Settings.Secure.CONTENT_URI;
3133 } else if (isSystemSettingsKey(key)) {
3134 return (name != null) ? Uri.withAppendedPath(Settings.System.CONTENT_URI, name)
3135 : Settings.System.CONTENT_URI;
3136 } else {
3137 throw new IllegalArgumentException("Invalid settings key:" + key);
3138 }
3139 }
3140
3141 private int getMaxBytesPerPackageForType(int type) {
3142 switch (type) {
Matt Pape1b31a332018-10-17 09:58:28 -07003143 case SETTINGS_TYPE_CONFIG:
Svetoslav683914b2015-01-15 14:22:26 -08003144 case SETTINGS_TYPE_GLOBAL:
Mark Rathjend891f012017-01-19 04:10:37 +00003145 case SETTINGS_TYPE_SECURE:
3146 case SETTINGS_TYPE_SSAID: {
Svetoslav683914b2015-01-15 14:22:26 -08003147 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED;
3148 }
3149
3150 default: {
3151 return SettingsState.MAX_BYTES_PER_APP_PACKAGE_LIMITED;
3152 }
3153 }
3154 }
3155
Svetoslav7e0683b2015-08-03 16:02:52 -07003156 private final class MyHandler extends Handler {
3157 private static final int MSG_NOTIFY_URI_CHANGED = 1;
3158 private static final int MSG_NOTIFY_DATA_CHANGED = 2;
3159
3160 public MyHandler(Looper looper) {
3161 super(looper);
3162 }
3163
3164 @Override
3165 public void handleMessage(Message msg) {
3166 switch (msg.what) {
3167 case MSG_NOTIFY_URI_CHANGED: {
3168 final int userId = msg.arg1;
3169 Uri uri = (Uri) msg.obj;
Jeff Sharkey88f9d0b2017-08-11 15:29:40 -06003170 try {
3171 getContext().getContentResolver().notifyChange(uri, null, true, userId);
3172 } catch (SecurityException e) {
3173 Slog.w(LOG_TAG, "Failed to notify for " + userId + ": " + uri, e);
3174 }
Makoto Onuki0000d322017-11-28 16:31:47 -08003175 if (DEBUG || true) {
Svetoslav7e0683b2015-08-03 16:02:52 -07003176 Slog.v(LOG_TAG, "Notifying for " + userId + ": " + uri);
3177 }
3178 } break;
3179
3180 case MSG_NOTIFY_DATA_CHANGED: {
3181 mBackupManager.dataChanged();
3182 } break;
3183 }
3184 }
3185 }
3186
Svetoslav683914b2015-01-15 14:22:26 -08003187 private final class UpgradeController {
Eric Laurent02153e62019-06-12 17:20:03 -07003188 private static final int SETTINGS_VERSION = 182;
Svetoslav683914b2015-01-15 14:22:26 -08003189
3190 private final int mUserId;
3191
3192 public UpgradeController(int userId) {
3193 mUserId = userId;
3194 }
3195
3196 public void upgradeIfNeededLocked() {
3197 // The version of all settings for a user is the same (all users have secure).
3198 SettingsState secureSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07003199 SETTINGS_TYPE_SECURE, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08003200
3201 // Try an update from the current state.
3202 final int oldVersion = secureSettings.getVersionLocked();
3203 final int newVersion = SETTINGS_VERSION;
3204
Svet Ganovc9755bc2015-03-28 13:21:22 -07003205 // If up do date - done.
Svetoslav683914b2015-01-15 14:22:26 -08003206 if (oldVersion == newVersion) {
3207 return;
3208 }
3209
3210 // Try to upgrade.
3211 final int curVersion = onUpgradeLocked(mUserId, oldVersion, newVersion);
3212
3213 // If upgrade failed start from scratch and upgrade.
3214 if (curVersion != newVersion) {
3215 // Drop state we have for this user.
3216 removeUserStateLocked(mUserId, true);
3217
3218 // Recreate the database.
3219 DatabaseHelper dbHelper = new DatabaseHelper(getContext(), mUserId);
3220 SQLiteDatabase database = dbHelper.getWritableDatabase();
3221 dbHelper.recreateDatabase(database, newVersion, curVersion, oldVersion);
3222
3223 // Migrate the settings for this user.
3224 migrateLegacySettingsForUserLocked(dbHelper, database, mUserId);
3225
3226 // Now upgrade should work fine.
3227 onUpgradeLocked(mUserId, oldVersion, newVersion);
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07003228
3229 // Make a note what happened, so we don't wonder why data was lost
3230 String reason = "Settings rebuilt! Current version: "
3231 + curVersion + " while expected: " + newVersion;
3232 getGlobalSettingsLocked().insertSettingLocked(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003233 Settings.Global.DATABASE_DOWNGRADE_REASON,
3234 reason, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Svetoslav683914b2015-01-15 14:22:26 -08003235 }
3236
3237 // Set the global settings version if owner.
Xiaohui Chen43765b72015-08-31 10:57:33 -07003238 if (mUserId == UserHandle.USER_SYSTEM) {
Svetoslav683914b2015-01-15 14:22:26 -08003239 SettingsState globalSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07003240 SETTINGS_TYPE_GLOBAL, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08003241 globalSettings.setVersionLocked(newVersion);
3242 }
3243
3244 // Set the secure settings version.
3245 secureSettings.setVersionLocked(newVersion);
3246
3247 // Set the system settings version.
3248 SettingsState systemSettings = getSettingsLocked(
Svet Ganov53a441c2016-04-19 19:38:00 -07003249 SETTINGS_TYPE_SYSTEM, mUserId);
Svetoslav683914b2015-01-15 14:22:26 -08003250 systemSettings.setVersionLocked(newVersion);
3251 }
3252
3253 private SettingsState getGlobalSettingsLocked() {
Xiaohui Chen43765b72015-08-31 10:57:33 -07003254 return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
Svetoslav683914b2015-01-15 14:22:26 -08003255 }
3256
3257 private SettingsState getSecureSettingsLocked(int userId) {
3258 return getSettingsLocked(SETTINGS_TYPE_SECURE, userId);
3259 }
3260
Mark Rathjend891f012017-01-19 04:10:37 +00003261 private SettingsState getSsaidSettingsLocked(int userId) {
3262 return getSettingsLocked(SETTINGS_TYPE_SSAID, userId);
3263 }
3264
Svetoslav683914b2015-01-15 14:22:26 -08003265 private SettingsState getSystemSettingsLocked(int userId) {
3266 return getSettingsLocked(SETTINGS_TYPE_SYSTEM, userId);
3267 }
3268
Jeff Brown503cffc2015-03-26 18:08:51 -07003269 /**
3270 * You must perform all necessary mutations to bring the settings
3271 * for this user from the old to the new version. When you add a new
3272 * upgrade step you *must* update SETTINGS_VERSION.
3273 *
3274 * This is an example of moving a setting from secure to global.
3275 *
3276 * // v119: Example settings changes.
3277 * if (currentVersion == 118) {
3278 * if (userId == UserHandle.USER_OWNER) {
3279 * // Remove from the secure settings.
3280 * SettingsState secureSettings = getSecureSettingsLocked(userId);
3281 * String name = "example_setting_to_move";
3282 * String value = secureSettings.getSetting(name);
3283 * secureSettings.deleteSetting(name);
3284 *
3285 * // Add to the global settings.
3286 * SettingsState globalSettings = getGlobalSettingsLocked();
3287 * globalSettings.insertSetting(name, value, SettingsState.SYSTEM_PACKAGE_NAME);
3288 * }
3289 *
3290 * // Update the current version.
3291 * currentVersion = 119;
3292 * }
3293 */
Svetoslav683914b2015-01-15 14:22:26 -08003294 private int onUpgradeLocked(int userId, int oldVersion, int newVersion) {
3295 if (DEBUG) {
3296 Slog.w(LOG_TAG, "Upgrading settings for user: " + userId + " from version: "
3297 + oldVersion + " to version: " + newVersion);
3298 }
3299
Jeff Brown503cffc2015-03-26 18:08:51 -07003300 int currentVersion = oldVersion;
Svetoslav683914b2015-01-15 14:22:26 -08003301
John Spurlocke11ae112015-05-11 16:09:03 -04003302 // v119: Reset zen + ringer mode.
3303 if (currentVersion == 118) {
Xiaohui Chen43765b72015-08-31 10:57:33 -07003304 if (userId == UserHandle.USER_SYSTEM) {
John Spurlocke11ae112015-05-11 16:09:03 -04003305 final SettingsState globalSettings = getGlobalSettingsLocked();
3306 globalSettings.updateSettingLocked(Settings.Global.ZEN_MODE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003307 Integer.toString(Settings.Global.ZEN_MODE_OFF), null,
3308 true, SettingsState.SYSTEM_PACKAGE_NAME);
John Spurlocke11ae112015-05-11 16:09:03 -04003309 globalSettings.updateSettingLocked(Settings.Global.MODE_RINGER,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003310 Integer.toString(AudioManager.RINGER_MODE_NORMAL), null,
3311 true, SettingsState.SYSTEM_PACKAGE_NAME);
John Spurlocke11ae112015-05-11 16:09:03 -04003312 }
3313 currentVersion = 119;
3314 }
3315
Jason Monk27bbb2d2015-03-31 16:46:39 -04003316 // v120: Add double tap to wake setting.
3317 if (currentVersion == 119) {
3318 SettingsState secureSettings = getSecureSettingsLocked(userId);
3319 secureSettings.insertSettingLocked(Settings.Secure.DOUBLE_TAP_TO_WAKE,
3320 getContext().getResources().getBoolean(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003321 R.bool.def_double_tap_to_wake) ? "1" : "0", null, true,
Jason Monk27bbb2d2015-03-31 16:46:39 -04003322 SettingsState.SYSTEM_PACKAGE_NAME);
3323
3324 currentVersion = 120;
3325 }
3326
Svetoslav7e0683b2015-08-03 16:02:52 -07003327 if (currentVersion == 120) {
3328 // Before 121, we used a different string encoding logic. We just bump the
3329 // version here; SettingsState knows how to handle pre-version 120 files.
3330 currentVersion = 121;
3331 }
Makoto Onuki3a2c35782015-06-18 11:21:58 -07003332
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003333 if (currentVersion == 121) {
3334 // Version 122: allow OEMs to set a default payment component in resources.
3335 // Note that we only write the default if no default has been set;
3336 // if there is, we just leave the default at whatever it currently is.
3337 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3338 String defaultComponent = (getContext().getResources().getString(
3339 R.string.def_nfc_payment_component));
3340 Setting currentSetting = secureSettings.getSettingLocked(
3341 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT);
3342 if (defaultComponent != null && !defaultComponent.isEmpty() &&
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003343 currentSetting.isNull()) {
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003344 secureSettings.insertSettingLocked(
3345 Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003346 defaultComponent, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Martijn Coenen7ab4b7f2015-07-27 15:58:32 +02003347 }
3348 currentVersion = 122;
3349 }
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003350
3351 if (currentVersion == 122) {
3352 // Version 123: Adding a default value for the ability to add a user from
3353 // the lock screen.
3354 if (userId == UserHandle.USER_SYSTEM) {
3355 final SettingsState globalSettings = getGlobalSettingsLocked();
3356 Setting currentSetting = globalSettings.getSettingLocked(
3357 Settings.Global.ADD_USERS_WHEN_LOCKED);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003358 if (currentSetting.isNull()) {
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003359 globalSettings.insertSettingLocked(
3360 Settings.Global.ADD_USERS_WHEN_LOCKED,
3361 getContext().getResources().getBoolean(
3362 R.bool.def_add_users_from_lockscreen) ? "1" : "0",
Svetoslav Ganove080da92016-12-21 17:10:35 -08003363 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shukla269c11e2015-12-02 16:51:16 -08003364 }
3365 }
3366 currentVersion = 123;
3367 }
Bryce Leebd179282015-12-17 19:01:37 -08003368
3369 if (currentVersion == 123) {
Bryce Leeec85f342015-12-16 13:32:28 -08003370 final SettingsState globalSettings = getGlobalSettingsLocked();
3371 String defaultDisabledProfiles = (getContext().getResources().getString(
3372 R.string.def_bluetooth_disabled_profiles));
3373 globalSettings.insertSettingLocked(Settings.Global.BLUETOOTH_DISABLED_PROFILES,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003374 defaultDisabledProfiles, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Bryce Leebd179282015-12-17 19:01:37 -08003375 currentVersion = 124;
Bryce Leeec85f342015-12-16 13:32:28 -08003376 }
3377
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003378 if (currentVersion == 124) {
3379 // Version 124: allow OEMs to set a default value for whether IME should be
3380 // shown when a physical keyboard is connected.
3381 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3382 Setting currentSetting = secureSettings.getSettingLocked(
3383 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003384 if (currentSetting.isNull()) {
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003385 secureSettings.insertSettingLocked(
3386 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD,
3387 getContext().getResources().getBoolean(
3388 R.bool.def_show_ime_with_hard_keyboard) ? "1" : "0",
Svetoslav Ganove080da92016-12-21 17:10:35 -08003389 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Prathmesh Prabhude16b862016-03-04 15:22:24 -08003390 }
3391 currentVersion = 125;
3392 }
3393
Ruben Brunk98576cf2016-03-07 18:54:28 -08003394 if (currentVersion == 125) {
3395 // Version 125: Allow OEMs to set the default VR service.
3396 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3397
3398 Setting currentSetting = secureSettings.getSettingLocked(
3399 Settings.Secure.ENABLED_VR_LISTENERS);
Svetoslav Ganovfedb2302016-04-26 18:36:42 -07003400 if (currentSetting.isNull()) {
Ruben Brunk98576cf2016-03-07 18:54:28 -08003401 ArraySet<ComponentName> l =
3402 SystemConfig.getInstance().getDefaultVrComponents();
3403
3404 if (l != null && !l.isEmpty()) {
3405 StringBuilder b = new StringBuilder();
3406 boolean start = true;
3407 for (ComponentName c : l) {
3408 if (!start) {
3409 b.append(':');
3410 }
3411 b.append(c.flattenToString());
3412 start = false;
3413 }
3414 secureSettings.insertSettingLocked(
3415 Settings.Secure.ENABLED_VR_LISTENERS, b.toString(),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003416 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Ruben Brunk98576cf2016-03-07 18:54:28 -08003417 }
3418
3419 }
3420 currentVersion = 126;
3421 }
3422
Daniel U02ba6122016-04-01 18:41:42 +01003423 if (currentVersion == 126) {
3424 // Version 126: copy the primary values of LOCK_SCREEN_SHOW_NOTIFICATIONS and
3425 // LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS into managed profile.
3426 if (mUserManager.isManagedProfile(userId)) {
3427 final SettingsState systemSecureSettings =
3428 getSecureSettingsLocked(UserHandle.USER_SYSTEM);
3429
3430 final Setting showNotifications = systemSecureSettings.getSettingLocked(
3431 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09003432 if (!showNotifications.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01003433 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3434 secureSettings.insertSettingLocked(
3435 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003436 showNotifications.getValue(), null, true,
Daniel U02ba6122016-04-01 18:41:42 +01003437 SettingsState.SYSTEM_PACKAGE_NAME);
3438 }
3439
3440 final Setting allowPrivate = systemSecureSettings.getSettingLocked(
3441 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS);
Seigo Nonaka6e5b6022016-04-27 16:32:44 +09003442 if (!allowPrivate.isNull()) {
Daniel U02ba6122016-04-01 18:41:42 +01003443 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3444 secureSettings.insertSettingLocked(
3445 Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003446 allowPrivate.getValue(), null, true,
Daniel U02ba6122016-04-01 18:41:42 +01003447 SettingsState.SYSTEM_PACKAGE_NAME);
3448 }
3449 }
3450 currentVersion = 127;
3451 }
3452
Steven Ngdc20ba62016-04-26 18:19:04 +01003453 if (currentVersion == 127) {
Mahaver Chopra3d9805d2016-07-07 16:25:05 +01003454 // version 127 is no longer used.
Steven Ngdc20ba62016-04-26 18:19:04 +01003455 currentVersion = 128;
3456 }
3457
Julia Reynolds1f721e12016-07-11 08:50:58 -04003458 if (currentVersion == 128) {
Julia Reynoldsb852e562017-06-06 16:14:18 -04003459 // Version 128: Removed
Julia Reynolds1f721e12016-07-11 08:50:58 -04003460 currentVersion = 129;
3461 }
3462
Dan Sandler71f85e92016-07-20 13:46:05 -04003463 if (currentVersion == 129) {
3464 // default longpress timeout changed from 500 to 400. If unchanged from the old
3465 // default, update to the new default.
3466 final SettingsState systemSecureSettings =
3467 getSecureSettingsLocked(userId);
3468 final String oldValue = systemSecureSettings.getSettingLocked(
3469 Settings.Secure.LONG_PRESS_TIMEOUT).getValue();
3470 if (TextUtils.equals("500", oldValue)) {
3471 systemSecureSettings.insertSettingLocked(
3472 Settings.Secure.LONG_PRESS_TIMEOUT,
3473 String.valueOf(getContext().getResources().getInteger(
3474 R.integer.def_long_press_timeout_millis)),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003475 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Dan Sandler71f85e92016-07-20 13:46:05 -04003476 }
3477 currentVersion = 130;
3478 }
3479
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003480 if (currentVersion == 130) {
Adrian Roos69741a22016-10-21 14:49:17 -07003481 // Split Ambient settings
3482 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3483 boolean dozeExplicitlyDisabled = "0".equals(secureSettings.
3484 getSettingLocked(Settings.Secure.DOZE_ENABLED).getValue());
3485
3486 if (dozeExplicitlyDisabled) {
Lucas Dupin4359b552018-08-09 15:07:54 -07003487 secureSettings.insertSettingLocked(Settings.Secure.DOZE_PICK_UP_GESTURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003488 "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Lucas Dupin4359b552018-08-09 15:07:54 -07003489 secureSettings.insertSettingLocked(Settings.Secure.DOZE_DOUBLE_TAP_GESTURE,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003490 "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Adrian Roos69741a22016-10-21 14:49:17 -07003491 }
3492 currentVersion = 131;
3493 }
3494
3495 if (currentVersion == 131) {
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003496 // Initialize new multi-press timeout to default value
3497 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3498 final String oldValue = systemSecureSettings.getSettingLocked(
3499 Settings.Secure.MULTI_PRESS_TIMEOUT).getValue();
3500 if (TextUtils.equals(null, oldValue)) {
3501 systemSecureSettings.insertSettingLocked(
3502 Settings.Secure.MULTI_PRESS_TIMEOUT,
3503 String.valueOf(getContext().getResources().getInteger(
3504 R.integer.def_multi_press_timeout_millis)),
Svetoslav Ganove080da92016-12-21 17:10:35 -08003505 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003506 }
3507
Adrian Roos69741a22016-10-21 14:49:17 -07003508 currentVersion = 132;
Anthony Hugh96e9cc52016-07-12 15:17:24 -07003509 }
3510
Adrian Roos69741a22016-10-21 14:49:17 -07003511 if (currentVersion == 132) {
3512 // Version 132: Allow managed profile to optionally use the parent's ringtones
Andre Lagoea35e072016-08-04 13:41:13 +01003513 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3514 String defaultSyncParentSounds = (getContext().getResources()
3515 .getBoolean(R.bool.def_sync_parent_sounds) ? "1" : "0");
3516 systemSecureSettings.insertSettingLocked(
Svetoslav Ganove080da92016-12-21 17:10:35 -08003517 Settings.Secure.SYNC_PARENT_SOUNDS, defaultSyncParentSounds,
3518 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Adrian Roos69741a22016-10-21 14:49:17 -07003519 currentVersion = 133;
Andre Lagoea35e072016-08-04 13:41:13 +01003520 }
3521
Adrian Roos69741a22016-10-21 14:49:17 -07003522 if (currentVersion == 133) {
3523 // Version 133: Add default end button behavior
Keun-young Parkec7a1182016-10-18 11:52:38 -07003524 final SettingsState systemSettings = getSystemSettingsLocked(userId);
davidlnedb31e12018-09-25 10:21:20 -07003525 if (systemSettings.getSettingLocked(Settings.System.END_BUTTON_BEHAVIOR)
3526 .isNull()) {
Keun-young Parkec7a1182016-10-18 11:52:38 -07003527 String defaultEndButtonBehavior = Integer.toString(getContext()
3528 .getResources().getInteger(R.integer.def_end_button_behavior));
3529 systemSettings.insertSettingLocked(Settings.System.END_BUTTON_BEHAVIOR,
Svetoslav Ganove080da92016-12-21 17:10:35 -08003530 defaultEndButtonBehavior, null, true,
3531 SettingsState.SYSTEM_PACKAGE_NAME);
Keun-young Parkec7a1182016-10-18 11:52:38 -07003532 }
Adrian Roos69741a22016-10-21 14:49:17 -07003533 currentVersion = 134;
Keun-young Parkec7a1182016-10-18 11:52:38 -07003534 }
3535
Phil Weaver89e3ffc2016-09-19 13:51:10 -07003536 if (currentVersion == 134) {
3537 // Remove setting that specifies if magnification values should be preserved.
3538 // This setting defaulted to true and never has a UI.
3539 getSecureSettingsLocked(userId).deleteSettingLocked(
3540 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_AUTO_UPDATE);
3541 currentVersion = 135;
3542 }
3543
Jeremy Joslin8bdad342016-12-14 11:46:47 -08003544 if (currentVersion == 135) {
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003545 // Version 135 no longer used.
Jeremy Joslin8bdad342016-12-14 11:46:47 -08003546 currentVersion = 136;
3547 }
3548
Mark Rathjend891f012017-01-19 04:10:37 +00003549 if (currentVersion == 136) {
3550 // Version 136: Store legacy SSAID for all apps currently installed on the
3551 // device as first step in migrating SSAID to be unique per application.
3552
3553 final boolean isUpgrade;
3554 try {
Svet Ganovd8eb8b22019-04-05 18:52:08 -07003555 isUpgrade = mPackageManager.isDeviceUpgrading();
Mark Rathjend891f012017-01-19 04:10:37 +00003556 } catch (RemoteException e) {
3557 throw new IllegalStateException("Package manager not available");
3558 }
3559 // Only retain legacy ssaid if the device is performing an OTA. After wiping
3560 // user data or first boot on a new device should use new ssaid generation.
3561 if (isUpgrade) {
3562 // Retrieve the legacy ssaid from the secure settings table.
Mark Rathjenea617592017-01-18 23:03:41 -08003563 final Setting legacySsaidSetting = getSettingLocked(SETTINGS_TYPE_SECURE,
3564 userId, Settings.Secure.ANDROID_ID);
3565 if (legacySsaidSetting == null || legacySsaidSetting.isNull()
3566 || legacySsaidSetting.getValue() == null) {
3567 throw new IllegalStateException("Legacy ssaid not accessible");
3568 }
3569 final String legacySsaid = legacySsaidSetting.getValue();
Mark Rathjend891f012017-01-19 04:10:37 +00003570
3571 // Fill each uid with the legacy ssaid to be backwards compatible.
3572 final List<PackageInfo> packages;
3573 try {
Tetsutoki Shiozawaebe0e5f2018-01-17 11:07:09 +09003574 packages = mPackageManager.getInstalledPackages(
3575 PackageManager.MATCH_UNINSTALLED_PACKAGES,
3576 userId).getList();
Mark Rathjend891f012017-01-19 04:10:37 +00003577 } catch (RemoteException e) {
3578 throw new IllegalStateException("Package manager not available");
3579 }
3580
3581 final SettingsState ssaidSettings = getSsaidSettingsLocked(userId);
3582 for (PackageInfo info : packages) {
3583 // Check if the UID already has an entry in the table.
3584 final String uid = Integer.toString(info.applicationInfo.uid);
3585 final Setting ssaid = ssaidSettings.getSettingLocked(uid);
3586
3587 if (ssaid.isNull() || ssaid.getValue() == null) {
3588 // Android Id doesn't exist for this package so create it.
3589 ssaidSettings.insertSettingLocked(uid, legacySsaid, null, true,
3590 info.packageName);
Tetsutoki Shiozawaebe0e5f2018-01-17 11:07:09 +09003591 if (DEBUG) {
3592 Slog.d(LOG_TAG, "Keep the legacy ssaid for uid=" + uid);
3593 }
Mark Rathjend891f012017-01-19 04:10:37 +00003594 }
3595 }
3596 }
3597
3598 currentVersion = 137;
3599 }
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003600 if (currentVersion == 137) {
3601 // Version 138: Settings.Secure#INSTALL_NON_MARKET_APPS is deprecated and its
3602 // default value set to 1. The user can no longer change the value of this
3603 // setting through the UI.
3604 final SettingsState secureSetting = getSecureSettingsLocked(userId);
3605 if (!mUserManager.hasUserRestriction(
Suprabh Shukla0b1356f2017-02-21 14:33:50 -08003606 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, UserHandle.of(userId))
3607 && secureSetting.getSettingLocked(
3608 Settings.Secure.INSTALL_NON_MARKET_APPS).getValue().equals("0")) {
3609
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003610 secureSetting.insertSettingLocked(Settings.Secure.INSTALL_NON_MARKET_APPS,
3611 "1", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shukla0b1356f2017-02-21 14:33:50 -08003612 // For managed profiles with profile owners, DevicePolicyManagerService
3613 // may want to set the user restriction in this case
3614 secureSetting.insertSettingLocked(
3615 Settings.Secure.UNKNOWN_SOURCES_DEFAULT_REVERSED, "1", null, true,
3616 SettingsState.SYSTEM_PACKAGE_NAME);
Suprabh Shuklae3745ee2017-02-02 20:01:11 -08003617 }
3618 currentVersion = 138;
3619 }
Mark Rathjend891f012017-01-19 04:10:37 +00003620
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003621 if (currentVersion == 138) {
Jeremy Joslin27d14c42017-02-15 12:02:03 -08003622 // Version 139: Removed.
Jeremy Joslinc9eb3c42017-02-08 10:45:30 -08003623 currentVersion = 139;
3624 }
3625
Phil Weaver385912e2017-02-10 10:06:56 -08003626 if (currentVersion == 139) {
3627 // Version 140: Settings.Secure#ACCESSIBILITY_SPEAK_PASSWORD is deprecated and
3628 // the user can no longer change the value of this setting through the UI.
3629 // Force to true.
3630 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3631 secureSettings.updateSettingLocked(Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD,
3632 "1", null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3633 currentVersion = 140;
3634 }
3635
Julia Reynoldsad0d9e02017-02-15 08:41:48 -05003636 if (currentVersion == 140) {
Julia Reynoldsb852e562017-06-06 16:14:18 -04003637 // Version 141: Removed
Julia Reynoldsad0d9e02017-02-15 08:41:48 -05003638 currentVersion = 141;
3639 }
3640
Svet Ganov13701552017-02-23 12:45:17 -08003641 if (currentVersion == 141) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003642 // This implementation was incorrectly setting the current value of
3643 // settings changed by non-system packages as the default which default
3644 // is set by the system. We add a new upgrade step at the end to properly
3645 // handle this case which would also fix incorrect changes made by the
3646 // old implementation of this step.
Svet Ganov13701552017-02-23 12:45:17 -08003647 currentVersion = 142;
3648 }
3649
Stephen Chen5d0922f2017-03-27 10:28:04 -07003650 if (currentVersion == 142) {
Felipe Lemeff355092017-04-03 12:55:02 -07003651 // Version 143: Set a default value for Wi-Fi wakeup feature.
Stephen Chen5d0922f2017-03-27 10:28:04 -07003652 if (userId == UserHandle.USER_SYSTEM) {
3653 final SettingsState globalSettings = getGlobalSettingsLocked();
3654 Setting currentSetting = globalSettings.getSettingLocked(
3655 Settings.Global.WIFI_WAKEUP_ENABLED);
3656 if (currentSetting.isNull()) {
3657 globalSettings.insertSettingLocked(
3658 Settings.Global.WIFI_WAKEUP_ENABLED,
3659 getContext().getResources().getBoolean(
3660 R.bool.def_wifi_wakeup_enabled) ? "1" : "0",
3661 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3662 }
3663 }
3664
3665 currentVersion = 143;
3666 }
3667
Felipe Lemeff355092017-04-03 12:55:02 -07003668 if (currentVersion == 143) {
3669 // Version 144: Set a default value for Autofill service.
3670 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3671 final Setting currentSetting = secureSettings
3672 .getSettingLocked(Settings.Secure.AUTOFILL_SERVICE);
3673 if (currentSetting.isNull()) {
3674 final String defaultValue = getContext().getResources().getString(
3675 com.android.internal.R.string.config_defaultAutofillService);
3676 if (defaultValue != null) {
3677 Slog.d(LOG_TAG, "Setting [" + defaultValue + "] as Autofill Service "
3678 + "for user " + userId);
3679 secureSettings.insertSettingLocked(Settings.Secure.AUTOFILL_SERVICE,
3680 defaultValue, null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3681 }
3682 }
3683
3684 currentVersion = 144;
3685 }
3686
Jeremy Joslin45caa252017-05-04 11:22:46 -07003687 if (currentVersion == 144) {
Amin Shaikh86367962017-06-07 08:58:22 -07003688 // Version 145: Removed
3689 currentVersion = 145;
3690 }
3691
3692 if (currentVersion == 145) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003693 // Version 146: In step 142 we had a bug where incorrectly
3694 // some settings were considered system set and as a result
3695 // made the default and marked as the default being set by
3696 // the system. Here reevaluate the default and default system
3697 // set flags. This would both fix corruption by the old impl
3698 // of step 142 and also properly handle devices which never
3699 // run 142.
Jeremy Joslin45caa252017-05-04 11:22:46 -07003700 if (userId == UserHandle.USER_SYSTEM) {
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003701 SettingsState globalSettings = getGlobalSettingsLocked();
3702 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(globalSettings, userId);
3703 globalSettings.persistSyncLocked();
Jeremy Joslin45caa252017-05-04 11:22:46 -07003704 }
3705
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003706 SettingsState secureSettings = getSecureSettingsLocked(mUserId);
3707 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(secureSettings, userId);
3708 secureSettings.persistSyncLocked();
3709
3710 SettingsState systemSettings = getSystemSettingsLocked(mUserId);
3711 ensureLegacyDefaultValueAndSystemSetUpdatedLocked(systemSettings, userId);
3712 systemSettings.persistSyncLocked();
3713
Amin Shaikh86367962017-06-07 08:58:22 -07003714 currentVersion = 146;
Jeremy Joslin45caa252017-05-04 11:22:46 -07003715 }
3716
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003717 if (currentVersion == 146) {
Joe LaPenna250d7842018-01-25 10:19:42 -08003718 // Version 147: Removed. (This version previously allowed showing the
3719 // "wifi_wakeup_available" setting).
3720 // The setting that was added here is deleted in 153.
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003721 currentVersion = 147;
3722 }
3723
3724 if (currentVersion == 147) {
3725 // Version 148: Set the default value for DEFAULT_RESTRICT_BACKGROUND_DATA.
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003726 if (userId == UserHandle.USER_SYSTEM) {
3727 final SettingsState globalSettings = getGlobalSettingsLocked();
3728 final Setting currentSetting = globalSettings.getSettingLocked(
3729 Global.DEFAULT_RESTRICT_BACKGROUND_DATA);
3730 if (currentSetting.isNull()) {
3731 globalSettings.insertSettingLocked(
3732 Global.DEFAULT_RESTRICT_BACKGROUND_DATA,
3733 getContext().getResources().getBoolean(
3734 R.bool.def_restrict_background_data) ? "1" : "0",
3735 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3736 }
3737 }
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07003738 currentVersion = 148;
Narayan Kamath94bcdbc2017-07-17 15:32:53 +01003739 }
3740
Tim Zhengcc1e76a2017-08-30 17:46:19 -07003741 if (currentVersion == 148) {
3742 // Version 149: Set the default value for BACKUP_MANAGER_CONSTANTS.
3743 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3744 final String oldValue = systemSecureSettings.getSettingLocked(
3745 Settings.Secure.BACKUP_MANAGER_CONSTANTS).getValue();
3746 if (TextUtils.equals(null, oldValue)) {
3747 final String defaultValue = getContext().getResources().getString(
3748 R.string.def_backup_manager_constants);
3749 if (!TextUtils.isEmpty(defaultValue)) {
3750 systemSecureSettings.insertSettingLocked(
3751 Settings.Secure.BACKUP_MANAGER_CONSTANTS, defaultValue, null,
3752 true, SettingsState.SYSTEM_PACKAGE_NAME);
3753 }
3754 }
Tim Zhengcc1e76a2017-08-30 17:46:19 -07003755 currentVersion = 149;
3756 }
Jacky Cheung7076a312017-10-02 10:40:48 -07003757
3758 if (currentVersion == 149) {
3759 // Version 150: Set a default value for mobile data always on
3760 final SettingsState globalSettings = getGlobalSettingsLocked();
3761 final Setting currentSetting = globalSettings.getSettingLocked(
3762 Settings.Global.MOBILE_DATA_ALWAYS_ON);
3763 if (currentSetting.isNull()) {
3764 globalSettings.insertSettingLocked(
3765 Settings.Global.MOBILE_DATA_ALWAYS_ON,
3766 getContext().getResources().getBoolean(
3767 R.bool.def_mobile_data_always_on) ? "1" : "0",
3768 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3769 }
3770
3771 currentVersion = 150;
3772 }
3773
Mike Digman4af4a6f2018-01-16 14:49:38 -08003774 if (currentVersion == 150) {
Mike Digman32e03312018-05-16 16:43:23 -07003775 // Version 151: Removed.
Mike Digman4af4a6f2018-01-16 14:49:38 -08003776 currentVersion = 151;
3777 }
3778
Joe LaPenna4f50ed42018-01-22 14:54:45 -08003779 if (currentVersion == 151) {
Joe LaPenna250d7842018-01-25 10:19:42 -08003780 // Version 152: Removed. (This version made the setting for wifi_wakeup enabled
3781 // by default but it is now no longer configurable).
3782 // The setting updated here is deleted in 153.
Joe LaPenna4f50ed42018-01-22 14:54:45 -08003783 currentVersion = 152;
3784 }
3785
Joe LaPenna250d7842018-01-25 10:19:42 -08003786 if (currentVersion == 152) {
3787 getGlobalSettingsLocked().deleteSettingLocked("wifi_wakeup_available");
3788 currentVersion = 153;
3789 }
3790
Ben Linb4df8bc2018-01-29 11:48:20 -08003791 if (currentVersion == 153) {
3792 // Version 154: Read notification badge configuration from config.
3793 // If user has already set the value, don't do anything.
3794 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3795 final Setting showNotificationBadges = systemSecureSettings.getSettingLocked(
3796 Settings.Secure.NOTIFICATION_BADGING);
3797 if (showNotificationBadges.isNull()) {
3798 final boolean defaultValue = getContext().getResources().getBoolean(
3799 com.android.internal.R.bool.config_notificationBadging);
3800 systemSecureSettings.insertSettingLocked(
3801 Secure.NOTIFICATION_BADGING,
3802 defaultValue ? "1" : "0",
3803 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3804 }
3805 currentVersion = 154;
3806 }
3807
Bernardo Rufinoeaa78b92018-01-26 11:25:37 +00003808 if (currentVersion == 154) {
3809 // Version 155: Set the default value for BACKUP_LOCAL_TRANSPORT_PARAMETERS.
3810 final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
3811 final String oldValue = systemSecureSettings.getSettingLocked(
3812 Settings.Secure.BACKUP_LOCAL_TRANSPORT_PARAMETERS).getValue();
3813 if (TextUtils.equals(null, oldValue)) {
3814 final String defaultValue = getContext().getResources().getString(
3815 R.string.def_backup_local_transport_parameters);
3816 if (!TextUtils.isEmpty(defaultValue)) {
3817 systemSecureSettings.insertSettingLocked(
3818 Settings.Secure.BACKUP_LOCAL_TRANSPORT_PARAMETERS, defaultValue,
3819 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3820 }
3821
3822 }
3823 currentVersion = 155;
3824 }
3825
Beverlyda904812018-03-02 09:55:30 -05003826 if (currentVersion == 155) {
Annie Mengd069a882018-03-13 15:31:40 +00003827 // Version 156: Set the default value for CHARGING_STARTED_SOUND.
Beverlyda904812018-03-02 09:55:30 -05003828 final SettingsState globalSettings = getGlobalSettingsLocked();
3829 final String oldValue = globalSettings.getSettingLocked(
3830 Global.CHARGING_STARTED_SOUND).getValue();
3831 final String oldDefault = getContext().getResources().getString(
3832 R.string.def_wireless_charging_started_sound);
3833 if (TextUtils.equals(null, oldValue)
3834 || TextUtils.equals(oldValue, oldDefault)) {
3835 final String defaultValue = getContext().getResources().getString(
3836 R.string.def_charging_started_sound);
3837 if (!TextUtils.isEmpty(defaultValue)) {
3838 globalSettings.insertSettingLocked(
3839 Settings.Global.CHARGING_STARTED_SOUND, defaultValue,
3840 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3841 }
3842
3843 }
3844 currentVersion = 156;
3845 }
3846
Beverly09da25f2018-02-26 09:17:07 -05003847 if (currentVersion == 156) {
Beverly91d0a632018-07-02 16:45:00 -04003848 // Version 157: Set a default value for zen duration,
3849 // in version 169, zen duration is moved to secure settings
Beverly09da25f2018-02-26 09:17:07 -05003850 final SettingsState globalSettings = getGlobalSettingsLocked();
3851 final Setting currentSetting = globalSettings.getSettingLocked(
3852 Global.ZEN_DURATION);
3853 if (currentSetting.isNull()) {
3854 String defaultZenDuration = Integer.toString(getContext()
3855 .getResources().getInteger(R.integer.def_zen_duration));
3856 globalSettings.insertSettingLocked(
3857 Global.ZEN_DURATION, defaultZenDuration,
3858 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3859 }
Beverly09da25f2018-02-26 09:17:07 -05003860 currentVersion = 157;
3861 }
Annie Mengd069a882018-03-13 15:31:40 +00003862
3863 if (currentVersion == 157) {
3864 // Version 158: Set default value for BACKUP_AGENT_TIMEOUT_PARAMETERS.
3865 final SettingsState globalSettings = getGlobalSettingsLocked();
3866 final String oldValue = globalSettings.getSettingLocked(
3867 Settings.Global.BACKUP_AGENT_TIMEOUT_PARAMETERS).getValue();
3868 if (TextUtils.equals(null, oldValue)) {
3869 final String defaultValue = getContext().getResources().getString(
3870 R.string.def_backup_agent_timeout_parameters);
3871 if (!TextUtils.isEmpty(defaultValue)) {
3872 globalSettings.insertSettingLocked(
3873 Settings.Global.BACKUP_AGENT_TIMEOUT_PARAMETERS, defaultValue,
3874 null, true,
3875 SettingsState.SYSTEM_PACKAGE_NAME);
3876 }
3877 }
3878 currentVersion = 158;
3879 }
Roshan Pius9c396672018-03-02 14:54:13 -08003880
3881 if (currentVersion == 158) {
3882 // Remove setting that specifies wifi bgscan throttling params
3883 getGlobalSettingsLocked().deleteSettingLocked(
3884 "wifi_scan_background_throttle_interval_ms");
3885 getGlobalSettingsLocked().deleteSettingLocked(
3886 "wifi_scan_background_throttle_package_whitelist");
3887 currentVersion = 159;
3888 }
3889
Pavel Grafovc5c97302018-03-19 13:37:15 +00003890 if (currentVersion == 159) {
3891 // Version 160: Hiding notifications from the lockscreen is only available as
3892 // primary user option, profiles can only make them redacted. If a profile was
3893 // configured to not show lockscreen notifications, ensure that at the very
3894 // least these will be come hidden.
3895 if (mUserManager.isManagedProfile(userId)) {
3896 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3897 Setting showNotifications = secureSettings.getSettingLocked(
3898 Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
3899 // The default value is "1", check if user has turned it off.
3900 if ("0".equals(showNotifications.getValue())) {
3901 secureSettings.insertSettingLocked(
3902 Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, "0",
3903 null /* tag */, false /* makeDefault */,
3904 SettingsState.SYSTEM_PACKAGE_NAME);
3905 }
3906 // The setting is no longer valid for managed profiles, it should be
3907 // treated as if it was set to "1".
3908 secureSettings.deleteSettingLocked(Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS);
3909 }
3910 currentVersion = 160;
3911 }
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08003912
3913 if (currentVersion == 160) {
3914 // Version 161: Set the default value for
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08003915 // MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY and
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08003916 // SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT
3917 final SettingsState globalSettings = getGlobalSettingsLocked();
3918
3919 String oldValue = globalSettings.getSettingLocked(
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08003920 Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY).getValue();
3921 if (TextUtils.equals(null, oldValue)) {
3922 globalSettings.insertSettingLocked(
3923 Settings.Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY,
3924 Integer.toString(getContext().getResources().getInteger(
3925 R.integer.def_max_sound_trigger_detection_service_ops_per_day)),
3926 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3927 }
3928
3929 oldValue = globalSettings.getSettingLocked(
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08003930 Global.SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT).getValue();
3931 if (TextUtils.equals(null, oldValue)) {
3932 globalSettings.insertSettingLocked(
3933 Settings.Global.SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT,
3934 Integer.toString(getContext().getResources().getInteger(
3935 R.integer.def_sound_trigger_detection_service_op_timeout)),
3936 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3937 }
3938 currentVersion = 161;
3939 }
3940
Mike Digman55272862018-02-20 14:35:17 -08003941 if (currentVersion == 161) {
3942 // Version 161: Add a gesture for silencing phones
3943 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3944 final Setting currentSetting = secureSettings.getSettingLocked(
3945 Secure.VOLUME_HUSH_GESTURE);
3946 if (currentSetting.isNull()) {
3947 secureSettings.insertSettingLocked(
3948 Secure.VOLUME_HUSH_GESTURE,
3949 Integer.toString(Secure.VOLUME_HUSH_VIBRATE),
3950 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3951 }
3952
3953 currentVersion = 162;
3954 }
3955
Julia Reynoldsc4e5ecf2018-04-11 11:33:32 -04003956 if (currentVersion == 162) {
Julia Reynolds76bfa602018-04-23 09:38:47 -04003957 // Version 162: REMOVED: Add a gesture for silencing phones
Julia Reynoldsc4e5ecf2018-04-11 11:33:32 -04003958 currentVersion = 163;
3959 }
3960
Philip P. Moltmanncb58a832018-04-16 09:19:42 -07003961 if (currentVersion == 163) {
3962 // Version 163: Update default value of
3963 // MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY from old to new default
3964 final SettingsState settings = getGlobalSettingsLocked();
3965 final Setting currentSetting = settings.getSettingLocked(
3966 Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY);
3967 if (currentSetting.isDefaultFromSystem()) {
3968 settings.insertSettingLocked(
3969 Settings.Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY,
3970 Integer.toString(getContext().getResources().getInteger(
3971 R.integer
3972 .def_max_sound_trigger_detection_service_ops_per_day)),
3973 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
3974 }
3975
3976 currentVersion = 164;
3977 }
3978
Julia Reynolds76bfa602018-04-23 09:38:47 -04003979 if (currentVersion == 164) {
Julia Reynolds24836c52018-06-06 14:36:03 -04003980 // Version 164: REMOVED: show zen upgrade notification
Julia Reynolds76bfa602018-04-23 09:38:47 -04003981 currentVersion = 165;
3982 }
3983
Beverly301e92a2018-04-27 09:43:05 -04003984 if (currentVersion == 165) {
Beverly91d0a632018-07-02 16:45:00 -04003985 // Version 165: MOVED: Show zen settings suggestion and zen updated settings
3986 // moved to secure settings and are set in version 169
Beverly301e92a2018-04-27 09:43:05 -04003987 currentVersion = 166;
3988 }
3989
Beverly38fcfd02018-05-18 17:33:40 -04003990 if (currentVersion == 166) {
3991 // Version 166: add default values for hush gesture used and manual ringer
3992 // toggle
3993 final SettingsState secureSettings = getSecureSettingsLocked(userId);
3994 Setting currentHushUsedSetting = secureSettings.getSettingLocked(
3995 Secure.HUSH_GESTURE_USED);
3996 if (currentHushUsedSetting.isNull()) {
3997 secureSettings.insertSettingLocked(
3998 Settings.Secure.HUSH_GESTURE_USED, "0", null, true,
3999 SettingsState.SYSTEM_PACKAGE_NAME);
4000 }
4001
4002 Setting currentRingerToggleCountSetting = secureSettings.getSettingLocked(
4003 Secure.MANUAL_RINGER_TOGGLE_COUNT);
4004 if (currentRingerToggleCountSetting.isNull()) {
4005 secureSettings.insertSettingLocked(
4006 Settings.Secure.MANUAL_RINGER_TOGGLE_COUNT, "0", null, true,
4007 SettingsState.SYSTEM_PACKAGE_NAME);
4008 }
4009 currentVersion = 167;
4010 }
4011
Beverly155c9d22018-05-23 18:03:23 -04004012 if (currentVersion == 167) {
Beverly91d0a632018-07-02 16:45:00 -04004013 // Version 167: MOVED - Settings.Global.CHARGING_VIBRATION_ENABLED moved to
4014 // Settings.Secure.CHARGING_VIBRATION_ENABLED, set in version 170
Beverly155c9d22018-05-23 18:03:23 -04004015 currentVersion = 168;
4016 }
4017
Michael Wright0e9eeee2018-05-26 00:31:20 +01004018 if (currentVersion == 168) {
4019 // Version 168: by default, vibrate for phone calls
4020 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4021 final Setting currentSetting = systemSettings.getSettingLocked(
4022 Settings.System.VIBRATE_WHEN_RINGING);
4023 if (currentSetting.isNull()) {
4024 systemSettings.insertSettingLocked(
4025 Settings.System.VIBRATE_WHEN_RINGING,
4026 getContext().getResources().getBoolean(
4027 R.bool.def_vibrate_when_ringing) ? "1" : "0",
4028 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4029 }
4030 currentVersion = 169;
4031 }
4032
Nadav Barf9f115d2018-06-24 10:06:50 +03004033 if (currentVersion == 169) {
Beverly91d0a632018-07-02 16:45:00 -04004034 // Version 169: Set the default value for Secure Settings ZEN_DURATION,
4035 // SHOW_ZEN_SETTINGS_SUGGESTION, ZEN_SETTINGS_UPDATE and
4036 // ZEN_SETTINGS_SUGGESTION_VIEWED
Nadav Barf9f115d2018-06-24 10:06:50 +03004037
Beverly91d0a632018-07-02 16:45:00 -04004038 final SettingsState globalSettings = getGlobalSettingsLocked();
4039 final Setting globalZenDuration = globalSettings.getSettingLocked(
4040 Global.ZEN_DURATION);
4041
4042 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4043 final Setting secureZenDuration = secureSettings.getSettingLocked(
4044 Secure.ZEN_DURATION);
4045
4046 // ZEN_DURATION
4047 if (!globalZenDuration.isNull()) {
4048 secureSettings.insertSettingLocked(
4049 Secure.ZEN_DURATION, globalZenDuration.getValue(), null, false,
4050 SettingsState.SYSTEM_PACKAGE_NAME);
4051
4052 // set global zen duration setting to null since it's deprecated
4053 globalSettings.insertSettingLocked(
4054 Global.ZEN_DURATION, null, null, true,
4055 SettingsState.SYSTEM_PACKAGE_NAME);
4056 } else if (secureZenDuration.isNull()) {
4057 String defaultZenDuration = Integer.toString(getContext()
4058 .getResources().getInteger(R.integer.def_zen_duration));
4059 secureSettings.insertSettingLocked(
4060 Secure.ZEN_DURATION, defaultZenDuration, null, true,
4061 SettingsState.SYSTEM_PACKAGE_NAME);
Nadav Barf9f115d2018-06-24 10:06:50 +03004062 }
Beverly91d0a632018-07-02 16:45:00 -04004063
4064 // SHOW_ZEN_SETTINGS_SUGGESTION
4065 final Setting currentShowZenSettingSuggestion = secureSettings.getSettingLocked(
4066 Secure.SHOW_ZEN_SETTINGS_SUGGESTION);
4067 if (currentShowZenSettingSuggestion.isNull()) {
4068 secureSettings.insertSettingLocked(
4069 Secure.SHOW_ZEN_SETTINGS_SUGGESTION, "1",
4070 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4071 }
4072
4073 // ZEN_SETTINGS_UPDATED
4074 final Setting currentUpdatedSetting = secureSettings.getSettingLocked(
4075 Secure.ZEN_SETTINGS_UPDATED);
4076 if (currentUpdatedSetting.isNull()) {
4077 secureSettings.insertSettingLocked(
4078 Secure.ZEN_SETTINGS_UPDATED, "0",
4079 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4080 }
4081
4082 // ZEN_SETTINGS_SUGGESTION_VIEWED
4083 final Setting currentSettingSuggestionViewed = secureSettings.getSettingLocked(
4084 Secure.ZEN_SETTINGS_SUGGESTION_VIEWED);
4085 if (currentSettingSuggestionViewed.isNull()) {
4086 secureSettings.insertSettingLocked(
4087 Secure.ZEN_SETTINGS_SUGGESTION_VIEWED, "0",
4088 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4089 }
4090
Nadav Barf9f115d2018-06-24 10:06:50 +03004091 currentVersion = 170;
4092 }
4093
Beverly91d0a632018-07-02 16:45:00 -04004094 if (currentVersion == 170) {
4095 // Version 170: Set the default value for Secure Settings:
4096 // CHARGING_SOUNDS_ENABLED and CHARGING_VIBRATION_ENABLED
4097
4098 final SettingsState globalSettings = getGlobalSettingsLocked();
4099 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4100
4101 // CHARGING_SOUNDS_ENABLED
4102 final Setting globalChargingSoundEnabled = globalSettings.getSettingLocked(
4103 Global.CHARGING_SOUNDS_ENABLED);
4104 final Setting secureChargingSoundsEnabled = secureSettings.getSettingLocked(
4105 Secure.CHARGING_SOUNDS_ENABLED);
4106
4107 if (!globalChargingSoundEnabled.isNull()) {
Beverlyff9c5872019-01-02 15:44:00 -05004108 if (secureChargingSoundsEnabled.isNull()) {
4109 secureSettings.insertSettingLocked(
4110 Secure.CHARGING_SOUNDS_ENABLED,
4111 globalChargingSoundEnabled.getValue(), null, false,
4112 SettingsState.SYSTEM_PACKAGE_NAME);
4113 }
Beverly91d0a632018-07-02 16:45:00 -04004114
4115 // set global charging_sounds_enabled setting to null since it's deprecated
4116 globalSettings.insertSettingLocked(
4117 Global.CHARGING_SOUNDS_ENABLED, null, null, true,
4118 SettingsState.SYSTEM_PACKAGE_NAME);
4119 } else if (secureChargingSoundsEnabled.isNull()) {
4120 String defChargingSoundsEnabled = getContext().getResources()
4121 .getBoolean(R.bool.def_charging_sounds_enabled) ? "1" : "0";
4122 secureSettings.insertSettingLocked(
4123 Secure.CHARGING_SOUNDS_ENABLED, defChargingSoundsEnabled, null,
4124 true, SettingsState.SYSTEM_PACKAGE_NAME);
4125 }
4126
4127 // CHARGING_VIBRATION_ENABLED
4128 final Setting secureChargingVibrationEnabled = secureSettings.getSettingLocked(
4129 Secure.CHARGING_VIBRATION_ENABLED);
4130
4131 if (secureChargingVibrationEnabled.isNull()) {
4132 String defChargingVibrationEnabled = getContext().getResources()
4133 .getBoolean(R.bool.def_charging_vibration_enabled) ? "1" : "0";
4134 secureSettings.insertSettingLocked(
4135 Secure.CHARGING_VIBRATION_ENABLED, defChargingVibrationEnabled,
4136 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4137 }
4138
4139 currentVersion = 171;
4140 }
4141
Nadav Bar8bc8c9e2018-09-12 15:41:51 +03004142 if (currentVersion == 171) {
4143 // Version 171: by default, add STREAM_VOICE_CALL to list of streams that can
4144 // be muted.
4145 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4146 final Setting currentSetting = systemSettings.getSettingLocked(
4147 Settings.System.MUTE_STREAMS_AFFECTED);
4148 if (!currentSetting.isNull()) {
4149 try {
4150 int currentSettingIntegerValue = Integer.parseInt(
4151 currentSetting.getValue());
4152 if ((currentSettingIntegerValue
4153 & (1 << AudioManager.STREAM_VOICE_CALL)) == 0) {
4154 systemSettings.insertSettingLocked(
4155 Settings.System.MUTE_STREAMS_AFFECTED,
4156 Integer.toString(
4157 currentSettingIntegerValue
4158 | (1 << AudioManager.STREAM_VOICE_CALL)),
4159 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4160 }
4161 } catch (NumberFormatException e) {
4162 // remove the setting in case it is not a valid integer
4163 Slog.w("Failed to parse integer value of MUTE_STREAMS_AFFECTED"
4164 + "setting, removing setting", e);
4165 systemSettings.deleteSettingLocked(
4166 Settings.System.MUTE_STREAMS_AFFECTED);
4167 }
4168
4169 }
4170 currentVersion = 172;
4171 }
4172
Soonil Nagarkar42da1b12019-01-22 11:29:27 -08004173 if (currentVersion == 172) {
4174 // Version 172: Set the default value for Secure Settings: LOCATION_MODE
4175
4176 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4177
4178 final Setting locationMode = secureSettings.getSettingLocked(
4179 Secure.LOCATION_MODE);
4180
4181 if (locationMode.isNull()) {
4182 final Setting locationProvidersAllowed = secureSettings.getSettingLocked(
4183 Secure.LOCATION_PROVIDERS_ALLOWED);
4184
Soonil Nagarkar4ee3ac22019-02-08 19:19:24 -08004185 final int defLocationMode;
4186 if (locationProvidersAllowed.isNull()) {
4187 defLocationMode = getContext().getResources().getInteger(
4188 R.integer.def_location_mode);
4189 } else {
4190 defLocationMode =
4191 !TextUtils.isEmpty(locationProvidersAllowed.getValue())
4192 ? Secure.LOCATION_MODE_ON
4193 : Secure.LOCATION_MODE_OFF;
4194 }
Soonil Nagarkar42da1b12019-01-22 11:29:27 -08004195 secureSettings.insertSettingLocked(
Soonil Nagarkar4ee3ac22019-02-08 19:19:24 -08004196 Secure.LOCATION_MODE, Integer.toString(defLocationMode),
Soonil Nagarkar42da1b12019-01-22 11:29:27 -08004197 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4198 }
4199
4200 currentVersion = 173;
4201 }
4202
Beverly4179f992019-02-08 11:34:16 -05004203 if (currentVersion == 173) {
4204 // Version 173: Set the default value for Secure Settings: NOTIFICATION_BUBBLES
4205
4206 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4207
4208 final Setting bubblesSetting = secureSettings.getSettingLocked(
4209 Secure.NOTIFICATION_BUBBLES);
4210
4211 if (bubblesSetting.isNull()) {
4212 secureSettings.insertSettingLocked(Secure.NOTIFICATION_BUBBLES,
4213 getContext().getResources().getBoolean(
4214 R.bool.def_notification_bubbles) ? "1" : "0", null,
4215 true, SettingsState.SYSTEM_PACKAGE_NAME);
4216 }
4217
4218 currentVersion = 174;
4219 }
4220
Yiwen Chen0305b572019-03-05 11:26:59 -08004221 if (currentVersion == 174) {
4222 // Version 174: Set the default value for Global Settings: APPLY_RAMPING_RINGER
4223
4224 final SettingsState globalSettings = getGlobalSettingsLocked();
4225
4226 Setting currentRampingRingerSetting = globalSettings.getSettingLocked(
4227 Settings.Global.APPLY_RAMPING_RINGER);
4228 if (currentRampingRingerSetting.isNull()) {
4229 globalSettings.insertSettingLocked(
4230 Settings.Global.APPLY_RAMPING_RINGER,
4231 getContext().getResources().getBoolean(
4232 R.bool.def_apply_ramping_ringer) ? "1" : "0", null,
4233 true, SettingsState.SYSTEM_PACKAGE_NAME);
4234 }
4235
4236 currentVersion = 175;
4237 }
4238
wilsonshih5d999e22019-03-20 11:50:42 +08004239 if (currentVersion == 175) {
4240 // Version 175: Set the default value for System Settings:
4241 // RING_VIBRATION_INTENSITY. If the notification vibration intensity has been
4242 // set and ring vibration intensity hasn't, the ring vibration intensity should
4243 // followed notification vibration intensity.
4244
4245 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4246
4247 Setting notificationVibrationIntensity = systemSettings.getSettingLocked(
4248 Settings.System.NOTIFICATION_VIBRATION_INTENSITY);
4249
4250 Setting ringVibrationIntensity = systemSettings.getSettingLocked(
4251 Settings.System.RING_VIBRATION_INTENSITY);
4252
4253 if (!notificationVibrationIntensity.isNull()
4254 && ringVibrationIntensity.isNull()) {
4255 systemSettings.insertSettingLocked(
4256 Settings.System.RING_VIBRATION_INTENSITY,
4257 notificationVibrationIntensity.getValue(),
4258 null , true, SettingsState.SYSTEM_PACKAGE_NAME);
4259 }
4260
4261 currentVersion = 176;
4262 }
4263
Winson Chungd9f2fb32019-03-05 11:10:12 -08004264 if (currentVersion == 176) {
4265 // Version 176: Migrate the existing swipe up setting into the resource overlay
Winson Chung3d5d3b32019-04-24 16:52:47 -07004266 // for the navigation bar interaction mode. We do so only if the
4267 // setting is set.
Winson Chungd9f2fb32019-03-05 11:10:12 -08004268
Winson Chungd9f2fb32019-03-05 11:10:12 -08004269 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4270 final Setting swipeUpSetting = secureSettings.getSettingLocked(
Winson Chung85e74592019-03-06 11:49:22 -08004271 "swipe_up_to_switch_apps_enabled");
Winson Chung3d5d3b32019-04-24 16:52:47 -07004272 if (swipeUpSetting != null && !swipeUpSetting.isNull()
4273 && swipeUpSetting.getValue().equals("1")) {
4274 final IOverlayManager overlayManager = IOverlayManager.Stub.asInterface(
4275 ServiceManager.getService(Context.OVERLAY_SERVICE));
Winson Chungd9f2fb32019-03-05 11:10:12 -08004276 try {
Winson Chung3d5d3b32019-04-24 16:52:47 -07004277 overlayManager.setEnabledExclusiveInCategory(
4278 NAV_BAR_MODE_2BUTTON_OVERLAY, UserHandle.USER_CURRENT);
Winson Chungd9f2fb32019-03-05 11:10:12 -08004279 } catch (RemoteException e) {
4280 throw new IllegalStateException(
4281 "Failed to set nav bar interaction mode overlay");
4282 }
4283 }
4284
4285 currentVersion = 177;
4286 }
4287
Edgar Wangeb8bddb2019-04-19 17:29:49 +08004288 if (currentVersion == 177) {
4289 // Version 177: Set the default value for Secure Settings: AWARE_ENABLED
4290
4291 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4292
4293 final Setting awareEnabled = secureSettings.getSettingLocked(
4294 Secure.AWARE_ENABLED);
4295
4296 if (awareEnabled.isNull()) {
4297 final boolean defAwareEnabled = getContext().getResources().getBoolean(
4298 R.bool.def_aware_enabled);
4299 secureSettings.insertSettingLocked(
4300 Secure.AWARE_ENABLED, defAwareEnabled ? "1" : "0",
4301 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4302 }
4303
4304 currentVersion = 178;
4305 }
4306
Edgar Wangdabc41e2019-05-07 18:53:31 +08004307 if (currentVersion == 178) {
4308 // Version 178: Set the default value for Secure Settings:
4309 // SKIP_GESTURE & SILENCE_GESTURE
4310
4311 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4312
4313 final Setting skipGesture = secureSettings.getSettingLocked(
4314 Secure.SKIP_GESTURE);
4315
4316 if (skipGesture.isNull()) {
4317 final boolean defSkipGesture = getContext().getResources().getBoolean(
4318 R.bool.def_skip_gesture);
4319 secureSettings.insertSettingLocked(
4320 Secure.SKIP_GESTURE, defSkipGesture ? "1" : "0",
4321 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4322 }
4323
4324 final Setting silenceGesture = secureSettings.getSettingLocked(
4325 Secure.SILENCE_GESTURE);
4326
4327 if (silenceGesture.isNull()) {
4328 final boolean defSilenceGesture = getContext().getResources().getBoolean(
4329 R.bool.def_silence_gesture);
4330 secureSettings.insertSettingLocked(
4331 Secure.SILENCE_GESTURE, defSilenceGesture ? "1" : "0",
4332 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4333 }
4334
4335 currentVersion = 179;
4336 }
4337
Mady Mellor95e879a2019-05-06 08:50:06 -07004338 if (currentVersion == 179) {
4339 // Version 178: Reset the default for Secure Settings: NOTIFICATION_BUBBLES
4340 // This is originally set in version 173, however, the default value changed
4341 // so this step is to ensure the value is updated to the correct defaulte
4342 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4343
4344 secureSettings.insertSettingLocked(Secure.NOTIFICATION_BUBBLES,
4345 getContext().getResources().getBoolean(
4346 R.bool.def_notification_bubbles) ? "1" : "0", null,
4347 true, SettingsState.SYSTEM_PACKAGE_NAME);
4348
4349 currentVersion = 180;
4350 }
Winson Chungd9f2fb32019-03-05 11:10:12 -08004351
Edgar Wang4870e4b2019-05-15 19:14:43 +08004352 if (currentVersion == 180) {
4353 // Version 180: Set the default value for Secure Settings: AWARE_LOCK_ENABLED
4354
4355 final SettingsState secureSettings = getSecureSettingsLocked(userId);
4356
4357 final Setting awareLockEnabled = secureSettings.getSettingLocked(
4358 Secure.AWARE_LOCK_ENABLED);
4359
4360 if (awareLockEnabled.isNull()) {
4361 final boolean defAwareLockEnabled = getContext().getResources().getBoolean(
4362 R.bool.def_aware_lock_enabled);
4363 secureSettings.insertSettingLocked(
4364 Secure.AWARE_LOCK_ENABLED, defAwareLockEnabled ? "1" : "0",
4365 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4366 }
4367
4368 currentVersion = 181;
4369 }
4370
Eric Laurent02153e62019-06-12 17:20:03 -07004371 if (currentVersion == 181) {
4372 // Version cd : by default, add STREAM_BLUETOOTH_SCO to list of streams that can
4373 // be muted.
4374 final SettingsState systemSettings = getSystemSettingsLocked(userId);
4375 final Setting currentSetting = systemSettings.getSettingLocked(
4376 Settings.System.MUTE_STREAMS_AFFECTED);
4377 if (!currentSetting.isNull()) {
4378 try {
4379 int currentSettingIntegerValue = Integer.parseInt(
4380 currentSetting.getValue());
4381 if ((currentSettingIntegerValue
4382 & (1 << AudioManager.STREAM_BLUETOOTH_SCO)) == 0) {
4383 systemSettings.insertSettingLocked(
4384 Settings.System.MUTE_STREAMS_AFFECTED,
4385 Integer.toString(
4386 currentSettingIntegerValue
4387 | (1 << AudioManager.STREAM_BLUETOOTH_SCO)),
4388 null, true, SettingsState.SYSTEM_PACKAGE_NAME);
4389 }
4390 } catch (NumberFormatException e) {
4391 // remove the setting in case it is not a valid integer
4392 Slog.w("Failed to parse integer value of MUTE_STREAMS_AFFECTED"
4393 + "setting, removing setting", e);
4394 systemSettings.deleteSettingLocked(
4395 Settings.System.MUTE_STREAMS_AFFECTED);
4396 }
4397
4398 }
4399 currentVersion = 182;
4400 }
4401
Felipe Lemeff355092017-04-03 12:55:02 -07004402 // vXXX: Add new settings above this point.
4403
Dan Sandler71f85e92016-07-20 13:46:05 -04004404 if (currentVersion != newVersion) {
Svetoslav Ganov264c7a92016-08-24 17:31:14 -07004405 Slog.wtf("SettingsProvider", "warning: upgrading settings database to version "
Dan Sandler71f85e92016-07-20 13:46:05 -04004406 + newVersion + " left it at "
Stephen Chen5d0922f2017-03-27 10:28:04 -07004407 + currentVersion +
4408 " instead; this is probably a bug. Did you update SETTINGS_VERSION?",
4409 new Throwable());
Dan Sandler71f85e92016-07-20 13:46:05 -04004410 if (DEBUG) {
4411 throw new RuntimeException("db upgrade error");
4412 }
4413 }
4414
Jeff Brown503cffc2015-03-26 18:08:51 -07004415 // Return the current version.
4416 return currentVersion;
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -08004417 }
4418 }
Svet Ganov13701552017-02-23 12:45:17 -08004419
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004420 private void ensureLegacyDefaultValueAndSystemSetUpdatedLocked(SettingsState settings,
4421 int userId) {
Svet Ganov13701552017-02-23 12:45:17 -08004422 List<String> names = settings.getSettingNamesLocked();
4423 final int nameCount = names.size();
4424 for (int i = 0; i < nameCount; i++) {
4425 String name = names.get(i);
4426 Setting setting = settings.getSettingLocked(name);
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004427
4428 // In the upgrade case we pretend the call is made from the app
4429 // that made the last change to the setting to properly determine
4430 // whether the call has been made by a system component.
4431 int callingUid = -1;
4432 try {
4433 callingUid = mPackageManager.getPackageUid(setting.getPackageName(), 0, userId);
4434 } catch (RemoteException e) {
4435 /* ignore - handled below */
4436 }
4437 if (callingUid < 0) {
4438 Slog.e(LOG_TAG, "Unknown package: " + setting.getPackageName());
4439 continue;
4440 }
4441 try {
4442 final boolean systemSet = SettingsState.isSystemPackage(getContext(),
Matt Pape25c940d2019-03-26 12:14:33 -07004443 setting.getPackageName(), callingUid, userId);
Svet Ganov13701552017-02-23 12:45:17 -08004444 if (systemSet) {
4445 settings.insertSettingLocked(name, setting.getValue(),
4446 setting.getTag(), true, setting.getPackageName());
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004447 } else if (setting.getDefaultValue() != null && setting.isDefaultFromSystem()) {
4448 // We had a bug where changes by non-system packages were marked
4449 // as system made and as a result set as the default. Therefore, if
4450 // the package changed the setting last is not a system one but the
4451 // setting is marked as its default coming from the system we clear
4452 // the default and clear the system set flag.
4453 settings.resetSettingDefaultValueLocked(name);
Svet Ganov13701552017-02-23 12:45:17 -08004454 }
Svetoslav Ganovf7654c52017-08-16 17:33:22 -07004455 } catch (IllegalStateException e) {
4456 // If the package goes over its quota during the upgrade, don't
4457 // crash but just log the error as the system does the upgrade.
4458 Slog.e(LOG_TAG, "Error upgrading setting: " + setting.getName(), e);
4459
Svet Ganov13701552017-02-23 12:45:17 -08004460 }
4461 }
4462 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08004463 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07004464}