blob: 158227fe78af23b6e258ae4b9539ad136fd9ad36 [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.providers.settings;
18
-b master501eec92009-07-06 13:53:11 -070019import java.io.FileNotFoundException;
Doug Zongker4f8ff392010-02-03 10:36:40 -080020import java.security.SecureRandom;
Christopher Tate06efb532012-08-24 15:29:27 -070021import java.util.HashSet;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -070022import java.util.concurrent.atomic.AtomicBoolean;
23import java.util.concurrent.atomic.AtomicInteger;
-b master501eec92009-07-06 13:53:11 -070024
Christopher Tated5fe1472012-09-10 15:48:38 -070025import android.app.ActivityManager;
Dianne Hackborn961321f2013-02-05 17:22:41 -080026import android.app.AppOpsManager;
Christopher Tate45281862010-03-05 15:46:30 -080027import android.app.backup.BackupManager;
Christopher Tate06efb532012-08-24 15:29:27 -070028import android.content.BroadcastReceiver;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070029import android.content.ContentProvider;
30import android.content.ContentUris;
31import android.content.ContentValues;
32import android.content.Context;
Christopher Tate06efb532012-08-24 15:29:27 -070033import android.content.Intent;
34import android.content.IntentFilter;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070035import android.content.pm.PackageManager;
Christopher Tate38e7a602013-09-03 16:57:34 -070036import android.content.pm.UserInfo;
Marco Nelissen69f593c2009-07-28 09:55:04 -070037import android.content.res.AssetFileDescriptor;
Christopher Tateafccaa82012-10-03 17:41:51 -070038import android.database.AbstractCursor;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070039import android.database.Cursor;
40import android.database.sqlite.SQLiteDatabase;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080041import android.database.sqlite.SQLiteException;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070042import android.database.sqlite.SQLiteQueryBuilder;
43import android.media.RingtoneManager;
44import android.net.Uri;
Christopher Tate06efb532012-08-24 15:29:27 -070045import android.os.Binder;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080046import android.os.Bundle;
Amith Yamasani5cdf7f52013-06-27 15:12:01 -070047import android.os.DropBoxManager;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -070048import android.os.FileObserver;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070049import android.os.ParcelFileDescriptor;
Christopher Tate0da13572013-10-13 17:34:49 -070050import android.os.Process;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070051import android.os.SystemProperties;
Christopher Tate06efb532012-08-24 15:29:27 -070052import android.os.UserHandle;
53import android.os.UserManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070054import android.provider.MediaStore;
55import android.provider.Settings;
56import android.text.TextUtils;
57import android.util.Log;
Jesse Wilson0c7faee2011-02-10 11:33:19 -080058import android.util.LruCache;
Christopher Tate06efb532012-08-24 15:29:27 -070059import android.util.Slog;
60import android.util.SparseArray;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070061
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070062public class SettingsProvider extends ContentProvider {
63 private static final String TAG = "SettingsProvider";
Christopher Tate4dc7a682012-09-11 12:15:49 -070064 private static final boolean LOCAL_LOGV = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070065
Christopher Tate0da13572013-10-13 17:34:49 -070066 private static final boolean USER_CHECK_THROWS = true;
67
Christopher Tate06efb532012-08-24 15:29:27 -070068 private static final String TABLE_SYSTEM = "system";
69 private static final String TABLE_SECURE = "secure";
70 private static final String TABLE_GLOBAL = "global";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 private static final String TABLE_FAVORITES = "favorites";
72 private static final String TABLE_OLD_FAVORITES = "old_favorites";
73
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080074 private static final String[] COLUMN_VALUE = new String[] { "value" };
75
Christopher Tate06efb532012-08-24 15:29:27 -070076 // Caches for each user's settings, access-ordered for acting as LRU.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -080077 // Guarded by themselves.
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -070078 private static final int MAX_CACHE_ENTRIES = 200;
Christopher Tate06efb532012-08-24 15:29:27 -070079 private static final SparseArray<SettingsCache> sSystemCaches
80 = new SparseArray<SettingsCache>();
81 private static final SparseArray<SettingsCache> sSecureCaches
82 = new SparseArray<SettingsCache>();
83 private static final SettingsCache sGlobalCache = new SettingsCache(TABLE_GLOBAL);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -070084
85 // The count of how many known (handled by SettingsProvider)
Christopher Tate06efb532012-08-24 15:29:27 -070086 // database mutations are currently being handled for this user.
87 // Used by file observers to not reload the database when it's ourselves
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -070088 // modifying it.
Christopher Tate06efb532012-08-24 15:29:27 -070089 private static final SparseArray<AtomicInteger> sKnownMutationsInFlight
90 = new SparseArray<AtomicInteger>();
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -080091
Christopher Tate78d2a662012-09-13 16:19:44 -070092 // Each defined user has their own settings
93 protected final SparseArray<DatabaseHelper> mOpenHelpers = new SparseArray<DatabaseHelper>();
94
Brad Fitzpatrick342984a2010-03-09 16:59:30 -080095 // Over this size we don't reject loading or saving settings but
96 // we do consider them broken/malicious and don't keep them in
97 // memory at least:
98 private static final int MAX_CACHE_ENTRY_SIZE = 500;
99
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800100 private static final Bundle NULL_SETTING = Bundle.forPair("value", null);
101
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700102 // Used as a sentinel value in an instance equality test when we
103 // want to cache the existence of a key, but not store its value.
104 private static final Bundle TOO_LARGE_TO_CACHE_MARKER = Bundle.forPair("_dummy", null);
105
Christopher Tate06efb532012-08-24 15:29:27 -0700106 private UserManager mUserManager;
Amith Yamasani8823c0a82009-07-07 14:30:17 -0700107 private BackupManager mBackupManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700108
109 /**
Christopher Tate06efb532012-08-24 15:29:27 -0700110 * Settings which need to be treated as global/shared in multi-user environments.
111 */
112 static final HashSet<String> sSecureGlobalKeys;
113 static final HashSet<String> sSystemGlobalKeys;
Amith Yamasani5cdf7f52013-06-27 15:12:01 -0700114
115 private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid";
116
Christopher Tate06efb532012-08-24 15:29:27 -0700117 static {
118 // Keys (name column) from the 'secure' table that are now in the owner user's 'global'
119 // table, shared across all users
120 // These must match Settings.Secure.MOVED_TO_GLOBAL
121 sSecureGlobalKeys = new HashSet<String>();
Christopher Tate66488d62012-10-02 11:58:01 -0700122 Settings.Secure.getMovedKeys(sSecureGlobalKeys);
Christopher Tate06efb532012-08-24 15:29:27 -0700123
124 // Keys from the 'system' table now moved to 'global'
125 // These must match Settings.System.MOVED_TO_GLOBAL
126 sSystemGlobalKeys = new HashSet<String>();
Christopher Tate66488d62012-10-02 11:58:01 -0700127 Settings.System.getNonLegacyMovedKeys(sSystemGlobalKeys);
Christopher Tate06efb532012-08-24 15:29:27 -0700128 }
129
130 private boolean settingMovedToGlobal(final String name) {
131 return sSecureGlobalKeys.contains(name) || sSystemGlobalKeys.contains(name);
132 }
133
134 /**
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700135 * Decode a content URL into the table, projection, and arguments
136 * used to access the corresponding database rows.
137 */
138 private static class SqlArguments {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 public String table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700140 public final String where;
141 public final String[] args;
142
143 /** Operate on existing rows. */
144 SqlArguments(Uri url, String where, String[] args) {
145 if (url.getPathSegments().size() == 1) {
Christopher Tatec221d2b2012-10-03 18:33:52 -0700146 // of the form content://settings/secure, arbitrary where clause
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700147 this.table = url.getPathSegments().get(0);
Dianne Hackborn24117ce2010-07-12 15:54:38 -0700148 if (!DatabaseHelper.isValidTable(this.table)) {
149 throw new IllegalArgumentException("Bad root path: " + this.table);
150 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700151 this.where = where;
152 this.args = args;
153 } else if (url.getPathSegments().size() != 2) {
154 throw new IllegalArgumentException("Invalid URI: " + url);
155 } else if (!TextUtils.isEmpty(where)) {
156 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
157 } else {
Christopher Tatec221d2b2012-10-03 18:33:52 -0700158 // of the form content://settings/secure/element_name, no where clause
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700159 this.table = url.getPathSegments().get(0);
Dianne Hackborn24117ce2010-07-12 15:54:38 -0700160 if (!DatabaseHelper.isValidTable(this.table)) {
161 throw new IllegalArgumentException("Bad root path: " + this.table);
162 }
Doug Zongker5bcb5512012-09-24 12:24:54 -0700163 if (TABLE_SYSTEM.equals(this.table) || TABLE_SECURE.equals(this.table) ||
164 TABLE_GLOBAL.equals(this.table)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700165 this.where = Settings.NameValueTable.NAME + "=?";
Christopher Tatec221d2b2012-10-03 18:33:52 -0700166 final String name = url.getPathSegments().get(1);
167 this.args = new String[] { name };
168 // Rewrite the table for known-migrated names
169 if (TABLE_SYSTEM.equals(this.table) || TABLE_SECURE.equals(this.table)) {
170 if (sSecureGlobalKeys.contains(name) || sSystemGlobalKeys.contains(name)) {
171 this.table = TABLE_GLOBAL;
172 }
173 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700174 } else {
Christopher Tatec221d2b2012-10-03 18:33:52 -0700175 // of the form content://bookmarks/19
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700176 this.where = "_id=" + ContentUris.parseId(url);
177 this.args = null;
178 }
179 }
180 }
181
182 /** Insert new rows (no where clause allowed). */
183 SqlArguments(Uri url) {
184 if (url.getPathSegments().size() == 1) {
185 this.table = url.getPathSegments().get(0);
Dianne Hackborn24117ce2010-07-12 15:54:38 -0700186 if (!DatabaseHelper.isValidTable(this.table)) {
187 throw new IllegalArgumentException("Bad root path: " + this.table);
188 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700189 this.where = null;
190 this.args = null;
191 } else {
192 throw new IllegalArgumentException("Invalid URI: " + url);
193 }
194 }
195 }
196
197 /**
198 * Get the content URI of a row added to a table.
199 * @param tableUri of the entire table
200 * @param values found in the row
201 * @param rowId of the row
202 * @return the content URI for this particular row
203 */
204 private Uri getUriFor(Uri tableUri, ContentValues values, long rowId) {
205 if (tableUri.getPathSegments().size() != 1) {
206 throw new IllegalArgumentException("Invalid URI: " + tableUri);
207 }
208 String table = tableUri.getPathSegments().get(0);
Christopher Tate06efb532012-08-24 15:29:27 -0700209 if (TABLE_SYSTEM.equals(table) ||
210 TABLE_SECURE.equals(table) ||
211 TABLE_GLOBAL.equals(table)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700212 String name = values.getAsString(Settings.NameValueTable.NAME);
213 return Uri.withAppendedPath(tableUri, name);
214 } else {
215 return ContentUris.withAppendedId(tableUri, rowId);
216 }
217 }
218
219 /**
220 * Send a notification when a particular content URI changes.
221 * Modify the system property used to communicate the version of
222 * this table, for tables which have such a property. (The Settings
223 * contract class uses these to provide client-side caches.)
224 * @param uri to send notifications for
225 */
Christopher Tate06efb532012-08-24 15:29:27 -0700226 private void sendNotify(Uri uri, int userHandle) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700227 // Update the system property *first*, so if someone is listening for
228 // a notification and then using the contract class to get their data,
229 // the system property will be updated and they'll get the new data.
230
Amith Yamasanid1582142009-07-08 20:04:55 -0700231 boolean backedUpDataChanged = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700232 String property = null, table = uri.getPathSegments().get(0);
Christopher Tate16aa9732012-09-17 16:23:44 -0700233 final boolean isGlobal = table.equals(TABLE_GLOBAL);
Christopher Tate06efb532012-08-24 15:29:27 -0700234 if (table.equals(TABLE_SYSTEM)) {
Dianne Hackborn139748f2012-09-24 11:36:57 -0700235 property = Settings.System.SYS_PROP_SETTING_VERSION;
Amith Yamasanid1582142009-07-08 20:04:55 -0700236 backedUpDataChanged = true;
Christopher Tate06efb532012-08-24 15:29:27 -0700237 } else if (table.equals(TABLE_SECURE)) {
Dianne Hackborn139748f2012-09-24 11:36:57 -0700238 property = Settings.Secure.SYS_PROP_SETTING_VERSION;
Christopher Tate06efb532012-08-24 15:29:27 -0700239 backedUpDataChanged = true;
Christopher Tate16aa9732012-09-17 16:23:44 -0700240 } else if (isGlobal) {
Christopher Tate06efb532012-08-24 15:29:27 -0700241 property = Settings.Global.SYS_PROP_SETTING_VERSION; // this one is global
Amith Yamasanid1582142009-07-08 20:04:55 -0700242 backedUpDataChanged = true;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700243 }
244
245 if (property != null) {
246 long version = SystemProperties.getLong(property, 0) + 1;
247 if (LOCAL_LOGV) Log.v(TAG, "property: " + property + "=" + version);
248 SystemProperties.set(property, Long.toString(version));
249 }
250
-b master501eec92009-07-06 13:53:11 -0700251 // Inform the backup manager about a data change
Amith Yamasanid1582142009-07-08 20:04:55 -0700252 if (backedUpDataChanged) {
253 mBackupManager.dataChanged();
254 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700255 // Now send the notification through the content framework.
256
257 String notify = uri.getQueryParameter("notify");
258 if (notify == null || "true".equals(notify)) {
Christopher Tate16aa9732012-09-17 16:23:44 -0700259 final int notifyTarget = isGlobal ? UserHandle.USER_ALL : userHandle;
Christopher Tatec8459dc82012-09-18 13:27:36 -0700260 final long oldId = Binder.clearCallingIdentity();
261 try {
262 getContext().getContentResolver().notifyChange(uri, null, true, notifyTarget);
263 } finally {
264 Binder.restoreCallingIdentity(oldId);
265 }
Christopher Tate16aa9732012-09-17 16:23:44 -0700266 if (LOCAL_LOGV) Log.v(TAG, "notifying for " + notifyTarget + ": " + uri);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700267 } else {
268 if (LOCAL_LOGV) Log.v(TAG, "notification suppressed: " + uri);
269 }
270 }
271
272 /**
273 * Make sure the caller has permission to write this data.
274 * @param args supplied by the caller
275 * @throws SecurityException if the caller is forbidden to write.
276 */
277 private void checkWritePermissions(SqlArguments args) {
Christopher Tate06efb532012-08-24 15:29:27 -0700278 if ((TABLE_SECURE.equals(args.table) || TABLE_GLOBAL.equals(args.table)) &&
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800279 getContext().checkCallingOrSelfPermission(
Doug Zongkeraed8f8e2010-01-07 18:07:50 -0800280 android.Manifest.permission.WRITE_SECURE_SETTINGS) !=
281 PackageManager.PERMISSION_GRANTED) {
Brett Chabot16dd82c2009-06-18 17:00:48 -0700282 throw new SecurityException(
Doug Zongkeraed8f8e2010-01-07 18:07:50 -0800283 String.format("Permission denial: writing to secure settings requires %1$s",
284 android.Manifest.permission.WRITE_SECURE_SETTINGS));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700285 }
286 }
287
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700288 // FileObserver for external modifications to the database file.
289 // Note that this is for platform developers only with
290 // userdebug/eng builds who should be able to tinker with the
291 // sqlite database out from under the SettingsProvider, which is
292 // normally the exclusive owner of the database. But we keep this
293 // enabled all the time to minimize development-vs-user
294 // differences in testing.
Christopher Tate06efb532012-08-24 15:29:27 -0700295 private static SparseArray<SettingsFileObserver> sObserverInstances
296 = new SparseArray<SettingsFileObserver>();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700297 private class SettingsFileObserver extends FileObserver {
298 private final AtomicBoolean mIsDirty = new AtomicBoolean(false);
Christopher Tate06efb532012-08-24 15:29:27 -0700299 private final int mUserHandle;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700300 private final String mPath;
301
Christopher Tate06efb532012-08-24 15:29:27 -0700302 public SettingsFileObserver(int userHandle, String path) {
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700303 super(path, FileObserver.CLOSE_WRITE |
304 FileObserver.CREATE | FileObserver.DELETE |
305 FileObserver.MOVED_TO | FileObserver.MODIFY);
Christopher Tate06efb532012-08-24 15:29:27 -0700306 mUserHandle = userHandle;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700307 mPath = path;
308 }
309
310 public void onEvent(int event, String path) {
Christopher Tate06efb532012-08-24 15:29:27 -0700311 int modsInFlight = sKnownMutationsInFlight.get(mUserHandle).get();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700312 if (modsInFlight > 0) {
313 // our own modification.
314 return;
315 }
Christopher Tate06efb532012-08-24 15:29:27 -0700316 Log.d(TAG, "User " + mUserHandle + " external modification to " + mPath
317 + "; event=" + event);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700318 if (!mIsDirty.compareAndSet(false, true)) {
319 // already handled. (we get a few update events
320 // during an sqlite write)
321 return;
322 }
Christopher Tate06efb532012-08-24 15:29:27 -0700323 Log.d(TAG, "User " + mUserHandle + " updating our caches for " + mPath);
324 fullyPopulateCaches(mUserHandle);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700325 mIsDirty.set(false);
326 }
327 }
328
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700329 @Override
330 public boolean onCreate() {
Amith Yamasani8823c0a82009-07-07 14:30:17 -0700331 mBackupManager = new BackupManager(getContext());
Amith Yamasani27db4682013-03-30 17:07:47 -0700332 mUserManager = UserManager.get(getContext());
Fred Quintanac70239e2009-12-17 10:28:33 -0800333
Dianne Hackborn961321f2013-02-05 17:22:41 -0800334 setAppOps(AppOpsManager.OP_NONE, AppOpsManager.OP_WRITE_SETTINGS);
Christopher Tate78d2a662012-09-13 16:19:44 -0700335 establishDbTracking(UserHandle.USER_OWNER);
Christopher Tate06efb532012-08-24 15:29:27 -0700336
Christopher Tate78d2a662012-09-13 16:19:44 -0700337 IntentFilter userFilter = new IntentFilter();
338 userFilter.addAction(Intent.ACTION_USER_REMOVED);
339 getContext().registerReceiver(new BroadcastReceiver() {
340 @Override
341 public void onReceive(Context context, Intent intent) {
342 if (intent.getAction().equals(Intent.ACTION_USER_REMOVED)) {
343 final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
344 UserHandle.USER_OWNER);
345 if (userHandle != UserHandle.USER_OWNER) {
346 onUserRemoved(userHandle);
Christopher Tate06efb532012-08-24 15:29:27 -0700347 }
348 }
Christopher Tate78d2a662012-09-13 16:19:44 -0700349 }
350 }, userFilter);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700351 return true;
352 }
353
Christopher Tate06efb532012-08-24 15:29:27 -0700354 void onUserRemoved(int userHandle) {
Christopher Tate06efb532012-08-24 15:29:27 -0700355 synchronized (this) {
Christopher Tate78d2a662012-09-13 16:19:44 -0700356 // the db file itself will be deleted automatically, but we need to tear down
357 // our caches and other internal bookkeeping.
Christopher Tate06efb532012-08-24 15:29:27 -0700358 FileObserver observer = sObserverInstances.get(userHandle);
359 if (observer != null) {
360 observer.stopWatching();
361 sObserverInstances.delete(userHandle);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700362 }
Christopher Tate06efb532012-08-24 15:29:27 -0700363
364 mOpenHelpers.delete(userHandle);
365 sSystemCaches.delete(userHandle);
366 sSecureCaches.delete(userHandle);
367 sKnownMutationsInFlight.delete(userHandle);
Christopher Tate06efb532012-08-24 15:29:27 -0700368 }
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700369 }
370
Christopher Tate78d2a662012-09-13 16:19:44 -0700371 private void establishDbTracking(int userHandle) {
Christopher Tate06efb532012-08-24 15:29:27 -0700372 if (LOCAL_LOGV) {
373 Slog.i(TAG, "Installing settings db helper and caches for user " + userHandle);
374 }
375
Christopher Tate78d2a662012-09-13 16:19:44 -0700376 DatabaseHelper dbhelper;
Christopher Tate06efb532012-08-24 15:29:27 -0700377
Christopher Tate78d2a662012-09-13 16:19:44 -0700378 synchronized (this) {
379 dbhelper = mOpenHelpers.get(userHandle);
380 if (dbhelper == null) {
381 dbhelper = new DatabaseHelper(getContext(), userHandle);
382 mOpenHelpers.append(userHandle, dbhelper);
383
384 sSystemCaches.append(userHandle, new SettingsCache(TABLE_SYSTEM));
385 sSecureCaches.append(userHandle, new SettingsCache(TABLE_SECURE));
386 sKnownMutationsInFlight.append(userHandle, new AtomicInteger(0));
387 }
388 }
389
390 // Initialization of the db *outside* the locks. It's possible that racing
391 // threads might wind up here, the second having read the cache entries
392 // written by the first, but that's benign: the SQLite helper implementation
393 // manages concurrency itself, and it's important that we not run the db
394 // initialization with any of our own locks held, so we're fine.
Christopher Tate06efb532012-08-24 15:29:27 -0700395 SQLiteDatabase db = dbhelper.getWritableDatabase();
396
Christopher Tate78d2a662012-09-13 16:19:44 -0700397 // Watch for external modifications to the database files,
398 // keeping our caches in sync. We synchronize the observer set
399 // separately, and of course it has to run after the db file
400 // itself was set up by the DatabaseHelper.
401 synchronized (sObserverInstances) {
402 if (sObserverInstances.get(userHandle) == null) {
403 SettingsFileObserver observer = new SettingsFileObserver(userHandle, db.getPath());
404 sObserverInstances.append(userHandle, observer);
405 observer.startWatching();
406 }
407 }
Christopher Tate06efb532012-08-24 15:29:27 -0700408
Christopher Tate4dc7a682012-09-11 12:15:49 -0700409 ensureAndroidIdIsSet(userHandle);
410
Christopher Tate06efb532012-08-24 15:29:27 -0700411 startAsyncCachePopulation(userHandle);
412 }
413
414 class CachePrefetchThread extends Thread {
415 private int mUserHandle;
416
417 CachePrefetchThread(int userHandle) {
418 super("populate-settings-caches");
419 mUserHandle = userHandle;
420 }
421
422 @Override
423 public void run() {
424 fullyPopulateCaches(mUserHandle);
425 }
426 }
427
428 private void startAsyncCachePopulation(int userHandle) {
429 new CachePrefetchThread(userHandle).start();
430 }
431
432 private void fullyPopulateCaches(final int userHandle) {
433 DatabaseHelper dbHelper = mOpenHelpers.get(userHandle);
434 // Only populate the globals cache once, for the owning user
435 if (userHandle == UserHandle.USER_OWNER) {
436 fullyPopulateCache(dbHelper, TABLE_GLOBAL, sGlobalCache);
437 }
438 fullyPopulateCache(dbHelper, TABLE_SECURE, sSecureCaches.get(userHandle));
439 fullyPopulateCache(dbHelper, TABLE_SYSTEM, sSystemCaches.get(userHandle));
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700440 }
441
442 // Slurp all values (if sane in number & size) into cache.
Christopher Tate06efb532012-08-24 15:29:27 -0700443 private void fullyPopulateCache(DatabaseHelper dbHelper, String table, SettingsCache cache) {
444 SQLiteDatabase db = dbHelper.getReadableDatabase();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700445 Cursor c = db.query(
446 table,
447 new String[] { Settings.NameValueTable.NAME, Settings.NameValueTable.VALUE },
448 null, null, null, null, null,
449 "" + (MAX_CACHE_ENTRIES + 1) /* limit */);
450 try {
451 synchronized (cache) {
Jesse Wilson0c7faee2011-02-10 11:33:19 -0800452 cache.evictAll();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700453 cache.setFullyMatchesDisk(true); // optimistic
454 int rows = 0;
455 while (c.moveToNext()) {
456 rows++;
457 String name = c.getString(0);
458 String value = c.getString(1);
459 cache.populate(name, value);
460 }
461 if (rows > MAX_CACHE_ENTRIES) {
462 // Somewhat redundant, as removeEldestEntry() will
463 // have already done this, but to be explicit:
464 cache.setFullyMatchesDisk(false);
465 Log.d(TAG, "row count exceeds max cache entries for table " + table);
466 }
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800467 if (LOCAL_LOGV) Log.d(TAG, "cache for settings table '" + table
468 + "' rows=" + rows + "; fullycached=" + cache.fullyMatchesDisk());
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700469 }
470 } finally {
471 c.close();
472 }
473 }
474
Christopher Tate4dc7a682012-09-11 12:15:49 -0700475 private boolean ensureAndroidIdIsSet(int userHandle) {
476 final Cursor c = queryForUser(Settings.Secure.CONTENT_URI,
Fred Quintanac70239e2009-12-17 10:28:33 -0800477 new String[] { Settings.NameValueTable.VALUE },
478 Settings.NameValueTable.NAME + "=?",
Christopher Tate4dc7a682012-09-11 12:15:49 -0700479 new String[] { Settings.Secure.ANDROID_ID }, null,
480 userHandle);
Fred Quintanac70239e2009-12-17 10:28:33 -0800481 try {
482 final String value = c.moveToNext() ? c.getString(0) : null;
483 if (value == null) {
Christopher Tate38e7a602013-09-03 16:57:34 -0700484 // sanity-check the user before touching the db
485 final UserInfo user = mUserManager.getUserInfo(userHandle);
486 if (user == null) {
487 // can happen due to races when deleting users; treat as benign
488 return false;
489 }
490
Nick Kralevich9bb4ec42010-09-24 11:48:37 -0700491 final SecureRandom random = new SecureRandom();
Fred Quintanac70239e2009-12-17 10:28:33 -0800492 final String newAndroidIdValue = Long.toHexString(random.nextLong());
Fred Quintanac70239e2009-12-17 10:28:33 -0800493 final ContentValues values = new ContentValues();
494 values.put(Settings.NameValueTable.NAME, Settings.Secure.ANDROID_ID);
495 values.put(Settings.NameValueTable.VALUE, newAndroidIdValue);
Christopher Tate4dc7a682012-09-11 12:15:49 -0700496 final Uri uri = insertForUser(Settings.Secure.CONTENT_URI, values, userHandle);
Fred Quintanac70239e2009-12-17 10:28:33 -0800497 if (uri == null) {
Christopher Tate4dc7a682012-09-11 12:15:49 -0700498 Slog.e(TAG, "Unable to generate new ANDROID_ID for user " + userHandle);
Fred Quintanac70239e2009-12-17 10:28:33 -0800499 return false;
500 }
Christopher Tate4dc7a682012-09-11 12:15:49 -0700501 Slog.d(TAG, "Generated and saved new ANDROID_ID [" + newAndroidIdValue
502 + "] for user " + userHandle);
Amith Yamasani5cdf7f52013-06-27 15:12:01 -0700503 // Write a dropbox entry if it's a restricted profile
Christopher Tate38e7a602013-09-03 16:57:34 -0700504 if (user.isRestricted()) {
Amith Yamasani5cdf7f52013-06-27 15:12:01 -0700505 DropBoxManager dbm = (DropBoxManager)
506 getContext().getSystemService(Context.DROPBOX_SERVICE);
507 if (dbm != null && dbm.isTagEnabled(DROPBOX_TAG_USERLOG)) {
508 dbm.addText(DROPBOX_TAG_USERLOG, System.currentTimeMillis()
509 + ",restricted_profile_ssaid,"
510 + newAndroidIdValue + "\n");
511 }
512 }
Fred Quintanac70239e2009-12-17 10:28:33 -0800513 }
514 return true;
Fred Quintanac70239e2009-12-17 10:28:33 -0800515 } finally {
516 c.close();
517 }
518 }
519
Christopher Tate06efb532012-08-24 15:29:27 -0700520 // Lazy-initialize the settings caches for non-primary users
521 private SettingsCache getOrConstructCache(int callingUser, SparseArray<SettingsCache> which) {
Christopher Tate78d2a662012-09-13 16:19:44 -0700522 getOrEstablishDatabase(callingUser); // ignore return value; we don't need it
523 return which.get(callingUser);
Christopher Tate06efb532012-08-24 15:29:27 -0700524 }
525
526 // Lazy initialize the database helper and caches for this user, if necessary
Christopher Tate78d2a662012-09-13 16:19:44 -0700527 private DatabaseHelper getOrEstablishDatabase(int callingUser) {
Christopher Tate0da13572013-10-13 17:34:49 -0700528 if (callingUser >= Process.SYSTEM_UID) {
529 if (USER_CHECK_THROWS) {
530 throw new IllegalArgumentException("Uid rather than user handle: " + callingUser);
531 } else {
532 Slog.wtf(TAG, "establish db for uid rather than user: " + callingUser);
533 }
534 }
535
Christopher Tate06efb532012-08-24 15:29:27 -0700536 long oldId = Binder.clearCallingIdentity();
537 try {
538 DatabaseHelper dbHelper = mOpenHelpers.get(callingUser);
539 if (null == dbHelper) {
Christopher Tate78d2a662012-09-13 16:19:44 -0700540 establishDbTracking(callingUser);
Christopher Tate06efb532012-08-24 15:29:27 -0700541 dbHelper = mOpenHelpers.get(callingUser);
542 }
543 return dbHelper;
544 } finally {
545 Binder.restoreCallingIdentity(oldId);
546 }
547 }
548
549 public SettingsCache cacheForTable(final int callingUser, String tableName) {
550 if (TABLE_SYSTEM.equals(tableName)) {
551 return getOrConstructCache(callingUser, sSystemCaches);
552 }
553 if (TABLE_SECURE.equals(tableName)) {
554 return getOrConstructCache(callingUser, sSecureCaches);
555 }
556 if (TABLE_GLOBAL.equals(tableName)) {
557 return sGlobalCache;
558 }
559 return null;
560 }
561
562 /**
563 * Used for wiping a whole cache on deletes when we're not
564 * sure what exactly was deleted or changed.
565 */
566 public void invalidateCache(final int callingUser, String tableName) {
567 SettingsCache cache = cacheForTable(callingUser, tableName);
568 if (cache == null) {
569 return;
570 }
571 synchronized (cache) {
572 cache.evictAll();
573 cache.mCacheFullyMatchesDisk = false;
574 }
575 }
576
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800577 /**
578 * Fast path that avoids the use of chatty remoted Cursors.
579 */
580 @Override
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700581 public Bundle call(String method, String request, Bundle args) {
Christopher Tate06efb532012-08-24 15:29:27 -0700582 int callingUser = UserHandle.getCallingUserId();
583 if (args != null) {
584 int reqUser = args.getInt(Settings.CALL_METHOD_USER_KEY, callingUser);
585 if (reqUser != callingUser) {
Christopher Tated5fe1472012-09-10 15:48:38 -0700586 callingUser = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
587 Binder.getCallingUid(), reqUser, false, true,
588 "get/set setting for user", null);
589 if (LOCAL_LOGV) Slog.v(TAG, " access setting for user " + callingUser);
Christopher Tate06efb532012-08-24 15:29:27 -0700590 }
591 }
592
Christopher Tate61695ff2012-10-05 12:05:13 -0700593 // Note: we assume that get/put operations for moved-to-global names have already
594 // been directed to the new location on the caller side (otherwise we'd fix them
595 // up here).
596 DatabaseHelper dbHelper;
597 SettingsCache cache;
Christopher Tate06efb532012-08-24 15:29:27 -0700598
Christopher Tate61695ff2012-10-05 12:05:13 -0700599 // Get methods
600 if (Settings.CALL_METHOD_GET_SYSTEM.equals(method)) {
601 if (LOCAL_LOGV) Slog.v(TAG, "call(system:" + request + ") for " + callingUser);
602 dbHelper = getOrEstablishDatabase(callingUser);
603 cache = sSystemCaches.get(callingUser);
604 return lookupValue(dbHelper, TABLE_SYSTEM, cache, request);
605 }
606 if (Settings.CALL_METHOD_GET_SECURE.equals(method)) {
607 if (LOCAL_LOGV) Slog.v(TAG, "call(secure:" + request + ") for " + callingUser);
608 dbHelper = getOrEstablishDatabase(callingUser);
609 cache = sSecureCaches.get(callingUser);
610 return lookupValue(dbHelper, TABLE_SECURE, cache, request);
611 }
612 if (Settings.CALL_METHOD_GET_GLOBAL.equals(method)) {
613 if (LOCAL_LOGV) Slog.v(TAG, "call(global:" + request + ") for " + callingUser);
614 // fast path: owner db & cache are immutable after onCreate() so we need not
615 // guard on the attempt to look them up
616 return lookupValue(getOrEstablishDatabase(UserHandle.USER_OWNER), TABLE_GLOBAL,
617 sGlobalCache, request);
618 }
Christopher Tate06efb532012-08-24 15:29:27 -0700619
Christopher Tate61695ff2012-10-05 12:05:13 -0700620 // Put methods - new value is in the args bundle under the key named by
621 // the Settings.NameValueTable.VALUE static.
622 final String newValue = (args == null)
Dianne Hackborn961321f2013-02-05 17:22:41 -0800623 ? null : args.getString(Settings.NameValueTable.VALUE);
624
625 // Framework can't do automatic permission checking for calls, so we need
626 // to do it here.
627 if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.WRITE_SETTINGS)
628 != PackageManager.PERMISSION_GRANTED) {
629 throw new SecurityException(
630 String.format("Permission denial: writing to settings requires %1$s",
631 android.Manifest.permission.WRITE_SETTINGS));
632 }
633
634 // Also need to take care of app op.
635 if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SETTINGS, Binder.getCallingUid(),
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700636 getCallingPackage()) != AppOpsManager.MODE_ALLOWED) {
Dianne Hackborn961321f2013-02-05 17:22:41 -0800637 return null;
638 }
Christopher Tate06efb532012-08-24 15:29:27 -0700639
Christopher Tate61695ff2012-10-05 12:05:13 -0700640 final ContentValues values = new ContentValues();
641 values.put(Settings.NameValueTable.NAME, request);
642 values.put(Settings.NameValueTable.VALUE, newValue);
643 if (Settings.CALL_METHOD_PUT_SYSTEM.equals(method)) {
644 if (LOCAL_LOGV) Slog.v(TAG, "call_put(system:" + request + "=" + newValue + ") for " + callingUser);
645 insertForUser(Settings.System.CONTENT_URI, values, callingUser);
646 } else if (Settings.CALL_METHOD_PUT_SECURE.equals(method)) {
647 if (LOCAL_LOGV) Slog.v(TAG, "call_put(secure:" + request + "=" + newValue + ") for " + callingUser);
648 insertForUser(Settings.Secure.CONTENT_URI, values, callingUser);
649 } else if (Settings.CALL_METHOD_PUT_GLOBAL.equals(method)) {
650 if (LOCAL_LOGV) Slog.v(TAG, "call_put(global:" + request + "=" + newValue + ") for " + callingUser);
651 insertForUser(Settings.Global.CONTENT_URI, values, callingUser);
652 } else {
653 Slog.w(TAG, "call() with invalid method: " + method);
Christopher Tate06efb532012-08-24 15:29:27 -0700654 }
655
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800656 return null;
657 }
658
659 // Looks up value 'key' in 'table' and returns either a single-pair Bundle,
660 // possibly with a null value, or null on failure.
Christopher Tate06efb532012-08-24 15:29:27 -0700661 private Bundle lookupValue(DatabaseHelper dbHelper, String table,
662 final SettingsCache cache, String key) {
663 if (cache == null) {
664 Slog.e(TAG, "cache is null for user " + UserHandle.getCallingUserId() + " : key=" + key);
665 return null;
666 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800667 synchronized (cache) {
Jesse Wilson0c7faee2011-02-10 11:33:19 -0800668 Bundle value = cache.get(key);
669 if (value != null) {
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700670 if (value != TOO_LARGE_TO_CACHE_MARKER) {
671 return value;
672 }
673 // else we fall through and read the value from disk
674 } else if (cache.fullyMatchesDisk()) {
675 // Fast path (very common). Don't even try touch disk
676 // if we know we've slurped it all in. Trying to
677 // touch the disk would mean waiting for yaffs2 to
678 // give us access, which could takes hundreds of
679 // milliseconds. And we're very likely being called
680 // from somebody's UI thread...
681 return NULL_SETTING;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800682 }
683 }
684
Christopher Tate06efb532012-08-24 15:29:27 -0700685 SQLiteDatabase db = dbHelper.getReadableDatabase();
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800686 Cursor cursor = null;
687 try {
688 cursor = db.query(table, COLUMN_VALUE, "name=?", new String[]{key},
689 null, null, null, null);
690 if (cursor != null && cursor.getCount() == 1) {
691 cursor.moveToFirst();
Brad Fitzpatrick342984a2010-03-09 16:59:30 -0800692 return cache.putIfAbsent(key, cursor.getString(0));
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800693 }
694 } catch (SQLiteException e) {
695 Log.w(TAG, "settings lookup error", e);
696 return null;
697 } finally {
698 if (cursor != null) cursor.close();
699 }
Brad Fitzpatrick342984a2010-03-09 16:59:30 -0800700 cache.putIfAbsent(key, null);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800701 return NULL_SETTING;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800702 }
703
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700704 @Override
705 public Cursor query(Uri url, String[] select, String where, String[] whereArgs, String sort) {
Christopher Tate4dc7a682012-09-11 12:15:49 -0700706 return queryForUser(url, select, where, whereArgs, sort, UserHandle.getCallingUserId());
707 }
708
Christopher Tateb7564452012-09-19 16:21:18 -0700709 private Cursor queryForUser(Uri url, String[] select, String where, String[] whereArgs,
Christopher Tate4dc7a682012-09-11 12:15:49 -0700710 String sort, int forUser) {
711 if (LOCAL_LOGV) Slog.v(TAG, "query(" + url + ") for user " + forUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700712 SqlArguments args = new SqlArguments(url, where, whereArgs);
Christopher Tate06efb532012-08-24 15:29:27 -0700713 DatabaseHelper dbH;
Christopher Tate78d2a662012-09-13 16:19:44 -0700714 dbH = getOrEstablishDatabase(
715 TABLE_GLOBAL.equals(args.table) ? UserHandle.USER_OWNER : forUser);
Christopher Tate06efb532012-08-24 15:29:27 -0700716 SQLiteDatabase db = dbH.getReadableDatabase();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800718 // The favorites table was moved from this provider to a provider inside Home
719 // Home still need to query this table to upgrade from pre-cupcake builds
720 // However, a cupcake+ build with no data does not contain this table which will
721 // cause an exception in the SQL stack. The following line is a special case to
722 // let the caller of the query have a chance to recover and avoid the exception
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 if (TABLE_FAVORITES.equals(args.table)) {
724 return null;
725 } else if (TABLE_OLD_FAVORITES.equals(args.table)) {
726 args.table = TABLE_FAVORITES;
727 Cursor cursor = db.rawQuery("PRAGMA table_info(favorites);", null);
728 if (cursor != null) {
729 boolean exists = cursor.getCount() > 0;
730 cursor.close();
731 if (!exists) return null;
732 } else {
733 return null;
734 }
735 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800736
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700737 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
738 qb.setTables(args.table);
739
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700740 Cursor ret = qb.query(db, select, args.where, args.args, null, null, sort);
Christopher Tateafccaa82012-10-03 17:41:51 -0700741 // the default Cursor interface does not support per-user observation
742 try {
743 AbstractCursor c = (AbstractCursor) ret;
744 c.setNotificationUri(getContext().getContentResolver(), url, forUser);
745 } catch (ClassCastException e) {
746 // details of the concrete Cursor implementation have changed and this code has
747 // not been updated to match -- complain and fail hard.
748 Log.wtf(TAG, "Incompatible cursor derivation!");
749 throw e;
750 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700751 return ret;
752 }
753
754 @Override
755 public String getType(Uri url) {
756 // If SqlArguments supplies a where clause, then it must be an item
757 // (because we aren't supplying our own where clause).
758 SqlArguments args = new SqlArguments(url, null, null);
759 if (TextUtils.isEmpty(args.where)) {
760 return "vnd.android.cursor.dir/" + args.table;
761 } else {
762 return "vnd.android.cursor.item/" + args.table;
763 }
764 }
765
766 @Override
767 public int bulkInsert(Uri uri, ContentValues[] values) {
Christopher Tate06efb532012-08-24 15:29:27 -0700768 final int callingUser = UserHandle.getCallingUserId();
769 if (LOCAL_LOGV) Slog.v(TAG, "bulkInsert() for user " + callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700770 SqlArguments args = new SqlArguments(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 if (TABLE_FAVORITES.equals(args.table)) {
772 return 0;
773 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700774 checkWritePermissions(args);
Christopher Tate06efb532012-08-24 15:29:27 -0700775 SettingsCache cache = cacheForTable(callingUser, args.table);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700776
Christopher Tate06efb532012-08-24 15:29:27 -0700777 final AtomicInteger mutationCount = sKnownMutationsInFlight.get(callingUser);
778 mutationCount.incrementAndGet();
Christopher Tate78d2a662012-09-13 16:19:44 -0700779 DatabaseHelper dbH = getOrEstablishDatabase(
780 TABLE_GLOBAL.equals(args.table) ? UserHandle.USER_OWNER : callingUser);
Christopher Tate06efb532012-08-24 15:29:27 -0700781 SQLiteDatabase db = dbH.getWritableDatabase();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700782 db.beginTransaction();
783 try {
784 int numValues = values.length;
785 for (int i = 0; i < numValues; i++) {
786 if (db.insert(args.table, null, values[i]) < 0) return 0;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800787 SettingsCache.populate(cache, values[i]);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700788 if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + values[i]);
789 }
790 db.setTransactionSuccessful();
791 } finally {
792 db.endTransaction();
Christopher Tate06efb532012-08-24 15:29:27 -0700793 mutationCount.decrementAndGet();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700794 }
795
Christopher Tate06efb532012-08-24 15:29:27 -0700796 sendNotify(uri, callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700797 return values.length;
798 }
799
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700800 /*
801 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
802 * This setting contains a list of the currently enabled location providers.
803 * But helper functions in android.providers.Settings can enable or disable
804 * a single provider by using a "+" or "-" prefix before the provider name.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800805 *
806 * @returns whether the database needs to be updated or not, also modifying
807 * 'initialValues' if needed.
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700808 */
Maggie Benthalld2726582013-02-04 13:28:19 -0500809 private boolean parseProviderList(Uri url, ContentValues initialValues, int desiredUser) {
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700810 String value = initialValues.getAsString(Settings.Secure.VALUE);
811 String newProviders = null;
812 if (value != null && value.length() > 1) {
813 char prefix = value.charAt(0);
814 if (prefix == '+' || prefix == '-') {
815 // skip prefix
816 value = value.substring(1);
817
818 // read list of enabled providers into "providers"
819 String providers = "";
820 String[] columns = {Settings.Secure.VALUE};
821 String where = Settings.Secure.NAME + "=\'" + Settings.Secure.LOCATION_PROVIDERS_ALLOWED + "\'";
Maggie Benthalld2726582013-02-04 13:28:19 -0500822 Cursor cursor = queryForUser(url, columns, where, null, null, desiredUser);
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700823 if (cursor != null && cursor.getCount() == 1) {
824 try {
825 cursor.moveToFirst();
826 providers = cursor.getString(0);
827 } finally {
828 cursor.close();
829 }
830 }
831
832 int index = providers.indexOf(value);
833 int end = index + value.length();
834 // check for commas to avoid matching on partial string
835 if (index > 0 && providers.charAt(index - 1) != ',') index = -1;
836 if (end < providers.length() && providers.charAt(end) != ',') index = -1;
837
838 if (prefix == '+' && index < 0) {
839 // append the provider to the list if not present
840 if (providers.length() == 0) {
841 newProviders = value;
842 } else {
843 newProviders = providers + ',' + value;
844 }
845 } else if (prefix == '-' && index >= 0) {
846 // remove the provider from the list if present
Mike Lockwoodbdc7f892010-04-21 18:24:57 -0400847 // remove leading or trailing comma
848 if (index > 0) {
849 index--;
850 } else if (end < providers.length()) {
851 end++;
852 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700853
854 newProviders = providers.substring(0, index);
855 if (end < providers.length()) {
856 newProviders += providers.substring(end);
857 }
858 } else {
859 // nothing changed, so no need to update the database
860 return false;
861 }
862
863 if (newProviders != null) {
864 initialValues.put(Settings.Secure.VALUE, newProviders);
865 }
866 }
867 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800868
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700869 return true;
870 }
871
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700872 @Override
873 public Uri insert(Uri url, ContentValues initialValues) {
Christopher Tate06efb532012-08-24 15:29:27 -0700874 return insertForUser(url, initialValues, UserHandle.getCallingUserId());
875 }
876
877 // Settings.put*ForUser() always winds up here, so this is where we apply
878 // policy around permission to write settings for other users.
879 private Uri insertForUser(Uri url, ContentValues initialValues, int desiredUserHandle) {
880 final int callingUser = UserHandle.getCallingUserId();
881 if (callingUser != desiredUserHandle) {
Christopher Tate4dc7a682012-09-11 12:15:49 -0700882 getContext().enforceCallingOrSelfPermission(
Christopher Tate06efb532012-08-24 15:29:27 -0700883 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
884 "Not permitted to access settings for other users");
885 }
886
887 if (LOCAL_LOGV) Slog.v(TAG, "insert(" + url + ") for user " + desiredUserHandle
888 + " by " + callingUser);
889
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700890 SqlArguments args = new SqlArguments(url);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 if (TABLE_FAVORITES.equals(args.table)) {
892 return null;
893 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700894
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700895 // Special case LOCATION_PROVIDERS_ALLOWED.
896 // Support enabling/disabling a single provider (using "+" or "-" prefix)
897 String name = initialValues.getAsString(Settings.Secure.NAME);
898 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
Maggie Benthalld2726582013-02-04 13:28:19 -0500899 if (!parseProviderList(url, initialValues, desiredUserHandle)) return null;
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700900 }
901
Christopher Tatec221d2b2012-10-03 18:33:52 -0700902 // If this is an insert() of a key that has been migrated to the global store,
903 // redirect the operation to that store
904 if (name != null) {
905 if (sSecureGlobalKeys.contains(name) || sSystemGlobalKeys.contains(name)) {
906 if (!TABLE_GLOBAL.equals(args.table)) {
907 if (LOCAL_LOGV) Slog.i(TAG, "Rewrite of insert() of now-global key " + name);
908 }
909 args.table = TABLE_GLOBAL; // next condition will rewrite the user handle
910 }
911 }
912
Christopher Tate34637e52012-10-04 15:00:00 -0700913 // Check write permissions only after determining which table the insert will touch
914 checkWritePermissions(args);
915
Christopher Tate06efb532012-08-24 15:29:27 -0700916 // The global table is stored under the owner, always
917 if (TABLE_GLOBAL.equals(args.table)) {
918 desiredUserHandle = UserHandle.USER_OWNER;
919 }
920
921 SettingsCache cache = cacheForTable(desiredUserHandle, args.table);
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -0800922 String value = initialValues.getAsString(Settings.NameValueTable.VALUE);
923 if (SettingsCache.isRedundantSetValue(cache, name, value)) {
924 return Uri.withAppendedPath(url, name);
925 }
926
Christopher Tate06efb532012-08-24 15:29:27 -0700927 final AtomicInteger mutationCount = sKnownMutationsInFlight.get(desiredUserHandle);
928 mutationCount.incrementAndGet();
Christopher Tate78d2a662012-09-13 16:19:44 -0700929 DatabaseHelper dbH = getOrEstablishDatabase(desiredUserHandle);
Christopher Tate06efb532012-08-24 15:29:27 -0700930 SQLiteDatabase db = dbH.getWritableDatabase();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700931 final long rowId = db.insert(args.table, null, initialValues);
Christopher Tate06efb532012-08-24 15:29:27 -0700932 mutationCount.decrementAndGet();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700933 if (rowId <= 0) return null;
934
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800935 SettingsCache.populate(cache, initialValues); // before we notify
936
Christopher Tate78d2a662012-09-13 16:19:44 -0700937 if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + initialValues
938 + " for user " + desiredUserHandle);
Christopher Tate06efb532012-08-24 15:29:27 -0700939 // Note that we use the original url here, not the potentially-rewritten table name
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700940 url = getUriFor(url, initialValues, rowId);
Christopher Tate06efb532012-08-24 15:29:27 -0700941 sendNotify(url, desiredUserHandle);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700942 return url;
943 }
944
945 @Override
946 public int delete(Uri url, String where, String[] whereArgs) {
Christopher Tate4dc7a682012-09-11 12:15:49 -0700947 int callingUser = UserHandle.getCallingUserId();
Christopher Tate06efb532012-08-24 15:29:27 -0700948 if (LOCAL_LOGV) Slog.v(TAG, "delete() for user " + callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700949 SqlArguments args = new SqlArguments(url, where, whereArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 if (TABLE_FAVORITES.equals(args.table)) {
951 return 0;
952 } else if (TABLE_OLD_FAVORITES.equals(args.table)) {
953 args.table = TABLE_FAVORITES;
Christopher Tate4dc7a682012-09-11 12:15:49 -0700954 } else if (TABLE_GLOBAL.equals(args.table)) {
955 callingUser = UserHandle.USER_OWNER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700957 checkWritePermissions(args);
958
Christopher Tate06efb532012-08-24 15:29:27 -0700959 final AtomicInteger mutationCount = sKnownMutationsInFlight.get(callingUser);
960 mutationCount.incrementAndGet();
Christopher Tate78d2a662012-09-13 16:19:44 -0700961 DatabaseHelper dbH = getOrEstablishDatabase(callingUser);
Christopher Tate06efb532012-08-24 15:29:27 -0700962 SQLiteDatabase db = dbH.getWritableDatabase();
963 int count = db.delete(args.table, args.where, args.args);
964 mutationCount.decrementAndGet();
965 if (count > 0) {
966 invalidateCache(callingUser, args.table); // before we notify
967 sendNotify(url, callingUser);
968 }
969 startAsyncCachePopulation(callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700970 if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) deleted");
971 return count;
972 }
973
974 @Override
975 public int update(Uri url, ContentValues initialValues, String where, String[] whereArgs) {
Christopher Tate06efb532012-08-24 15:29:27 -0700976 // NOTE: update() is never called by the front-end Settings API, and updates that
977 // wind up affecting rows in Secure that are globally shared will not have the
978 // intended effect (the update will be invisible to the rest of the system).
979 // This should have no practical effect, since writes to the Secure db can only
980 // be done by system code, and that code should be using the correct API up front.
Christopher Tate4dc7a682012-09-11 12:15:49 -0700981 int callingUser = UserHandle.getCallingUserId();
Christopher Tate06efb532012-08-24 15:29:27 -0700982 if (LOCAL_LOGV) Slog.v(TAG, "update() for user " + callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700983 SqlArguments args = new SqlArguments(url, where, whereArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 if (TABLE_FAVORITES.equals(args.table)) {
985 return 0;
Christopher Tate4dc7a682012-09-11 12:15:49 -0700986 } else if (TABLE_GLOBAL.equals(args.table)) {
987 callingUser = UserHandle.USER_OWNER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700989 checkWritePermissions(args);
990
Christopher Tate06efb532012-08-24 15:29:27 -0700991 final AtomicInteger mutationCount = sKnownMutationsInFlight.get(callingUser);
992 mutationCount.incrementAndGet();
Christopher Tate78d2a662012-09-13 16:19:44 -0700993 DatabaseHelper dbH = getOrEstablishDatabase(callingUser);
Christopher Tate06efb532012-08-24 15:29:27 -0700994 SQLiteDatabase db = dbH.getWritableDatabase();
995 int count = db.update(args.table, initialValues, args.where, args.args);
996 mutationCount.decrementAndGet();
997 if (count > 0) {
998 invalidateCache(callingUser, args.table); // before we notify
999 sendNotify(url, callingUser);
1000 }
1001 startAsyncCachePopulation(callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001002 if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) <- " + initialValues);
1003 return count;
1004 }
1005
1006 @Override
1007 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
1008
1009 /*
1010 * When a client attempts to openFile the default ringtone or
1011 * notification setting Uri, we will proxy the call to the current
Mike Lockwood853ad6f2013-04-29 16:12:23 -07001012 * default ringtone's Uri (if it is in the media provider).
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001013 */
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001014 int ringtoneType = RingtoneManager.getDefaultType(uri);
1015 // Above call returns -1 if the Uri doesn't match a default type
1016 if (ringtoneType != -1) {
1017 Context context = getContext();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001018
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001019 // Get the current value for the default sound
1020 Uri soundUri = RingtoneManager.getActualDefaultRingtoneUri(context, ringtoneType);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001021
Marco Nelissen69f593c2009-07-28 09:55:04 -07001022 if (soundUri != null) {
Mike Lockwood853ad6f2013-04-29 16:12:23 -07001023 // Proxy the openFile call to media provider
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001024 String authority = soundUri.getAuthority();
Mike Lockwood853ad6f2013-04-29 16:12:23 -07001025 if (authority.equals(MediaStore.AUTHORITY)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001026 return context.getContentResolver().openFileDescriptor(soundUri, mode);
1027 }
1028 }
1029 }
1030
1031 return super.openFile(uri, mode);
1032 }
Marco Nelissen69f593c2009-07-28 09:55:04 -07001033
1034 @Override
1035 public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
1036
1037 /*
1038 * When a client attempts to openFile the default ringtone or
1039 * notification setting Uri, we will proxy the call to the current
Mike Lockwood853ad6f2013-04-29 16:12:23 -07001040 * default ringtone's Uri (if it is in the media provider).
Marco Nelissen69f593c2009-07-28 09:55:04 -07001041 */
1042 int ringtoneType = RingtoneManager.getDefaultType(uri);
1043 // Above call returns -1 if the Uri doesn't match a default type
1044 if (ringtoneType != -1) {
1045 Context context = getContext();
1046
1047 // Get the current value for the default sound
1048 Uri soundUri = RingtoneManager.getActualDefaultRingtoneUri(context, ringtoneType);
1049
1050 if (soundUri != null) {
Mike Lockwood853ad6f2013-04-29 16:12:23 -07001051 // Proxy the openFile call to media provider
Marco Nelissen69f593c2009-07-28 09:55:04 -07001052 String authority = soundUri.getAuthority();
Mike Lockwood853ad6f2013-04-29 16:12:23 -07001053 if (authority.equals(MediaStore.AUTHORITY)) {
Marco Nelissen69f593c2009-07-28 09:55:04 -07001054 ParcelFileDescriptor pfd = null;
1055 try {
1056 pfd = context.getContentResolver().openFileDescriptor(soundUri, mode);
1057 return new AssetFileDescriptor(pfd, 0, -1);
1058 } catch (FileNotFoundException ex) {
1059 // fall through and open the fallback ringtone below
1060 }
1061 }
1062
1063 try {
1064 return super.openAssetFile(soundUri, mode);
1065 } catch (FileNotFoundException ex) {
1066 // Since a non-null Uri was specified, but couldn't be opened,
1067 // fall back to the built-in ringtone.
1068 return context.getResources().openRawResourceFd(
1069 com.android.internal.R.raw.fallbackring);
1070 }
1071 }
1072 // no need to fall through and have openFile() try again, since we
1073 // already know that will fail.
1074 throw new FileNotFoundException(); // or return null ?
1075 }
1076
1077 // Note that this will end up calling openFile() above.
1078 return super.openAssetFile(uri, mode);
1079 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001080
1081 /**
1082 * In-memory LRU Cache of system and secure settings, along with
1083 * associated helper functions to keep cache coherent with the
1084 * database.
1085 */
Jesse Wilson0c7faee2011-02-10 11:33:19 -08001086 private static final class SettingsCache extends LruCache<String, Bundle> {
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001087
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001088 private final String mCacheName;
1089 private boolean mCacheFullyMatchesDisk = false; // has the whole database slurped.
1090
1091 public SettingsCache(String name) {
Jesse Wilson0c7faee2011-02-10 11:33:19 -08001092 super(MAX_CACHE_ENTRIES);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001093 mCacheName = name;
1094 }
1095
1096 /**
1097 * Is the whole database table slurped into this cache?
1098 */
1099 public boolean fullyMatchesDisk() {
1100 synchronized (this) {
1101 return mCacheFullyMatchesDisk;
1102 }
1103 }
1104
1105 public void setFullyMatchesDisk(boolean value) {
1106 synchronized (this) {
1107 mCacheFullyMatchesDisk = value;
1108 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001109 }
1110
1111 @Override
Jesse Wilson32c80a22011-02-25 17:28:41 -08001112 protected void entryRemoved(boolean evicted, String key, Bundle oldValue, Bundle newValue) {
1113 if (evicted) {
1114 mCacheFullyMatchesDisk = false;
1115 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001116 }
1117
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001118 /**
1119 * Atomic cache population, conditional on size of value and if
1120 * we lost a race.
1121 *
1122 * @returns a Bundle to send back to the client from call(), even
1123 * if we lost the race.
1124 */
1125 public Bundle putIfAbsent(String key, String value) {
1126 Bundle bundle = (value == null) ? NULL_SETTING : Bundle.forPair("value", value);
1127 if (value == null || value.length() <= MAX_CACHE_ENTRY_SIZE) {
1128 synchronized (this) {
Jesse Wilson0c7faee2011-02-10 11:33:19 -08001129 if (get(key) == null) {
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001130 put(key, bundle);
1131 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001132 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001133 }
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001134 return bundle;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001135 }
1136
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001137 /**
1138 * Populates a key in a given (possibly-null) cache.
1139 */
1140 public static void populate(SettingsCache cache, ContentValues contentValues) {
1141 if (cache == null) {
1142 return;
1143 }
1144 String name = contentValues.getAsString(Settings.NameValueTable.NAME);
1145 if (name == null) {
1146 Log.w(TAG, "null name populating settings cache.");
1147 return;
1148 }
1149 String value = contentValues.getAsString(Settings.NameValueTable.VALUE);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001150 cache.populate(name, value);
1151 }
1152
1153 public void populate(String name, String value) {
1154 synchronized (this) {
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001155 if (value == null || value.length() <= MAX_CACHE_ENTRY_SIZE) {
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001156 put(name, Bundle.forPair(Settings.NameValueTable.VALUE, value));
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001157 } else {
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001158 put(name, TOO_LARGE_TO_CACHE_MARKER);
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001159 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001160 }
1161 }
1162
1163 /**
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -08001164 * For suppressing duplicate/redundant settings inserts early,
1165 * checking our cache first (but without faulting it in),
1166 * before going to sqlite with the mutation.
1167 */
1168 public static boolean isRedundantSetValue(SettingsCache cache, String name, String value) {
1169 if (cache == null) return false;
1170 synchronized (cache) {
1171 Bundle bundle = cache.get(name);
1172 if (bundle == null) return false;
1173 String oldValue = bundle.getPairValue();
1174 if (oldValue == null && value == null) return true;
1175 if ((oldValue == null) != (value == null)) return false;
1176 return oldValue.equals(value);
1177 }
1178 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001179 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001180}