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); |
Dianne Hackborn | 24117ce | 2010-07-12 15:54:38 -0700 | [diff] [blame] | 86 | if (!DatabaseHelper.isValidTable(this.table)) { |
| 87 | throw new IllegalArgumentException("Bad root path: " + this.table); |
| 88 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 89 | this.where = where; |
| 90 | this.args = args; |
| 91 | } else if (url.getPathSegments().size() != 2) { |
| 92 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 93 | } else if (!TextUtils.isEmpty(where)) { |
| 94 | throw new UnsupportedOperationException("WHERE clause not supported: " + url); |
| 95 | } else { |
| 96 | this.table = url.getPathSegments().get(0); |
Dianne Hackborn | 24117ce | 2010-07-12 15:54:38 -0700 | [diff] [blame] | 97 | if (!DatabaseHelper.isValidTable(this.table)) { |
| 98 | throw new IllegalArgumentException("Bad root path: " + this.table); |
| 99 | } |
Doug Zongker | aed8f8e | 2010-01-07 18:07:50 -0800 | [diff] [blame] | 100 | if ("system".equals(this.table) || "secure".equals(this.table)) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 101 | this.where = Settings.NameValueTable.NAME + "=?"; |
| 102 | this.args = new String[] { url.getPathSegments().get(1) }; |
| 103 | } else { |
| 104 | this.where = "_id=" + ContentUris.parseId(url); |
| 105 | this.args = null; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** Insert new rows (no where clause allowed). */ |
| 111 | SqlArguments(Uri url) { |
| 112 | if (url.getPathSegments().size() == 1) { |
| 113 | this.table = url.getPathSegments().get(0); |
Dianne Hackborn | 24117ce | 2010-07-12 15:54:38 -0700 | [diff] [blame] | 114 | if (!DatabaseHelper.isValidTable(this.table)) { |
| 115 | throw new IllegalArgumentException("Bad root path: " + this.table); |
| 116 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 117 | this.where = null; |
| 118 | this.args = null; |
| 119 | } else { |
| 120 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get the content URI of a row added to a table. |
| 127 | * @param tableUri of the entire table |
| 128 | * @param values found in the row |
| 129 | * @param rowId of the row |
| 130 | * @return the content URI for this particular row |
| 131 | */ |
| 132 | private Uri getUriFor(Uri tableUri, ContentValues values, long rowId) { |
| 133 | if (tableUri.getPathSegments().size() != 1) { |
| 134 | throw new IllegalArgumentException("Invalid URI: " + tableUri); |
| 135 | } |
| 136 | String table = tableUri.getPathSegments().get(0); |
Doug Zongker | aed8f8e | 2010-01-07 18:07:50 -0800 | [diff] [blame] | 137 | if ("system".equals(table) || "secure".equals(table)) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 138 | String name = values.getAsString(Settings.NameValueTable.NAME); |
| 139 | return Uri.withAppendedPath(tableUri, name); |
| 140 | } else { |
| 141 | return ContentUris.withAppendedId(tableUri, rowId); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Send a notification when a particular content URI changes. |
| 147 | * Modify the system property used to communicate the version of |
| 148 | * this table, for tables which have such a property. (The Settings |
| 149 | * contract class uses these to provide client-side caches.) |
| 150 | * @param uri to send notifications for |
| 151 | */ |
| 152 | private void sendNotify(Uri uri) { |
| 153 | // Update the system property *first*, so if someone is listening for |
| 154 | // a notification and then using the contract class to get their data, |
| 155 | // the system property will be updated and they'll get the new data. |
| 156 | |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 157 | boolean backedUpDataChanged = false; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 158 | String property = null, table = uri.getPathSegments().get(0); |
| 159 | if (table.equals("system")) { |
| 160 | property = Settings.System.SYS_PROP_SETTING_VERSION; |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 161 | backedUpDataChanged = true; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 162 | } else if (table.equals("secure")) { |
| 163 | property = Settings.Secure.SYS_PROP_SETTING_VERSION; |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 164 | backedUpDataChanged = true; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | if (property != null) { |
| 168 | long version = SystemProperties.getLong(property, 0) + 1; |
| 169 | if (LOCAL_LOGV) Log.v(TAG, "property: " + property + "=" + version); |
| 170 | SystemProperties.set(property, Long.toString(version)); |
| 171 | } |
| 172 | |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 173 | // Inform the backup manager about a data change |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 174 | if (backedUpDataChanged) { |
| 175 | mBackupManager.dataChanged(); |
| 176 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 177 | // Now send the notification through the content framework. |
| 178 | |
| 179 | String notify = uri.getQueryParameter("notify"); |
| 180 | if (notify == null || "true".equals(notify)) { |
| 181 | getContext().getContentResolver().notifyChange(uri, null); |
| 182 | if (LOCAL_LOGV) Log.v(TAG, "notifying: " + uri); |
| 183 | } else { |
| 184 | if (LOCAL_LOGV) Log.v(TAG, "notification suppressed: " + uri); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Make sure the caller has permission to write this data. |
| 190 | * @param args supplied by the caller |
| 191 | * @throws SecurityException if the caller is forbidden to write. |
| 192 | */ |
| 193 | private void checkWritePermissions(SqlArguments args) { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 194 | if ("secure".equals(args.table) && |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 195 | getContext().checkCallingOrSelfPermission( |
Doug Zongker | aed8f8e | 2010-01-07 18:07:50 -0800 | [diff] [blame] | 196 | android.Manifest.permission.WRITE_SECURE_SETTINGS) != |
| 197 | PackageManager.PERMISSION_GRANTED) { |
Brett Chabot | 16dd82c | 2009-06-18 17:00:48 -0700 | [diff] [blame] | 198 | throw new SecurityException( |
Doug Zongker | aed8f8e | 2010-01-07 18:07:50 -0800 | [diff] [blame] | 199 | String.format("Permission denial: writing to secure settings requires %1$s", |
| 200 | android.Manifest.permission.WRITE_SECURE_SETTINGS)); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public boolean onCreate() { |
| 206 | mOpenHelper = new DatabaseHelper(getContext()); |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 207 | mBackupManager = new BackupManager(getContext()); |
Fred Quintana | c70239e | 2009-12-17 10:28:33 -0800 | [diff] [blame] | 208 | |
| 209 | if (!ensureAndroidIdIsSet()) { |
| 210 | return false; |
| 211 | } |
| 212 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 213 | return true; |
| 214 | } |
| 215 | |
Fred Quintana | c70239e | 2009-12-17 10:28:33 -0800 | [diff] [blame] | 216 | private boolean ensureAndroidIdIsSet() { |
| 217 | final Cursor c = query(Settings.Secure.CONTENT_URI, |
| 218 | new String[] { Settings.NameValueTable.VALUE }, |
| 219 | Settings.NameValueTable.NAME + "=?", |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 220 | new String[] { Settings.Secure.ANDROID_ID }, null); |
Fred Quintana | c70239e | 2009-12-17 10:28:33 -0800 | [diff] [blame] | 221 | try { |
| 222 | final String value = c.moveToNext() ? c.getString(0) : null; |
| 223 | if (value == null) { |
| 224 | final SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); |
Doug Zongker | 0fe27cf | 2010-08-19 13:38:26 -0700 | [diff] [blame] | 225 | String serial = SystemProperties.get("ro.serialno", ""); |
| 226 | random.setSeed( |
| 227 | (serial + System.nanoTime() + new SecureRandom().nextLong()).getBytes()); |
Fred Quintana | c70239e | 2009-12-17 10:28:33 -0800 | [diff] [blame] | 228 | final String newAndroidIdValue = Long.toHexString(random.nextLong()); |
Doug Zongker | 0fe27cf | 2010-08-19 13:38:26 -0700 | [diff] [blame] | 229 | Log.d(TAG, "Generated and saved new ANDROID_ID [" + newAndroidIdValue + "]"); |
Fred Quintana | c70239e | 2009-12-17 10:28:33 -0800 | [diff] [blame] | 230 | final ContentValues values = new ContentValues(); |
| 231 | values.put(Settings.NameValueTable.NAME, Settings.Secure.ANDROID_ID); |
| 232 | values.put(Settings.NameValueTable.VALUE, newAndroidIdValue); |
| 233 | final Uri uri = insert(Settings.Secure.CONTENT_URI, values); |
| 234 | if (uri == null) { |
| 235 | return false; |
| 236 | } |
| 237 | } |
| 238 | return true; |
| 239 | } catch (NoSuchAlgorithmException e) { |
| 240 | return false; |
| 241 | } finally { |
| 242 | c.close(); |
| 243 | } |
| 244 | } |
| 245 | |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 246 | /** |
| 247 | * Fast path that avoids the use of chatty remoted Cursors. |
| 248 | */ |
| 249 | @Override |
| 250 | public Bundle call(String method, String request, Bundle args) { |
| 251 | if (Settings.CALL_METHOD_GET_SYSTEM.equals(method)) { |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 252 | return lookupValue("system", sSystemCache, request); |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 253 | } |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 254 | if (Settings.CALL_METHOD_GET_SECURE.equals(method)) { |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 255 | return lookupValue("secure", sSecureCache, request); |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 256 | } |
| 257 | return null; |
| 258 | } |
| 259 | |
| 260 | // Looks up value 'key' in 'table' and returns either a single-pair Bundle, |
| 261 | // possibly with a null value, or null on failure. |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 262 | private Bundle lookupValue(String table, SettingsCache cache, String key) { |
| 263 | synchronized (cache) { |
| 264 | if (cache.containsKey(key)) { |
| 265 | return cache.get(key); |
| 266 | } |
| 267 | } |
| 268 | |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 269 | SQLiteDatabase db = mOpenHelper.getReadableDatabase(); |
| 270 | Cursor cursor = null; |
| 271 | try { |
| 272 | cursor = db.query(table, COLUMN_VALUE, "name=?", new String[]{key}, |
| 273 | null, null, null, null); |
| 274 | if (cursor != null && cursor.getCount() == 1) { |
| 275 | cursor.moveToFirst(); |
Brad Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame] | 276 | return cache.putIfAbsent(key, cursor.getString(0)); |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 277 | } |
| 278 | } catch (SQLiteException e) { |
| 279 | Log.w(TAG, "settings lookup error", e); |
| 280 | return null; |
| 281 | } finally { |
| 282 | if (cursor != null) cursor.close(); |
| 283 | } |
Brad Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame] | 284 | cache.putIfAbsent(key, null); |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 285 | return NULL_SETTING; |
Brad Fitzpatrick | 1877d01 | 2010-03-04 17:48:13 -0800 | [diff] [blame] | 286 | } |
| 287 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 288 | @Override |
| 289 | public Cursor query(Uri url, String[] select, String where, String[] whereArgs, String sort) { |
| 290 | SqlArguments args = new SqlArguments(url, where, whereArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | SQLiteDatabase db = mOpenHelper.getReadableDatabase(); |
| 292 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 293 | // The favorites table was moved from this provider to a provider inside Home |
| 294 | // Home still need to query this table to upgrade from pre-cupcake builds |
| 295 | // However, a cupcake+ build with no data does not contain this table which will |
| 296 | // cause an exception in the SQL stack. The following line is a special case to |
| 297 | // 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] | 298 | if (TABLE_FAVORITES.equals(args.table)) { |
| 299 | return null; |
| 300 | } else if (TABLE_OLD_FAVORITES.equals(args.table)) { |
| 301 | args.table = TABLE_FAVORITES; |
| 302 | Cursor cursor = db.rawQuery("PRAGMA table_info(favorites);", null); |
| 303 | if (cursor != null) { |
| 304 | boolean exists = cursor.getCount() > 0; |
| 305 | cursor.close(); |
| 306 | if (!exists) return null; |
| 307 | } else { |
| 308 | return null; |
| 309 | } |
| 310 | } |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 311 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 312 | SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); |
| 313 | qb.setTables(args.table); |
| 314 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 315 | Cursor ret = qb.query(db, select, args.where, args.args, null, null, sort); |
| 316 | ret.setNotificationUri(getContext().getContentResolver(), url); |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public String getType(Uri url) { |
| 322 | // If SqlArguments supplies a where clause, then it must be an item |
| 323 | // (because we aren't supplying our own where clause). |
| 324 | SqlArguments args = new SqlArguments(url, null, null); |
| 325 | if (TextUtils.isEmpty(args.where)) { |
| 326 | return "vnd.android.cursor.dir/" + args.table; |
| 327 | } else { |
| 328 | return "vnd.android.cursor.item/" + args.table; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | @Override |
| 333 | public int bulkInsert(Uri uri, ContentValues[] values) { |
| 334 | SqlArguments args = new SqlArguments(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | if (TABLE_FAVORITES.equals(args.table)) { |
| 336 | return 0; |
| 337 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 338 | checkWritePermissions(args); |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 339 | SettingsCache cache = SettingsCache.forTable(args.table); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 340 | |
| 341 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 342 | db.beginTransaction(); |
| 343 | try { |
| 344 | int numValues = values.length; |
| 345 | for (int i = 0; i < numValues; i++) { |
| 346 | if (db.insert(args.table, null, values[i]) < 0) return 0; |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 347 | SettingsCache.populate(cache, values[i]); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 348 | if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + values[i]); |
| 349 | } |
| 350 | db.setTransactionSuccessful(); |
| 351 | } finally { |
| 352 | db.endTransaction(); |
| 353 | } |
| 354 | |
| 355 | sendNotify(uri); |
| 356 | return values.length; |
| 357 | } |
| 358 | |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 359 | /* |
| 360 | * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED. |
| 361 | * This setting contains a list of the currently enabled location providers. |
| 362 | * But helper functions in android.providers.Settings can enable or disable |
| 363 | * a single provider by using a "+" or "-" prefix before the provider name. |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 364 | * |
| 365 | * @returns whether the database needs to be updated or not, also modifying |
| 366 | * 'initialValues' if needed. |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 367 | */ |
| 368 | private boolean parseProviderList(Uri url, ContentValues initialValues) { |
| 369 | String value = initialValues.getAsString(Settings.Secure.VALUE); |
| 370 | String newProviders = null; |
| 371 | if (value != null && value.length() > 1) { |
| 372 | char prefix = value.charAt(0); |
| 373 | if (prefix == '+' || prefix == '-') { |
| 374 | // skip prefix |
| 375 | value = value.substring(1); |
| 376 | |
| 377 | // read list of enabled providers into "providers" |
| 378 | String providers = ""; |
| 379 | String[] columns = {Settings.Secure.VALUE}; |
| 380 | String where = Settings.Secure.NAME + "=\'" + Settings.Secure.LOCATION_PROVIDERS_ALLOWED + "\'"; |
| 381 | Cursor cursor = query(url, columns, where, null, null); |
| 382 | if (cursor != null && cursor.getCount() == 1) { |
| 383 | try { |
| 384 | cursor.moveToFirst(); |
| 385 | providers = cursor.getString(0); |
| 386 | } finally { |
| 387 | cursor.close(); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | int index = providers.indexOf(value); |
| 392 | int end = index + value.length(); |
| 393 | // check for commas to avoid matching on partial string |
| 394 | if (index > 0 && providers.charAt(index - 1) != ',') index = -1; |
| 395 | if (end < providers.length() && providers.charAt(end) != ',') index = -1; |
| 396 | |
| 397 | if (prefix == '+' && index < 0) { |
| 398 | // append the provider to the list if not present |
| 399 | if (providers.length() == 0) { |
| 400 | newProviders = value; |
| 401 | } else { |
| 402 | newProviders = providers + ',' + value; |
| 403 | } |
| 404 | } else if (prefix == '-' && index >= 0) { |
| 405 | // remove the provider from the list if present |
Mike Lockwood | bdc7f89 | 2010-04-21 18:24:57 -0400 | [diff] [blame] | 406 | // remove leading or trailing comma |
| 407 | if (index > 0) { |
| 408 | index--; |
| 409 | } else if (end < providers.length()) { |
| 410 | end++; |
| 411 | } |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 412 | |
| 413 | newProviders = providers.substring(0, index); |
| 414 | if (end < providers.length()) { |
| 415 | newProviders += providers.substring(end); |
| 416 | } |
| 417 | } else { |
| 418 | // nothing changed, so no need to update the database |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | if (newProviders != null) { |
| 423 | initialValues.put(Settings.Secure.VALUE, newProviders); |
| 424 | } |
| 425 | } |
| 426 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 427 | |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 428 | return true; |
| 429 | } |
| 430 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 431 | @Override |
| 432 | public Uri insert(Uri url, ContentValues initialValues) { |
| 433 | SqlArguments args = new SqlArguments(url); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | if (TABLE_FAVORITES.equals(args.table)) { |
| 435 | return null; |
| 436 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 437 | checkWritePermissions(args); |
| 438 | |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 439 | // Special case LOCATION_PROVIDERS_ALLOWED. |
| 440 | // Support enabling/disabling a single provider (using "+" or "-" prefix) |
| 441 | String name = initialValues.getAsString(Settings.Secure.NAME); |
| 442 | if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) { |
| 443 | if (!parseProviderList(url, initialValues)) return null; |
| 444 | } |
| 445 | |
Brad Fitzpatrick | 547a96b | 2010-03-09 17:58:53 -0800 | [diff] [blame] | 446 | SettingsCache cache = SettingsCache.forTable(args.table); |
| 447 | String value = initialValues.getAsString(Settings.NameValueTable.VALUE); |
| 448 | if (SettingsCache.isRedundantSetValue(cache, name, value)) { |
| 449 | return Uri.withAppendedPath(url, name); |
| 450 | } |
| 451 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 452 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 453 | final long rowId = db.insert(args.table, null, initialValues); |
| 454 | if (rowId <= 0) return null; |
| 455 | |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 456 | SettingsCache.populate(cache, initialValues); // before we notify |
| 457 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 458 | if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + initialValues); |
| 459 | url = getUriFor(url, initialValues, rowId); |
| 460 | sendNotify(url); |
| 461 | return url; |
| 462 | } |
| 463 | |
| 464 | @Override |
| 465 | public int delete(Uri url, String where, String[] whereArgs) { |
| 466 | SqlArguments args = new SqlArguments(url, where, whereArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | if (TABLE_FAVORITES.equals(args.table)) { |
| 468 | return 0; |
| 469 | } else if (TABLE_OLD_FAVORITES.equals(args.table)) { |
| 470 | args.table = TABLE_FAVORITES; |
| 471 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 472 | checkWritePermissions(args); |
| 473 | |
| 474 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 475 | int count = db.delete(args.table, args.where, args.args); |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 476 | if (count > 0) { |
| 477 | SettingsCache.wipe(args.table); // before we notify |
| 478 | sendNotify(url); |
| 479 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 480 | if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) deleted"); |
| 481 | return count; |
| 482 | } |
| 483 | |
| 484 | @Override |
| 485 | public int update(Uri url, ContentValues initialValues, String where, String[] whereArgs) { |
| 486 | SqlArguments args = new SqlArguments(url, where, whereArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | if (TABLE_FAVORITES.equals(args.table)) { |
| 488 | return 0; |
| 489 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 490 | checkWritePermissions(args); |
| 491 | |
| 492 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 493 | int count = db.update(args.table, initialValues, args.where, args.args); |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 494 | if (count > 0) { |
| 495 | SettingsCache.wipe(args.table); // before we notify |
| 496 | sendNotify(url); |
| 497 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 498 | if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) <- " + initialValues); |
| 499 | return count; |
| 500 | } |
| 501 | |
| 502 | @Override |
| 503 | public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { |
| 504 | |
| 505 | /* |
| 506 | * When a client attempts to openFile the default ringtone or |
| 507 | * notification setting Uri, we will proxy the call to the current |
| 508 | * default ringtone's Uri (if it is in the DRM or media provider). |
| 509 | */ |
| 510 | int ringtoneType = RingtoneManager.getDefaultType(uri); |
| 511 | // Above call returns -1 if the Uri doesn't match a default type |
| 512 | if (ringtoneType != -1) { |
| 513 | Context context = getContext(); |
| 514 | |
| 515 | // Get the current value for the default sound |
| 516 | Uri soundUri = RingtoneManager.getActualDefaultRingtoneUri(context, ringtoneType); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 517 | |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 518 | if (soundUri != null) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 519 | // Only proxy the openFile call to drm or media providers |
| 520 | String authority = soundUri.getAuthority(); |
| 521 | boolean isDrmAuthority = authority.equals(DrmStore.AUTHORITY); |
| 522 | if (isDrmAuthority || authority.equals(MediaStore.AUTHORITY)) { |
| 523 | |
| 524 | if (isDrmAuthority) { |
| 525 | try { |
| 526 | // Check DRM access permission here, since once we |
| 527 | // do the below call the DRM will be checking our |
| 528 | // permission, not our caller's permission |
| 529 | DrmStore.enforceAccessDrmPermission(context); |
| 530 | } catch (SecurityException e) { |
| 531 | throw new FileNotFoundException(e.getMessage()); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | return context.getContentResolver().openFileDescriptor(soundUri, mode); |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | return super.openFile(uri, mode); |
| 541 | } |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 542 | |
| 543 | @Override |
| 544 | public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException { |
| 545 | |
| 546 | /* |
| 547 | * When a client attempts to openFile the default ringtone or |
| 548 | * notification setting Uri, we will proxy the call to the current |
| 549 | * default ringtone's Uri (if it is in the DRM or media provider). |
| 550 | */ |
| 551 | int ringtoneType = RingtoneManager.getDefaultType(uri); |
| 552 | // Above call returns -1 if the Uri doesn't match a default type |
| 553 | if (ringtoneType != -1) { |
| 554 | Context context = getContext(); |
| 555 | |
| 556 | // Get the current value for the default sound |
| 557 | Uri soundUri = RingtoneManager.getActualDefaultRingtoneUri(context, ringtoneType); |
| 558 | |
| 559 | if (soundUri != null) { |
| 560 | // Only proxy the openFile call to drm or media providers |
| 561 | String authority = soundUri.getAuthority(); |
| 562 | boolean isDrmAuthority = authority.equals(DrmStore.AUTHORITY); |
| 563 | if (isDrmAuthority || authority.equals(MediaStore.AUTHORITY)) { |
| 564 | |
| 565 | if (isDrmAuthority) { |
| 566 | try { |
| 567 | // Check DRM access permission here, since once we |
| 568 | // do the below call the DRM will be checking our |
| 569 | // permission, not our caller's permission |
| 570 | DrmStore.enforceAccessDrmPermission(context); |
| 571 | } catch (SecurityException e) { |
| 572 | throw new FileNotFoundException(e.getMessage()); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | ParcelFileDescriptor pfd = null; |
| 577 | try { |
| 578 | pfd = context.getContentResolver().openFileDescriptor(soundUri, mode); |
| 579 | return new AssetFileDescriptor(pfd, 0, -1); |
| 580 | } catch (FileNotFoundException ex) { |
| 581 | // fall through and open the fallback ringtone below |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | try { |
| 586 | return super.openAssetFile(soundUri, mode); |
| 587 | } catch (FileNotFoundException ex) { |
| 588 | // Since a non-null Uri was specified, but couldn't be opened, |
| 589 | // fall back to the built-in ringtone. |
| 590 | return context.getResources().openRawResourceFd( |
| 591 | com.android.internal.R.raw.fallbackring); |
| 592 | } |
| 593 | } |
| 594 | // no need to fall through and have openFile() try again, since we |
| 595 | // already know that will fail. |
| 596 | throw new FileNotFoundException(); // or return null ? |
| 597 | } |
| 598 | |
| 599 | // Note that this will end up calling openFile() above. |
| 600 | return super.openAssetFile(uri, mode); |
| 601 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 602 | |
| 603 | /** |
| 604 | * In-memory LRU Cache of system and secure settings, along with |
| 605 | * associated helper functions to keep cache coherent with the |
| 606 | * database. |
| 607 | */ |
| 608 | private static final class SettingsCache extends LinkedHashMap<String, Bundle> { |
Brad Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame] | 609 | |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 610 | public SettingsCache() { |
| 611 | super(MAX_CACHE_ENTRIES, 0.75f /* load factor */, true /* access ordered */); |
| 612 | } |
| 613 | |
| 614 | @Override |
| 615 | protected boolean removeEldestEntry(Map.Entry eldest) { |
| 616 | return size() > MAX_CACHE_ENTRIES; |
| 617 | } |
| 618 | |
Brad Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame] | 619 | /** |
| 620 | * Atomic cache population, conditional on size of value and if |
| 621 | * we lost a race. |
| 622 | * |
| 623 | * @returns a Bundle to send back to the client from call(), even |
| 624 | * if we lost the race. |
| 625 | */ |
| 626 | public Bundle putIfAbsent(String key, String value) { |
| 627 | Bundle bundle = (value == null) ? NULL_SETTING : Bundle.forPair("value", value); |
| 628 | if (value == null || value.length() <= MAX_CACHE_ENTRY_SIZE) { |
| 629 | synchronized (this) { |
| 630 | if (!containsKey(key)) { |
| 631 | put(key, bundle); |
| 632 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 633 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 634 | } |
Brad Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame] | 635 | return bundle; |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | public static SettingsCache forTable(String tableName) { |
| 639 | if ("system".equals(tableName)) { |
| 640 | return SettingsProvider.sSystemCache; |
| 641 | } |
| 642 | if ("secure".equals(tableName)) { |
| 643 | return SettingsProvider.sSecureCache; |
| 644 | } |
| 645 | return null; |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * Populates a key in a given (possibly-null) cache. |
| 650 | */ |
| 651 | public static void populate(SettingsCache cache, ContentValues contentValues) { |
| 652 | if (cache == null) { |
| 653 | return; |
| 654 | } |
| 655 | String name = contentValues.getAsString(Settings.NameValueTable.NAME); |
| 656 | if (name == null) { |
| 657 | Log.w(TAG, "null name populating settings cache."); |
| 658 | return; |
| 659 | } |
| 660 | String value = contentValues.getAsString(Settings.NameValueTable.VALUE); |
| 661 | synchronized (cache) { |
Brad Fitzpatrick | 342984a | 2010-03-09 16:59:30 -0800 | [diff] [blame] | 662 | if (value == null || value.length() <= MAX_CACHE_ENTRY_SIZE) { |
| 663 | cache.put(name, Bundle.forPair(Settings.NameValueTable.VALUE, value)); |
| 664 | } else { |
| 665 | cache.remove(name); |
| 666 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Used for wiping a whole cache on deletes when we're not |
| 672 | * sure what exactly was deleted or changed. |
| 673 | */ |
| 674 | public static void wipe(String tableName) { |
| 675 | SettingsCache cache = SettingsCache.forTable(tableName); |
| 676 | if (cache == null) { |
| 677 | return; |
| 678 | } |
| 679 | synchronized (cache) { |
| 680 | cache.clear(); |
| 681 | } |
| 682 | } |
| 683 | |
Brad Fitzpatrick | 547a96b | 2010-03-09 17:58:53 -0800 | [diff] [blame] | 684 | /** |
| 685 | * For suppressing duplicate/redundant settings inserts early, |
| 686 | * checking our cache first (but without faulting it in), |
| 687 | * before going to sqlite with the mutation. |
| 688 | */ |
| 689 | public static boolean isRedundantSetValue(SettingsCache cache, String name, String value) { |
| 690 | if (cache == null) return false; |
| 691 | synchronized (cache) { |
| 692 | Bundle bundle = cache.get(name); |
| 693 | if (bundle == null) return false; |
| 694 | String oldValue = bundle.getPairValue(); |
| 695 | if (oldValue == null && value == null) return true; |
| 696 | if ((oldValue == null) != (value == null)) return false; |
| 697 | return oldValue.equals(value); |
| 698 | } |
| 699 | } |
Brad Fitzpatrick | 1bd62bd | 2010-03-08 18:30:52 -0800 | [diff] [blame] | 700 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 701 | } |