blob: 83937fa4f7c2f26a69a92374a6209bb9454f3d70 [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.io.UnsupportedEncodingException;
Fred Quintanac70239e2009-12-17 10:28:33 -080021import java.security.NoSuchAlgorithmException;
Doug Zongker4f8ff392010-02-03 10:36:40 -080022import java.security.SecureRandom;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -080023import java.util.LinkedHashMap;
24import java.util.Map;
-b master501eec92009-07-06 13:53:11 -070025
Christopher Tate45281862010-03-05 15:46:30 -080026import android.app.backup.BackupManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070027import android.content.ContentProvider;
28import android.content.ContentUris;
29import android.content.ContentValues;
30import android.content.Context;
31import android.content.pm.PackageManager;
Marco Nelissen69f593c2009-07-28 09:55:04 -070032import android.content.res.AssetFileDescriptor;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070033import android.database.Cursor;
34import android.database.sqlite.SQLiteDatabase;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080035import android.database.sqlite.SQLiteException;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070036import android.database.sqlite.SQLiteQueryBuilder;
37import android.media.RingtoneManager;
38import android.net.Uri;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080039import android.os.Bundle;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070040import android.os.ParcelFileDescriptor;
41import android.os.SystemProperties;
42import android.provider.DrmStore;
43import android.provider.MediaStore;
44import android.provider.Settings;
45import android.text.TextUtils;
46import android.util.Log;
47
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070048public class SettingsProvider extends ContentProvider {
49 private static final String TAG = "SettingsProvider";
50 private static final boolean LOCAL_LOGV = false;
51
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 private static final String TABLE_FAVORITES = "favorites";
53 private static final String TABLE_OLD_FAVORITES = "old_favorites";
54
Brad Fitzpatrick1877d012010-03-04 17:48:13 -080055 private static final String[] COLUMN_VALUE = new String[] { "value" };
56
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -080057 // Cache for settings, access-ordered for acting as LRU.
58 // Guarded by themselves.
59 private static final int MAX_CACHE_ENTRIES = 50;
60 private static final SettingsCache sSystemCache = new SettingsCache();
61 private static final SettingsCache sSecureCache = new SettingsCache();
62
Brad Fitzpatrick342984a2010-03-09 16:59:30 -080063 // Over this size we don't reject loading or saving settings but
64 // we do consider them broken/malicious and don't keep them in
65 // memory at least:
66 private static final int MAX_CACHE_ENTRY_SIZE = 500;
67
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -080068 private static final Bundle NULL_SETTING = Bundle.forPair("value", null);
69
James Wylder074da8f2009-02-25 08:38:31 -060070 protected DatabaseHelper mOpenHelper;
Amith Yamasani8823c0a82009-07-07 14:30:17 -070071 private BackupManager mBackupManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070072
73 /**
74 * Decode a content URL into the table, projection, and arguments
75 * used to access the corresponding database rows.
76 */
77 private static class SqlArguments {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 public String table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070079 public final String where;
80 public final String[] args;
81
82 /** Operate on existing rows. */
83 SqlArguments(Uri url, String where, String[] args) {
84 if (url.getPathSegments().size() == 1) {
85 this.table = url.getPathSegments().get(0);
86 this.where = where;
87 this.args = args;
88 } else if (url.getPathSegments().size() != 2) {
89 throw new IllegalArgumentException("Invalid URI: " + url);
90 } else if (!TextUtils.isEmpty(where)) {
91 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
92 } else {
93 this.table = url.getPathSegments().get(0);
Doug Zongkeraed8f8e2010-01-07 18:07:50 -080094 if ("system".equals(this.table) || "secure".equals(this.table)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070095 this.where = Settings.NameValueTable.NAME + "=?";
96 this.args = new String[] { url.getPathSegments().get(1) };
97 } else {
98 this.where = "_id=" + ContentUris.parseId(url);
99 this.args = null;
100 }
101 }
102 }
103
104 /** Insert new rows (no where clause allowed). */
105 SqlArguments(Uri url) {
106 if (url.getPathSegments().size() == 1) {
107 this.table = url.getPathSegments().get(0);
108 this.where = null;
109 this.args = null;
110 } else {
111 throw new IllegalArgumentException("Invalid URI: " + url);
112 }
113 }
114 }
115
116 /**
117 * Get the content URI of a row added to a table.
118 * @param tableUri of the entire table
119 * @param values found in the row
120 * @param rowId of the row
121 * @return the content URI for this particular row
122 */
123 private Uri getUriFor(Uri tableUri, ContentValues values, long rowId) {
124 if (tableUri.getPathSegments().size() != 1) {
125 throw new IllegalArgumentException("Invalid URI: " + tableUri);
126 }
127 String table = tableUri.getPathSegments().get(0);
Doug Zongkeraed8f8e2010-01-07 18:07:50 -0800128 if ("system".equals(table) || "secure".equals(table)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700129 String name = values.getAsString(Settings.NameValueTable.NAME);
130 return Uri.withAppendedPath(tableUri, name);
131 } else {
132 return ContentUris.withAppendedId(tableUri, rowId);
133 }
134 }
135
136 /**
137 * Send a notification when a particular content URI changes.
138 * Modify the system property used to communicate the version of
139 * this table, for tables which have such a property. (The Settings
140 * contract class uses these to provide client-side caches.)
141 * @param uri to send notifications for
142 */
143 private void sendNotify(Uri uri) {
144 // Update the system property *first*, so if someone is listening for
145 // a notification and then using the contract class to get their data,
146 // the system property will be updated and they'll get the new data.
147
Amith Yamasanid1582142009-07-08 20:04:55 -0700148 boolean backedUpDataChanged = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700149 String property = null, table = uri.getPathSegments().get(0);
150 if (table.equals("system")) {
151 property = Settings.System.SYS_PROP_SETTING_VERSION;
Amith Yamasanid1582142009-07-08 20:04:55 -0700152 backedUpDataChanged = true;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800153 } else if (table.equals("secure")) {
154 property = Settings.Secure.SYS_PROP_SETTING_VERSION;
Amith Yamasanid1582142009-07-08 20:04:55 -0700155 backedUpDataChanged = true;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700156 }
157
158 if (property != null) {
159 long version = SystemProperties.getLong(property, 0) + 1;
160 if (LOCAL_LOGV) Log.v(TAG, "property: " + property + "=" + version);
161 SystemProperties.set(property, Long.toString(version));
162 }
163
-b master501eec92009-07-06 13:53:11 -0700164 // Inform the backup manager about a data change
Amith Yamasanid1582142009-07-08 20:04:55 -0700165 if (backedUpDataChanged) {
166 mBackupManager.dataChanged();
167 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700168 // Now send the notification through the content framework.
169
170 String notify = uri.getQueryParameter("notify");
171 if (notify == null || "true".equals(notify)) {
172 getContext().getContentResolver().notifyChange(uri, null);
173 if (LOCAL_LOGV) Log.v(TAG, "notifying: " + uri);
174 } else {
175 if (LOCAL_LOGV) Log.v(TAG, "notification suppressed: " + uri);
176 }
177 }
178
179 /**
180 * Make sure the caller has permission to write this data.
181 * @param args supplied by the caller
182 * @throws SecurityException if the caller is forbidden to write.
183 */
184 private void checkWritePermissions(SqlArguments args) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800185 if ("secure".equals(args.table) &&
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800186 getContext().checkCallingOrSelfPermission(
Doug Zongkeraed8f8e2010-01-07 18:07:50 -0800187 android.Manifest.permission.WRITE_SECURE_SETTINGS) !=
188 PackageManager.PERMISSION_GRANTED) {
Brett Chabot16dd82c2009-06-18 17:00:48 -0700189 throw new SecurityException(
Doug Zongkeraed8f8e2010-01-07 18:07:50 -0800190 String.format("Permission denial: writing to secure settings requires %1$s",
191 android.Manifest.permission.WRITE_SECURE_SETTINGS));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700192 }
193 }
194
195 @Override
196 public boolean onCreate() {
197 mOpenHelper = new DatabaseHelper(getContext());
Amith Yamasani8823c0a82009-07-07 14:30:17 -0700198 mBackupManager = new BackupManager(getContext());
Fred Quintanac70239e2009-12-17 10:28:33 -0800199
200 if (!ensureAndroidIdIsSet()) {
201 return false;
202 }
203
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700204 return true;
205 }
206
Fred Quintanac70239e2009-12-17 10:28:33 -0800207 private boolean ensureAndroidIdIsSet() {
208 final Cursor c = query(Settings.Secure.CONTENT_URI,
209 new String[] { Settings.NameValueTable.VALUE },
210 Settings.NameValueTable.NAME + "=?",
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800211 new String[] { Settings.Secure.ANDROID_ID }, null);
Fred Quintanac70239e2009-12-17 10:28:33 -0800212 try {
213 final String value = c.moveToNext() ? c.getString(0) : null;
214 if (value == null) {
215 final SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
Doug Zongker4f8ff392010-02-03 10:36:40 -0800216 String serial = SystemProperties.get("ro.serialno");
217 if (serial != null) {
218 try {
219 random.setSeed(serial.getBytes("UTF-8"));
220 } catch (UnsupportedEncodingException ignore) {
221 // stick with default seed
222 }
223 }
Fred Quintanac70239e2009-12-17 10:28:33 -0800224 final String newAndroidIdValue = Long.toHexString(random.nextLong());
225 Log.d(TAG, "Generated and saved new ANDROID_ID");
226 final ContentValues values = new ContentValues();
227 values.put(Settings.NameValueTable.NAME, Settings.Secure.ANDROID_ID);
228 values.put(Settings.NameValueTable.VALUE, newAndroidIdValue);
229 final Uri uri = insert(Settings.Secure.CONTENT_URI, values);
230 if (uri == null) {
231 return false;
232 }
233 }
234 return true;
235 } catch (NoSuchAlgorithmException e) {
236 return false;
237 } finally {
238 c.close();
239 }
240 }
241
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800242 /**
243 * Fast path that avoids the use of chatty remoted Cursors.
244 */
245 @Override
246 public Bundle call(String method, String request, Bundle args) {
247 if (Settings.CALL_METHOD_GET_SYSTEM.equals(method)) {
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800248 return lookupValue("system", sSystemCache, request);
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800249 }
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800250 if (Settings.CALL_METHOD_GET_SECURE.equals(method)) {
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800251 return lookupValue("secure", sSecureCache, request);
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800252 }
253 return null;
254 }
255
256 // Looks up value 'key' in 'table' and returns either a single-pair Bundle,
257 // possibly with a null value, or null on failure.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800258 private Bundle lookupValue(String table, SettingsCache cache, String key) {
259 synchronized (cache) {
260 if (cache.containsKey(key)) {
261 return cache.get(key);
262 }
263 }
264
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800265 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
266 Cursor cursor = null;
267 try {
268 cursor = db.query(table, COLUMN_VALUE, "name=?", new String[]{key},
269 null, null, null, null);
270 if (cursor != null && cursor.getCount() == 1) {
271 cursor.moveToFirst();
Brad Fitzpatrick342984a2010-03-09 16:59:30 -0800272 return cache.putIfAbsent(key, cursor.getString(0));
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800273 }
274 } catch (SQLiteException e) {
275 Log.w(TAG, "settings lookup error", e);
276 return null;
277 } finally {
278 if (cursor != null) cursor.close();
279 }
Brad Fitzpatrick342984a2010-03-09 16:59:30 -0800280 cache.putIfAbsent(key, null);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800281 return NULL_SETTING;
Brad Fitzpatrick1877d012010-03-04 17:48:13 -0800282 }
283
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700284 @Override
285 public Cursor query(Uri url, String[] select, String where, String[] whereArgs, String sort) {
286 SqlArguments args = new SqlArguments(url, where, whereArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
288
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800289 // The favorites table was moved from this provider to a provider inside Home
290 // Home still need to query this table to upgrade from pre-cupcake builds
291 // However, a cupcake+ build with no data does not contain this table which will
292 // cause an exception in the SQL stack. The following line is a special case to
293 // 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 -0800294 if (TABLE_FAVORITES.equals(args.table)) {
295 return null;
296 } else if (TABLE_OLD_FAVORITES.equals(args.table)) {
297 args.table = TABLE_FAVORITES;
298 Cursor cursor = db.rawQuery("PRAGMA table_info(favorites);", null);
299 if (cursor != null) {
300 boolean exists = cursor.getCount() > 0;
301 cursor.close();
302 if (!exists) return null;
303 } else {
304 return null;
305 }
306 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800307
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700308 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
309 qb.setTables(args.table);
310
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700311 Cursor ret = qb.query(db, select, args.where, args.args, null, null, sort);
312 ret.setNotificationUri(getContext().getContentResolver(), url);
313 return ret;
314 }
315
316 @Override
317 public String getType(Uri url) {
318 // If SqlArguments supplies a where clause, then it must be an item
319 // (because we aren't supplying our own where clause).
320 SqlArguments args = new SqlArguments(url, null, null);
321 if (TextUtils.isEmpty(args.where)) {
322 return "vnd.android.cursor.dir/" + args.table;
323 } else {
324 return "vnd.android.cursor.item/" + args.table;
325 }
326 }
327
328 @Override
329 public int bulkInsert(Uri uri, ContentValues[] values) {
330 SqlArguments args = new SqlArguments(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 if (TABLE_FAVORITES.equals(args.table)) {
332 return 0;
333 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700334 checkWritePermissions(args);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800335 SettingsCache cache = SettingsCache.forTable(args.table);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700336
337 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
338 db.beginTransaction();
339 try {
340 int numValues = values.length;
341 for (int i = 0; i < numValues; i++) {
342 if (db.insert(args.table, null, values[i]) < 0) return 0;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800343 SettingsCache.populate(cache, values[i]);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700344 if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + values[i]);
345 }
346 db.setTransactionSuccessful();
347 } finally {
348 db.endTransaction();
349 }
350
351 sendNotify(uri);
352 return values.length;
353 }
354
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700355 /*
356 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
357 * This setting contains a list of the currently enabled location providers.
358 * But helper functions in android.providers.Settings can enable or disable
359 * a single provider by using a "+" or "-" prefix before the provider name.
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800360 *
361 * @returns whether the database needs to be updated or not, also modifying
362 * 'initialValues' if needed.
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700363 */
364 private boolean parseProviderList(Uri url, ContentValues initialValues) {
365 String value = initialValues.getAsString(Settings.Secure.VALUE);
366 String newProviders = null;
367 if (value != null && value.length() > 1) {
368 char prefix = value.charAt(0);
369 if (prefix == '+' || prefix == '-') {
370 // skip prefix
371 value = value.substring(1);
372
373 // read list of enabled providers into "providers"
374 String providers = "";
375 String[] columns = {Settings.Secure.VALUE};
376 String where = Settings.Secure.NAME + "=\'" + Settings.Secure.LOCATION_PROVIDERS_ALLOWED + "\'";
377 Cursor cursor = query(url, columns, where, null, null);
378 if (cursor != null && cursor.getCount() == 1) {
379 try {
380 cursor.moveToFirst();
381 providers = cursor.getString(0);
382 } finally {
383 cursor.close();
384 }
385 }
386
387 int index = providers.indexOf(value);
388 int end = index + value.length();
389 // check for commas to avoid matching on partial string
390 if (index > 0 && providers.charAt(index - 1) != ',') index = -1;
391 if (end < providers.length() && providers.charAt(end) != ',') index = -1;
392
393 if (prefix == '+' && index < 0) {
394 // append the provider to the list if not present
395 if (providers.length() == 0) {
396 newProviders = value;
397 } else {
398 newProviders = providers + ',' + value;
399 }
400 } else if (prefix == '-' && index >= 0) {
401 // remove the provider from the list if present
402 // remove leading and trailing commas
403 if (index > 0) index--;
404 if (end < providers.length()) end++;
405
406 newProviders = providers.substring(0, index);
407 if (end < providers.length()) {
408 newProviders += providers.substring(end);
409 }
410 } else {
411 // nothing changed, so no need to update the database
412 return false;
413 }
414
415 if (newProviders != null) {
416 initialValues.put(Settings.Secure.VALUE, newProviders);
417 }
418 }
419 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800420
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700421 return true;
422 }
423
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700424 @Override
425 public Uri insert(Uri url, ContentValues initialValues) {
426 SqlArguments args = new SqlArguments(url);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 if (TABLE_FAVORITES.equals(args.table)) {
428 return null;
429 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700430 checkWritePermissions(args);
431
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700432 // Special case LOCATION_PROVIDERS_ALLOWED.
433 // Support enabling/disabling a single provider (using "+" or "-" prefix)
434 String name = initialValues.getAsString(Settings.Secure.NAME);
435 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
436 if (!parseProviderList(url, initialValues)) return null;
437 }
438
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700439 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
440 final long rowId = db.insert(args.table, null, initialValues);
441 if (rowId <= 0) return null;
442
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800443 SettingsCache cache = SettingsCache.forTable(args.table);
444 SettingsCache.populate(cache, initialValues); // before we notify
445
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700446 if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + initialValues);
447 url = getUriFor(url, initialValues, rowId);
448 sendNotify(url);
449 return url;
450 }
451
452 @Override
453 public int delete(Uri url, String where, String[] whereArgs) {
454 SqlArguments args = new SqlArguments(url, where, whereArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 if (TABLE_FAVORITES.equals(args.table)) {
456 return 0;
457 } else if (TABLE_OLD_FAVORITES.equals(args.table)) {
458 args.table = TABLE_FAVORITES;
459 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700460 checkWritePermissions(args);
461
462 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
463 int count = db.delete(args.table, args.where, args.args);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800464 if (count > 0) {
465 SettingsCache.wipe(args.table); // before we notify
466 sendNotify(url);
467 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700468 if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) deleted");
469 return count;
470 }
471
472 @Override
473 public int update(Uri url, ContentValues initialValues, String where, String[] whereArgs) {
474 SqlArguments args = new SqlArguments(url, where, whereArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 if (TABLE_FAVORITES.equals(args.table)) {
476 return 0;
477 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700478 checkWritePermissions(args);
479
480 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
481 int count = db.update(args.table, initialValues, args.where, args.args);
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800482 if (count > 0) {
483 SettingsCache.wipe(args.table); // before we notify
484 sendNotify(url);
485 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700486 if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) <- " + initialValues);
487 return count;
488 }
489
490 @Override
491 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
492
493 /*
494 * When a client attempts to openFile the default ringtone or
495 * notification setting Uri, we will proxy the call to the current
496 * default ringtone's Uri (if it is in the DRM or media provider).
497 */
498 int ringtoneType = RingtoneManager.getDefaultType(uri);
499 // Above call returns -1 if the Uri doesn't match a default type
500 if (ringtoneType != -1) {
501 Context context = getContext();
502
503 // Get the current value for the default sound
504 Uri soundUri = RingtoneManager.getActualDefaultRingtoneUri(context, ringtoneType);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700505
Marco Nelissen69f593c2009-07-28 09:55:04 -0700506 if (soundUri != null) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700507 // Only proxy the openFile call to drm or media providers
508 String authority = soundUri.getAuthority();
509 boolean isDrmAuthority = authority.equals(DrmStore.AUTHORITY);
510 if (isDrmAuthority || authority.equals(MediaStore.AUTHORITY)) {
511
512 if (isDrmAuthority) {
513 try {
514 // Check DRM access permission here, since once we
515 // do the below call the DRM will be checking our
516 // permission, not our caller's permission
517 DrmStore.enforceAccessDrmPermission(context);
518 } catch (SecurityException e) {
519 throw new FileNotFoundException(e.getMessage());
520 }
521 }
522
523 return context.getContentResolver().openFileDescriptor(soundUri, mode);
524 }
525 }
526 }
527
528 return super.openFile(uri, mode);
529 }
Marco Nelissen69f593c2009-07-28 09:55:04 -0700530
531 @Override
532 public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
533
534 /*
535 * When a client attempts to openFile the default ringtone or
536 * notification setting Uri, we will proxy the call to the current
537 * default ringtone's Uri (if it is in the DRM or media provider).
538 */
539 int ringtoneType = RingtoneManager.getDefaultType(uri);
540 // Above call returns -1 if the Uri doesn't match a default type
541 if (ringtoneType != -1) {
542 Context context = getContext();
543
544 // Get the current value for the default sound
545 Uri soundUri = RingtoneManager.getActualDefaultRingtoneUri(context, ringtoneType);
546
547 if (soundUri != null) {
548 // Only proxy the openFile call to drm or media providers
549 String authority = soundUri.getAuthority();
550 boolean isDrmAuthority = authority.equals(DrmStore.AUTHORITY);
551 if (isDrmAuthority || authority.equals(MediaStore.AUTHORITY)) {
552
553 if (isDrmAuthority) {
554 try {
555 // Check DRM access permission here, since once we
556 // do the below call the DRM will be checking our
557 // permission, not our caller's permission
558 DrmStore.enforceAccessDrmPermission(context);
559 } catch (SecurityException e) {
560 throw new FileNotFoundException(e.getMessage());
561 }
562 }
563
564 ParcelFileDescriptor pfd = null;
565 try {
566 pfd = context.getContentResolver().openFileDescriptor(soundUri, mode);
567 return new AssetFileDescriptor(pfd, 0, -1);
568 } catch (FileNotFoundException ex) {
569 // fall through and open the fallback ringtone below
570 }
571 }
572
573 try {
574 return super.openAssetFile(soundUri, mode);
575 } catch (FileNotFoundException ex) {
576 // Since a non-null Uri was specified, but couldn't be opened,
577 // fall back to the built-in ringtone.
578 return context.getResources().openRawResourceFd(
579 com.android.internal.R.raw.fallbackring);
580 }
581 }
582 // no need to fall through and have openFile() try again, since we
583 // already know that will fail.
584 throw new FileNotFoundException(); // or return null ?
585 }
586
587 // Note that this will end up calling openFile() above.
588 return super.openAssetFile(uri, mode);
589 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800590
591 /**
592 * In-memory LRU Cache of system and secure settings, along with
593 * associated helper functions to keep cache coherent with the
594 * database.
595 */
596 private static final class SettingsCache extends LinkedHashMap<String, Bundle> {
Brad Fitzpatrick342984a2010-03-09 16:59:30 -0800597
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800598 public SettingsCache() {
599 super(MAX_CACHE_ENTRIES, 0.75f /* load factor */, true /* access ordered */);
600 }
601
602 @Override
603 protected boolean removeEldestEntry(Map.Entry eldest) {
604 return size() > MAX_CACHE_ENTRIES;
605 }
606
Brad Fitzpatrick342984a2010-03-09 16:59:30 -0800607 /**
608 * Atomic cache population, conditional on size of value and if
609 * we lost a race.
610 *
611 * @returns a Bundle to send back to the client from call(), even
612 * if we lost the race.
613 */
614 public Bundle putIfAbsent(String key, String value) {
615 Bundle bundle = (value == null) ? NULL_SETTING : Bundle.forPair("value", value);
616 if (value == null || value.length() <= MAX_CACHE_ENTRY_SIZE) {
617 synchronized (this) {
618 if (!containsKey(key)) {
619 put(key, bundle);
620 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800621 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800622 }
Brad Fitzpatrick342984a2010-03-09 16:59:30 -0800623 return bundle;
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800624 }
625
626 public static SettingsCache forTable(String tableName) {
627 if ("system".equals(tableName)) {
628 return SettingsProvider.sSystemCache;
629 }
630 if ("secure".equals(tableName)) {
631 return SettingsProvider.sSecureCache;
632 }
633 return null;
634 }
635
636 /**
637 * Populates a key in a given (possibly-null) cache.
638 */
639 public static void populate(SettingsCache cache, ContentValues contentValues) {
640 if (cache == null) {
641 return;
642 }
643 String name = contentValues.getAsString(Settings.NameValueTable.NAME);
644 if (name == null) {
645 Log.w(TAG, "null name populating settings cache.");
646 return;
647 }
648 String value = contentValues.getAsString(Settings.NameValueTable.VALUE);
649 synchronized (cache) {
Brad Fitzpatrick342984a2010-03-09 16:59:30 -0800650 if (value == null || value.length() <= MAX_CACHE_ENTRY_SIZE) {
651 cache.put(name, Bundle.forPair(Settings.NameValueTable.VALUE, value));
652 } else {
653 cache.remove(name);
654 }
Brad Fitzpatrick1bd62bd2010-03-08 18:30:52 -0800655 }
656 }
657
658 /**
659 * Used for wiping a whole cache on deletes when we're not
660 * sure what exactly was deleted or changed.
661 */
662 public static void wipe(String tableName) {
663 SettingsCache cache = SettingsCache.forTable(tableName);
664 if (cache == null) {
665 return;
666 }
667 synchronized (cache) {
668 cache.clear();
669 }
670 }
671
672 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700673}