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; |
| 23 | import java.util.Random; |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 24 | |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 25 | import android.backup.BackupManager; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | import android.content.ContentProvider; |
| 27 | import android.content.ContentUris; |
| 28 | import android.content.ContentValues; |
| 29 | import android.content.Context; |
| 30 | import android.content.pm.PackageManager; |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 31 | import android.content.res.AssetFileDescriptor; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 32 | import android.database.Cursor; |
Fred Quintana | c70239e | 2009-12-17 10:28:33 -0800 | [diff] [blame] | 33 | import android.database.SQLException; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | import android.database.sqlite.SQLiteDatabase; |
| 35 | import android.database.sqlite.SQLiteQueryBuilder; |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 36 | import android.media.Ringtone; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 37 | import android.media.RingtoneManager; |
| 38 | import android.net.Uri; |
| 39 | import android.os.ParcelFileDescriptor; |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 40 | import android.os.ServiceManager; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
James Wylder | 074da8f | 2009-02-25 08:38:31 -0600 | [diff] [blame] | 55 | protected DatabaseHelper mOpenHelper; |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 56 | private BackupManager mBackupManager; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Decode a content URL into the table, projection, and arguments |
| 60 | * used to access the corresponding database rows. |
| 61 | */ |
| 62 | private static class SqlArguments { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | public String table; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 64 | public final String where; |
| 65 | public final String[] args; |
| 66 | |
| 67 | /** Operate on existing rows. */ |
| 68 | SqlArguments(Uri url, String where, String[] args) { |
| 69 | if (url.getPathSegments().size() == 1) { |
| 70 | this.table = url.getPathSegments().get(0); |
| 71 | this.where = where; |
| 72 | this.args = args; |
| 73 | } else if (url.getPathSegments().size() != 2) { |
| 74 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 75 | } else if (!TextUtils.isEmpty(where)) { |
| 76 | throw new UnsupportedOperationException("WHERE clause not supported: " + url); |
| 77 | } else { |
| 78 | this.table = url.getPathSegments().get(0); |
Doug Zongker | aed8f8e | 2010-01-07 18:07:50 -0800 | [diff] [blame] | 79 | if ("system".equals(this.table) || "secure".equals(this.table)) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 80 | this.where = Settings.NameValueTable.NAME + "=?"; |
| 81 | this.args = new String[] { url.getPathSegments().get(1) }; |
| 82 | } else { |
| 83 | this.where = "_id=" + ContentUris.parseId(url); |
| 84 | this.args = null; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** Insert new rows (no where clause allowed). */ |
| 90 | SqlArguments(Uri url) { |
| 91 | if (url.getPathSegments().size() == 1) { |
| 92 | this.table = url.getPathSegments().get(0); |
| 93 | this.where = null; |
| 94 | this.args = null; |
| 95 | } else { |
| 96 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get the content URI of a row added to a table. |
| 103 | * @param tableUri of the entire table |
| 104 | * @param values found in the row |
| 105 | * @param rowId of the row |
| 106 | * @return the content URI for this particular row |
| 107 | */ |
| 108 | private Uri getUriFor(Uri tableUri, ContentValues values, long rowId) { |
| 109 | if (tableUri.getPathSegments().size() != 1) { |
| 110 | throw new IllegalArgumentException("Invalid URI: " + tableUri); |
| 111 | } |
| 112 | String table = tableUri.getPathSegments().get(0); |
Doug Zongker | aed8f8e | 2010-01-07 18:07:50 -0800 | [diff] [blame] | 113 | if ("system".equals(table) || "secure".equals(table)) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 114 | String name = values.getAsString(Settings.NameValueTable.NAME); |
| 115 | return Uri.withAppendedPath(tableUri, name); |
| 116 | } else { |
| 117 | return ContentUris.withAppendedId(tableUri, rowId); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Send a notification when a particular content URI changes. |
| 123 | * Modify the system property used to communicate the version of |
| 124 | * this table, for tables which have such a property. (The Settings |
| 125 | * contract class uses these to provide client-side caches.) |
| 126 | * @param uri to send notifications for |
| 127 | */ |
| 128 | private void sendNotify(Uri uri) { |
| 129 | // Update the system property *first*, so if someone is listening for |
| 130 | // a notification and then using the contract class to get their data, |
| 131 | // the system property will be updated and they'll get the new data. |
| 132 | |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 133 | boolean backedUpDataChanged = false; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 134 | String property = null, table = uri.getPathSegments().get(0); |
| 135 | if (table.equals("system")) { |
| 136 | property = Settings.System.SYS_PROP_SETTING_VERSION; |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 137 | backedUpDataChanged = true; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 138 | } else if (table.equals("secure")) { |
| 139 | property = Settings.Secure.SYS_PROP_SETTING_VERSION; |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 140 | backedUpDataChanged = true; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | if (property != null) { |
| 144 | long version = SystemProperties.getLong(property, 0) + 1; |
| 145 | if (LOCAL_LOGV) Log.v(TAG, "property: " + property + "=" + version); |
| 146 | SystemProperties.set(property, Long.toString(version)); |
| 147 | } |
| 148 | |
-b master | 501eec9 | 2009-07-06 13:53:11 -0700 | [diff] [blame] | 149 | // Inform the backup manager about a data change |
Amith Yamasani | d158214 | 2009-07-08 20:04:55 -0700 | [diff] [blame] | 150 | if (backedUpDataChanged) { |
| 151 | mBackupManager.dataChanged(); |
| 152 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 153 | // Now send the notification through the content framework. |
| 154 | |
| 155 | String notify = uri.getQueryParameter("notify"); |
| 156 | if (notify == null || "true".equals(notify)) { |
| 157 | getContext().getContentResolver().notifyChange(uri, null); |
| 158 | if (LOCAL_LOGV) Log.v(TAG, "notifying: " + uri); |
| 159 | } else { |
| 160 | if (LOCAL_LOGV) Log.v(TAG, "notification suppressed: " + uri); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Make sure the caller has permission to write this data. |
| 166 | * @param args supplied by the caller |
| 167 | * @throws SecurityException if the caller is forbidden to write. |
| 168 | */ |
| 169 | private void checkWritePermissions(SqlArguments args) { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 170 | if ("secure".equals(args.table) && |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 171 | getContext().checkCallingOrSelfPermission( |
Doug Zongker | aed8f8e | 2010-01-07 18:07:50 -0800 | [diff] [blame] | 172 | android.Manifest.permission.WRITE_SECURE_SETTINGS) != |
| 173 | PackageManager.PERMISSION_GRANTED) { |
Brett Chabot | 16dd82c | 2009-06-18 17:00:48 -0700 | [diff] [blame] | 174 | throw new SecurityException( |
Doug Zongker | aed8f8e | 2010-01-07 18:07:50 -0800 | [diff] [blame] | 175 | String.format("Permission denial: writing to secure settings requires %1$s", |
| 176 | android.Manifest.permission.WRITE_SECURE_SETTINGS)); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public boolean onCreate() { |
| 182 | mOpenHelper = new DatabaseHelper(getContext()); |
Amith Yamasani | 8823c0a8 | 2009-07-07 14:30:17 -0700 | [diff] [blame] | 183 | mBackupManager = new BackupManager(getContext()); |
Fred Quintana | c70239e | 2009-12-17 10:28:33 -0800 | [diff] [blame] | 184 | |
| 185 | if (!ensureAndroidIdIsSet()) { |
| 186 | return false; |
| 187 | } |
| 188 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 189 | return true; |
| 190 | } |
| 191 | |
Fred Quintana | c70239e | 2009-12-17 10:28:33 -0800 | [diff] [blame] | 192 | private boolean ensureAndroidIdIsSet() { |
| 193 | final Cursor c = query(Settings.Secure.CONTENT_URI, |
| 194 | new String[] { Settings.NameValueTable.VALUE }, |
| 195 | Settings.NameValueTable.NAME + "=?", |
| 196 | new String[]{Settings.Secure.ANDROID_ID}, null); |
| 197 | try { |
| 198 | final String value = c.moveToNext() ? c.getString(0) : null; |
| 199 | if (value == null) { |
| 200 | final SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); |
Doug Zongker | 4f8ff39 | 2010-02-03 10:36:40 -0800 | [diff] [blame^] | 201 | String serial = SystemProperties.get("ro.serialno"); |
| 202 | if (serial != null) { |
| 203 | try { |
| 204 | random.setSeed(serial.getBytes("UTF-8")); |
| 205 | } catch (UnsupportedEncodingException ignore) { |
| 206 | // stick with default seed |
| 207 | } |
| 208 | } |
Fred Quintana | c70239e | 2009-12-17 10:28:33 -0800 | [diff] [blame] | 209 | final String newAndroidIdValue = Long.toHexString(random.nextLong()); |
| 210 | Log.d(TAG, "Generated and saved new ANDROID_ID"); |
| 211 | final ContentValues values = new ContentValues(); |
| 212 | values.put(Settings.NameValueTable.NAME, Settings.Secure.ANDROID_ID); |
| 213 | values.put(Settings.NameValueTable.VALUE, newAndroidIdValue); |
| 214 | final Uri uri = insert(Settings.Secure.CONTENT_URI, values); |
| 215 | if (uri == null) { |
| 216 | return false; |
| 217 | } |
| 218 | } |
| 219 | return true; |
| 220 | } catch (NoSuchAlgorithmException e) { |
| 221 | return false; |
| 222 | } finally { |
| 223 | c.close(); |
| 224 | } |
| 225 | } |
| 226 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 227 | @Override |
| 228 | public Cursor query(Uri url, String[] select, String where, String[] whereArgs, String sort) { |
| 229 | SqlArguments args = new SqlArguments(url, where, whereArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | SQLiteDatabase db = mOpenHelper.getReadableDatabase(); |
| 231 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 232 | // The favorites table was moved from this provider to a provider inside Home |
| 233 | // Home still need to query this table to upgrade from pre-cupcake builds |
| 234 | // However, a cupcake+ build with no data does not contain this table which will |
| 235 | // cause an exception in the SQL stack. The following line is a special case to |
| 236 | // 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] | 237 | if (TABLE_FAVORITES.equals(args.table)) { |
| 238 | return null; |
| 239 | } else if (TABLE_OLD_FAVORITES.equals(args.table)) { |
| 240 | args.table = TABLE_FAVORITES; |
| 241 | Cursor cursor = db.rawQuery("PRAGMA table_info(favorites);", null); |
| 242 | if (cursor != null) { |
| 243 | boolean exists = cursor.getCount() > 0; |
| 244 | cursor.close(); |
| 245 | if (!exists) return null; |
| 246 | } else { |
| 247 | return null; |
| 248 | } |
| 249 | } |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 250 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 251 | SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); |
| 252 | qb.setTables(args.table); |
| 253 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 254 | Cursor ret = qb.query(db, select, args.where, args.args, null, null, sort); |
| 255 | ret.setNotificationUri(getContext().getContentResolver(), url); |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public String getType(Uri url) { |
| 261 | // If SqlArguments supplies a where clause, then it must be an item |
| 262 | // (because we aren't supplying our own where clause). |
| 263 | SqlArguments args = new SqlArguments(url, null, null); |
| 264 | if (TextUtils.isEmpty(args.where)) { |
| 265 | return "vnd.android.cursor.dir/" + args.table; |
| 266 | } else { |
| 267 | return "vnd.android.cursor.item/" + args.table; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public int bulkInsert(Uri uri, ContentValues[] values) { |
| 273 | SqlArguments args = new SqlArguments(uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | if (TABLE_FAVORITES.equals(args.table)) { |
| 275 | return 0; |
| 276 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 277 | checkWritePermissions(args); |
| 278 | |
| 279 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 280 | db.beginTransaction(); |
| 281 | try { |
| 282 | int numValues = values.length; |
| 283 | for (int i = 0; i < numValues; i++) { |
| 284 | if (db.insert(args.table, null, values[i]) < 0) return 0; |
| 285 | if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + values[i]); |
| 286 | } |
| 287 | db.setTransactionSuccessful(); |
| 288 | } finally { |
| 289 | db.endTransaction(); |
| 290 | } |
| 291 | |
| 292 | sendNotify(uri); |
| 293 | return values.length; |
| 294 | } |
| 295 | |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 296 | /* |
| 297 | * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED. |
| 298 | * This setting contains a list of the currently enabled location providers. |
| 299 | * But helper functions in android.providers.Settings can enable or disable |
| 300 | * a single provider by using a "+" or "-" prefix before the provider name. |
| 301 | */ |
| 302 | private boolean parseProviderList(Uri url, ContentValues initialValues) { |
| 303 | String value = initialValues.getAsString(Settings.Secure.VALUE); |
| 304 | String newProviders = null; |
| 305 | if (value != null && value.length() > 1) { |
| 306 | char prefix = value.charAt(0); |
| 307 | if (prefix == '+' || prefix == '-') { |
| 308 | // skip prefix |
| 309 | value = value.substring(1); |
| 310 | |
| 311 | // read list of enabled providers into "providers" |
| 312 | String providers = ""; |
| 313 | String[] columns = {Settings.Secure.VALUE}; |
| 314 | String where = Settings.Secure.NAME + "=\'" + Settings.Secure.LOCATION_PROVIDERS_ALLOWED + "\'"; |
| 315 | Cursor cursor = query(url, columns, where, null, null); |
| 316 | if (cursor != null && cursor.getCount() == 1) { |
| 317 | try { |
| 318 | cursor.moveToFirst(); |
| 319 | providers = cursor.getString(0); |
| 320 | } finally { |
| 321 | cursor.close(); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | int index = providers.indexOf(value); |
| 326 | int end = index + value.length(); |
| 327 | // check for commas to avoid matching on partial string |
| 328 | if (index > 0 && providers.charAt(index - 1) != ',') index = -1; |
| 329 | if (end < providers.length() && providers.charAt(end) != ',') index = -1; |
| 330 | |
| 331 | if (prefix == '+' && index < 0) { |
| 332 | // append the provider to the list if not present |
| 333 | if (providers.length() == 0) { |
| 334 | newProviders = value; |
| 335 | } else { |
| 336 | newProviders = providers + ',' + value; |
| 337 | } |
| 338 | } else if (prefix == '-' && index >= 0) { |
| 339 | // remove the provider from the list if present |
| 340 | // remove leading and trailing commas |
| 341 | if (index > 0) index--; |
| 342 | if (end < providers.length()) end++; |
| 343 | |
| 344 | newProviders = providers.substring(0, index); |
| 345 | if (end < providers.length()) { |
| 346 | newProviders += providers.substring(end); |
| 347 | } |
| 348 | } else { |
| 349 | // nothing changed, so no need to update the database |
| 350 | return false; |
| 351 | } |
| 352 | |
| 353 | if (newProviders != null) { |
| 354 | initialValues.put(Settings.Secure.VALUE, newProviders); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | return true; |
| 360 | } |
| 361 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 362 | @Override |
| 363 | public Uri insert(Uri url, ContentValues initialValues) { |
| 364 | SqlArguments args = new SqlArguments(url); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | if (TABLE_FAVORITES.equals(args.table)) { |
| 366 | return null; |
| 367 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 368 | checkWritePermissions(args); |
| 369 | |
Mike Lockwood | bd2a712 | 2009-04-02 23:41:33 -0700 | [diff] [blame] | 370 | // Special case LOCATION_PROVIDERS_ALLOWED. |
| 371 | // Support enabling/disabling a single provider (using "+" or "-" prefix) |
| 372 | String name = initialValues.getAsString(Settings.Secure.NAME); |
| 373 | if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) { |
| 374 | if (!parseProviderList(url, initialValues)) return null; |
| 375 | } |
| 376 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 377 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 378 | final long rowId = db.insert(args.table, null, initialValues); |
| 379 | if (rowId <= 0) return null; |
| 380 | |
| 381 | if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + initialValues); |
| 382 | url = getUriFor(url, initialValues, rowId); |
| 383 | sendNotify(url); |
| 384 | return url; |
| 385 | } |
| 386 | |
| 387 | @Override |
| 388 | public int delete(Uri url, String where, String[] whereArgs) { |
| 389 | SqlArguments args = new SqlArguments(url, where, whereArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | if (TABLE_FAVORITES.equals(args.table)) { |
| 391 | return 0; |
| 392 | } else if (TABLE_OLD_FAVORITES.equals(args.table)) { |
| 393 | args.table = TABLE_FAVORITES; |
| 394 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 395 | checkWritePermissions(args); |
| 396 | |
| 397 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 398 | int count = db.delete(args.table, args.where, args.args); |
| 399 | if (count > 0) sendNotify(url); |
| 400 | if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) deleted"); |
| 401 | return count; |
| 402 | } |
| 403 | |
| 404 | @Override |
| 405 | public int update(Uri url, ContentValues initialValues, String where, String[] whereArgs) { |
| 406 | SqlArguments args = new SqlArguments(url, where, whereArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | if (TABLE_FAVORITES.equals(args.table)) { |
| 408 | return 0; |
| 409 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 410 | checkWritePermissions(args); |
| 411 | |
| 412 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 413 | int count = db.update(args.table, initialValues, args.where, args.args); |
| 414 | if (count > 0) sendNotify(url); |
| 415 | if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) <- " + initialValues); |
| 416 | return count; |
| 417 | } |
| 418 | |
| 419 | @Override |
| 420 | public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { |
| 421 | |
| 422 | /* |
| 423 | * When a client attempts to openFile the default ringtone or |
| 424 | * notification setting Uri, we will proxy the call to the current |
| 425 | * default ringtone's Uri (if it is in the DRM or media provider). |
| 426 | */ |
| 427 | int ringtoneType = RingtoneManager.getDefaultType(uri); |
| 428 | // Above call returns -1 if the Uri doesn't match a default type |
| 429 | if (ringtoneType != -1) { |
| 430 | Context context = getContext(); |
| 431 | |
| 432 | // Get the current value for the default sound |
| 433 | Uri soundUri = RingtoneManager.getActualDefaultRingtoneUri(context, ringtoneType); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 434 | |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 435 | if (soundUri != null) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 436 | // Only proxy the openFile call to drm or media providers |
| 437 | String authority = soundUri.getAuthority(); |
| 438 | boolean isDrmAuthority = authority.equals(DrmStore.AUTHORITY); |
| 439 | if (isDrmAuthority || authority.equals(MediaStore.AUTHORITY)) { |
| 440 | |
| 441 | if (isDrmAuthority) { |
| 442 | try { |
| 443 | // Check DRM access permission here, since once we |
| 444 | // do the below call the DRM will be checking our |
| 445 | // permission, not our caller's permission |
| 446 | DrmStore.enforceAccessDrmPermission(context); |
| 447 | } catch (SecurityException e) { |
| 448 | throw new FileNotFoundException(e.getMessage()); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | return context.getContentResolver().openFileDescriptor(soundUri, mode); |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | return super.openFile(uri, mode); |
| 458 | } |
Marco Nelissen | 69f593c | 2009-07-28 09:55:04 -0700 | [diff] [blame] | 459 | |
| 460 | @Override |
| 461 | public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException { |
| 462 | |
| 463 | /* |
| 464 | * When a client attempts to openFile the default ringtone or |
| 465 | * notification setting Uri, we will proxy the call to the current |
| 466 | * default ringtone's Uri (if it is in the DRM or media provider). |
| 467 | */ |
| 468 | int ringtoneType = RingtoneManager.getDefaultType(uri); |
| 469 | // Above call returns -1 if the Uri doesn't match a default type |
| 470 | if (ringtoneType != -1) { |
| 471 | Context context = getContext(); |
| 472 | |
| 473 | // Get the current value for the default sound |
| 474 | Uri soundUri = RingtoneManager.getActualDefaultRingtoneUri(context, ringtoneType); |
| 475 | |
| 476 | if (soundUri != null) { |
| 477 | // Only proxy the openFile call to drm or media providers |
| 478 | String authority = soundUri.getAuthority(); |
| 479 | boolean isDrmAuthority = authority.equals(DrmStore.AUTHORITY); |
| 480 | if (isDrmAuthority || authority.equals(MediaStore.AUTHORITY)) { |
| 481 | |
| 482 | if (isDrmAuthority) { |
| 483 | try { |
| 484 | // Check DRM access permission here, since once we |
| 485 | // do the below call the DRM will be checking our |
| 486 | // permission, not our caller's permission |
| 487 | DrmStore.enforceAccessDrmPermission(context); |
| 488 | } catch (SecurityException e) { |
| 489 | throw new FileNotFoundException(e.getMessage()); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | ParcelFileDescriptor pfd = null; |
| 494 | try { |
| 495 | pfd = context.getContentResolver().openFileDescriptor(soundUri, mode); |
| 496 | return new AssetFileDescriptor(pfd, 0, -1); |
| 497 | } catch (FileNotFoundException ex) { |
| 498 | // fall through and open the fallback ringtone below |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | try { |
| 503 | return super.openAssetFile(soundUri, mode); |
| 504 | } catch (FileNotFoundException ex) { |
| 505 | // Since a non-null Uri was specified, but couldn't be opened, |
| 506 | // fall back to the built-in ringtone. |
| 507 | return context.getResources().openRawResourceFd( |
| 508 | com.android.internal.R.raw.fallbackring); |
| 509 | } |
| 510 | } |
| 511 | // no need to fall through and have openFile() try again, since we |
| 512 | // already know that will fail. |
| 513 | throw new FileNotFoundException(); // or return null ? |
| 514 | } |
| 515 | |
| 516 | // Note that this will end up calling openFile() above. |
| 517 | return super.openAssetFile(uri, mode); |
| 518 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 519 | } |