blob: 9877342a490aaa64dbcb595122dec33a751fcb4d [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.providers.settings;
18
-b master501eec92009-07-06 13:53:11 -070019import java.io.FileNotFoundException;
20
Amith Yamasani8823c0a82009-07-07 14:30:17 -070021import android.backup.BackupManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070022import android.content.ContentProvider;
23import android.content.ContentUris;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.pm.PackageManager;
Marco Nelissen69f593c2009-07-28 09:55:04 -070027import android.content.res.AssetFileDescriptor;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070028import android.database.Cursor;
29import android.database.sqlite.SQLiteDatabase;
30import android.database.sqlite.SQLiteQueryBuilder;
Marco Nelissen69f593c2009-07-28 09:55:04 -070031import android.media.Ringtone;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070032import android.media.RingtoneManager;
33import android.net.Uri;
34import android.os.ParcelFileDescriptor;
-b master501eec92009-07-06 13:53:11 -070035import android.os.ServiceManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070036import android.os.SystemProperties;
37import android.provider.DrmStore;
38import android.provider.MediaStore;
39import android.provider.Settings;
40import android.text.TextUtils;
41import android.util.Log;
42
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070043public class SettingsProvider extends ContentProvider {
44 private static final String TAG = "SettingsProvider";
45 private static final boolean LOCAL_LOGV = false;
46
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 private static final String TABLE_FAVORITES = "favorites";
48 private static final String TABLE_OLD_FAVORITES = "old_favorites";
49
James Wylder074da8f2009-02-25 08:38:31 -060050 protected DatabaseHelper mOpenHelper;
Amith Yamasani8823c0a82009-07-07 14:30:17 -070051 private BackupManager mBackupManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070052
53 /**
54 * Decode a content URL into the table, projection, and arguments
55 * used to access the corresponding database rows.
56 */
57 private static class SqlArguments {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 public String table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070059 public final String where;
60 public final String[] args;
61
62 /** Operate on existing rows. */
63 SqlArguments(Uri url, String where, String[] args) {
64 if (url.getPathSegments().size() == 1) {
65 this.table = url.getPathSegments().get(0);
66 this.where = where;
67 this.args = args;
68 } else if (url.getPathSegments().size() != 2) {
69 throw new IllegalArgumentException("Invalid URI: " + url);
70 } else if (!TextUtils.isEmpty(where)) {
71 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
72 } else {
73 this.table = url.getPathSegments().get(0);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080074 if ("gservices".equals(this.table) || "system".equals(this.table)
75 || "secure".equals(this.table)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070076 this.where = Settings.NameValueTable.NAME + "=?";
77 this.args = new String[] { url.getPathSegments().get(1) };
78 } else {
79 this.where = "_id=" + ContentUris.parseId(url);
80 this.args = null;
81 }
82 }
83 }
84
85 /** Insert new rows (no where clause allowed). */
86 SqlArguments(Uri url) {
87 if (url.getPathSegments().size() == 1) {
88 this.table = url.getPathSegments().get(0);
89 this.where = null;
90 this.args = null;
91 } else {
92 throw new IllegalArgumentException("Invalid URI: " + url);
93 }
94 }
95 }
96
97 /**
98 * Get the content URI of a row added to a table.
99 * @param tableUri of the entire table
100 * @param values found in the row
101 * @param rowId of the row
102 * @return the content URI for this particular row
103 */
104 private Uri getUriFor(Uri tableUri, ContentValues values, long rowId) {
105 if (tableUri.getPathSegments().size() != 1) {
106 throw new IllegalArgumentException("Invalid URI: " + tableUri);
107 }
108 String table = tableUri.getPathSegments().get(0);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800109 if ("gservices".equals(table) || "system".equals(table)
110 || "secure".equals(table)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700111 String name = values.getAsString(Settings.NameValueTable.NAME);
112 return Uri.withAppendedPath(tableUri, name);
113 } else {
114 return ContentUris.withAppendedId(tableUri, rowId);
115 }
116 }
117
118 /**
119 * Send a notification when a particular content URI changes.
120 * Modify the system property used to communicate the version of
121 * this table, for tables which have such a property. (The Settings
122 * contract class uses these to provide client-side caches.)
123 * @param uri to send notifications for
124 */
125 private void sendNotify(Uri uri) {
126 // Update the system property *first*, so if someone is listening for
127 // a notification and then using the contract class to get their data,
128 // the system property will be updated and they'll get the new data.
129
Amith Yamasanid1582142009-07-08 20:04:55 -0700130 boolean backedUpDataChanged = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700131 String property = null, table = uri.getPathSegments().get(0);
132 if (table.equals("system")) {
133 property = Settings.System.SYS_PROP_SETTING_VERSION;
Amith Yamasanid1582142009-07-08 20:04:55 -0700134 backedUpDataChanged = true;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800135 } else if (table.equals("secure")) {
136 property = Settings.Secure.SYS_PROP_SETTING_VERSION;
Amith Yamasanid1582142009-07-08 20:04:55 -0700137 backedUpDataChanged = true;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700138 } else if (table.equals("gservices")) {
139 property = Settings.Gservices.SYS_PROP_SETTING_VERSION;
140 }
141
142 if (property != null) {
143 long version = SystemProperties.getLong(property, 0) + 1;
144 if (LOCAL_LOGV) Log.v(TAG, "property: " + property + "=" + version);
145 SystemProperties.set(property, Long.toString(version));
146 }
147
-b master501eec92009-07-06 13:53:11 -0700148 // Inform the backup manager about a data change
Amith Yamasanid1582142009-07-08 20:04:55 -0700149 if (backedUpDataChanged) {
150 mBackupManager.dataChanged();
151 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700152 // Now send the notification through the content framework.
153
154 String notify = uri.getQueryParameter("notify");
155 if (notify == null || "true".equals(notify)) {
156 getContext().getContentResolver().notifyChange(uri, null);
157 if (LOCAL_LOGV) Log.v(TAG, "notifying: " + uri);
158 } else {
159 if (LOCAL_LOGV) Log.v(TAG, "notification suppressed: " + uri);
160 }
161 }
162
163 /**
164 * Make sure the caller has permission to write this data.
165 * @param args supplied by the caller
166 * @throws SecurityException if the caller is forbidden to write.
167 */
168 private void checkWritePermissions(SqlArguments args) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800169 if ("secure".equals(args.table) &&
170 getContext().checkCallingOrSelfPermission(
171 android.Manifest.permission.WRITE_SECURE_SETTINGS) !=
172 PackageManager.PERMISSION_GRANTED) {
Brett Chabot16dd82c2009-06-18 17:00:48 -0700173 throw new SecurityException(
Brett Chabot31a88fa2009-06-18 20:44:42 -0700174 String.format("Permission denial: writing to secure settings requires %1$s",
Brett Chabot16dd82c2009-06-18 17:00:48 -0700175 android.Manifest.permission.WRITE_SECURE_SETTINGS));
176
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700177 // TODO: Move gservices into its own provider so we don't need this nonsense.
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800178 } else if ("gservices".equals(args.table) &&
179 getContext().checkCallingOrSelfPermission(
180 android.Manifest.permission.WRITE_GSERVICES) !=
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700181 PackageManager.PERMISSION_GRANTED) {
Brett Chabot16dd82c2009-06-18 17:00:48 -0700182 throw new SecurityException(
Brett Chabot31a88fa2009-06-18 20:44:42 -0700183 String.format("Permission denial: writing to gservices settings requires %1$s",
Brett Chabot16dd82c2009-06-18 17:00:48 -0700184 android.Manifest.permission.WRITE_GSERVICES));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700185 }
186 }
187
188 @Override
189 public boolean onCreate() {
190 mOpenHelper = new DatabaseHelper(getContext());
Amith Yamasani8823c0a82009-07-07 14:30:17 -0700191 mBackupManager = new BackupManager(getContext());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700192 return true;
193 }
194
195 @Override
196 public Cursor query(Uri url, String[] select, String where, String[] whereArgs, String sort) {
197 SqlArguments args = new SqlArguments(url, where, whereArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
199
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800200 // The favorites table was moved from this provider to a provider inside Home
201 // Home still need to query this table to upgrade from pre-cupcake builds
202 // However, a cupcake+ build with no data does not contain this table which will
203 // cause an exception in the SQL stack. The following line is a special case to
204 // let the caller of the query have a chance to recover and avoid the exception
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 if (TABLE_FAVORITES.equals(args.table)) {
206 return null;
207 } else if (TABLE_OLD_FAVORITES.equals(args.table)) {
208 args.table = TABLE_FAVORITES;
209 Cursor cursor = db.rawQuery("PRAGMA table_info(favorites);", null);
210 if (cursor != null) {
211 boolean exists = cursor.getCount() > 0;
212 cursor.close();
213 if (!exists) return null;
214 } else {
215 return null;
216 }
217 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800218
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700219 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
220 qb.setTables(args.table);
221
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700222 Cursor ret = qb.query(db, select, args.where, args.args, null, null, sort);
223 ret.setNotificationUri(getContext().getContentResolver(), url);
224 return ret;
225 }
226
227 @Override
228 public String getType(Uri url) {
229 // If SqlArguments supplies a where clause, then it must be an item
230 // (because we aren't supplying our own where clause).
231 SqlArguments args = new SqlArguments(url, null, null);
232 if (TextUtils.isEmpty(args.where)) {
233 return "vnd.android.cursor.dir/" + args.table;
234 } else {
235 return "vnd.android.cursor.item/" + args.table;
236 }
237 }
238
239 @Override
240 public int bulkInsert(Uri uri, ContentValues[] values) {
241 SqlArguments args = new SqlArguments(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 if (TABLE_FAVORITES.equals(args.table)) {
243 return 0;
244 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700245 checkWritePermissions(args);
246
247 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
248 db.beginTransaction();
249 try {
250 int numValues = values.length;
251 for (int i = 0; i < numValues; i++) {
252 if (db.insert(args.table, null, values[i]) < 0) return 0;
253 if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + values[i]);
254 }
255 db.setTransactionSuccessful();
256 } finally {
257 db.endTransaction();
258 }
259
260 sendNotify(uri);
261 return values.length;
262 }
263
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700264 /*
265 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
266 * This setting contains a list of the currently enabled location providers.
267 * But helper functions in android.providers.Settings can enable or disable
268 * a single provider by using a "+" or "-" prefix before the provider name.
269 */
270 private boolean parseProviderList(Uri url, ContentValues initialValues) {
271 String value = initialValues.getAsString(Settings.Secure.VALUE);
272 String newProviders = null;
273 if (value != null && value.length() > 1) {
274 char prefix = value.charAt(0);
275 if (prefix == '+' || prefix == '-') {
276 // skip prefix
277 value = value.substring(1);
278
279 // read list of enabled providers into "providers"
280 String providers = "";
281 String[] columns = {Settings.Secure.VALUE};
282 String where = Settings.Secure.NAME + "=\'" + Settings.Secure.LOCATION_PROVIDERS_ALLOWED + "\'";
283 Cursor cursor = query(url, columns, where, null, null);
284 if (cursor != null && cursor.getCount() == 1) {
285 try {
286 cursor.moveToFirst();
287 providers = cursor.getString(0);
288 } finally {
289 cursor.close();
290 }
291 }
292
293 int index = providers.indexOf(value);
294 int end = index + value.length();
295 // check for commas to avoid matching on partial string
296 if (index > 0 && providers.charAt(index - 1) != ',') index = -1;
297 if (end < providers.length() && providers.charAt(end) != ',') index = -1;
298
299 if (prefix == '+' && index < 0) {
300 // append the provider to the list if not present
301 if (providers.length() == 0) {
302 newProviders = value;
303 } else {
304 newProviders = providers + ',' + value;
305 }
306 } else if (prefix == '-' && index >= 0) {
307 // remove the provider from the list if present
308 // remove leading and trailing commas
309 if (index > 0) index--;
310 if (end < providers.length()) end++;
311
312 newProviders = providers.substring(0, index);
313 if (end < providers.length()) {
314 newProviders += providers.substring(end);
315 }
316 } else {
317 // nothing changed, so no need to update the database
318 return false;
319 }
320
321 if (newProviders != null) {
322 initialValues.put(Settings.Secure.VALUE, newProviders);
323 }
324 }
325 }
326
327 return true;
328 }
329
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700330 @Override
331 public Uri insert(Uri url, ContentValues initialValues) {
332 SqlArguments args = new SqlArguments(url);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 if (TABLE_FAVORITES.equals(args.table)) {
334 return null;
335 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700336 checkWritePermissions(args);
337
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700338 // Special case LOCATION_PROVIDERS_ALLOWED.
339 // Support enabling/disabling a single provider (using "+" or "-" prefix)
340 String name = initialValues.getAsString(Settings.Secure.NAME);
341 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
342 if (!parseProviderList(url, initialValues)) return null;
343 }
344
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700345 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
346 final long rowId = db.insert(args.table, null, initialValues);
347 if (rowId <= 0) return null;
348
349 if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + initialValues);
350 url = getUriFor(url, initialValues, rowId);
351 sendNotify(url);
352 return url;
353 }
354
355 @Override
356 public int delete(Uri url, String where, String[] whereArgs) {
357 SqlArguments args = new SqlArguments(url, where, whereArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 if (TABLE_FAVORITES.equals(args.table)) {
359 return 0;
360 } else if (TABLE_OLD_FAVORITES.equals(args.table)) {
361 args.table = TABLE_FAVORITES;
362 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700363 checkWritePermissions(args);
364
365 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
366 int count = db.delete(args.table, args.where, args.args);
367 if (count > 0) sendNotify(url);
368 if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) deleted");
369 return count;
370 }
371
372 @Override
373 public int update(Uri url, ContentValues initialValues, String where, String[] whereArgs) {
374 SqlArguments args = new SqlArguments(url, where, whereArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 if (TABLE_FAVORITES.equals(args.table)) {
376 return 0;
377 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700378 checkWritePermissions(args);
379
380 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
381 int count = db.update(args.table, initialValues, args.where, args.args);
382 if (count > 0) sendNotify(url);
383 if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) <- " + initialValues);
384 return count;
385 }
386
387 @Override
388 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
389
390 /*
391 * When a client attempts to openFile the default ringtone or
392 * notification setting Uri, we will proxy the call to the current
393 * default ringtone's Uri (if it is in the DRM or media provider).
394 */
395 int ringtoneType = RingtoneManager.getDefaultType(uri);
396 // Above call returns -1 if the Uri doesn't match a default type
397 if (ringtoneType != -1) {
398 Context context = getContext();
399
400 // Get the current value for the default sound
401 Uri soundUri = RingtoneManager.getActualDefaultRingtoneUri(context, ringtoneType);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700402
Marco Nelissen69f593c2009-07-28 09:55:04 -0700403 if (soundUri != null) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700404 // Only proxy the openFile call to drm or media providers
405 String authority = soundUri.getAuthority();
406 boolean isDrmAuthority = authority.equals(DrmStore.AUTHORITY);
407 if (isDrmAuthority || authority.equals(MediaStore.AUTHORITY)) {
408
409 if (isDrmAuthority) {
410 try {
411 // Check DRM access permission here, since once we
412 // do the below call the DRM will be checking our
413 // permission, not our caller's permission
414 DrmStore.enforceAccessDrmPermission(context);
415 } catch (SecurityException e) {
416 throw new FileNotFoundException(e.getMessage());
417 }
418 }
419
420 return context.getContentResolver().openFileDescriptor(soundUri, mode);
421 }
422 }
423 }
424
425 return super.openFile(uri, mode);
426 }
Marco Nelissen69f593c2009-07-28 09:55:04 -0700427
428 @Override
429 public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
430
431 /*
432 * When a client attempts to openFile the default ringtone or
433 * notification setting Uri, we will proxy the call to the current
434 * default ringtone's Uri (if it is in the DRM or media provider).
435 */
436 int ringtoneType = RingtoneManager.getDefaultType(uri);
437 // Above call returns -1 if the Uri doesn't match a default type
438 if (ringtoneType != -1) {
439 Context context = getContext();
440
441 // Get the current value for the default sound
442 Uri soundUri = RingtoneManager.getActualDefaultRingtoneUri(context, ringtoneType);
443
444 if (soundUri != null) {
445 // Only proxy the openFile call to drm or media providers
446 String authority = soundUri.getAuthority();
447 boolean isDrmAuthority = authority.equals(DrmStore.AUTHORITY);
448 if (isDrmAuthority || authority.equals(MediaStore.AUTHORITY)) {
449
450 if (isDrmAuthority) {
451 try {
452 // Check DRM access permission here, since once we
453 // do the below call the DRM will be checking our
454 // permission, not our caller's permission
455 DrmStore.enforceAccessDrmPermission(context);
456 } catch (SecurityException e) {
457 throw new FileNotFoundException(e.getMessage());
458 }
459 }
460
461 ParcelFileDescriptor pfd = null;
462 try {
463 pfd = context.getContentResolver().openFileDescriptor(soundUri, mode);
464 return new AssetFileDescriptor(pfd, 0, -1);
465 } catch (FileNotFoundException ex) {
466 // fall through and open the fallback ringtone below
467 }
468 }
469
470 try {
471 return super.openAssetFile(soundUri, mode);
472 } catch (FileNotFoundException ex) {
473 // Since a non-null Uri was specified, but couldn't be opened,
474 // fall back to the built-in ringtone.
475 return context.getResources().openRawResourceFd(
476 com.android.internal.R.raw.fallbackring);
477 }
478 }
479 // no need to fall through and have openFile() try again, since we
480 // already know that will fail.
481 throw new FileNotFoundException(); // or return null ?
482 }
483
484 // Note that this will end up calling openFile() above.
485 return super.openAssetFile(uri, mode);
486 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700487}