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 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 19 | import android.Manifest; |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame^] | 20 | import android.annotation.NonNull; |
Christopher Tate | d5fe147 | 2012-09-10 15:48:38 -0700 | [diff] [blame] | 21 | import android.app.ActivityManager; |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 22 | import android.app.AppGlobals; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 23 | import android.app.backup.BackupManager; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 25 | import android.content.ComponentName; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | import android.content.ContentProvider; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | import android.content.ContentValues; |
| 28 | import android.content.Context; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 29 | import android.content.Intent; |
| 30 | import android.content.IntentFilter; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 31 | import android.content.pm.ApplicationInfo; |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 32 | import android.content.pm.IPackageManager; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 33 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | import android.content.pm.PackageManager; |
Christopher Tate | 38e7a60 | 2013-09-03 16:57:34 -0700 | [diff] [blame] | 35 | import android.content.pm.UserInfo; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | import android.database.Cursor; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 37 | import android.database.MatrixCursor; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | import android.database.sqlite.SQLiteDatabase; |
| 39 | import android.database.sqlite.SQLiteQueryBuilder; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 40 | import android.hardware.camera2.utils.ArrayUtils; |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 41 | import android.media.AudioManager; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 42 | import android.net.Uri; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 43 | import android.os.Binder; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 44 | import android.os.Build; |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 45 | import android.os.Bundle; |
Amith Yamasani | 5cdf7f5 | 2013-06-27 15:12:01 -0700 | [diff] [blame] | 46 | import android.os.DropBoxManager; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 47 | import android.os.Environment; |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 48 | import android.os.Handler; |
Svet Ganov | a8f9026 | 2016-05-10 08:44:48 -0700 | [diff] [blame] | 49 | import android.os.HandlerThread; |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 50 | import android.os.Looper; |
| 51 | import android.os.Message; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 52 | import android.os.ParcelFileDescriptor; |
Christopher Tate | 0da1357 | 2013-10-13 17:34:49 -0700 | [diff] [blame] | 53 | import android.os.Process; |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 54 | import android.os.RemoteException; |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 55 | import android.os.SELinux; |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 56 | import android.os.ServiceManager; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 57 | import android.os.UserHandle; |
| 58 | import android.os.UserManager; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 59 | import android.os.UserManagerInternal; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 60 | import android.provider.Settings; |
Jeremy Joslin | 8bdad34 | 2016-12-14 11:46:47 -0800 | [diff] [blame] | 61 | import android.provider.Settings.Global; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 62 | import android.text.TextUtils; |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 63 | import android.util.ArrayMap; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 64 | import android.util.ArraySet; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 65 | import android.util.ByteStringUtils; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 66 | import android.util.Slog; |
| 67 | import android.util.SparseArray; |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 68 | import android.util.SparseBooleanArray; |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame^] | 69 | import android.util.proto.ProtoOutputStream; |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 70 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 71 | import com.android.internal.annotations.GuardedBy; |
| 72 | import com.android.internal.content.PackageMonitor; |
| 73 | import com.android.internal.os.BackgroundThread; |
Suprabh Shukla | 269c11e | 2015-12-02 16:51:16 -0800 | [diff] [blame] | 74 | import com.android.providers.settings.SettingsState.Setting; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 75 | import com.android.server.LocalServices; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 76 | import com.android.server.SystemConfig; |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 77 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 78 | import java.io.File; |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 79 | import java.io.FileDescriptor; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 80 | import java.io.FileNotFoundException; |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 81 | import java.io.PrintWriter; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 82 | import java.nio.charset.StandardCharsets; |
| 83 | import java.security.MessageDigest; |
| 84 | import java.security.NoSuchAlgorithmException; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 85 | import java.security.SecureRandom; |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 86 | import java.util.ArrayList; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 87 | import java.util.Arrays; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 88 | import java.util.HashSet; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 89 | import java.util.List; |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 90 | import java.util.Map; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 91 | import java.util.Set; |
| 92 | import java.util.regex.Pattern; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 93 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 94 | import static android.os.Process.ROOT_UID; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 95 | import static android.os.Process.SHELL_UID; |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame^] | 96 | import static android.os.Process.SYSTEM_UID; |
| 97 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 98 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 99 | /** |
| 100 | * <p> |
| 101 | * This class is a content provider that publishes the system settings. |
| 102 | * It can be accessed via the content provider APIs or via custom call |
| 103 | * commands. The latter is a bit faster and is the preferred way to access |
| 104 | * the platform settings. |
| 105 | * </p> |
| 106 | * <p> |
| 107 | * There are three settings types, global (with signature level protection |
| 108 | * and shared across users), secure (with signature permission level |
| 109 | * protection and per user), and system (with dangerous permission level |
| 110 | * protection and per user). Global settings are stored under the device owner. |
| 111 | * Each of these settings is represented by a {@link |
| 112 | * com.android.providers.settings.SettingsState} object mapped to an integer |
| 113 | * key derived from the setting type in the most significant bits and user |
| 114 | * id in the least significant bits. Settings are synchronously loaded on |
| 115 | * instantiation of a SettingsState and asynchronously persisted on mutation. |
| 116 | * Settings are stored in the user specific system directory. |
| 117 | * </p> |
| 118 | * <p> |
| 119 | * Apps targeting APIs Lollipop MR1 and lower can add custom settings entries |
| 120 | * and get a warning. Targeting higher API version prohibits this as the |
| 121 | * system settings are not a place for apps to save their state. When a package |
| 122 | * is removed the settings it added are deleted. Apps cannot delete system |
| 123 | * settings added by the platform. System settings values are validated to |
| 124 | * ensure the clients do not put bad values. Global and secure settings are |
| 125 | * changed only by trusted parties, therefore no validation is performed. Also |
| 126 | * there is a limit on the amount of app specific settings that can be added |
| 127 | * to prevent unlimited growth of the system process memory footprint. |
| 128 | * </p> |
| 129 | */ |
| 130 | @SuppressWarnings("deprecation") |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 131 | public class SettingsProvider extends ContentProvider { |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 132 | static final boolean DEBUG = false; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | |
Svetoslav Ganov | 264c7a9 | 2016-08-24 17:31:14 -0700 | [diff] [blame] | 134 | private static final boolean DROP_DATABASE_ON_MIGRATION = true; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 135 | |
| 136 | private static final String LOG_TAG = "SettingsProvider"; |
Christopher Tate | 0da1357 | 2013-10-13 17:34:49 -0700 | [diff] [blame] | 137 | |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 138 | private static final String TABLE_SYSTEM = "system"; |
| 139 | private static final String TABLE_SECURE = "secure"; |
| 140 | private static final String TABLE_GLOBAL = "global"; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 141 | |
| 142 | // Old tables no longer exist. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | private static final String TABLE_FAVORITES = "favorites"; |
| 144 | private static final String TABLE_OLD_FAVORITES = "old_favorites"; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 145 | private static final String TABLE_BLUETOOTH_DEVICES = "bluetooth_devices"; |
| 146 | private static final String TABLE_BOOKMARKS = "bookmarks"; |
| 147 | private static final String TABLE_ANDROID_METADATA = "android_metadata"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 149 | // The set of removed legacy tables. |
| 150 | private static final Set<String> REMOVED_LEGACY_TABLES = new ArraySet<>(); |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 151 | static { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 152 | REMOVED_LEGACY_TABLES.add(TABLE_FAVORITES); |
| 153 | REMOVED_LEGACY_TABLES.add(TABLE_OLD_FAVORITES); |
| 154 | REMOVED_LEGACY_TABLES.add(TABLE_BLUETOOTH_DEVICES); |
| 155 | REMOVED_LEGACY_TABLES.add(TABLE_BOOKMARKS); |
| 156 | REMOVED_LEGACY_TABLES.add(TABLE_ANDROID_METADATA); |
| 157 | } |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 158 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 159 | private static final int MUTATION_OPERATION_INSERT = 1; |
| 160 | private static final int MUTATION_OPERATION_DELETE = 2; |
| 161 | private static final int MUTATION_OPERATION_UPDATE = 3; |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 162 | private static final int MUTATION_OPERATION_RESET = 4; |
Julia Reynolds | 5e458dd | 2014-07-07 16:07:01 -0400 | [diff] [blame] | 163 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 164 | private static final String[] ALL_COLUMNS = new String[] { |
| 165 | Settings.NameValueTable._ID, |
| 166 | Settings.NameValueTable.NAME, |
| 167 | Settings.NameValueTable.VALUE |
| 168 | }; |
| 169 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 170 | public static final int SETTINGS_TYPE_GLOBAL = 0; |
| 171 | public static final int SETTINGS_TYPE_SYSTEM = 1; |
| 172 | public static final int SETTINGS_TYPE_SECURE = 2; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 173 | public static final int SETTINGS_TYPE_SSAID = 3; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 174 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 175 | public static final int SETTINGS_TYPE_MASK = 0xF0000000; |
| 176 | public static final int SETTINGS_TYPE_SHIFT = 28; |
| 177 | |
| 178 | private static final Bundle NULL_SETTING_BUNDLE = Bundle.forPair( |
| 179 | Settings.NameValueTable.VALUE, null); |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 180 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 181 | // Per user secure settings that moved to the for all users global settings. |
| 182 | static final Set<String> sSecureMovedToGlobalSettings = new ArraySet<>(); |
| 183 | static { |
| 184 | Settings.Secure.getMovedToGlobalSettings(sSecureMovedToGlobalSettings); |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 187 | // Per user system settings that moved to the for all users global settings. |
| 188 | static final Set<String> sSystemMovedToGlobalSettings = new ArraySet<>(); |
| 189 | static { |
| 190 | Settings.System.getMovedToGlobalSettings(sSystemMovedToGlobalSettings); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 193 | // Per user system settings that moved to the per user secure settings. |
| 194 | static final Set<String> sSystemMovedToSecureSettings = new ArraySet<>(); |
| 195 | static { |
| 196 | Settings.System.getMovedToSecureSettings(sSystemMovedToSecureSettings); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 199 | // Per all users global settings that moved to the per user secure settings. |
| 200 | static final Set<String> sGlobalMovedToSecureSettings = new ArraySet<>(); |
| 201 | static { |
| 202 | Settings.Global.getMovedToSecureSettings(sGlobalMovedToSecureSettings); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 205 | // Per user secure settings that are cloned for the managed profiles of the user. |
| 206 | private static final Set<String> sSecureCloneToManagedSettings = new ArraySet<>(); |
| 207 | static { |
| 208 | Settings.Secure.getCloneToManagedProfileSettings(sSecureCloneToManagedSettings); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 211 | // Per user system settings that are cloned for the managed profiles of the user. |
| 212 | private static final Set<String> sSystemCloneToManagedSettings = new ArraySet<>(); |
| 213 | static { |
| 214 | Settings.System.getCloneToManagedProfileSettings(sSystemCloneToManagedSettings); |
Julia Reynolds | 5e458dd | 2014-07-07 16:07:01 -0400 | [diff] [blame] | 215 | } |
| 216 | |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 217 | // Per user system settings that are cloned from the profile's parent when a dependency |
| 218 | // in {@link Settings.Secure} is set to "1". |
| 219 | public static final Map<String, String> sSystemCloneFromParentOnDependency = new ArrayMap<>(); |
| 220 | static { |
| 221 | Settings.System.getCloneFromParentOnValueSettings(sSystemCloneFromParentOnDependency); |
| 222 | } |
| 223 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 224 | private final Object mLock = new Object(); |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 225 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 226 | @GuardedBy("mLock") |
| 227 | private SettingsRegistry mSettingsRegistry; |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 228 | |
Svet Ganov | a8f9026 | 2016-05-10 08:44:48 -0700 | [diff] [blame] | 229 | @GuardedBy("mLock") |
| 230 | private HandlerThread mHandlerThread; |
| 231 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 232 | // We have to call in the user manager with no lock held, |
| 233 | private volatile UserManager mUserManager; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 234 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 235 | // We have to call in the package manager with no lock held, |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 236 | private volatile IPackageManager mPackageManager; |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 237 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 238 | public static int makeKey(int type, int userId) { |
| 239 | return (type << SETTINGS_TYPE_SHIFT) | userId; |
| 240 | } |
| 241 | |
| 242 | public static int getTypeFromKey(int key) { |
| 243 | return key >>> SETTINGS_TYPE_SHIFT; |
| 244 | } |
| 245 | |
| 246 | public static int getUserIdFromKey(int key) { |
| 247 | return key & ~SETTINGS_TYPE_MASK; |
| 248 | } |
| 249 | |
| 250 | public static String settingTypeToString(int type) { |
| 251 | switch (type) { |
| 252 | case SETTINGS_TYPE_GLOBAL: { |
| 253 | return "SETTINGS_GLOBAL"; |
| 254 | } |
| 255 | case SETTINGS_TYPE_SECURE: { |
| 256 | return "SETTINGS_SECURE"; |
| 257 | } |
| 258 | case SETTINGS_TYPE_SYSTEM: { |
| 259 | return "SETTINGS_SYSTEM"; |
| 260 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 261 | case SETTINGS_TYPE_SSAID: { |
| 262 | return "SETTINGS_SSAID"; |
| 263 | } |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 264 | default: { |
| 265 | return "UNKNOWN"; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | public static String keyToString(int key) { |
| 271 | return "Key[user=" + getUserIdFromKey(key) + ";type=" |
| 272 | + settingTypeToString(getTypeFromKey(key)) + "]"; |
| 273 | } |
| 274 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 275 | @Override |
| 276 | public boolean onCreate() { |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 277 | Settings.setInSystemServer(); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 278 | synchronized (mLock) { |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 279 | mUserManager = UserManager.get(getContext()); |
| 280 | mPackageManager = AppGlobals.getPackageManager(); |
Svet Ganov | a8f9026 | 2016-05-10 08:44:48 -0700 | [diff] [blame] | 281 | mHandlerThread = new HandlerThread(LOG_TAG, |
| 282 | Process.THREAD_PRIORITY_BACKGROUND); |
| 283 | mHandlerThread.start(); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 284 | mSettingsRegistry = new SettingsRegistry(); |
| 285 | } |
| 286 | registerBroadcastReceivers(); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 287 | startWatchingUserRestrictionChanges(); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 288 | ServiceManager.addService("settings", new SettingsService(this)); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 289 | return true; |
| 290 | } |
| 291 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 292 | @Override |
| 293 | public Bundle call(String method, String name, Bundle args) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 294 | final int requestingUserId = getRequestingUserId(args); |
| 295 | switch (method) { |
| 296 | case Settings.CALL_METHOD_GET_GLOBAL: { |
| 297 | Setting setting = getGlobalSetting(name); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 298 | return packageValueForCallResult(setting, isTrackingGeneration(args)); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 299 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 300 | |
| 301 | case Settings.CALL_METHOD_GET_SECURE: { |
| 302 | Setting setting = getSecureSetting(name, requestingUserId); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 303 | return packageValueForCallResult(setting, isTrackingGeneration(args)); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | case Settings.CALL_METHOD_GET_SYSTEM: { |
| 307 | Setting setting = getSystemSetting(name, requestingUserId); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 308 | return packageValueForCallResult(setting, isTrackingGeneration(args)); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | case Settings.CALL_METHOD_PUT_GLOBAL: { |
| 312 | String value = getSettingValue(args); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 313 | String tag = getSettingTag(args); |
| 314 | final boolean makeDefault = getSettingMakeDefault(args); |
| 315 | insertGlobalSetting(name, value, tag, makeDefault, requestingUserId, false); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 316 | break; |
| 317 | } |
| 318 | |
| 319 | case Settings.CALL_METHOD_PUT_SECURE: { |
| 320 | String value = getSettingValue(args); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 321 | String tag = getSettingTag(args); |
| 322 | final boolean makeDefault = getSettingMakeDefault(args); |
| 323 | insertSecureSetting(name, value, tag, makeDefault, requestingUserId, false); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 324 | break; |
| 325 | } |
| 326 | |
| 327 | case Settings.CALL_METHOD_PUT_SYSTEM: { |
| 328 | String value = getSettingValue(args); |
| 329 | insertSystemSetting(name, value, requestingUserId); |
| 330 | break; |
| 331 | } |
| 332 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 333 | case Settings.CALL_METHOD_RESET_GLOBAL: { |
| 334 | final int mode = getResetModeEnforcingPermission(args); |
| 335 | String tag = getSettingTag(args); |
| 336 | resetGlobalSetting(requestingUserId, mode, tag); |
| 337 | break; |
| 338 | } |
| 339 | |
| 340 | case Settings.CALL_METHOD_RESET_SECURE: { |
| 341 | final int mode = getResetModeEnforcingPermission(args); |
| 342 | String tag = getSettingTag(args); |
| 343 | resetSecureSetting(requestingUserId, mode, tag); |
| 344 | break; |
| 345 | } |
| 346 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 347 | default: { |
| 348 | Slog.w(LOG_TAG, "call() with invalid method: " + method); |
| 349 | } break; |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 350 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 351 | |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 352 | return null; |
| 353 | } |
| 354 | |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 355 | @Override |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 356 | public String getType(Uri uri) { |
| 357 | Arguments args = new Arguments(uri, null, null, true); |
| 358 | if (TextUtils.isEmpty(args.name)) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 359 | return "vnd.android.cursor.dir/" + args.table; |
| 360 | } else { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 361 | return "vnd.android.cursor.item/" + args.table; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | @Override |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 366 | public Cursor query(Uri uri, String[] projection, String where, String[] whereArgs, |
| 367 | String order) { |
| 368 | if (DEBUG) { |
| 369 | Slog.v(LOG_TAG, "query() for user: " + UserHandle.getCallingUserId()); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 372 | Arguments args = new Arguments(uri, where, whereArgs, true); |
| 373 | String[] normalizedProjection = normalizeProjection(projection); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 374 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 375 | // If a legacy table that is gone, done. |
| 376 | if (REMOVED_LEGACY_TABLES.contains(args.table)) { |
| 377 | return new MatrixCursor(normalizedProjection, 0); |
| 378 | } |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 379 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 380 | switch (args.table) { |
| 381 | case TABLE_GLOBAL: { |
| 382 | if (args.name != null) { |
| 383 | Setting setting = getGlobalSetting(args.name); |
| 384 | return packageSettingForQuery(setting, normalizedProjection); |
| 385 | } else { |
| 386 | return getAllGlobalSettings(projection); |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 387 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 388 | } |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 389 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 390 | case TABLE_SECURE: { |
| 391 | final int userId = UserHandle.getCallingUserId(); |
| 392 | if (args.name != null) { |
| 393 | Setting setting = getSecureSetting(args.name, userId); |
| 394 | return packageSettingForQuery(setting, normalizedProjection); |
| 395 | } else { |
| 396 | return getAllSecureSettings(userId, projection); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 397 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 398 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 399 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 400 | case TABLE_SYSTEM: { |
| 401 | final int userId = UserHandle.getCallingUserId(); |
| 402 | if (args.name != null) { |
| 403 | Setting setting = getSystemSetting(args.name, userId); |
| 404 | return packageSettingForQuery(setting, normalizedProjection); |
| 405 | } else { |
| 406 | return getAllSystemSettings(userId, projection); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 407 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 408 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 409 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 410 | default: { |
| 411 | throw new IllegalArgumentException("Invalid Uri path:" + uri); |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 412 | } |
| 413 | } |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 414 | } |
| 415 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 416 | @Override |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 417 | public Uri insert(Uri uri, ContentValues values) { |
| 418 | if (DEBUG) { |
| 419 | Slog.v(LOG_TAG, "insert() for user: " + UserHandle.getCallingUserId()); |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 422 | String table = getValidTableOrThrow(uri); |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 423 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 424 | // If a legacy table that is gone, done. |
| 425 | if (REMOVED_LEGACY_TABLES.contains(table)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | return null; |
| 427 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 428 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 429 | String name = values.getAsString(Settings.Secure.NAME); |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 430 | if (!isKeyValid(name)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 431 | return null; |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 434 | String value = values.getAsString(Settings.Secure.VALUE); |
| 435 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 436 | switch (table) { |
| 437 | case TABLE_GLOBAL: { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 438 | if (insertGlobalSetting(name, value, null, false, |
| 439 | UserHandle.getCallingUserId(), false)) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 440 | return Uri.withAppendedPath(Settings.Global.CONTENT_URI, name); |
Christopher Tate | c221d2b | 2012-10-03 18:33:52 -0700 | [diff] [blame] | 441 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 442 | } break; |
| 443 | |
| 444 | case TABLE_SECURE: { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 445 | if (insertSecureSetting(name, value, null, false, |
| 446 | UserHandle.getCallingUserId(), false)) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 447 | return Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name); |
| 448 | } |
| 449 | } break; |
| 450 | |
| 451 | case TABLE_SYSTEM: { |
| 452 | if (insertSystemSetting(name, value, UserHandle.getCallingUserId())) { |
| 453 | return Uri.withAppendedPath(Settings.System.CONTENT_URI, name); |
| 454 | } |
| 455 | } break; |
| 456 | |
| 457 | default: { |
| 458 | throw new IllegalArgumentException("Bad Uri path:" + uri); |
Christopher Tate | c221d2b | 2012-10-03 18:33:52 -0700 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 462 | return null; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | @Override |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 466 | public int bulkInsert(Uri uri, ContentValues[] allValues) { |
| 467 | if (DEBUG) { |
| 468 | Slog.v(LOG_TAG, "bulkInsert() for user: " + UserHandle.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 470 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 471 | int insertionCount = 0; |
| 472 | final int valuesCount = allValues.length; |
| 473 | for (int i = 0; i < valuesCount; i++) { |
| 474 | ContentValues values = allValues[i]; |
| 475 | if (insert(uri, values) != null) { |
| 476 | insertionCount++; |
| 477 | } |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 478 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 479 | |
| 480 | return insertionCount; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | @Override |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 484 | public int delete(Uri uri, String where, String[] whereArgs) { |
| 485 | if (DEBUG) { |
| 486 | Slog.v(LOG_TAG, "delete() for user: " + UserHandle.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 488 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 489 | Arguments args = new Arguments(uri, where, whereArgs, false); |
| 490 | |
| 491 | // If a legacy table that is gone, done. |
| 492 | if (REMOVED_LEGACY_TABLES.contains(args.table)) { |
| 493 | return 0; |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 494 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 495 | |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 496 | if (!isKeyValid(args.name)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 497 | return 0; |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 498 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 499 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 500 | switch (args.table) { |
| 501 | case TABLE_GLOBAL: { |
| 502 | final int userId = UserHandle.getCallingUserId(); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 503 | return deleteGlobalSetting(args.name, userId, false) ? 1 : 0; |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 504 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 505 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 506 | case TABLE_SECURE: { |
| 507 | final int userId = UserHandle.getCallingUserId(); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 508 | return deleteSecureSetting(args.name, userId, false) ? 1 : 0; |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 509 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 510 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 511 | case TABLE_SYSTEM: { |
| 512 | final int userId = UserHandle.getCallingUserId(); |
| 513 | return deleteSystemSetting(args.name, userId) ? 1 : 0; |
| 514 | } |
| 515 | |
| 516 | default: { |
| 517 | throw new IllegalArgumentException("Bad Uri path:" + uri); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 518 | } |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 519 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | @Override |
| 523 | public int update(Uri uri, ContentValues values, String where, String[] whereArgs) { |
| 524 | if (DEBUG) { |
| 525 | Slog.v(LOG_TAG, "update() for user: " + UserHandle.getCallingUserId()); |
Christopher Tate | 06efb53 | 2012-08-24 15:29:27 -0700 | [diff] [blame] | 526 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 527 | |
| 528 | Arguments args = new Arguments(uri, where, whereArgs, false); |
| 529 | |
| 530 | // If a legacy table that is gone, done. |
| 531 | if (REMOVED_LEGACY_TABLES.contains(args.table)) { |
| 532 | return 0; |
| 533 | } |
| 534 | |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 535 | String name = values.getAsString(Settings.Secure.NAME); |
| 536 | if (!isKeyValid(name)) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 537 | return 0; |
| 538 | } |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 539 | String value = values.getAsString(Settings.Secure.VALUE); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 540 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 541 | switch (args.table) { |
| 542 | case TABLE_GLOBAL: { |
| 543 | final int userId = UserHandle.getCallingUserId(); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 544 | return updateGlobalSetting(args.name, value, null, false, |
| 545 | userId, false) ? 1 : 0; |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 546 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 547 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 548 | case TABLE_SECURE: { |
| 549 | final int userId = UserHandle.getCallingUserId(); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 550 | return updateSecureSetting(args.name, value, null, false, |
| 551 | userId, false) ? 1 : 0; |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 552 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 553 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 554 | case TABLE_SYSTEM: { |
| 555 | final int userId = UserHandle.getCallingUserId(); |
| 556 | return updateSystemSetting(args.name, value, userId) ? 1 : 0; |
| 557 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 558 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 559 | default: { |
| 560 | throw new IllegalArgumentException("Invalid Uri path:" + uri); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 561 | } |
| 562 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | @Override |
| 566 | public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { |
Robin Lee | 2ab02e2 | 2016-07-28 18:41:23 +0100 | [diff] [blame] | 567 | final int userId = getUserIdFromUri(uri, UserHandle.getCallingUserId()); |
| 568 | if (userId != UserHandle.getCallingUserId()) { |
| 569 | getContext().enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS, |
| 570 | "Access files from the settings of another user"); |
| 571 | } |
| 572 | uri = ContentProvider.getUriWithoutUserId(uri); |
| 573 | |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 574 | final String cacheRingtoneSetting; |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 575 | final String cacheName; |
| 576 | if (Settings.System.RINGTONE_CACHE_URI.equals(uri)) { |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 577 | cacheRingtoneSetting = Settings.System.RINGTONE; |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 578 | cacheName = Settings.System.RINGTONE_CACHE; |
| 579 | } else if (Settings.System.NOTIFICATION_SOUND_CACHE_URI.equals(uri)) { |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 580 | cacheRingtoneSetting = Settings.System.NOTIFICATION_SOUND; |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 581 | cacheName = Settings.System.NOTIFICATION_SOUND_CACHE; |
| 582 | } else if (Settings.System.ALARM_ALERT_CACHE_URI.equals(uri)) { |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 583 | cacheRingtoneSetting = Settings.System.ALARM_ALERT; |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 584 | cacheName = Settings.System.ALARM_ALERT_CACHE; |
| 585 | } else { |
| 586 | throw new FileNotFoundException("Direct file access no longer supported; " |
| 587 | + "ringtone playback is available through android.media.Ringtone"); |
| 588 | } |
| 589 | |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 590 | int actualCacheOwner; |
| 591 | // Redirect cache to parent if ringtone setting is owned by profile parent |
| 592 | synchronized (mLock) { |
| 593 | actualCacheOwner = resolveOwningUserIdForSystemSettingLocked(userId, |
| 594 | cacheRingtoneSetting); |
| 595 | } |
| 596 | final File cacheFile = new File(getRingtoneCacheDir(actualCacheOwner), cacheName); |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 597 | return ParcelFileDescriptor.open(cacheFile, ParcelFileDescriptor.parseMode(mode)); |
| 598 | } |
| 599 | |
| 600 | private File getRingtoneCacheDir(int userId) { |
| 601 | final File cacheDir = new File(Environment.getDataSystemDeDirectory(userId), "ringtones"); |
| 602 | cacheDir.mkdir(); |
| 603 | SELinux.restorecon(cacheDir); |
| 604 | return cacheDir; |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 605 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 606 | |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame^] | 607 | /** |
| 608 | * Dump all settings as a proto buf. |
| 609 | * |
| 610 | * @param fd The file to dump to |
| 611 | */ |
| 612 | void dumpProto(@NonNull FileDescriptor fd) { |
| 613 | ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 614 | |
| 615 | synchronized (mLock) { |
| 616 | SettingsProtoDumpUtil.dumpProtoLocked(mSettingsRegistry, proto); |
| 617 | |
| 618 | } |
| 619 | |
| 620 | proto.flush(); |
| 621 | } |
| 622 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 623 | public void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) { |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 624 | synchronized (mLock) { |
| 625 | final long identity = Binder.clearCallingIdentity(); |
| 626 | try { |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 627 | SparseBooleanArray users = mSettingsRegistry.getKnownUsersLocked(); |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 628 | final int userCount = users.size(); |
| 629 | for (int i = 0; i < userCount; i++) { |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 630 | dumpForUserLocked(users.keyAt(i), pw); |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 631 | } |
| 632 | } finally { |
| 633 | Binder.restoreCallingIdentity(identity); |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
Svetoslav Ganov | a340bfd | 2016-08-02 18:24:49 -0700 | [diff] [blame] | 638 | private void dumpForUserLocked(int userId, PrintWriter pw) { |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 639 | if (userId == UserHandle.USER_SYSTEM) { |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 640 | pw.println("GLOBAL SETTINGS (user " + userId + ")"); |
Svetoslav Ganov | a340bfd | 2016-08-02 18:24:49 -0700 | [diff] [blame] | 641 | SettingsState globalSettings = mSettingsRegistry.getSettingsLocked( |
| 642 | SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 643 | if (globalSettings != null) { |
| 644 | dumpSettingsLocked(globalSettings, pw); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 645 | pw.println(); |
| 646 | globalSettings.dumpHistoricalOperations(pw); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 647 | } |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | pw.println("SECURE SETTINGS (user " + userId + ")"); |
Svetoslav Ganov | a340bfd | 2016-08-02 18:24:49 -0700 | [diff] [blame] | 651 | SettingsState secureSettings = mSettingsRegistry.getSettingsLocked( |
| 652 | SETTINGS_TYPE_SECURE, userId); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 653 | if (secureSettings != null) { |
| 654 | dumpSettingsLocked(secureSettings, pw); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 655 | pw.println(); |
| 656 | secureSettings.dumpHistoricalOperations(pw); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 657 | } |
Svetoslav Ganov | a340bfd | 2016-08-02 18:24:49 -0700 | [diff] [blame] | 658 | |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 659 | pw.println("SYSTEM SETTINGS (user " + userId + ")"); |
Svetoslav Ganov | a340bfd | 2016-08-02 18:24:49 -0700 | [diff] [blame] | 660 | SettingsState systemSettings = mSettingsRegistry.getSettingsLocked( |
| 661 | SETTINGS_TYPE_SYSTEM, userId); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 662 | if (systemSettings != null) { |
| 663 | dumpSettingsLocked(systemSettings, pw); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 664 | pw.println(); |
| 665 | systemSettings.dumpHistoricalOperations(pw); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 666 | } |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 667 | } |
| 668 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 669 | private void dumpSettingsLocked(SettingsState settingsState, PrintWriter pw) { |
| 670 | List<String> names = settingsState.getSettingNamesLocked(); |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 671 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 672 | final int nameCount = names.size(); |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 673 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 674 | for (int i = 0; i < nameCount; i++) { |
| 675 | String name = names.get(i); |
| 676 | Setting setting = settingsState.getSettingLocked(name); |
| 677 | pw.print("_id:"); pw.print(toDumpString(setting.getId())); |
| 678 | pw.print(" name:"); pw.print(toDumpString(name)); |
| 679 | if (setting.getPackageName() != null) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 680 | pw.print(" pkg:"); pw.print(setting.getPackageName()); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 681 | } |
| 682 | pw.print(" value:"); pw.print(toDumpString(setting.getValue())); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 683 | if (setting.getDefaultValue() != null) { |
| 684 | pw.print(" default:"); pw.print(setting.getDefaultValue()); |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame^] | 685 | pw.print(" defaultSystemSet:"); pw.print(setting.isDefaultFromSystem()); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 686 | } |
| 687 | if (setting.getTag() != null) { |
| 688 | pw.print(" tag:"); pw.print(setting.getTag()); |
| 689 | } |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 690 | pw.println(); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 691 | } |
Svetoslav | b505ccc | 2015-02-17 12:41:04 -0800 | [diff] [blame] | 692 | } |
| 693 | |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 694 | private static String toDumpString(String s) { |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 695 | if (s != null) { |
| 696 | return s; |
| 697 | } |
| 698 | return "{null}"; |
| 699 | } |
| 700 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 701 | private void registerBroadcastReceivers() { |
| 702 | IntentFilter userFilter = new IntentFilter(); |
| 703 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 704 | userFilter.addAction(Intent.ACTION_USER_STOPPED); |
| 705 | |
| 706 | getContext().registerReceiver(new BroadcastReceiver() { |
| 707 | @Override |
| 708 | public void onReceive(Context context, Intent intent) { |
| 709 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 710 | UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 711 | |
| 712 | switch (intent.getAction()) { |
| 713 | case Intent.ACTION_USER_REMOVED: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 714 | synchronized (mLock) { |
| 715 | mSettingsRegistry.removeUserStateLocked(userId, true); |
| 716 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 717 | } break; |
| 718 | |
| 719 | case Intent.ACTION_USER_STOPPED: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 720 | synchronized (mLock) { |
| 721 | mSettingsRegistry.removeUserStateLocked(userId, false); |
| 722 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 723 | } break; |
| 724 | } |
| 725 | } |
| 726 | }, userFilter); |
| 727 | |
| 728 | PackageMonitor monitor = new PackageMonitor() { |
| 729 | @Override |
| 730 | public void onPackageRemoved(String packageName, int uid) { |
| 731 | synchronized (mLock) { |
| 732 | mSettingsRegistry.onPackageRemovedLocked(packageName, |
| 733 | UserHandle.getUserId(uid)); |
| 734 | } |
| 735 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 736 | |
| 737 | @Override |
| 738 | public void onUidRemoved(int uid) { |
| 739 | synchronized (mLock) { |
| 740 | mSettingsRegistry.onUidRemovedLocked(uid); |
| 741 | } |
| 742 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 743 | }; |
| 744 | |
| 745 | // package changes |
| 746 | monitor.register(getContext(), BackgroundThread.getHandler().getLooper(), |
| 747 | UserHandle.ALL, true); |
| 748 | } |
| 749 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 750 | private void startWatchingUserRestrictionChanges() { |
| 751 | // TODO: The current design of settings looking different based on user restrictions |
| 752 | // should be reworked to keep them separate and system code should check the setting |
| 753 | // first followed by checking the user restriction before performing an operation. |
| 754 | UserManagerInternal userManager = LocalServices.getService(UserManagerInternal.class); |
| 755 | userManager.addUserRestrictionsListener((int userId, Bundle newRestrictions, |
| 756 | Bundle prevRestrictions) -> { |
| 757 | // We are changing the settings affected by restrictions to their current |
| 758 | // value with a forced update to ensure that all cross profile dependencies |
| 759 | // are taken into account. Also make sure the settings update to.. the same |
| 760 | // value passes the security checks, so clear binder calling id. |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 761 | if (newRestrictions.getBoolean(UserManager.DISALLOW_SHARE_LOCATION) |
| 762 | != prevRestrictions.getBoolean(UserManager.DISALLOW_SHARE_LOCATION)) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 763 | final long identity = Binder.clearCallingIdentity(); |
| 764 | try { |
| 765 | synchronized (mLock) { |
| 766 | Setting setting = getSecureSetting( |
| 767 | Settings.Secure.LOCATION_PROVIDERS_ALLOWED, userId); |
| 768 | updateSecureSetting(Settings.Secure.LOCATION_PROVIDERS_ALLOWED, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 769 | setting != null ? setting.getValue() : null, null, |
| 770 | true, userId, true); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 771 | } |
| 772 | } finally { |
| 773 | Binder.restoreCallingIdentity(identity); |
| 774 | } |
| 775 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 776 | if (newRestrictions.getBoolean(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES) |
| 777 | != prevRestrictions.getBoolean(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 778 | final long identity = Binder.clearCallingIdentity(); |
| 779 | try { |
| 780 | synchronized (mLock) { |
| 781 | Setting setting = getGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 782 | String value = setting != null ? setting.getValue() : null; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 783 | updateGlobalSetting(Settings.Global.INSTALL_NON_MARKET_APPS, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 784 | value, null, true, userId, true); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 785 | } |
| 786 | } finally { |
| 787 | Binder.restoreCallingIdentity(identity); |
| 788 | } |
| 789 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 790 | if (newRestrictions.getBoolean(UserManager.DISALLOW_DEBUGGING_FEATURES) |
| 791 | != prevRestrictions.getBoolean(UserManager.DISALLOW_DEBUGGING_FEATURES)) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 792 | final long identity = Binder.clearCallingIdentity(); |
| 793 | try { |
| 794 | synchronized (mLock) { |
| 795 | Setting setting = getGlobalSetting(Settings.Global.ADB_ENABLED); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 796 | String value = setting != null ? setting.getValue() : null; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 797 | updateGlobalSetting(Settings.Global.ADB_ENABLED, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 798 | value, null, true, userId, true); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 799 | } |
| 800 | } finally { |
| 801 | Binder.restoreCallingIdentity(identity); |
| 802 | } |
| 803 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 804 | if (newRestrictions.getBoolean(UserManager.ENSURE_VERIFY_APPS) |
| 805 | != prevRestrictions.getBoolean(UserManager.ENSURE_VERIFY_APPS)) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 806 | final long identity = Binder.clearCallingIdentity(); |
| 807 | try { |
| 808 | synchronized (mLock) { |
| 809 | Setting enable = getGlobalSetting( |
| 810 | Settings.Global.PACKAGE_VERIFIER_ENABLE); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 811 | String enableValue = enable != null ? enable.getValue() : null; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 812 | updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_ENABLE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 813 | enableValue, null, true, userId, true); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 814 | Setting include = getGlobalSetting( |
| 815 | Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 816 | String includeValue = include != null ? include.getValue() : null; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 817 | updateGlobalSetting(Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 818 | includeValue, null, true, userId, true); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 819 | } |
| 820 | } finally { |
| 821 | Binder.restoreCallingIdentity(identity); |
| 822 | } |
| 823 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 824 | if (newRestrictions.getBoolean(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS) |
| 825 | != prevRestrictions.getBoolean(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 826 | final long identity = Binder.clearCallingIdentity(); |
| 827 | try { |
| 828 | synchronized (mLock) { |
| 829 | Setting setting = getGlobalSetting( |
| 830 | Settings.Global.PREFERRED_NETWORK_MODE); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 831 | String value = setting != null ? setting.getValue() : null; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 832 | updateGlobalSetting(Settings.Global.PREFERRED_NETWORK_MODE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 833 | value, null, true, userId, true); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 834 | } |
| 835 | } finally { |
| 836 | Binder.restoreCallingIdentity(identity); |
| 837 | } |
| 838 | } |
| 839 | }); |
| 840 | } |
| 841 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 842 | private Cursor getAllGlobalSettings(String[] projection) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 843 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 844 | Slog.v(LOG_TAG, "getAllGlobalSettings()"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 845 | } |
| 846 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 847 | synchronized (mLock) { |
| 848 | // Get the settings. |
| 849 | SettingsState settingsState = mSettingsRegistry.getSettingsLocked( |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 850 | SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 851 | |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 852 | List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_GLOBAL, |
| 853 | UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 854 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 855 | final int nameCount = names.size(); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 856 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 857 | String[] normalizedProjection = normalizeProjection(projection); |
| 858 | MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 859 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 860 | // Anyone can get the global settings, so no security checks. |
| 861 | for (int i = 0; i < nameCount; i++) { |
| 862 | String name = names.get(i); |
| 863 | Setting setting = settingsState.getSettingLocked(name); |
| 864 | appendSettingToCursor(result, setting); |
| 865 | } |
| 866 | |
| 867 | return result; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 868 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 869 | } |
| 870 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 871 | private Setting getGlobalSetting(String name) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 872 | if (DEBUG) { |
| 873 | Slog.v(LOG_TAG, "getGlobalSetting(" + name + ")"); |
| 874 | } |
| 875 | |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 876 | // Ensure the caller can access the setting. |
| 877 | enforceSettingReadable(name, SETTINGS_TYPE_GLOBAL, UserHandle.getCallingUserId()); |
| 878 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 879 | // Get the value. |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 880 | synchronized (mLock) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 881 | return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_GLOBAL, |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 882 | UserHandle.USER_SYSTEM, name); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 883 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 884 | } |
| 885 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 886 | private boolean updateGlobalSetting(String name, String value, String tag, |
| 887 | boolean makeDefault, int requestingUserId, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 888 | if (DEBUG) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 889 | Slog.v(LOG_TAG, "updateGlobalSetting(" + name + ", " + value + ", " |
| 890 | + ", " + tag + ", " + makeDefault + ", " + requestingUserId |
| 891 | + ", " + forceNotify + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 892 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 893 | return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId, |
| 894 | MUTATION_OPERATION_UPDATE, forceNotify, 0); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 895 | } |
| 896 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 897 | private boolean insertGlobalSetting(String name, String value, String tag, |
| 898 | boolean makeDefault, int requestingUserId, boolean forceNotify) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 899 | if (DEBUG) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 900 | Slog.v(LOG_TAG, "insertGlobalSetting(" + name + ", " + value + ", " |
| 901 | + ", " + tag + ", " + makeDefault + ", " + requestingUserId |
| 902 | + ", " + forceNotify + ")"); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 903 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 904 | return mutateGlobalSetting(name, value, tag, makeDefault, requestingUserId, |
| 905 | MUTATION_OPERATION_INSERT, forceNotify, 0); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 908 | private boolean deleteGlobalSetting(String name, int requestingUserId, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 909 | if (DEBUG) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 910 | Slog.v(LOG_TAG, "deleteGlobalSetting(" + name + ", " + requestingUserId |
| 911 | + ", " + forceNotify + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 912 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 913 | return mutateGlobalSetting(name, null, null, false, requestingUserId, |
| 914 | MUTATION_OPERATION_DELETE, forceNotify, 0); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 915 | } |
| 916 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 917 | private void resetGlobalSetting(int requestingUserId, int mode, String tag) { |
| 918 | if (DEBUG) { |
| 919 | Slog.v(LOG_TAG, "resetGlobalSetting(" + requestingUserId + ", " |
| 920 | + mode + ", " + tag + ")"); |
| 921 | } |
| 922 | mutateGlobalSetting(null, null, tag, false, requestingUserId, |
| 923 | MUTATION_OPERATION_RESET, false, mode); |
| 924 | } |
| 925 | |
| 926 | private boolean mutateGlobalSetting(String name, String value, String tag, |
| 927 | boolean makeDefault, int requestingUserId, int operation, boolean forceNotify, |
| 928 | int mode) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 929 | // Make sure the caller can change the settings - treated as secure. |
| 930 | enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS); |
| 931 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 932 | // Resolve the userId on whose behalf the call is made. |
| 933 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId); |
| 934 | |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 935 | // If this is a setting that is currently restricted for this user, do not allow |
| 936 | // unrestricting changes. |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 937 | if (name != null && isGlobalOrSecureSettingRestrictedForUser(name, callingUserId, value, |
Victor Chang | 9c7b706 | 2016-07-12 23:47:29 +0100 | [diff] [blame] | 938 | Binder.getCallingUid())) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 939 | return false; |
| 940 | } |
| 941 | |
| 942 | // Perform the mutation. |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 943 | synchronized (mLock) { |
| 944 | switch (operation) { |
| 945 | case MUTATION_OPERATION_INSERT: { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 946 | return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_GLOBAL, |
| 947 | UserHandle.USER_SYSTEM, name, value, tag, makeDefault, |
| 948 | getCallingPackage(), forceNotify); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 949 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 950 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 951 | case MUTATION_OPERATION_DELETE: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 952 | return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_GLOBAL, |
| 953 | UserHandle.USER_SYSTEM, name, forceNotify); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 954 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 955 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 956 | case MUTATION_OPERATION_UPDATE: { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 957 | return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_GLOBAL, |
| 958 | UserHandle.USER_SYSTEM, name, value, tag, makeDefault, |
| 959 | getCallingPackage(), forceNotify); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 960 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 961 | |
| 962 | case MUTATION_OPERATION_RESET: { |
| 963 | mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_GLOBAL, |
| 964 | UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag); |
| 965 | } return true; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
| 969 | return false; |
| 970 | } |
| 971 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 972 | private Cursor getAllSecureSettings(int userId, String[] projection) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 973 | if (DEBUG) { |
| 974 | Slog.v(LOG_TAG, "getAllSecureSettings(" + userId + ")"); |
| 975 | } |
| 976 | |
| 977 | // Resolve the userId on whose behalf the call is made. |
| 978 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId); |
| 979 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 980 | synchronized (mLock) { |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 981 | List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SECURE, callingUserId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 982 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 983 | final int nameCount = names.size(); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 984 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 985 | String[] normalizedProjection = normalizeProjection(projection); |
| 986 | MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 987 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 988 | for (int i = 0; i < nameCount; i++) { |
| 989 | String name = names.get(i); |
| 990 | // Determine the owning user as some profile settings are cloned from the parent. |
| 991 | final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, |
| 992 | name); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 993 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 994 | // Special case for location (sigh). |
| 995 | if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) { |
Svetoslav Ganov | 83a1f7f | 2016-04-27 13:50:49 -0700 | [diff] [blame] | 996 | continue; |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 997 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 998 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 999 | // As of Android O (API 24), the SSAID is read from an app-specific entry in table |
| 1000 | // SETTINGS_FILE_SSAID, unless accessed by a system process. |
| 1001 | final Setting setting; |
| 1002 | if (isNewSsaidSetting(name)) { |
| 1003 | setting = getSsaidSettingLocked(owningUserId); |
| 1004 | } else { |
| 1005 | setting = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE, owningUserId, |
| 1006 | name); |
| 1007 | } |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1008 | appendSettingToCursor(result, setting); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1009 | } |
| 1010 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1011 | return result; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1012 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1015 | private Setting getSecureSetting(String name, int requestingUserId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1016 | if (DEBUG) { |
| 1017 | Slog.v(LOG_TAG, "getSecureSetting(" + name + ", " + requestingUserId + ")"); |
| 1018 | } |
| 1019 | |
| 1020 | // Resolve the userId on whose behalf the call is made. |
| 1021 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId); |
| 1022 | |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1023 | // Ensure the caller can access the setting. |
| 1024 | enforceSettingReadable(name, SETTINGS_TYPE_SECURE, callingUserId); |
| 1025 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1026 | // Determine the owning user as some profile settings are cloned from the parent. |
| 1027 | final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name); |
| 1028 | |
| 1029 | // Special case for location (sigh). |
| 1030 | if (isLocationProvidersAllowedRestricted(name, callingUserId, owningUserId)) { |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 1031 | SettingsState settings = mSettingsRegistry.getSettingsLocked(SETTINGS_TYPE_SECURE, |
| 1032 | owningUserId); |
| 1033 | return settings != null ? settings.getNullSetting() : null; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | // Get the value. |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1037 | synchronized (mLock) { |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1038 | // As of Android O (API 24), the SSAID is read from an app-specific entry in table |
| 1039 | // SETTINGS_FILE_SSAID, unless accessed by a system process. |
| 1040 | if (isNewSsaidSetting(name)) { |
| 1041 | return getSsaidSettingLocked(owningUserId); |
| 1042 | } |
| 1043 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1044 | return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SECURE, |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1045 | owningUserId, name); |
| 1046 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1047 | } |
| 1048 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1049 | private boolean isNewSsaidSetting(String name) { |
| 1050 | return Settings.Secure.ANDROID_ID.equals(name) |
| 1051 | && UserHandle.getAppId(Binder.getCallingUid()) >= Process.FIRST_APPLICATION_UID; |
| 1052 | } |
| 1053 | |
| 1054 | private Setting getSsaidSettingLocked(int owningUserId) { |
| 1055 | // Get uid of caller (key) used to store ssaid value |
| 1056 | String name = Integer.toString( |
| 1057 | UserHandle.getUid(owningUserId, UserHandle.getAppId(Binder.getCallingUid()))); |
| 1058 | |
| 1059 | if (DEBUG) { |
| 1060 | Slog.v(LOG_TAG, "getSsaidSettingLocked(" + name + "," + owningUserId + ")"); |
| 1061 | } |
| 1062 | |
| 1063 | // Retrieve the ssaid from the table if present. |
| 1064 | final Setting ssaid = mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SSAID, owningUserId, |
| 1065 | name); |
| 1066 | |
| 1067 | // Lazy initialize ssaid if not yet present in ssaid table. |
Mark Rathjen | ea61759 | 2017-01-18 23:03:41 -0800 | [diff] [blame] | 1068 | if (ssaid == null || ssaid.isNull() || ssaid.getValue() == null) { |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1069 | return mSettingsRegistry.generateSsaidLocked(getCallingPackage(), owningUserId); |
| 1070 | } |
| 1071 | |
| 1072 | return ssaid; |
| 1073 | } |
| 1074 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1075 | private boolean insertSecureSetting(String name, String value, String tag, |
| 1076 | boolean makeDefault, int requestingUserId, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1077 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1078 | Slog.v(LOG_TAG, "insertSecureSetting(" + name + ", " + value + ", " |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1079 | + ", " + tag + ", " + makeDefault + ", " + requestingUserId |
| 1080 | + ", " + forceNotify + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1083 | return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId, |
| 1084 | MUTATION_OPERATION_INSERT, forceNotify, 0); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1085 | } |
| 1086 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1087 | private boolean deleteSecureSetting(String name, int requestingUserId, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1088 | if (DEBUG) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1089 | Slog.v(LOG_TAG, "deleteSecureSetting(" + name + ", " + requestingUserId |
| 1090 | + ", " + forceNotify + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1091 | } |
| 1092 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1093 | return mutateSecureSetting(name, null, null, false, requestingUserId, |
| 1094 | MUTATION_OPERATION_DELETE, forceNotify, 0); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1095 | } |
| 1096 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1097 | private boolean updateSecureSetting(String name, String value, String tag, |
| 1098 | boolean makeDefault, int requestingUserId, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1099 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1100 | Slog.v(LOG_TAG, "updateSecureSetting(" + name + ", " + value + ", " |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1101 | + ", " + tag + ", " + makeDefault + ", " + requestingUserId |
| 1102 | + ", " + forceNotify +")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1103 | } |
| 1104 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1105 | return mutateSecureSetting(name, value, tag, makeDefault, requestingUserId, |
| 1106 | MUTATION_OPERATION_UPDATE, forceNotify, 0); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1107 | } |
| 1108 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1109 | private void resetSecureSetting(int requestingUserId, int mode, String tag) { |
| 1110 | if (DEBUG) { |
| 1111 | Slog.v(LOG_TAG, "resetSecureSetting(" + requestingUserId + ", " |
| 1112 | + mode + ", " + tag + ")"); |
| 1113 | } |
| 1114 | |
| 1115 | mutateSecureSetting(null, null, tag, false, requestingUserId, |
| 1116 | MUTATION_OPERATION_RESET, false, mode); |
| 1117 | } |
| 1118 | |
| 1119 | private boolean mutateSecureSetting(String name, String value, String tag, |
| 1120 | boolean makeDefault, int requestingUserId, int operation, boolean forceNotify, |
| 1121 | int mode) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1122 | // Make sure the caller can change the settings. |
| 1123 | enforceWritePermission(Manifest.permission.WRITE_SECURE_SETTINGS); |
| 1124 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1125 | // Resolve the userId on whose behalf the call is made. |
| 1126 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId); |
| 1127 | |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 1128 | // If this is a setting that is currently restricted for this user, do not allow |
| 1129 | // unrestricting changes. |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1130 | if (name != null && isGlobalOrSecureSettingRestrictedForUser(name, callingUserId, value, |
Victor Chang | 9c7b706 | 2016-07-12 23:47:29 +0100 | [diff] [blame] | 1131 | Binder.getCallingUid())) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1132 | return false; |
| 1133 | } |
| 1134 | |
| 1135 | // Determine the owning user as some profile settings are cloned from the parent. |
| 1136 | final int owningUserId = resolveOwningUserIdForSecureSettingLocked(callingUserId, name); |
| 1137 | |
| 1138 | // Only the owning user can change the setting. |
| 1139 | if (owningUserId != callingUserId) { |
| 1140 | return false; |
| 1141 | } |
| 1142 | |
| 1143 | // Special cases for location providers (sigh). |
| 1144 | if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1145 | return updateLocationProvidersAllowedLocked(value, tag, owningUserId, makeDefault, |
| 1146 | forceNotify); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | // Mutate the value. |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1150 | synchronized (mLock) { |
| 1151 | switch (operation) { |
| 1152 | case MUTATION_OPERATION_INSERT: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1153 | return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1154 | owningUserId, name, value, tag, makeDefault, |
| 1155 | getCallingPackage(), forceNotify); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1156 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1157 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1158 | case MUTATION_OPERATION_DELETE: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1159 | return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SECURE, |
| 1160 | owningUserId, name, forceNotify); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1161 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1162 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1163 | case MUTATION_OPERATION_UPDATE: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1164 | return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SECURE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1165 | owningUserId, name, value, tag, makeDefault, |
| 1166 | getCallingPackage(), forceNotify); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1167 | } |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1168 | |
| 1169 | case MUTATION_OPERATION_RESET: { |
| 1170 | mSettingsRegistry.resetSettingsLocked(SETTINGS_TYPE_SECURE, |
| 1171 | UserHandle.USER_SYSTEM, getCallingPackage(), mode, tag); |
| 1172 | } return true; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | return false; |
| 1177 | } |
| 1178 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1179 | private Cursor getAllSystemSettings(int userId, String[] projection) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1180 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1181 | Slog.v(LOG_TAG, "getAllSecureSystem(" + userId + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | // Resolve the userId on whose behalf the call is made. |
| 1185 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(userId); |
| 1186 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1187 | synchronized (mLock) { |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1188 | List<String> names = getSettingsNamesLocked(SETTINGS_TYPE_SYSTEM, callingUserId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1189 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1190 | final int nameCount = names.size(); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1191 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1192 | String[] normalizedProjection = normalizeProjection(projection); |
| 1193 | MatrixCursor result = new MatrixCursor(normalizedProjection, nameCount); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1194 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1195 | for (int i = 0; i < nameCount; i++) { |
| 1196 | String name = names.get(i); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1197 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1198 | // Determine the owning user as some profile settings are cloned from the parent. |
| 1199 | final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, |
| 1200 | name); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1201 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1202 | Setting setting = mSettingsRegistry.getSettingLocked( |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1203 | SETTINGS_TYPE_SYSTEM, owningUserId, name); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1204 | appendSettingToCursor(result, setting); |
| 1205 | } |
| 1206 | |
| 1207 | return result; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1208 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1209 | } |
| 1210 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1211 | private Setting getSystemSetting(String name, int requestingUserId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1212 | if (DEBUG) { |
| 1213 | Slog.v(LOG_TAG, "getSystemSetting(" + name + ", " + requestingUserId + ")"); |
| 1214 | } |
| 1215 | |
| 1216 | // Resolve the userId on whose behalf the call is made. |
| 1217 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(requestingUserId); |
| 1218 | |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1219 | // Ensure the caller can access the setting. |
| 1220 | enforceSettingReadable(name, SETTINGS_TYPE_SYSTEM, callingUserId); |
| 1221 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1222 | // Determine the owning user as some profile settings are cloned from the parent. |
| 1223 | final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name); |
| 1224 | |
| 1225 | // Get the value. |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1226 | synchronized (mLock) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1227 | return mSettingsRegistry.getSettingLocked(SETTINGS_TYPE_SYSTEM, owningUserId, name); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1228 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1229 | } |
| 1230 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1231 | private boolean insertSystemSetting(String name, String value, int requestingUserId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1232 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1233 | Slog.v(LOG_TAG, "insertSystemSetting(" + name + ", " + value + ", " |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1234 | + requestingUserId + ")"); |
| 1235 | } |
| 1236 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1237 | return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_INSERT); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1238 | } |
| 1239 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1240 | private boolean deleteSystemSetting(String name, int requestingUserId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1241 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1242 | Slog.v(LOG_TAG, "deleteSystemSetting(" + name + ", " + requestingUserId + ")"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1243 | } |
| 1244 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1245 | return mutateSystemSetting(name, null, requestingUserId, MUTATION_OPERATION_DELETE); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1246 | } |
| 1247 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1248 | private boolean updateSystemSetting(String name, String value, int requestingUserId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1249 | if (DEBUG) { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1250 | Slog.v(LOG_TAG, "updateSystemSetting(" + name + ", " + value + ", " |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1251 | + requestingUserId + ")"); |
| 1252 | } |
| 1253 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1254 | return mutateSystemSetting(name, value, requestingUserId, MUTATION_OPERATION_UPDATE); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1255 | } |
| 1256 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1257 | private boolean mutateSystemSetting(String name, String value, int runAsUserId, |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1258 | int operation) { |
Billy Lau | 6ad2d66 | 2015-07-18 00:26:58 +0100 | [diff] [blame] | 1259 | if (!hasWriteSecureSettingsPermission()) { |
| 1260 | // If the caller doesn't hold WRITE_SECURE_SETTINGS, we verify whether this |
| 1261 | // operation is allowed for the calling package through appops. |
| 1262 | if (!Settings.checkAndNoteWriteSettingsOperation(getContext(), |
| 1263 | Binder.getCallingUid(), getCallingPackage(), true)) { |
| 1264 | return false; |
| 1265 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1266 | } |
| 1267 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1268 | // Resolve the userId on whose behalf the call is made. |
| 1269 | final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId); |
| 1270 | |
Svetoslav | d8d25e0 | 2015-11-20 13:09:26 -0800 | [diff] [blame] | 1271 | // Enforce what the calling package can mutate the system settings. |
| 1272 | enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name, callingUserId); |
| 1273 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1274 | // Determine the owning user as some profile settings are cloned from the parent. |
| 1275 | final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name); |
| 1276 | |
| 1277 | // Only the owning user id can change the setting. |
| 1278 | if (owningUserId != callingUserId) { |
| 1279 | return false; |
| 1280 | } |
| 1281 | |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 1282 | // Invalidate any relevant cache files |
| 1283 | String cacheName = null; |
| 1284 | if (Settings.System.RINGTONE.equals(name)) { |
| 1285 | cacheName = Settings.System.RINGTONE_CACHE; |
| 1286 | } else if (Settings.System.NOTIFICATION_SOUND.equals(name)) { |
| 1287 | cacheName = Settings.System.NOTIFICATION_SOUND_CACHE; |
| 1288 | } else if (Settings.System.ALARM_ALERT.equals(name)) { |
| 1289 | cacheName = Settings.System.ALARM_ALERT_CACHE; |
| 1290 | } |
| 1291 | if (cacheName != null) { |
| 1292 | final File cacheFile = new File( |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 1293 | getRingtoneCacheDir(owningUserId), cacheName); |
Jeff Sharkey | 413573a | 2016-02-22 17:52:45 -0700 | [diff] [blame] | 1294 | cacheFile.delete(); |
| 1295 | } |
| 1296 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1297 | // Mutate the value. |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1298 | synchronized (mLock) { |
| 1299 | switch (operation) { |
| 1300 | case MUTATION_OPERATION_INSERT: { |
| 1301 | validateSystemSettingValue(name, value); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1302 | return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SYSTEM, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1303 | owningUserId, name, value, null, false, getCallingPackage(), false); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | case MUTATION_OPERATION_DELETE: { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1307 | return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SYSTEM, |
| 1308 | owningUserId, name, false); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | case MUTATION_OPERATION_UPDATE: { |
| 1312 | validateSystemSettingValue(name, value); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1313 | return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SYSTEM, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1314 | owningUserId, name, value, null, false, getCallingPackage(), false); |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1315 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1316 | } |
| 1317 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1318 | return false; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1319 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1320 | } |
| 1321 | |
Billy Lau | 6ad2d66 | 2015-07-18 00:26:58 +0100 | [diff] [blame] | 1322 | private boolean hasWriteSecureSettingsPermission() { |
Svetoslav | f41334b | 2015-06-23 12:06:03 -0700 | [diff] [blame] | 1323 | // Write secure settings is a more protected permission. If caller has it we are good. |
| 1324 | if (getContext().checkCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1325 | == PackageManager.PERMISSION_GRANTED) { |
| 1326 | return true; |
| 1327 | } |
| 1328 | |
Svetoslav | f41334b | 2015-06-23 12:06:03 -0700 | [diff] [blame] | 1329 | return false; |
| 1330 | } |
| 1331 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1332 | private void validateSystemSettingValue(String name, String value) { |
| 1333 | Settings.System.Validator validator = Settings.System.VALIDATORS.get(name); |
| 1334 | if (validator != null && !validator.validate(value)) { |
| 1335 | throw new IllegalArgumentException("Invalid value: " + value |
| 1336 | + " for setting: " + name); |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | private boolean isLocationProvidersAllowedRestricted(String name, int callingUserId, |
| 1341 | int owningUserId) { |
| 1342 | // Optimization - location providers are restricted only for managed profiles. |
| 1343 | if (callingUserId == owningUserId) { |
| 1344 | return false; |
| 1345 | } |
| 1346 | if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name) |
| 1347 | && mUserManager.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, |
| 1348 | new UserHandle(callingUserId))) { |
| 1349 | return true; |
| 1350 | } |
| 1351 | return false; |
| 1352 | } |
| 1353 | |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 1354 | /** |
| 1355 | * Checks whether changing a setting to a value is prohibited by the corresponding user |
| 1356 | * restriction. |
| 1357 | * |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1358 | * <p>See also {@link com.android.server.pm.UserRestrictionsUtils#applyUserRestriction( |
| 1359 | * Context, int, String, boolean)}, which should be in sync with this method. |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 1360 | * |
| 1361 | * @return true if the change is prohibited, false if the change is allowed. |
| 1362 | */ |
| 1363 | private boolean isGlobalOrSecureSettingRestrictedForUser(String setting, int userId, |
Victor Chang | 9c7b706 | 2016-07-12 23:47:29 +0100 | [diff] [blame] | 1364 | String value, int callingUid) { |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 1365 | String restriction; |
| 1366 | switch (setting) { |
| 1367 | case Settings.Secure.LOCATION_MODE: |
| 1368 | // Note LOCATION_MODE will be converted into LOCATION_PROVIDERS_ALLOWED |
| 1369 | // in android.provider.Settings.Secure.putStringForUser(), so we shouldn't come |
| 1370 | // here normally, but we still protect it here from a direct provider write. |
| 1371 | if (String.valueOf(Settings.Secure.LOCATION_MODE_OFF).equals(value)) return false; |
| 1372 | restriction = UserManager.DISALLOW_SHARE_LOCATION; |
| 1373 | break; |
| 1374 | |
| 1375 | case Settings.Secure.LOCATION_PROVIDERS_ALLOWED: |
| 1376 | // See SettingsProvider.updateLocationProvidersAllowedLocked. "-" is to disable |
| 1377 | // a provider, which should be allowed even if the user restriction is set. |
| 1378 | if (value != null && value.startsWith("-")) return false; |
| 1379 | restriction = UserManager.DISALLOW_SHARE_LOCATION; |
| 1380 | break; |
| 1381 | |
| 1382 | case Settings.Secure.INSTALL_NON_MARKET_APPS: |
| 1383 | if ("0".equals(value)) return false; |
| 1384 | restriction = UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES; |
| 1385 | break; |
| 1386 | |
| 1387 | case Settings.Global.ADB_ENABLED: |
| 1388 | if ("0".equals(value)) return false; |
| 1389 | restriction = UserManager.DISALLOW_DEBUGGING_FEATURES; |
| 1390 | break; |
| 1391 | |
| 1392 | case Settings.Global.PACKAGE_VERIFIER_ENABLE: |
| 1393 | case Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB: |
| 1394 | if ("1".equals(value)) return false; |
| 1395 | restriction = UserManager.ENSURE_VERIFY_APPS; |
| 1396 | break; |
| 1397 | |
| 1398 | case Settings.Global.PREFERRED_NETWORK_MODE: |
| 1399 | restriction = UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS; |
| 1400 | break; |
| 1401 | |
Victor Chang | 9c7b706 | 2016-07-12 23:47:29 +0100 | [diff] [blame] | 1402 | case Settings.Secure.ALWAYS_ON_VPN_APP: |
| 1403 | case Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN: |
| 1404 | // Whitelist system uid (ConnectivityService) and root uid to change always-on vpn |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1405 | final int appId = UserHandle.getAppId(callingUid); |
| 1406 | if (appId == Process.SYSTEM_UID || appId == Process.ROOT_UID) { |
Victor Chang | 9c7b706 | 2016-07-12 23:47:29 +0100 | [diff] [blame] | 1407 | return false; |
| 1408 | } |
| 1409 | restriction = UserManager.DISALLOW_CONFIG_VPN; |
| 1410 | break; |
| 1411 | |
Benjamin Franz | 0ff13fc | 2016-07-12 13:42:21 +0100 | [diff] [blame] | 1412 | case Settings.Global.SAFE_BOOT_DISALLOWED: |
| 1413 | if ("1".equals(value)) return false; |
| 1414 | restriction = UserManager.DISALLOW_SAFE_BOOT; |
| 1415 | break; |
| 1416 | |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 1417 | default: |
Mahaver Chopra | 8764875 | 2016-01-08 19:23:57 +0000 | [diff] [blame] | 1418 | if (setting != null && setting.startsWith(Settings.Global.DATA_ROAMING)) { |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 1419 | if ("0".equals(value)) return false; |
| 1420 | restriction = UserManager.DISALLOW_DATA_ROAMING; |
| 1421 | break; |
| 1422 | } |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 1423 | return false; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1424 | } |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 1425 | |
| 1426 | return mUserManager.hasUserRestriction(restriction, UserHandle.of(userId)); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | private int resolveOwningUserIdForSecureSettingLocked(int userId, String setting) { |
| 1430 | return resolveOwningUserIdLocked(userId, sSecureCloneToManagedSettings, setting); |
| 1431 | } |
| 1432 | |
| 1433 | private int resolveOwningUserIdForSystemSettingLocked(int userId, String setting) { |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 1434 | final int parentId; |
| 1435 | // Resolves dependency if setting has a dependency and the calling user has a parent |
| 1436 | if (sSystemCloneFromParentOnDependency.containsKey(setting) |
| 1437 | && (parentId = getGroupParentLocked(userId)) != userId) { |
| 1438 | // The setting has a dependency and the profile has a parent |
| 1439 | String dependency = sSystemCloneFromParentOnDependency.get(setting); |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1440 | // Lookup the dependency setting as ourselves, some callers may not have access to it. |
| 1441 | final long token = Binder.clearCallingIdentity(); |
| 1442 | try { |
| 1443 | Setting settingObj = getSecureSetting(dependency, userId); |
| 1444 | if (settingObj != null && settingObj.getValue().equals("1")) { |
| 1445 | return parentId; |
| 1446 | } |
| 1447 | } finally { |
| 1448 | Binder.restoreCallingIdentity(token); |
Andre Lago | 3fa139c | 2016-08-04 13:53:44 +0100 | [diff] [blame] | 1449 | } |
| 1450 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1451 | return resolveOwningUserIdLocked(userId, sSystemCloneToManagedSettings, setting); |
| 1452 | } |
| 1453 | |
| 1454 | private int resolveOwningUserIdLocked(int userId, Set<String> keys, String name) { |
| 1455 | final int parentId = getGroupParentLocked(userId); |
| 1456 | if (parentId != userId && keys.contains(name)) { |
| 1457 | return parentId; |
| 1458 | } |
| 1459 | return userId; |
| 1460 | } |
| 1461 | |
Svetoslav | f41334b | 2015-06-23 12:06:03 -0700 | [diff] [blame] | 1462 | private void enforceRestrictedSystemSettingsMutationForCallingPackage(int operation, |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1463 | String name, int userId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1464 | // System/root/shell can mutate whatever secure settings they want. |
| 1465 | final int callingUid = Binder.getCallingUid(); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1466 | final int appId = UserHandle.getAppId(callingUid); |
| 1467 | if (appId == android.os.Process.SYSTEM_UID |
| 1468 | || appId == Process.SHELL_UID |
| 1469 | || appId == Process.ROOT_UID) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1470 | return; |
| 1471 | } |
| 1472 | |
| 1473 | switch (operation) { |
| 1474 | case MUTATION_OPERATION_INSERT: |
| 1475 | // Insert updates. |
| 1476 | case MUTATION_OPERATION_UPDATE: { |
| 1477 | if (Settings.System.PUBLIC_SETTINGS.contains(name)) { |
| 1478 | return; |
| 1479 | } |
| 1480 | |
| 1481 | // The calling package is already verified. |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1482 | PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1483 | |
| 1484 | // Privileged apps can do whatever they want. |
| 1485 | if ((packageInfo.applicationInfo.privateFlags |
| 1486 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
| 1487 | return; |
| 1488 | } |
| 1489 | |
| 1490 | warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk( |
| 1491 | packageInfo.applicationInfo.targetSdkVersion, name); |
| 1492 | } break; |
| 1493 | |
| 1494 | case MUTATION_OPERATION_DELETE: { |
| 1495 | if (Settings.System.PUBLIC_SETTINGS.contains(name) |
| 1496 | || Settings.System.PRIVATE_SETTINGS.contains(name)) { |
| 1497 | throw new IllegalArgumentException("You cannot delete system defined" |
| 1498 | + " secure settings."); |
| 1499 | } |
| 1500 | |
| 1501 | // The calling package is already verified. |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1502 | PackageInfo packageInfo = getCallingPackageInfoOrThrow(userId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1503 | |
| 1504 | // Privileged apps can do whatever they want. |
| 1505 | if ((packageInfo.applicationInfo.privateFlags & |
| 1506 | ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
| 1507 | return; |
| 1508 | } |
| 1509 | |
| 1510 | warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk( |
| 1511 | packageInfo.applicationInfo.targetSdkVersion, name); |
| 1512 | } break; |
| 1513 | } |
| 1514 | } |
| 1515 | |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1516 | private Set<String> getEphemeralAccessibleSettings(int settingsType) { |
| 1517 | switch (settingsType) { |
| 1518 | case SETTINGS_TYPE_GLOBAL: |
| 1519 | return Settings.Global.EPHEMERAL_SETTINGS; |
| 1520 | case SETTINGS_TYPE_SECURE: |
| 1521 | return Settings.Secure.EPHEMERAL_SETTINGS; |
| 1522 | case SETTINGS_TYPE_SYSTEM: |
| 1523 | return Settings.System.EPHEMERAL_SETTINGS; |
| 1524 | default: |
| 1525 | throw new IllegalArgumentException("Invalid settings type: " + settingsType); |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | private List<String> getSettingsNamesLocked(int settingsType, int userId) { |
| 1530 | ApplicationInfo ai = getCallingApplicationInfoOrThrow(userId); |
| 1531 | if (ai.isEphemeralApp()) { |
| 1532 | return new ArrayList<String>(getEphemeralAccessibleSettings(settingsType)); |
| 1533 | } else { |
| 1534 | return mSettingsRegistry.getSettingsNamesLocked(settingsType, userId); |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | private void enforceSettingReadable(String settingName, int settingsType, int userId) { |
| 1539 | if (UserHandle.getAppId(Binder.getCallingUid()) < Process.FIRST_APPLICATION_UID) { |
| 1540 | return; |
| 1541 | } |
| 1542 | ApplicationInfo ai = getCallingApplicationInfoOrThrow(userId); |
| 1543 | if (!ai.isEphemeralApp()) { |
| 1544 | return; |
| 1545 | } |
| 1546 | if (!getEphemeralAccessibleSettings(settingsType).contains(settingName)) { |
| 1547 | throw new SecurityException("Setting " + settingName + " is not accessible from" |
| 1548 | + " ephemeral package " + getCallingPackage()); |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | private ApplicationInfo getCallingApplicationInfoOrThrow(int userId) { |
| 1553 | ApplicationInfo ai = null; |
| 1554 | try { |
| 1555 | ai = mPackageManager.getApplicationInfo(getCallingPackage(), 0 , userId); |
| 1556 | } catch (RemoteException ignored) { |
| 1557 | } |
| 1558 | if (ai == null) { |
| 1559 | throw new IllegalStateException("Failed to lookup info for package " |
| 1560 | + getCallingPackage()); |
| 1561 | } |
| 1562 | return ai; |
| 1563 | } |
| 1564 | |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1565 | private PackageInfo getCallingPackageInfoOrThrow(int userId) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1566 | try { |
Svetoslav Ganov | 67a8d35 | 2016-03-02 13:26:40 -0800 | [diff] [blame] | 1567 | PackageInfo packageInfo = mPackageManager.getPackageInfo( |
| 1568 | getCallingPackage(), 0, userId); |
| 1569 | if (packageInfo != null) { |
| 1570 | return packageInfo; |
| 1571 | } |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1572 | } catch (RemoteException e) { |
Svetoslav Ganov | 67a8d35 | 2016-03-02 13:26:40 -0800 | [diff] [blame] | 1573 | /* ignore */ |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1574 | } |
Svetoslav Ganov | 67a8d35 | 2016-03-02 13:26:40 -0800 | [diff] [blame] | 1575 | throw new IllegalStateException("Calling package doesn't exist"); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | private int getGroupParentLocked(int userId) { |
| 1579 | // Most frequent use case. |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 1580 | if (userId == UserHandle.USER_SYSTEM) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1581 | return userId; |
| 1582 | } |
| 1583 | // We are in the same process with the user manager and the returned |
| 1584 | // user info is a cached instance, so just look up instead of cache. |
| 1585 | final long identity = Binder.clearCallingIdentity(); |
| 1586 | try { |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1587 | // Just a lookup and not reentrant, so holding a lock is fine. |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1588 | UserInfo userInfo = mUserManager.getProfileParent(userId); |
| 1589 | return (userInfo != null) ? userInfo.id : userId; |
| 1590 | } finally { |
| 1591 | Binder.restoreCallingIdentity(identity); |
| 1592 | } |
| 1593 | } |
| 1594 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1595 | private void enforceWritePermission(String permission) { |
| 1596 | if (getContext().checkCallingOrSelfPermission(permission) |
| 1597 | != PackageManager.PERMISSION_GRANTED) { |
| 1598 | throw new SecurityException("Permission denial: writing to settings requires:" |
| 1599 | + permission); |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | /* |
| 1604 | * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED. |
| 1605 | * This setting contains a list of the currently enabled location providers. |
| 1606 | * But helper functions in android.providers.Settings can enable or disable |
| 1607 | * a single provider by using a "+" or "-" prefix before the provider name. |
| 1608 | * |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 1609 | * <p>See also {@link #isGlobalOrSecureSettingRestrictedForUser()}. If DISALLOW_SHARE_LOCATION |
| 1610 | * is set, the said method will only allow values with the "-" prefix. |
| 1611 | * |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1612 | * @returns whether the enabled location providers changed. |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 1613 | */ |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1614 | private boolean updateLocationProvidersAllowedLocked(String value, String tag, |
| 1615 | int owningUserId, boolean makeDefault, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1616 | if (TextUtils.isEmpty(value)) { |
| 1617 | return false; |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 1618 | } |
| 1619 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1620 | final char prefix = value.charAt(0); |
| 1621 | if (prefix != '+' && prefix != '-') { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1622 | if (forceNotify) { |
| 1623 | final int key = makeKey(SETTINGS_TYPE_SECURE, owningUserId); |
| 1624 | mSettingsRegistry.notifyForSettingsChange(key, |
| 1625 | Settings.Secure.LOCATION_PROVIDERS_ALLOWED); |
| 1626 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1627 | return false; |
| 1628 | } |
| 1629 | |
| 1630 | // skip prefix |
| 1631 | value = value.substring(1); |
| 1632 | |
Svetoslav | 7ec28e8 | 2015-05-20 17:01:10 -0700 | [diff] [blame] | 1633 | Setting settingValue = getSecureSetting( |
Chad Brubaker | 97bccee | 2017-01-05 15:51:41 -0800 | [diff] [blame] | 1634 | Settings.Secure.LOCATION_PROVIDERS_ALLOWED, owningUserId); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 1635 | if (settingValue == null) { |
| 1636 | return false; |
| 1637 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1638 | |
| 1639 | String oldProviders = (settingValue != null) ? settingValue.getValue() : ""; |
| 1640 | |
| 1641 | int index = oldProviders.indexOf(value); |
| 1642 | int end = index + value.length(); |
| 1643 | |
| 1644 | // check for commas to avoid matching on partial string |
| 1645 | if (index > 0 && oldProviders.charAt(index - 1) != ',') { |
| 1646 | index = -1; |
| 1647 | } |
| 1648 | |
| 1649 | // check for commas to avoid matching on partial string |
| 1650 | if (end < oldProviders.length() && oldProviders.charAt(end) != ',') { |
| 1651 | index = -1; |
| 1652 | } |
| 1653 | |
| 1654 | String newProviders; |
| 1655 | |
| 1656 | if (prefix == '+' && index < 0) { |
| 1657 | // append the provider to the list if not present |
| 1658 | if (oldProviders.length() == 0) { |
| 1659 | newProviders = value; |
| 1660 | } else { |
| 1661 | newProviders = oldProviders + ',' + value; |
| 1662 | } |
| 1663 | } else if (prefix == '-' && index >= 0) { |
| 1664 | // remove the provider from the list if present |
| 1665 | // remove leading or trailing comma |
| 1666 | if (index > 0) { |
| 1667 | index--; |
| 1668 | } else if (end < oldProviders.length()) { |
| 1669 | end++; |
| 1670 | } |
| 1671 | |
| 1672 | newProviders = oldProviders.substring(0, index); |
| 1673 | if (end < oldProviders.length()) { |
| 1674 | newProviders += oldProviders.substring(end); |
| 1675 | } |
| 1676 | } else { |
| 1677 | // nothing changed, so no need to update the database |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1678 | if (forceNotify) { |
| 1679 | final int key = makeKey(SETTINGS_TYPE_SECURE, owningUserId); |
| 1680 | mSettingsRegistry.notifyForSettingsChange(key, |
| 1681 | Settings.Secure.LOCATION_PROVIDERS_ALLOWED); |
| 1682 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1683 | return false; |
| 1684 | } |
| 1685 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1686 | return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SECURE, |
Svetoslav | b596a2c | 2015-02-17 21:37:09 -0800 | [diff] [blame] | 1687 | owningUserId, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, newProviders, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1688 | tag, makeDefault, getCallingPackage(), forceNotify); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1689 | } |
| 1690 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1691 | private static void warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk( |
| 1692 | int targetSdkVersion, String name) { |
| 1693 | // If the app targets Lollipop MR1 or older SDK we warn, otherwise crash. |
| 1694 | if (targetSdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) { |
| 1695 | if (Settings.System.PRIVATE_SETTINGS.contains(name)) { |
| 1696 | Slog.w(LOG_TAG, "You shouldn't not change private system settings." |
| 1697 | + " This will soon become an error."); |
| 1698 | } else { |
| 1699 | Slog.w(LOG_TAG, "You shouldn't keep your settings in the secure settings." |
| 1700 | + " This will soon become an error."); |
| 1701 | } |
| 1702 | } else { |
| 1703 | if (Settings.System.PRIVATE_SETTINGS.contains(name)) { |
| 1704 | throw new IllegalArgumentException("You cannot change private secure settings."); |
| 1705 | } else { |
| 1706 | throw new IllegalArgumentException("You cannot keep your settings in" |
| 1707 | + " the secure settings."); |
| 1708 | } |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | private static int resolveCallingUserIdEnforcingPermissionsLocked(int requestingUserId) { |
| 1713 | if (requestingUserId == UserHandle.getCallingUserId()) { |
| 1714 | return requestingUserId; |
| 1715 | } |
| 1716 | return ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1717 | Binder.getCallingUid(), requestingUserId, false, true, |
| 1718 | "get/set setting for user", null); |
| 1719 | } |
| 1720 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1721 | private Bundle packageValueForCallResult(Setting setting, |
| 1722 | boolean trackingGeneration) { |
| 1723 | if (!trackingGeneration) { |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 1724 | if (setting == null || setting.isNull()) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1725 | return NULL_SETTING_BUNDLE; |
| 1726 | } |
| 1727 | return Bundle.forPair(Settings.NameValueTable.VALUE, setting.getValue()); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1728 | } |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1729 | Bundle result = new Bundle(); |
| 1730 | result.putString(Settings.NameValueTable.VALUE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1731 | !setting.isNull() ? setting.getValue() : null); |
| 1732 | mSettingsRegistry.mGenerationRegistry.addGenerationData(result, setting.getKey()); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1733 | return result; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | private static int getRequestingUserId(Bundle args) { |
| 1737 | final int callingUserId = UserHandle.getCallingUserId(); |
| 1738 | return (args != null) ? args.getInt(Settings.CALL_METHOD_USER_KEY, callingUserId) |
| 1739 | : callingUserId; |
| 1740 | } |
| 1741 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1742 | private boolean isTrackingGeneration(Bundle args) { |
| 1743 | return args != null && args.containsKey(Settings.CALL_METHOD_TRACK_GENERATION_KEY); |
| 1744 | } |
| 1745 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1746 | private static String getSettingValue(Bundle args) { |
| 1747 | return (args != null) ? args.getString(Settings.NameValueTable.VALUE) : null; |
| 1748 | } |
| 1749 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 1750 | private static String getSettingTag(Bundle args) { |
| 1751 | return (args != null) ? args.getString(Settings.CALL_METHOD_TAG_KEY) : null; |
| 1752 | } |
| 1753 | |
| 1754 | private static boolean getSettingMakeDefault(Bundle args) { |
| 1755 | return (args != null) && args.getBoolean(Settings.CALL_METHOD_MAKE_DEFAULT_KEY); |
| 1756 | } |
| 1757 | |
| 1758 | private static int getResetModeEnforcingPermission(Bundle args) { |
| 1759 | final int mode = (args != null) ? args.getInt(Settings.CALL_METHOD_RESET_MODE_KEY) : 0; |
| 1760 | switch (mode) { |
| 1761 | case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: { |
| 1762 | if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) { |
| 1763 | throw new SecurityException("Only system, shell/root on a " |
| 1764 | + "debuggable build can reset to untrusted defaults"); |
| 1765 | } |
| 1766 | return mode; |
| 1767 | } |
| 1768 | case Settings.RESET_MODE_UNTRUSTED_CHANGES: { |
| 1769 | if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) { |
| 1770 | throw new SecurityException("Only system, shell/root on a " |
| 1771 | + "debuggable build can reset untrusted changes"); |
| 1772 | } |
| 1773 | return mode; |
| 1774 | } |
| 1775 | case Settings.RESET_MODE_TRUSTED_DEFAULTS: { |
| 1776 | if (!isCallerSystemOrShellOrRootOnDebuggableBuild()) { |
| 1777 | throw new SecurityException("Only system, shell/root on a " |
| 1778 | + "debuggable build can reset to trusted defaults"); |
| 1779 | } |
| 1780 | return mode; |
| 1781 | } |
| 1782 | case Settings.RESET_MODE_PACKAGE_DEFAULTS: { |
| 1783 | return mode; |
| 1784 | } |
| 1785 | } |
| 1786 | throw new IllegalArgumentException("Invalid reset mode: " + mode); |
| 1787 | } |
| 1788 | |
| 1789 | private static boolean isCallerSystemOrShellOrRootOnDebuggableBuild() { |
| 1790 | final int appId = UserHandle.getAppId(Binder.getCallingUid()); |
| 1791 | return appId == SYSTEM_UID || (Build.IS_DEBUGGABLE |
| 1792 | && (appId == SHELL_UID || appId == ROOT_UID)); |
| 1793 | } |
| 1794 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1795 | private static String getValidTableOrThrow(Uri uri) { |
| 1796 | if (uri.getPathSegments().size() > 0) { |
| 1797 | String table = uri.getPathSegments().get(0); |
| 1798 | if (DatabaseHelper.isValidTable(table)) { |
| 1799 | return table; |
| 1800 | } |
| 1801 | throw new IllegalArgumentException("Bad root path: " + table); |
| 1802 | } |
| 1803 | throw new IllegalArgumentException("Invalid URI:" + uri); |
| 1804 | } |
| 1805 | |
| 1806 | private static MatrixCursor packageSettingForQuery(Setting setting, String[] projection) { |
Svetoslav Ganov | fedb230 | 2016-04-26 18:36:42 -0700 | [diff] [blame] | 1807 | if (setting.isNull()) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1808 | return new MatrixCursor(projection, 0); |
| 1809 | } |
| 1810 | MatrixCursor cursor = new MatrixCursor(projection, 1); |
| 1811 | appendSettingToCursor(cursor, setting); |
| 1812 | return cursor; |
| 1813 | } |
| 1814 | |
| 1815 | private static String[] normalizeProjection(String[] projection) { |
| 1816 | if (projection == null) { |
| 1817 | return ALL_COLUMNS; |
| 1818 | } |
| 1819 | |
| 1820 | final int columnCount = projection.length; |
| 1821 | for (int i = 0; i < columnCount; i++) { |
| 1822 | String column = projection[i]; |
| 1823 | if (!ArrayUtils.contains(ALL_COLUMNS, column)) { |
| 1824 | throw new IllegalArgumentException("Invalid column: " + column); |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 1825 | } |
| 1826 | } |
| 1827 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1828 | return projection; |
| 1829 | } |
| 1830 | |
| 1831 | private static void appendSettingToCursor(MatrixCursor cursor, Setting setting) { |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 1832 | if (setting == null || setting.isNull()) { |
Suprabh Shukla | c9d064a | 2016-04-12 18:45:34 -0700 | [diff] [blame] | 1833 | return; |
| 1834 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1835 | final int columnCount = cursor.getColumnCount(); |
| 1836 | |
| 1837 | String[] values = new String[columnCount]; |
| 1838 | |
| 1839 | for (int i = 0; i < columnCount; i++) { |
| 1840 | String column = cursor.getColumnName(i); |
| 1841 | |
| 1842 | switch (column) { |
| 1843 | case Settings.NameValueTable._ID: { |
| 1844 | values[i] = setting.getId(); |
| 1845 | } break; |
| 1846 | |
| 1847 | case Settings.NameValueTable.NAME: { |
| 1848 | values[i] = setting.getName(); |
| 1849 | } break; |
| 1850 | |
| 1851 | case Settings.NameValueTable.VALUE: { |
| 1852 | values[i] = setting.getValue(); |
| 1853 | } break; |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 1854 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 1855 | } |
| 1856 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1857 | cursor.addRow(values); |
| 1858 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 1859 | |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 1860 | private static boolean isKeyValid(String key) { |
| 1861 | return !(TextUtils.isEmpty(key) || SettingsState.isBinary(key)); |
| 1862 | } |
| 1863 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1864 | private static final class Arguments { |
| 1865 | private static final Pattern WHERE_PATTERN_WITH_PARAM_NO_BRACKETS = |
| 1866 | Pattern.compile("[\\s]*name[\\s]*=[\\s]*\\?[\\s]*"); |
| 1867 | |
| 1868 | private static final Pattern WHERE_PATTERN_WITH_PARAM_IN_BRACKETS = |
| 1869 | Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*\\?[\\s]*\\)[\\s]*"); |
| 1870 | |
| 1871 | private static final Pattern WHERE_PATTERN_NO_PARAM_IN_BRACKETS = |
| 1872 | Pattern.compile("[\\s]*\\([\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*\\)[\\s]*"); |
| 1873 | |
| 1874 | private static final Pattern WHERE_PATTERN_NO_PARAM_NO_BRACKETS = |
| 1875 | Pattern.compile("[\\s]*name[\\s]*=[\\s]*['\"].*['\"][\\s]*"); |
| 1876 | |
| 1877 | public final String table; |
| 1878 | public final String name; |
| 1879 | |
| 1880 | public Arguments(Uri uri, String where, String[] whereArgs, boolean supportAll) { |
| 1881 | final int segmentSize = uri.getPathSegments().size(); |
| 1882 | switch (segmentSize) { |
| 1883 | case 1: { |
| 1884 | if (where != null |
| 1885 | && (WHERE_PATTERN_WITH_PARAM_NO_BRACKETS.matcher(where).matches() |
| 1886 | || WHERE_PATTERN_WITH_PARAM_IN_BRACKETS.matcher(where).matches()) |
| 1887 | && whereArgs.length == 1) { |
| 1888 | name = whereArgs[0]; |
| 1889 | table = computeTableForSetting(uri, name); |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 1890 | return; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1891 | } else if (where != null |
| 1892 | && (WHERE_PATTERN_NO_PARAM_NO_BRACKETS.matcher(where).matches() |
| 1893 | || WHERE_PATTERN_NO_PARAM_IN_BRACKETS.matcher(where).matches())) { |
| 1894 | final int startIndex = Math.max(where.indexOf("'"), |
| 1895 | where.indexOf("\"")) + 1; |
| 1896 | final int endIndex = Math.max(where.lastIndexOf("'"), |
| 1897 | where.lastIndexOf("\"")); |
| 1898 | name = where.substring(startIndex, endIndex); |
| 1899 | table = computeTableForSetting(uri, name); |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 1900 | return; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1901 | } else if (supportAll && where == null && whereArgs == null) { |
| 1902 | name = null; |
| 1903 | table = computeTableForSetting(uri, null); |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 1904 | return; |
Brad Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame] | 1905 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1906 | } break; |
| 1907 | |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 1908 | case 2: { |
| 1909 | if (where == null && whereArgs == null) { |
| 1910 | name = uri.getPathSegments().get(1); |
| 1911 | table = computeTableForSetting(uri, name); |
| 1912 | return; |
| 1913 | } |
| 1914 | } break; |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 1915 | } |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 1916 | |
| 1917 | EventLogTags.writeUnsupportedSettingsQuery( |
| 1918 | uri.toSafeString(), where, Arrays.toString(whereArgs)); |
| 1919 | String message = String.format( "Supported SQL:\n" |
| 1920 | + " uri content://some_table/some_property with null where and where args\n" |
| 1921 | + " uri content://some_table with query name=? and single name as arg\n" |
| 1922 | + " uri content://some_table with query name=some_name and null args\n" |
| 1923 | + " but got - uri:%1s, where:%2s whereArgs:%3s", uri, where, |
| 1924 | Arrays.toString(whereArgs)); |
| 1925 | throw new IllegalArgumentException(message); |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 1926 | } |
| 1927 | |
Svetoslav | 2849465 | 2015-02-12 14:11:42 -0800 | [diff] [blame] | 1928 | private static String computeTableForSetting(Uri uri, String name) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1929 | String table = getValidTableOrThrow(uri); |
| 1930 | |
| 1931 | if (name != null) { |
| 1932 | if (sSystemMovedToSecureSettings.contains(name)) { |
| 1933 | table = TABLE_SECURE; |
| 1934 | } |
| 1935 | |
| 1936 | if (sSystemMovedToGlobalSettings.contains(name)) { |
| 1937 | table = TABLE_GLOBAL; |
| 1938 | } |
| 1939 | |
| 1940 | if (sSecureMovedToGlobalSettings.contains(name)) { |
| 1941 | table = TABLE_GLOBAL; |
| 1942 | } |
| 1943 | |
| 1944 | if (sGlobalMovedToSecureSettings.contains(name)) { |
| 1945 | table = TABLE_SECURE; |
| 1946 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 1947 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1948 | |
| 1949 | return table; |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | final class SettingsRegistry { |
| 1954 | private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid"; |
| 1955 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1956 | private static final String SETTINGS_FILE_GLOBAL = "settings_global.xml"; |
| 1957 | private static final String SETTINGS_FILE_SYSTEM = "settings_system.xml"; |
| 1958 | private static final String SETTINGS_FILE_SECURE = "settings_secure.xml"; |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1959 | private static final String SETTINGS_FILE_SSAID = "settings_ssaid.xml"; |
| 1960 | |
| 1961 | private static final String SSAID_USER_KEY = "userkey"; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1962 | |
| 1963 | private final SparseArray<SettingsState> mSettingsStates = new SparseArray<>(); |
| 1964 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1965 | private GenerationRegistry mGenerationRegistry; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1966 | |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 1967 | private final Handler mHandler; |
| 1968 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1969 | private final BackupManager mBackupManager; |
| 1970 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1971 | public SettingsRegistry() { |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 1972 | mHandler = new MyHandler(getContext().getMainLooper()); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1973 | mGenerationRegistry = new GenerationRegistry(mLock); |
| 1974 | mBackupManager = new BackupManager(getContext()); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 1975 | migrateAllLegacySettingsIfNeeded(); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 1976 | syncSsaidTableOnStart(); |
| 1977 | } |
| 1978 | |
| 1979 | private void generateUserKeyLocked(int userId) { |
| 1980 | // Generate a random key for each user used for creating a new ssaid. |
| 1981 | final byte[] keyBytes = new byte[16]; |
| 1982 | final SecureRandom rand = new SecureRandom(); |
| 1983 | rand.nextBytes(keyBytes); |
| 1984 | |
| 1985 | // Convert to string for storage in settings table. |
| 1986 | final String userKey = ByteStringUtils.toString(keyBytes); |
| 1987 | |
| 1988 | // Store the key in the ssaid table. |
| 1989 | final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId); |
| 1990 | final boolean success = ssaidSettings.insertSettingLocked(SSAID_USER_KEY, userKey, null, |
| 1991 | true, SettingsState.SYSTEM_PACKAGE_NAME); |
| 1992 | |
| 1993 | if (!success) { |
| 1994 | throw new IllegalStateException("Ssaid settings not accessible"); |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | public Setting generateSsaidLocked(String packageName, int userId) { |
| 1999 | final PackageInfo packageInfo; |
| 2000 | try { |
| 2001 | packageInfo = mPackageManager.getPackageInfo(packageName, |
| 2002 | PackageManager.GET_SIGNATURES, userId); |
| 2003 | } catch (RemoteException e) { |
| 2004 | throw new IllegalStateException("Package info doesn't exist"); |
| 2005 | } |
| 2006 | |
| 2007 | // Read the user's key from the ssaid table. |
| 2008 | Setting userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY); |
Mark Rathjen | ea61759 | 2017-01-18 23:03:41 -0800 | [diff] [blame] | 2009 | if (userKeySetting == null || userKeySetting.isNull() |
| 2010 | || userKeySetting.getValue() == null) { |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2011 | // Lazy initialize and store the user key. |
| 2012 | generateUserKeyLocked(userId); |
| 2013 | userKeySetting = getSettingLocked(SETTINGS_TYPE_SSAID, userId, SSAID_USER_KEY); |
Mark Rathjen | ea61759 | 2017-01-18 23:03:41 -0800 | [diff] [blame] | 2014 | if (userKeySetting == null || userKeySetting.isNull() |
| 2015 | || userKeySetting.getValue() == null) { |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2016 | throw new IllegalStateException("User key not accessible"); |
| 2017 | } |
| 2018 | } |
| 2019 | final String userKey = userKeySetting.getValue(); |
| 2020 | |
| 2021 | // Convert the user's key back to a byte array. |
| 2022 | final byte[] keyBytes = ByteStringUtils.toByteArray(userKey); |
| 2023 | if (keyBytes == null || keyBytes.length != 16) { |
| 2024 | throw new IllegalStateException("User key invalid"); |
| 2025 | } |
| 2026 | |
| 2027 | final MessageDigest md; |
| 2028 | try { |
| 2029 | // Hash package name and signature. |
| 2030 | md = MessageDigest.getInstance("SHA-256"); |
| 2031 | } catch (NoSuchAlgorithmException e) { |
| 2032 | throw new IllegalStateException("HmacSHA256 is not available"); |
| 2033 | } |
| 2034 | md.update(keyBytes); |
| 2035 | md.update(packageInfo.packageName.getBytes(StandardCharsets.UTF_8)); |
| 2036 | md.update(packageInfo.signatures[0].toByteArray()); |
| 2037 | |
| 2038 | // Convert result to a string for storage in settings table. Only want first 64 bits. |
| 2039 | final String ssaid = ByteStringUtils.toString(md.digest()).substring(0, 16) |
| 2040 | .toLowerCase(); |
| 2041 | |
| 2042 | // Save the ssaid in the ssaid table. |
| 2043 | final String uid = Integer.toString(packageInfo.applicationInfo.uid); |
| 2044 | final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, userId); |
| 2045 | final boolean success = ssaidSettings.insertSettingLocked(uid, ssaid, null, true, |
| 2046 | packageName); |
| 2047 | |
| 2048 | if (!success) { |
| 2049 | throw new IllegalStateException("Ssaid settings not accessible"); |
| 2050 | } |
| 2051 | |
| 2052 | return getSettingLocked(SETTINGS_TYPE_SSAID, userId, uid); |
| 2053 | } |
| 2054 | |
| 2055 | public void syncSsaidTableOnStart() { |
| 2056 | synchronized (mLock) { |
| 2057 | // Verify that each user's packages and ssaid's are in sync. |
| 2058 | for (UserInfo user : mUserManager.getUsers(true)) { |
| 2059 | // Get all uids for the user's packages. |
| 2060 | final List<PackageInfo> packages; |
| 2061 | try { |
| 2062 | packages = mPackageManager.getInstalledPackages(0, user.id).getList(); |
| 2063 | } catch (RemoteException e) { |
| 2064 | throw new IllegalStateException("Package manager not available"); |
| 2065 | } |
| 2066 | final Set<String> appUids = new HashSet<>(); |
| 2067 | for (PackageInfo info : packages) { |
| 2068 | appUids.add(Integer.toString(info.applicationInfo.uid)); |
| 2069 | } |
| 2070 | |
| 2071 | // Get all uids currently stored in the user's ssaid table. |
| 2072 | final Set<String> ssaidUids = new HashSet<>( |
| 2073 | getSettingsNamesLocked(SETTINGS_TYPE_SSAID, user.id)); |
| 2074 | ssaidUids.remove(SSAID_USER_KEY); |
| 2075 | |
| 2076 | // Perform a set difference for the appUids and ssaidUids. |
| 2077 | ssaidUids.removeAll(appUids); |
| 2078 | |
| 2079 | // If there are ssaidUids left over they need to be removed from the table. |
| 2080 | final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, |
| 2081 | user.id); |
| 2082 | for (String uid : ssaidUids) { |
| 2083 | ssaidSettings.deleteSettingLocked(uid); |
| 2084 | } |
| 2085 | } |
| 2086 | } |
Brad Fitzpatrick | f366a9b | 2010-08-24 16:14:07 -0700 | [diff] [blame] | 2087 | } |
| 2088 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2089 | public List<String> getSettingsNamesLocked(int type, int userId) { |
| 2090 | final int key = makeKey(type, userId); |
| 2091 | SettingsState settingsState = peekSettingsStateLocked(key); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2092 | if (settingsState == null) { |
| 2093 | return new ArrayList<String>(); |
| 2094 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2095 | return settingsState.getSettingNamesLocked(); |
| 2096 | } |
| 2097 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2098 | public SparseBooleanArray getKnownUsersLocked() { |
| 2099 | SparseBooleanArray users = new SparseBooleanArray(); |
| 2100 | for (int i = mSettingsStates.size()-1; i >= 0; i--) { |
| 2101 | users.put(getUserIdFromKey(mSettingsStates.keyAt(i)), true); |
| 2102 | } |
| 2103 | return users; |
| 2104 | } |
| 2105 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2106 | public SettingsState getSettingsLocked(int type, int userId) { |
| 2107 | final int key = makeKey(type, userId); |
| 2108 | return peekSettingsStateLocked(key); |
| 2109 | } |
| 2110 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2111 | public boolean ensureSettingsForUserLocked(int userId) { |
| 2112 | // First make sure this user actually exists. |
| 2113 | if (mUserManager.getUserInfo(userId) == null) { |
| 2114 | Slog.wtf(LOG_TAG, "Requested user " + userId + " does not exist"); |
| 2115 | return false; |
| 2116 | } |
| 2117 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2118 | // Migrate the setting for this user if needed. |
| 2119 | migrateLegacySettingsForUserIfNeededLocked(userId); |
| 2120 | |
| 2121 | // Ensure global settings loaded if owner. |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 2122 | if (userId == UserHandle.USER_SYSTEM) { |
| 2123 | final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2124 | ensureSettingsStateLocked(globalKey); |
| 2125 | } |
| 2126 | |
| 2127 | // Ensure secure settings loaded. |
| 2128 | final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId); |
| 2129 | ensureSettingsStateLocked(secureKey); |
| 2130 | |
| 2131 | // Make sure the secure settings have an Android id set. |
| 2132 | SettingsState secureSettings = getSettingsLocked(SETTINGS_TYPE_SECURE, userId); |
| 2133 | ensureSecureSettingAndroidIdSetLocked(secureSettings); |
| 2134 | |
| 2135 | // Ensure system settings loaded. |
| 2136 | final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId); |
| 2137 | ensureSettingsStateLocked(systemKey); |
| 2138 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2139 | // Ensure secure settings loaded. |
| 2140 | final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId); |
| 2141 | ensureSettingsStateLocked(ssaidKey); |
| 2142 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2143 | // Upgrade the settings to the latest version. |
| 2144 | UpgradeController upgrader = new UpgradeController(userId); |
| 2145 | upgrader.upgradeIfNeededLocked(); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2146 | return true; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | private void ensureSettingsStateLocked(int key) { |
| 2150 | if (mSettingsStates.get(key) == null) { |
| 2151 | final int maxBytesPerPackage = getMaxBytesPerPackageForType(getTypeFromKey(key)); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2152 | SettingsState settingsState = new SettingsState(getContext(), mLock, |
| 2153 | getSettingsFile(key), key, maxBytesPerPackage, mHandlerThread.getLooper()); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2154 | mSettingsStates.put(key, settingsState); |
| 2155 | } |
| 2156 | } |
| 2157 | |
| 2158 | public void removeUserStateLocked(int userId, boolean permanently) { |
| 2159 | // We always keep the global settings in memory. |
| 2160 | |
| 2161 | // Nuke system settings. |
| 2162 | final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId); |
| 2163 | final SettingsState systemSettingsState = mSettingsStates.get(systemKey); |
| 2164 | if (systemSettingsState != null) { |
| 2165 | if (permanently) { |
| 2166 | mSettingsStates.remove(systemKey); |
| 2167 | systemSettingsState.destroyLocked(null); |
Brad Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame] | 2168 | } else { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2169 | systemSettingsState.destroyLocked(new Runnable() { |
| 2170 | @Override |
| 2171 | public void run() { |
| 2172 | mSettingsStates.remove(systemKey); |
| 2173 | } |
| 2174 | }); |
| 2175 | } |
| 2176 | } |
| 2177 | |
| 2178 | // Nuke secure settings. |
| 2179 | final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId); |
| 2180 | final SettingsState secureSettingsState = mSettingsStates.get(secureKey); |
| 2181 | if (secureSettingsState != null) { |
| 2182 | if (permanently) { |
| 2183 | mSettingsStates.remove(secureKey); |
| 2184 | secureSettingsState.destroyLocked(null); |
| 2185 | } else { |
| 2186 | secureSettingsState.destroyLocked(new Runnable() { |
| 2187 | @Override |
| 2188 | public void run() { |
| 2189 | mSettingsStates.remove(secureKey); |
| 2190 | } |
| 2191 | }); |
Brad Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame] | 2192 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2193 | } |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2194 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2195 | // Nuke ssaid settings. |
| 2196 | final int ssaidKey = makeKey(SETTINGS_TYPE_SSAID, userId); |
| 2197 | final SettingsState ssaidSettingsState = mSettingsStates.get(ssaidKey); |
| 2198 | if (ssaidSettingsState != null) { |
| 2199 | if (permanently) { |
| 2200 | mSettingsStates.remove(ssaidKey); |
| 2201 | ssaidSettingsState.destroyLocked(null); |
| 2202 | } else { |
| 2203 | ssaidSettingsState.destroyLocked(new Runnable() { |
| 2204 | @Override |
| 2205 | public void run() { |
| 2206 | mSettingsStates.remove(ssaidKey); |
| 2207 | } |
| 2208 | }); |
| 2209 | } |
| 2210 | } |
| 2211 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2212 | // Nuke generation tracking data |
| 2213 | mGenerationRegistry.onUserRemoved(userId); |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 2214 | } |
| 2215 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2216 | public boolean insertSettingLocked(int type, int userId, String name, String value, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2217 | String tag, boolean makeDefault, String packageName, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2218 | final int key = makeKey(type, userId); |
| 2219 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2220 | boolean success = false; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2221 | SettingsState settingsState = peekSettingsStateLocked(key); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2222 | if (settingsState != null) { |
| 2223 | success = settingsState.insertSettingLocked(name, value, |
| 2224 | tag, makeDefault, packageName); |
| 2225 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2226 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2227 | if (forceNotify || success) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2228 | notifyForSettingsChange(key, name); |
| 2229 | } |
| 2230 | return success; |
| 2231 | } |
| 2232 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2233 | public boolean deleteSettingLocked(int type, int userId, String name, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2234 | final int key = makeKey(type, userId); |
| 2235 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2236 | boolean success = false; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2237 | SettingsState settingsState = peekSettingsStateLocked(key); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2238 | if (settingsState != null) { |
| 2239 | success = settingsState.deleteSettingLocked(name); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2240 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2241 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2242 | if (forceNotify || success) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2243 | notifyForSettingsChange(key, name); |
| 2244 | } |
| 2245 | return success; |
| 2246 | } |
| 2247 | |
| 2248 | public Setting getSettingLocked(int type, int userId, String name) { |
| 2249 | final int key = makeKey(type, userId); |
| 2250 | |
| 2251 | SettingsState settingsState = peekSettingsStateLocked(key); |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2252 | if (settingsState == null) { |
Mark Rathjen | ea61759 | 2017-01-18 23:03:41 -0800 | [diff] [blame] | 2253 | return null; |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2254 | } |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2255 | |
| 2256 | // getSettingLocked will return non-null result |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2257 | return settingsState.getSettingLocked(name); |
| 2258 | } |
| 2259 | |
| 2260 | public boolean updateSettingLocked(int type, int userId, String name, String value, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2261 | String tag, boolean makeDefault, String packageName, boolean forceNotify) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2262 | final int key = makeKey(type, userId); |
| 2263 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2264 | boolean success = false; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2265 | SettingsState settingsState = peekSettingsStateLocked(key); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2266 | if (settingsState != null) { |
| 2267 | success = settingsState.updateSettingLocked(name, value, tag, |
| 2268 | makeDefault, packageName); |
| 2269 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2270 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2271 | if (forceNotify || success) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2272 | notifyForSettingsChange(key, name); |
| 2273 | } |
| 2274 | |
| 2275 | return success; |
| 2276 | } |
| 2277 | |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2278 | public void resetSettingsLocked(int type, int userId, String packageName, int mode, |
| 2279 | String tag) { |
| 2280 | final int key = makeKey(type, userId); |
| 2281 | SettingsState settingsState = peekSettingsStateLocked(key); |
| 2282 | if (settingsState == null) { |
| 2283 | return; |
| 2284 | } |
| 2285 | |
| 2286 | switch (mode) { |
| 2287 | case Settings.RESET_MODE_PACKAGE_DEFAULTS: { |
| 2288 | for (String name : settingsState.getSettingNamesLocked()) { |
| 2289 | Setting setting = settingsState.getSettingLocked(name); |
| 2290 | if (packageName.equals(setting.getPackageName())) { |
| 2291 | if (tag != null && !tag.equals(setting.getTag())) { |
| 2292 | continue; |
| 2293 | } |
| 2294 | if (settingsState.resetSettingLocked(name, packageName)) { |
| 2295 | notifyForSettingsChange(key, name); |
| 2296 | } |
| 2297 | } |
| 2298 | } |
| 2299 | } break; |
| 2300 | |
| 2301 | case Settings.RESET_MODE_UNTRUSTED_DEFAULTS: { |
| 2302 | for (String name : settingsState.getSettingNamesLocked()) { |
| 2303 | Setting setting = settingsState.getSettingLocked(name); |
| 2304 | if (!SettingsState.isSystemPackage(getContext(), |
| 2305 | setting.getPackageName())) { |
| 2306 | if (settingsState.resetSettingLocked(name, packageName)) { |
| 2307 | notifyForSettingsChange(key, name); |
| 2308 | } |
| 2309 | } |
| 2310 | } |
| 2311 | } break; |
| 2312 | |
| 2313 | case Settings.RESET_MODE_UNTRUSTED_CHANGES: { |
| 2314 | for (String name : settingsState.getSettingNamesLocked()) { |
| 2315 | Setting setting = settingsState.getSettingLocked(name); |
| 2316 | if (!SettingsState.isSystemPackage(getContext(), |
| 2317 | setting.getPackageName())) { |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame^] | 2318 | if (setting.isDefaultFromSystem()) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2319 | if (settingsState.resetSettingLocked(name, packageName)) { |
| 2320 | notifyForSettingsChange(key, name); |
| 2321 | } |
| 2322 | } else if (settingsState.deleteSettingLocked(name)) { |
| 2323 | notifyForSettingsChange(key, name); |
| 2324 | } |
| 2325 | } |
| 2326 | } |
| 2327 | } break; |
| 2328 | |
| 2329 | case Settings.RESET_MODE_TRUSTED_DEFAULTS: { |
| 2330 | for (String name : settingsState.getSettingNamesLocked()) { |
| 2331 | Setting setting = settingsState.getSettingLocked(name); |
Eugene Susla | d72c397 | 2016-12-27 15:49:30 -0800 | [diff] [blame^] | 2332 | if (setting.isDefaultFromSystem()) { |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2333 | if (settingsState.resetSettingLocked(name, packageName)) { |
| 2334 | notifyForSettingsChange(key, name); |
| 2335 | } |
| 2336 | } else if (settingsState.deleteSettingLocked(name)) { |
| 2337 | notifyForSettingsChange(key, name); |
| 2338 | } |
| 2339 | } |
| 2340 | } break; |
| 2341 | } |
| 2342 | } |
| 2343 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2344 | public void onPackageRemovedLocked(String packageName, int userId) { |
Svet Ganov | 8de3480 | 2015-04-27 09:33:40 -0700 | [diff] [blame] | 2345 | // Global and secure settings are signature protected. Apps signed |
| 2346 | // by the platform certificate are generally not uninstalled and |
| 2347 | // the main exception is tests. We trust components signed |
| 2348 | // by the platform certificate and do not do a clean up after them. |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2349 | |
| 2350 | final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId); |
| 2351 | SettingsState systemSettings = mSettingsStates.get(systemKey); |
Svet Ganov | 8de3480 | 2015-04-27 09:33:40 -0700 | [diff] [blame] | 2352 | if (systemSettings != null) { |
| 2353 | systemSettings.onPackageRemovedLocked(packageName); |
| 2354 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2355 | } |
| 2356 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2357 | public void onUidRemovedLocked(int uid) { |
| 2358 | final SettingsState ssaidSettings = getSettingsLocked(SETTINGS_TYPE_SSAID, |
| 2359 | UserHandle.getUserId(uid)); |
| 2360 | ssaidSettings.deleteSettingLocked(Integer.toString(uid)); |
| 2361 | } |
| 2362 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2363 | private SettingsState peekSettingsStateLocked(int key) { |
| 2364 | SettingsState settingsState = mSettingsStates.get(key); |
| 2365 | if (settingsState != null) { |
| 2366 | return settingsState; |
| 2367 | } |
| 2368 | |
Dianne Hackborn | 32f40ee | 2016-10-20 15:54:14 -0700 | [diff] [blame] | 2369 | if (!ensureSettingsForUserLocked(getUserIdFromKey(key))) { |
| 2370 | return null; |
| 2371 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2372 | return mSettingsStates.get(key); |
| 2373 | } |
| 2374 | |
| 2375 | private void migrateAllLegacySettingsIfNeeded() { |
| 2376 | synchronized (mLock) { |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 2377 | final int key = makeKey(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2378 | File globalFile = getSettingsFile(key); |
| 2379 | if (globalFile.exists()) { |
| 2380 | return; |
| 2381 | } |
| 2382 | |
| 2383 | final long identity = Binder.clearCallingIdentity(); |
| 2384 | try { |
| 2385 | List<UserInfo> users = mUserManager.getUsers(true); |
| 2386 | |
| 2387 | final int userCount = users.size(); |
| 2388 | for (int i = 0; i < userCount; i++) { |
| 2389 | final int userId = users.get(i).id; |
| 2390 | |
| 2391 | DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId); |
| 2392 | SQLiteDatabase database = dbHelper.getWritableDatabase(); |
| 2393 | migrateLegacySettingsForUserLocked(dbHelper, database, userId); |
| 2394 | |
| 2395 | // Upgrade to the latest version. |
| 2396 | UpgradeController upgrader = new UpgradeController(userId); |
| 2397 | upgrader.upgradeIfNeededLocked(); |
| 2398 | |
| 2399 | // Drop from memory if not a running user. |
| 2400 | if (!mUserManager.isUserRunning(new UserHandle(userId))) { |
| 2401 | removeUserStateLocked(userId, false); |
| 2402 | } |
| 2403 | } |
| 2404 | } finally { |
| 2405 | Binder.restoreCallingIdentity(identity); |
| 2406 | } |
| 2407 | } |
| 2408 | } |
| 2409 | |
| 2410 | private void migrateLegacySettingsForUserIfNeededLocked(int userId) { |
| 2411 | // Every user has secure settings and if no file we need to migrate. |
| 2412 | final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId); |
| 2413 | File secureFile = getSettingsFile(secureKey); |
| 2414 | if (secureFile.exists()) { |
| 2415 | return; |
| 2416 | } |
| 2417 | |
| 2418 | DatabaseHelper dbHelper = new DatabaseHelper(getContext(), userId); |
| 2419 | SQLiteDatabase database = dbHelper.getWritableDatabase(); |
| 2420 | |
| 2421 | migrateLegacySettingsForUserLocked(dbHelper, database, userId); |
| 2422 | } |
| 2423 | |
| 2424 | private void migrateLegacySettingsForUserLocked(DatabaseHelper dbHelper, |
| 2425 | SQLiteDatabase database, int userId) { |
Amith Yamasani | bf2ef61 | 2016-03-07 16:37:18 -0800 | [diff] [blame] | 2426 | // Move over the system settings. |
| 2427 | final int systemKey = makeKey(SETTINGS_TYPE_SYSTEM, userId); |
| 2428 | ensureSettingsStateLocked(systemKey); |
| 2429 | SettingsState systemSettings = mSettingsStates.get(systemKey); |
| 2430 | migrateLegacySettingsLocked(systemSettings, database, TABLE_SYSTEM); |
| 2431 | systemSettings.persistSyncLocked(); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2432 | |
| 2433 | // Move over the secure settings. |
Amith Yamasani | bf2ef61 | 2016-03-07 16:37:18 -0800 | [diff] [blame] | 2434 | // Do this after System settings, since this is the first thing we check when deciding |
| 2435 | // to skip over migration from db to xml for a secondary user. |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2436 | final int secureKey = makeKey(SETTINGS_TYPE_SECURE, userId); |
| 2437 | ensureSettingsStateLocked(secureKey); |
| 2438 | SettingsState secureSettings = mSettingsStates.get(secureKey); |
| 2439 | migrateLegacySettingsLocked(secureSettings, database, TABLE_SECURE); |
| 2440 | ensureSecureSettingAndroidIdSetLocked(secureSettings); |
| 2441 | secureSettings.persistSyncLocked(); |
| 2442 | |
Amith Yamasani | bf2ef61 | 2016-03-07 16:37:18 -0800 | [diff] [blame] | 2443 | // Move over the global settings if owner. |
| 2444 | // Do this last, since this is the first thing we check when deciding |
| 2445 | // to skip over migration from db to xml for owner user. |
| 2446 | if (userId == UserHandle.USER_SYSTEM) { |
| 2447 | final int globalKey = makeKey(SETTINGS_TYPE_GLOBAL, userId); |
| 2448 | ensureSettingsStateLocked(globalKey); |
| 2449 | SettingsState globalSettings = mSettingsStates.get(globalKey); |
| 2450 | migrateLegacySettingsLocked(globalSettings, database, TABLE_GLOBAL); |
| 2451 | globalSettings.persistSyncLocked(); |
| 2452 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2453 | |
| 2454 | // Drop the database as now all is moved and persisted. |
| 2455 | if (DROP_DATABASE_ON_MIGRATION) { |
| 2456 | dbHelper.dropDatabase(); |
| 2457 | } else { |
| 2458 | dbHelper.backupDatabase(); |
| 2459 | } |
| 2460 | } |
| 2461 | |
| 2462 | private void migrateLegacySettingsLocked(SettingsState settingsState, |
| 2463 | SQLiteDatabase database, String table) { |
| 2464 | SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); |
| 2465 | queryBuilder.setTables(table); |
| 2466 | |
| 2467 | Cursor cursor = queryBuilder.query(database, ALL_COLUMNS, |
| 2468 | null, null, null, null, null); |
| 2469 | |
| 2470 | if (cursor == null) { |
| 2471 | return; |
| 2472 | } |
| 2473 | |
| 2474 | try { |
| 2475 | if (!cursor.moveToFirst()) { |
| 2476 | return; |
| 2477 | } |
| 2478 | |
| 2479 | final int nameColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.NAME); |
| 2480 | final int valueColumnIdx = cursor.getColumnIndex(Settings.NameValueTable.VALUE); |
| 2481 | |
| 2482 | settingsState.setVersionLocked(database.getVersion()); |
| 2483 | |
| 2484 | while (!cursor.isAfterLast()) { |
| 2485 | String name = cursor.getString(nameColumnIdx); |
| 2486 | String value = cursor.getString(valueColumnIdx); |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2487 | settingsState.insertSettingLocked(name, value, null, true, |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2488 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 2489 | cursor.moveToNext(); |
| 2490 | } |
| 2491 | } finally { |
| 2492 | cursor.close(); |
| 2493 | } |
| 2494 | } |
| 2495 | |
| 2496 | private void ensureSecureSettingAndroidIdSetLocked(SettingsState secureSettings) { |
| 2497 | Setting value = secureSettings.getSettingLocked(Settings.Secure.ANDROID_ID); |
| 2498 | |
Seigo Nonaka | 6e5b602 | 2016-04-27 16:32:44 +0900 | [diff] [blame] | 2499 | if (!value.isNull()) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2500 | return; |
| 2501 | } |
| 2502 | |
| 2503 | final int userId = getUserIdFromKey(secureSettings.mKey); |
| 2504 | |
| 2505 | final UserInfo user; |
| 2506 | final long identity = Binder.clearCallingIdentity(); |
| 2507 | try { |
| 2508 | user = mUserManager.getUserInfo(userId); |
| 2509 | } finally { |
| 2510 | Binder.restoreCallingIdentity(identity); |
| 2511 | } |
| 2512 | if (user == null) { |
| 2513 | // Can happen due to races when deleting users - treat as benign. |
| 2514 | return; |
| 2515 | } |
| 2516 | |
| 2517 | String androidId = Long.toHexString(new SecureRandom().nextLong()); |
| 2518 | secureSettings.insertSettingLocked(Settings.Secure.ANDROID_ID, androidId, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2519 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2520 | |
| 2521 | Slog.d(LOG_TAG, "Generated and saved new ANDROID_ID [" + androidId |
| 2522 | + "] for user " + userId); |
| 2523 | |
| 2524 | // Write a drop box entry if it's a restricted profile |
| 2525 | if (user.isRestricted()) { |
| 2526 | DropBoxManager dbm = (DropBoxManager) getContext().getSystemService( |
| 2527 | Context.DROPBOX_SERVICE); |
| 2528 | if (dbm != null && dbm.isTagEnabled(DROPBOX_TAG_USERLOG)) { |
| 2529 | dbm.addText(DROPBOX_TAG_USERLOG, System.currentTimeMillis() |
| 2530 | + "," + DROPBOX_TAG_USERLOG + "," + androidId + "\n"); |
| 2531 | } |
| 2532 | } |
| 2533 | } |
| 2534 | |
| 2535 | private void notifyForSettingsChange(int key, String name) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2536 | final int userId = getUserIdFromKey(key); |
| 2537 | Uri uri = getNotificationUriFor(key, name); |
| 2538 | |
Phil Weaver | 83fec00 | 2016-05-11 10:55:29 -0700 | [diff] [blame] | 2539 | mGenerationRegistry.incrementGeneration(key); |
| 2540 | |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 2541 | mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED, |
| 2542 | userId, 0, uri).sendToTarget(); |
| 2543 | |
Nicolas Prevot | 310e1ee | 2015-07-02 14:03:06 +0100 | [diff] [blame] | 2544 | if (isSecureSettingsKey(key)) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2545 | maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name, |
| 2546 | sSecureCloneToManagedSettings); |
Nicolas Prevot | 310e1ee | 2015-07-02 14:03:06 +0100 | [diff] [blame] | 2547 | } else if (isSystemSettingsKey(key)) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2548 | maybeNotifyProfiles(getTypeFromKey(key), userId, uri, name, |
| 2549 | sSystemCloneToManagedSettings); |
Nicolas Prevot | 310e1ee | 2015-07-02 14:03:06 +0100 | [diff] [blame] | 2550 | } |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2551 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2552 | mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget(); |
Nicolas Prevot | 310e1ee | 2015-07-02 14:03:06 +0100 | [diff] [blame] | 2553 | } |
| 2554 | |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2555 | private void maybeNotifyProfiles(int type, int userId, Uri uri, String name, |
Nicolas Prevot | 310e1ee | 2015-07-02 14:03:06 +0100 | [diff] [blame] | 2556 | Set<String> keysCloned) { |
| 2557 | if (keysCloned.contains(name)) { |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 2558 | for (int profileId : mUserManager.getProfileIdsWithDisabled(userId)) { |
Nicolas Prevot | 310e1ee | 2015-07-02 14:03:06 +0100 | [diff] [blame] | 2559 | // the notification for userId has already been sent. |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 2560 | if (profileId != userId) { |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 2561 | mHandler.obtainMessage(MyHandler.MSG_NOTIFY_URI_CHANGED, |
Fyodor Kupolov | 7f98aa4 | 2016-04-07 14:56:25 -0700 | [diff] [blame] | 2562 | profileId, 0, uri).sendToTarget(); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2563 | final int key = makeKey(type, profileId); |
| 2564 | mGenerationRegistry.incrementGeneration(key); |
| 2565 | |
| 2566 | mHandler.obtainMessage(MyHandler.MSG_NOTIFY_DATA_CHANGED).sendToTarget(); |
Nicolas Prevot | 310e1ee | 2015-07-02 14:03:06 +0100 | [diff] [blame] | 2567 | } |
| 2568 | } |
| 2569 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2570 | } |
| 2571 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2572 | private boolean isGlobalSettingsKey(int key) { |
| 2573 | return getTypeFromKey(key) == SETTINGS_TYPE_GLOBAL; |
| 2574 | } |
| 2575 | |
| 2576 | private boolean isSystemSettingsKey(int key) { |
| 2577 | return getTypeFromKey(key) == SETTINGS_TYPE_SYSTEM; |
| 2578 | } |
| 2579 | |
| 2580 | private boolean isSecureSettingsKey(int key) { |
| 2581 | return getTypeFromKey(key) == SETTINGS_TYPE_SECURE; |
| 2582 | } |
| 2583 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2584 | private boolean isSsaidSettingsKey(int key) { |
| 2585 | return getTypeFromKey(key) == SETTINGS_TYPE_SSAID; |
| 2586 | } |
| 2587 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2588 | private File getSettingsFile(int key) { |
| 2589 | if (isGlobalSettingsKey(key)) { |
| 2590 | final int userId = getUserIdFromKey(key); |
| 2591 | return new File(Environment.getUserSystemDirectory(userId), |
| 2592 | SETTINGS_FILE_GLOBAL); |
| 2593 | } else if (isSystemSettingsKey(key)) { |
| 2594 | final int userId = getUserIdFromKey(key); |
| 2595 | return new File(Environment.getUserSystemDirectory(userId), |
| 2596 | SETTINGS_FILE_SYSTEM); |
| 2597 | } else if (isSecureSettingsKey(key)) { |
| 2598 | final int userId = getUserIdFromKey(key); |
| 2599 | return new File(Environment.getUserSystemDirectory(userId), |
| 2600 | SETTINGS_FILE_SECURE); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2601 | } else if (isSsaidSettingsKey(key)) { |
| 2602 | final int userId = getUserIdFromKey(key); |
| 2603 | return new File(Environment.getUserSystemDirectory(userId), |
| 2604 | SETTINGS_FILE_SSAID); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2605 | } else { |
| 2606 | throw new IllegalArgumentException("Invalid settings key:" + key); |
| 2607 | } |
| 2608 | } |
| 2609 | |
| 2610 | private Uri getNotificationUriFor(int key, String name) { |
| 2611 | if (isGlobalSettingsKey(key)) { |
| 2612 | return (name != null) ? Uri.withAppendedPath(Settings.Global.CONTENT_URI, name) |
| 2613 | : Settings.Global.CONTENT_URI; |
| 2614 | } else if (isSecureSettingsKey(key)) { |
| 2615 | return (name != null) ? Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name) |
| 2616 | : Settings.Secure.CONTENT_URI; |
| 2617 | } else if (isSystemSettingsKey(key)) { |
| 2618 | return (name != null) ? Uri.withAppendedPath(Settings.System.CONTENT_URI, name) |
| 2619 | : Settings.System.CONTENT_URI; |
| 2620 | } else { |
| 2621 | throw new IllegalArgumentException("Invalid settings key:" + key); |
| 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | private int getMaxBytesPerPackageForType(int type) { |
| 2626 | switch (type) { |
| 2627 | case SETTINGS_TYPE_GLOBAL: |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2628 | case SETTINGS_TYPE_SECURE: |
| 2629 | case SETTINGS_TYPE_SSAID: { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2630 | return SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED; |
| 2631 | } |
| 2632 | |
| 2633 | default: { |
| 2634 | return SettingsState.MAX_BYTES_PER_APP_PACKAGE_LIMITED; |
| 2635 | } |
| 2636 | } |
| 2637 | } |
| 2638 | |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 2639 | private final class MyHandler extends Handler { |
| 2640 | private static final int MSG_NOTIFY_URI_CHANGED = 1; |
| 2641 | private static final int MSG_NOTIFY_DATA_CHANGED = 2; |
| 2642 | |
| 2643 | public MyHandler(Looper looper) { |
| 2644 | super(looper); |
| 2645 | } |
| 2646 | |
| 2647 | @Override |
| 2648 | public void handleMessage(Message msg) { |
| 2649 | switch (msg.what) { |
| 2650 | case MSG_NOTIFY_URI_CHANGED: { |
| 2651 | final int userId = msg.arg1; |
| 2652 | Uri uri = (Uri) msg.obj; |
| 2653 | getContext().getContentResolver().notifyChange(uri, null, true, userId); |
| 2654 | if (DEBUG) { |
| 2655 | Slog.v(LOG_TAG, "Notifying for " + userId + ": " + uri); |
| 2656 | } |
| 2657 | } break; |
| 2658 | |
| 2659 | case MSG_NOTIFY_DATA_CHANGED: { |
| 2660 | mBackupManager.dataChanged(); |
| 2661 | } break; |
| 2662 | } |
| 2663 | } |
| 2664 | } |
| 2665 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2666 | private final class UpgradeController { |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2667 | private static final int SETTINGS_VERSION = 137; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2668 | |
| 2669 | private final int mUserId; |
| 2670 | |
| 2671 | public UpgradeController(int userId) { |
| 2672 | mUserId = userId; |
| 2673 | } |
| 2674 | |
| 2675 | public void upgradeIfNeededLocked() { |
| 2676 | // The version of all settings for a user is the same (all users have secure). |
| 2677 | SettingsState secureSettings = getSettingsLocked( |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2678 | SETTINGS_TYPE_SECURE, mUserId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2679 | |
| 2680 | // Try an update from the current state. |
| 2681 | final int oldVersion = secureSettings.getVersionLocked(); |
| 2682 | final int newVersion = SETTINGS_VERSION; |
| 2683 | |
Svet Ganov | c9755bc | 2015-03-28 13:21:22 -0700 | [diff] [blame] | 2684 | // If up do date - done. |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2685 | if (oldVersion == newVersion) { |
| 2686 | return; |
| 2687 | } |
| 2688 | |
| 2689 | // Try to upgrade. |
| 2690 | final int curVersion = onUpgradeLocked(mUserId, oldVersion, newVersion); |
| 2691 | |
| 2692 | // If upgrade failed start from scratch and upgrade. |
| 2693 | if (curVersion != newVersion) { |
| 2694 | // Drop state we have for this user. |
| 2695 | removeUserStateLocked(mUserId, true); |
| 2696 | |
| 2697 | // Recreate the database. |
| 2698 | DatabaseHelper dbHelper = new DatabaseHelper(getContext(), mUserId); |
| 2699 | SQLiteDatabase database = dbHelper.getWritableDatabase(); |
| 2700 | dbHelper.recreateDatabase(database, newVersion, curVersion, oldVersion); |
| 2701 | |
| 2702 | // Migrate the settings for this user. |
| 2703 | migrateLegacySettingsForUserLocked(dbHelper, database, mUserId); |
| 2704 | |
| 2705 | // Now upgrade should work fine. |
| 2706 | onUpgradeLocked(mUserId, oldVersion, newVersion); |
Svetoslav Ganov | 264c7a9 | 2016-08-24 17:31:14 -0700 | [diff] [blame] | 2707 | |
| 2708 | // Make a note what happened, so we don't wonder why data was lost |
| 2709 | String reason = "Settings rebuilt! Current version: " |
| 2710 | + curVersion + " while expected: " + newVersion; |
| 2711 | getGlobalSettingsLocked().insertSettingLocked( |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2712 | Settings.Global.DATABASE_DOWNGRADE_REASON, |
| 2713 | reason, null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2714 | } |
| 2715 | |
| 2716 | // Set the global settings version if owner. |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 2717 | if (mUserId == UserHandle.USER_SYSTEM) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2718 | SettingsState globalSettings = getSettingsLocked( |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2719 | SETTINGS_TYPE_GLOBAL, mUserId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2720 | globalSettings.setVersionLocked(newVersion); |
| 2721 | } |
| 2722 | |
| 2723 | // Set the secure settings version. |
| 2724 | secureSettings.setVersionLocked(newVersion); |
| 2725 | |
| 2726 | // Set the system settings version. |
| 2727 | SettingsState systemSettings = getSettingsLocked( |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 2728 | SETTINGS_TYPE_SYSTEM, mUserId); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2729 | systemSettings.setVersionLocked(newVersion); |
| 2730 | } |
| 2731 | |
| 2732 | private SettingsState getGlobalSettingsLocked() { |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 2733 | return getSettingsLocked(SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | private SettingsState getSecureSettingsLocked(int userId) { |
| 2737 | return getSettingsLocked(SETTINGS_TYPE_SECURE, userId); |
| 2738 | } |
| 2739 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 2740 | private SettingsState getSsaidSettingsLocked(int userId) { |
| 2741 | return getSettingsLocked(SETTINGS_TYPE_SSAID, userId); |
| 2742 | } |
| 2743 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2744 | private SettingsState getSystemSettingsLocked(int userId) { |
| 2745 | return getSettingsLocked(SETTINGS_TYPE_SYSTEM, userId); |
| 2746 | } |
| 2747 | |
Jeff Brown | 503cffc | 2015-03-26 18:08:51 -0700 | [diff] [blame] | 2748 | /** |
| 2749 | * You must perform all necessary mutations to bring the settings |
| 2750 | * for this user from the old to the new version. When you add a new |
| 2751 | * upgrade step you *must* update SETTINGS_VERSION. |
| 2752 | * |
| 2753 | * This is an example of moving a setting from secure to global. |
| 2754 | * |
| 2755 | * // v119: Example settings changes. |
| 2756 | * if (currentVersion == 118) { |
| 2757 | * if (userId == UserHandle.USER_OWNER) { |
| 2758 | * // Remove from the secure settings. |
| 2759 | * SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 2760 | * String name = "example_setting_to_move"; |
| 2761 | * String value = secureSettings.getSetting(name); |
| 2762 | * secureSettings.deleteSetting(name); |
| 2763 | * |
| 2764 | * // Add to the global settings. |
| 2765 | * SettingsState globalSettings = getGlobalSettingsLocked(); |
| 2766 | * globalSettings.insertSetting(name, value, SettingsState.SYSTEM_PACKAGE_NAME); |
| 2767 | * } |
| 2768 | * |
| 2769 | * // Update the current version. |
| 2770 | * currentVersion = 119; |
| 2771 | * } |
| 2772 | */ |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2773 | private int onUpgradeLocked(int userId, int oldVersion, int newVersion) { |
| 2774 | if (DEBUG) { |
| 2775 | Slog.w(LOG_TAG, "Upgrading settings for user: " + userId + " from version: " |
| 2776 | + oldVersion + " to version: " + newVersion); |
| 2777 | } |
| 2778 | |
Jeff Brown | 503cffc | 2015-03-26 18:08:51 -0700 | [diff] [blame] | 2779 | int currentVersion = oldVersion; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 2780 | |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 2781 | // v119: Reset zen + ringer mode. |
| 2782 | if (currentVersion == 118) { |
Xiaohui Chen | 43765b7 | 2015-08-31 10:57:33 -0700 | [diff] [blame] | 2783 | if (userId == UserHandle.USER_SYSTEM) { |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 2784 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 2785 | globalSettings.updateSettingLocked(Settings.Global.ZEN_MODE, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2786 | Integer.toString(Settings.Global.ZEN_MODE_OFF), null, |
| 2787 | true, SettingsState.SYSTEM_PACKAGE_NAME); |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 2788 | globalSettings.updateSettingLocked(Settings.Global.MODE_RINGER, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2789 | Integer.toString(AudioManager.RINGER_MODE_NORMAL), null, |
| 2790 | true, SettingsState.SYSTEM_PACKAGE_NAME); |
John Spurlock | e11ae11 | 2015-05-11 16:09:03 -0400 | [diff] [blame] | 2791 | } |
| 2792 | currentVersion = 119; |
| 2793 | } |
| 2794 | |
Jason Monk | 27bbb2d | 2015-03-31 16:46:39 -0400 | [diff] [blame] | 2795 | // v120: Add double tap to wake setting. |
| 2796 | if (currentVersion == 119) { |
| 2797 | SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 2798 | secureSettings.insertSettingLocked(Settings.Secure.DOUBLE_TAP_TO_WAKE, |
| 2799 | getContext().getResources().getBoolean( |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2800 | R.bool.def_double_tap_to_wake) ? "1" : "0", null, true, |
Jason Monk | 27bbb2d | 2015-03-31 16:46:39 -0400 | [diff] [blame] | 2801 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 2802 | |
| 2803 | currentVersion = 120; |
| 2804 | } |
| 2805 | |
Svetoslav | 7e0683b | 2015-08-03 16:02:52 -0700 | [diff] [blame] | 2806 | if (currentVersion == 120) { |
| 2807 | // Before 121, we used a different string encoding logic. We just bump the |
| 2808 | // version here; SettingsState knows how to handle pre-version 120 files. |
| 2809 | currentVersion = 121; |
| 2810 | } |
Makoto Onuki | 3a2c3578 | 2015-06-18 11:21:58 -0700 | [diff] [blame] | 2811 | |
Martijn Coenen | 7ab4b7f | 2015-07-27 15:58:32 +0200 | [diff] [blame] | 2812 | if (currentVersion == 121) { |
| 2813 | // Version 122: allow OEMs to set a default payment component in resources. |
| 2814 | // Note that we only write the default if no default has been set; |
| 2815 | // if there is, we just leave the default at whatever it currently is. |
| 2816 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 2817 | String defaultComponent = (getContext().getResources().getString( |
| 2818 | R.string.def_nfc_payment_component)); |
| 2819 | Setting currentSetting = secureSettings.getSettingLocked( |
| 2820 | Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT); |
| 2821 | if (defaultComponent != null && !defaultComponent.isEmpty() && |
Svetoslav Ganov | fedb230 | 2016-04-26 18:36:42 -0700 | [diff] [blame] | 2822 | currentSetting.isNull()) { |
Martijn Coenen | 7ab4b7f | 2015-07-27 15:58:32 +0200 | [diff] [blame] | 2823 | secureSettings.insertSettingLocked( |
| 2824 | Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2825 | defaultComponent, null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Martijn Coenen | 7ab4b7f | 2015-07-27 15:58:32 +0200 | [diff] [blame] | 2826 | } |
| 2827 | currentVersion = 122; |
| 2828 | } |
Suprabh Shukla | 269c11e | 2015-12-02 16:51:16 -0800 | [diff] [blame] | 2829 | |
| 2830 | if (currentVersion == 122) { |
| 2831 | // Version 123: Adding a default value for the ability to add a user from |
| 2832 | // the lock screen. |
| 2833 | if (userId == UserHandle.USER_SYSTEM) { |
| 2834 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 2835 | Setting currentSetting = globalSettings.getSettingLocked( |
| 2836 | Settings.Global.ADD_USERS_WHEN_LOCKED); |
Svetoslav Ganov | fedb230 | 2016-04-26 18:36:42 -0700 | [diff] [blame] | 2837 | if (currentSetting.isNull()) { |
Suprabh Shukla | 269c11e | 2015-12-02 16:51:16 -0800 | [diff] [blame] | 2838 | globalSettings.insertSettingLocked( |
| 2839 | Settings.Global.ADD_USERS_WHEN_LOCKED, |
| 2840 | getContext().getResources().getBoolean( |
| 2841 | R.bool.def_add_users_from_lockscreen) ? "1" : "0", |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2842 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Suprabh Shukla | 269c11e | 2015-12-02 16:51:16 -0800 | [diff] [blame] | 2843 | } |
| 2844 | } |
| 2845 | currentVersion = 123; |
| 2846 | } |
Bryce Lee | bd17928 | 2015-12-17 19:01:37 -0800 | [diff] [blame] | 2847 | |
| 2848 | if (currentVersion == 123) { |
Bryce Lee | ec85f34 | 2015-12-16 13:32:28 -0800 | [diff] [blame] | 2849 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 2850 | String defaultDisabledProfiles = (getContext().getResources().getString( |
| 2851 | R.string.def_bluetooth_disabled_profiles)); |
| 2852 | globalSettings.insertSettingLocked(Settings.Global.BLUETOOTH_DISABLED_PROFILES, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2853 | defaultDisabledProfiles, null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Bryce Lee | bd17928 | 2015-12-17 19:01:37 -0800 | [diff] [blame] | 2854 | currentVersion = 124; |
Bryce Lee | ec85f34 | 2015-12-16 13:32:28 -0800 | [diff] [blame] | 2855 | } |
| 2856 | |
Prathmesh Prabhu | de16b86 | 2016-03-04 15:22:24 -0800 | [diff] [blame] | 2857 | if (currentVersion == 124) { |
| 2858 | // Version 124: allow OEMs to set a default value for whether IME should be |
| 2859 | // shown when a physical keyboard is connected. |
| 2860 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 2861 | Setting currentSetting = secureSettings.getSettingLocked( |
| 2862 | Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD); |
Svetoslav Ganov | fedb230 | 2016-04-26 18:36:42 -0700 | [diff] [blame] | 2863 | if (currentSetting.isNull()) { |
Prathmesh Prabhu | de16b86 | 2016-03-04 15:22:24 -0800 | [diff] [blame] | 2864 | secureSettings.insertSettingLocked( |
| 2865 | Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD, |
| 2866 | getContext().getResources().getBoolean( |
| 2867 | R.bool.def_show_ime_with_hard_keyboard) ? "1" : "0", |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2868 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Prathmesh Prabhu | de16b86 | 2016-03-04 15:22:24 -0800 | [diff] [blame] | 2869 | } |
| 2870 | currentVersion = 125; |
| 2871 | } |
| 2872 | |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 2873 | if (currentVersion == 125) { |
| 2874 | // Version 125: Allow OEMs to set the default VR service. |
| 2875 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 2876 | |
| 2877 | Setting currentSetting = secureSettings.getSettingLocked( |
| 2878 | Settings.Secure.ENABLED_VR_LISTENERS); |
Svetoslav Ganov | fedb230 | 2016-04-26 18:36:42 -0700 | [diff] [blame] | 2879 | if (currentSetting.isNull()) { |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 2880 | ArraySet<ComponentName> l = |
| 2881 | SystemConfig.getInstance().getDefaultVrComponents(); |
| 2882 | |
| 2883 | if (l != null && !l.isEmpty()) { |
| 2884 | StringBuilder b = new StringBuilder(); |
| 2885 | boolean start = true; |
| 2886 | for (ComponentName c : l) { |
| 2887 | if (!start) { |
| 2888 | b.append(':'); |
| 2889 | } |
| 2890 | b.append(c.flattenToString()); |
| 2891 | start = false; |
| 2892 | } |
| 2893 | secureSettings.insertSettingLocked( |
| 2894 | Settings.Secure.ENABLED_VR_LISTENERS, b.toString(), |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2895 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 2896 | } |
| 2897 | |
| 2898 | } |
| 2899 | currentVersion = 126; |
| 2900 | } |
| 2901 | |
Daniel U | 02ba612 | 2016-04-01 18:41:42 +0100 | [diff] [blame] | 2902 | if (currentVersion == 126) { |
| 2903 | // Version 126: copy the primary values of LOCK_SCREEN_SHOW_NOTIFICATIONS and |
| 2904 | // LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS into managed profile. |
| 2905 | if (mUserManager.isManagedProfile(userId)) { |
| 2906 | final SettingsState systemSecureSettings = |
| 2907 | getSecureSettingsLocked(UserHandle.USER_SYSTEM); |
| 2908 | |
| 2909 | final Setting showNotifications = systemSecureSettings.getSettingLocked( |
| 2910 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS); |
Seigo Nonaka | 6e5b602 | 2016-04-27 16:32:44 +0900 | [diff] [blame] | 2911 | if (!showNotifications.isNull()) { |
Daniel U | 02ba612 | 2016-04-01 18:41:42 +0100 | [diff] [blame] | 2912 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 2913 | secureSettings.insertSettingLocked( |
| 2914 | Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2915 | showNotifications.getValue(), null, true, |
Daniel U | 02ba612 | 2016-04-01 18:41:42 +0100 | [diff] [blame] | 2916 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 2917 | } |
| 2918 | |
| 2919 | final Setting allowPrivate = systemSecureSettings.getSettingLocked( |
| 2920 | Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS); |
Seigo Nonaka | 6e5b602 | 2016-04-27 16:32:44 +0900 | [diff] [blame] | 2921 | if (!allowPrivate.isNull()) { |
Daniel U | 02ba612 | 2016-04-01 18:41:42 +0100 | [diff] [blame] | 2922 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 2923 | secureSettings.insertSettingLocked( |
| 2924 | Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2925 | allowPrivate.getValue(), null, true, |
Daniel U | 02ba612 | 2016-04-01 18:41:42 +0100 | [diff] [blame] | 2926 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 2927 | } |
| 2928 | } |
| 2929 | currentVersion = 127; |
| 2930 | } |
| 2931 | |
Steven Ng | dc20ba6 | 2016-04-26 18:19:04 +0100 | [diff] [blame] | 2932 | if (currentVersion == 127) { |
Mahaver Chopra | 3d9805d | 2016-07-07 16:25:05 +0100 | [diff] [blame] | 2933 | // version 127 is no longer used. |
Steven Ng | dc20ba6 | 2016-04-26 18:19:04 +0100 | [diff] [blame] | 2934 | currentVersion = 128; |
| 2935 | } |
| 2936 | |
Julia Reynolds | 1f721e1 | 2016-07-11 08:50:58 -0400 | [diff] [blame] | 2937 | if (currentVersion == 128) { |
| 2938 | // Version 128: Allow OEMs to grant DND access to default apps. Note that |
| 2939 | // the new apps are appended to the list of already approved apps. |
| 2940 | final SettingsState systemSecureSettings = |
| 2941 | getSecureSettingsLocked(userId); |
| 2942 | |
| 2943 | final Setting policyAccess = systemSecureSettings.getSettingLocked( |
| 2944 | Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES); |
| 2945 | String defaultPolicyAccess = getContext().getResources().getString( |
| 2946 | com.android.internal.R.string.config_defaultDndAccessPackages); |
| 2947 | if (!TextUtils.isEmpty(defaultPolicyAccess)) { |
| 2948 | if (policyAccess.isNull()) { |
| 2949 | systemSecureSettings.insertSettingLocked( |
| 2950 | Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2951 | defaultPolicyAccess, null, true, |
Julia Reynolds | 1f721e1 | 2016-07-11 08:50:58 -0400 | [diff] [blame] | 2952 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 2953 | } else { |
| 2954 | StringBuilder currentSetting = |
| 2955 | new StringBuilder(policyAccess.getValue()); |
| 2956 | currentSetting.append(":"); |
| 2957 | currentSetting.append(defaultPolicyAccess); |
| 2958 | systemSecureSettings.updateSettingLocked( |
| 2959 | Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2960 | currentSetting.toString(), null, true, |
Julia Reynolds | 1f721e1 | 2016-07-11 08:50:58 -0400 | [diff] [blame] | 2961 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 2962 | } |
| 2963 | } |
| 2964 | |
| 2965 | currentVersion = 129; |
| 2966 | } |
| 2967 | |
Dan Sandler | 71f85e9 | 2016-07-20 13:46:05 -0400 | [diff] [blame] | 2968 | if (currentVersion == 129) { |
| 2969 | // default longpress timeout changed from 500 to 400. If unchanged from the old |
| 2970 | // default, update to the new default. |
| 2971 | final SettingsState systemSecureSettings = |
| 2972 | getSecureSettingsLocked(userId); |
| 2973 | final String oldValue = systemSecureSettings.getSettingLocked( |
| 2974 | Settings.Secure.LONG_PRESS_TIMEOUT).getValue(); |
| 2975 | if (TextUtils.equals("500", oldValue)) { |
| 2976 | systemSecureSettings.insertSettingLocked( |
| 2977 | Settings.Secure.LONG_PRESS_TIMEOUT, |
| 2978 | String.valueOf(getContext().getResources().getInteger( |
| 2979 | R.integer.def_long_press_timeout_millis)), |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2980 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Dan Sandler | 71f85e9 | 2016-07-20 13:46:05 -0400 | [diff] [blame] | 2981 | } |
| 2982 | currentVersion = 130; |
| 2983 | } |
| 2984 | |
Anthony Hugh | 96e9cc5 | 2016-07-12 15:17:24 -0700 | [diff] [blame] | 2985 | if (currentVersion == 130) { |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 2986 | // Split Ambient settings |
| 2987 | final SettingsState secureSettings = getSecureSettingsLocked(userId); |
| 2988 | boolean dozeExplicitlyDisabled = "0".equals(secureSettings. |
| 2989 | getSettingLocked(Settings.Secure.DOZE_ENABLED).getValue()); |
| 2990 | |
| 2991 | if (dozeExplicitlyDisabled) { |
| 2992 | secureSettings.insertSettingLocked(Settings.Secure.DOZE_PULSE_ON_PICK_UP, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2993 | "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 2994 | secureSettings.insertSettingLocked(Settings.Secure.DOZE_PULSE_ON_DOUBLE_TAP, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 2995 | "0", null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 2996 | } |
| 2997 | currentVersion = 131; |
| 2998 | } |
| 2999 | |
| 3000 | if (currentVersion == 131) { |
Anthony Hugh | 96e9cc5 | 2016-07-12 15:17:24 -0700 | [diff] [blame] | 3001 | // Initialize new multi-press timeout to default value |
| 3002 | final SettingsState systemSecureSettings = getSecureSettingsLocked(userId); |
| 3003 | final String oldValue = systemSecureSettings.getSettingLocked( |
| 3004 | Settings.Secure.MULTI_PRESS_TIMEOUT).getValue(); |
| 3005 | if (TextUtils.equals(null, oldValue)) { |
| 3006 | systemSecureSettings.insertSettingLocked( |
| 3007 | Settings.Secure.MULTI_PRESS_TIMEOUT, |
| 3008 | String.valueOf(getContext().getResources().getInteger( |
| 3009 | R.integer.def_multi_press_timeout_millis)), |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3010 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Anthony Hugh | 96e9cc5 | 2016-07-12 15:17:24 -0700 | [diff] [blame] | 3011 | } |
| 3012 | |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3013 | currentVersion = 132; |
Anthony Hugh | 96e9cc5 | 2016-07-12 15:17:24 -0700 | [diff] [blame] | 3014 | } |
| 3015 | |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3016 | if (currentVersion == 132) { |
| 3017 | // Version 132: Allow managed profile to optionally use the parent's ringtones |
Andre Lago | ea35e07 | 2016-08-04 13:41:13 +0100 | [diff] [blame] | 3018 | final SettingsState systemSecureSettings = getSecureSettingsLocked(userId); |
| 3019 | String defaultSyncParentSounds = (getContext().getResources() |
| 3020 | .getBoolean(R.bool.def_sync_parent_sounds) ? "1" : "0"); |
| 3021 | systemSecureSettings.insertSettingLocked( |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3022 | Settings.Secure.SYNC_PARENT_SOUNDS, defaultSyncParentSounds, |
| 3023 | null, true, SettingsState.SYSTEM_PACKAGE_NAME); |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3024 | currentVersion = 133; |
Andre Lago | ea35e07 | 2016-08-04 13:41:13 +0100 | [diff] [blame] | 3025 | } |
| 3026 | |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3027 | if (currentVersion == 133) { |
| 3028 | // Version 133: Add default end button behavior |
Keun-young Park | ec7a118 | 2016-10-18 11:52:38 -0700 | [diff] [blame] | 3029 | final SettingsState systemSettings = getSystemSettingsLocked(userId); |
| 3030 | if (systemSettings.getSettingLocked(Settings.System.END_BUTTON_BEHAVIOR) == |
| 3031 | null) { |
| 3032 | String defaultEndButtonBehavior = Integer.toString(getContext() |
| 3033 | .getResources().getInteger(R.integer.def_end_button_behavior)); |
| 3034 | systemSettings.insertSettingLocked(Settings.System.END_BUTTON_BEHAVIOR, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3035 | defaultEndButtonBehavior, null, true, |
| 3036 | SettingsState.SYSTEM_PACKAGE_NAME); |
Keun-young Park | ec7a118 | 2016-10-18 11:52:38 -0700 | [diff] [blame] | 3037 | } |
Adrian Roos | 69741a2 | 2016-10-21 14:49:17 -0700 | [diff] [blame] | 3038 | currentVersion = 134; |
Keun-young Park | ec7a118 | 2016-10-18 11:52:38 -0700 | [diff] [blame] | 3039 | } |
| 3040 | |
Phil Weaver | 89e3ffc | 2016-09-19 13:51:10 -0700 | [diff] [blame] | 3041 | if (currentVersion == 134) { |
| 3042 | // Remove setting that specifies if magnification values should be preserved. |
| 3043 | // This setting defaulted to true and never has a UI. |
| 3044 | getSecureSettingsLocked(userId).deleteSettingLocked( |
| 3045 | Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_AUTO_UPDATE); |
| 3046 | currentVersion = 135; |
| 3047 | } |
| 3048 | |
Jeremy Joslin | 8bdad34 | 2016-12-14 11:46:47 -0800 | [diff] [blame] | 3049 | if (currentVersion == 135) { |
| 3050 | // Version 135: Migrating the NETWORK_SCORER_APP setting to the |
| 3051 | // NETWORK_RECOMMENDATIONS_ENABLED setting. |
| 3052 | if (userId == UserHandle.USER_SYSTEM) { |
| 3053 | final SettingsState globalSettings = getGlobalSettingsLocked(); |
| 3054 | Setting currentSetting = globalSettings.getSettingLocked( |
| 3055 | Global.NETWORK_SCORER_APP); |
| 3056 | if (!currentSetting.isNull()) { |
| 3057 | // A scorer was set so enable recommendations. |
| 3058 | globalSettings.insertSettingLocked( |
| 3059 | Global.NETWORK_RECOMMENDATIONS_ENABLED, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3060 | "1", null, true, |
Jeremy Joslin | 8bdad34 | 2016-12-14 11:46:47 -0800 | [diff] [blame] | 3061 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 3062 | |
| 3063 | // and clear the scorer setting since it's no longer needed. |
| 3064 | globalSettings.insertSettingLocked( |
| 3065 | Global.NETWORK_SCORER_APP, |
Svetoslav Ganov | e080da9 | 2016-12-21 17:10:35 -0800 | [diff] [blame] | 3066 | null, null, true, |
Jeremy Joslin | 8bdad34 | 2016-12-14 11:46:47 -0800 | [diff] [blame] | 3067 | SettingsState.SYSTEM_PACKAGE_NAME); |
| 3068 | } |
| 3069 | } |
| 3070 | currentVersion = 136; |
| 3071 | } |
| 3072 | |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3073 | if (currentVersion == 136) { |
| 3074 | // Version 136: Store legacy SSAID for all apps currently installed on the |
| 3075 | // device as first step in migrating SSAID to be unique per application. |
| 3076 | |
| 3077 | final boolean isUpgrade; |
| 3078 | try { |
| 3079 | isUpgrade = mPackageManager.isUpgrade(); |
| 3080 | } catch (RemoteException e) { |
| 3081 | throw new IllegalStateException("Package manager not available"); |
| 3082 | } |
| 3083 | // Only retain legacy ssaid if the device is performing an OTA. After wiping |
| 3084 | // user data or first boot on a new device should use new ssaid generation. |
| 3085 | if (isUpgrade) { |
| 3086 | // Retrieve the legacy ssaid from the secure settings table. |
Mark Rathjen | ea61759 | 2017-01-18 23:03:41 -0800 | [diff] [blame] | 3087 | final Setting legacySsaidSetting = getSettingLocked(SETTINGS_TYPE_SECURE, |
| 3088 | userId, Settings.Secure.ANDROID_ID); |
| 3089 | if (legacySsaidSetting == null || legacySsaidSetting.isNull() |
| 3090 | || legacySsaidSetting.getValue() == null) { |
| 3091 | throw new IllegalStateException("Legacy ssaid not accessible"); |
| 3092 | } |
| 3093 | final String legacySsaid = legacySsaidSetting.getValue(); |
Mark Rathjen | d891f01 | 2017-01-19 04:10:37 +0000 | [diff] [blame] | 3094 | |
| 3095 | // Fill each uid with the legacy ssaid to be backwards compatible. |
| 3096 | final List<PackageInfo> packages; |
| 3097 | try { |
| 3098 | packages = mPackageManager.getInstalledPackages(0, userId).getList(); |
| 3099 | } catch (RemoteException e) { |
| 3100 | throw new IllegalStateException("Package manager not available"); |
| 3101 | } |
| 3102 | |
| 3103 | final SettingsState ssaidSettings = getSsaidSettingsLocked(userId); |
| 3104 | for (PackageInfo info : packages) { |
| 3105 | // Check if the UID already has an entry in the table. |
| 3106 | final String uid = Integer.toString(info.applicationInfo.uid); |
| 3107 | final Setting ssaid = ssaidSettings.getSettingLocked(uid); |
| 3108 | |
| 3109 | if (ssaid.isNull() || ssaid.getValue() == null) { |
| 3110 | // Android Id doesn't exist for this package so create it. |
| 3111 | ssaidSettings.insertSettingLocked(uid, legacySsaid, null, true, |
| 3112 | info.packageName); |
| 3113 | } |
| 3114 | } |
| 3115 | } |
| 3116 | |
| 3117 | currentVersion = 137; |
| 3118 | } |
| 3119 | |
Dan Sandler | 71f85e9 | 2016-07-20 13:46:05 -0400 | [diff] [blame] | 3120 | if (currentVersion != newVersion) { |
Svetoslav Ganov | 264c7a9 | 2016-08-24 17:31:14 -0700 | [diff] [blame] | 3121 | Slog.wtf("SettingsProvider", "warning: upgrading settings database to version " |
Dan Sandler | 71f85e9 | 2016-07-20 13:46:05 -0400 | [diff] [blame] | 3122 | + newVersion + " left it at " |
| 3123 | + currentVersion + " instead; this is probably a bug", new Throwable()); |
| 3124 | if (DEBUG) { |
| 3125 | throw new RuntimeException("db upgrade error"); |
| 3126 | } |
| 3127 | } |
| 3128 | |
Jeff Brown | 503cffc | 2015-03-26 18:08:51 -0700 | [diff] [blame] | 3129 | // vXXX: Add new settings above this point. |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 3130 | |
Jeff Brown | 503cffc | 2015-03-26 18:08:51 -0700 | [diff] [blame] | 3131 | // Return the current version. |
| 3132 | return currentVersion; |
Brad Fitzpatrick | 547a96b | 2010-03-09 17:58:53 -0800 | [diff] [blame] | 3133 | } |
| 3134 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 3135 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3136 | } |