blob: 8f2b2cb2a0609edd862f249b0f29344840d2fcd6 [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
21import android.backup.IBackupManager;
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;
27import android.database.Cursor;
28import android.database.sqlite.SQLiteDatabase;
29import android.database.sqlite.SQLiteQueryBuilder;
30import android.media.RingtoneManager;
31import android.net.Uri;
32import android.os.ParcelFileDescriptor;
-b master501eec92009-07-06 13:53:11 -070033import android.os.ServiceManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070034import android.os.SystemProperties;
35import android.provider.DrmStore;
36import android.provider.MediaStore;
37import android.provider.Settings;
38import android.text.TextUtils;
39import android.util.Log;
40
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070041public class SettingsProvider extends ContentProvider {
42 private static final String TAG = "SettingsProvider";
43 private static final boolean LOCAL_LOGV = false;
44
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 private static final String TABLE_FAVORITES = "favorites";
46 private static final String TABLE_OLD_FAVORITES = "old_favorites";
47
James Wylder074da8f2009-02-25 08:38:31 -060048 protected DatabaseHelper mOpenHelper;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070049
50 /**
51 * Decode a content URL into the table, projection, and arguments
52 * used to access the corresponding database rows.
53 */
54 private static class SqlArguments {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 public String table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070056 public final String where;
57 public final String[] args;
58
59 /** Operate on existing rows. */
60 SqlArguments(Uri url, String where, String[] args) {
61 if (url.getPathSegments().size() == 1) {
62 this.table = url.getPathSegments().get(0);
63 this.where = where;
64 this.args = args;
65 } else if (url.getPathSegments().size() != 2) {
66 throw new IllegalArgumentException("Invalid URI: " + url);
67 } else if (!TextUtils.isEmpty(where)) {
68 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
69 } else {
70 this.table = url.getPathSegments().get(0);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080071 if ("gservices".equals(this.table) || "system".equals(this.table)
72 || "secure".equals(this.table)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070073 this.where = Settings.NameValueTable.NAME + "=?";
74 this.args = new String[] { url.getPathSegments().get(1) };
75 } else {
76 this.where = "_id=" + ContentUris.parseId(url);
77 this.args = null;
78 }
79 }
80 }
81
82 /** Insert new rows (no where clause allowed). */
83 SqlArguments(Uri url) {
84 if (url.getPathSegments().size() == 1) {
85 this.table = url.getPathSegments().get(0);
86 this.where = null;
87 this.args = null;
88 } else {
89 throw new IllegalArgumentException("Invalid URI: " + url);
90 }
91 }
92 }
93
94 /**
95 * Get the content URI of a row added to a table.
96 * @param tableUri of the entire table
97 * @param values found in the row
98 * @param rowId of the row
99 * @return the content URI for this particular row
100 */
101 private Uri getUriFor(Uri tableUri, ContentValues values, long rowId) {
102 if (tableUri.getPathSegments().size() != 1) {
103 throw new IllegalArgumentException("Invalid URI: " + tableUri);
104 }
105 String table = tableUri.getPathSegments().get(0);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800106 if ("gservices".equals(table) || "system".equals(table)
107 || "secure".equals(table)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700108 String name = values.getAsString(Settings.NameValueTable.NAME);
109 return Uri.withAppendedPath(tableUri, name);
110 } else {
111 return ContentUris.withAppendedId(tableUri, rowId);
112 }
113 }
114
115 /**
116 * Send a notification when a particular content URI changes.
117 * Modify the system property used to communicate the version of
118 * this table, for tables which have such a property. (The Settings
119 * contract class uses these to provide client-side caches.)
120 * @param uri to send notifications for
121 */
122 private void sendNotify(Uri uri) {
123 // Update the system property *first*, so if someone is listening for
124 // a notification and then using the contract class to get their data,
125 // the system property will be updated and they'll get the new data.
126
127 String property = null, table = uri.getPathSegments().get(0);
128 if (table.equals("system")) {
129 property = Settings.System.SYS_PROP_SETTING_VERSION;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800130 } else if (table.equals("secure")) {
131 property = Settings.Secure.SYS_PROP_SETTING_VERSION;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700132 } else if (table.equals("gservices")) {
133 property = Settings.Gservices.SYS_PROP_SETTING_VERSION;
134 }
135
136 if (property != null) {
137 long version = SystemProperties.getLong(property, 0) + 1;
138 if (LOCAL_LOGV) Log.v(TAG, "property: " + property + "=" + version);
139 SystemProperties.set(property, Long.toString(version));
140 }
141
-b master501eec92009-07-06 13:53:11 -0700142 // Inform the backup manager about a data change
143 IBackupManager ibm = IBackupManager.Stub.asInterface(
144 ServiceManager.getService(Context.BACKUP_SERVICE));
145 if (ibm != null) {
146 try {
147 ibm.dataChanged(getContext().getPackageName());
148 } catch (Exception e) {
149 // Try again later
150 }
151 }
152
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700153 // 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 Projectf013e1a2008-12-17 18:05:43 -0800170 if ("secure".equals(args.table) &&
171 getContext().checkCallingOrSelfPermission(
172 android.Manifest.permission.WRITE_SECURE_SETTINGS) !=
173 PackageManager.PERMISSION_GRANTED) {
Brett Chabot16dd82c2009-06-18 17:00:48 -0700174 throw new SecurityException(
Brett Chabot31a88fa2009-06-18 20:44:42 -0700175 String.format("Permission denial: writing to secure settings requires %1$s",
Brett Chabot16dd82c2009-06-18 17:00:48 -0700176 android.Manifest.permission.WRITE_SECURE_SETTINGS));
177
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700178 // TODO: Move gservices into its own provider so we don't need this nonsense.
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800179 } else if ("gservices".equals(args.table) &&
180 getContext().checkCallingOrSelfPermission(
181 android.Manifest.permission.WRITE_GSERVICES) !=
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700182 PackageManager.PERMISSION_GRANTED) {
Brett Chabot16dd82c2009-06-18 17:00:48 -0700183 throw new SecurityException(
Brett Chabot31a88fa2009-06-18 20:44:42 -0700184 String.format("Permission denial: writing to gservices settings requires %1$s",
Brett Chabot16dd82c2009-06-18 17:00:48 -0700185 android.Manifest.permission.WRITE_GSERVICES));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700186 }
187 }
188
189 @Override
190 public boolean onCreate() {
191 mOpenHelper = new DatabaseHelper(getContext());
192 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);
402 if (soundUri == null) {
403 // Fallback on any valid ringtone Uri
404 soundUri = RingtoneManager.getValidRingtoneUri(context);
405 }
406
407 if (soundUri != null) {
408 // Only proxy the openFile call to drm or media providers
409 String authority = soundUri.getAuthority();
410 boolean isDrmAuthority = authority.equals(DrmStore.AUTHORITY);
411 if (isDrmAuthority || authority.equals(MediaStore.AUTHORITY)) {
412
413 if (isDrmAuthority) {
414 try {
415 // Check DRM access permission here, since once we
416 // do the below call the DRM will be checking our
417 // permission, not our caller's permission
418 DrmStore.enforceAccessDrmPermission(context);
419 } catch (SecurityException e) {
420 throw new FileNotFoundException(e.getMessage());
421 }
422 }
423
424 return context.getContentResolver().openFileDescriptor(soundUri, mode);
425 }
426 }
427 }
428
429 return super.openFile(uri, mode);
430 }
431}