The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.providers.settings; |
| 18 | |
Matt Pape | 25c940d | 2019-03-26 12:14:33 -0700 | [diff] [blame] | 19 | import static android.os.Process.INVALID_UID; |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 20 | import static android.os.Process.ROOT_UID; |
| 21 | import static android.os.Process.SHELL_UID; |
| 22 | import static android.os.Process.SYSTEM_UID; |
Winson Chung | d9f2fb3 | 2019-03-05 11:10:12 -0800 | [diff] [blame] | 23 | import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_2BUTTON_OVERLAY; |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 24 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 25 | import android.Manifest; |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame] | 26 | import android.annotation.NonNull; |
Kweku Adams | b0886f3 | 2017-10-31 15:32:09 -0700 | [diff] [blame] | 27 | import android.annotation.Nullable; |
Christopher Tate | d5fe147 | 2012-09-10 15:48:38 -0700 | [diff] [blame] | 28 | import android.app.ActivityManager; |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 29 | import android.app.AppGlobals; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 30 | import android.app.backup.BackupManager; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 31 | import android.content.BroadcastReceiver; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 32 | import android.content.ComponentName; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | import android.content.ContentProvider; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | import android.content.ContentValues; |
| 35 | import android.content.Context; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 36 | import android.content.Intent; |
| 37 | import android.content.IntentFilter; |
Winson Chung | d9f2fb3 | 2019-03-05 11:10:12 -0800 | [diff] [blame] | 38 | import android.content.om.IOverlayManager; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 39 | import android.content.pm.ApplicationInfo; |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 40 | import android.content.pm.IPackageManager; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 41 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 42 | import android.content.pm.PackageManager; |
Christopher Tate | 38e7a60 | 2013-09-03 16:57:34 -0700 | [diff] [blame] | 43 | import android.content.pm.UserInfo; |
Chad Brubaker | 20e0dc3 | 2017-04-28 18:24:55 -0700 | [diff] [blame] | 44 | import android.content.res.Resources; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 45 | import android.database.Cursor; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 46 | import android.database.MatrixCursor; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 47 | import android.database.sqlite.SQLiteDatabase; |
| 48 | import android.database.sqlite.SQLiteQueryBuilder; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 49 | import android.hardware.camera2.utils.ArrayUtils; |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 50 | import android.media.AudioManager; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 51 | import android.net.Uri; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 52 | import android.os.Binder; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 53 | import android.os.Build; |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 54 | import android.os.Bundle; |
Amith Yamasani | 5cdf7f5 | 2013-06-27 15:12:01 -0700 | [diff] [blame] | 55 | import android.os.DropBoxManager; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 56 | import android.os.Environment; |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 57 | import android.os.Handler; |
Svet Ganov | a8f9026 | 2016-05-10 08:44:48 -0700 | [diff] [blame] | 58 | import android.os.HandlerThread; |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 59 | import android.os.Looper; |
| 60 | import android.os.Message; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 61 | import android.os.ParcelFileDescriptor; |
Christopher Tate | 0da1357 | 2013-10-13 17:34:49 -0700 | [diff] [blame] | 62 | import android.os.Process; |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 63 | import android.os.RemoteException; |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 64 | import android.os.SELinux; |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 65 | import android.os.ServiceManager; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 66 | import android.os.UserHandle; |
| 67 | import android.os.UserManager; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 68 | import android.os.UserManagerInternal; |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 69 | import android.provider.DeviceConfig; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 70 | import android.provider.Settings; |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 71 | import android.provider.Settings.Global; |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 72 | import android.provider.Settings.Secure; |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 73 | import android.provider.SettingsValidators; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 74 | import android.text.TextUtils; |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 75 | import android.util.ArrayMap; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 76 | import android.util.ArraySet; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 77 | import android.util.Slog; |
| 78 | import android.util.SparseArray; |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 79 | import android.util.SparseBooleanArray; |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame] | 80 | import android.util.proto.ProtoOutputStream; |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 81 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 82 | import com.android.internal.annotations.GuardedBy; |
| 83 | import com.android.internal.content.PackageMonitor; |
| 84 | import com.android.internal.os.BackgroundThread; |
Suprabh Shukla | 269c11e | 2015-12-02 16:51:16 -0800 | [diff] [blame] | 85 | import com.android.providers.settings.SettingsState.Setting; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 86 | import com.android.server.LocalServices; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 87 | import com.android.server.SystemConfig; |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 88 | |
Irina Dumitrescu | e369687 | 2019-01-09 16:07:59 +0000 | [diff] [blame] | 89 | import com.google.android.collect.Sets; |
| 90 | |
Neil Fuller | 3447fb4 | 2019-04-08 22:03:14 +0100 | [diff] [blame] | 91 | import libcore.util.HexEncoding; |
| 92 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 93 | import java.io.File; |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 94 | import java.io.FileDescriptor; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 95 | import java.io.FileNotFoundException; |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 96 | import java.io.PrintWriter; |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 97 | import java.nio.ByteBuffer; |
| 98 | import java.security.InvalidKeyException; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 99 | import java.security.NoSuchAlgorithmException; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 100 | import java.security.SecureRandom; |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 101 | import java.util.ArrayList; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 102 | import java.util.Arrays; |
Robin Lee | 7af9a74 | 2017-02-20 14:47:30 +0000 | [diff] [blame] | 103 | import java.util.Collection; |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 104 | import java.util.HashMap; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 105 | import java.util.HashSet; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 106 | import java.util.List; |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 107 | import java.util.Map; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 108 | import java.util.Set; |
| 109 | import java.util.regex.Pattern; |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 110 | |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 111 | import javax.crypto.Mac; |
| 112 | import javax.crypto.spec.SecretKeySpec; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 113 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 114 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 115 | /** |
| 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 Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 147 | public class SettingsProvider extends ContentProvider { |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 148 | static final boolean DEBUG = false; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 149 | |
Svetoslav Ganov | 264c7a9 | 2016-08-24 17:31:14 -0700 | [diff] [blame] | 150 | private static final boolean DROP_DATABASE_ON_MIGRATION = true; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 151 | |
| 152 | private static final String LOG_TAG = "SettingsProvider"; |
Christopher Tate | 0da1357 | 2013-10-13 17:34:49 -0700 | [diff] [blame] | 153 | |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 154 | private static final String TABLE_SYSTEM = "system"; |
| 155 | private static final String TABLE_SECURE = "secure"; |
| 156 | private static final String TABLE_GLOBAL = "global"; |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 157 | private static final String TABLE_CONFIG = "config"; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 158 | |
| 159 | // Old tables no longer exist. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | private static final String TABLE_FAVORITES = "favorites"; |
| 161 | private static final String TABLE_OLD_FAVORITES = "old_favorites"; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 162 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 166 | // The set of removed legacy tables. |
| 167 | private static final Set<String> REMOVED_LEGACY_TABLES = new ArraySet<>(); |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 168 | static { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 169 | 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 Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 175 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 176 | 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 Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 179 | private static final int MUTATION_OPERATION_RESET = 4; |
Julia Reynolds | 5e458dd | 2014-07-07 16:07:01 -0400 | [diff] [blame] | 180 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 181 | private static final String[] ALL_COLUMNS = new String[] { |
| 182 | Settings.NameValueTable._ID, |
| 183 | Settings.NameValueTable.NAME, |
| 184 | Settings.NameValueTable.VALUE |
| 185 | }; |
| 186 | |
Makoto Onuki | 53f0e02 | 2017-11-29 13:51:01 -0800 | [diff] [blame] | 187 | 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 Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 191 | public static final int SETTINGS_TYPE_CONFIG = SettingsState.SETTINGS_TYPE_CONFIG; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 192 | |
| 193 | private static final Bundle NULL_SETTING_BUNDLE = Bundle.forPair( |
| 194 | Settings.NameValueTable.VALUE, null); |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 195 | |
Nicholas Sauer | 3d87d1e | 2018-11-06 08:38:39 -0800 | [diff] [blame] | 196 | public static final String RESULT_ROWS_DELETED = "result_rows_deleted"; |
Nicholas Sauer | 7250053 | 2018-11-21 10:30:58 -0800 | [diff] [blame] | 197 | public static final String RESULT_SETTINGS_LIST = "result_settings_list"; |
Nicholas Sauer | 3d87d1e | 2018-11-06 08:38:39 -0800 | [diff] [blame] | 198 | |
Chad Brubaker | 20e0dc3 | 2017-04-28 18:24:55 -0700 | [diff] [blame] | 199 | // 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 Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 219 | // 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 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 231 | // 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 Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 237 | // 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 Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 243 | // 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 Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 249 | // 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 Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 255 | // 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 Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 261 | // 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 Reynolds | 5e458dd | 2014-07-07 16:07:01 -0400 | [diff] [blame] | 265 | } |
| 266 | |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 267 | // 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 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 274 | private final Object mLock = new Object(); |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 275 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 276 | @GuardedBy("mLock") |
| 277 | private SettingsRegistry mSettingsRegistry; |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 278 | |
Svet Ganov | a8f9026 | 2016-05-10 08:44:48 -0700 | [diff] [blame] | 279 | @GuardedBy("mLock") |
| 280 | private HandlerThread mHandlerThread; |
| 281 | |
Makoto Onuki | 73360ab | 2017-03-17 11:50:13 -0700 | [diff] [blame] | 282 | @GuardedBy("mLock") |
| 283 | private Handler mHandler; |
| 284 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 285 | // We have to call in the user manager with no lock held, |
| 286 | private volatile UserManager mUserManager; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 287 | |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 288 | private UserManagerInternal mUserManagerInternal; |
| 289 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 290 | // We have to call in the package manager with no lock held, |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 291 | private volatile IPackageManager mPackageManager; |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 292 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 293 | public static int makeKey(int type, int userId) { |
Makoto Onuki | 53f0e02 | 2017-11-29 13:51:01 -0800 | [diff] [blame] | 294 | return SettingsState.makeKey(type, userId); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | public static int getTypeFromKey(int key) { |
Makoto Onuki | 53f0e02 | 2017-11-29 13:51:01 -0800 | [diff] [blame] | 298 | return SettingsState.getTypeFromKey(key); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | public static int getUserIdFromKey(int key) { |
Makoto Onuki | 53f0e02 | 2017-11-29 13:51:01 -0800 | [diff] [blame] | 302 | return SettingsState.getUserIdFromKey(key); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | public static String settingTypeToString(int type) { |
Makoto Onuki | 53f0e02 | 2017-11-29 13:51:01 -0800 | [diff] [blame] | 306 | return SettingsState.settingTypeToString(type); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | public static String keyToString(int key) { |
Makoto Onuki | 53f0e02 | 2017-11-29 13:51:01 -0800 | [diff] [blame] | 310 | return SettingsState.keyToString(key); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 311 | } |
| 312 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 313 | @Override |
| 314 | public boolean onCreate() { |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 315 | Settings.setInSystemServer(); |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 316 | |
| 317 | // fail to boot if there're any backed up settings that don't have a non-null validator |
| 318 | ensureAllBackedUpSystemSettingsHaveValidators(); |
Michal Karpinski | 5db1e43 | 2018-01-18 20:10:24 +0000 | [diff] [blame] | 319 | ensureAllBackedUpGlobalSettingsHaveValidators(); |
Michal Karpinski | 964943a | 2018-01-19 16:28:26 +0000 | [diff] [blame] | 320 | ensureAllBackedUpSecureSettingsHaveValidators(); |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 321 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 322 | synchronized (mLock) { |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 323 | mUserManager = UserManager.get(getContext()); |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 324 | mUserManagerInternal = LocalServices.getService(UserManagerInternal.class); |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 325 | mPackageManager = AppGlobals.getPackageManager(); |
Svet Ganov | a8f9026 | 2016-05-10 08:44:48 -0700 | [diff] [blame] | 326 | mHandlerThread = new HandlerThread(LOG_TAG, |
| 327 | Process.THREAD_PRIORITY_BACKGROUND); |
| 328 | mHandlerThread.start(); |
Makoto Onuki | 73360ab | 2017-03-17 11:50:13 -0700 | [diff] [blame] | 329 | mHandler = new Handler(mHandlerThread.getLooper()); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 330 | mSettingsRegistry = new SettingsRegistry(); |
| 331 | } |
Makoto Onuki | 73360ab | 2017-03-17 11:50:13 -0700 | [diff] [blame] | 332 | mHandler.post(() -> { |
| 333 | registerBroadcastReceivers(); |
| 334 | startWatchingUserRestrictionChanges(); |
| 335 | }); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 336 | ServiceManager.addService("settings", new SettingsService(this)); |
Matt Pape | abb6789 | 2018-12-07 09:13:26 -0800 | [diff] [blame] | 337 | ServiceManager.addService("device_config", new DeviceConfigService(this)); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 338 | return true; |
| 339 | } |
| 340 | |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 341 | private void ensureAllBackedUpSystemSettingsHaveValidators() { |
Michal Karpinski | 964943a | 2018-01-19 16:28:26 +0000 | [diff] [blame] | 342 | String offenders = getOffenders(concat(Settings.System.SETTINGS_TO_BACKUP, |
| 343 | Settings.System.LEGACY_RESTORE_SETTINGS), Settings.System.VALIDATORS); |
Michal Karpinski | 5db1e43 | 2018-01-18 20:10:24 +0000 | [diff] [blame] | 344 | |
| 345 | failToBootIfOffendersPresent(offenders, "Settings.System"); |
| 346 | } |
| 347 | |
| 348 | private void ensureAllBackedUpGlobalSettingsHaveValidators() { |
Michal Karpinski | 964943a | 2018-01-19 16:28:26 +0000 | [diff] [blame] | 349 | String offenders = getOffenders(concat(Settings.Global.SETTINGS_TO_BACKUP, |
| 350 | Settings.Global.LEGACY_RESTORE_SETTINGS), Settings.Global.VALIDATORS); |
Michal Karpinski | 5db1e43 | 2018-01-18 20:10:24 +0000 | [diff] [blame] | 351 | |
| 352 | failToBootIfOffendersPresent(offenders, "Settings.Global"); |
| 353 | } |
| 354 | |
Michal Karpinski | 964943a | 2018-01-19 16:28:26 +0000 | [diff] [blame] | 355 | private void ensureAllBackedUpSecureSettingsHaveValidators() { |
| 356 | String offenders = getOffenders(concat(Settings.Secure.SETTINGS_TO_BACKUP, |
| 357 | Settings.Secure.LEGACY_RESTORE_SETTINGS), Settings.Secure.VALIDATORS); |
| 358 | |
| 359 | failToBootIfOffendersPresent(offenders, "Settings.Secure"); |
| 360 | } |
| 361 | |
Michal Karpinski | 5db1e43 | 2018-01-18 20:10:24 +0000 | [diff] [blame] | 362 | private void failToBootIfOffendersPresent(String offenders, String settingsType) { |
| 363 | if (offenders.length() > 0) { |
| 364 | throw new RuntimeException("All " + settingsType + " settings that are backed up" |
| 365 | + " have to have a non-null validator, but those don't: " + offenders); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | private String getOffenders(String[] settingsToBackup, Map<String, |
| 370 | SettingsValidators.Validator> validators) { |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 371 | StringBuilder offenders = new StringBuilder(); |
Michal Karpinski | 5db1e43 | 2018-01-18 20:10:24 +0000 | [diff] [blame] | 372 | for (String setting : settingsToBackup) { |
| 373 | if (validators.get(setting) == null) { |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 374 | offenders.append(setting).append(" "); |
| 375 | } |
| 376 | } |
Michal Karpinski | 5db1e43 | 2018-01-18 20:10:24 +0000 | [diff] [blame] | 377 | return offenders.toString(); |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Michal Karpinski | 964943a | 2018-01-19 16:28:26 +0000 | [diff] [blame] | 380 | private final String[] concat(String[] first, String[] second) { |
| 381 | if (second == null || second.length == 0) { |
| 382 | return first; |
| 383 | } |
| 384 | final int firstLen = first.length; |
| 385 | final int secondLen = second.length; |
| 386 | String[] both = new String[firstLen + secondLen]; |
| 387 | System.arraycopy(first, 0, both, 0, firstLen); |
| 388 | System.arraycopy(second, 0, both, firstLen, secondLen); |
| 389 | return both; |
| 390 | } |
| 391 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 392 | @Override |
| 393 | public Bundle call(String method, String name, Bundle args) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 394 | final int requestingUserId = getRequestingUserId(args); |
| 395 | switch (method) { |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 396 | case Settings.CALL_METHOD_GET_CONFIG: { |
| 397 | Setting setting = getConfigSetting(name); |
| 398 | return packageValueForCallResult(setting, isTrackingGeneration(args)); |
| 399 | } |
| 400 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 401 | case Settings.CALL_METHOD_GET_GLOBAL: { |
| 402 | Setting setting = getGlobalSetting(name); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 403 | return packageValueForCallResult(setting, isTrackingGeneration(args)); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 404 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 405 | |
| 406 | case Settings.CALL_METHOD_GET_SECURE: { |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 407 | Setting setting = getSecureSetting(name, requestingUserId, |
| 408 | /*enableOverride=*/ true); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 409 | return packageValueForCallResult(setting, isTrackingGeneration(args)); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | case Settings.CALL_METHOD_GET_SYSTEM: { |
| 413 | Setting setting = getSystemSetting(name, requestingUserId); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 414 | return packageValueForCallResult(setting, isTrackingGeneration(args)); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 417 | case Settings.CALL_METHOD_PUT_CONFIG: { |
| 418 | String value = getSettingValue(args); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 419 | final boolean makeDefault = getSettingMakeDefault(args); |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 420 | insertConfigSetting(name, value, makeDefault); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 421 | break; |
| 422 | } |
| 423 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 424 | case Settings.CALL_METHOD_PUT_GLOBAL: { |
| 425 | String value = getSettingValue(args); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 426 | String tag = getSettingTag(args); |
| 427 | final boolean makeDefault = getSettingMakeDefault(args); |
| 428 | insertGlobalSetting(name, value, tag, makeDefault, requestingUserId, false); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 429 | break; |
| 430 | } |
| 431 | |
| 432 | case Settings.CALL_METHOD_PUT_SECURE: { |
| 433 | String value = getSettingValue(args); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 434 | String tag = getSettingTag(args); |
| 435 | final boolean makeDefault = getSettingMakeDefault(args); |
| 436 | insertSecureSetting(name, value, tag, makeDefault, requestingUserId, false); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 437 | break; |
| 438 | } |
| 439 | |
| 440 | case Settings.CALL_METHOD_PUT_SYSTEM: { |
| 441 | String value = getSettingValue(args); |
| 442 | insertSystemSetting(name, value, requestingUserId); |
| 443 | break; |
| 444 | } |
| 445 | |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 446 | case Settings.CALL_METHOD_RESET_CONFIG: { |
| 447 | final int mode = getResetModeEnforcingPermission(args); |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 448 | String prefix = getSettingPrefix(args); |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 449 | resetConfigSetting(mode, prefix); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 450 | break; |
| 451 | } |
| 452 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 453 | case Settings.CALL_METHOD_RESET_GLOBAL: { |
| 454 | final int mode = getResetModeEnforcingPermission(args); |
| 455 | String tag = getSettingTag(args); |
| 456 | resetGlobalSetting(requestingUserId, mode, tag); |
| 457 | break; |
| 458 | } |
| 459 | |
| 460 | case Settings.CALL_METHOD_RESET_SECURE: { |
| 461 | final int mode = getResetModeEnforcingPermission(args); |
| 462 | String tag = getSettingTag(args); |
| 463 | resetSecureSetting(requestingUserId, mode, tag); |
| 464 | break; |
| 465 | } |
| 466 | |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 467 | case Settings.CALL_METHOD_DELETE_CONFIG: { |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 468 | int rows = deleteConfigSetting(name) ? 1 : 0; |
Nicholas Sauer | 3d87d1e | 2018-11-06 08:38:39 -0800 | [diff] [blame] | 469 | Bundle result = new Bundle(); |
| 470 | result.putInt(RESULT_ROWS_DELETED, rows); |
| 471 | return result; |
| 472 | } |
| 473 | |
| 474 | case Settings.CALL_METHOD_DELETE_GLOBAL: { |
| 475 | int rows = deleteGlobalSetting(name, requestingUserId, false) ? 1 : 0; |
| 476 | Bundle result = new Bundle(); |
| 477 | result.putInt(RESULT_ROWS_DELETED, rows); |
| 478 | return result; |
| 479 | } |
| 480 | |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 481 | case Settings.CALL_METHOD_DELETE_SECURE: { |
| 482 | int rows = deleteSecureSetting(name, requestingUserId, false) ? 1 : 0; |
| 483 | Bundle result = new Bundle(); |
| 484 | result.putInt(RESULT_ROWS_DELETED, rows); |
| 485 | return result; |
| 486 | } |
| 487 | |
| 488 | case Settings.CALL_METHOD_DELETE_SYSTEM: { |
| 489 | int rows = deleteSystemSetting(name, requestingUserId) ? 1 : 0; |
| 490 | Bundle result = new Bundle(); |
| 491 | result.putInt(RESULT_ROWS_DELETED, rows); |
| 492 | return result; |
| 493 | } |
| 494 | |
| 495 | case Settings.CALL_METHOD_LIST_CONFIG: { |
| 496 | String prefix = getSettingPrefix(args); |
| 497 | Bundle result = new Bundle(); |
| 498 | result.putSerializable( |
| 499 | Settings.NameValueTable.VALUE, (HashMap) getAllConfigFlags(prefix)); |
| 500 | return result; |
| 501 | } |
| 502 | |
| 503 | case Settings.CALL_METHOD_LIST_GLOBAL: { |
Nicholas Sauer | 7250053 | 2018-11-21 10:30:58 -0800 | [diff] [blame] | 504 | Bundle result = new Bundle(); |
| 505 | result.putStringArrayList(RESULT_SETTINGS_LIST, |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 506 | buildSettingsList(getAllGlobalSettings(null))); |
Nicholas Sauer | 7250053 | 2018-11-21 10:30:58 -0800 | [diff] [blame] | 507 | return result; |
| 508 | } |
| 509 | |
| 510 | case Settings.CALL_METHOD_LIST_SECURE: { |
| 511 | Bundle result = new Bundle(); |
| 512 | result.putStringArrayList(RESULT_SETTINGS_LIST, |
| 513 | buildSettingsList(getAllSecureSettings(requestingUserId, null))); |
| 514 | return result; |
| 515 | } |
| 516 | |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 517 | case Settings.CALL_METHOD_LIST_SYSTEM: { |
Nicholas Sauer | 7250053 | 2018-11-21 10:30:58 -0800 | [diff] [blame] | 518 | Bundle result = new Bundle(); |
| 519 | result.putStringArrayList(RESULT_SETTINGS_LIST, |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 520 | buildSettingsList(getAllSystemSettings(requestingUserId, null))); |
Nicholas Sauer | 7250053 | 2018-11-21 10:30:58 -0800 | [diff] [blame] | 521 | return result; |
| 522 | } |
| 523 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 524 | default: { |
| 525 | Slog.w(LOG_TAG, "call() with invalid method: " + method); |
| 526 | } break; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 527 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 528 | |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 529 | return null; |
| 530 | } |
| 531 | |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 532 | @Override |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 533 | public String getType(Uri uri) { |
| 534 | Arguments args = new Arguments(uri, null, null, true); |
| 535 | if (TextUtils.isEmpty(args.name)) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 536 | return "vnd.android.cursor.dir/" + args.table; |
| 537 | } else { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 538 | return "vnd.android.cursor.item/" + args.table; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
| 542 | @Override |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 543 | public Cursor query(Uri uri, String[] projection, String where, String[] whereArgs, |
| 544 | String order) { |
| 545 | if (DEBUG) { |
| 546 | Slog.v(LOG_TAG, "query() for user: " + UserHandle.getCallingUserId()); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 549 | Arguments args = new Arguments(uri, where, whereArgs, true); |
| 550 | String[] normalizedProjection = normalizeProjection(projection); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 551 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 552 | // If a legacy table that is gone, done. |
| 553 | if (REMOVED_LEGACY_TABLES.contains(args.table)) { |
| 554 | return new MatrixCursor(normalizedProjection, 0); |
| 555 | } |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 556 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 557 | switch (args.table) { |
| 558 | case TABLE_GLOBAL: { |
| 559 | if (args.name != null) { |
| 560 | Setting setting = getGlobalSetting(args.name); |
| 561 | return packageSettingForQuery(setting, normalizedProjection); |
| 562 | } else { |
| 563 | return getAllGlobalSettings(projection); |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 564 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 565 | } |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 566 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 567 | case TABLE_SECURE: { |
| 568 | final int userId = UserHandle.getCallingUserId(); |
| 569 | if (args.name != null) { |
| 570 | Setting setting = getSecureSetting(args.name, userId); |
| 571 | return packageSettingForQuery(setting, normalizedProjection); |
| 572 | } else { |
| 573 | return getAllSecureSettings(userId, projection); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 574 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 575 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 576 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 577 | case TABLE_SYSTEM: { |
| 578 | final int userId = UserHandle.getCallingUserId(); |
| 579 | if (args.name != null) { |
| 580 | Setting setting = getSystemSetting(args.name, userId); |
| 581 | return packageSettingForQuery(setting, normalizedProjection); |
| 582 | } else { |
| 583 | return getAllSystemSettings(userId, projection); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 584 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 585 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 586 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 587 | default: { |
| 588 | throw new IllegalArgumentException("Invalid Uri path:" + uri); |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 589 | } |
| 590 | } |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Nicholas Sauer | 7250053 | 2018-11-21 10:30:58 -0800 | [diff] [blame] | 593 | private ArrayList<String> buildSettingsList(Cursor cursor) { |
| 594 | final ArrayList<String> lines = new ArrayList<String>(); |
| 595 | try { |
| 596 | while (cursor != null && cursor.moveToNext()) { |
| 597 | lines.add(cursor.getString(1) + "=" + cursor.getString(2)); |
| 598 | } |
| 599 | } finally { |
| 600 | if (cursor != null) { |
| 601 | cursor.close(); |
| 602 | } |
| 603 | } |
| 604 | return lines; |
| 605 | } |
| 606 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 607 | @Override |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 608 | public Uri insert(Uri uri, ContentValues values) { |
| 609 | if (DEBUG) { |
| 610 | Slog.v(LOG_TAG, "insert() for user: " + UserHandle.getCallingUserId()); |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 613 | String table = getValidTableOrThrow(uri); |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 614 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 615 | // If a legacy table that is gone, done. |
| 616 | if (REMOVED_LEGACY_TABLES.contains(table)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | return null; |
| 618 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 619 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 620 | String name = values.getAsString(Settings.Secure.NAME); |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 621 | if (!isKeyValid(name)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 622 | return null; |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 625 | String value = values.getAsString(Settings.Secure.VALUE); |
| 626 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 627 | switch (table) { |
| 628 | case TABLE_GLOBAL: { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 629 | if (insertGlobalSetting(name, value, null, false, |
| 630 | UserHandle.getCallingUserId(), false)) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 631 | return Uri.withAppendedPath(Settings.Global.CONTENT_URI, name); |
Christopher Tate | c221d2b | 2012-10-03 18:33:52 -0700 | [diff] [blame] | 632 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 633 | } break; |
| 634 | |
| 635 | case TABLE_SECURE: { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 636 | if (insertSecureSetting(name, value, null, false, |
| 637 | UserHandle.getCallingUserId(), false)) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 638 | return Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name); |
| 639 | } |
| 640 | } break; |
| 641 | |
| 642 | case TABLE_SYSTEM: { |
| 643 | if (insertSystemSetting(name, value, UserHandle.getCallingUserId())) { |
| 644 | return Uri.withAppendedPath(Settings.System.CONTENT_URI, name); |
| 645 | } |
| 646 | } break; |
| 647 | |
| 648 | default: { |
| 649 | throw new IllegalArgumentException("Bad Uri path:" + uri); |
Christopher Tate | c221d2b | 2012-10-03 18:33:52 -0700 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 653 | return null; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | @Override |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 657 | public int bulkInsert(Uri uri, ContentValues[] allValues) { |
| 658 | if (DEBUG) { |
| 659 | Slog.v(LOG_TAG, "bulkInsert() for user: " + UserHandle.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 660 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 661 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 662 | int insertionCount = 0; |
| 663 | final int valuesCount = allValues.length; |
| 664 | for (int i = 0; i < valuesCount; i++) { |
| 665 | ContentValues values = allValues[i]; |
| 666 | if (insert(uri, values) != null) { |
| 667 | insertionCount++; |
| 668 | } |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 669 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 670 | |
| 671 | return insertionCount; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | @Override |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 675 | public int delete(Uri uri, String where, String[] whereArgs) { |
| 676 | if (DEBUG) { |
| 677 | Slog.v(LOG_TAG, "delete() for user: " + UserHandle.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 679 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 680 | Arguments args = new Arguments(uri, where, whereArgs, false); |
| 681 | |
| 682 | // If a legacy table that is gone, done. |
| 683 | if (REMOVED_LEGACY_TABLES.contains(args.table)) { |
| 684 | return 0; |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 685 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 686 | |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 687 | if (!isKeyValid(args.name)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 688 | return 0; |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 689 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 690 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 691 | switch (args.table) { |
| 692 | case TABLE_GLOBAL: { |
| 693 | final int userId = UserHandle.getCallingUserId(); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 694 | return deleteGlobalSetting(args.name, userId, false) ? 1 : 0; |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 695 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 696 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 697 | case TABLE_SECURE: { |
| 698 | final int userId = UserHandle.getCallingUserId(); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 699 | return deleteSecureSetting(args.name, userId, false) ? 1 : 0; |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 700 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 701 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 702 | case TABLE_SYSTEM: { |
| 703 | final int userId = UserHandle.getCallingUserId(); |
| 704 | return deleteSystemSetting(args.name, userId) ? 1 : 0; |
| 705 | } |
| 706 | |
| 707 | default: { |
| 708 | throw new IllegalArgumentException("Bad Uri path:" + uri); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 709 | } |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 710 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | @Override |
| 714 | public int update(Uri uri, ContentValues values, String where, String[] whereArgs) { |
| 715 | if (DEBUG) { |
| 716 | Slog.v(LOG_TAG, "update() for user: " + UserHandle.getCallingUserId()); |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 717 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 718 | |
| 719 | Arguments args = new Arguments(uri, where, whereArgs, false); |
| 720 | |
| 721 | // If a legacy table that is gone, done. |
| 722 | if (REMOVED_LEGACY_TABLES.contains(args.table)) { |
| 723 | return 0; |
| 724 | } |
| 725 | |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 726 | String name = values.getAsString(Settings.Secure.NAME); |
| 727 | if (!isKeyValid(name)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 728 | return 0; |
| 729 | } |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 730 | String value = values.getAsString(Settings.Secure.VALUE); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 731 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 732 | switch (args.table) { |
| 733 | case TABLE_GLOBAL: { |
| 734 | final int userId = UserHandle.getCallingUserId(); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 735 | return updateGlobalSetting(args.name, value, null, false, |
| 736 | userId, false) ? 1 : 0; |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 737 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 738 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 739 | case TABLE_SECURE: { |
| 740 | final int userId = UserHandle.getCallingUserId(); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 741 | return updateSecureSetting(args.name, value, null, false, |
| 742 | userId, false) ? 1 : 0; |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 743 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 744 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 745 | case TABLE_SYSTEM: { |
| 746 | final int userId = UserHandle.getCallingUserId(); |
| 747 | return updateSystemSetting(args.name, value, userId) ? 1 : 0; |
| 748 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 749 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 750 | default: { |
| 751 | throw new IllegalArgumentException("Invalid Uri path:" + uri); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 752 | } |
| 753 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | @Override |
| 757 | public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 758 | final int userId = getUserIdFromUri(uri, UserHandle.getCallingUserId()); |
| 759 | if (userId != UserHandle.getCallingUserId()) { |
| 760 | getContext().enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS, |
| 761 | "Access files from the settings of another user"); |
| 762 | } |
| 763 | uri = ContentProvider.getUriWithoutUserId(uri); |
| 764 | |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 765 | final String cacheRingtoneSetting; |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 766 | final String cacheName; |
| 767 | if (Settings.System.RINGTONE_CACHE_URI.equals(uri)) { |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 768 | cacheRingtoneSetting = Settings.System.RINGTONE; |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 769 | cacheName = Settings.System.RINGTONE_CACHE; |
| 770 | } else if (Settings.System.NOTIFICATION_SOUND_CACHE_URI.equals(uri)) { |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 771 | cacheRingtoneSetting = Settings.System.NOTIFICATION_SOUND; |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 772 | cacheName = Settings.System.NOTIFICATION_SOUND_CACHE; |
| 773 | } else if (Settings.System.ALARM_ALERT_CACHE_URI.equals(uri)) { |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 774 | cacheRingtoneSetting = Settings.System.ALARM_ALERT; |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 775 | cacheName = Settings.System.ALARM_ALERT_CACHE; |
| 776 | } else { |
| 777 | throw new FileNotFoundException("Direct file access no longer supported; " |
| 778 | + "ringtone playback is available through android.media.Ringtone"); |
| 779 | } |
| 780 | |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 781 | int actualCacheOwner; |
| 782 | // Redirect cache to parent if ringtone setting is owned by profile parent |
| 783 | synchronized (mLock) { |
| 784 | actualCacheOwner = resolveOwningUserIdForSystemSettingLocked(userId, |
| 785 | cacheRingtoneSetting); |
| 786 | } |
| 787 | final File cacheFile = new File(getRingtoneCacheDir(actualCacheOwner), cacheName); |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 788 | return ParcelFileDescriptor.open(cacheFile, ParcelFileDescriptor.parseMode(mode)); |
| 789 | } |
| 790 | |
| 791 | private File getRingtoneCacheDir(int userId) { |
| 792 | final File cacheDir = new File(Environment.getDataSystemDeDirectory(userId), "ringtones"); |
| 793 | cacheDir.mkdir(); |
| 794 | SELinux.restorecon(cacheDir); |
| 795 | return cacheDir; |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 796 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 797 | |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame] | 798 | /** |
| 799 | * Dump all settings as a proto buf. |
| 800 | * |
| 801 | * @param fd The file to dump to |
| 802 | */ |
| 803 | void dumpProto(@NonNull FileDescriptor fd) { |
| 804 | ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 805 | |
| 806 | synchronized (mLock) { |
| 807 | SettingsProtoDumpUtil.dumpProtoLocked(mSettingsRegistry, proto); |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | proto.flush(); |
| 811 | } |
| 812 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 813 | public void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) { |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 814 | synchronized (mLock) { |
| 815 | final long identity = Binder.clearCallingIdentity(); |
| 816 | try { |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 817 | SparseBooleanArray users = mSettingsRegistry.getKnownUsersLocked(); |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 818 | final int userCount = users.size(); |
| 819 | for (int i = 0; i < userCount; i++) { |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 820 | dumpForUserLocked(users.keyAt(i), pw); |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 821 | } |
| 822 | } finally { |
| 823 | Binder.restoreCallingIdentity(identity); |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | |
Andreas Gampe | b5889307 | 2018-09-05 16:52:31 -0700 | [diff] [blame] | 828 | @GuardedBy("mLock") |
Svetoslav Ganov | a340bfd | 2016-08-02 18:24:49 -0700 | [diff] [blame] | 829 | private void dumpForUserLocked(int userId, PrintWriter pw) { |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 830 | if (userId == UserHandle.USER_SYSTEM) { |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 831 | pw.println("CONFIG SETTINGS (user " + userId + ")"); |
| 832 | SettingsState configSettings = mSettingsRegistry.getSettingsLocked( |
| 833 | SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM); |
| 834 | if (configSettings != null) { |
| 835 | dumpSettingsLocked(configSettings, pw); |
| 836 | pw.println(); |
| 837 | configSettings.dumpHistoricalOperations(pw); |
| 838 | } |
| 839 | |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 840 | pw.println("GLOBAL SETTINGS (user " + userId + ")"); |
Svetoslav Ganov | a340bfd | 2016-08-02 18:24:49 -0700 | [diff] [blame] | 841 | SettingsState globalSettings = mSettingsRegistry.getSettingsLocked( |
| 842 | SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 843 | if (globalSettings != null) { |
| 844 | dumpSettingsLocked(globalSettings, pw); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 845 | pw.println(); |
| 846 | globalSettings.dumpHistoricalOperations(pw); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 847 | } |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | pw.println("SECURE SETTINGS (user " + userId + ")"); |
Svetoslav Ganov | a340bfd | 2016-08-02 18:24:49 -0700 | [diff] [blame] | 851 | SettingsState secureSettings = mSettingsRegistry.getSettingsLocked( |
| 852 | SETTINGS_TYPE_SECURE, userId); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 853 | if (secureSettings != null) { |
| 854 | dumpSettingsLocked(secureSettings, pw); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 855 | pw.println(); |
| 856 | secureSettings.dumpHistoricalOperations(pw); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 857 | } |
Svetoslav Ganov | a340bfd | 2016-08-02 18:24:49 -0700 | [diff] [blame] | 858 | |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 859 | pw.println("SYSTEM SETTINGS (user " + userId + ")"); |
Svetoslav Ganov | a340bfd | 2016-08-02 18:24:49 -0700 | [diff] [blame] | 860 | SettingsState systemSettings = mSettingsRegistry.getSettingsLocked( |
| 861 | SETTINGS_TYPE_SYSTEM, userId); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 862 | if (systemSettings != null) { |
| 863 | dumpSettingsLocked(systemSettings, pw); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 864 | pw.println(); |
| 865 | systemSettings.dumpHistoricalOperations(pw); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 866 | } |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 867 | } |
| 868 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 869 | private void dumpSettingsLocked(SettingsState settingsState, PrintWriter pw) { |
| 870 | List<String> names = settingsState.getSettingNamesLocked(); |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 871 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 872 | final int nameCount = names.size(); |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 873 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 874 | for (int i = 0; i < nameCount; i++) { |
| 875 | String name = names.get(i); |
| 876 | Setting setting = settingsState.getSettingLocked(name); |
| 877 | pw.print("_id:"); pw.print(toDumpString(setting.getId())); |
| 878 | pw.print(" name:"); pw.print(toDumpString(name)); |
| 879 | if (setting.getPackageName() != null) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 880 | pw.print(" pkg:"); pw.print(setting.getPackageName()); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 881 | } |
| 882 | pw.print(" value:"); pw.print(toDumpString(setting.getValue())); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 883 | if (setting.getDefaultValue() != null) { |
| 884 | pw.print(" default:"); pw.print(setting.getDefaultValue()); |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame] | 885 | pw.print(" defaultSystemSet:"); pw.print(setting.isDefaultFromSystem()); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 886 | } |
| 887 | if (setting.getTag() != null) { |
| 888 | pw.print(" tag:"); pw.print(setting.getTag()); |
| 889 | } |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 890 | pw.println(); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 891 | } |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 892 | } |
| 893 | |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 894 | private static String toDumpString(String s) { |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 895 | if (s != null) { |
| 896 | return s; |
| 897 | } |
| 898 | return "{null}"; |
| 899 | } |
| 900 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 901 | private void registerBroadcastReceivers() { |
| 902 | IntentFilter userFilter = new IntentFilter(); |
| 903 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 904 | userFilter.addAction(Intent.ACTION_USER_STOPPED); |
| 905 | |
| 906 | getContext().registerReceiver(new BroadcastReceiver() { |
| 907 | @Override |
| 908 | public void onReceive(Context context, Intent intent) { |
| 909 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 910 | UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 911 | |
| 912 | switch (intent.getAction()) { |
| 913 | case Intent.ACTION_USER_REMOVED: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 914 | synchronized (mLock) { |
| 915 | mSettingsRegistry.removeUserStateLocked(userId, true); |
| 916 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 917 | } break; |
| 918 | |
| 919 | case Intent.ACTION_USER_STOPPED: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 920 | synchronized (mLock) { |
| 921 | mSettingsRegistry.removeUserStateLocked(userId, false); |
| 922 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 923 | } break; |
| 924 | } |
| 925 | } |
| 926 | }, userFilter); |
| 927 | |
| 928 | PackageMonitor monitor = new PackageMonitor() { |
| 929 | @Override |
| 930 | public void onPackageRemoved(String packageName, int uid) { |
| 931 | synchronized (mLock) { |
Zimuzo | c56192c | 2018-07-25 10:40:01 +0100 | [diff] [blame] | 932 | mSettingsRegistry.removeSettingsForPackageLocked(packageName, |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 933 | UserHandle.getUserId(uid)); |
| 934 | } |
| 935 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 936 | |
| 937 | @Override |
| 938 | public void onUidRemoved(int uid) { |
| 939 | synchronized (mLock) { |
| 940 | mSettingsRegistry.onUidRemovedLocked(uid); |
| 941 | } |
| 942 | } |
Zimuzo | c56192c | 2018-07-25 10:40:01 +0100 | [diff] [blame] | 943 | |
| 944 | @Override |
| 945 | public void onPackageDataCleared(String packageName, int uid) { |
| 946 | synchronized (mLock) { |
| 947 | mSettingsRegistry.removeSettingsForPackageLocked(packageName, |
| 948 | UserHandle.getUserId(uid)); |
| 949 | } |
| 950 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 951 | }; |
| 952 | |
| 953 | // package changes |
| 954 | monitor.register(getContext(), BackgroundThread.getHandler().getLooper(), |
| 955 | UserHandle.ALL, true); |
| 956 | } |
| 957 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 958 | private void startWatchingUserRestrictionChanges() { |
| 959 | // TODO: The current design of settings looking different based on user restrictions |
| 960 | // should be reworked to keep them separate and system code should check the setting |
| 961 | // first followed by checking the user restriction before performing an operation. |
| 962 | UserManagerInternal userManager = LocalServices.getService(UserManagerInternal.class); |
| 963 | userManager.addUserRestrictionsListener((int userId, Bundle newRestrictions, |
| 964 | Bundle prevRestrictions) -> { |
Irina Dumitrescu | e369687 | 2019-01-09 16:07:59 +0000 | [diff] [blame] | 965 | Set<String> changedRestrictions = getRestrictionDiff(prevRestrictions, newRestrictions); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 966 | // We are changing the settings affected by restrictions to their current |
| 967 | // value with a forced update to ensure that all cross profile dependencies |
| 968 | // are taken into account. Also make sure the settings update to.. the same |
| 969 | // value passes the security checks, so clear binder calling id. |
Irina Dumitrescu | e369687 | 2019-01-09 16:07:59 +0000 | [diff] [blame] | 970 | if (changedRestrictions.contains(UserManager.DISALLOW_SHARE_LOCATION)) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 971 | final long identity = Binder.clearCallingIdentity(); |
| 972 | try { |
| 973 | synchronized (mLock) { |
| 974 | Setting setting = getSecureSetting( |
Soonil Nagarkar | 4ee3ac2 | 2019-02-08 19:19:24 -0800 | [diff] [blame] | 975 | Settings.Secure.LOCATION_MODE, userId); |
| 976 | updateSecureSetting(Settings.Secure.LOCATION_MODE, |
| 977 | setting != null ? setting.getValue() : null, null, |
| 978 | true, userId, true); |
| 979 | setting = getSecureSetting( |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 980 | Settings.Secure.LOCATION_PROVIDERS_ALLOWED, userId); |
| 981 | updateSecureSetting(Settings.Secure.LOCATION_PROVIDERS_ALLOWED, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 982 | setting != null ? setting.getValue() : null, null, |
| 983 | true, userId, true); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 984 | } |
| 985 | } finally { |
| 986 | Binder.restoreCallingIdentity(identity); |
| 987 | } |
| 988 | } |
Irina Dumitrescu | e369687 | 2019-01-09 16:07:59 +0000 | [diff] [blame] | 989 | if (changedRestrictions.contains(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES) |
| 990 | || changedRestrictions.contains( |
Irina Dumitrescu | 4638edd | 2018-09-05 14:08:33 +0100 | [diff] [blame] | 991 | UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY)) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 992 | final long identity = Binder.clearCallingIdentity(); |
| 993 | try { |
| 994 | synchronized (mLock) { |
| 995 | Setting setting = getGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 996 | String value = setting != null ? setting.getValue() : null; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 997 | updateGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 998 | value, null, true, userId, true); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 999 | } |
| 1000 | } finally { |
| 1001 | Binder.restoreCallingIdentity(identity); |
| 1002 | } |
| 1003 | } |
Irina Dumitrescu | e369687 | 2019-01-09 16:07:59 +0000 | [diff] [blame] | 1004 | if (changedRestrictions.contains(UserManager.DISALLOW_DEBUGGING_FEATURES)) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1005 | final long identity = Binder.clearCallingIdentity(); |
| 1006 | try { |
| 1007 | synchronized (mLock) { |
| 1008 | Setting setting = getGlobalSetting(Settings.Global.ADB_ENABLED); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1009 | String value = setting != null ? setting.getValue() : null; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1010 | updateGlobalSetting(Settings.Global.ADB_ENABLED, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1011 | value, null, true, userId, true); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1012 | } |
| 1013 | } finally { |
| 1014 | Binder.restoreCallingIdentity(identity); |
| 1015 | } |
| 1016 | } |
Irina Dumitrescu | e369687 | 2019-01-09 16:07:59 +0000 | [diff] [blame] | 1017 | if (changedRestrictions.contains(UserManager.ENSURE_VERIFY_APPS)) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1018 | final long identity = Binder.clearCallingIdentity(); |
| 1019 | try { |
| 1020 | synchronized (mLock) { |
| 1021 | Setting enable = getGlobalSetting( |
| 1022 | Settings.Global.PACKAGE_VERIFIER_ENABLE); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1023 | String enableValue = enable != null ? enable.getValue() : null; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1024 | updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_ENABLE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1025 | enableValue, null, true, userId, true); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1026 | Setting include = getGlobalSetting( |
| 1027 | Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1028 | String includeValue = include != null ? include.getValue() : null; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1029 | updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1030 | includeValue, null, true, userId, true); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1031 | } |
| 1032 | } finally { |
| 1033 | Binder.restoreCallingIdentity(identity); |
| 1034 | } |
| 1035 | } |
Irina Dumitrescu | e369687 | 2019-01-09 16:07:59 +0000 | [diff] [blame] | 1036 | if (changedRestrictions.contains(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1037 | final long identity = Binder.clearCallingIdentity(); |
| 1038 | try { |
| 1039 | synchronized (mLock) { |
| 1040 | Setting setting = getGlobalSetting( |
| 1041 | Settings.Global.PREFERRED_NETWORK_MODE); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1042 | String value = setting != null ? setting.getValue() : null; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1043 | updateGlobalSetting(Settings.Global.PREFERRED_NETWORK_MODE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1044 | value, null, true, userId, true); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1045 | } |
| 1046 | } finally { |
| 1047 | Binder.restoreCallingIdentity(identity); |
| 1048 | } |
| 1049 | } |
| 1050 | }); |
| 1051 | } |
| 1052 | |
Irina Dumitrescu | e369687 | 2019-01-09 16:07:59 +0000 | [diff] [blame] | 1053 | private static Set<String> getRestrictionDiff(Bundle prevRestrictions, Bundle newRestrictions) { |
| 1054 | Set<String> restrictionNames = Sets.newArraySet(); |
| 1055 | restrictionNames.addAll(prevRestrictions.keySet()); |
| 1056 | restrictionNames.addAll(newRestrictions.keySet()); |
| 1057 | Set<String> diff = Sets.newArraySet(); |
| 1058 | for (String restrictionName : restrictionNames) { |
| 1059 | if (prevRestrictions.getBoolean(restrictionName) != newRestrictions.getBoolean( |
| 1060 | restrictionName)) { |
| 1061 | diff.add(restrictionName); |
| 1062 | } |
| 1063 | } |
| 1064 | return diff; |
| 1065 | } |
| 1066 | |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1067 | private Setting getConfigSetting(String name) { |
| 1068 | if (DEBUG) { |
| 1069 | Slog.v(LOG_TAG, "getConfigSetting(" + name + ")"); |
| 1070 | } |
| 1071 | |
Stanislav Zholnin | 5579950 | 2019-03-08 14:54:55 +0000 | [diff] [blame] | 1072 | DeviceConfig.enforceReadPermission(getContext(), /*namespace=*/name.split("/")[0]); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1073 | |
| 1074 | // Get the value. |
| 1075 | synchronized (mLock) { |
| 1076 | return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_CONFIG, |
| 1077 | UserHandle.USER_SYSTEM, name); |
| 1078 | } |
| 1079 | } |
| 1080 | |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1081 | private boolean insertConfigSetting(String name, String value, boolean makeDefault) { |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1082 | if (DEBUG) { |
| 1083 | Slog.v(LOG_TAG, "insertConfigSetting(" + name + ", " + value + ", " |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1084 | + makeDefault + ")"); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1085 | } |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1086 | return mutateConfigSetting(name, value, null, makeDefault, |
| 1087 | MUTATION_OPERATION_INSERT, 0); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1090 | private boolean deleteConfigSetting(String name) { |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 1091 | if (DEBUG) { |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1092 | Slog.v(LOG_TAG, "deleteConfigSetting(" + name + ")"); |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 1093 | } |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1094 | return mutateConfigSetting(name, null, null, false, |
| 1095 | MUTATION_OPERATION_DELETE, 0); |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 1096 | } |
| 1097 | |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1098 | private void resetConfigSetting(int mode, String prefix) { |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1099 | if (DEBUG) { |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1100 | Slog.v(LOG_TAG, "resetConfigSetting(" + mode + ", " + prefix + ")"); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1101 | } |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1102 | mutateConfigSetting(null, null, prefix, false, |
| 1103 | MUTATION_OPERATION_RESET, mode); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 1106 | private boolean mutateConfigSetting(String name, String value, String prefix, |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1107 | boolean makeDefault, int operation, int mode) { |
Stanislav Zholnin | 5a25a84 | 2019-03-13 14:43:26 +0000 | [diff] [blame] | 1108 | enforceWritePermission(Manifest.permission.WRITE_DEVICE_CONFIG); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1109 | |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1110 | // Perform the mutation. |
| 1111 | synchronized (mLock) { |
| 1112 | switch (operation) { |
| 1113 | case MUTATION_OPERATION_INSERT: { |
| 1114 | return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_CONFIG, |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1115 | UserHandle.USER_SYSTEM, name, value, null, makeDefault, true, |
Matt Pape | 7b1c6cd | 2019-01-04 08:10:41 -0800 | [diff] [blame] | 1116 | resolveCallingPackage(), false, null); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 1119 | case MUTATION_OPERATION_DELETE: { |
| 1120 | return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_CONFIG, |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 1121 | UserHandle.USER_SYSTEM, name, false, null); |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1124 | case MUTATION_OPERATION_RESET: { |
| 1125 | mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_CONFIG, |
Matt Pape | 7b1c6cd | 2019-01-04 08:10:41 -0800 | [diff] [blame] | 1126 | UserHandle.USER_SYSTEM, resolveCallingPackage(), mode, null, prefix); |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 1127 | } return true; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | return false; |
| 1132 | } |
| 1133 | |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 1134 | private Map<String, String> getAllConfigFlags(@Nullable String prefix) { |
| 1135 | if (DEBUG) { |
| 1136 | Slog.v(LOG_TAG, "getAllConfigFlags() for " + prefix); |
| 1137 | } |
| 1138 | |
| 1139 | synchronized (mLock) { |
| 1140 | // Get the settings. |
| 1141 | SettingsState settingsState = mSettingsRegistry.getSettingsLocked( |
| 1142 | SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM); |
| 1143 | |
| 1144 | List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_CONFIG, |
| 1145 | UserHandle.USER_SYSTEM); |
| 1146 | |
| 1147 | final int nameCount = names.size(); |
| 1148 | Map<String, String> flagsToValues = new HashMap<>(names.size()); |
| 1149 | |
| 1150 | for (int i = 0; i < nameCount; i++) { |
| 1151 | String name = names.get(i); |
| 1152 | Setting setting = settingsState.getSettingLocked(name); |
| 1153 | if (prefix == null || setting.getName().startsWith(prefix)) { |
| 1154 | flagsToValues.put(setting.getName(), setting.getValue()); |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | return flagsToValues; |
| 1159 | } |
| 1160 | } |
| 1161 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1162 | private Cursor getAllGlobalSettings(String[] projection) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1163 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1164 | Slog.v(LOG_TAG, "getAllGlobalSettings()"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1167 | synchronized (mLock) { |
| 1168 | // Get the settings. |
| 1169 | SettingsState settingsState = mSettingsRegistry.getSettingsLocked( |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1170 | SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1171 | |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1172 | List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_GLOBAL, |
| 1173 | UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1174 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1175 | final int nameCount = names.size(); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1176 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1177 | String[] normalizedProjection = normalizeProjection(projection); |
| 1178 | MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1179 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1180 | // Anyone can get the global settings, so no security checks. |
| 1181 | for (int i = 0; i < nameCount; i++) { |
| 1182 | String name = names.get(i); |
| 1183 | Setting setting = settingsState.getSettingLocked(name); |
| 1184 | appendSettingToCursor(result, setting); |
| 1185 | } |
| 1186 | |
| 1187 | return result; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1188 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1189 | } |
| 1190 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1191 | private Setting getGlobalSetting(String name) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1192 | if (DEBUG) { |
| 1193 | Slog.v(LOG_TAG, "getGlobalSetting(" + name + ")"); |
| 1194 | } |
| 1195 | |
Chad Brubaker | a6830e7 | 2017-04-28 17:34:36 -0700 | [diff] [blame] | 1196 | // Ensure the caller can access the setting. |
| 1197 | enforceSettingReadable(name, SETTINGS_TYPE_GLOBAL, UserHandle.getCallingUserId()); |
| 1198 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1199 | // Get the value. |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1200 | synchronized (mLock) { |
Chad Brubaker | a6830e7 | 2017-04-28 17:34:36 -0700 | [diff] [blame] | 1201 | return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_GLOBAL, |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1202 | UserHandle.USER_SYSTEM, name); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1203 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1204 | } |
| 1205 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1206 | private boolean updateGlobalSetting(String name, String value, String tag, |
| 1207 | boolean makeDefault, int requestingUserId, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1208 | if (DEBUG) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1209 | Slog.v(LOG_TAG, "updateGlobalSetting(" + name + ", " + value + ", " |
| 1210 | + ", " + tag + ", " + makeDefault + ", " + requestingUserId |
| 1211 | + ", " + forceNotify + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1212 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1213 | return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId, |
| 1214 | MUTATION_OPERATION_UPDATE, forceNotify, 0); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1215 | } |
| 1216 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1217 | private boolean insertGlobalSetting(String name, String value, String tag, |
| 1218 | boolean makeDefault, int requestingUserId, boolean forceNotify) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1219 | if (DEBUG) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1220 | Slog.v(LOG_TAG, "insertGlobalSetting(" + name + ", " + value + ", " |
| 1221 | + ", " + tag + ", " + makeDefault + ", " + requestingUserId |
| 1222 | + ", " + forceNotify + ")"); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1223 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1224 | return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId, |
| 1225 | MUTATION_OPERATION_INSERT, forceNotify, 0); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1228 | private boolean deleteGlobalSetting(String name, int requestingUserId, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1229 | if (DEBUG) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1230 | Slog.v(LOG_TAG, "deleteGlobalSetting(" + name + ", " + requestingUserId |
| 1231 | + ", " + forceNotify + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1232 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1233 | return mutateGlobalSetting(name, null, null, false, requestingUserId, |
| 1234 | MUTATION_OPERATION_DELETE, forceNotify, 0); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1237 | private void resetGlobalSetting(int requestingUserId, int mode, String tag) { |
| 1238 | if (DEBUG) { |
| 1239 | Slog.v(LOG_TAG, "resetGlobalSetting(" + requestingUserId + ", " |
| 1240 | + mode + ", " + tag + ")"); |
| 1241 | } |
| 1242 | mutateGlobalSetting(null, null, tag, false, requestingUserId, |
| 1243 | MUTATION_OPERATION_RESET, false, mode); |
| 1244 | } |
| 1245 | |
| 1246 | private boolean mutateGlobalSetting(String name, String value, String tag, |
| 1247 | boolean makeDefault, int requestingUserId, int operation, boolean forceNotify, |
| 1248 | int mode) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1249 | // Make sure the caller can change the settings - treated as secure. |
| 1250 | enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS); |
| 1251 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1252 | // Resolve the userId on whose behalf the call is made. |
| 1253 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId); |
| 1254 | |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 1255 | // If this is a setting that is currently restricted for this user, do not allow |
| 1256 | // unrestricting changes. |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 1257 | if (name != null && mUserManagerInternal.isSettingRestrictedForUser( |
| 1258 | name, callingUserId, value, Binder.getCallingUid())) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1259 | return false; |
| 1260 | } |
| 1261 | |
| 1262 | // Perform the mutation. |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1263 | synchronized (mLock) { |
| 1264 | switch (operation) { |
| 1265 | case MUTATION_OPERATION_INSERT: { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1266 | return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_GLOBAL, |
| 1267 | UserHandle.USER_SYSTEM, name, value, tag, makeDefault, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1268 | getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1269 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1270 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1271 | case MUTATION_OPERATION_DELETE: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1272 | return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_GLOBAL, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1273 | UserHandle.USER_SYSTEM, name, forceNotify, CRITICAL_GLOBAL_SETTINGS); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1274 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1275 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1276 | case MUTATION_OPERATION_UPDATE: { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1277 | return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_GLOBAL, |
| 1278 | UserHandle.USER_SYSTEM, name, value, tag, makeDefault, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1279 | getCallingPackage(), forceNotify, CRITICAL_GLOBAL_SETTINGS); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1280 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1281 | |
| 1282 | case MUTATION_OPERATION_RESET: { |
| 1283 | mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_GLOBAL, |
| 1284 | UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag); |
| 1285 | } return true; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | return false; |
| 1290 | } |
| 1291 | |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 1292 | private PackageInfo getCallingPackageInfo(int userId) { |
| 1293 | try { |
| 1294 | return mPackageManager.getPackageInfo(getCallingPackage(), |
| 1295 | PackageManager.GET_SIGNATURES, userId); |
| 1296 | } catch (RemoteException e) { |
| 1297 | throw new IllegalStateException("Package " + getCallingPackage() + " doesn't exist"); |
| 1298 | } |
| 1299 | } |
| 1300 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1301 | private Cursor getAllSecureSettings(int userId, String[] projection) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1302 | if (DEBUG) { |
| 1303 | Slog.v(LOG_TAG, "getAllSecureSettings(" + userId + ")"); |
| 1304 | } |
| 1305 | |
| 1306 | // Resolve the userId on whose behalf the call is made. |
| 1307 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId); |
| 1308 | |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 1309 | // The relevant "calling package" userId will be the owning userId for some |
| 1310 | // profiles, and we can't do the lookup inside our [lock held] loop, so work out |
| 1311 | // up front who the effective "new SSAID" user ID for that settings name will be. |
| 1312 | final int ssaidUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, |
| 1313 | Settings.Secure.ANDROID_ID); |
| 1314 | final PackageInfo ssaidCallingPkg = getCallingPackageInfo(ssaidUserId); |
| 1315 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1316 | synchronized (mLock) { |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1317 | List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SECURE, callingUserId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1318 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1319 | final int nameCount = names.size(); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1320 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1321 | String[] normalizedProjection = normalizeProjection(projection); |
| 1322 | MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1323 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1324 | for (int i = 0; i < nameCount; i++) { |
| 1325 | String name = names.get(i); |
| 1326 | // Determine the owning user as some profile settings are cloned from the parent. |
| 1327 | final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, |
| 1328 | name); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1329 | |
Alex Klyubin | 1991f57 | 2017-03-03 14:08:36 -0800 | [diff] [blame] | 1330 | if (!isSecureSettingAccessible(name, callingUserId, owningUserId)) { |
| 1331 | // This caller is not permitted to access this setting. Pretend the setting |
| 1332 | // doesn't exist. |
Svetoslav Ganov | 83a1f7f | 2016-04-27 13:50:49 -0700 | [diff] [blame] | 1333 | continue; |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1334 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1335 | |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 1336 | // As of Android O, the SSAID is read from an app-specific entry in table |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1337 | // SETTINGS_FILE_SSAID, unless accessed by a system process. |
| 1338 | final Setting setting; |
| 1339 | if (isNewSsaidSetting(name)) { |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 1340 | setting = getSsaidSettingLocked(ssaidCallingPkg, owningUserId); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1341 | } else { |
| 1342 | setting = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE, owningUserId, |
| 1343 | name); |
| 1344 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1345 | appendSettingToCursor(result, setting); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1346 | } |
| 1347 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1348 | return result; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1349 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1350 | } |
| 1351 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1352 | private Setting getSecureSetting(String name, int requestingUserId) { |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 1353 | return getSecureSetting(name, requestingUserId, /*enableOverride=*/ false); |
| 1354 | } |
| 1355 | |
| 1356 | private Setting getSecureSetting(String name, int requestingUserId, boolean enableOverride) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1357 | if (DEBUG) { |
| 1358 | Slog.v(LOG_TAG, "getSecureSetting(" + name + ", " + requestingUserId + ")"); |
| 1359 | } |
| 1360 | |
| 1361 | // Resolve the userId on whose behalf the call is made. |
| 1362 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId); |
| 1363 | |
Chad Brubaker | a6830e7 | 2017-04-28 17:34:36 -0700 | [diff] [blame] | 1364 | // Ensure the caller can access the setting. |
| 1365 | enforceSettingReadable(name, SETTINGS_TYPE_SECURE, UserHandle.getCallingUserId()); |
| 1366 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1367 | // Determine the owning user as some profile settings are cloned from the parent. |
| 1368 | final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name); |
| 1369 | |
Alex Klyubin | 1991f57 | 2017-03-03 14:08:36 -0800 | [diff] [blame] | 1370 | if (!isSecureSettingAccessible(name, callingUserId, owningUserId)) { |
| 1371 | // This caller is not permitted to access this setting. Pretend the setting doesn't |
| 1372 | // exist. |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 1373 | SettingsState settings = mSettingsRegistry.getSettingsLocked(SETTINGS_TYPE_SECURE, |
| 1374 | owningUserId); |
| 1375 | return settings != null ? settings.getNullSetting() : null; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1376 | } |
| 1377 | |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 1378 | // As of Android O, the SSAID is read from an app-specific entry in table |
| 1379 | // SETTINGS_FILE_SSAID, unless accessed by a system process. |
| 1380 | if (isNewSsaidSetting(name)) { |
| 1381 | PackageInfo callingPkg = getCallingPackageInfo(owningUserId); |
| 1382 | synchronized (mLock) { |
| 1383 | return getSsaidSettingLocked(callingPkg, owningUserId); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1384 | } |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 1385 | } |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 1386 | if (enableOverride) { |
Kweku Adams | 5e0052b | 2019-02-22 15:17:52 -0800 | [diff] [blame] | 1387 | if (Secure.LOCATION_MODE.equals(name)) { |
| 1388 | final Setting overridden = getLocationModeSetting(owningUserId); |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 1389 | if (overridden != null) { |
| 1390 | return overridden; |
| 1391 | } |
| 1392 | } |
| 1393 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1394 | |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 1395 | // Not the SSAID; do a straight lookup |
| 1396 | synchronized (mLock) { |
Chad Brubaker | a6830e7 | 2017-04-28 17:34:36 -0700 | [diff] [blame] | 1397 | return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE, |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1398 | owningUserId, name); |
| 1399 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1400 | } |
| 1401 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1402 | private boolean isNewSsaidSetting(String name) { |
| 1403 | return Settings.Secure.ANDROID_ID.equals(name) |
| 1404 | && UserHandle.getAppId(Binder.getCallingUid()) >= Process.FIRST_APPLICATION_UID; |
| 1405 | } |
| 1406 | |
Andreas Gampe | b5889307 | 2018-09-05 16:52:31 -0700 | [diff] [blame] | 1407 | @GuardedBy("mLock") |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 1408 | private Setting getSsaidSettingLocked(PackageInfo callingPkg, int owningUserId) { |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1409 | // Get uid of caller (key) used to store ssaid value |
| 1410 | String name = Integer.toString( |
| 1411 | UserHandle.getUid(owningUserId, UserHandle.getAppId(Binder.getCallingUid()))); |
| 1412 | |
| 1413 | if (DEBUG) { |
| 1414 | Slog.v(LOG_TAG, "getSsaidSettingLocked(" + name + "," + owningUserId + ")"); |
| 1415 | } |
| 1416 | |
| 1417 | // Retrieve the ssaid from the table if present. |
| 1418 | final Setting ssaid = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SSAID, owningUserId, |
| 1419 | name); |
Chad Brubaker | 0d277a7 | 2017-04-12 16:56:53 -0700 | [diff] [blame] | 1420 | // If the app is an Instant App use its stored SSAID instead of our own. |
| 1421 | final String instantSsaid; |
| 1422 | final long token = Binder.clearCallingIdentity(); |
| 1423 | try { |
| 1424 | instantSsaid = mPackageManager.getInstantAppAndroidId(callingPkg.packageName, |
| 1425 | owningUserId); |
| 1426 | } catch (RemoteException e) { |
| 1427 | Slog.e(LOG_TAG, "Failed to get Instant App Android ID", e); |
| 1428 | return null; |
| 1429 | } finally { |
| 1430 | Binder.restoreCallingIdentity(token); |
| 1431 | } |
Svet Ganov | 96c9946 | 2017-05-05 14:27:13 -0700 | [diff] [blame] | 1432 | |
| 1433 | final SettingsState ssaidSettings = mSettingsRegistry.getSettingsLocked( |
| 1434 | SETTINGS_TYPE_SSAID, owningUserId); |
| 1435 | |
Chad Brubaker | 0d277a7 | 2017-04-12 16:56:53 -0700 | [diff] [blame] | 1436 | if (instantSsaid != null) { |
| 1437 | // Use the stored value if it is still valid. |
| 1438 | if (ssaid != null && instantSsaid.equals(ssaid.getValue())) { |
Svet Ganov | 96c9946 | 2017-05-05 14:27:13 -0700 | [diff] [blame] | 1439 | return mascaradeSsaidSetting(ssaidSettings, ssaid); |
Chad Brubaker | 0d277a7 | 2017-04-12 16:56:53 -0700 | [diff] [blame] | 1440 | } |
| 1441 | // The value has changed, update the stored value. |
Chad Brubaker | 0d277a7 | 2017-04-12 16:56:53 -0700 | [diff] [blame] | 1442 | final boolean success = ssaidSettings.insertSettingLocked(name, instantSsaid, null, |
| 1443 | true, callingPkg.packageName); |
| 1444 | if (!success) { |
| 1445 | throw new IllegalStateException("Failed to update instant app android id"); |
| 1446 | } |
Svet Ganov | 96c9946 | 2017-05-05 14:27:13 -0700 | [diff] [blame] | 1447 | Setting setting = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SSAID, |
| 1448 | owningUserId, name); |
| 1449 | return mascaradeSsaidSetting(ssaidSettings, setting); |
Chad Brubaker | 0d277a7 | 2017-04-12 16:56:53 -0700 | [diff] [blame] | 1450 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1451 | |
| 1452 | // Lazy initialize ssaid if not yet present in ssaid table. |
Mark Rathjen | ea61759 | 2017-01-18 23:03:41 -0800 | [diff] [blame] | 1453 | if (ssaid == null || ssaid.isNull() || ssaid.getValue() == null) { |
Svet Ganov | 96c9946 | 2017-05-05 14:27:13 -0700 | [diff] [blame] | 1454 | Setting setting = mSettingsRegistry.generateSsaidLocked(callingPkg, owningUserId); |
| 1455 | return mascaradeSsaidSetting(ssaidSettings, setting); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Svet Ganov | 96c9946 | 2017-05-05 14:27:13 -0700 | [diff] [blame] | 1458 | return mascaradeSsaidSetting(ssaidSettings, ssaid); |
| 1459 | } |
| 1460 | |
| 1461 | private Setting mascaradeSsaidSetting(SettingsState settingsState, Setting ssaidSetting) { |
| 1462 | // SSAID settings are located in a dedicated table for internal bookkeeping |
| 1463 | // but for the world they reside in the secure table, so adjust the key here. |
| 1464 | // We have a special name when looking it up but want the world to see it as |
| 1465 | // "android_id". |
| 1466 | if (ssaidSetting != null) { |
| 1467 | return settingsState.new Setting(ssaidSetting) { |
| 1468 | @Override |
| 1469 | public int getKey() { |
| 1470 | final int userId = getUserIdFromKey(super.getKey()); |
| 1471 | return makeKey(SETTINGS_TYPE_SECURE, userId); |
| 1472 | } |
| 1473 | |
| 1474 | @Override |
| 1475 | public String getName() { |
| 1476 | return Settings.Secure.ANDROID_ID; |
| 1477 | } |
| 1478 | }; |
| 1479 | } |
| 1480 | return null; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Kweku Adams | 5e0052b | 2019-02-22 15:17:52 -0800 | [diff] [blame] | 1483 | private Setting getLocationModeSetting(int owningUserId) { |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 1484 | synchronized (mLock) { |
| 1485 | final Setting setting = getGlobalSetting( |
| 1486 | Global.LOCATION_GLOBAL_KILL_SWITCH); |
| 1487 | if (!"1".equals(setting.getValue())) { |
| 1488 | return null; |
| 1489 | } |
| 1490 | // Global kill-switch is enabled. Return an empty value. |
| 1491 | final SettingsState settingsState = mSettingsRegistry.getSettingsLocked( |
| 1492 | SETTINGS_TYPE_SECURE, owningUserId); |
| 1493 | return settingsState.new Setting( |
Kweku Adams | 5e0052b | 2019-02-22 15:17:52 -0800 | [diff] [blame] | 1494 | Secure.LOCATION_MODE, |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 1495 | "", // value |
| 1496 | "", // tag |
| 1497 | "", // default value |
| 1498 | "", // package name |
| 1499 | false, // from system |
| 1500 | "0" // id |
| 1501 | ) { |
| 1502 | @Override |
| 1503 | public boolean update(String value, boolean setDefault, String packageName, |
| 1504 | String tag, boolean forceNonSystemPackage) { |
Kweku Adams | 5e0052b | 2019-02-22 15:17:52 -0800 | [diff] [blame] | 1505 | Slog.wtf(LOG_TAG, "update shouldn't be called on this instance."); |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 1506 | return false; |
| 1507 | } |
| 1508 | }; |
| 1509 | } |
| 1510 | } |
| 1511 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1512 | private boolean insertSecureSetting(String name, String value, String tag, |
| 1513 | boolean makeDefault, int requestingUserId, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1514 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1515 | Slog.v(LOG_TAG, "insertSecureSetting(" + name + ", " + value + ", " |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1516 | + ", " + tag + ", " + makeDefault + ", " + requestingUserId |
| 1517 | + ", " + forceNotify + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1518 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1519 | return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId, |
| 1520 | MUTATION_OPERATION_INSERT, forceNotify, 0); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1521 | } |
| 1522 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1523 | private boolean deleteSecureSetting(String name, int requestingUserId, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1524 | if (DEBUG) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1525 | Slog.v(LOG_TAG, "deleteSecureSetting(" + name + ", " + requestingUserId |
| 1526 | + ", " + forceNotify + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1527 | } |
| 1528 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1529 | return mutateSecureSetting(name, null, null, false, requestingUserId, |
| 1530 | MUTATION_OPERATION_DELETE, forceNotify, 0); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1531 | } |
| 1532 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1533 | private boolean updateSecureSetting(String name, String value, String tag, |
| 1534 | boolean makeDefault, int requestingUserId, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1535 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1536 | Slog.v(LOG_TAG, "updateSecureSetting(" + name + ", " + value + ", " |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1537 | + ", " + tag + ", " + makeDefault + ", " + requestingUserId |
| 1538 | + ", " + forceNotify +")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1539 | } |
| 1540 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1541 | return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId, |
| 1542 | MUTATION_OPERATION_UPDATE, forceNotify, 0); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1543 | } |
| 1544 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1545 | private void resetSecureSetting(int requestingUserId, int mode, String tag) { |
| 1546 | if (DEBUG) { |
| 1547 | Slog.v(LOG_TAG, "resetSecureSetting(" + requestingUserId + ", " |
| 1548 | + mode + ", " + tag + ")"); |
| 1549 | } |
| 1550 | |
| 1551 | mutateSecureSetting(null, null, tag, false, requestingUserId, |
| 1552 | MUTATION_OPERATION_RESET, false, mode); |
| 1553 | } |
| 1554 | |
| 1555 | private boolean mutateSecureSetting(String name, String value, String tag, |
| 1556 | boolean makeDefault, int requestingUserId, int operation, boolean forceNotify, |
| 1557 | int mode) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1558 | // Make sure the caller can change the settings. |
| 1559 | enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS); |
| 1560 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1561 | // Resolve the userId on whose behalf the call is made. |
| 1562 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId); |
| 1563 | |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 1564 | // If this is a setting that is currently restricted for this user, do not allow |
| 1565 | // unrestricting changes. |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 1566 | if (name != null && mUserManagerInternal.isSettingRestrictedForUser( |
| 1567 | name, callingUserId, value, Binder.getCallingUid())) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1568 | return false; |
| 1569 | } |
| 1570 | |
| 1571 | // Determine the owning user as some profile settings are cloned from the parent. |
| 1572 | final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name); |
| 1573 | |
| 1574 | // Only the owning user can change the setting. |
| 1575 | if (owningUserId != callingUserId) { |
| 1576 | return false; |
| 1577 | } |
| 1578 | |
| 1579 | // Special cases for location providers (sigh). |
| 1580 | if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1581 | return updateLocationProvidersAllowedLocked(value, tag, owningUserId, makeDefault, |
| 1582 | forceNotify); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | // Mutate the value. |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1586 | synchronized (mLock) { |
| 1587 | switch (operation) { |
| 1588 | case MUTATION_OPERATION_INSERT: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1589 | return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1590 | owningUserId, name, value, tag, makeDefault, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1591 | getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1592 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1593 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1594 | case MUTATION_OPERATION_DELETE: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1595 | return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SECURE, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1596 | owningUserId, name, forceNotify, CRITICAL_SECURE_SETTINGS); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1597 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1598 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1599 | case MUTATION_OPERATION_UPDATE: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1600 | return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SECURE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1601 | owningUserId, name, value, tag, makeDefault, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1602 | getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1603 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1604 | |
| 1605 | case MUTATION_OPERATION_RESET: { |
| 1606 | mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_SECURE, |
| 1607 | UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag); |
| 1608 | } return true; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | return false; |
| 1613 | } |
| 1614 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1615 | private Cursor getAllSystemSettings(int userId, String[] projection) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1616 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1617 | Slog.v(LOG_TAG, "getAllSecureSystem(" + userId + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | // Resolve the userId on whose behalf the call is made. |
| 1621 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId); |
| 1622 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1623 | synchronized (mLock) { |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1624 | List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SYSTEM, callingUserId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1625 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1626 | final int nameCount = names.size(); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1627 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1628 | String[] normalizedProjection = normalizeProjection(projection); |
| 1629 | MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1630 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1631 | for (int i = 0; i < nameCount; i++) { |
| 1632 | String name = names.get(i); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1633 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1634 | // Determine the owning user as some profile settings are cloned from the parent. |
| 1635 | final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, |
| 1636 | name); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1637 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1638 | Setting setting = mSettingsRegistry.getSettingLocked( |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1639 | SETTINGS_TYPE_SYSTEM, owningUserId, name); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1640 | appendSettingToCursor(result, setting); |
| 1641 | } |
| 1642 | |
| 1643 | return result; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1644 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1645 | } |
| 1646 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1647 | private Setting getSystemSetting(String name, int requestingUserId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1648 | if (DEBUG) { |
| 1649 | Slog.v(LOG_TAG, "getSystemSetting(" + name + ", " + requestingUserId + ")"); |
| 1650 | } |
| 1651 | |
| 1652 | // Resolve the userId on whose behalf the call is made. |
| 1653 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId); |
| 1654 | |
Chad Brubaker | a6830e7 | 2017-04-28 17:34:36 -0700 | [diff] [blame] | 1655 | // Ensure the caller can access the setting. |
| 1656 | enforceSettingReadable(name, SETTINGS_TYPE_SYSTEM, UserHandle.getCallingUserId()); |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1657 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1658 | // Determine the owning user as some profile settings are cloned from the parent. |
| 1659 | final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name); |
| 1660 | |
| 1661 | // Get the value. |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1662 | synchronized (mLock) { |
Chad Brubaker | a6830e7 | 2017-04-28 17:34:36 -0700 | [diff] [blame] | 1663 | return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SYSTEM, owningUserId, name); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1664 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1665 | } |
| 1666 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1667 | private boolean insertSystemSetting(String name, String value, int requestingUserId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1668 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1669 | Slog.v(LOG_TAG, "insertSystemSetting(" + name + ", " + value + ", " |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1670 | + requestingUserId + ")"); |
| 1671 | } |
| 1672 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1673 | return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1674 | } |
| 1675 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1676 | private boolean deleteSystemSetting(String name, int requestingUserId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1677 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1678 | Slog.v(LOG_TAG, "deleteSystemSetting(" + name + ", " + requestingUserId + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1679 | } |
| 1680 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1681 | return mutateSystemSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1682 | } |
| 1683 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1684 | private boolean updateSystemSetting(String name, String value, int requestingUserId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1685 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1686 | Slog.v(LOG_TAG, "updateSystemSetting(" + name + ", " + value + ", " |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1687 | + requestingUserId + ")"); |
| 1688 | } |
| 1689 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1690 | return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1691 | } |
| 1692 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1693 | private boolean mutateSystemSetting(String name, String value, int runAsUserId, |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1694 | int operation) { |
Billy Lau | 6ad2d66 | 2015-07-18 00:26:58 +0100 | [diff] [blame] | 1695 | if (!hasWriteSecureSettingsPermission()) { |
| 1696 | // If the caller doesn't hold WRITE_SECURE_SETTINGS, we verify whether this |
| 1697 | // operation is allowed for the calling package through appops. |
| 1698 | if (!Settings.checkAndNoteWriteSettingsOperation(getContext(), |
| 1699 | Binder.getCallingUid(), getCallingPackage(), true)) { |
| 1700 | return false; |
| 1701 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1702 | } |
| 1703 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1704 | // Resolve the userId on whose behalf the call is made. |
| 1705 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId); |
| 1706 | |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 1707 | if (name != null && mUserManagerInternal.isSettingRestrictedForUser( |
| 1708 | name, callingUserId, value, Binder.getCallingUid())) { |
| 1709 | return false; |
| 1710 | } |
| 1711 | |
Svetoslav | d8d25e0 | 2015-11-20 13:09:26 -0800 | [diff] [blame] | 1712 | // Enforce what the calling package can mutate the system settings. |
| 1713 | enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name, callingUserId); |
| 1714 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1715 | // Determine the owning user as some profile settings are cloned from the parent. |
| 1716 | final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name); |
| 1717 | |
| 1718 | // Only the owning user id can change the setting. |
| 1719 | if (owningUserId != callingUserId) { |
| 1720 | return false; |
| 1721 | } |
| 1722 | |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 1723 | // Invalidate any relevant cache files |
| 1724 | String cacheName = null; |
| 1725 | if (Settings.System.RINGTONE.equals(name)) { |
| 1726 | cacheName = Settings.System.RINGTONE_CACHE; |
| 1727 | } else if (Settings.System.NOTIFICATION_SOUND.equals(name)) { |
| 1728 | cacheName = Settings.System.NOTIFICATION_SOUND_CACHE; |
| 1729 | } else if (Settings.System.ALARM_ALERT.equals(name)) { |
| 1730 | cacheName = Settings.System.ALARM_ALERT_CACHE; |
| 1731 | } |
| 1732 | if (cacheName != null) { |
| 1733 | final File cacheFile = new File( |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 1734 | getRingtoneCacheDir(owningUserId), cacheName); |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 1735 | cacheFile.delete(); |
| 1736 | } |
| 1737 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1738 | // Mutate the value. |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1739 | synchronized (mLock) { |
| 1740 | switch (operation) { |
| 1741 | case MUTATION_OPERATION_INSERT: { |
| 1742 | validateSystemSettingValue(name, value); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1743 | return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SYSTEM, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1744 | owningUserId, name, value, null, false, getCallingPackage(), |
| 1745 | false, null); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | case MUTATION_OPERATION_DELETE: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1749 | return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SYSTEM, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1750 | owningUserId, name, false, null); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | case MUTATION_OPERATION_UPDATE: { |
| 1754 | validateSystemSettingValue(name, value); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1755 | return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SYSTEM, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 1756 | owningUserId, name, value, null, false, getCallingPackage(), |
| 1757 | false, null); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1758 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1759 | } |
| 1760 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1761 | return false; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1762 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1763 | } |
| 1764 | |
Billy Lau | 6ad2d66 | 2015-07-18 00:26:58 +0100 | [diff] [blame] | 1765 | private boolean hasWriteSecureSettingsPermission() { |
Svetoslav | f41334b | 2015-06-23 12:06:03 -0700 | [diff] [blame] | 1766 | // Write secure settings is a more protected permission. If caller has it we are good. |
| 1767 | if (getContext().checkCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1768 | == PackageManager.PERMISSION_GRANTED) { |
| 1769 | return true; |
| 1770 | } |
| 1771 | |
Svetoslav | f41334b | 2015-06-23 12:06:03 -0700 | [diff] [blame] | 1772 | return false; |
| 1773 | } |
| 1774 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1775 | private void validateSystemSettingValue(String name, String value) { |
Michal Karpinski | 2c37b08 | 2018-01-18 16:14:27 +0000 | [diff] [blame] | 1776 | SettingsValidators.Validator validator = Settings.System.VALIDATORS.get(name); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1777 | if (validator != null && !validator.validate(value)) { |
| 1778 | throw new IllegalArgumentException("Invalid value: " + value |
| 1779 | + " for setting: " + name); |
| 1780 | } |
| 1781 | } |
| 1782 | |
Alex Klyubin | 1991f57 | 2017-03-03 14:08:36 -0800 | [diff] [blame] | 1783 | /** |
| 1784 | * Returns {@code true} if the specified secure setting should be accessible to the caller. |
| 1785 | */ |
| 1786 | private boolean isSecureSettingAccessible(String name, int callingUserId, |
| 1787 | int owningUserId) { |
| 1788 | // Special case for location (sigh). |
| 1789 | // This check is not inside the name-based checks below because this method performs checks |
| 1790 | // only if the calling user ID is not the same as the owning user ID. |
| 1791 | if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) { |
| 1792 | return false; |
| 1793 | } |
| 1794 | |
| 1795 | switch (name) { |
| 1796 | case "bluetooth_address": |
| 1797 | // BluetoothManagerService for some reason stores the Android's Bluetooth MAC |
| 1798 | // address in this secure setting. Secure settings can normally be read by any app, |
| 1799 | // which thus enables them to bypass the recently introduced restrictions on access |
| 1800 | // to device identifiers. |
| 1801 | // To mitigate this we make this setting available only to callers privileged to see |
| 1802 | // this device's MAC addresses, same as through public API |
| 1803 | // BluetoothAdapter.getAddress() (see BluetoothManagerService for details). |
| 1804 | return getContext().checkCallingOrSelfPermission( |
| 1805 | Manifest.permission.LOCAL_MAC_ADDRESS) == PackageManager.PERMISSION_GRANTED; |
| 1806 | default: |
| 1807 | return true; |
| 1808 | } |
| 1809 | } |
| 1810 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1811 | private boolean isLocationProvidersAllowedRestricted(String name, int callingUserId, |
| 1812 | int owningUserId) { |
| 1813 | // Optimization - location providers are restricted only for managed profiles. |
| 1814 | if (callingUserId == owningUserId) { |
| 1815 | return false; |
| 1816 | } |
| 1817 | if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name) |
| 1818 | && mUserManager.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, |
| 1819 | new UserHandle(callingUserId))) { |
| 1820 | return true; |
| 1821 | } |
| 1822 | return false; |
| 1823 | } |
| 1824 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1825 | private int resolveOwningUserIdForSecureSettingLocked(int userId, String setting) { |
| 1826 | return resolveOwningUserIdLocked(userId, sSecureCloneToManagedSettings, setting); |
| 1827 | } |
| 1828 | |
| 1829 | private int resolveOwningUserIdForSystemSettingLocked(int userId, String setting) { |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 1830 | final int parentId; |
| 1831 | // Resolves dependency if setting has a dependency and the calling user has a parent |
| 1832 | if (sSystemCloneFromParentOnDependency.containsKey(setting) |
| 1833 | && (parentId = getGroupParentLocked(userId)) != userId) { |
| 1834 | // The setting has a dependency and the profile has a parent |
| 1835 | String dependency = sSystemCloneFromParentOnDependency.get(setting); |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1836 | // Lookup the dependency setting as ourselves, some callers may not have access to it. |
| 1837 | final long token = Binder.clearCallingIdentity(); |
| 1838 | try { |
| 1839 | Setting settingObj = getSecureSetting(dependency, userId); |
| 1840 | if (settingObj != null && settingObj.getValue().equals("1")) { |
| 1841 | return parentId; |
| 1842 | } |
| 1843 | } finally { |
| 1844 | Binder.restoreCallingIdentity(token); |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 1845 | } |
| 1846 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1847 | return resolveOwningUserIdLocked(userId, sSystemCloneToManagedSettings, setting); |
| 1848 | } |
| 1849 | |
| 1850 | private int resolveOwningUserIdLocked(int userId, Set<String> keys, String name) { |
| 1851 | final int parentId = getGroupParentLocked(userId); |
| 1852 | if (parentId != userId && keys.contains(name)) { |
| 1853 | return parentId; |
| 1854 | } |
| 1855 | return userId; |
| 1856 | } |
| 1857 | |
Svetoslav | f41334b | 2015-06-23 12:06:03 -0700 | [diff] [blame] | 1858 | private void enforceRestrictedSystemSettingsMutationForCallingPackage(int operation, |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1859 | String name, int userId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1860 | // System/root/shell can mutate whatever secure settings they want. |
| 1861 | final int callingUid = Binder.getCallingUid(); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1862 | final int appId = UserHandle.getAppId(callingUid); |
| 1863 | if (appId == android.os.Process.SYSTEM_UID |
| 1864 | || appId == Process.SHELL_UID |
| 1865 | || appId == Process.ROOT_UID) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1866 | return; |
| 1867 | } |
| 1868 | |
| 1869 | switch (operation) { |
| 1870 | case MUTATION_OPERATION_INSERT: |
| 1871 | // Insert updates. |
| 1872 | case MUTATION_OPERATION_UPDATE: { |
| 1873 | if (Settings.System.PUBLIC_SETTINGS.contains(name)) { |
| 1874 | return; |
| 1875 | } |
| 1876 | |
| 1877 | // The calling package is already verified. |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1878 | PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1879 | |
| 1880 | // Privileged apps can do whatever they want. |
| 1881 | if ((packageInfo.applicationInfo.privateFlags |
| 1882 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
| 1883 | return; |
| 1884 | } |
| 1885 | |
| 1886 | warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk( |
| 1887 | packageInfo.applicationInfo.targetSdkVersion, name); |
| 1888 | } break; |
| 1889 | |
| 1890 | case MUTATION_OPERATION_DELETE: { |
| 1891 | if (Settings.System.PUBLIC_SETTINGS.contains(name) |
| 1892 | || Settings.System.PRIVATE_SETTINGS.contains(name)) { |
| 1893 | throw new IllegalArgumentException("You cannot delete system defined" |
| 1894 | + " secure settings."); |
| 1895 | } |
| 1896 | |
| 1897 | // The calling package is already verified. |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1898 | PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1899 | |
| 1900 | // Privileged apps can do whatever they want. |
| 1901 | if ((packageInfo.applicationInfo.privateFlags & |
| 1902 | ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
| 1903 | return; |
| 1904 | } |
| 1905 | |
| 1906 | warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk( |
| 1907 | packageInfo.applicationInfo.targetSdkVersion, name); |
| 1908 | } break; |
| 1909 | } |
| 1910 | } |
| 1911 | |
Todd Kennedy | be0b889 | 2017-02-15 14:13:52 -0800 | [diff] [blame] | 1912 | private Set<String> getInstantAppAccessibleSettings(int settingsType) { |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1913 | switch (settingsType) { |
| 1914 | case SETTINGS_TYPE_GLOBAL: |
Todd Kennedy | be0b889 | 2017-02-15 14:13:52 -0800 | [diff] [blame] | 1915 | return Settings.Global.INSTANT_APP_SETTINGS; |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1916 | case SETTINGS_TYPE_SECURE: |
Todd Kennedy | be0b889 | 2017-02-15 14:13:52 -0800 | [diff] [blame] | 1917 | return Settings.Secure.INSTANT_APP_SETTINGS; |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1918 | case SETTINGS_TYPE_SYSTEM: |
Todd Kennedy | be0b889 | 2017-02-15 14:13:52 -0800 | [diff] [blame] | 1919 | return Settings.System.INSTANT_APP_SETTINGS; |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1920 | default: |
| 1921 | throw new IllegalArgumentException("Invalid settings type: " + settingsType); |
| 1922 | } |
| 1923 | } |
| 1924 | |
Chad Brubaker | 20e0dc3 | 2017-04-28 18:24:55 -0700 | [diff] [blame] | 1925 | private Set<String> getOverlayInstantAppAccessibleSettings(int settingsType) { |
| 1926 | switch (settingsType) { |
| 1927 | case SETTINGS_TYPE_GLOBAL: |
| 1928 | return OVERLAY_ALLOWED_GLOBAL_INSTANT_APP_SETTINGS; |
| 1929 | case SETTINGS_TYPE_SYSTEM: |
| 1930 | return OVERLAY_ALLOWED_SYSTEM_INSTANT_APP_SETTINGS; |
| 1931 | case SETTINGS_TYPE_SECURE: |
| 1932 | return OVERLAY_ALLOWED_SECURE_INSTANT_APP_SETTINGS; |
| 1933 | default: |
| 1934 | throw new IllegalArgumentException("Invalid settings type: " + settingsType); |
| 1935 | } |
| 1936 | } |
| 1937 | |
Andreas Gampe | b5889307 | 2018-09-05 16:52:31 -0700 | [diff] [blame] | 1938 | @GuardedBy("mLock") |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1939 | private List<String> getSettingsNamesLocked(int settingsType, int userId) { |
Chad Brubaker | b6108d6 | 2017-12-23 20:06:44 -0800 | [diff] [blame] | 1940 | // Don't enforce the instant app whitelist for now -- its too prone to unintended breakage |
| 1941 | // in the current form. |
| 1942 | return mSettingsRegistry.getSettingsNamesLocked(settingsType, userId); |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1943 | } |
| 1944 | |
Chad Brubaker | a6830e7 | 2017-04-28 17:34:36 -0700 | [diff] [blame] | 1945 | private void enforceSettingReadable(String settingName, int settingsType, int userId) { |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1946 | if (UserHandle.getAppId(Binder.getCallingUid()) < Process.FIRST_APPLICATION_UID) { |
| 1947 | return; |
| 1948 | } |
Chad Brubaker | f0fa853 | 2017-02-23 10:45:20 -0800 | [diff] [blame] | 1949 | ApplicationInfo ai = getCallingApplicationInfoOrThrow(); |
Svetoslav Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 1950 | if (!ai.isInstantApp()) { |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1951 | return; |
| 1952 | } |
Chad Brubaker | 20e0dc3 | 2017-04-28 18:24:55 -0700 | [diff] [blame] | 1953 | if (!getInstantAppAccessibleSettings(settingsType).contains(settingName) |
| 1954 | && !getOverlayInstantAppAccessibleSettings(settingsType).contains(settingName)) { |
Chad Brubaker | b6108d6 | 2017-12-23 20:06:44 -0800 | [diff] [blame] | 1955 | // Don't enforce the instant app whitelist for now -- its too prone to unintended |
| 1956 | // breakage in the current form. |
| 1957 | Slog.w(LOG_TAG, "Instant App " + ai.packageName |
| 1958 | + " trying to access unexposed setting, this will be an error in the future."); |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1959 | } |
| 1960 | } |
| 1961 | |
Chad Brubaker | f0fa853 | 2017-02-23 10:45:20 -0800 | [diff] [blame] | 1962 | private ApplicationInfo getCallingApplicationInfoOrThrow() { |
| 1963 | // We always use the callingUid for this lookup. This means that if hypothetically an |
| 1964 | // app was installed in user A with cross user and in user B as an Instant App |
| 1965 | // the app in A would be able to see all the settings in user B. However since cross |
| 1966 | // user is a system permission and the app must be uninstalled in B and then installed as |
| 1967 | // an Instant App that situation is not realistic or supported. |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1968 | ApplicationInfo ai = null; |
| 1969 | try { |
Chad Brubaker | f0fa853 | 2017-02-23 10:45:20 -0800 | [diff] [blame] | 1970 | ai = mPackageManager.getApplicationInfo(getCallingPackage(), 0 |
| 1971 | , UserHandle.getCallingUserId()); |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1972 | } catch (RemoteException ignored) { |
| 1973 | } |
| 1974 | if (ai == null) { |
| 1975 | throw new IllegalStateException("Failed to lookup info for package " |
| 1976 | + getCallingPackage()); |
| 1977 | } |
| 1978 | return ai; |
| 1979 | } |
| 1980 | |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1981 | private PackageInfo getCallingPackageInfoOrThrow(int userId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1982 | try { |
Svetoslav Ganov | 67a8d35 | 2016-03-02 13:26:40 -0800 | [diff] [blame] | 1983 | PackageInfo packageInfo = mPackageManager.getPackageInfo( |
| 1984 | getCallingPackage(), 0, userId); |
| 1985 | if (packageInfo != null) { |
| 1986 | return packageInfo; |
| 1987 | } |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1988 | } catch (RemoteException e) { |
Svetoslav Ganov | 67a8d35 | 2016-03-02 13:26:40 -0800 | [diff] [blame] | 1989 | /* ignore */ |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1990 | } |
Svetoslav Ganov | 67a8d35 | 2016-03-02 13:26:40 -0800 | [diff] [blame] | 1991 | throw new IllegalStateException("Calling package doesn't exist"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | private int getGroupParentLocked(int userId) { |
| 1995 | // Most frequent use case. |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1996 | if (userId == UserHandle.USER_SYSTEM) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1997 | return userId; |
| 1998 | } |
| 1999 | // We are in the same process with the user manager and the returned |
| 2000 | // user info is a cached instance, so just look up instead of cache. |
| 2001 | final long identity = Binder.clearCallingIdentity(); |
| 2002 | try { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 2003 | // Just a lookup and not reentrant, so holding a lock is fine. |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2004 | UserInfo userInfo = mUserManager.getProfileParent(userId); |
| 2005 | return (userInfo != null) ? userInfo.id : userId; |
| 2006 | } finally { |
| 2007 | Binder.restoreCallingIdentity(identity); |
| 2008 | } |
| 2009 | } |
| 2010 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2011 | private void enforceWritePermission(String permission) { |
| 2012 | if (getContext().checkCallingOrSelfPermission(permission) |
| 2013 | != PackageManager.PERMISSION_GRANTED) { |
| 2014 | throw new SecurityException("Permission denial: writing to settings requires:" |
| 2015 | + permission); |
| 2016 | } |
| 2017 | } |
| 2018 | |
| 2019 | /* |
| 2020 | * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED. |
| 2021 | * This setting contains a list of the currently enabled location providers. |
| 2022 | * But helper functions in android.providers.Settings can enable or disable |
| 2023 | * a single provider by using a "+" or "-" prefix before the provider name. |
| 2024 | * |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 2025 | * <p>See also {@link com.android.server.pm.UserRestrictionsUtils#isSettingRestrictedForUser()}. |
| 2026 | * If DISALLOW_SHARE_LOCATION is set, the said method will only allow values with |
| 2027 | * the "-" prefix. |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 2028 | * |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2029 | * @returns whether the enabled location providers changed. |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2030 | */ |
Andreas Gampe | b5889307 | 2018-09-05 16:52:31 -0700 | [diff] [blame] | 2031 | @GuardedBy("mLock") |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2032 | private boolean updateLocationProvidersAllowedLocked(String value, String tag, |
| 2033 | int owningUserId, boolean makeDefault, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2034 | if (TextUtils.isEmpty(value)) { |
| 2035 | return false; |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 2036 | } |
Maggie | 83e03f5 | 2018-03-16 12:22:20 -0700 | [diff] [blame] | 2037 | Setting oldSetting = getSecureSetting( |
| 2038 | Settings.Secure.LOCATION_PROVIDERS_ALLOWED, owningUserId); |
| 2039 | if (oldSetting == null) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2040 | return false; |
| 2041 | } |
Maggie | 83e03f5 | 2018-03-16 12:22:20 -0700 | [diff] [blame] | 2042 | String oldProviders = oldSetting.getValue(); |
| 2043 | List<String> oldProvidersList = TextUtils.isEmpty(oldProviders) |
| 2044 | ? new ArrayList<>() : new ArrayList<>(Arrays.asList(oldProviders.split(","))); |
| 2045 | Set<String> newProvidersSet = new ArraySet<>(); |
| 2046 | newProvidersSet.addAll(oldProvidersList); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2047 | |
Maggie | 83e03f5 | 2018-03-16 12:22:20 -0700 | [diff] [blame] | 2048 | String[] providerUpdates = value.split(","); |
| 2049 | boolean inputError = false; |
| 2050 | for (String provider : providerUpdates) { |
| 2051 | // do not update location_providers_allowed when input is invalid |
| 2052 | if (TextUtils.isEmpty(provider)) { |
| 2053 | inputError = true; |
| 2054 | break; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2055 | } |
Maggie | 83e03f5 | 2018-03-16 12:22:20 -0700 | [diff] [blame] | 2056 | final char prefix = provider.charAt(0); |
| 2057 | // do not update location_providers_allowed when input is invalid |
| 2058 | if (prefix != '+' && prefix != '-') { |
| 2059 | inputError = true; |
| 2060 | break; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2061 | } |
Maggie | 83e03f5 | 2018-03-16 12:22:20 -0700 | [diff] [blame] | 2062 | // skip prefix |
| 2063 | provider = provider.substring(1); |
| 2064 | if (prefix == '+') { |
| 2065 | newProvidersSet.add(provider); |
| 2066 | } else if (prefix == '-') { |
| 2067 | newProvidersSet.remove(provider); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2068 | } |
Maggie | 83e03f5 | 2018-03-16 12:22:20 -0700 | [diff] [blame] | 2069 | } |
| 2070 | String newProviders = TextUtils.join(",", newProvidersSet.toArray()); |
| 2071 | if (inputError == true || newProviders.equals(oldProviders)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2072 | // nothing changed, so no need to update the database |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2073 | if (forceNotify) { |
Maggie | 83e03f5 | 2018-03-16 12:22:20 -0700 | [diff] [blame] | 2074 | mSettingsRegistry.notifyForSettingsChange( |
| 2075 | makeKey(SETTINGS_TYPE_SECURE, owningUserId), |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2076 | Settings.Secure.LOCATION_PROVIDERS_ALLOWED); |
| 2077 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2078 | return false; |
| 2079 | } |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2080 | return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE, |
Maggie | 83e03f5 | 2018-03-16 12:22:20 -0700 | [diff] [blame] | 2081 | owningUserId, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, newProviders, tag, |
| 2082 | makeDefault, getCallingPackage(), forceNotify, CRITICAL_SECURE_SETTINGS); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2083 | } |
| 2084 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2085 | private static void warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk( |
| 2086 | int targetSdkVersion, String name) { |
| 2087 | // If the app targets Lollipop MR1 or older SDK we warn, otherwise crash. |
| 2088 | if (targetSdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) { |
| 2089 | if (Settings.System.PRIVATE_SETTINGS.contains(name)) { |
| 2090 | Slog.w(LOG_TAG, "You shouldn't not change private system settings." |
| 2091 | + " This will soon become an error."); |
| 2092 | } else { |
| 2093 | Slog.w(LOG_TAG, "You shouldn't keep your settings in the secure settings." |
| 2094 | + " This will soon become an error."); |
| 2095 | } |
| 2096 | } else { |
| 2097 | if (Settings.System.PRIVATE_SETTINGS.contains(name)) { |
| 2098 | throw new IllegalArgumentException("You cannot change private secure settings."); |
| 2099 | } else { |
| 2100 | throw new IllegalArgumentException("You cannot keep your settings in" |
| 2101 | + " the secure settings."); |
| 2102 | } |
| 2103 | } |
| 2104 | } |
| 2105 | |
| 2106 | private static int resolveCallingUserIdEnforcingPermissionsLocked(int requestingUserId) { |
| 2107 | if (requestingUserId == UserHandle.getCallingUserId()) { |
| 2108 | return requestingUserId; |
| 2109 | } |
| 2110 | return ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2111 | Binder.getCallingUid(), requestingUserId, false, true, |
| 2112 | "get/set setting for user", null); |
| 2113 | } |
| 2114 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2115 | private Bundle packageValueForCallResult(Setting setting, |
| 2116 | boolean trackingGeneration) { |
| 2117 | if (!trackingGeneration) { |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2118 | if (setting == null || setting.isNull()) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2119 | return NULL_SETTING_BUNDLE; |
| 2120 | } |
| 2121 | return Bundle.forPair(Settings.NameValueTable.VALUE, setting.getValue()); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2122 | } |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2123 | Bundle result = new Bundle(); |
| 2124 | result.putString(Settings.NameValueTable.VALUE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2125 | !setting.isNull() ? setting.getValue() : null); |
Svet Ganov | 96c9946 | 2017-05-05 14:27:13 -0700 | [diff] [blame] | 2126 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2127 | mSettingsRegistry.mGenerationRegistry.addGenerationData(result, setting.getKey()); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2128 | return result; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | private static int getRequestingUserId(Bundle args) { |
| 2132 | final int callingUserId = UserHandle.getCallingUserId(); |
| 2133 | return (args != null) ? args.getInt(Settings.CALL_METHOD_USER_KEY, callingUserId) |
| 2134 | : callingUserId; |
| 2135 | } |
| 2136 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2137 | private boolean isTrackingGeneration(Bundle args) { |
| 2138 | return args != null && args.containsKey(Settings.CALL_METHOD_TRACK_GENERATION_KEY); |
| 2139 | } |
| 2140 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2141 | private static String getSettingValue(Bundle args) { |
| 2142 | return (args != null) ? args.getString(Settings.NameValueTable.VALUE) : null; |
| 2143 | } |
| 2144 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2145 | private static String getSettingTag(Bundle args) { |
| 2146 | return (args != null) ? args.getString(Settings.CALL_METHOD_TAG_KEY) : null; |
| 2147 | } |
| 2148 | |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 2149 | private static String getSettingPrefix(Bundle args) { |
| 2150 | String prefix = (args != null) ? args.getString(Settings.CALL_METHOD_PREFIX_KEY) : null; |
| 2151 | // Append '/' to ensure we only match properties with this exact prefix. |
| 2152 | // i.e. "foo" should match "foo/property" but not "foobar/property" |
| 2153 | return prefix != null ? prefix + "/" : null; |
| 2154 | } |
| 2155 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2156 | private static boolean getSettingMakeDefault(Bundle args) { |
| 2157 | return (args != null) && args.getBoolean(Settings.CALL_METHOD_MAKE_DEFAULT_KEY); |
| 2158 | } |
| 2159 | |
| 2160 | private static int getResetModeEnforcingPermission(Bundle args) { |
| 2161 | final int mode = (args != null) ? args.getInt(Settings.CALL_METHOD_RESET_MODE_KEY) : 0; |
| 2162 | switch (mode) { |
| 2163 | case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: { |
| 2164 | if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) { |
| 2165 | throw new SecurityException("Only system, shell/root on a " |
| 2166 | + "debuggable build can reset to untrusted defaults"); |
| 2167 | } |
| 2168 | return mode; |
| 2169 | } |
| 2170 | case Settings.RESET_MODE_UNTRUSTED_CHANGES: { |
| 2171 | if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) { |
| 2172 | throw new SecurityException("Only system, shell/root on a " |
| 2173 | + "debuggable build can reset untrusted changes"); |
| 2174 | } |
| 2175 | return mode; |
| 2176 | } |
| 2177 | case Settings.RESET_MODE_TRUSTED_DEFAULTS: { |
| 2178 | if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) { |
| 2179 | throw new SecurityException("Only system, shell/root on a " |
| 2180 | + "debuggable build can reset to trusted defaults"); |
| 2181 | } |
| 2182 | return mode; |
| 2183 | } |
| 2184 | case Settings.RESET_MODE_PACKAGE_DEFAULTS: { |
| 2185 | return mode; |
| 2186 | } |
| 2187 | } |
| 2188 | throw new IllegalArgumentException("Invalid reset mode: " + mode); |
| 2189 | } |
| 2190 | |
| 2191 | private static boolean isCallerSystemOrShellOrRootOnDebuggableBuild() { |
| 2192 | final int appId = UserHandle.getAppId(Binder.getCallingUid()); |
| 2193 | return appId == SYSTEM_UID || (Build.IS_DEBUGGABLE |
| 2194 | && (appId == SHELL_UID || appId == ROOT_UID)); |
| 2195 | } |
| 2196 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2197 | private static String getValidTableOrThrow(Uri uri) { |
| 2198 | if (uri.getPathSegments().size() > 0) { |
| 2199 | String table = uri.getPathSegments().get(0); |
| 2200 | if (DatabaseHelper.isValidTable(table)) { |
| 2201 | return table; |
| 2202 | } |
| 2203 | throw new IllegalArgumentException("Bad root path: " + table); |
| 2204 | } |
| 2205 | throw new IllegalArgumentException("Invalid URI:" + uri); |
| 2206 | } |
| 2207 | |
| 2208 | private static MatrixCursor packageSettingForQuery(Setting setting, String[] projection) { |
Svetoslav Ganov | fedb230 | 2016-04-26 18:36:42 -0700 | [diff] [blame] | 2209 | if (setting.isNull()) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2210 | return new MatrixCursor(projection, 0); |
| 2211 | } |
| 2212 | MatrixCursor cursor = new MatrixCursor(projection, 1); |
| 2213 | appendSettingToCursor(cursor, setting); |
| 2214 | return cursor; |
| 2215 | } |
| 2216 | |
| 2217 | private static String[] normalizeProjection(String[] projection) { |
| 2218 | if (projection == null) { |
| 2219 | return ALL_COLUMNS; |
| 2220 | } |
| 2221 | |
| 2222 | final int columnCount = projection.length; |
| 2223 | for (int i = 0; i < columnCount; i++) { |
| 2224 | String column = projection[i]; |
| 2225 | if (!ArrayUtils.contains(ALL_COLUMNS, column)) { |
| 2226 | throw new IllegalArgumentException("Invalid column: " + column); |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 2227 | } |
| 2228 | } |
| 2229 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2230 | return projection; |
| 2231 | } |
| 2232 | |
| 2233 | private static void appendSettingToCursor(MatrixCursor cursor, Setting setting) { |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2234 | if (setting == null || setting.isNull()) { |
Suprabh Shukla | c9d064a | 2016-04-12 18:45:34 -0700 | [diff] [blame] | 2235 | return; |
| 2236 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2237 | final int columnCount = cursor.getColumnCount(); |
| 2238 | |
| 2239 | String[] values = new String[columnCount]; |
| 2240 | |
| 2241 | for (int i = 0; i < columnCount; i++) { |
| 2242 | String column = cursor.getColumnName(i); |
| 2243 | |
| 2244 | switch (column) { |
| 2245 | case Settings.NameValueTable._ID: { |
| 2246 | values[i] = setting.getId(); |
| 2247 | } break; |
| 2248 | |
| 2249 | case Settings.NameValueTable.NAME: { |
| 2250 | values[i] = setting.getName(); |
| 2251 | } break; |
| 2252 | |
| 2253 | case Settings.NameValueTable.VALUE: { |
| 2254 | values[i] = setting.getValue(); |
| 2255 | } break; |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 2256 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2257 | } |
| 2258 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2259 | cursor.addRow(values); |
| 2260 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2261 | |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 2262 | private static boolean isKeyValid(String key) { |
| 2263 | return !(TextUtils.isEmpty(key) || SettingsState.isBinary(key)); |
| 2264 | } |
| 2265 | |
Matt Pape | 7b1c6cd | 2019-01-04 08:10:41 -0800 | [diff] [blame] | 2266 | private String resolveCallingPackage() { |
| 2267 | switch (Binder.getCallingUid()) { |
| 2268 | case Process.ROOT_UID: { |
| 2269 | return "root"; |
| 2270 | } |
| 2271 | |
| 2272 | case Process.SHELL_UID: { |
| 2273 | return "com.android.shell"; |
| 2274 | } |
| 2275 | |
| 2276 | default: { |
| 2277 | return getCallingPackage(); |
| 2278 | } |
| 2279 | } |
| 2280 | } |
| 2281 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2282 | private static final class Arguments { |
| 2283 | private static final Pattern WHERE_PATTERN_WITH_PARAM_NO_BRACKETS = |
| 2284 | Pattern.compile("[\\s]*name[\\s]*=[\\s]*\\?[\\s]*"); |
| 2285 | |
| 2286 | private static final Pattern WHERE_PATTERN_WITH_PARAM_IN_BRACKETS = |
| 2287 | Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*\\?[\\s]*\\)[\\s]*"); |
| 2288 | |
| 2289 | private static final Pattern WHERE_PATTERN_NO_PARAM_IN_BRACKETS = |
| 2290 | Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*\\)[\\s]*"); |
| 2291 | |
| 2292 | private static final Pattern WHERE_PATTERN_NO_PARAM_NO_BRACKETS = |
| 2293 | Pattern.compile("[\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*"); |
| 2294 | |
| 2295 | public final String table; |
| 2296 | public final String name; |
| 2297 | |
| 2298 | public Arguments(Uri uri, String where, String[] whereArgs, boolean supportAll) { |
| 2299 | final int segmentSize = uri.getPathSegments().size(); |
| 2300 | switch (segmentSize) { |
| 2301 | case 1: { |
| 2302 | if (where != null |
| 2303 | && (WHERE_PATTERN_WITH_PARAM_NO_BRACKETS.matcher(where).matches() |
| 2304 | || WHERE_PATTERN_WITH_PARAM_IN_BRACKETS.matcher(where).matches()) |
| 2305 | && whereArgs.length == 1) { |
| 2306 | name = whereArgs[0]; |
| 2307 | table = computeTableForSetting(uri, name); |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 2308 | return; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2309 | } else if (where != null |
| 2310 | && (WHERE_PATTERN_NO_PARAM_NO_BRACKETS.matcher(where).matches() |
| 2311 | || WHERE_PATTERN_NO_PARAM_IN_BRACKETS.matcher(where).matches())) { |
| 2312 | final int startIndex = Math.max(where.indexOf("'"), |
| 2313 | where.indexOf("\"")) + 1; |
| 2314 | final int endIndex = Math.max(where.lastIndexOf("'"), |
| 2315 | where.lastIndexOf("\"")); |
| 2316 | name = where.substring(startIndex, endIndex); |
| 2317 | table = computeTableForSetting(uri, name); |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 2318 | return; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2319 | } else if (supportAll && where == null && whereArgs == null) { |
| 2320 | name = null; |
| 2321 | table = computeTableForSetting(uri, null); |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 2322 | return; |
Brad Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame] | 2323 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2324 | } break; |
| 2325 | |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 2326 | case 2: { |
| 2327 | if (where == null && whereArgs == null) { |
| 2328 | name = uri.getPathSegments().get(1); |
| 2329 | table = computeTableForSetting(uri, name); |
| 2330 | return; |
| 2331 | } |
| 2332 | } break; |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2333 | } |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 2334 | |
| 2335 | EventLogTags.writeUnsupportedSettingsQuery( |
| 2336 | uri.toSafeString(), where, Arrays.toString(whereArgs)); |
| 2337 | String message = String.format( "Supported SQL:\n" |
| 2338 | + " uri content://some_table/some_property with null where and where args\n" |
| 2339 | + " uri content://some_table with query name=? and single name as arg\n" |
| 2340 | + " uri content://some_table with query name=some_name and null args\n" |
| 2341 | + " but got - uri:%1s, where:%2s whereArgs:%3s", uri, where, |
| 2342 | Arrays.toString(whereArgs)); |
| 2343 | throw new IllegalArgumentException(message); |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2344 | } |
| 2345 | |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 2346 | private static String computeTableForSetting(Uri uri, String name) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2347 | String table = getValidTableOrThrow(uri); |
| 2348 | |
| 2349 | if (name != null) { |
| 2350 | if (sSystemMovedToSecureSettings.contains(name)) { |
| 2351 | table = TABLE_SECURE; |
| 2352 | } |
| 2353 | |
| 2354 | if (sSystemMovedToGlobalSettings.contains(name)) { |
| 2355 | table = TABLE_GLOBAL; |
| 2356 | } |
| 2357 | |
| 2358 | if (sSecureMovedToGlobalSettings.contains(name)) { |
| 2359 | table = TABLE_GLOBAL; |
| 2360 | } |
| 2361 | |
| 2362 | if (sGlobalMovedToSecureSettings.contains(name)) { |
| 2363 | table = TABLE_SECURE; |
| 2364 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2365 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2366 | |
| 2367 | return table; |
| 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | final class SettingsRegistry { |
| 2372 | private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid"; |
| 2373 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2374 | private static final String SETTINGS_FILE_GLOBAL = "settings_global.xml"; |
| 2375 | private static final String SETTINGS_FILE_SYSTEM = "settings_system.xml"; |
| 2376 | private static final String SETTINGS_FILE_SECURE = "settings_secure.xml"; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2377 | private static final String SETTINGS_FILE_SSAID = "settings_ssaid.xml"; |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 2378 | private static final String SETTINGS_FILE_CONFIG = "settings_config.xml"; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2379 | |
| 2380 | private static final String SSAID_USER_KEY = "userkey"; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2381 | |
| 2382 | private final SparseArray<SettingsState> mSettingsStates = new SparseArray<>(); |
| 2383 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2384 | private GenerationRegistry mGenerationRegistry; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2385 | |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 2386 | private final Handler mHandler; |
| 2387 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2388 | private final BackupManager mBackupManager; |
| 2389 | |
Amith Yamasani | 3945202 | 2017-03-21 15:23:47 -0700 | [diff] [blame] | 2390 | private String mSettingsCreationBuildId; |
| 2391 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2392 | public SettingsRegistry() { |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 2393 | mHandler = new MyHandler(getContext().getMainLooper()); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2394 | mGenerationRegistry = new GenerationRegistry(mLock); |
| 2395 | mBackupManager = new BackupManager(getContext()); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2396 | migrateAllLegacySettingsIfNeeded(); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2397 | syncSsaidTableOnStart(); |
| 2398 | } |
| 2399 | |
| 2400 | private void generateUserKeyLocked(int userId) { |
| 2401 | // Generate a random key for each user used for creating a new ssaid. |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 2402 | final byte[] keyBytes = new byte[32]; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2403 | final SecureRandom rand = new SecureRandom(); |
| 2404 | rand.nextBytes(keyBytes); |
| 2405 | |
| 2406 | // Convert to string for storage in settings table. |
Neil Fuller | 3447fb4 | 2019-04-08 22:03:14 +0100 | [diff] [blame] | 2407 | final String userKey = HexEncoding.encodeToString(keyBytes, true /* upperCase */); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2408 | |
| 2409 | // Store the key in the ssaid table. |
| 2410 | final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId); |
| 2411 | final boolean success = ssaidSettings.insertSettingLocked(SSAID_USER_KEY, userKey, null, |
| 2412 | true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 2413 | |
| 2414 | if (!success) { |
| 2415 | throw new IllegalStateException("Ssaid settings not accessible"); |
| 2416 | } |
| 2417 | } |
| 2418 | |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 2419 | private byte[] getLengthPrefix(byte[] data) { |
| 2420 | return ByteBuffer.allocate(4).putInt(data.length).array(); |
| 2421 | } |
| 2422 | |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 2423 | public Setting generateSsaidLocked(PackageInfo callingPkg, int userId) { |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2424 | // Read the user's key from the ssaid table. |
| 2425 | Setting userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY); |
Mark Rathjen | ea61759 | 2017-01-18 23:03:41 -0800 | [diff] [blame] | 2426 | if (userKeySetting == null || userKeySetting.isNull() |
| 2427 | || userKeySetting.getValue() == null) { |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2428 | // Lazy initialize and store the user key. |
| 2429 | generateUserKeyLocked(userId); |
| 2430 | userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY); |
Mark Rathjen | ea61759 | 2017-01-18 23:03:41 -0800 | [diff] [blame] | 2431 | if (userKeySetting == null || userKeySetting.isNull() |
| 2432 | || userKeySetting.getValue() == null) { |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2433 | throw new IllegalStateException("User key not accessible"); |
| 2434 | } |
| 2435 | } |
| 2436 | final String userKey = userKeySetting.getValue(); |
Neil Fuller | 3447fb4 | 2019-04-08 22:03:14 +0100 | [diff] [blame] | 2437 | if (userKey == null || userKey.length() % 2 != 0) { |
| 2438 | throw new IllegalStateException("User key invalid"); |
| 2439 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2440 | |
| 2441 | // Convert the user's key back to a byte array. |
Neil Fuller | 3447fb4 | 2019-04-08 22:03:14 +0100 | [diff] [blame] | 2442 | final byte[] keyBytes = HexEncoding.decode(userKey); |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 2443 | |
| 2444 | // Validate that the key is of expected length. |
| 2445 | // Keys are currently 32 bytes, but were once 16 bytes during Android O development. |
Neil Fuller | 3447fb4 | 2019-04-08 22:03:14 +0100 | [diff] [blame] | 2446 | if (keyBytes.length != 16 && keyBytes.length != 32) { |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2447 | throw new IllegalStateException("User key invalid"); |
| 2448 | } |
| 2449 | |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 2450 | final Mac m; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2451 | try { |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 2452 | m = Mac.getInstance("HmacSHA256"); |
| 2453 | m.init(new SecretKeySpec(keyBytes, m.getAlgorithm())); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2454 | } catch (NoSuchAlgorithmException e) { |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 2455 | throw new IllegalStateException("HmacSHA256 is not available", e); |
| 2456 | } catch (InvalidKeyException e) { |
| 2457 | throw new IllegalStateException("Key is corrupted", e); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2458 | } |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 2459 | |
Mark Rathjen | f42dd91 | 2017-06-05 19:04:34 -0700 | [diff] [blame] | 2460 | // Mac each of the developer signatures. |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 2461 | for (int i = 0; i < callingPkg.signatures.length; i++) { |
| 2462 | byte[] sig = callingPkg.signatures[i].toByteArray(); |
Mark Rathjen | 7599f13 | 2017-01-23 14:15:54 -0800 | [diff] [blame] | 2463 | m.update(getLengthPrefix(sig), 0, 4); |
| 2464 | m.update(sig); |
| 2465 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2466 | |
| 2467 | // Convert result to a string for storage in settings table. Only want first 64 bits. |
Neil Fuller | 3447fb4 | 2019-04-08 22:03:14 +0100 | [diff] [blame] | 2468 | final String ssaid = HexEncoding.encodeToString(m.doFinal(), false /* upperCase */) |
| 2469 | .substring(0, 16); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2470 | |
| 2471 | // Save the ssaid in the ssaid table. |
Christopher Tate | b218e76 | 2017-04-05 16:34:07 -0700 | [diff] [blame] | 2472 | final String uid = Integer.toString(callingPkg.applicationInfo.uid); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2473 | final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId); |
| 2474 | final boolean success = ssaidSettings.insertSettingLocked(uid, ssaid, null, true, |
Mark Rathjen | f42dd91 | 2017-06-05 19:04:34 -0700 | [diff] [blame] | 2475 | callingPkg.packageName); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2476 | |
| 2477 | if (!success) { |
| 2478 | throw new IllegalStateException("Ssaid settings not accessible"); |
| 2479 | } |
| 2480 | |
| 2481 | return getSettingLocked(SETTINGS_TYPE_SSAID, userId, uid); |
| 2482 | } |
| 2483 | |
| 2484 | public void syncSsaidTableOnStart() { |
| 2485 | synchronized (mLock) { |
| 2486 | // Verify that each user's packages and ssaid's are in sync. |
| 2487 | for (UserInfo user : mUserManager.getUsers(true)) { |
| 2488 | // Get all uids for the user's packages. |
| 2489 | final List<PackageInfo> packages; |
| 2490 | try { |
Tetsutoki Shiozawa | ebe0e5f | 2018-01-17 11:07:09 +0900 | [diff] [blame] | 2491 | packages = mPackageManager.getInstalledPackages( |
| 2492 | PackageManager.MATCH_UNINSTALLED_PACKAGES, |
| 2493 | user.id).getList(); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2494 | } catch (RemoteException e) { |
| 2495 | throw new IllegalStateException("Package manager not available"); |
| 2496 | } |
| 2497 | final Set<String> appUids = new HashSet<>(); |
| 2498 | for (PackageInfo info : packages) { |
| 2499 | appUids.add(Integer.toString(info.applicationInfo.uid)); |
| 2500 | } |
| 2501 | |
| 2502 | // Get all uids currently stored in the user's ssaid table. |
| 2503 | final Set<String> ssaidUids = new HashSet<>( |
| 2504 | getSettingsNamesLocked(SETTINGS_TYPE_SSAID, user.id)); |
| 2505 | ssaidUids.remove(SSAID_USER_KEY); |
| 2506 | |
| 2507 | // Perform a set difference for the appUids and ssaidUids. |
| 2508 | ssaidUids.removeAll(appUids); |
| 2509 | |
| 2510 | // If there are ssaidUids left over they need to be removed from the table. |
| 2511 | final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, |
| 2512 | user.id); |
| 2513 | for (String uid : ssaidUids) { |
| 2514 | ssaidSettings.deleteSettingLocked(uid); |
| 2515 | } |
| 2516 | } |
| 2517 | } |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 2518 | } |
| 2519 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2520 | public List<String> getSettingsNamesLocked(int type, int userId) { |
| 2521 | final int key = makeKey(type, userId); |
| 2522 | SettingsState settingsState = peekSettingsStateLocked(key); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2523 | if (settingsState == null) { |
| 2524 | return new ArrayList<String>(); |
| 2525 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2526 | return settingsState.getSettingNamesLocked(); |
| 2527 | } |
| 2528 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2529 | public SparseBooleanArray getKnownUsersLocked() { |
| 2530 | SparseBooleanArray users = new SparseBooleanArray(); |
| 2531 | for (int i = mSettingsStates.size()-1; i >= 0; i--) { |
| 2532 | users.put(getUserIdFromKey(mSettingsStates.keyAt(i)), true); |
| 2533 | } |
| 2534 | return users; |
| 2535 | } |
| 2536 | |
Kweku Adams | b0886f3 | 2017-10-31 15:32:09 -0700 | [diff] [blame] | 2537 | @Nullable |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2538 | public SettingsState getSettingsLocked(int type, int userId) { |
| 2539 | final int key = makeKey(type, userId); |
| 2540 | return peekSettingsStateLocked(key); |
| 2541 | } |
| 2542 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2543 | public boolean ensureSettingsForUserLocked(int userId) { |
| 2544 | // First make sure this user actually exists. |
| 2545 | if (mUserManager.getUserInfo(userId) == null) { |
| 2546 | Slog.wtf(LOG_TAG, "Requested user " + userId + " does not exist"); |
| 2547 | return false; |
| 2548 | } |
| 2549 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2550 | // Migrate the setting for this user if needed. |
| 2551 | migrateLegacySettingsForUserIfNeededLocked(userId); |
| 2552 | |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 2553 | // Ensure config settings loaded if owner. |
| 2554 | if (userId == UserHandle.USER_SYSTEM) { |
| 2555 | final int configKey |
| 2556 | = makeKey(SETTINGS_TYPE_CONFIG, UserHandle.USER_SYSTEM); |
| 2557 | ensureSettingsStateLocked(configKey); |
| 2558 | } |
| 2559 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2560 | // Ensure global settings loaded if owner. |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 2561 | if (userId == UserHandle.USER_SYSTEM) { |
| 2562 | final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2563 | ensureSettingsStateLocked(globalKey); |
| 2564 | } |
| 2565 | |
| 2566 | // Ensure secure settings loaded. |
| 2567 | final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId); |
| 2568 | ensureSettingsStateLocked(secureKey); |
| 2569 | |
| 2570 | // Make sure the secure settings have an Android id set. |
| 2571 | SettingsState secureSettings = getSettingsLocked(SETTINGS_TYPE_SECURE, userId); |
| 2572 | ensureSecureSettingAndroidIdSetLocked(secureSettings); |
| 2573 | |
| 2574 | // Ensure system settings loaded. |
| 2575 | final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId); |
| 2576 | ensureSettingsStateLocked(systemKey); |
| 2577 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2578 | // Ensure secure settings loaded. |
| 2579 | final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId); |
| 2580 | ensureSettingsStateLocked(ssaidKey); |
| 2581 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2582 | // Upgrade the settings to the latest version. |
| 2583 | UpgradeController upgrader = new UpgradeController(userId); |
| 2584 | upgrader.upgradeIfNeededLocked(); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2585 | return true; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | private void ensureSettingsStateLocked(int key) { |
| 2589 | if (mSettingsStates.get(key) == null) { |
| 2590 | final int maxBytesPerPackage = getMaxBytesPerPackageForType(getTypeFromKey(key)); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2591 | SettingsState settingsState = new SettingsState(getContext(), mLock, |
| 2592 | getSettingsFile(key), key, maxBytesPerPackage, mHandlerThread.getLooper()); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2593 | mSettingsStates.put(key, settingsState); |
| 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | public void removeUserStateLocked(int userId, boolean permanently) { |
| 2598 | // We always keep the global settings in memory. |
| 2599 | |
| 2600 | // Nuke system settings. |
| 2601 | final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId); |
| 2602 | final SettingsState systemSettingsState = mSettingsStates.get(systemKey); |
| 2603 | if (systemSettingsState != null) { |
| 2604 | if (permanently) { |
| 2605 | mSettingsStates.remove(systemKey); |
| 2606 | systemSettingsState.destroyLocked(null); |
Brad Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame] | 2607 | } else { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2608 | systemSettingsState.destroyLocked(new Runnable() { |
| 2609 | @Override |
| 2610 | public void run() { |
| 2611 | mSettingsStates.remove(systemKey); |
| 2612 | } |
| 2613 | }); |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | // Nuke secure settings. |
| 2618 | final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId); |
| 2619 | final SettingsState secureSettingsState = mSettingsStates.get(secureKey); |
| 2620 | if (secureSettingsState != null) { |
| 2621 | if (permanently) { |
| 2622 | mSettingsStates.remove(secureKey); |
| 2623 | secureSettingsState.destroyLocked(null); |
| 2624 | } else { |
| 2625 | secureSettingsState.destroyLocked(new Runnable() { |
| 2626 | @Override |
| 2627 | public void run() { |
| 2628 | mSettingsStates.remove(secureKey); |
| 2629 | } |
| 2630 | }); |
Brad Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame] | 2631 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2632 | } |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2633 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2634 | // Nuke ssaid settings. |
| 2635 | final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId); |
| 2636 | final SettingsState ssaidSettingsState = mSettingsStates.get(ssaidKey); |
| 2637 | if (ssaidSettingsState != null) { |
| 2638 | if (permanently) { |
| 2639 | mSettingsStates.remove(ssaidKey); |
| 2640 | ssaidSettingsState.destroyLocked(null); |
| 2641 | } else { |
| 2642 | ssaidSettingsState.destroyLocked(new Runnable() { |
| 2643 | @Override |
| 2644 | public void run() { |
| 2645 | mSettingsStates.remove(ssaidKey); |
| 2646 | } |
| 2647 | }); |
| 2648 | } |
| 2649 | } |
| 2650 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2651 | // Nuke generation tracking data |
| 2652 | mGenerationRegistry.onUserRemoved(userId); |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2653 | } |
| 2654 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2655 | public boolean insertSettingLocked(int type, int userId, String name, String value, |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2656 | String tag, boolean makeDefault, String packageName, boolean forceNotify, |
| 2657 | Set<String> criticalSettings) { |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 2658 | return insertSettingLocked(type, userId, name, value, tag, makeDefault, false, |
| 2659 | packageName, forceNotify, criticalSettings); |
| 2660 | } |
| 2661 | |
| 2662 | public boolean insertSettingLocked(int type, int userId, String name, String value, |
| 2663 | String tag, boolean makeDefault, boolean forceNonSystemPackage, String packageName, |
| 2664 | boolean forceNotify, Set<String> criticalSettings) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2665 | final int key = makeKey(type, userId); |
| 2666 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2667 | boolean success = false; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2668 | SettingsState settingsState = peekSettingsStateLocked(key); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2669 | if (settingsState != null) { |
| 2670 | success = settingsState.insertSettingLocked(name, value, |
Matt Pape | c1323dc | 2018-12-11 12:32:42 -0800 | [diff] [blame] | 2671 | tag, makeDefault, forceNonSystemPackage, packageName); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2672 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2673 | |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2674 | if (success && criticalSettings != null && criticalSettings.contains(name)) { |
| 2675 | settingsState.persistSyncLocked(); |
| 2676 | } |
| 2677 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2678 | if (forceNotify || success) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2679 | notifyForSettingsChange(key, name); |
| 2680 | } |
| 2681 | return success; |
| 2682 | } |
| 2683 | |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2684 | public boolean deleteSettingLocked(int type, int userId, String name, boolean forceNotify, |
| 2685 | Set<String> criticalSettings) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2686 | final int key = makeKey(type, userId); |
| 2687 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2688 | boolean success = false; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2689 | SettingsState settingsState = peekSettingsStateLocked(key); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2690 | if (settingsState != null) { |
| 2691 | success = settingsState.deleteSettingLocked(name); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2692 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2693 | |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2694 | if (success && criticalSettings != null && criticalSettings.contains(name)) { |
| 2695 | settingsState.persistSyncLocked(); |
| 2696 | } |
| 2697 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2698 | if (forceNotify || success) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2699 | notifyForSettingsChange(key, name); |
| 2700 | } |
| 2701 | return success; |
| 2702 | } |
| 2703 | |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2704 | public boolean updateSettingLocked(int type, int userId, String name, String value, |
| 2705 | String tag, boolean makeDefault, String packageName, boolean forceNotify, |
| 2706 | Set<String> criticalSettings) { |
| 2707 | final int key = makeKey(type, userId); |
| 2708 | |
| 2709 | boolean success = false; |
| 2710 | SettingsState settingsState = peekSettingsStateLocked(key); |
| 2711 | if (settingsState != null) { |
| 2712 | success = settingsState.updateSettingLocked(name, value, tag, |
| 2713 | makeDefault, packageName); |
| 2714 | } |
| 2715 | |
| 2716 | if (success && criticalSettings != null && criticalSettings.contains(name)) { |
| 2717 | settingsState.persistSyncLocked(); |
| 2718 | } |
| 2719 | |
| 2720 | if (forceNotify || success) { |
| 2721 | notifyForSettingsChange(key, name); |
| 2722 | } |
| 2723 | |
| 2724 | return success; |
| 2725 | } |
| 2726 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2727 | public Setting getSettingLocked(int type, int userId, String name) { |
| 2728 | final int key = makeKey(type, userId); |
| 2729 | |
| 2730 | SettingsState settingsState = peekSettingsStateLocked(key); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2731 | if (settingsState == null) { |
Mark Rathjen | ea61759 | 2017-01-18 23:03:41 -0800 | [diff] [blame] | 2732 | return null; |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2733 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2734 | |
| 2735 | // getSettingLocked will return non-null result |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2736 | return settingsState.getSettingLocked(name); |
| 2737 | } |
| 2738 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2739 | public void resetSettingsLocked(int type, int userId, String packageName, int mode, |
| 2740 | String tag) { |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 2741 | resetSettingsLocked(type, userId, packageName, mode, tag, null); |
| 2742 | } |
| 2743 | |
| 2744 | public void resetSettingsLocked(int type, int userId, String packageName, int mode, |
| 2745 | String tag, @Nullable String prefix) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2746 | final int key = makeKey(type, userId); |
| 2747 | SettingsState settingsState = peekSettingsStateLocked(key); |
| 2748 | if (settingsState == null) { |
| 2749 | return; |
| 2750 | } |
| 2751 | |
| 2752 | switch (mode) { |
| 2753 | case Settings.RESET_MODE_PACKAGE_DEFAULTS: { |
| 2754 | for (String name : settingsState.getSettingNamesLocked()) { |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2755 | boolean someSettingChanged = false; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2756 | Setting setting = settingsState.getSettingLocked(name); |
| 2757 | if (packageName.equals(setting.getPackageName())) { |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 2758 | if ((tag != null && !tag.equals(setting.getTag())) |
| 2759 | || (prefix != null && !setting.getName().startsWith(prefix))) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2760 | continue; |
| 2761 | } |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2762 | if (settingsState.resetSettingLocked(name)) { |
| 2763 | someSettingChanged = true; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2764 | notifyForSettingsChange(key, name); |
| 2765 | } |
| 2766 | } |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2767 | if (someSettingChanged) { |
| 2768 | settingsState.persistSyncLocked(); |
| 2769 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2770 | } |
| 2771 | } break; |
| 2772 | |
| 2773 | case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: { |
| 2774 | for (String name : settingsState.getSettingNamesLocked()) { |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2775 | boolean someSettingChanged = false; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2776 | Setting setting = settingsState.getSettingLocked(name); |
| 2777 | if (!SettingsState.isSystemPackage(getContext(), |
Matt Pape | 25c940d | 2019-03-26 12:14:33 -0700 | [diff] [blame] | 2778 | setting.getPackageName(), INVALID_UID, userId)) { |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 2779 | if (prefix != null && !setting.getName().startsWith(prefix)) { |
| 2780 | continue; |
| 2781 | } |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2782 | if (settingsState.resetSettingLocked(name)) { |
| 2783 | someSettingChanged = true; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2784 | notifyForSettingsChange(key, name); |
| 2785 | } |
| 2786 | } |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2787 | if (someSettingChanged) { |
| 2788 | settingsState.persistSyncLocked(); |
| 2789 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2790 | } |
| 2791 | } break; |
| 2792 | |
| 2793 | case Settings.RESET_MODE_UNTRUSTED_CHANGES: { |
| 2794 | for (String name : settingsState.getSettingNamesLocked()) { |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2795 | boolean someSettingChanged = false; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2796 | Setting setting = settingsState.getSettingLocked(name); |
| 2797 | if (!SettingsState.isSystemPackage(getContext(), |
Matt Pape | 25c940d | 2019-03-26 12:14:33 -0700 | [diff] [blame] | 2798 | setting.getPackageName(), INVALID_UID, userId)) { |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 2799 | if (prefix != null && !setting.getName().startsWith(prefix)) { |
| 2800 | continue; |
| 2801 | } |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame] | 2802 | if (setting.isDefaultFromSystem()) { |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2803 | if (settingsState.resetSettingLocked(name)) { |
| 2804 | someSettingChanged = true; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2805 | notifyForSettingsChange(key, name); |
| 2806 | } |
| 2807 | } else if (settingsState.deleteSettingLocked(name)) { |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2808 | someSettingChanged = true; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2809 | notifyForSettingsChange(key, name); |
| 2810 | } |
| 2811 | } |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2812 | if (someSettingChanged) { |
| 2813 | settingsState.persistSyncLocked(); |
| 2814 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2815 | } |
| 2816 | } break; |
| 2817 | |
| 2818 | case Settings.RESET_MODE_TRUSTED_DEFAULTS: { |
| 2819 | for (String name : settingsState.getSettingNamesLocked()) { |
| 2820 | Setting setting = settingsState.getSettingLocked(name); |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2821 | boolean someSettingChanged = false; |
Matt Pape | 6bfc62e | 2018-11-28 13:16:03 -0800 | [diff] [blame] | 2822 | if (prefix != null && !setting.getName().startsWith(prefix)) { |
| 2823 | continue; |
| 2824 | } |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame] | 2825 | if (setting.isDefaultFromSystem()) { |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2826 | if (settingsState.resetSettingLocked(name)) { |
| 2827 | someSettingChanged = true; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2828 | notifyForSettingsChange(key, name); |
| 2829 | } |
| 2830 | } else if (settingsState.deleteSettingLocked(name)) { |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2831 | someSettingChanged = true; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2832 | notifyForSettingsChange(key, name); |
| 2833 | } |
Svetoslav Ganov | 5fb405b | 2017-01-26 22:43:09 -0800 | [diff] [blame] | 2834 | if (someSettingChanged) { |
| 2835 | settingsState.persistSyncLocked(); |
| 2836 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2837 | } |
| 2838 | } break; |
| 2839 | } |
| 2840 | } |
| 2841 | |
Zimuzo | c56192c | 2018-07-25 10:40:01 +0100 | [diff] [blame] | 2842 | public void removeSettingsForPackageLocked(String packageName, int userId) { |
Svet Ganov | 8de3480 | 2015-04-27 09:33:40 -0700 | [diff] [blame] | 2843 | // Global and secure settings are signature protected. Apps signed |
| 2844 | // by the platform certificate are generally not uninstalled and |
| 2845 | // the main exception is tests. We trust components signed |
| 2846 | // by the platform certificate and do not do a clean up after them. |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2847 | |
| 2848 | final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId); |
| 2849 | SettingsState systemSettings = mSettingsStates.get(systemKey); |
Svet Ganov | 8de3480 | 2015-04-27 09:33:40 -0700 | [diff] [blame] | 2850 | if (systemSettings != null) { |
Zimuzo | c56192c | 2018-07-25 10:40:01 +0100 | [diff] [blame] | 2851 | systemSettings.removeSettingsForPackageLocked(packageName); |
Svet Ganov | 8de3480 | 2015-04-27 09:33:40 -0700 | [diff] [blame] | 2852 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2853 | } |
| 2854 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2855 | public void onUidRemovedLocked(int uid) { |
| 2856 | final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, |
| 2857 | UserHandle.getUserId(uid)); |
Tony Mak | 24c6ab4 | 2018-01-08 14:35:34 +0000 | [diff] [blame] | 2858 | if (ssaidSettings != null) { |
| 2859 | ssaidSettings.deleteSettingLocked(Integer.toString(uid)); |
| 2860 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Kweku Adams | b0886f3 | 2017-10-31 15:32:09 -0700 | [diff] [blame] | 2863 | @Nullable |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2864 | private SettingsState peekSettingsStateLocked(int key) { |
| 2865 | SettingsState settingsState = mSettingsStates.get(key); |
| 2866 | if (settingsState != null) { |
| 2867 | return settingsState; |
| 2868 | } |
| 2869 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2870 | if (!ensureSettingsForUserLocked(getUserIdFromKey(key))) { |
| 2871 | return null; |
| 2872 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2873 | return mSettingsStates.get(key); |
| 2874 | } |
| 2875 | |
| 2876 | private void migrateAllLegacySettingsIfNeeded() { |
| 2877 | synchronized (mLock) { |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 2878 | final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2879 | File globalFile = getSettingsFile(key); |
Amith Yamasani | 74bf71b | 2017-07-19 16:49:52 -0700 | [diff] [blame] | 2880 | if (SettingsState.stateFileExists(globalFile)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2881 | return; |
| 2882 | } |
| 2883 | |
Amith Yamasani | 3945202 | 2017-03-21 15:23:47 -0700 | [diff] [blame] | 2884 | mSettingsCreationBuildId = Build.ID; |
| 2885 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2886 | final long identity = Binder.clearCallingIdentity(); |
| 2887 | try { |
| 2888 | List<UserInfo> users = mUserManager.getUsers(true); |
| 2889 | |
| 2890 | final int userCount = users.size(); |
| 2891 | for (int i = 0; i < userCount; i++) { |
| 2892 | final int userId = users.get(i).id; |
| 2893 | |
| 2894 | DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId); |
| 2895 | SQLiteDatabase database = dbHelper.getWritableDatabase(); |
| 2896 | migrateLegacySettingsForUserLocked(dbHelper, database, userId); |
| 2897 | |
| 2898 | // Upgrade to the latest version. |
| 2899 | UpgradeController upgrader = new UpgradeController(userId); |
| 2900 | upgrader.upgradeIfNeededLocked(); |
| 2901 | |
| 2902 | // Drop from memory if not a running user. |
| 2903 | if (!mUserManager.isUserRunning(new UserHandle(userId))) { |
| 2904 | removeUserStateLocked(userId, false); |
| 2905 | } |
| 2906 | } |
| 2907 | } finally { |
| 2908 | Binder.restoreCallingIdentity(identity); |
| 2909 | } |
| 2910 | } |
| 2911 | } |
| 2912 | |
| 2913 | private void migrateLegacySettingsForUserIfNeededLocked(int userId) { |
| 2914 | // Every user has secure settings and if no file we need to migrate. |
| 2915 | final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId); |
| 2916 | File secureFile = getSettingsFile(secureKey); |
Amith Yamasani | 74bf71b | 2017-07-19 16:49:52 -0700 | [diff] [blame] | 2917 | if (SettingsState.stateFileExists(secureFile)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2918 | return; |
| 2919 | } |
| 2920 | |
| 2921 | DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId); |
| 2922 | SQLiteDatabase database = dbHelper.getWritableDatabase(); |
| 2923 | |
| 2924 | migrateLegacySettingsForUserLocked(dbHelper, database, userId); |
| 2925 | } |
| 2926 | |
| 2927 | private void migrateLegacySettingsForUserLocked(DatabaseHelper dbHelper, |
| 2928 | SQLiteDatabase database, int userId) { |
Amith Yamasani | bf2ef61 | 2016-03-07 16:37:18 -0800 | [diff] [blame] | 2929 | // Move over the system settings. |
| 2930 | final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId); |
| 2931 | ensureSettingsStateLocked(systemKey); |
| 2932 | SettingsState systemSettings = mSettingsStates.get(systemKey); |
| 2933 | migrateLegacySettingsLocked(systemSettings, database, TABLE_SYSTEM); |
| 2934 | systemSettings.persistSyncLocked(); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2935 | |
| 2936 | // Move over the secure settings. |
Amith Yamasani | bf2ef61 | 2016-03-07 16:37:18 -0800 | [diff] [blame] | 2937 | // Do this after System settings, since this is the first thing we check when deciding |
| 2938 | // to skip over migration from db to xml for a secondary user. |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2939 | final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId); |
| 2940 | ensureSettingsStateLocked(secureKey); |
| 2941 | SettingsState secureSettings = mSettingsStates.get(secureKey); |
| 2942 | migrateLegacySettingsLocked(secureSettings, database, TABLE_SECURE); |
| 2943 | ensureSecureSettingAndroidIdSetLocked(secureSettings); |
| 2944 | secureSettings.persistSyncLocked(); |
| 2945 | |
Amith Yamasani | bf2ef61 | 2016-03-07 16:37:18 -0800 | [diff] [blame] | 2946 | // Move over the global settings if owner. |
| 2947 | // Do this last, since this is the first thing we check when deciding |
| 2948 | // to skip over migration from db to xml for owner user. |
| 2949 | if (userId == UserHandle.USER_SYSTEM) { |
| 2950 | final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, userId); |
| 2951 | ensureSettingsStateLocked(globalKey); |
| 2952 | SettingsState globalSettings = mSettingsStates.get(globalKey); |
| 2953 | migrateLegacySettingsLocked(globalSettings, database, TABLE_GLOBAL); |
Amith Yamasani | 3945202 | 2017-03-21 15:23:47 -0700 | [diff] [blame] | 2954 | // If this was just created |
| 2955 | if (mSettingsCreationBuildId != null) { |
| 2956 | globalSettings.insertSettingLocked(Settings.Global.DATABASE_CREATION_BUILDID, |
| 2957 | mSettingsCreationBuildId, null, true, |
| 2958 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 2959 | } |
Amith Yamasani | bf2ef61 | 2016-03-07 16:37:18 -0800 | [diff] [blame] | 2960 | globalSettings.persistSyncLocked(); |
| 2961 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2962 | |
| 2963 | // Drop the database as now all is moved and persisted. |
| 2964 | if (DROP_DATABASE_ON_MIGRATION) { |
| 2965 | dbHelper.dropDatabase(); |
| 2966 | } else { |
| 2967 | dbHelper.backupDatabase(); |
| 2968 | } |
| 2969 | } |
| 2970 | |
| 2971 | private void migrateLegacySettingsLocked(SettingsState settingsState, |
| 2972 | SQLiteDatabase database, String table) { |
| 2973 | SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); |
| 2974 | queryBuilder.setTables(table); |
| 2975 | |
| 2976 | Cursor cursor = queryBuilder.query(database, ALL_COLUMNS, |
| 2977 | null, null, null, null, null); |
| 2978 | |
| 2979 | if (cursor == null) { |
| 2980 | return; |
| 2981 | } |
| 2982 | |
| 2983 | try { |
| 2984 | if (!cursor.moveToFirst()) { |
| 2985 | return; |
| 2986 | } |
| 2987 | |
| 2988 | final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME); |
| 2989 | final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE); |
| 2990 | |
| 2991 | settingsState.setVersionLocked(database.getVersion()); |
| 2992 | |
| 2993 | while (!cursor.isAfterLast()) { |
| 2994 | String name = cursor.getString(nameColumnIdx); |
| 2995 | String value = cursor.getString(valueColumnIdx); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2996 | settingsState.insertSettingLocked(name, value, null, true, |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2997 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 2998 | cursor.moveToNext(); |
| 2999 | } |
| 3000 | } finally { |
| 3001 | cursor.close(); |
| 3002 | } |
| 3003 | } |
| 3004 | |
Andreas Gampe | b5889307 | 2018-09-05 16:52:31 -0700 | [diff] [blame] | 3005 | @GuardedBy("secureSettings.mLock") |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3006 | private void ensureSecureSettingAndroidIdSetLocked(SettingsState secureSettings) { |
| 3007 | Setting value = secureSettings.getSettingLocked(Settings.Secure.ANDROID_ID); |
| 3008 | |
Seigo Nonaka | 6e5b602 | 2016-04-27 16:32:44 +0900 | [diff] [blame] | 3009 | if (!value.isNull()) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3010 | return; |
| 3011 | } |
| 3012 | |
| 3013 | final int userId = getUserIdFromKey(secureSettings.mKey); |
| 3014 | |
| 3015 | final UserInfo user; |
| 3016 | final long identity = Binder.clearCallingIdentity(); |
| 3017 | try { |
| 3018 | user = mUserManager.getUserInfo(userId); |
| 3019 | } finally { |
| 3020 | Binder.restoreCallingIdentity(identity); |
| 3021 | } |
| 3022 | if (user == null) { |
| 3023 | // Can happen due to races when deleting users - treat as benign. |
| 3024 | return; |
| 3025 | } |
| 3026 | |
| 3027 | String androidId = Long.toHexString(new SecureRandom().nextLong()); |
| 3028 | secureSettings.insertSettingLocked(Settings.Secure.ANDROID_ID, androidId, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3029 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3030 | |
| 3031 | Slog.d(LOG_TAG, "Generated and saved new ANDROID_ID [" + androidId |
| 3032 | + "] for user " + userId); |
| 3033 | |
| 3034 | // Write a drop box entry if it's a restricted profile |
| 3035 | if (user.isRestricted()) { |
| 3036 | DropBoxManager dbm = (DropBoxManager) getContext().getSystemService( |
| 3037 | Context.DROPBOX_SERVICE); |
| 3038 | if (dbm != null && dbm.isTagEnabled(DROPBOX_TAG_USERLOG)) { |
| 3039 | dbm.addText(DROPBOX_TAG_USERLOG, System.currentTimeMillis() |
| 3040 | + "," + DROPBOX_TAG_USERLOG + "," + androidId + "\n"); |
| 3041 | } |
| 3042 | } |
| 3043 | } |
| 3044 | |
| 3045 | private void notifyForSettingsChange(int key, String name) { |
Svet Ganov | 945864c | 2018-03-22 21:49:10 -0700 | [diff] [blame] | 3046 | // Increment the generation first, so observers always see the new value |
Phil Weaver | 83fec00 | 2016-05-11 10:55:29 -0700 | [diff] [blame] | 3047 | mGenerationRegistry.incrementGeneration(key); |
| 3048 | |
Linus Tufvesson | 93c3855 | 2019-05-30 12:47:39 +0100 | [diff] [blame] | 3049 | if (isGlobalSettingsKey(key) || isConfigSettingsKey(key)) { |
Jeff Sharkey | 308093f | 2018-03-25 22:53:29 -0600 | [diff] [blame] | 3050 | final long token = Binder.clearCallingIdentity(); |
| 3051 | try { |
Linus Tufvesson | 93c3855 | 2019-05-30 12:47:39 +0100 | [diff] [blame] | 3052 | if (Global.LOCATION_GLOBAL_KILL_SWITCH.equals(name) |
| 3053 | && isGlobalSettingsKey(key)) { |
Jeff Sharkey | 308093f | 2018-03-25 22:53:29 -0600 | [diff] [blame] | 3054 | // When the global kill switch is updated, send the |
| 3055 | // change notification for the location setting. |
| 3056 | notifyLocationChangeForRunningUsers(); |
| 3057 | } |
Linus Tufvesson | 93c3855 | 2019-05-30 12:47:39 +0100 | [diff] [blame] | 3058 | notifySettingChangeForRunningUsers(key, name); |
Jeff Sharkey | 308093f | 2018-03-25 22:53:29 -0600 | [diff] [blame] | 3059 | } finally { |
| 3060 | Binder.restoreCallingIdentity(token); |
Svet Ganov | 945864c | 2018-03-22 21:49:10 -0700 | [diff] [blame] | 3061 | } |
Svet Ganov | 945864c | 2018-03-22 21:49:10 -0700 | [diff] [blame] | 3062 | } else { |
| 3063 | final int userId = getUserIdFromKey(key); |
| 3064 | final Uri uri = getNotificationUriFor(key, name); |
| 3065 | mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED, |
| 3066 | userId, 0, uri).sendToTarget(); |
| 3067 | if (isSecureSettingsKey(key)) { |
| 3068 | maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name, |
| 3069 | sSecureCloneToManagedSettings); |
| 3070 | maybeNotifyProfiles(SETTINGS_TYPE_SYSTEM, userId, uri, name, |
| 3071 | sSystemCloneFromParentOnDependency.values()); |
| 3072 | } else if (isSystemSettingsKey(key)) { |
| 3073 | maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name, |
| 3074 | sSystemCloneToManagedSettings); |
| 3075 | } |
Nicolas Prevot | 310e1ee | 2015-07-02 14:03:06 +0100 | [diff] [blame] | 3076 | } |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 3077 | |
Svet Ganov | 945864c | 2018-03-22 21:49:10 -0700 | [diff] [blame] | 3078 | // Always notify that our data changed |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 3079 | mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget(); |
Nicolas Prevot | 310e1ee | 2015-07-02 14:03:06 +0100 | [diff] [blame] | 3080 | } |
| 3081 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 3082 | private void maybeNotifyProfiles(int type, int userId, Uri uri, String name, |
Robin Lee | 7af9a74 | 2017-02-20 14:47:30 +0000 | [diff] [blame] | 3083 | Collection<String> keysCloned) { |
Nicolas Prevot | 310e1ee | 2015-07-02 14:03:06 +0100 | [diff] [blame] | 3084 | if (keysCloned.contains(name)) { |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 3085 | for (int profileId : mUserManager.getProfileIdsWithDisabled(userId)) { |
Nicolas Prevot | 310e1ee | 2015-07-02 14:03:06 +0100 | [diff] [blame] | 3086 | // the notification for userId has already been sent. |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 3087 | if (profileId != userId) { |
Svet Ganov | 945864c | 2018-03-22 21:49:10 -0700 | [diff] [blame] | 3088 | final int key = makeKey(type, profileId); |
| 3089 | // Increment the generation first, so observers always see the new value |
| 3090 | mGenerationRegistry.incrementGeneration(key); |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 3091 | mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED, |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 3092 | profileId, 0, uri).sendToTarget(); |
Nicolas Prevot | 310e1ee | 2015-07-02 14:03:06 +0100 | [diff] [blame] | 3093 | } |
| 3094 | } |
| 3095 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3096 | } |
| 3097 | |
Linus Tufvesson | 93c3855 | 2019-05-30 12:47:39 +0100 | [diff] [blame] | 3098 | private void notifySettingChangeForRunningUsers(int key, String name) { |
Svet Ganov | 945864c | 2018-03-22 21:49:10 -0700 | [diff] [blame] | 3099 | // Important: No need to update generation for each user as there |
| 3100 | // is a singleton generation entry for the global settings which |
| 3101 | // is already incremented be the caller. |
| 3102 | final Uri uri = getNotificationUriFor(key, name); |
| 3103 | final List<UserInfo> users = mUserManager.getUsers(/*excludeDying*/ true); |
| 3104 | for (int i = 0; i < users.size(); i++) { |
| 3105 | final int userId = users.get(i).id; |
| 3106 | if (mUserManager.isUserRunning(UserHandle.of(userId))) { |
| 3107 | mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED, |
| 3108 | userId, 0, uri).sendToTarget(); |
| 3109 | } |
| 3110 | } |
| 3111 | } |
| 3112 | |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 3113 | private void notifyLocationChangeForRunningUsers() { |
| 3114 | final List<UserInfo> users = mUserManager.getUsers(/*excludeDying=*/ true); |
| 3115 | |
| 3116 | for (int i = 0; i < users.size(); i++) { |
| 3117 | final int userId = users.get(i).id; |
| 3118 | |
| 3119 | if (!mUserManager.isUserRunning(UserHandle.of(userId))) { |
| 3120 | continue; |
| 3121 | } |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 3122 | |
Svet Ganov | 945864c | 2018-03-22 21:49:10 -0700 | [diff] [blame] | 3123 | // Increment the generation first, so observers always see the new value |
| 3124 | final int key = makeKey(SETTINGS_TYPE_SECURE, userId); |
| 3125 | mGenerationRegistry.incrementGeneration(key); |
| 3126 | |
Kweku Adams | 5e0052b | 2019-02-22 15:17:52 -0800 | [diff] [blame] | 3127 | final Uri uri = getNotificationUriFor(key, Secure.LOCATION_MODE); |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 3128 | mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED, |
| 3129 | userId, 0, uri).sendToTarget(); |
| 3130 | } |
| 3131 | } |
| 3132 | |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 3133 | private boolean isConfigSettingsKey(int key) { |
| 3134 | return getTypeFromKey(key) == SETTINGS_TYPE_CONFIG; |
| 3135 | } |
| 3136 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3137 | private boolean isGlobalSettingsKey(int key) { |
| 3138 | return getTypeFromKey(key) == SETTINGS_TYPE_GLOBAL; |
| 3139 | } |
| 3140 | |
| 3141 | private boolean isSystemSettingsKey(int key) { |
| 3142 | return getTypeFromKey(key) == SETTINGS_TYPE_SYSTEM; |
| 3143 | } |
| 3144 | |
| 3145 | private boolean isSecureSettingsKey(int key) { |
| 3146 | return getTypeFromKey(key) == SETTINGS_TYPE_SECURE; |
| 3147 | } |
| 3148 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3149 | private boolean isSsaidSettingsKey(int key) { |
| 3150 | return getTypeFromKey(key) == SETTINGS_TYPE_SSAID; |
| 3151 | } |
| 3152 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3153 | private File getSettingsFile(int key) { |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 3154 | if (isConfigSettingsKey(key)) { |
| 3155 | final int userId = getUserIdFromKey(key); |
| 3156 | return new File(Environment.getUserSystemDirectory(userId), |
| 3157 | SETTINGS_FILE_CONFIG); |
| 3158 | } else if (isGlobalSettingsKey(key)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3159 | final int userId = getUserIdFromKey(key); |
| 3160 | return new File(Environment.getUserSystemDirectory(userId), |
| 3161 | SETTINGS_FILE_GLOBAL); |
| 3162 | } else if (isSystemSettingsKey(key)) { |
| 3163 | final int userId = getUserIdFromKey(key); |
| 3164 | return new File(Environment.getUserSystemDirectory(userId), |
| 3165 | SETTINGS_FILE_SYSTEM); |
| 3166 | } else if (isSecureSettingsKey(key)) { |
| 3167 | final int userId = getUserIdFromKey(key); |
| 3168 | return new File(Environment.getUserSystemDirectory(userId), |
| 3169 | SETTINGS_FILE_SECURE); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3170 | } else if (isSsaidSettingsKey(key)) { |
| 3171 | final int userId = getUserIdFromKey(key); |
| 3172 | return new File(Environment.getUserSystemDirectory(userId), |
| 3173 | SETTINGS_FILE_SSAID); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3174 | } else { |
| 3175 | throw new IllegalArgumentException("Invalid settings key:" + key); |
| 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | private Uri getNotificationUriFor(int key, String name) { |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 3180 | if (isConfigSettingsKey(key)) { |
Matt Pape | 1278d1c | 2018-12-11 13:03:49 -0800 | [diff] [blame] | 3181 | return (name != null) ? Uri.withAppendedPath(DeviceConfig.CONTENT_URI, name) |
| 3182 | : DeviceConfig.CONTENT_URI; |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 3183 | } else if (isGlobalSettingsKey(key)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3184 | return (name != null) ? Uri.withAppendedPath(Settings.Global.CONTENT_URI, name) |
| 3185 | : Settings.Global.CONTENT_URI; |
| 3186 | } else if (isSecureSettingsKey(key)) { |
| 3187 | return (name != null) ? Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name) |
| 3188 | : Settings.Secure.CONTENT_URI; |
| 3189 | } else if (isSystemSettingsKey(key)) { |
| 3190 | return (name != null) ? Uri.withAppendedPath(Settings.System.CONTENT_URI, name) |
| 3191 | : Settings.System.CONTENT_URI; |
| 3192 | } else { |
| 3193 | throw new IllegalArgumentException("Invalid settings key:" + key); |
| 3194 | } |
| 3195 | } |
| 3196 | |
| 3197 | private int getMaxBytesPerPackageForType(int type) { |
| 3198 | switch (type) { |
Matt Pape | 1b31a33 | 2018-10-17 09:58:28 -0700 | [diff] [blame] | 3199 | case SETTINGS_TYPE_CONFIG: |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3200 | case SETTINGS_TYPE_GLOBAL: |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3201 | case SETTINGS_TYPE_SECURE: |
| 3202 | case SETTINGS_TYPE_SSAID: { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3203 | return SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED; |
| 3204 | } |
| 3205 | |
| 3206 | default: { |
| 3207 | return SettingsState.MAX_BYTES_PER_APP_PACKAGE_LIMITED; |
| 3208 | } |
| 3209 | } |
| 3210 | } |
| 3211 | |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 3212 | private final class MyHandler extends Handler { |
| 3213 | private static final int MSG_NOTIFY_URI_CHANGED = 1; |
| 3214 | private static final int MSG_NOTIFY_DATA_CHANGED = 2; |
| 3215 | |
| 3216 | public MyHandler(Looper looper) { |
| 3217 | super(looper); |
| 3218 | } |
| 3219 | |
| 3220 | @Override |
| 3221 | public void handleMessage(Message msg) { |
| 3222 | switch (msg.what) { |
| 3223 | case MSG_NOTIFY_URI_CHANGED: { |
| 3224 | final int userId = msg.arg1; |
| 3225 | Uri uri = (Uri) msg.obj; |
Jeff Sharkey | 88f9d0b | 2017-08-11 15:29:40 -0600 | [diff] [blame] | 3226 | try { |
| 3227 | getContext().getContentResolver().notifyChange(uri, null, true, userId); |
| 3228 | } catch (SecurityException e) { |
| 3229 | Slog.w(LOG_TAG, "Failed to notify for " + userId + ": " + uri, e); |
| 3230 | } |
Makoto Onuki | 0000d32 | 2017-11-28 16:31:47 -0800 | [diff] [blame] | 3231 | if (DEBUG || true) { |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 3232 | Slog.v(LOG_TAG, "Notifying for " + userId + ": " + uri); |
| 3233 | } |
| 3234 | } break; |
| 3235 | |
| 3236 | case MSG_NOTIFY_DATA_CHANGED: { |
| 3237 | mBackupManager.dataChanged(); |
| 3238 | } break; |
| 3239 | } |
| 3240 | } |
| 3241 | } |
| 3242 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3243 | private final class UpgradeController { |
Eric Laurent | 02153e6 | 2019-06-12 17:20:03 -0700 | [diff] [blame] | 3244 | private static final int SETTINGS_VERSION = 182; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3245 | |
| 3246 | private final int mUserId; |
| 3247 | |
| 3248 | public UpgradeController(int userId) { |
| 3249 | mUserId = userId; |
| 3250 | } |
| 3251 | |
| 3252 | public void upgradeIfNeededLocked() { |
| 3253 | // The version of all settings for a user is the same (all users have secure). |
| 3254 | SettingsState secureSettings = getSettingsLocked( |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 3255 | SETTINGS_TYPE_SECURE, mUserId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3256 | |
| 3257 | // Try an update from the current state. |
| 3258 | final int oldVersion = secureSettings.getVersionLocked(); |
| 3259 | final int newVersion = SETTINGS_VERSION; |
| 3260 | |
Svet Ganov | c9755bc | 2015-03-28 13:21:22 -0700 | [diff] [blame] | 3261 | // If up do date - done. |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3262 | if (oldVersion == newVersion) { |
| 3263 | return; |
| 3264 | } |
| 3265 | |
| 3266 | // Try to upgrade. |
| 3267 | final int curVersion = onUpgradeLocked(mUserId, oldVersion, newVersion); |
| 3268 | |
| 3269 | // If upgrade failed start from scratch and upgrade. |
| 3270 | if (curVersion != newVersion) { |
| 3271 | // Drop state we have for this user. |
| 3272 | removeUserStateLocked(mUserId, true); |
| 3273 | |
| 3274 | // Recreate the database. |
| 3275 | DatabaseHelper dbHelper = new DatabaseHelper(getContext(), mUserId); |
| 3276 | SQLiteDatabase database = dbHelper.getWritableDatabase(); |
| 3277 | dbHelper.recreateDatabase(database, newVersion, curVersion, oldVersion); |
| 3278 | |
| 3279 | // Migrate the settings for this user. |
| 3280 | migrateLegacySettingsForUserLocked(dbHelper, database, mUserId); |
| 3281 | |
| 3282 | // Now upgrade should work fine. |
| 3283 | onUpgradeLocked(mUserId, oldVersion, newVersion); |
Svetoslav Ganov | 264c7a9 | 2016-08-24 17:31:14 -0700 | [diff] [blame] | 3284 | |
| 3285 | // Make a note what happened, so we don't wonder why data was lost |
| 3286 | String reason = "Settings rebuilt! Current version: " |
| 3287 | + curVersion + " while expected: " + newVersion; |
| 3288 | getGlobalSettingsLocked().insertSettingLocked( |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3289 | Settings.Global.DATABASE_DOWNGRADE_REASON, |
| 3290 | reason, null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3291 | } |
| 3292 | |
| 3293 | // Set the global settings version if owner. |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 3294 | if (mUserId == UserHandle.USER_SYSTEM) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3295 | SettingsState globalSettings = getSettingsLocked( |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 3296 | SETTINGS_TYPE_GLOBAL, mUserId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3297 | globalSettings.setVersionLocked(newVersion); |
| 3298 | } |
| 3299 | |
| 3300 | // Set the secure settings version. |
| 3301 | secureSettings.setVersionLocked(newVersion); |
| 3302 | |
| 3303 | // Set the system settings version. |
| 3304 | SettingsState systemSettings = getSettingsLocked( |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 3305 | SETTINGS_TYPE_SYSTEM, mUserId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3306 | systemSettings.setVersionLocked(newVersion); |
| 3307 | } |
| 3308 | |
| 3309 | private SettingsState getGlobalSettingsLocked() { |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 3310 | return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3311 | } |
| 3312 | |
| 3313 | private SettingsState getSecureSettingsLocked(int userId) { |
| 3314 | return getSettingsLocked(SETTINGS_TYPE_SECURE, userId); |
| 3315 | } |
| 3316 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3317 | private SettingsState getSsaidSettingsLocked(int userId) { |
| 3318 | return getSettingsLocked(SETTINGS_TYPE_SSAID, userId); |
| 3319 | } |
| 3320 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3321 | private SettingsState getSystemSettingsLocked(int userId) { |
| 3322 | return getSettingsLocked(SETTINGS_TYPE_SYSTEM, userId); |
| 3323 | } |
| 3324 | |
Jeff Brown | 503cffc | 2015-03-26 18:08:51 -0700 | [diff] [blame] | 3325 | /** |
| 3326 | * You must perform all necessary mutations to bring the settings |
| 3327 | * for this user from the old to the new version. When you add a new |
| 3328 | * upgrade step you *must* update SETTINGS_VERSION. |
| 3329 | * |
| 3330 | * This is an example of moving a setting from secure to global. |
| 3331 | * |
| 3332 | * // v119: Example settings changes. |
| 3333 | * if (currentVersion == 118) { |
| 3334 | * if (userId == UserHandle.USER_OWNER) { |
| 3335 | * // Remove from the secure settings. |
| 3336 | * SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3337 | * String name = "example_setting_to_move"; |
| 3338 | * String value = secureSettings.getSetting(name); |
| 3339 | * secureSettings.deleteSetting(name); |
| 3340 | * |
| 3341 | * // Add to the global settings. |
| 3342 | * SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3343 | * globalSettings.insertSetting(name, value, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3344 | * } |
| 3345 | * |
| 3346 | * // Update the current version. |
| 3347 | * currentVersion = 119; |
| 3348 | * } |
| 3349 | */ |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3350 | private int onUpgradeLocked(int userId, int oldVersion, int newVersion) { |
| 3351 | if (DEBUG) { |
| 3352 | Slog.w(LOG_TAG, "Upgrading settings for user: " + userId + " from version: " |
| 3353 | + oldVersion + " to version: " + newVersion); |
| 3354 | } |
| 3355 | |
Jeff Brown | 503cffc | 2015-03-26 18:08:51 -0700 | [diff] [blame] | 3356 | int currentVersion = oldVersion; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3357 | |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 3358 | // v119: Reset zen + ringer mode. |
| 3359 | if (currentVersion == 118) { |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 3360 | if (userId == UserHandle.USER_SYSTEM) { |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 3361 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3362 | globalSettings.updateSettingLocked(Settings.Global.ZEN_MODE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3363 | Integer.toString(Settings.Global.ZEN_MODE_OFF), null, |
| 3364 | true, SettingsState.SYSTEM_PACKAGE_NAME); |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 3365 | globalSettings.updateSettingLocked(Settings.Global.MODE_RINGER, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3366 | Integer.toString(AudioManager.RINGER_MODE_NORMAL), null, |
| 3367 | true, SettingsState.SYSTEM_PACKAGE_NAME); |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 3368 | } |
| 3369 | currentVersion = 119; |
| 3370 | } |
| 3371 | |
Jason Monk | 27bbb2d | 2015-03-31 16:46:39 -0400 | [diff] [blame] | 3372 | // v120: Add double tap to wake setting. |
| 3373 | if (currentVersion == 119) { |
| 3374 | SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3375 | secureSettings.insertSettingLocked(Settings.Secure.DOUBLE_TAP_TO_WAKE, |
| 3376 | getContext().getResources().getBoolean( |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3377 | R.bool.def_double_tap_to_wake) ? "1" : "0", null, true, |
Jason Monk | 27bbb2d | 2015-03-31 16:46:39 -0400 | [diff] [blame] | 3378 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 3379 | |
| 3380 | currentVersion = 120; |
| 3381 | } |
| 3382 | |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 3383 | if (currentVersion == 120) { |
| 3384 | // Before 121, we used a different string encoding logic. We just bump the |
| 3385 | // version here; SettingsState knows how to handle pre-version 120 files. |
| 3386 | currentVersion = 121; |
| 3387 | } |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 3388 | |
Martijn Coenen | 7ab4b7f | 2015-07-27 15:58:32 +0200 | [diff] [blame] | 3389 | if (currentVersion == 121) { |
| 3390 | // Version 122: allow OEMs to set a default payment component in resources. |
| 3391 | // Note that we only write the default if no default has been set; |
| 3392 | // if there is, we just leave the default at whatever it currently is. |
| 3393 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3394 | String defaultComponent = (getContext().getResources().getString( |
| 3395 | R.string.def_nfc_payment_component)); |
| 3396 | Setting currentSetting = secureSettings.getSettingLocked( |
| 3397 | Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT); |
| 3398 | if (defaultComponent != null && !defaultComponent.isEmpty() && |
Svetoslav Ganov | fedb230 | 2016-04-26 18:36:42 -0700 | [diff] [blame] | 3399 | currentSetting.isNull()) { |
Martijn Coenen | 7ab4b7f | 2015-07-27 15:58:32 +0200 | [diff] [blame] | 3400 | secureSettings.insertSettingLocked( |
| 3401 | Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3402 | defaultComponent, null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Martijn Coenen | 7ab4b7f | 2015-07-27 15:58:32 +0200 | [diff] [blame] | 3403 | } |
| 3404 | currentVersion = 122; |
| 3405 | } |
Suprabh Shukla | 269c11e | 2015-12-02 16:51:16 -0800 | [diff] [blame] | 3406 | |
| 3407 | if (currentVersion == 122) { |
| 3408 | // Version 123: Adding a default value for the ability to add a user from |
| 3409 | // the lock screen. |
| 3410 | if (userId == UserHandle.USER_SYSTEM) { |
| 3411 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3412 | Setting currentSetting = globalSettings.getSettingLocked( |
| 3413 | Settings.Global.ADD_USERS_WHEN_LOCKED); |
Svetoslav Ganov | fedb230 | 2016-04-26 18:36:42 -0700 | [diff] [blame] | 3414 | if (currentSetting.isNull()) { |
Suprabh Shukla | 269c11e | 2015-12-02 16:51:16 -0800 | [diff] [blame] | 3415 | globalSettings.insertSettingLocked( |
| 3416 | Settings.Global.ADD_USERS_WHEN_LOCKED, |
| 3417 | getContext().getResources().getBoolean( |
| 3418 | R.bool.def_add_users_from_lockscreen) ? "1" : "0", |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3419 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Suprabh Shukla | 269c11e | 2015-12-02 16:51:16 -0800 | [diff] [blame] | 3420 | } |
| 3421 | } |
| 3422 | currentVersion = 123; |
| 3423 | } |
Bryce Lee | bd17928 | 2015-12-17 19:01:37 -0800 | [diff] [blame] | 3424 | |
| 3425 | if (currentVersion == 123) { |
Bryce Lee | ec85f34 | 2015-12-16 13:32:28 -0800 | [diff] [blame] | 3426 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3427 | String defaultDisabledProfiles = (getContext().getResources().getString( |
| 3428 | R.string.def_bluetooth_disabled_profiles)); |
| 3429 | globalSettings.insertSettingLocked(Settings.Global.BLUETOOTH_DISABLED_PROFILES, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3430 | defaultDisabledProfiles, null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Bryce Lee | bd17928 | 2015-12-17 19:01:37 -0800 | [diff] [blame] | 3431 | currentVersion = 124; |
Bryce Lee | ec85f34 | 2015-12-16 13:32:28 -0800 | [diff] [blame] | 3432 | } |
| 3433 | |
Prathmesh Prabhu | de16b86 | 2016-03-04 15:22:24 -0800 | [diff] [blame] | 3434 | if (currentVersion == 124) { |
| 3435 | // Version 124: allow OEMs to set a default value for whether IME should be |
| 3436 | // shown when a physical keyboard is connected. |
| 3437 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3438 | Setting currentSetting = secureSettings.getSettingLocked( |
| 3439 | Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD); |
Svetoslav Ganov | fedb230 | 2016-04-26 18:36:42 -0700 | [diff] [blame] | 3440 | if (currentSetting.isNull()) { |
Prathmesh Prabhu | de16b86 | 2016-03-04 15:22:24 -0800 | [diff] [blame] | 3441 | secureSettings.insertSettingLocked( |
| 3442 | Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD, |
| 3443 | getContext().getResources().getBoolean( |
| 3444 | R.bool.def_show_ime_with_hard_keyboard) ? "1" : "0", |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3445 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Prathmesh Prabhu | de16b86 | 2016-03-04 15:22:24 -0800 | [diff] [blame] | 3446 | } |
| 3447 | currentVersion = 125; |
| 3448 | } |
| 3449 | |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 3450 | if (currentVersion == 125) { |
| 3451 | // Version 125: Allow OEMs to set the default VR service. |
| 3452 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3453 | |
| 3454 | Setting currentSetting = secureSettings.getSettingLocked( |
| 3455 | Settings.Secure.ENABLED_VR_LISTENERS); |
Svetoslav Ganov | fedb230 | 2016-04-26 18:36:42 -0700 | [diff] [blame] | 3456 | if (currentSetting.isNull()) { |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 3457 | ArraySet<ComponentName> l = |
| 3458 | SystemConfig.getInstance().getDefaultVrComponents(); |
| 3459 | |
| 3460 | if (l != null && !l.isEmpty()) { |
| 3461 | StringBuilder b = new StringBuilder(); |
| 3462 | boolean start = true; |
| 3463 | for (ComponentName c : l) { |
| 3464 | if (!start) { |
| 3465 | b.append(':'); |
| 3466 | } |
| 3467 | b.append(c.flattenToString()); |
| 3468 | start = false; |
| 3469 | } |
| 3470 | secureSettings.insertSettingLocked( |
| 3471 | Settings.Secure.ENABLED_VR_LISTENERS, b.toString(), |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3472 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 3473 | } |
| 3474 | |
| 3475 | } |
| 3476 | currentVersion = 126; |
| 3477 | } |
| 3478 | |
Daniel U | 02ba612 | 2016-04-01 18:41:42 +0100 | [diff] [blame] | 3479 | if (currentVersion == 126) { |
| 3480 | // Version 126: copy the primary values of LOCK_SCREEN_SHOW_NOTIFICATIONS and |
| 3481 | // LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS into managed profile. |
| 3482 | if (mUserManager.isManagedProfile(userId)) { |
| 3483 | final SettingsState systemSecureSettings = |
| 3484 | getSecureSettingsLocked(UserHandle.USER_SYSTEM); |
| 3485 | |
| 3486 | final Setting showNotifications = systemSecureSettings.getSettingLocked( |
| 3487 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS); |
Seigo Nonaka | 6e5b602 | 2016-04-27 16:32:44 +0900 | [diff] [blame] | 3488 | if (!showNotifications.isNull()) { |
Daniel U | 02ba612 | 2016-04-01 18:41:42 +0100 | [diff] [blame] | 3489 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3490 | secureSettings.insertSettingLocked( |
| 3491 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3492 | showNotifications.getValue(), null, true, |
Daniel U | 02ba612 | 2016-04-01 18:41:42 +0100 | [diff] [blame] | 3493 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 3494 | } |
| 3495 | |
| 3496 | final Setting allowPrivate = systemSecureSettings.getSettingLocked( |
| 3497 | Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS); |
Seigo Nonaka | 6e5b602 | 2016-04-27 16:32:44 +0900 | [diff] [blame] | 3498 | if (!allowPrivate.isNull()) { |
Daniel U | 02ba612 | 2016-04-01 18:41:42 +0100 | [diff] [blame] | 3499 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3500 | secureSettings.insertSettingLocked( |
| 3501 | Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3502 | allowPrivate.getValue(), null, true, |
Daniel U | 02ba612 | 2016-04-01 18:41:42 +0100 | [diff] [blame] | 3503 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 3504 | } |
| 3505 | } |
| 3506 | currentVersion = 127; |
| 3507 | } |
| 3508 | |
Steven Ng | dc20ba6 | 2016-04-26 18:19:04 +0100 | [diff] [blame] | 3509 | if (currentVersion == 127) { |
Mahaver Chopra | 3d9805d | 2016-07-07 16:25:05 +0100 | [diff] [blame] | 3510 | // version 127 is no longer used. |
Steven Ng | dc20ba6 | 2016-04-26 18:19:04 +0100 | [diff] [blame] | 3511 | currentVersion = 128; |
| 3512 | } |
| 3513 | |
Julia Reynolds | 1f721e1 | 2016-07-11 08:50:58 -0400 | [diff] [blame] | 3514 | if (currentVersion == 128) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3515 | // Version 128: Removed |
Julia Reynolds | 1f721e1 | 2016-07-11 08:50:58 -0400 | [diff] [blame] | 3516 | currentVersion = 129; |
| 3517 | } |
| 3518 | |
Dan Sandler | 71f85e9 | 2016-07-20 13:46:05 -0400 | [diff] [blame] | 3519 | if (currentVersion == 129) { |
| 3520 | // default longpress timeout changed from 500 to 400. If unchanged from the old |
| 3521 | // default, update to the new default. |
| 3522 | final SettingsState systemSecureSettings = |
| 3523 | getSecureSettingsLocked(userId); |
| 3524 | final String oldValue = systemSecureSettings.getSettingLocked( |
| 3525 | Settings.Secure.LONG_PRESS_TIMEOUT).getValue(); |
| 3526 | if (TextUtils.equals("500", oldValue)) { |
| 3527 | systemSecureSettings.insertSettingLocked( |
| 3528 | Settings.Secure.LONG_PRESS_TIMEOUT, |
| 3529 | String.valueOf(getContext().getResources().getInteger( |
| 3530 | R.integer.def_long_press_timeout_millis)), |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3531 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Dan Sandler | 71f85e9 | 2016-07-20 13:46:05 -0400 | [diff] [blame] | 3532 | } |
| 3533 | currentVersion = 130; |
| 3534 | } |
| 3535 | |
Anthony Hugh | 96e9cc5 | 2016-07-12 15:17:24 -0700 | [diff] [blame] | 3536 | if (currentVersion == 130) { |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3537 | // Split Ambient settings |
| 3538 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3539 | boolean dozeExplicitlyDisabled = "0".equals(secureSettings. |
| 3540 | getSettingLocked(Settings.Secure.DOZE_ENABLED).getValue()); |
| 3541 | |
| 3542 | if (dozeExplicitlyDisabled) { |
Lucas Dupin | 4359b55 | 2018-08-09 15:07:54 -0700 | [diff] [blame] | 3543 | secureSettings.insertSettingLocked(Settings.Secure.DOZE_PICK_UP_GESTURE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3544 | "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Lucas Dupin | 4359b55 | 2018-08-09 15:07:54 -0700 | [diff] [blame] | 3545 | secureSettings.insertSettingLocked(Settings.Secure.DOZE_DOUBLE_TAP_GESTURE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3546 | "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3547 | } |
| 3548 | currentVersion = 131; |
| 3549 | } |
| 3550 | |
| 3551 | if (currentVersion == 131) { |
Anthony Hugh | 96e9cc5 | 2016-07-12 15:17:24 -0700 | [diff] [blame] | 3552 | // Initialize new multi-press timeout to default value |
| 3553 | final SettingsState systemSecureSettings = getSecureSettingsLocked(userId); |
| 3554 | final String oldValue = systemSecureSettings.getSettingLocked( |
| 3555 | Settings.Secure.MULTI_PRESS_TIMEOUT).getValue(); |
| 3556 | if (TextUtils.equals(null, oldValue)) { |
| 3557 | systemSecureSettings.insertSettingLocked( |
| 3558 | Settings.Secure.MULTI_PRESS_TIMEOUT, |
| 3559 | String.valueOf(getContext().getResources().getInteger( |
| 3560 | R.integer.def_multi_press_timeout_millis)), |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3561 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Anthony Hugh | 96e9cc5 | 2016-07-12 15:17:24 -0700 | [diff] [blame] | 3562 | } |
| 3563 | |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3564 | currentVersion = 132; |
Anthony Hugh | 96e9cc5 | 2016-07-12 15:17:24 -0700 | [diff] [blame] | 3565 | } |
| 3566 | |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3567 | if (currentVersion == 132) { |
| 3568 | // Version 132: Allow managed profile to optionally use the parent's ringtones |
Andre Lago | ea35e07 | 2016-08-04 13:41:13 +0100 | [diff] [blame] | 3569 | final SettingsState systemSecureSettings = getSecureSettingsLocked(userId); |
| 3570 | String defaultSyncParentSounds = (getContext().getResources() |
| 3571 | .getBoolean(R.bool.def_sync_parent_sounds) ? "1" : "0"); |
| 3572 | systemSecureSettings.insertSettingLocked( |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3573 | Settings.Secure.SYNC_PARENT_SOUNDS, defaultSyncParentSounds, |
| 3574 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3575 | currentVersion = 133; |
Andre Lago | ea35e07 | 2016-08-04 13:41:13 +0100 | [diff] [blame] | 3576 | } |
| 3577 | |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3578 | if (currentVersion == 133) { |
| 3579 | // Version 133: Add default end button behavior |
Keun-young Park | ec7a118 | 2016-10-18 11:52:38 -0700 | [diff] [blame] | 3580 | final SettingsState systemSettings = getSystemSettingsLocked(userId); |
davidln | edb31e1 | 2018-09-25 10:21:20 -0700 | [diff] [blame] | 3581 | if (systemSettings.getSettingLocked(Settings.System.END_BUTTON_BEHAVIOR) |
| 3582 | .isNull()) { |
Keun-young Park | ec7a118 | 2016-10-18 11:52:38 -0700 | [diff] [blame] | 3583 | String defaultEndButtonBehavior = Integer.toString(getContext() |
| 3584 | .getResources().getInteger(R.integer.def_end_button_behavior)); |
| 3585 | systemSettings.insertSettingLocked(Settings.System.END_BUTTON_BEHAVIOR, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3586 | defaultEndButtonBehavior, null, true, |
| 3587 | SettingsState.SYSTEM_PACKAGE_NAME); |
Keun-young Park | ec7a118 | 2016-10-18 11:52:38 -0700 | [diff] [blame] | 3588 | } |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3589 | currentVersion = 134; |
Keun-young Park | ec7a118 | 2016-10-18 11:52:38 -0700 | [diff] [blame] | 3590 | } |
| 3591 | |
Phil Weaver | 89e3ffc | 2016-09-19 13:51:10 -0700 | [diff] [blame] | 3592 | if (currentVersion == 134) { |
| 3593 | // Remove setting that specifies if magnification values should be preserved. |
| 3594 | // This setting defaulted to true and never has a UI. |
| 3595 | getSecureSettingsLocked(userId).deleteSettingLocked( |
| 3596 | Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_AUTO_UPDATE); |
| 3597 | currentVersion = 135; |
| 3598 | } |
| 3599 | |
Jeremy Joslin | 8bdad34 | 2016-12-14 11:46:47 -0800 | [diff] [blame] | 3600 | if (currentVersion == 135) { |
Jeremy Joslin | c9eb3c4 | 2017-02-08 10:45:30 -0800 | [diff] [blame] | 3601 | // Version 135 no longer used. |
Jeremy Joslin | 8bdad34 | 2016-12-14 11:46:47 -0800 | [diff] [blame] | 3602 | currentVersion = 136; |
| 3603 | } |
| 3604 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3605 | if (currentVersion == 136) { |
| 3606 | // Version 136: Store legacy SSAID for all apps currently installed on the |
| 3607 | // device as first step in migrating SSAID to be unique per application. |
| 3608 | |
| 3609 | final boolean isUpgrade; |
| 3610 | try { |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 3611 | isUpgrade = mPackageManager.isDeviceUpgrading(); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3612 | } catch (RemoteException e) { |
| 3613 | throw new IllegalStateException("Package manager not available"); |
| 3614 | } |
| 3615 | // Only retain legacy ssaid if the device is performing an OTA. After wiping |
| 3616 | // user data or first boot on a new device should use new ssaid generation. |
| 3617 | if (isUpgrade) { |
| 3618 | // Retrieve the legacy ssaid from the secure settings table. |
Mark Rathjen | ea61759 | 2017-01-18 23:03:41 -0800 | [diff] [blame] | 3619 | final Setting legacySsaidSetting = getSettingLocked(SETTINGS_TYPE_SECURE, |
| 3620 | userId, Settings.Secure.ANDROID_ID); |
| 3621 | if (legacySsaidSetting == null || legacySsaidSetting.isNull() |
| 3622 | || legacySsaidSetting.getValue() == null) { |
| 3623 | throw new IllegalStateException("Legacy ssaid not accessible"); |
| 3624 | } |
| 3625 | final String legacySsaid = legacySsaidSetting.getValue(); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3626 | |
| 3627 | // Fill each uid with the legacy ssaid to be backwards compatible. |
| 3628 | final List<PackageInfo> packages; |
| 3629 | try { |
Tetsutoki Shiozawa | ebe0e5f | 2018-01-17 11:07:09 +0900 | [diff] [blame] | 3630 | packages = mPackageManager.getInstalledPackages( |
| 3631 | PackageManager.MATCH_UNINSTALLED_PACKAGES, |
| 3632 | userId).getList(); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3633 | } catch (RemoteException e) { |
| 3634 | throw new IllegalStateException("Package manager not available"); |
| 3635 | } |
| 3636 | |
| 3637 | final SettingsState ssaidSettings = getSsaidSettingsLocked(userId); |
| 3638 | for (PackageInfo info : packages) { |
| 3639 | // Check if the UID already has an entry in the table. |
| 3640 | final String uid = Integer.toString(info.applicationInfo.uid); |
| 3641 | final Setting ssaid = ssaidSettings.getSettingLocked(uid); |
| 3642 | |
| 3643 | if (ssaid.isNull() || ssaid.getValue() == null) { |
| 3644 | // Android Id doesn't exist for this package so create it. |
| 3645 | ssaidSettings.insertSettingLocked(uid, legacySsaid, null, true, |
| 3646 | info.packageName); |
Tetsutoki Shiozawa | ebe0e5f | 2018-01-17 11:07:09 +0900 | [diff] [blame] | 3647 | if (DEBUG) { |
| 3648 | Slog.d(LOG_TAG, "Keep the legacy ssaid for uid=" + uid); |
| 3649 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3650 | } |
| 3651 | } |
| 3652 | } |
| 3653 | |
| 3654 | currentVersion = 137; |
| 3655 | } |
Suprabh Shukla | e3745ee | 2017-02-02 20:01:11 -0800 | [diff] [blame] | 3656 | if (currentVersion == 137) { |
| 3657 | // Version 138: Settings.Secure#INSTALL_NON_MARKET_APPS is deprecated and its |
| 3658 | // default value set to 1. The user can no longer change the value of this |
| 3659 | // setting through the UI. |
| 3660 | final SettingsState secureSetting = getSecureSettingsLocked(userId); |
| 3661 | if (!mUserManager.hasUserRestriction( |
Suprabh Shukla | 0b1356f | 2017-02-21 14:33:50 -0800 | [diff] [blame] | 3662 | UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, UserHandle.of(userId)) |
| 3663 | && secureSetting.getSettingLocked( |
| 3664 | Settings.Secure.INSTALL_NON_MARKET_APPS).getValue().equals("0")) { |
| 3665 | |
Suprabh Shukla | e3745ee | 2017-02-02 20:01:11 -0800 | [diff] [blame] | 3666 | secureSetting.insertSettingLocked(Settings.Secure.INSTALL_NON_MARKET_APPS, |
| 3667 | "1", null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Suprabh Shukla | 0b1356f | 2017-02-21 14:33:50 -0800 | [diff] [blame] | 3668 | // For managed profiles with profile owners, DevicePolicyManagerService |
| 3669 | // may want to set the user restriction in this case |
| 3670 | secureSetting.insertSettingLocked( |
| 3671 | Settings.Secure.UNKNOWN_SOURCES_DEFAULT_REVERSED, "1", null, true, |
| 3672 | SettingsState.SYSTEM_PACKAGE_NAME); |
Suprabh Shukla | e3745ee | 2017-02-02 20:01:11 -0800 | [diff] [blame] | 3673 | } |
| 3674 | currentVersion = 138; |
| 3675 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3676 | |
Jeremy Joslin | c9eb3c4 | 2017-02-08 10:45:30 -0800 | [diff] [blame] | 3677 | if (currentVersion == 138) { |
Jeremy Joslin | 27d14c4 | 2017-02-15 12:02:03 -0800 | [diff] [blame] | 3678 | // Version 139: Removed. |
Jeremy Joslin | c9eb3c4 | 2017-02-08 10:45:30 -0800 | [diff] [blame] | 3679 | currentVersion = 139; |
| 3680 | } |
| 3681 | |
Phil Weaver | 385912e | 2017-02-10 10:06:56 -0800 | [diff] [blame] | 3682 | if (currentVersion == 139) { |
| 3683 | // Version 140: Settings.Secure#ACCESSIBILITY_SPEAK_PASSWORD is deprecated and |
| 3684 | // the user can no longer change the value of this setting through the UI. |
| 3685 | // Force to true. |
| 3686 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3687 | secureSettings.updateSettingLocked(Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD, |
| 3688 | "1", null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3689 | currentVersion = 140; |
| 3690 | } |
| 3691 | |
Julia Reynolds | ad0d9e0 | 2017-02-15 08:41:48 -0500 | [diff] [blame] | 3692 | if (currentVersion == 140) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3693 | // Version 141: Removed |
Julia Reynolds | ad0d9e0 | 2017-02-15 08:41:48 -0500 | [diff] [blame] | 3694 | currentVersion = 141; |
| 3695 | } |
| 3696 | |
Svet Ganov | 1370155 | 2017-02-23 12:45:17 -0800 | [diff] [blame] | 3697 | if (currentVersion == 141) { |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 3698 | // This implementation was incorrectly setting the current value of |
| 3699 | // settings changed by non-system packages as the default which default |
| 3700 | // is set by the system. We add a new upgrade step at the end to properly |
| 3701 | // handle this case which would also fix incorrect changes made by the |
| 3702 | // old implementation of this step. |
Svet Ganov | 1370155 | 2017-02-23 12:45:17 -0800 | [diff] [blame] | 3703 | currentVersion = 142; |
| 3704 | } |
| 3705 | |
Stephen Chen | 5d0922f | 2017-03-27 10:28:04 -0700 | [diff] [blame] | 3706 | if (currentVersion == 142) { |
Felipe Leme | ff35509 | 2017-04-03 12:55:02 -0700 | [diff] [blame] | 3707 | // Version 143: Set a default value for Wi-Fi wakeup feature. |
Stephen Chen | 5d0922f | 2017-03-27 10:28:04 -0700 | [diff] [blame] | 3708 | if (userId == UserHandle.USER_SYSTEM) { |
| 3709 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3710 | Setting currentSetting = globalSettings.getSettingLocked( |
| 3711 | Settings.Global.WIFI_WAKEUP_ENABLED); |
| 3712 | if (currentSetting.isNull()) { |
| 3713 | globalSettings.insertSettingLocked( |
| 3714 | Settings.Global.WIFI_WAKEUP_ENABLED, |
| 3715 | getContext().getResources().getBoolean( |
| 3716 | R.bool.def_wifi_wakeup_enabled) ? "1" : "0", |
| 3717 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3718 | } |
| 3719 | } |
| 3720 | |
| 3721 | currentVersion = 143; |
| 3722 | } |
| 3723 | |
Felipe Leme | ff35509 | 2017-04-03 12:55:02 -0700 | [diff] [blame] | 3724 | if (currentVersion == 143) { |
| 3725 | // Version 144: Set a default value for Autofill service. |
| 3726 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3727 | final Setting currentSetting = secureSettings |
| 3728 | .getSettingLocked(Settings.Secure.AUTOFILL_SERVICE); |
| 3729 | if (currentSetting.isNull()) { |
| 3730 | final String defaultValue = getContext().getResources().getString( |
| 3731 | com.android.internal.R.string.config_defaultAutofillService); |
| 3732 | if (defaultValue != null) { |
| 3733 | Slog.d(LOG_TAG, "Setting [" + defaultValue + "] as Autofill Service " |
| 3734 | + "for user " + userId); |
| 3735 | secureSettings.insertSettingLocked(Settings.Secure.AUTOFILL_SERVICE, |
| 3736 | defaultValue, null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3737 | } |
| 3738 | } |
| 3739 | |
| 3740 | currentVersion = 144; |
| 3741 | } |
| 3742 | |
Jeremy Joslin | 45caa25 | 2017-05-04 11:22:46 -0700 | [diff] [blame] | 3743 | if (currentVersion == 144) { |
Amin Shaikh | 8636796 | 2017-06-07 08:58:22 -0700 | [diff] [blame] | 3744 | // Version 145: Removed |
| 3745 | currentVersion = 145; |
| 3746 | } |
| 3747 | |
| 3748 | if (currentVersion == 145) { |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 3749 | // Version 146: In step 142 we had a bug where incorrectly |
| 3750 | // some settings were considered system set and as a result |
| 3751 | // made the default and marked as the default being set by |
| 3752 | // the system. Here reevaluate the default and default system |
| 3753 | // set flags. This would both fix corruption by the old impl |
| 3754 | // of step 142 and also properly handle devices which never |
| 3755 | // run 142. |
Jeremy Joslin | 45caa25 | 2017-05-04 11:22:46 -0700 | [diff] [blame] | 3756 | if (userId == UserHandle.USER_SYSTEM) { |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 3757 | SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3758 | ensureLegacyDefaultValueAndSystemSetUpdatedLocked(globalSettings, userId); |
| 3759 | globalSettings.persistSyncLocked(); |
Jeremy Joslin | 45caa25 | 2017-05-04 11:22:46 -0700 | [diff] [blame] | 3760 | } |
| 3761 | |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 3762 | SettingsState secureSettings = getSecureSettingsLocked(mUserId); |
| 3763 | ensureLegacyDefaultValueAndSystemSetUpdatedLocked(secureSettings, userId); |
| 3764 | secureSettings.persistSyncLocked(); |
| 3765 | |
| 3766 | SettingsState systemSettings = getSystemSettingsLocked(mUserId); |
| 3767 | ensureLegacyDefaultValueAndSystemSetUpdatedLocked(systemSettings, userId); |
| 3768 | systemSettings.persistSyncLocked(); |
| 3769 | |
Amin Shaikh | 8636796 | 2017-06-07 08:58:22 -0700 | [diff] [blame] | 3770 | currentVersion = 146; |
Jeremy Joslin | 45caa25 | 2017-05-04 11:22:46 -0700 | [diff] [blame] | 3771 | } |
| 3772 | |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 3773 | if (currentVersion == 146) { |
Joe LaPenna | 250d784 | 2018-01-25 10:19:42 -0800 | [diff] [blame] | 3774 | // Version 147: Removed. (This version previously allowed showing the |
| 3775 | // "wifi_wakeup_available" setting). |
| 3776 | // The setting that was added here is deleted in 153. |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 3777 | currentVersion = 147; |
| 3778 | } |
| 3779 | |
| 3780 | if (currentVersion == 147) { |
| 3781 | // Version 148: Set the default value for DEFAULT_RESTRICT_BACKGROUND_DATA. |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 3782 | if (userId == UserHandle.USER_SYSTEM) { |
| 3783 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3784 | final Setting currentSetting = globalSettings.getSettingLocked( |
| 3785 | Global.DEFAULT_RESTRICT_BACKGROUND_DATA); |
| 3786 | if (currentSetting.isNull()) { |
| 3787 | globalSettings.insertSettingLocked( |
| 3788 | Global.DEFAULT_RESTRICT_BACKGROUND_DATA, |
| 3789 | getContext().getResources().getBoolean( |
| 3790 | R.bool.def_restrict_background_data) ? "1" : "0", |
| 3791 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3792 | } |
| 3793 | } |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 3794 | currentVersion = 148; |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 3795 | } |
| 3796 | |
Tim Zheng | cc1e76a | 2017-08-30 17:46:19 -0700 | [diff] [blame] | 3797 | if (currentVersion == 148) { |
| 3798 | // Version 149: Set the default value for BACKUP_MANAGER_CONSTANTS. |
| 3799 | final SettingsState systemSecureSettings = getSecureSettingsLocked(userId); |
| 3800 | final String oldValue = systemSecureSettings.getSettingLocked( |
| 3801 | Settings.Secure.BACKUP_MANAGER_CONSTANTS).getValue(); |
| 3802 | if (TextUtils.equals(null, oldValue)) { |
| 3803 | final String defaultValue = getContext().getResources().getString( |
| 3804 | R.string.def_backup_manager_constants); |
| 3805 | if (!TextUtils.isEmpty(defaultValue)) { |
| 3806 | systemSecureSettings.insertSettingLocked( |
| 3807 | Settings.Secure.BACKUP_MANAGER_CONSTANTS, defaultValue, null, |
| 3808 | true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3809 | } |
| 3810 | } |
Tim Zheng | cc1e76a | 2017-08-30 17:46:19 -0700 | [diff] [blame] | 3811 | currentVersion = 149; |
| 3812 | } |
Jacky Cheung | 7076a31 | 2017-10-02 10:40:48 -0700 | [diff] [blame] | 3813 | |
| 3814 | if (currentVersion == 149) { |
| 3815 | // Version 150: Set a default value for mobile data always on |
| 3816 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3817 | final Setting currentSetting = globalSettings.getSettingLocked( |
| 3818 | Settings.Global.MOBILE_DATA_ALWAYS_ON); |
| 3819 | if (currentSetting.isNull()) { |
| 3820 | globalSettings.insertSettingLocked( |
| 3821 | Settings.Global.MOBILE_DATA_ALWAYS_ON, |
| 3822 | getContext().getResources().getBoolean( |
| 3823 | R.bool.def_mobile_data_always_on) ? "1" : "0", |
| 3824 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3825 | } |
| 3826 | |
| 3827 | currentVersion = 150; |
| 3828 | } |
| 3829 | |
Mike Digman | 4af4a6f | 2018-01-16 14:49:38 -0800 | [diff] [blame] | 3830 | if (currentVersion == 150) { |
Mike Digman | 32e0331 | 2018-05-16 16:43:23 -0700 | [diff] [blame] | 3831 | // Version 151: Removed. |
Mike Digman | 4af4a6f | 2018-01-16 14:49:38 -0800 | [diff] [blame] | 3832 | currentVersion = 151; |
| 3833 | } |
| 3834 | |
Joe LaPenna | 4f50ed4 | 2018-01-22 14:54:45 -0800 | [diff] [blame] | 3835 | if (currentVersion == 151) { |
Joe LaPenna | 250d784 | 2018-01-25 10:19:42 -0800 | [diff] [blame] | 3836 | // Version 152: Removed. (This version made the setting for wifi_wakeup enabled |
| 3837 | // by default but it is now no longer configurable). |
| 3838 | // The setting updated here is deleted in 153. |
Joe LaPenna | 4f50ed4 | 2018-01-22 14:54:45 -0800 | [diff] [blame] | 3839 | currentVersion = 152; |
| 3840 | } |
| 3841 | |
Joe LaPenna | 250d784 | 2018-01-25 10:19:42 -0800 | [diff] [blame] | 3842 | if (currentVersion == 152) { |
| 3843 | getGlobalSettingsLocked().deleteSettingLocked("wifi_wakeup_available"); |
| 3844 | currentVersion = 153; |
| 3845 | } |
| 3846 | |
Ben Lin | b4df8bc | 2018-01-29 11:48:20 -0800 | [diff] [blame] | 3847 | if (currentVersion == 153) { |
| 3848 | // Version 154: Read notification badge configuration from config. |
| 3849 | // If user has already set the value, don't do anything. |
| 3850 | final SettingsState systemSecureSettings = getSecureSettingsLocked(userId); |
| 3851 | final Setting showNotificationBadges = systemSecureSettings.getSettingLocked( |
| 3852 | Settings.Secure.NOTIFICATION_BADGING); |
| 3853 | if (showNotificationBadges.isNull()) { |
| 3854 | final boolean defaultValue = getContext().getResources().getBoolean( |
| 3855 | com.android.internal.R.bool.config_notificationBadging); |
| 3856 | systemSecureSettings.insertSettingLocked( |
| 3857 | Secure.NOTIFICATION_BADGING, |
| 3858 | defaultValue ? "1" : "0", |
| 3859 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3860 | } |
| 3861 | currentVersion = 154; |
| 3862 | } |
| 3863 | |
Bernardo Rufino | eaa78b9 | 2018-01-26 11:25:37 +0000 | [diff] [blame] | 3864 | if (currentVersion == 154) { |
| 3865 | // Version 155: Set the default value for BACKUP_LOCAL_TRANSPORT_PARAMETERS. |
| 3866 | final SettingsState systemSecureSettings = getSecureSettingsLocked(userId); |
| 3867 | final String oldValue = systemSecureSettings.getSettingLocked( |
| 3868 | Settings.Secure.BACKUP_LOCAL_TRANSPORT_PARAMETERS).getValue(); |
| 3869 | if (TextUtils.equals(null, oldValue)) { |
| 3870 | final String defaultValue = getContext().getResources().getString( |
| 3871 | R.string.def_backup_local_transport_parameters); |
| 3872 | if (!TextUtils.isEmpty(defaultValue)) { |
| 3873 | systemSecureSettings.insertSettingLocked( |
| 3874 | Settings.Secure.BACKUP_LOCAL_TRANSPORT_PARAMETERS, defaultValue, |
| 3875 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3876 | } |
| 3877 | |
| 3878 | } |
| 3879 | currentVersion = 155; |
| 3880 | } |
| 3881 | |
Beverly | da90481 | 2018-03-02 09:55:30 -0500 | [diff] [blame] | 3882 | if (currentVersion == 155) { |
Annie Meng | d069a88 | 2018-03-13 15:31:40 +0000 | [diff] [blame] | 3883 | // Version 156: Set the default value for CHARGING_STARTED_SOUND. |
Beverly | da90481 | 2018-03-02 09:55:30 -0500 | [diff] [blame] | 3884 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3885 | final String oldValue = globalSettings.getSettingLocked( |
| 3886 | Global.CHARGING_STARTED_SOUND).getValue(); |
| 3887 | final String oldDefault = getContext().getResources().getString( |
| 3888 | R.string.def_wireless_charging_started_sound); |
| 3889 | if (TextUtils.equals(null, oldValue) |
| 3890 | || TextUtils.equals(oldValue, oldDefault)) { |
| 3891 | final String defaultValue = getContext().getResources().getString( |
| 3892 | R.string.def_charging_started_sound); |
| 3893 | if (!TextUtils.isEmpty(defaultValue)) { |
| 3894 | globalSettings.insertSettingLocked( |
| 3895 | Settings.Global.CHARGING_STARTED_SOUND, defaultValue, |
| 3896 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3897 | } |
| 3898 | |
| 3899 | } |
| 3900 | currentVersion = 156; |
| 3901 | } |
| 3902 | |
Beverly | 09da25f | 2018-02-26 09:17:07 -0500 | [diff] [blame] | 3903 | if (currentVersion == 156) { |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 3904 | // Version 157: Set a default value for zen duration, |
| 3905 | // in version 169, zen duration is moved to secure settings |
Beverly | 09da25f | 2018-02-26 09:17:07 -0500 | [diff] [blame] | 3906 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3907 | final Setting currentSetting = globalSettings.getSettingLocked( |
| 3908 | Global.ZEN_DURATION); |
| 3909 | if (currentSetting.isNull()) { |
| 3910 | String defaultZenDuration = Integer.toString(getContext() |
| 3911 | .getResources().getInteger(R.integer.def_zen_duration)); |
| 3912 | globalSettings.insertSettingLocked( |
| 3913 | Global.ZEN_DURATION, defaultZenDuration, |
| 3914 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3915 | } |
Beverly | 09da25f | 2018-02-26 09:17:07 -0500 | [diff] [blame] | 3916 | currentVersion = 157; |
| 3917 | } |
Annie Meng | d069a88 | 2018-03-13 15:31:40 +0000 | [diff] [blame] | 3918 | |
| 3919 | if (currentVersion == 157) { |
| 3920 | // Version 158: Set default value for BACKUP_AGENT_TIMEOUT_PARAMETERS. |
| 3921 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3922 | final String oldValue = globalSettings.getSettingLocked( |
| 3923 | Settings.Global.BACKUP_AGENT_TIMEOUT_PARAMETERS).getValue(); |
| 3924 | if (TextUtils.equals(null, oldValue)) { |
| 3925 | final String defaultValue = getContext().getResources().getString( |
| 3926 | R.string.def_backup_agent_timeout_parameters); |
| 3927 | if (!TextUtils.isEmpty(defaultValue)) { |
| 3928 | globalSettings.insertSettingLocked( |
| 3929 | Settings.Global.BACKUP_AGENT_TIMEOUT_PARAMETERS, defaultValue, |
| 3930 | null, true, |
| 3931 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 3932 | } |
| 3933 | } |
| 3934 | currentVersion = 158; |
| 3935 | } |
Roshan Pius | 9c39667 | 2018-03-02 14:54:13 -0800 | [diff] [blame] | 3936 | |
| 3937 | if (currentVersion == 158) { |
| 3938 | // Remove setting that specifies wifi bgscan throttling params |
| 3939 | getGlobalSettingsLocked().deleteSettingLocked( |
| 3940 | "wifi_scan_background_throttle_interval_ms"); |
| 3941 | getGlobalSettingsLocked().deleteSettingLocked( |
| 3942 | "wifi_scan_background_throttle_package_whitelist"); |
| 3943 | currentVersion = 159; |
| 3944 | } |
| 3945 | |
Pavel Grafov | c5c9730 | 2018-03-19 13:37:15 +0000 | [diff] [blame] | 3946 | if (currentVersion == 159) { |
| 3947 | // Version 160: Hiding notifications from the lockscreen is only available as |
| 3948 | // primary user option, profiles can only make them redacted. If a profile was |
| 3949 | // configured to not show lockscreen notifications, ensure that at the very |
| 3950 | // least these will be come hidden. |
| 3951 | if (mUserManager.isManagedProfile(userId)) { |
| 3952 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 3953 | Setting showNotifications = secureSettings.getSettingLocked( |
| 3954 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS); |
| 3955 | // The default value is "1", check if user has turned it off. |
| 3956 | if ("0".equals(showNotifications.getValue())) { |
| 3957 | secureSettings.insertSettingLocked( |
| 3958 | Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, "0", |
| 3959 | null /* tag */, false /* makeDefault */, |
| 3960 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 3961 | } |
| 3962 | // The setting is no longer valid for managed profiles, it should be |
| 3963 | // treated as if it was set to "1". |
| 3964 | secureSettings.deleteSettingLocked(Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS); |
| 3965 | } |
| 3966 | currentVersion = 160; |
| 3967 | } |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 3968 | |
| 3969 | if (currentVersion == 160) { |
| 3970 | // Version 161: Set the default value for |
Philip P. Moltmann | 7e25b3d | 2018-03-09 20:22:58 -0800 | [diff] [blame] | 3971 | // MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY and |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 3972 | // SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT |
| 3973 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3974 | |
| 3975 | String oldValue = globalSettings.getSettingLocked( |
Philip P. Moltmann | 7e25b3d | 2018-03-09 20:22:58 -0800 | [diff] [blame] | 3976 | Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY).getValue(); |
| 3977 | if (TextUtils.equals(null, oldValue)) { |
| 3978 | globalSettings.insertSettingLocked( |
| 3979 | Settings.Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY, |
| 3980 | Integer.toString(getContext().getResources().getInteger( |
| 3981 | R.integer.def_max_sound_trigger_detection_service_ops_per_day)), |
| 3982 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3983 | } |
| 3984 | |
| 3985 | oldValue = globalSettings.getSettingLocked( |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 3986 | Global.SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT).getValue(); |
| 3987 | if (TextUtils.equals(null, oldValue)) { |
| 3988 | globalSettings.insertSettingLocked( |
| 3989 | Settings.Global.SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT, |
| 3990 | Integer.toString(getContext().getResources().getInteger( |
| 3991 | R.integer.def_sound_trigger_detection_service_op_timeout)), |
| 3992 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 3993 | } |
| 3994 | currentVersion = 161; |
| 3995 | } |
| 3996 | |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 3997 | if (currentVersion == 161) { |
| 3998 | // Version 161: Add a gesture for silencing phones |
| 3999 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4000 | final Setting currentSetting = secureSettings.getSettingLocked( |
| 4001 | Secure.VOLUME_HUSH_GESTURE); |
| 4002 | if (currentSetting.isNull()) { |
| 4003 | secureSettings.insertSettingLocked( |
| 4004 | Secure.VOLUME_HUSH_GESTURE, |
| 4005 | Integer.toString(Secure.VOLUME_HUSH_VIBRATE), |
| 4006 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4007 | } |
| 4008 | |
| 4009 | currentVersion = 162; |
| 4010 | } |
| 4011 | |
Julia Reynolds | c4e5ecf | 2018-04-11 11:33:32 -0400 | [diff] [blame] | 4012 | if (currentVersion == 162) { |
Julia Reynolds | 76bfa60 | 2018-04-23 09:38:47 -0400 | [diff] [blame] | 4013 | // Version 162: REMOVED: Add a gesture for silencing phones |
Julia Reynolds | c4e5ecf | 2018-04-11 11:33:32 -0400 | [diff] [blame] | 4014 | currentVersion = 163; |
| 4015 | } |
| 4016 | |
Philip P. Moltmann | cb58a83 | 2018-04-16 09:19:42 -0700 | [diff] [blame] | 4017 | if (currentVersion == 163) { |
| 4018 | // Version 163: Update default value of |
| 4019 | // MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY from old to new default |
| 4020 | final SettingsState settings = getGlobalSettingsLocked(); |
| 4021 | final Setting currentSetting = settings.getSettingLocked( |
| 4022 | Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY); |
| 4023 | if (currentSetting.isDefaultFromSystem()) { |
| 4024 | settings.insertSettingLocked( |
| 4025 | Settings.Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY, |
| 4026 | Integer.toString(getContext().getResources().getInteger( |
| 4027 | R.integer |
| 4028 | .def_max_sound_trigger_detection_service_ops_per_day)), |
| 4029 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4030 | } |
| 4031 | |
| 4032 | currentVersion = 164; |
| 4033 | } |
| 4034 | |
Julia Reynolds | 76bfa60 | 2018-04-23 09:38:47 -0400 | [diff] [blame] | 4035 | if (currentVersion == 164) { |
Julia Reynolds | 24836c5 | 2018-06-06 14:36:03 -0400 | [diff] [blame] | 4036 | // Version 164: REMOVED: show zen upgrade notification |
Julia Reynolds | 76bfa60 | 2018-04-23 09:38:47 -0400 | [diff] [blame] | 4037 | currentVersion = 165; |
| 4038 | } |
| 4039 | |
Beverly | 301e92a | 2018-04-27 09:43:05 -0400 | [diff] [blame] | 4040 | if (currentVersion == 165) { |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 4041 | // Version 165: MOVED: Show zen settings suggestion and zen updated settings |
| 4042 | // moved to secure settings and are set in version 169 |
Beverly | 301e92a | 2018-04-27 09:43:05 -0400 | [diff] [blame] | 4043 | currentVersion = 166; |
| 4044 | } |
| 4045 | |
Beverly | 38fcfd0 | 2018-05-18 17:33:40 -0400 | [diff] [blame] | 4046 | if (currentVersion == 166) { |
| 4047 | // Version 166: add default values for hush gesture used and manual ringer |
| 4048 | // toggle |
| 4049 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4050 | Setting currentHushUsedSetting = secureSettings.getSettingLocked( |
| 4051 | Secure.HUSH_GESTURE_USED); |
| 4052 | if (currentHushUsedSetting.isNull()) { |
| 4053 | secureSettings.insertSettingLocked( |
| 4054 | Settings.Secure.HUSH_GESTURE_USED, "0", null, true, |
| 4055 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 4056 | } |
| 4057 | |
| 4058 | Setting currentRingerToggleCountSetting = secureSettings.getSettingLocked( |
| 4059 | Secure.MANUAL_RINGER_TOGGLE_COUNT); |
| 4060 | if (currentRingerToggleCountSetting.isNull()) { |
| 4061 | secureSettings.insertSettingLocked( |
| 4062 | Settings.Secure.MANUAL_RINGER_TOGGLE_COUNT, "0", null, true, |
| 4063 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 4064 | } |
| 4065 | currentVersion = 167; |
| 4066 | } |
| 4067 | |
Beverly | 155c9d2 | 2018-05-23 18:03:23 -0400 | [diff] [blame] | 4068 | if (currentVersion == 167) { |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 4069 | // Version 167: MOVED - Settings.Global.CHARGING_VIBRATION_ENABLED moved to |
| 4070 | // Settings.Secure.CHARGING_VIBRATION_ENABLED, set in version 170 |
Beverly | 155c9d2 | 2018-05-23 18:03:23 -0400 | [diff] [blame] | 4071 | currentVersion = 168; |
| 4072 | } |
| 4073 | |
Michael Wright | 0e9eeee | 2018-05-26 00:31:20 +0100 | [diff] [blame] | 4074 | if (currentVersion == 168) { |
| 4075 | // Version 168: by default, vibrate for phone calls |
| 4076 | final SettingsState systemSettings = getSystemSettingsLocked(userId); |
| 4077 | final Setting currentSetting = systemSettings.getSettingLocked( |
| 4078 | Settings.System.VIBRATE_WHEN_RINGING); |
| 4079 | if (currentSetting.isNull()) { |
| 4080 | systemSettings.insertSettingLocked( |
| 4081 | Settings.System.VIBRATE_WHEN_RINGING, |
| 4082 | getContext().getResources().getBoolean( |
| 4083 | R.bool.def_vibrate_when_ringing) ? "1" : "0", |
| 4084 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4085 | } |
| 4086 | currentVersion = 169; |
| 4087 | } |
| 4088 | |
Nadav Bar | f9f115d | 2018-06-24 10:06:50 +0300 | [diff] [blame] | 4089 | if (currentVersion == 169) { |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 4090 | // Version 169: Set the default value for Secure Settings ZEN_DURATION, |
| 4091 | // SHOW_ZEN_SETTINGS_SUGGESTION, ZEN_SETTINGS_UPDATE and |
| 4092 | // ZEN_SETTINGS_SUGGESTION_VIEWED |
Nadav Bar | f9f115d | 2018-06-24 10:06:50 +0300 | [diff] [blame] | 4093 | |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 4094 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 4095 | final Setting globalZenDuration = globalSettings.getSettingLocked( |
| 4096 | Global.ZEN_DURATION); |
| 4097 | |
| 4098 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4099 | final Setting secureZenDuration = secureSettings.getSettingLocked( |
| 4100 | Secure.ZEN_DURATION); |
| 4101 | |
| 4102 | // ZEN_DURATION |
| 4103 | if (!globalZenDuration.isNull()) { |
| 4104 | secureSettings.insertSettingLocked( |
| 4105 | Secure.ZEN_DURATION, globalZenDuration.getValue(), null, false, |
| 4106 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 4107 | |
| 4108 | // set global zen duration setting to null since it's deprecated |
| 4109 | globalSettings.insertSettingLocked( |
| 4110 | Global.ZEN_DURATION, null, null, true, |
| 4111 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 4112 | } else if (secureZenDuration.isNull()) { |
| 4113 | String defaultZenDuration = Integer.toString(getContext() |
| 4114 | .getResources().getInteger(R.integer.def_zen_duration)); |
| 4115 | secureSettings.insertSettingLocked( |
| 4116 | Secure.ZEN_DURATION, defaultZenDuration, null, true, |
| 4117 | SettingsState.SYSTEM_PACKAGE_NAME); |
Nadav Bar | f9f115d | 2018-06-24 10:06:50 +0300 | [diff] [blame] | 4118 | } |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 4119 | |
| 4120 | // SHOW_ZEN_SETTINGS_SUGGESTION |
| 4121 | final Setting currentShowZenSettingSuggestion = secureSettings.getSettingLocked( |
| 4122 | Secure.SHOW_ZEN_SETTINGS_SUGGESTION); |
| 4123 | if (currentShowZenSettingSuggestion.isNull()) { |
| 4124 | secureSettings.insertSettingLocked( |
| 4125 | Secure.SHOW_ZEN_SETTINGS_SUGGESTION, "1", |
| 4126 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4127 | } |
| 4128 | |
| 4129 | // ZEN_SETTINGS_UPDATED |
| 4130 | final Setting currentUpdatedSetting = secureSettings.getSettingLocked( |
| 4131 | Secure.ZEN_SETTINGS_UPDATED); |
| 4132 | if (currentUpdatedSetting.isNull()) { |
| 4133 | secureSettings.insertSettingLocked( |
| 4134 | Secure.ZEN_SETTINGS_UPDATED, "0", |
| 4135 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4136 | } |
| 4137 | |
| 4138 | // ZEN_SETTINGS_SUGGESTION_VIEWED |
| 4139 | final Setting currentSettingSuggestionViewed = secureSettings.getSettingLocked( |
| 4140 | Secure.ZEN_SETTINGS_SUGGESTION_VIEWED); |
| 4141 | if (currentSettingSuggestionViewed.isNull()) { |
| 4142 | secureSettings.insertSettingLocked( |
| 4143 | Secure.ZEN_SETTINGS_SUGGESTION_VIEWED, "0", |
| 4144 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4145 | } |
| 4146 | |
Nadav Bar | f9f115d | 2018-06-24 10:06:50 +0300 | [diff] [blame] | 4147 | currentVersion = 170; |
| 4148 | } |
| 4149 | |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 4150 | if (currentVersion == 170) { |
| 4151 | // Version 170: Set the default value for Secure Settings: |
| 4152 | // CHARGING_SOUNDS_ENABLED and CHARGING_VIBRATION_ENABLED |
| 4153 | |
| 4154 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 4155 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4156 | |
| 4157 | // CHARGING_SOUNDS_ENABLED |
| 4158 | final Setting globalChargingSoundEnabled = globalSettings.getSettingLocked( |
| 4159 | Global.CHARGING_SOUNDS_ENABLED); |
| 4160 | final Setting secureChargingSoundsEnabled = secureSettings.getSettingLocked( |
| 4161 | Secure.CHARGING_SOUNDS_ENABLED); |
| 4162 | |
| 4163 | if (!globalChargingSoundEnabled.isNull()) { |
Beverly | ff9c587 | 2019-01-02 15:44:00 -0500 | [diff] [blame] | 4164 | if (secureChargingSoundsEnabled.isNull()) { |
| 4165 | secureSettings.insertSettingLocked( |
| 4166 | Secure.CHARGING_SOUNDS_ENABLED, |
| 4167 | globalChargingSoundEnabled.getValue(), null, false, |
| 4168 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 4169 | } |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 4170 | |
| 4171 | // set global charging_sounds_enabled setting to null since it's deprecated |
| 4172 | globalSettings.insertSettingLocked( |
| 4173 | Global.CHARGING_SOUNDS_ENABLED, null, null, true, |
| 4174 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 4175 | } else if (secureChargingSoundsEnabled.isNull()) { |
| 4176 | String defChargingSoundsEnabled = getContext().getResources() |
| 4177 | .getBoolean(R.bool.def_charging_sounds_enabled) ? "1" : "0"; |
| 4178 | secureSettings.insertSettingLocked( |
| 4179 | Secure.CHARGING_SOUNDS_ENABLED, defChargingSoundsEnabled, null, |
| 4180 | true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4181 | } |
| 4182 | |
| 4183 | // CHARGING_VIBRATION_ENABLED |
| 4184 | final Setting secureChargingVibrationEnabled = secureSettings.getSettingLocked( |
| 4185 | Secure.CHARGING_VIBRATION_ENABLED); |
| 4186 | |
| 4187 | if (secureChargingVibrationEnabled.isNull()) { |
| 4188 | String defChargingVibrationEnabled = getContext().getResources() |
| 4189 | .getBoolean(R.bool.def_charging_vibration_enabled) ? "1" : "0"; |
| 4190 | secureSettings.insertSettingLocked( |
| 4191 | Secure.CHARGING_VIBRATION_ENABLED, defChargingVibrationEnabled, |
| 4192 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4193 | } |
| 4194 | |
| 4195 | currentVersion = 171; |
| 4196 | } |
| 4197 | |
Nadav Bar | 8bc8c9e | 2018-09-12 15:41:51 +0300 | [diff] [blame] | 4198 | if (currentVersion == 171) { |
| 4199 | // Version 171: by default, add STREAM_VOICE_CALL to list of streams that can |
| 4200 | // be muted. |
| 4201 | final SettingsState systemSettings = getSystemSettingsLocked(userId); |
| 4202 | final Setting currentSetting = systemSettings.getSettingLocked( |
| 4203 | Settings.System.MUTE_STREAMS_AFFECTED); |
| 4204 | if (!currentSetting.isNull()) { |
| 4205 | try { |
| 4206 | int currentSettingIntegerValue = Integer.parseInt( |
| 4207 | currentSetting.getValue()); |
| 4208 | if ((currentSettingIntegerValue |
| 4209 | & (1 << AudioManager.STREAM_VOICE_CALL)) == 0) { |
| 4210 | systemSettings.insertSettingLocked( |
| 4211 | Settings.System.MUTE_STREAMS_AFFECTED, |
| 4212 | Integer.toString( |
| 4213 | currentSettingIntegerValue |
| 4214 | | (1 << AudioManager.STREAM_VOICE_CALL)), |
| 4215 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4216 | } |
| 4217 | } catch (NumberFormatException e) { |
| 4218 | // remove the setting in case it is not a valid integer |
| 4219 | Slog.w("Failed to parse integer value of MUTE_STREAMS_AFFECTED" |
| 4220 | + "setting, removing setting", e); |
| 4221 | systemSettings.deleteSettingLocked( |
| 4222 | Settings.System.MUTE_STREAMS_AFFECTED); |
| 4223 | } |
| 4224 | |
| 4225 | } |
| 4226 | currentVersion = 172; |
| 4227 | } |
| 4228 | |
Soonil Nagarkar | 42da1b1 | 2019-01-22 11:29:27 -0800 | [diff] [blame] | 4229 | if (currentVersion == 172) { |
| 4230 | // Version 172: Set the default value for Secure Settings: LOCATION_MODE |
| 4231 | |
| 4232 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4233 | |
| 4234 | final Setting locationMode = secureSettings.getSettingLocked( |
| 4235 | Secure.LOCATION_MODE); |
| 4236 | |
| 4237 | if (locationMode.isNull()) { |
| 4238 | final Setting locationProvidersAllowed = secureSettings.getSettingLocked( |
| 4239 | Secure.LOCATION_PROVIDERS_ALLOWED); |
| 4240 | |
Soonil Nagarkar | 4ee3ac2 | 2019-02-08 19:19:24 -0800 | [diff] [blame] | 4241 | final int defLocationMode; |
| 4242 | if (locationProvidersAllowed.isNull()) { |
| 4243 | defLocationMode = getContext().getResources().getInteger( |
| 4244 | R.integer.def_location_mode); |
| 4245 | } else { |
| 4246 | defLocationMode = |
| 4247 | !TextUtils.isEmpty(locationProvidersAllowed.getValue()) |
| 4248 | ? Secure.LOCATION_MODE_ON |
| 4249 | : Secure.LOCATION_MODE_OFF; |
| 4250 | } |
Soonil Nagarkar | 42da1b1 | 2019-01-22 11:29:27 -0800 | [diff] [blame] | 4251 | secureSettings.insertSettingLocked( |
Soonil Nagarkar | 4ee3ac2 | 2019-02-08 19:19:24 -0800 | [diff] [blame] | 4252 | Secure.LOCATION_MODE, Integer.toString(defLocationMode), |
Soonil Nagarkar | 42da1b1 | 2019-01-22 11:29:27 -0800 | [diff] [blame] | 4253 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4254 | } |
| 4255 | |
| 4256 | currentVersion = 173; |
| 4257 | } |
| 4258 | |
Beverly | 4179f99 | 2019-02-08 11:34:16 -0500 | [diff] [blame] | 4259 | if (currentVersion == 173) { |
| 4260 | // Version 173: Set the default value for Secure Settings: NOTIFICATION_BUBBLES |
| 4261 | |
| 4262 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4263 | |
| 4264 | final Setting bubblesSetting = secureSettings.getSettingLocked( |
| 4265 | Secure.NOTIFICATION_BUBBLES); |
| 4266 | |
| 4267 | if (bubblesSetting.isNull()) { |
| 4268 | secureSettings.insertSettingLocked(Secure.NOTIFICATION_BUBBLES, |
| 4269 | getContext().getResources().getBoolean( |
| 4270 | R.bool.def_notification_bubbles) ? "1" : "0", null, |
| 4271 | true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4272 | } |
| 4273 | |
| 4274 | currentVersion = 174; |
| 4275 | } |
| 4276 | |
Yiwen Chen | 0305b57 | 2019-03-05 11:26:59 -0800 | [diff] [blame] | 4277 | if (currentVersion == 174) { |
| 4278 | // Version 174: Set the default value for Global Settings: APPLY_RAMPING_RINGER |
| 4279 | |
| 4280 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 4281 | |
| 4282 | Setting currentRampingRingerSetting = globalSettings.getSettingLocked( |
| 4283 | Settings.Global.APPLY_RAMPING_RINGER); |
| 4284 | if (currentRampingRingerSetting.isNull()) { |
| 4285 | globalSettings.insertSettingLocked( |
| 4286 | Settings.Global.APPLY_RAMPING_RINGER, |
| 4287 | getContext().getResources().getBoolean( |
| 4288 | R.bool.def_apply_ramping_ringer) ? "1" : "0", null, |
| 4289 | true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4290 | } |
| 4291 | |
| 4292 | currentVersion = 175; |
| 4293 | } |
| 4294 | |
wilsonshih | 5d999e2 | 2019-03-20 11:50:42 +0800 | [diff] [blame] | 4295 | if (currentVersion == 175) { |
| 4296 | // Version 175: Set the default value for System Settings: |
| 4297 | // RING_VIBRATION_INTENSITY. If the notification vibration intensity has been |
| 4298 | // set and ring vibration intensity hasn't, the ring vibration intensity should |
| 4299 | // followed notification vibration intensity. |
| 4300 | |
| 4301 | final SettingsState systemSettings = getSystemSettingsLocked(userId); |
| 4302 | |
| 4303 | Setting notificationVibrationIntensity = systemSettings.getSettingLocked( |
| 4304 | Settings.System.NOTIFICATION_VIBRATION_INTENSITY); |
| 4305 | |
| 4306 | Setting ringVibrationIntensity = systemSettings.getSettingLocked( |
| 4307 | Settings.System.RING_VIBRATION_INTENSITY); |
| 4308 | |
| 4309 | if (!notificationVibrationIntensity.isNull() |
| 4310 | && ringVibrationIntensity.isNull()) { |
| 4311 | systemSettings.insertSettingLocked( |
| 4312 | Settings.System.RING_VIBRATION_INTENSITY, |
| 4313 | notificationVibrationIntensity.getValue(), |
| 4314 | null , true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4315 | } |
| 4316 | |
| 4317 | currentVersion = 176; |
| 4318 | } |
| 4319 | |
Winson Chung | d9f2fb3 | 2019-03-05 11:10:12 -0800 | [diff] [blame] | 4320 | if (currentVersion == 176) { |
| 4321 | // Version 176: Migrate the existing swipe up setting into the resource overlay |
Winson Chung | 3d5d3b3 | 2019-04-24 16:52:47 -0700 | [diff] [blame] | 4322 | // for the navigation bar interaction mode. We do so only if the |
| 4323 | // setting is set. |
Winson Chung | d9f2fb3 | 2019-03-05 11:10:12 -0800 | [diff] [blame] | 4324 | |
Winson Chung | d9f2fb3 | 2019-03-05 11:10:12 -0800 | [diff] [blame] | 4325 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4326 | final Setting swipeUpSetting = secureSettings.getSettingLocked( |
Winson Chung | 85e7459 | 2019-03-06 11:49:22 -0800 | [diff] [blame] | 4327 | "swipe_up_to_switch_apps_enabled"); |
Winson Chung | 3d5d3b3 | 2019-04-24 16:52:47 -0700 | [diff] [blame] | 4328 | if (swipeUpSetting != null && !swipeUpSetting.isNull() |
| 4329 | && swipeUpSetting.getValue().equals("1")) { |
| 4330 | final IOverlayManager overlayManager = IOverlayManager.Stub.asInterface( |
| 4331 | ServiceManager.getService(Context.OVERLAY_SERVICE)); |
Winson Chung | d9f2fb3 | 2019-03-05 11:10:12 -0800 | [diff] [blame] | 4332 | try { |
Winson Chung | 3d5d3b3 | 2019-04-24 16:52:47 -0700 | [diff] [blame] | 4333 | overlayManager.setEnabledExclusiveInCategory( |
| 4334 | NAV_BAR_MODE_2BUTTON_OVERLAY, UserHandle.USER_CURRENT); |
Winson Chung | d9f2fb3 | 2019-03-05 11:10:12 -0800 | [diff] [blame] | 4335 | } catch (RemoteException e) { |
| 4336 | throw new IllegalStateException( |
| 4337 | "Failed to set nav bar interaction mode overlay"); |
| 4338 | } |
| 4339 | } |
| 4340 | |
| 4341 | currentVersion = 177; |
| 4342 | } |
| 4343 | |
Edgar Wang | eb8bddb | 2019-04-19 17:29:49 +0800 | [diff] [blame] | 4344 | if (currentVersion == 177) { |
| 4345 | // Version 177: Set the default value for Secure Settings: AWARE_ENABLED |
| 4346 | |
| 4347 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4348 | |
| 4349 | final Setting awareEnabled = secureSettings.getSettingLocked( |
| 4350 | Secure.AWARE_ENABLED); |
| 4351 | |
| 4352 | if (awareEnabled.isNull()) { |
| 4353 | final boolean defAwareEnabled = getContext().getResources().getBoolean( |
| 4354 | R.bool.def_aware_enabled); |
| 4355 | secureSettings.insertSettingLocked( |
| 4356 | Secure.AWARE_ENABLED, defAwareEnabled ? "1" : "0", |
| 4357 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4358 | } |
| 4359 | |
| 4360 | currentVersion = 178; |
| 4361 | } |
| 4362 | |
Edgar Wang | dabc41e | 2019-05-07 18:53:31 +0800 | [diff] [blame] | 4363 | if (currentVersion == 178) { |
| 4364 | // Version 178: Set the default value for Secure Settings: |
| 4365 | // SKIP_GESTURE & SILENCE_GESTURE |
| 4366 | |
| 4367 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4368 | |
| 4369 | final Setting skipGesture = secureSettings.getSettingLocked( |
| 4370 | Secure.SKIP_GESTURE); |
| 4371 | |
| 4372 | if (skipGesture.isNull()) { |
| 4373 | final boolean defSkipGesture = getContext().getResources().getBoolean( |
| 4374 | R.bool.def_skip_gesture); |
| 4375 | secureSettings.insertSettingLocked( |
| 4376 | Secure.SKIP_GESTURE, defSkipGesture ? "1" : "0", |
| 4377 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4378 | } |
| 4379 | |
| 4380 | final Setting silenceGesture = secureSettings.getSettingLocked( |
| 4381 | Secure.SILENCE_GESTURE); |
| 4382 | |
| 4383 | if (silenceGesture.isNull()) { |
| 4384 | final boolean defSilenceGesture = getContext().getResources().getBoolean( |
| 4385 | R.bool.def_silence_gesture); |
| 4386 | secureSettings.insertSettingLocked( |
| 4387 | Secure.SILENCE_GESTURE, defSilenceGesture ? "1" : "0", |
| 4388 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4389 | } |
| 4390 | |
| 4391 | currentVersion = 179; |
| 4392 | } |
| 4393 | |
Mady Mellor | 95e879a | 2019-05-06 08:50:06 -0700 | [diff] [blame] | 4394 | if (currentVersion == 179) { |
| 4395 | // Version 178: Reset the default for Secure Settings: NOTIFICATION_BUBBLES |
| 4396 | // This is originally set in version 173, however, the default value changed |
| 4397 | // so this step is to ensure the value is updated to the correct defaulte |
| 4398 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4399 | |
| 4400 | secureSettings.insertSettingLocked(Secure.NOTIFICATION_BUBBLES, |
| 4401 | getContext().getResources().getBoolean( |
| 4402 | R.bool.def_notification_bubbles) ? "1" : "0", null, |
| 4403 | true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4404 | |
| 4405 | currentVersion = 180; |
| 4406 | } |
Winson Chung | d9f2fb3 | 2019-03-05 11:10:12 -0800 | [diff] [blame] | 4407 | |
Edgar Wang | 4870e4b | 2019-05-15 19:14:43 +0800 | [diff] [blame] | 4408 | if (currentVersion == 180) { |
| 4409 | // Version 180: Set the default value for Secure Settings: AWARE_LOCK_ENABLED |
| 4410 | |
| 4411 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 4412 | |
| 4413 | final Setting awareLockEnabled = secureSettings.getSettingLocked( |
| 4414 | Secure.AWARE_LOCK_ENABLED); |
| 4415 | |
| 4416 | if (awareLockEnabled.isNull()) { |
| 4417 | final boolean defAwareLockEnabled = getContext().getResources().getBoolean( |
| 4418 | R.bool.def_aware_lock_enabled); |
| 4419 | secureSettings.insertSettingLocked( |
| 4420 | Secure.AWARE_LOCK_ENABLED, defAwareLockEnabled ? "1" : "0", |
| 4421 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4422 | } |
| 4423 | |
| 4424 | currentVersion = 181; |
| 4425 | } |
| 4426 | |
Eric Laurent | 02153e6 | 2019-06-12 17:20:03 -0700 | [diff] [blame] | 4427 | if (currentVersion == 181) { |
| 4428 | // Version cd : by default, add STREAM_BLUETOOTH_SCO to list of streams that can |
| 4429 | // be muted. |
| 4430 | final SettingsState systemSettings = getSystemSettingsLocked(userId); |
| 4431 | final Setting currentSetting = systemSettings.getSettingLocked( |
| 4432 | Settings.System.MUTE_STREAMS_AFFECTED); |
| 4433 | if (!currentSetting.isNull()) { |
| 4434 | try { |
| 4435 | int currentSettingIntegerValue = Integer.parseInt( |
| 4436 | currentSetting.getValue()); |
| 4437 | if ((currentSettingIntegerValue |
| 4438 | & (1 << AudioManager.STREAM_BLUETOOTH_SCO)) == 0) { |
| 4439 | systemSettings.insertSettingLocked( |
| 4440 | Settings.System.MUTE_STREAMS_AFFECTED, |
| 4441 | Integer.toString( |
| 4442 | currentSettingIntegerValue |
| 4443 | | (1 << AudioManager.STREAM_BLUETOOTH_SCO)), |
| 4444 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 4445 | } |
| 4446 | } catch (NumberFormatException e) { |
| 4447 | // remove the setting in case it is not a valid integer |
| 4448 | Slog.w("Failed to parse integer value of MUTE_STREAMS_AFFECTED" |
| 4449 | + "setting, removing setting", e); |
| 4450 | systemSettings.deleteSettingLocked( |
| 4451 | Settings.System.MUTE_STREAMS_AFFECTED); |
| 4452 | } |
| 4453 | |
| 4454 | } |
| 4455 | currentVersion = 182; |
| 4456 | } |
| 4457 | |
Felipe Leme | ff35509 | 2017-04-03 12:55:02 -0700 | [diff] [blame] | 4458 | // vXXX: Add new settings above this point. |
| 4459 | |
Dan Sandler | 71f85e9 | 2016-07-20 13:46:05 -0400 | [diff] [blame] | 4460 | if (currentVersion != newVersion) { |
Svetoslav Ganov | 264c7a9 | 2016-08-24 17:31:14 -0700 | [diff] [blame] | 4461 | Slog.wtf("SettingsProvider", "warning: upgrading settings database to version " |
Dan Sandler | 71f85e9 | 2016-07-20 13:46:05 -0400 | [diff] [blame] | 4462 | + newVersion + " left it at " |
Stephen Chen | 5d0922f | 2017-03-27 10:28:04 -0700 | [diff] [blame] | 4463 | + currentVersion + |
| 4464 | " instead; this is probably a bug. Did you update SETTINGS_VERSION?", |
| 4465 | new Throwable()); |
Dan Sandler | 71f85e9 | 2016-07-20 13:46:05 -0400 | [diff] [blame] | 4466 | if (DEBUG) { |
| 4467 | throw new RuntimeException("db upgrade error"); |
| 4468 | } |
| 4469 | } |
| 4470 | |
Jeff Brown | 503cffc | 2015-03-26 18:08:51 -0700 | [diff] [blame] | 4471 | // Return the current version. |
| 4472 | return currentVersion; |
Brad Fitzpatrick | 547a96b | 2010-03-09 17:58:53 -0800 | [diff] [blame] | 4473 | } |
| 4474 | } |
Svet Ganov | 1370155 | 2017-02-23 12:45:17 -0800 | [diff] [blame] | 4475 | |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 4476 | private void ensureLegacyDefaultValueAndSystemSetUpdatedLocked(SettingsState settings, |
| 4477 | int userId) { |
Svet Ganov | 1370155 | 2017-02-23 12:45:17 -0800 | [diff] [blame] | 4478 | List<String> names = settings.getSettingNamesLocked(); |
| 4479 | final int nameCount = names.size(); |
| 4480 | for (int i = 0; i < nameCount; i++) { |
| 4481 | String name = names.get(i); |
| 4482 | Setting setting = settings.getSettingLocked(name); |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 4483 | |
| 4484 | // In the upgrade case we pretend the call is made from the app |
| 4485 | // that made the last change to the setting to properly determine |
| 4486 | // whether the call has been made by a system component. |
| 4487 | int callingUid = -1; |
| 4488 | try { |
| 4489 | callingUid = mPackageManager.getPackageUid(setting.getPackageName(), 0, userId); |
| 4490 | } catch (RemoteException e) { |
| 4491 | /* ignore - handled below */ |
| 4492 | } |
| 4493 | if (callingUid < 0) { |
| 4494 | Slog.e(LOG_TAG, "Unknown package: " + setting.getPackageName()); |
| 4495 | continue; |
| 4496 | } |
| 4497 | try { |
| 4498 | final boolean systemSet = SettingsState.isSystemPackage(getContext(), |
Matt Pape | 25c940d | 2019-03-26 12:14:33 -0700 | [diff] [blame] | 4499 | setting.getPackageName(), callingUid, userId); |
Svet Ganov | 1370155 | 2017-02-23 12:45:17 -0800 | [diff] [blame] | 4500 | if (systemSet) { |
| 4501 | settings.insertSettingLocked(name, setting.getValue(), |
| 4502 | setting.getTag(), true, setting.getPackageName()); |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 4503 | } else if (setting.getDefaultValue() != null && setting.isDefaultFromSystem()) { |
| 4504 | // We had a bug where changes by non-system packages were marked |
| 4505 | // as system made and as a result set as the default. Therefore, if |
| 4506 | // the package changed the setting last is not a system one but the |
| 4507 | // setting is marked as its default coming from the system we clear |
| 4508 | // the default and clear the system set flag. |
| 4509 | settings.resetSettingDefaultValueLocked(name); |
Svet Ganov | 1370155 | 2017-02-23 12:45:17 -0800 | [diff] [blame] | 4510 | } |
Svetoslav Ganov | f7654c5 | 2017-08-16 17:33:22 -0700 | [diff] [blame] | 4511 | } catch (IllegalStateException e) { |
| 4512 | // If the package goes over its quota during the upgrade, don't |
| 4513 | // crash but just log the error as the system does the upgrade. |
| 4514 | Slog.e(LOG_TAG, "Error upgrading setting: " + setting.getName(), e); |
| 4515 | |
Svet Ganov | 1370155 | 2017-02-23 12:45:17 -0800 | [diff] [blame] | 4516 | } |
| 4517 | } |
| 4518 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 4519 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4520 | } |