The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.providers.settings; |
| 18 | |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 19 | import java.io.FileNotFoundException; |
Doug Zongker | 4f8ff39 | 2010-02-03 10:36:40 -0800 | [diff] [blame] | 20 | import java.io.UnsupportedEncodingException; |
Fred Quintana | c70239e | 2009-12-17 10:28:33 -0800 | [diff] [blame] | 21 | import java.security.NoSuchAlgorithmException; |
Doug Zongker | 4f8ff39 | 2010-02-03 10:36:40 -0800 | [diff] [blame] | 22 | import java.security.SecureRandom; |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 23 | import java.util.LinkedHashMap; |
| 24 | import java.util.Map; |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 25 | |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 26 | import android.app.backup.BackupManager; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | import android.content.ContentProvider; |
| 28 | import android.content.ContentUris; |
| 29 | import android.content.ContentValues; |
| 30 | import android.content.Context; |
| 31 | import android.content.pm.PackageManager; |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 32 | import android.content.res.AssetFileDescriptor; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | import android.database.Cursor; |
| 34 | import android.database.sqlite.SQLiteDatabase; |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 35 | import android.database.sqlite.SQLiteException; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | import android.database.sqlite.SQLiteQueryBuilder; |
| 37 | import android.media.RingtoneManager; |
| 38 | import android.net.Uri; |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 39 | import android.os.Bundle; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 40 | import android.os.ParcelFileDescriptor; |
| 41 | import android.os.SystemProperties; |
| 42 | import android.provider.DrmStore; |
| 43 | import android.provider.MediaStore; |
| 44 | import android.provider.Settings; |
| 45 | import android.text.TextUtils; |
| 46 | import android.util.Log; |
| 47 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 48 | public class SettingsProvider extends ContentProvider { |
| 49 | private static final String TAG = "SettingsProvider"; |
| 50 | private static final boolean LOCAL_LOGV = false; |
| 51 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | private static final String TABLE_FAVORITES = "favorites"; |
| 53 | private static final String TABLE_OLD_FAVORITES = "old_favorites"; |
| 54 | |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 55 | private static final String[] COLUMN_VALUE = new String[] { "value" }; |
| 56 | |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 57 | // 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 Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame^] | 63 | // 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 Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 68 | private static final Bundle NULL_SETTING = Bundle.forPair("value", null); |
| 69 | |
James Wylder | 074da8f | 2009-02-25 08:38:31 -0600 | [diff] [blame] | 70 | protected DatabaseHelper mOpenHelper; |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 71 | private BackupManager mBackupManager; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 72 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | public String table; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 79 | 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 Zongker | aed8f8e | 2010-01-07 18:07:50 -0800 | [diff] [blame] | 94 | if ("system".equals(this.table) || "secure".equals(this.table)) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 95 | 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 Zongker | aed8f8e | 2010-01-07 18:07:50 -0800 | [diff] [blame] | 128 | if ("system".equals(table) || "secure".equals(table)) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 129 | 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 Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 148 | boolean backedUpDataChanged = false; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 149 | String property = null, table = uri.getPathSegments().get(0); |
| 150 | if (table.equals("system")) { |
| 151 | property = Settings.System.SYS_PROP_SETTING_VERSION; |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 152 | backedUpDataChanged = true; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 153 | } else if (table.equals("secure")) { |
| 154 | property = Settings.Secure.SYS_PROP_SETTING_VERSION; |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 155 | backedUpDataChanged = true; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 156 | } |
| 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 master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 164 | // Inform the backup manager about a data change |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 165 | if (backedUpDataChanged) { |
| 166 | mBackupManager.dataChanged(); |
| 167 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 168 | // 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 Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 185 | if ("secure".equals(args.table) && |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 186 | getContext().checkCallingOrSelfPermission( |
Doug Zongker | aed8f8e | 2010-01-07 18:07:50 -0800 | [diff] [blame] | 187 | android.Manifest.permission.WRITE_SECURE_SETTINGS) != |
| 188 | PackageManager.PERMISSION_GRANTED) { |
Brett Chabot | 16dd82c | 2009-06-18 17:00:48 -0700 | [diff] [blame] | 189 | throw new SecurityException( |
Doug Zongker | aed8f8e | 2010-01-07 18:07:50 -0800 | [diff] [blame] | 190 | String.format("Permission denial: writing to secure settings requires %1$s", |
| 191 | android.Manifest.permission.WRITE_SECURE_SETTINGS)); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public boolean onCreate() { |
| 197 | mOpenHelper = new DatabaseHelper(getContext()); |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 198 | mBackupManager = new BackupManager(getContext()); |
Fred Quintana | c70239e | 2009-12-17 10:28:33 -0800 | [diff] [blame] | 199 | |
| 200 | if (!ensureAndroidIdIsSet()) { |
| 201 | return false; |
| 202 | } |
| 203 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 204 | return true; |
| 205 | } |
| 206 | |
Fred Quintana | c70239e | 2009-12-17 10:28:33 -0800 | [diff] [blame] | 207 | private boolean ensureAndroidIdIsSet() { |
| 208 | final Cursor c = query(Settings.Secure.CONTENT_URI, |
| 209 | new String[] { Settings.NameValueTable.VALUE }, |
| 210 | Settings.NameValueTable.NAME + "=?", |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 211 | new String[] { Settings.Secure.ANDROID_ID }, null); |
Fred Quintana | c70239e | 2009-12-17 10:28:33 -0800 | [diff] [blame] | 212 | try { |
| 213 | final String value = c.moveToNext() ? c.getString(0) : null; |
| 214 | if (value == null) { |
| 215 | final SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); |
Doug Zongker | 4f8ff39 | 2010-02-03 10:36:40 -0800 | [diff] [blame] | 216 | 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 Quintana | c70239e | 2009-12-17 10:28:33 -0800 | [diff] [blame] | 224 | 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 Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 242 | /** |
| 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 Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 248 | return lookupValue("system", sSystemCache, request); |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 249 | } |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 250 | if (Settings.CALL_METHOD_GET_SECURE.equals(method)) { |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 251 | return lookupValue("secure", sSecureCache, request); |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 252 | } |
| 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 Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 258 | 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 Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 265 | 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 Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame^] | 272 | return cache.putIfAbsent(key, cursor.getString(0)); |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 273 | } |
| 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 Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame^] | 280 | cache.putIfAbsent(key, null); |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 281 | return NULL_SETTING; |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 282 | } |
| 283 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 284 | @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | SQLiteDatabase db = mOpenHelper.getReadableDatabase(); |
| 288 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 289 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | 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 Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 307 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 308 | SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); |
| 309 | qb.setTables(args.table); |
| 310 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 311 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | if (TABLE_FAVORITES.equals(args.table)) { |
| 332 | return 0; |
| 333 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 334 | checkWritePermissions(args); |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 335 | SettingsCache cache = SettingsCache.forTable(args.table); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 336 | |
| 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 Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 343 | SettingsCache.populate(cache, values[i]); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 344 | 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 Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 355 | /* |
| 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 Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 360 | * |
| 361 | * @returns whether the database needs to be updated or not, also modifying |
| 362 | * 'initialValues' if needed. |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 363 | */ |
| 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 Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 420 | |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 421 | return true; |
| 422 | } |
| 423 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 424 | @Override |
| 425 | public Uri insert(Uri url, ContentValues initialValues) { |
| 426 | SqlArguments args = new SqlArguments(url); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | if (TABLE_FAVORITES.equals(args.table)) { |
| 428 | return null; |
| 429 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 430 | checkWritePermissions(args); |
| 431 | |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 432 | // 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 Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 439 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 440 | final long rowId = db.insert(args.table, null, initialValues); |
| 441 | if (rowId <= 0) return null; |
| 442 | |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 443 | SettingsCache cache = SettingsCache.forTable(args.table); |
| 444 | SettingsCache.populate(cache, initialValues); // before we notify |
| 445 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 446 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | 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 Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 460 | checkWritePermissions(args); |
| 461 | |
| 462 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 463 | int count = db.delete(args.table, args.where, args.args); |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 464 | if (count > 0) { |
| 465 | SettingsCache.wipe(args.table); // before we notify |
| 466 | sendNotify(url); |
| 467 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 468 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | if (TABLE_FAVORITES.equals(args.table)) { |
| 476 | return 0; |
| 477 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 478 | checkWritePermissions(args); |
| 479 | |
| 480 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 481 | int count = db.update(args.table, initialValues, args.where, args.args); |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 482 | if (count > 0) { |
| 483 | SettingsCache.wipe(args.table); // before we notify |
| 484 | sendNotify(url); |
| 485 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 486 | 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 Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 505 | |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 506 | if (soundUri != null) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 507 | // 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 Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 530 | |
| 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 Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 590 | |
| 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 Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame^] | 597 | |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 598 | 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 Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame^] | 607 | /** |
| 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 Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 621 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 622 | } |
Brad Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame^] | 623 | return bundle; |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 624 | } |
| 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 Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame^] | 650 | 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 Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 655 | } |
| 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 Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 673 | } |