blob: f18338a69541a7e3b54fff5eacfa3d0395187a92 [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;
Christopher Tate45281862010-03-05 15:46:30 -080026import android.app.backup.BackupManager;
Christopher Tate06efb532012-08-24 15:29:27 -070027import android.content.BroadcastReceiver;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070028import android.content.ContentProvider;
29import android.content.ContentUris;
30import android.content.ContentValues;
31import android.content.Context;
Christopher Tate06efb532012-08-24 15:29:27 -070032import android.content.Intent;
33import android.content.IntentFilter;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070034import android.content.pm.PackageManager;
Marco Nelissen69f593c2009-07-28 09:55:04 -070035import android.content.res.AssetFileDescriptor;
Christopher Tateafccaa82012-10-03 17:41:51 -070036import android.database.AbstractCursor;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070037import android.database.Cursor;
38import android.database.sqlite.SQLiteDatabase;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080039import android.database.sqlite.SQLiteException;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070040import android.database.sqlite.SQLiteQueryBuilder;
41import android.media.RingtoneManager;
42import android.net.Uri;
Christopher Tate06efb532012-08-24 15:29:27 -070043import android.os.Binder;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080044import android.os.Bundle;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -070045import android.os.FileObserver;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070046import android.os.ParcelFileDescriptor;
47import android.os.SystemProperties;
Christopher Tate06efb532012-08-24 15:29:27 -070048import android.os.UserHandle;
49import android.os.UserManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070050import android.provider.DrmStore;
51import android.provider.MediaStore;
52import android.provider.Settings;
53import android.text.TextUtils;
54import android.util.Log;
Jesse Wilson0c7faee2011-02-10 11:33:19 -080055import android.util.LruCache;
Christopher Tate06efb532012-08-24 15:29:27 -070056import android.util.Slog;
57import android.util.SparseArray;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070058
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070059public class SettingsProvider extends ContentProvider {
60 private static final String TAG = "SettingsProvider";
Christopher Tate4dc7a682012-09-11 12:15:49 -070061 private static final boolean LOCAL_LOGV = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070062
Christopher Tate06efb532012-08-24 15:29:27 -070063 private static final String TABLE_SYSTEM = "system";
64 private static final String TABLE_SECURE = "secure";
65 private static final String TABLE_GLOBAL = "global";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 private static final String TABLE_FAVORITES = "favorites";
67 private static final String TABLE_OLD_FAVORITES = "old_favorites";
68
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080069 private static final String[] COLUMN_VALUE = new String[] { "value" };
70
Christopher Tate06efb532012-08-24 15:29:27 -070071 // Caches for each user's settings, access-ordered for acting as LRU.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -080072 // Guarded by themselves.
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -070073 private static final int MAX_CACHE_ENTRIES = 200;
Christopher Tate06efb532012-08-24 15:29:27 -070074 private static final SparseArray<SettingsCache> sSystemCaches
75 = new SparseArray<SettingsCache>();
76 private static final SparseArray<SettingsCache> sSecureCaches
77 = new SparseArray<SettingsCache>();
78 private static final SettingsCache sGlobalCache = new SettingsCache(TABLE_GLOBAL);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -070079
80 // The count of how many known (handled by SettingsProvider)
Christopher Tate06efb532012-08-24 15:29:27 -070081 // database mutations are currently being handled for this user.
82 // Used by file observers to not reload the database when it's ourselves
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -070083 // modifying it.
Christopher Tate06efb532012-08-24 15:29:27 -070084 private static final SparseArray<AtomicInteger> sKnownMutationsInFlight
85 = new SparseArray<AtomicInteger>();
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -080086
Christopher Tate78d2a662012-09-13 16:19:44 -070087 // Each defined user has their own settings
88 protected final SparseArray<DatabaseHelper> mOpenHelpers = new SparseArray<DatabaseHelper>();
89
Brad Fitzpatrick342984a2010-03-09 16:59:30 -080090 // Over this size we don't reject loading or saving settings but
91 // we do consider them broken/malicious and don't keep them in
92 // memory at least:
93 private static final int MAX_CACHE_ENTRY_SIZE = 500;
94
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -080095 private static final Bundle NULL_SETTING = Bundle.forPair("value", null);
96
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -070097 // Used as a sentinel value in an instance equality test when we
98 // want to cache the existence of a key, but not store its value.
99 private static final Bundle TOO_LARGE_TO_CACHE_MARKER = Bundle.forPair("_dummy", null);
100
Christopher Tate06efb532012-08-24 15:29:27 -0700101 private UserManager mUserManager;
Amith Yamasani8823c0a82009-07-07 14:30:17 -0700102 private BackupManager mBackupManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700103
104 /**
Christopher Tate06efb532012-08-24 15:29:27 -0700105 * Settings which need to be treated as global/shared in multi-user environments.
106 */
107 static final HashSet<String> sSecureGlobalKeys;
108 static final HashSet<String> sSystemGlobalKeys;
109 static {
110 // Keys (name column) from the 'secure' table that are now in the owner user's 'global'
111 // table, shared across all users
112 // These must match Settings.Secure.MOVED_TO_GLOBAL
113 sSecureGlobalKeys = new HashSet<String>();
Christopher Tate66488d62012-10-02 11:58:01 -0700114 Settings.Secure.getMovedKeys(sSecureGlobalKeys);
Christopher Tate06efb532012-08-24 15:29:27 -0700115
116 // Keys from the 'system' table now moved to 'global'
117 // These must match Settings.System.MOVED_TO_GLOBAL
118 sSystemGlobalKeys = new HashSet<String>();
Christopher Tate66488d62012-10-02 11:58:01 -0700119 Settings.System.getNonLegacyMovedKeys(sSystemGlobalKeys);
Christopher Tate06efb532012-08-24 15:29:27 -0700120 }
121
122 private boolean settingMovedToGlobal(final String name) {
123 return sSecureGlobalKeys.contains(name) || sSystemGlobalKeys.contains(name);
124 }
125
126 /**
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700127 * Decode a content URL into the table, projection, and arguments
128 * used to access the corresponding database rows.
129 */
130 private static class SqlArguments {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 public String table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700132 public final String where;
133 public final String[] args;
134
135 /** Operate on existing rows. */
136 SqlArguments(Uri url, String where, String[] args) {
137 if (url.getPathSegments().size() == 1) {
Christopher Tatec221d2b2012-10-03 18:33:52 -0700138 // of the form content://settings/secure, arbitrary where clause
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700139 this.table = url.getPathSegments().get(0);
Dianne Hackborn24117ce2010-07-12 15:54:38 -0700140 if (!DatabaseHelper.isValidTable(this.table)) {
141 throw new IllegalArgumentException("Bad root path: " + this.table);
142 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700143 this.where = where;
144 this.args = args;
145 } else if (url.getPathSegments().size() != 2) {
146 throw new IllegalArgumentException("Invalid URI: " + url);
147 } else if (!TextUtils.isEmpty(where)) {
148 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
149 } else {
Christopher Tatec221d2b2012-10-03 18:33:52 -0700150 // of the form content://settings/secure/element_name, no where clause
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700151 this.table = url.getPathSegments().get(0);
Dianne Hackborn24117ce2010-07-12 15:54:38 -0700152 if (!DatabaseHelper.isValidTable(this.table)) {
153 throw new IllegalArgumentException("Bad root path: " + this.table);
154 }
Doug Zongker5bcb5512012-09-24 12:24:54 -0700155 if (TABLE_SYSTEM.equals(this.table) || TABLE_SECURE.equals(this.table) ||
156 TABLE_GLOBAL.equals(this.table)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700157 this.where = Settings.NameValueTable.NAME + "=?";
Christopher Tatec221d2b2012-10-03 18:33:52 -0700158 final String name = url.getPathSegments().get(1);
159 this.args = new String[] { name };
160 // Rewrite the table for known-migrated names
161 if (TABLE_SYSTEM.equals(this.table) || TABLE_SECURE.equals(this.table)) {
162 if (sSecureGlobalKeys.contains(name) || sSystemGlobalKeys.contains(name)) {
163 this.table = TABLE_GLOBAL;
164 }
165 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700166 } else {
Christopher Tatec221d2b2012-10-03 18:33:52 -0700167 // of the form content://bookmarks/19
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700168 this.where = "_id=" + ContentUris.parseId(url);
169 this.args = null;
170 }
171 }
172 }
173
174 /** Insert new rows (no where clause allowed). */
175 SqlArguments(Uri url) {
176 if (url.getPathSegments().size() == 1) {
177 this.table = url.getPathSegments().get(0);
Dianne Hackborn24117ce2010-07-12 15:54:38 -0700178 if (!DatabaseHelper.isValidTable(this.table)) {
179 throw new IllegalArgumentException("Bad root path: " + this.table);
180 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700181 this.where = null;
182 this.args = null;
183 } else {
184 throw new IllegalArgumentException("Invalid URI: " + url);
185 }
186 }
187 }
188
189 /**
190 * Get the content URI of a row added to a table.
191 * @param tableUri of the entire table
192 * @param values found in the row
193 * @param rowId of the row
194 * @return the content URI for this particular row
195 */
196 private Uri getUriFor(Uri tableUri, ContentValues values, long rowId) {
197 if (tableUri.getPathSegments().size() != 1) {
198 throw new IllegalArgumentException("Invalid URI: " + tableUri);
199 }
200 String table = tableUri.getPathSegments().get(0);
Christopher Tate06efb532012-08-24 15:29:27 -0700201 if (TABLE_SYSTEM.equals(table) ||
202 TABLE_SECURE.equals(table) ||
203 TABLE_GLOBAL.equals(table)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700204 String name = values.getAsString(Settings.NameValueTable.NAME);
205 return Uri.withAppendedPath(tableUri, name);
206 } else {
207 return ContentUris.withAppendedId(tableUri, rowId);
208 }
209 }
210
211 /**
212 * Send a notification when a particular content URI changes.
213 * Modify the system property used to communicate the version of
214 * this table, for tables which have such a property. (The Settings
215 * contract class uses these to provide client-side caches.)
216 * @param uri to send notifications for
217 */
Christopher Tate06efb532012-08-24 15:29:27 -0700218 private void sendNotify(Uri uri, int userHandle) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700219 // Update the system property *first*, so if someone is listening for
220 // a notification and then using the contract class to get their data,
221 // the system property will be updated and they'll get the new data.
222
Amith Yamasanid1582142009-07-08 20:04:55 -0700223 boolean backedUpDataChanged = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700224 String property = null, table = uri.getPathSegments().get(0);
Christopher Tate16aa9732012-09-17 16:23:44 -0700225 final boolean isGlobal = table.equals(TABLE_GLOBAL);
Christopher Tate06efb532012-08-24 15:29:27 -0700226 if (table.equals(TABLE_SYSTEM)) {
Dianne Hackborn139748f2012-09-24 11:36:57 -0700227 property = Settings.System.SYS_PROP_SETTING_VERSION;
Amith Yamasanid1582142009-07-08 20:04:55 -0700228 backedUpDataChanged = true;
Christopher Tate06efb532012-08-24 15:29:27 -0700229 } else if (table.equals(TABLE_SECURE)) {
Dianne Hackborn139748f2012-09-24 11:36:57 -0700230 property = Settings.Secure.SYS_PROP_SETTING_VERSION;
Christopher Tate06efb532012-08-24 15:29:27 -0700231 backedUpDataChanged = true;
Christopher Tate16aa9732012-09-17 16:23:44 -0700232 } else if (isGlobal) {
Christopher Tate06efb532012-08-24 15:29:27 -0700233 property = Settings.Global.SYS_PROP_SETTING_VERSION; // this one is global
Amith Yamasanid1582142009-07-08 20:04:55 -0700234 backedUpDataChanged = true;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700235 }
236
237 if (property != null) {
238 long version = SystemProperties.getLong(property, 0) + 1;
239 if (LOCAL_LOGV) Log.v(TAG, "property: " + property + "=" + version);
240 SystemProperties.set(property, Long.toString(version));
241 }
242
-b master501eec92009-07-06 13:53:11 -0700243 // Inform the backup manager about a data change
Amith Yamasanid1582142009-07-08 20:04:55 -0700244 if (backedUpDataChanged) {
245 mBackupManager.dataChanged();
246 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700247 // Now send the notification through the content framework.
248
249 String notify = uri.getQueryParameter("notify");
250 if (notify == null || "true".equals(notify)) {
Christopher Tate16aa9732012-09-17 16:23:44 -0700251 final int notifyTarget = isGlobal ? UserHandle.USER_ALL : userHandle;
Christopher Tatec8459dc82012-09-18 13:27:36 -0700252 final long oldId = Binder.clearCallingIdentity();
253 try {
254 getContext().getContentResolver().notifyChange(uri, null, true, notifyTarget);
255 } finally {
256 Binder.restoreCallingIdentity(oldId);
257 }
Christopher Tate16aa9732012-09-17 16:23:44 -0700258 if (LOCAL_LOGV) Log.v(TAG, "notifying for " + notifyTarget + ": " + uri);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700259 } else {
260 if (LOCAL_LOGV) Log.v(TAG, "notification suppressed: " + uri);
261 }
262 }
263
264 /**
265 * Make sure the caller has permission to write this data.
266 * @param args supplied by the caller
267 * @throws SecurityException if the caller is forbidden to write.
268 */
269 private void checkWritePermissions(SqlArguments args) {
Christopher Tate06efb532012-08-24 15:29:27 -0700270 if ((TABLE_SECURE.equals(args.table) || TABLE_GLOBAL.equals(args.table)) &&
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800271 getContext().checkCallingOrSelfPermission(
Doug Zongkeraed8f8e2010-01-07 18:07:50 -0800272 android.Manifest.permission.WRITE_SECURE_SETTINGS) !=
273 PackageManager.PERMISSION_GRANTED) {
Brett Chabot16dd82c2009-06-18 17:00:48 -0700274 throw new SecurityException(
Doug Zongkeraed8f8e2010-01-07 18:07:50 -0800275 String.format("Permission denial: writing to secure settings requires %1$s",
276 android.Manifest.permission.WRITE_SECURE_SETTINGS));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700277 }
278 }
279
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700280 // FileObserver for external modifications to the database file.
281 // Note that this is for platform developers only with
282 // userdebug/eng builds who should be able to tinker with the
283 // sqlite database out from under the SettingsProvider, which is
284 // normally the exclusive owner of the database. But we keep this
285 // enabled all the time to minimize development-vs-user
286 // differences in testing.
Christopher Tate06efb532012-08-24 15:29:27 -0700287 private static SparseArray<SettingsFileObserver> sObserverInstances
288 = new SparseArray<SettingsFileObserver>();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700289 private class SettingsFileObserver extends FileObserver {
290 private final AtomicBoolean mIsDirty = new AtomicBoolean(false);
Christopher Tate06efb532012-08-24 15:29:27 -0700291 private final int mUserHandle;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700292 private final String mPath;
293
Christopher Tate06efb532012-08-24 15:29:27 -0700294 public SettingsFileObserver(int userHandle, String path) {
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700295 super(path, FileObserver.CLOSE_WRITE |
296 FileObserver.CREATE | FileObserver.DELETE |
297 FileObserver.MOVED_TO | FileObserver.MODIFY);
Christopher Tate06efb532012-08-24 15:29:27 -0700298 mUserHandle = userHandle;
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700299 mPath = path;
300 }
301
302 public void onEvent(int event, String path) {
Christopher Tate06efb532012-08-24 15:29:27 -0700303 int modsInFlight = sKnownMutationsInFlight.get(mUserHandle).get();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700304 if (modsInFlight > 0) {
305 // our own modification.
306 return;
307 }
Christopher Tate06efb532012-08-24 15:29:27 -0700308 Log.d(TAG, "User " + mUserHandle + " external modification to " + mPath
309 + "; event=" + event);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700310 if (!mIsDirty.compareAndSet(false, true)) {
311 // already handled. (we get a few update events
312 // during an sqlite write)
313 return;
314 }
Christopher Tate06efb532012-08-24 15:29:27 -0700315 Log.d(TAG, "User " + mUserHandle + " updating our caches for " + mPath);
316 fullyPopulateCaches(mUserHandle);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700317 mIsDirty.set(false);
318 }
319 }
320
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700321 @Override
322 public boolean onCreate() {
Amith Yamasani8823c0a82009-07-07 14:30:17 -0700323 mBackupManager = new BackupManager(getContext());
Christopher Tate06efb532012-08-24 15:29:27 -0700324 mUserManager = (UserManager) getContext().getSystemService(Context.USER_SERVICE);
Fred Quintanac70239e2009-12-17 10:28:33 -0800325
Christopher Tate78d2a662012-09-13 16:19:44 -0700326 establishDbTracking(UserHandle.USER_OWNER);
Christopher Tate06efb532012-08-24 15:29:27 -0700327
Christopher Tate78d2a662012-09-13 16:19:44 -0700328 IntentFilter userFilter = new IntentFilter();
329 userFilter.addAction(Intent.ACTION_USER_REMOVED);
330 getContext().registerReceiver(new BroadcastReceiver() {
331 @Override
332 public void onReceive(Context context, Intent intent) {
333 if (intent.getAction().equals(Intent.ACTION_USER_REMOVED)) {
334 final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
335 UserHandle.USER_OWNER);
336 if (userHandle != UserHandle.USER_OWNER) {
337 onUserRemoved(userHandle);
Christopher Tate06efb532012-08-24 15:29:27 -0700338 }
339 }
Christopher Tate78d2a662012-09-13 16:19:44 -0700340 }
341 }, userFilter);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700342 return true;
343 }
344
Christopher Tate06efb532012-08-24 15:29:27 -0700345 void onUserRemoved(int userHandle) {
Christopher Tate06efb532012-08-24 15:29:27 -0700346 synchronized (this) {
Christopher Tate78d2a662012-09-13 16:19:44 -0700347 // the db file itself will be deleted automatically, but we need to tear down
348 // our caches and other internal bookkeeping.
Christopher Tate06efb532012-08-24 15:29:27 -0700349 FileObserver observer = sObserverInstances.get(userHandle);
350 if (observer != null) {
351 observer.stopWatching();
352 sObserverInstances.delete(userHandle);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700353 }
Christopher Tate06efb532012-08-24 15:29:27 -0700354
355 mOpenHelpers.delete(userHandle);
356 sSystemCaches.delete(userHandle);
357 sSecureCaches.delete(userHandle);
358 sKnownMutationsInFlight.delete(userHandle);
Christopher Tate06efb532012-08-24 15:29:27 -0700359 }
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700360 }
361
Christopher Tate78d2a662012-09-13 16:19:44 -0700362 private void establishDbTracking(int userHandle) {
Christopher Tate06efb532012-08-24 15:29:27 -0700363 if (LOCAL_LOGV) {
364 Slog.i(TAG, "Installing settings db helper and caches for user " + userHandle);
365 }
366
Christopher Tate78d2a662012-09-13 16:19:44 -0700367 DatabaseHelper dbhelper;
Christopher Tate06efb532012-08-24 15:29:27 -0700368
Christopher Tate78d2a662012-09-13 16:19:44 -0700369 synchronized (this) {
370 dbhelper = mOpenHelpers.get(userHandle);
371 if (dbhelper == null) {
372 dbhelper = new DatabaseHelper(getContext(), userHandle);
373 mOpenHelpers.append(userHandle, dbhelper);
374
375 sSystemCaches.append(userHandle, new SettingsCache(TABLE_SYSTEM));
376 sSecureCaches.append(userHandle, new SettingsCache(TABLE_SECURE));
377 sKnownMutationsInFlight.append(userHandle, new AtomicInteger(0));
378 }
379 }
380
381 // Initialization of the db *outside* the locks. It's possible that racing
382 // threads might wind up here, the second having read the cache entries
383 // written by the first, but that's benign: the SQLite helper implementation
384 // manages concurrency itself, and it's important that we not run the db
385 // initialization with any of our own locks held, so we're fine.
Christopher Tate06efb532012-08-24 15:29:27 -0700386 SQLiteDatabase db = dbhelper.getWritableDatabase();
387
Christopher Tate78d2a662012-09-13 16:19:44 -0700388 // Watch for external modifications to the database files,
389 // keeping our caches in sync. We synchronize the observer set
390 // separately, and of course it has to run after the db file
391 // itself was set up by the DatabaseHelper.
392 synchronized (sObserverInstances) {
393 if (sObserverInstances.get(userHandle) == null) {
394 SettingsFileObserver observer = new SettingsFileObserver(userHandle, db.getPath());
395 sObserverInstances.append(userHandle, observer);
396 observer.startWatching();
397 }
398 }
Christopher Tate06efb532012-08-24 15:29:27 -0700399
Christopher Tate4dc7a682012-09-11 12:15:49 -0700400 ensureAndroidIdIsSet(userHandle);
401
Christopher Tate06efb532012-08-24 15:29:27 -0700402 startAsyncCachePopulation(userHandle);
403 }
404
405 class CachePrefetchThread extends Thread {
406 private int mUserHandle;
407
408 CachePrefetchThread(int userHandle) {
409 super("populate-settings-caches");
410 mUserHandle = userHandle;
411 }
412
413 @Override
414 public void run() {
415 fullyPopulateCaches(mUserHandle);
416 }
417 }
418
419 private void startAsyncCachePopulation(int userHandle) {
420 new CachePrefetchThread(userHandle).start();
421 }
422
423 private void fullyPopulateCaches(final int userHandle) {
424 DatabaseHelper dbHelper = mOpenHelpers.get(userHandle);
425 // Only populate the globals cache once, for the owning user
426 if (userHandle == UserHandle.USER_OWNER) {
427 fullyPopulateCache(dbHelper, TABLE_GLOBAL, sGlobalCache);
428 }
429 fullyPopulateCache(dbHelper, TABLE_SECURE, sSecureCaches.get(userHandle));
430 fullyPopulateCache(dbHelper, TABLE_SYSTEM, sSystemCaches.get(userHandle));
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700431 }
432
433 // Slurp all values (if sane in number & size) into cache.
Christopher Tate06efb532012-08-24 15:29:27 -0700434 private void fullyPopulateCache(DatabaseHelper dbHelper, String table, SettingsCache cache) {
435 SQLiteDatabase db = dbHelper.getReadableDatabase();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700436 Cursor c = db.query(
437 table,
438 new String[] { Settings.NameValueTable.NAME, Settings.NameValueTable.VALUE },
439 null, null, null, null, null,
440 "" + (MAX_CACHE_ENTRIES + 1) /* limit */);
441 try {
442 synchronized (cache) {
Jesse Wilson0c7faee2011-02-10 11:33:19 -0800443 cache.evictAll();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700444 cache.setFullyMatchesDisk(true); // optimistic
445 int rows = 0;
446 while (c.moveToNext()) {
447 rows++;
448 String name = c.getString(0);
449 String value = c.getString(1);
450 cache.populate(name, value);
451 }
452 if (rows > MAX_CACHE_ENTRIES) {
453 // Somewhat redundant, as removeEldestEntry() will
454 // have already done this, but to be explicit:
455 cache.setFullyMatchesDisk(false);
456 Log.d(TAG, "row count exceeds max cache entries for table " + table);
457 }
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800458 if (LOCAL_LOGV) Log.d(TAG, "cache for settings table '" + table
459 + "' rows=" + rows + "; fullycached=" + cache.fullyMatchesDisk());
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700460 }
461 } finally {
462 c.close();
463 }
464 }
465
Christopher Tate4dc7a682012-09-11 12:15:49 -0700466 private boolean ensureAndroidIdIsSet(int userHandle) {
467 final Cursor c = queryForUser(Settings.Secure.CONTENT_URI,
Fred Quintanac70239e2009-12-17 10:28:33 -0800468 new String[] { Settings.NameValueTable.VALUE },
469 Settings.NameValueTable.NAME + "=?",
Christopher Tate4dc7a682012-09-11 12:15:49 -0700470 new String[] { Settings.Secure.ANDROID_ID }, null,
471 userHandle);
Fred Quintanac70239e2009-12-17 10:28:33 -0800472 try {
473 final String value = c.moveToNext() ? c.getString(0) : null;
474 if (value == null) {
Nick Kralevich9bb4ec42010-09-24 11:48:37 -0700475 final SecureRandom random = new SecureRandom();
Fred Quintanac70239e2009-12-17 10:28:33 -0800476 final String newAndroidIdValue = Long.toHexString(random.nextLong());
Fred Quintanac70239e2009-12-17 10:28:33 -0800477 final ContentValues values = new ContentValues();
478 values.put(Settings.NameValueTable.NAME, Settings.Secure.ANDROID_ID);
479 values.put(Settings.NameValueTable.VALUE, newAndroidIdValue);
Christopher Tate4dc7a682012-09-11 12:15:49 -0700480 final Uri uri = insertForUser(Settings.Secure.CONTENT_URI, values, userHandle);
Fred Quintanac70239e2009-12-17 10:28:33 -0800481 if (uri == null) {
Christopher Tate4dc7a682012-09-11 12:15:49 -0700482 Slog.e(TAG, "Unable to generate new ANDROID_ID for user " + userHandle);
Fred Quintanac70239e2009-12-17 10:28:33 -0800483 return false;
484 }
Christopher Tate4dc7a682012-09-11 12:15:49 -0700485 Slog.d(TAG, "Generated and saved new ANDROID_ID [" + newAndroidIdValue
486 + "] for user " + userHandle);
Fred Quintanac70239e2009-12-17 10:28:33 -0800487 }
488 return true;
Fred Quintanac70239e2009-12-17 10:28:33 -0800489 } finally {
490 c.close();
491 }
492 }
493
Christopher Tate06efb532012-08-24 15:29:27 -0700494 // Lazy-initialize the settings caches for non-primary users
495 private SettingsCache getOrConstructCache(int callingUser, SparseArray<SettingsCache> which) {
Christopher Tate78d2a662012-09-13 16:19:44 -0700496 getOrEstablishDatabase(callingUser); // ignore return value; we don't need it
497 return which.get(callingUser);
Christopher Tate06efb532012-08-24 15:29:27 -0700498 }
499
500 // Lazy initialize the database helper and caches for this user, if necessary
Christopher Tate78d2a662012-09-13 16:19:44 -0700501 private DatabaseHelper getOrEstablishDatabase(int callingUser) {
Christopher Tate06efb532012-08-24 15:29:27 -0700502 long oldId = Binder.clearCallingIdentity();
503 try {
504 DatabaseHelper dbHelper = mOpenHelpers.get(callingUser);
505 if (null == dbHelper) {
Christopher Tate78d2a662012-09-13 16:19:44 -0700506 establishDbTracking(callingUser);
Christopher Tate06efb532012-08-24 15:29:27 -0700507 dbHelper = mOpenHelpers.get(callingUser);
508 }
509 return dbHelper;
510 } finally {
511 Binder.restoreCallingIdentity(oldId);
512 }
513 }
514
515 public SettingsCache cacheForTable(final int callingUser, String tableName) {
516 if (TABLE_SYSTEM.equals(tableName)) {
517 return getOrConstructCache(callingUser, sSystemCaches);
518 }
519 if (TABLE_SECURE.equals(tableName)) {
520 return getOrConstructCache(callingUser, sSecureCaches);
521 }
522 if (TABLE_GLOBAL.equals(tableName)) {
523 return sGlobalCache;
524 }
525 return null;
526 }
527
528 /**
529 * Used for wiping a whole cache on deletes when we're not
530 * sure what exactly was deleted or changed.
531 */
532 public void invalidateCache(final int callingUser, String tableName) {
533 SettingsCache cache = cacheForTable(callingUser, tableName);
534 if (cache == null) {
535 return;
536 }
537 synchronized (cache) {
538 cache.evictAll();
539 cache.mCacheFullyMatchesDisk = false;
540 }
541 }
542
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800543 /**
544 * Fast path that avoids the use of chatty remoted Cursors.
545 */
546 @Override
547 public Bundle call(String method, String request, Bundle args) {
Christopher Tate06efb532012-08-24 15:29:27 -0700548 int callingUser = UserHandle.getCallingUserId();
549 if (args != null) {
550 int reqUser = args.getInt(Settings.CALL_METHOD_USER_KEY, callingUser);
551 if (reqUser != callingUser) {
Christopher Tated5fe1472012-09-10 15:48:38 -0700552 callingUser = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
553 Binder.getCallingUid(), reqUser, false, true,
554 "get/set setting for user", null);
555 if (LOCAL_LOGV) Slog.v(TAG, " access setting for user " + callingUser);
Christopher Tate06efb532012-08-24 15:29:27 -0700556 }
557 }
558
Christopher Tate61695ff2012-10-05 12:05:13 -0700559 // Note: we assume that get/put operations for moved-to-global names have already
560 // been directed to the new location on the caller side (otherwise we'd fix them
561 // up here).
562 DatabaseHelper dbHelper;
563 SettingsCache cache;
Christopher Tate06efb532012-08-24 15:29:27 -0700564
Christopher Tate61695ff2012-10-05 12:05:13 -0700565 // Get methods
566 if (Settings.CALL_METHOD_GET_SYSTEM.equals(method)) {
567 if (LOCAL_LOGV) Slog.v(TAG, "call(system:" + request + ") for " + callingUser);
568 dbHelper = getOrEstablishDatabase(callingUser);
569 cache = sSystemCaches.get(callingUser);
570 return lookupValue(dbHelper, TABLE_SYSTEM, cache, request);
571 }
572 if (Settings.CALL_METHOD_GET_SECURE.equals(method)) {
573 if (LOCAL_LOGV) Slog.v(TAG, "call(secure:" + request + ") for " + callingUser);
574 dbHelper = getOrEstablishDatabase(callingUser);
575 cache = sSecureCaches.get(callingUser);
576 return lookupValue(dbHelper, TABLE_SECURE, cache, request);
577 }
578 if (Settings.CALL_METHOD_GET_GLOBAL.equals(method)) {
579 if (LOCAL_LOGV) Slog.v(TAG, "call(global:" + request + ") for " + callingUser);
580 // fast path: owner db & cache are immutable after onCreate() so we need not
581 // guard on the attempt to look them up
582 return lookupValue(getOrEstablishDatabase(UserHandle.USER_OWNER), TABLE_GLOBAL,
583 sGlobalCache, request);
584 }
Christopher Tate06efb532012-08-24 15:29:27 -0700585
Christopher Tate61695ff2012-10-05 12:05:13 -0700586 // Put methods - new value is in the args bundle under the key named by
587 // the Settings.NameValueTable.VALUE static.
588 final String newValue = (args == null)
589 ? null : args.getString(Settings.NameValueTable.VALUE);
Christopher Tate06efb532012-08-24 15:29:27 -0700590
Christopher Tate61695ff2012-10-05 12:05:13 -0700591 final ContentValues values = new ContentValues();
592 values.put(Settings.NameValueTable.NAME, request);
593 values.put(Settings.NameValueTable.VALUE, newValue);
594 if (Settings.CALL_METHOD_PUT_SYSTEM.equals(method)) {
595 if (LOCAL_LOGV) Slog.v(TAG, "call_put(system:" + request + "=" + newValue + ") for " + callingUser);
596 insertForUser(Settings.System.CONTENT_URI, values, callingUser);
597 } else if (Settings.CALL_METHOD_PUT_SECURE.equals(method)) {
598 if (LOCAL_LOGV) Slog.v(TAG, "call_put(secure:" + request + "=" + newValue + ") for " + callingUser);
599 insertForUser(Settings.Secure.CONTENT_URI, values, callingUser);
600 } else if (Settings.CALL_METHOD_PUT_GLOBAL.equals(method)) {
601 if (LOCAL_LOGV) Slog.v(TAG, "call_put(global:" + request + "=" + newValue + ") for " + callingUser);
602 insertForUser(Settings.Global.CONTENT_URI, values, callingUser);
603 } else {
604 Slog.w(TAG, "call() with invalid method: " + method);
Christopher Tate06efb532012-08-24 15:29:27 -0700605 }
606
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800607 return null;
608 }
609
610 // Looks up value 'key' in 'table' and returns either a single-pair Bundle,
611 // possibly with a null value, or null on failure.
Christopher Tate06efb532012-08-24 15:29:27 -0700612 private Bundle lookupValue(DatabaseHelper dbHelper, String table,
613 final SettingsCache cache, String key) {
614 if (cache == null) {
615 Slog.e(TAG, "cache is null for user " + UserHandle.getCallingUserId() + " : key=" + key);
616 return null;
617 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800618 synchronized (cache) {
Jesse Wilson0c7faee2011-02-10 11:33:19 -0800619 Bundle value = cache.get(key);
620 if (value != null) {
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700621 if (value != TOO_LARGE_TO_CACHE_MARKER) {
622 return value;
623 }
624 // else we fall through and read the value from disk
625 } else if (cache.fullyMatchesDisk()) {
626 // Fast path (very common). Don't even try touch disk
627 // if we know we've slurped it all in. Trying to
628 // touch the disk would mean waiting for yaffs2 to
629 // give us access, which could takes hundreds of
630 // milliseconds. And we're very likely being called
631 // from somebody's UI thread...
632 return NULL_SETTING;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800633 }
634 }
635
Christopher Tate06efb532012-08-24 15:29:27 -0700636 SQLiteDatabase db = dbHelper.getReadableDatabase();
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800637 Cursor cursor = null;
638 try {
639 cursor = db.query(table, COLUMN_VALUE, "name=?", new String[]{key},
640 null, null, null, null);
641 if (cursor != null && cursor.getCount() == 1) {
642 cursor.moveToFirst();
Brad Fitzpatrick342984a2010-03-09 16:59:30 -0800643 return cache.putIfAbsent(key, cursor.getString(0));
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800644 }
645 } catch (SQLiteException e) {
646 Log.w(TAG, "settings lookup error", e);
647 return null;
648 } finally {
649 if (cursor != null) cursor.close();
650 }
Brad Fitzpatrick342984a2010-03-09 16:59:30 -0800651 cache.putIfAbsent(key, null);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800652 return NULL_SETTING;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800653 }
654
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700655 @Override
656 public Cursor query(Uri url, String[] select, String where, String[] whereArgs, String sort) {
Christopher Tate4dc7a682012-09-11 12:15:49 -0700657 return queryForUser(url, select, where, whereArgs, sort, UserHandle.getCallingUserId());
658 }
659
Christopher Tateb7564452012-09-19 16:21:18 -0700660 private Cursor queryForUser(Uri url, String[] select, String where, String[] whereArgs,
Christopher Tate4dc7a682012-09-11 12:15:49 -0700661 String sort, int forUser) {
662 if (LOCAL_LOGV) Slog.v(TAG, "query(" + url + ") for user " + forUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700663 SqlArguments args = new SqlArguments(url, where, whereArgs);
Christopher Tate06efb532012-08-24 15:29:27 -0700664 DatabaseHelper dbH;
Christopher Tate78d2a662012-09-13 16:19:44 -0700665 dbH = getOrEstablishDatabase(
666 TABLE_GLOBAL.equals(args.table) ? UserHandle.USER_OWNER : forUser);
Christopher Tate06efb532012-08-24 15:29:27 -0700667 SQLiteDatabase db = dbH.getReadableDatabase();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800669 // The favorites table was moved from this provider to a provider inside Home
670 // Home still need to query this table to upgrade from pre-cupcake builds
671 // However, a cupcake+ build with no data does not contain this table which will
672 // cause an exception in the SQL stack. The following line is a special case to
673 // 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 -0800674 if (TABLE_FAVORITES.equals(args.table)) {
675 return null;
676 } else if (TABLE_OLD_FAVORITES.equals(args.table)) {
677 args.table = TABLE_FAVORITES;
678 Cursor cursor = db.rawQuery("PRAGMA table_info(favorites);", null);
679 if (cursor != null) {
680 boolean exists = cursor.getCount() > 0;
681 cursor.close();
682 if (!exists) return null;
683 } else {
684 return null;
685 }
686 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800687
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700688 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
689 qb.setTables(args.table);
690
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700691 Cursor ret = qb.query(db, select, args.where, args.args, null, null, sort);
Christopher Tateafccaa82012-10-03 17:41:51 -0700692 // the default Cursor interface does not support per-user observation
693 try {
694 AbstractCursor c = (AbstractCursor) ret;
695 c.setNotificationUri(getContext().getContentResolver(), url, forUser);
696 } catch (ClassCastException e) {
697 // details of the concrete Cursor implementation have changed and this code has
698 // not been updated to match -- complain and fail hard.
699 Log.wtf(TAG, "Incompatible cursor derivation!");
700 throw e;
701 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700702 return ret;
703 }
704
705 @Override
706 public String getType(Uri url) {
707 // If SqlArguments supplies a where clause, then it must be an item
708 // (because we aren't supplying our own where clause).
709 SqlArguments args = new SqlArguments(url, null, null);
710 if (TextUtils.isEmpty(args.where)) {
711 return "vnd.android.cursor.dir/" + args.table;
712 } else {
713 return "vnd.android.cursor.item/" + args.table;
714 }
715 }
716
717 @Override
718 public int bulkInsert(Uri uri, ContentValues[] values) {
Christopher Tate06efb532012-08-24 15:29:27 -0700719 final int callingUser = UserHandle.getCallingUserId();
720 if (LOCAL_LOGV) Slog.v(TAG, "bulkInsert() for user " + callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700721 SqlArguments args = new SqlArguments(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 if (TABLE_FAVORITES.equals(args.table)) {
723 return 0;
724 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700725 checkWritePermissions(args);
Christopher Tate06efb532012-08-24 15:29:27 -0700726 SettingsCache cache = cacheForTable(callingUser, args.table);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700727
Christopher Tate06efb532012-08-24 15:29:27 -0700728 final AtomicInteger mutationCount = sKnownMutationsInFlight.get(callingUser);
729 mutationCount.incrementAndGet();
Christopher Tate78d2a662012-09-13 16:19:44 -0700730 DatabaseHelper dbH = getOrEstablishDatabase(
731 TABLE_GLOBAL.equals(args.table) ? UserHandle.USER_OWNER : callingUser);
Christopher Tate06efb532012-08-24 15:29:27 -0700732 SQLiteDatabase db = dbH.getWritableDatabase();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700733 db.beginTransaction();
734 try {
735 int numValues = values.length;
736 for (int i = 0; i < numValues; i++) {
737 if (db.insert(args.table, null, values[i]) < 0) return 0;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800738 SettingsCache.populate(cache, values[i]);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700739 if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + values[i]);
740 }
741 db.setTransactionSuccessful();
742 } finally {
743 db.endTransaction();
Christopher Tate06efb532012-08-24 15:29:27 -0700744 mutationCount.decrementAndGet();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700745 }
746
Christopher Tate06efb532012-08-24 15:29:27 -0700747 sendNotify(uri, callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700748 return values.length;
749 }
750
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700751 /*
752 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
753 * This setting contains a list of the currently enabled location providers.
754 * But helper functions in android.providers.Settings can enable or disable
755 * a single provider by using a "+" or "-" prefix before the provider name.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800756 *
757 * @returns whether the database needs to be updated or not, also modifying
758 * 'initialValues' if needed.
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700759 */
760 private boolean parseProviderList(Uri url, ContentValues initialValues) {
761 String value = initialValues.getAsString(Settings.Secure.VALUE);
762 String newProviders = null;
763 if (value != null && value.length() > 1) {
764 char prefix = value.charAt(0);
765 if (prefix == '+' || prefix == '-') {
766 // skip prefix
767 value = value.substring(1);
768
769 // read list of enabled providers into "providers"
770 String providers = "";
771 String[] columns = {Settings.Secure.VALUE};
772 String where = Settings.Secure.NAME + "=\'" + Settings.Secure.LOCATION_PROVIDERS_ALLOWED + "\'";
773 Cursor cursor = query(url, columns, where, null, null);
774 if (cursor != null && cursor.getCount() == 1) {
775 try {
776 cursor.moveToFirst();
777 providers = cursor.getString(0);
778 } finally {
779 cursor.close();
780 }
781 }
782
783 int index = providers.indexOf(value);
784 int end = index + value.length();
785 // check for commas to avoid matching on partial string
786 if (index > 0 && providers.charAt(index - 1) != ',') index = -1;
787 if (end < providers.length() && providers.charAt(end) != ',') index = -1;
788
789 if (prefix == '+' && index < 0) {
790 // append the provider to the list if not present
791 if (providers.length() == 0) {
792 newProviders = value;
793 } else {
794 newProviders = providers + ',' + value;
795 }
796 } else if (prefix == '-' && index >= 0) {
797 // remove the provider from the list if present
Mike Lockwoodbdc7f892010-04-21 18:24:57 -0400798 // remove leading or trailing comma
799 if (index > 0) {
800 index--;
801 } else if (end < providers.length()) {
802 end++;
803 }
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700804
805 newProviders = providers.substring(0, index);
806 if (end < providers.length()) {
807 newProviders += providers.substring(end);
808 }
809 } else {
810 // nothing changed, so no need to update the database
811 return false;
812 }
813
814 if (newProviders != null) {
815 initialValues.put(Settings.Secure.VALUE, newProviders);
816 }
817 }
818 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800819
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700820 return true;
821 }
822
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700823 @Override
824 public Uri insert(Uri url, ContentValues initialValues) {
Christopher Tate06efb532012-08-24 15:29:27 -0700825 return insertForUser(url, initialValues, UserHandle.getCallingUserId());
826 }
827
828 // Settings.put*ForUser() always winds up here, so this is where we apply
829 // policy around permission to write settings for other users.
830 private Uri insertForUser(Uri url, ContentValues initialValues, int desiredUserHandle) {
831 final int callingUser = UserHandle.getCallingUserId();
832 if (callingUser != desiredUserHandle) {
Christopher Tate4dc7a682012-09-11 12:15:49 -0700833 getContext().enforceCallingOrSelfPermission(
Christopher Tate06efb532012-08-24 15:29:27 -0700834 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
835 "Not permitted to access settings for other users");
836 }
837
838 if (LOCAL_LOGV) Slog.v(TAG, "insert(" + url + ") for user " + desiredUserHandle
839 + " by " + callingUser);
840
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700841 SqlArguments args = new SqlArguments(url);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 if (TABLE_FAVORITES.equals(args.table)) {
843 return null;
844 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700845
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700846 // Special case LOCATION_PROVIDERS_ALLOWED.
847 // Support enabling/disabling a single provider (using "+" or "-" prefix)
848 String name = initialValues.getAsString(Settings.Secure.NAME);
849 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
850 if (!parseProviderList(url, initialValues)) return null;
851 }
852
Christopher Tatec221d2b2012-10-03 18:33:52 -0700853 // If this is an insert() of a key that has been migrated to the global store,
854 // redirect the operation to that store
855 if (name != null) {
856 if (sSecureGlobalKeys.contains(name) || sSystemGlobalKeys.contains(name)) {
857 if (!TABLE_GLOBAL.equals(args.table)) {
858 if (LOCAL_LOGV) Slog.i(TAG, "Rewrite of insert() of now-global key " + name);
859 }
860 args.table = TABLE_GLOBAL; // next condition will rewrite the user handle
861 }
862 }
863
Christopher Tate34637e52012-10-04 15:00:00 -0700864 // Check write permissions only after determining which table the insert will touch
865 checkWritePermissions(args);
866
Christopher Tate06efb532012-08-24 15:29:27 -0700867 // The global table is stored under the owner, always
868 if (TABLE_GLOBAL.equals(args.table)) {
869 desiredUserHandle = UserHandle.USER_OWNER;
870 }
871
872 SettingsCache cache = cacheForTable(desiredUserHandle, args.table);
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -0800873 String value = initialValues.getAsString(Settings.NameValueTable.VALUE);
874 if (SettingsCache.isRedundantSetValue(cache, name, value)) {
875 return Uri.withAppendedPath(url, name);
876 }
877
Christopher Tate06efb532012-08-24 15:29:27 -0700878 final AtomicInteger mutationCount = sKnownMutationsInFlight.get(desiredUserHandle);
879 mutationCount.incrementAndGet();
Christopher Tate78d2a662012-09-13 16:19:44 -0700880 DatabaseHelper dbH = getOrEstablishDatabase(desiredUserHandle);
Christopher Tate06efb532012-08-24 15:29:27 -0700881 SQLiteDatabase db = dbH.getWritableDatabase();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700882 final long rowId = db.insert(args.table, null, initialValues);
Christopher Tate06efb532012-08-24 15:29:27 -0700883 mutationCount.decrementAndGet();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700884 if (rowId <= 0) return null;
885
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800886 SettingsCache.populate(cache, initialValues); // before we notify
887
Christopher Tate78d2a662012-09-13 16:19:44 -0700888 if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + initialValues
889 + " for user " + desiredUserHandle);
Christopher Tate06efb532012-08-24 15:29:27 -0700890 // Note that we use the original url here, not the potentially-rewritten table name
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700891 url = getUriFor(url, initialValues, rowId);
Christopher Tate06efb532012-08-24 15:29:27 -0700892 sendNotify(url, desiredUserHandle);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700893 return url;
894 }
895
896 @Override
897 public int delete(Uri url, String where, String[] whereArgs) {
Christopher Tate4dc7a682012-09-11 12:15:49 -0700898 int callingUser = UserHandle.getCallingUserId();
Christopher Tate06efb532012-08-24 15:29:27 -0700899 if (LOCAL_LOGV) Slog.v(TAG, "delete() for user " + callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700900 SqlArguments args = new SqlArguments(url, where, whereArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 if (TABLE_FAVORITES.equals(args.table)) {
902 return 0;
903 } else if (TABLE_OLD_FAVORITES.equals(args.table)) {
904 args.table = TABLE_FAVORITES;
Christopher Tate4dc7a682012-09-11 12:15:49 -0700905 } else if (TABLE_GLOBAL.equals(args.table)) {
906 callingUser = UserHandle.USER_OWNER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700908 checkWritePermissions(args);
909
Christopher Tate06efb532012-08-24 15:29:27 -0700910 final AtomicInteger mutationCount = sKnownMutationsInFlight.get(callingUser);
911 mutationCount.incrementAndGet();
Christopher Tate78d2a662012-09-13 16:19:44 -0700912 DatabaseHelper dbH = getOrEstablishDatabase(callingUser);
Christopher Tate06efb532012-08-24 15:29:27 -0700913 SQLiteDatabase db = dbH.getWritableDatabase();
914 int count = db.delete(args.table, args.where, args.args);
915 mutationCount.decrementAndGet();
916 if (count > 0) {
917 invalidateCache(callingUser, args.table); // before we notify
918 sendNotify(url, callingUser);
919 }
920 startAsyncCachePopulation(callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700921 if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) deleted");
922 return count;
923 }
924
925 @Override
926 public int update(Uri url, ContentValues initialValues, String where, String[] whereArgs) {
Christopher Tate06efb532012-08-24 15:29:27 -0700927 // NOTE: update() is never called by the front-end Settings API, and updates that
928 // wind up affecting rows in Secure that are globally shared will not have the
929 // intended effect (the update will be invisible to the rest of the system).
930 // This should have no practical effect, since writes to the Secure db can only
931 // be done by system code, and that code should be using the correct API up front.
Christopher Tate4dc7a682012-09-11 12:15:49 -0700932 int callingUser = UserHandle.getCallingUserId();
Christopher Tate06efb532012-08-24 15:29:27 -0700933 if (LOCAL_LOGV) Slog.v(TAG, "update() for user " + callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700934 SqlArguments args = new SqlArguments(url, where, whereArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 if (TABLE_FAVORITES.equals(args.table)) {
936 return 0;
Christopher Tate4dc7a682012-09-11 12:15:49 -0700937 } else if (TABLE_GLOBAL.equals(args.table)) {
938 callingUser = UserHandle.USER_OWNER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700940 checkWritePermissions(args);
941
Christopher Tate06efb532012-08-24 15:29:27 -0700942 final AtomicInteger mutationCount = sKnownMutationsInFlight.get(callingUser);
943 mutationCount.incrementAndGet();
Christopher Tate78d2a662012-09-13 16:19:44 -0700944 DatabaseHelper dbH = getOrEstablishDatabase(callingUser);
Christopher Tate06efb532012-08-24 15:29:27 -0700945 SQLiteDatabase db = dbH.getWritableDatabase();
946 int count = db.update(args.table, initialValues, args.where, args.args);
947 mutationCount.decrementAndGet();
948 if (count > 0) {
949 invalidateCache(callingUser, args.table); // before we notify
950 sendNotify(url, callingUser);
951 }
952 startAsyncCachePopulation(callingUser);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700953 if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) <- " + initialValues);
954 return count;
955 }
956
957 @Override
958 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
959
960 /*
961 * When a client attempts to openFile the default ringtone or
962 * notification setting Uri, we will proxy the call to the current
963 * default ringtone's Uri (if it is in the DRM or media provider).
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700964 */
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700965 int ringtoneType = RingtoneManager.getDefaultType(uri);
966 // Above call returns -1 if the Uri doesn't match a default type
967 if (ringtoneType != -1) {
968 Context context = getContext();
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700969
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700970 // Get the current value for the default sound
971 Uri soundUri = RingtoneManager.getActualDefaultRingtoneUri(context, ringtoneType);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700972
Marco Nelissen69f593c2009-07-28 09:55:04 -0700973 if (soundUri != null) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700974 // Only proxy the openFile call to drm or media providers
975 String authority = soundUri.getAuthority();
976 boolean isDrmAuthority = authority.equals(DrmStore.AUTHORITY);
977 if (isDrmAuthority || authority.equals(MediaStore.AUTHORITY)) {
978
979 if (isDrmAuthority) {
980 try {
981 // Check DRM access permission here, since once we
982 // do the below call the DRM will be checking our
983 // permission, not our caller's permission
984 DrmStore.enforceAccessDrmPermission(context);
985 } catch (SecurityException e) {
986 throw new FileNotFoundException(e.getMessage());
987 }
988 }
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -0700989
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700990 return context.getContentResolver().openFileDescriptor(soundUri, mode);
991 }
992 }
993 }
994
995 return super.openFile(uri, mode);
996 }
Marco Nelissen69f593c2009-07-28 09:55:04 -0700997
998 @Override
999 public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
1000
1001 /*
1002 * When a client attempts to openFile the default ringtone or
1003 * notification setting Uri, we will proxy the call to the current
1004 * default ringtone's Uri (if it is in the DRM or media provider).
1005 */
1006 int ringtoneType = RingtoneManager.getDefaultType(uri);
1007 // Above call returns -1 if the Uri doesn't match a default type
1008 if (ringtoneType != -1) {
1009 Context context = getContext();
1010
1011 // Get the current value for the default sound
1012 Uri soundUri = RingtoneManager.getActualDefaultRingtoneUri(context, ringtoneType);
1013
1014 if (soundUri != null) {
1015 // Only proxy the openFile call to drm or media providers
1016 String authority = soundUri.getAuthority();
1017 boolean isDrmAuthority = authority.equals(DrmStore.AUTHORITY);
1018 if (isDrmAuthority || authority.equals(MediaStore.AUTHORITY)) {
1019
1020 if (isDrmAuthority) {
1021 try {
1022 // Check DRM access permission here, since once we
1023 // do the below call the DRM will be checking our
1024 // permission, not our caller's permission
1025 DrmStore.enforceAccessDrmPermission(context);
1026 } catch (SecurityException e) {
1027 throw new FileNotFoundException(e.getMessage());
1028 }
1029 }
1030
1031 ParcelFileDescriptor pfd = null;
1032 try {
1033 pfd = context.getContentResolver().openFileDescriptor(soundUri, mode);
1034 return new AssetFileDescriptor(pfd, 0, -1);
1035 } catch (FileNotFoundException ex) {
1036 // fall through and open the fallback ringtone below
1037 }
1038 }
1039
1040 try {
1041 return super.openAssetFile(soundUri, mode);
1042 } catch (FileNotFoundException ex) {
1043 // Since a non-null Uri was specified, but couldn't be opened,
1044 // fall back to the built-in ringtone.
1045 return context.getResources().openRawResourceFd(
1046 com.android.internal.R.raw.fallbackring);
1047 }
1048 }
1049 // no need to fall through and have openFile() try again, since we
1050 // already know that will fail.
1051 throw new FileNotFoundException(); // or return null ?
1052 }
1053
1054 // Note that this will end up calling openFile() above.
1055 return super.openAssetFile(uri, mode);
1056 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001057
1058 /**
1059 * In-memory LRU Cache of system and secure settings, along with
1060 * associated helper functions to keep cache coherent with the
1061 * database.
1062 */
Jesse Wilson0c7faee2011-02-10 11:33:19 -08001063 private static final class SettingsCache extends LruCache<String, Bundle> {
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001064
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001065 private final String mCacheName;
1066 private boolean mCacheFullyMatchesDisk = false; // has the whole database slurped.
1067
1068 public SettingsCache(String name) {
Jesse Wilson0c7faee2011-02-10 11:33:19 -08001069 super(MAX_CACHE_ENTRIES);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001070 mCacheName = name;
1071 }
1072
1073 /**
1074 * Is the whole database table slurped into this cache?
1075 */
1076 public boolean fullyMatchesDisk() {
1077 synchronized (this) {
1078 return mCacheFullyMatchesDisk;
1079 }
1080 }
1081
1082 public void setFullyMatchesDisk(boolean value) {
1083 synchronized (this) {
1084 mCacheFullyMatchesDisk = value;
1085 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001086 }
1087
1088 @Override
Jesse Wilson32c80a22011-02-25 17:28:41 -08001089 protected void entryRemoved(boolean evicted, String key, Bundle oldValue, Bundle newValue) {
1090 if (evicted) {
1091 mCacheFullyMatchesDisk = false;
1092 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001093 }
1094
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001095 /**
1096 * Atomic cache population, conditional on size of value and if
1097 * we lost a race.
1098 *
1099 * @returns a Bundle to send back to the client from call(), even
1100 * if we lost the race.
1101 */
1102 public Bundle putIfAbsent(String key, String value) {
1103 Bundle bundle = (value == null) ? NULL_SETTING : Bundle.forPair("value", value);
1104 if (value == null || value.length() <= MAX_CACHE_ENTRY_SIZE) {
1105 synchronized (this) {
Jesse Wilson0c7faee2011-02-10 11:33:19 -08001106 if (get(key) == null) {
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001107 put(key, bundle);
1108 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001109 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001110 }
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001111 return bundle;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001112 }
1113
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001114 /**
1115 * Populates a key in a given (possibly-null) cache.
1116 */
1117 public static void populate(SettingsCache cache, ContentValues contentValues) {
1118 if (cache == null) {
1119 return;
1120 }
1121 String name = contentValues.getAsString(Settings.NameValueTable.NAME);
1122 if (name == null) {
1123 Log.w(TAG, "null name populating settings cache.");
1124 return;
1125 }
1126 String value = contentValues.getAsString(Settings.NameValueTable.VALUE);
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001127 cache.populate(name, value);
1128 }
1129
1130 public void populate(String name, String value) {
1131 synchronized (this) {
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001132 if (value == null || value.length() <= MAX_CACHE_ENTRY_SIZE) {
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001133 put(name, Bundle.forPair(Settings.NameValueTable.VALUE, value));
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001134 } else {
Brad Fitzpatrickf366a9b2010-08-24 16:14:07 -07001135 put(name, TOO_LARGE_TO_CACHE_MARKER);
Brad Fitzpatrick342984a2010-03-09 16:59:30 -08001136 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001137 }
1138 }
1139
1140 /**
Brad Fitzpatrick547a96b2010-03-09 17:58:53 -08001141 * For suppressing duplicate/redundant settings inserts early,
1142 * checking our cache first (but without faulting it in),
1143 * before going to sqlite with the mutation.
1144 */
1145 public static boolean isRedundantSetValue(SettingsCache cache, String name, String value) {
1146 if (cache == null) return false;
1147 synchronized (cache) {
1148 Bundle bundle = cache.get(name);
1149 if (bundle == null) return false;
1150 String oldValue = bundle.getPairValue();
1151 if (oldValue == null && value == null) return true;
1152 if ((oldValue == null) != (value == null)) return false;
1153 return oldValue.equals(value);
1154 }
1155 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -08001156 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001157}