blob: 1451682169660efce8bbebf4f593c65933523896 [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;
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;
Amith Yamasani8823c0a82009-07-07 14:30:17 -070049 private BackupManager mBackupManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070050
51 /**
52 * Decode a content URL into the table, projection, and arguments
53 * used to access the corresponding database rows.
54 */
55 private static class SqlArguments {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 public String table;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070057 public final String where;
58 public final String[] args;
59
60 /** Operate on existing rows. */
61 SqlArguments(Uri url, String where, String[] args) {
62 if (url.getPathSegments().size() == 1) {
63 this.table = url.getPathSegments().get(0);
64 this.where = where;
65 this.args = args;
66 } else if (url.getPathSegments().size() != 2) {
67 throw new IllegalArgumentException("Invalid URI: " + url);
68 } else if (!TextUtils.isEmpty(where)) {
69 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
70 } else {
71 this.table = url.getPathSegments().get(0);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080072 if ("gservices".equals(this.table) || "system".equals(this.table)
73 || "secure".equals(this.table)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070074 this.where = Settings.NameValueTable.NAME + "=?";
75 this.args = new String[] { url.getPathSegments().get(1) };
76 } else {
77 this.where = "_id=" + ContentUris.parseId(url);
78 this.args = null;
79 }
80 }
81 }
82
83 /** Insert new rows (no where clause allowed). */
84 SqlArguments(Uri url) {
85 if (url.getPathSegments().size() == 1) {
86 this.table = url.getPathSegments().get(0);
87 this.where = null;
88 this.args = null;
89 } else {
90 throw new IllegalArgumentException("Invalid URI: " + url);
91 }
92 }
93 }
94
95 /**
96 * Get the content URI of a row added to a table.
97 * @param tableUri of the entire table
98 * @param values found in the row
99 * @param rowId of the row
100 * @return the content URI for this particular row
101 */
102 private Uri getUriFor(Uri tableUri, ContentValues values, long rowId) {
103 if (tableUri.getPathSegments().size() != 1) {
104 throw new IllegalArgumentException("Invalid URI: " + tableUri);
105 }
106 String table = tableUri.getPathSegments().get(0);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800107 if ("gservices".equals(table) || "system".equals(table)
108 || "secure".equals(table)) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700109 String name = values.getAsString(Settings.NameValueTable.NAME);
110 return Uri.withAppendedPath(tableUri, name);
111 } else {
112 return ContentUris.withAppendedId(tableUri, rowId);
113 }
114 }
115
116 /**
117 * Send a notification when a particular content URI changes.
118 * Modify the system property used to communicate the version of
119 * this table, for tables which have such a property. (The Settings
120 * contract class uses these to provide client-side caches.)
121 * @param uri to send notifications for
122 */
123 private void sendNotify(Uri uri) {
124 // Update the system property *first*, so if someone is listening for
125 // a notification and then using the contract class to get their data,
126 // the system property will be updated and they'll get the new data.
127
Amith Yamasanid1582142009-07-08 20:04:55 -0700128 boolean backedUpDataChanged = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700129 String property = null, table = uri.getPathSegments().get(0);
130 if (table.equals("system")) {
131 property = Settings.System.SYS_PROP_SETTING_VERSION;
Amith Yamasanid1582142009-07-08 20:04:55 -0700132 backedUpDataChanged = true;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800133 } else if (table.equals("secure")) {
134 property = Settings.Secure.SYS_PROP_SETTING_VERSION;
Amith Yamasanid1582142009-07-08 20:04:55 -0700135 backedUpDataChanged = true;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700136 } else if (table.equals("gservices")) {
137 property = Settings.Gservices.SYS_PROP_SETTING_VERSION;
138 }
139
140 if (property != null) {
141 long version = SystemProperties.getLong(property, 0) + 1;
142 if (LOCAL_LOGV) Log.v(TAG, "property: " + property + "=" + version);
143 SystemProperties.set(property, Long.toString(version));
144 }
145
-b master501eec92009-07-06 13:53:11 -0700146 // Inform the backup manager about a data change
Amith Yamasanid1582142009-07-08 20:04:55 -0700147 if (backedUpDataChanged) {
148 mBackupManager.dataChanged();
149 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700150 // Now send the notification through the content framework.
151
152 String notify = uri.getQueryParameter("notify");
153 if (notify == null || "true".equals(notify)) {
154 getContext().getContentResolver().notifyChange(uri, null);
155 if (LOCAL_LOGV) Log.v(TAG, "notifying: " + uri);
156 } else {
157 if (LOCAL_LOGV) Log.v(TAG, "notification suppressed: " + uri);
158 }
159 }
160
161 /**
162 * Make sure the caller has permission to write this data.
163 * @param args supplied by the caller
164 * @throws SecurityException if the caller is forbidden to write.
165 */
166 private void checkWritePermissions(SqlArguments args) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800167 if ("secure".equals(args.table) &&
168 getContext().checkCallingOrSelfPermission(
169 android.Manifest.permission.WRITE_SECURE_SETTINGS) !=
170 PackageManager.PERMISSION_GRANTED) {
Brett Chabot16dd82c2009-06-18 17:00:48 -0700171 throw new SecurityException(
Brett Chabot31a88fa2009-06-18 20:44:42 -0700172 String.format("Permission denial: writing to secure settings requires %1$s",
Brett Chabot16dd82c2009-06-18 17:00:48 -0700173 android.Manifest.permission.WRITE_SECURE_SETTINGS));
174
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700175 // TODO: Move gservices into its own provider so we don't need this nonsense.
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800176 } else if ("gservices".equals(args.table) &&
177 getContext().checkCallingOrSelfPermission(
178 android.Manifest.permission.WRITE_GSERVICES) !=
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700179 PackageManager.PERMISSION_GRANTED) {
Brett Chabot16dd82c2009-06-18 17:00:48 -0700180 throw new SecurityException(
Brett Chabot31a88fa2009-06-18 20:44:42 -0700181 String.format("Permission denial: writing to gservices settings requires %1$s",
Brett Chabot16dd82c2009-06-18 17:00:48 -0700182 android.Manifest.permission.WRITE_GSERVICES));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700183 }
184 }
185
186 @Override
187 public boolean onCreate() {
188 mOpenHelper = new DatabaseHelper(getContext());
Amith Yamasani8823c0a82009-07-07 14:30:17 -0700189 mBackupManager = new BackupManager(getContext());
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700190 return true;
191 }
192
193 @Override
194 public Cursor query(Uri url, String[] select, String where, String[] whereArgs, String sort) {
195 SqlArguments args = new SqlArguments(url, where, whereArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
197
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800198 // The favorites table was moved from this provider to a provider inside Home
199 // Home still need to query this table to upgrade from pre-cupcake builds
200 // However, a cupcake+ build with no data does not contain this table which will
201 // cause an exception in the SQL stack. The following line is a special case to
202 // 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 -0800203 if (TABLE_FAVORITES.equals(args.table)) {
204 return null;
205 } else if (TABLE_OLD_FAVORITES.equals(args.table)) {
206 args.table = TABLE_FAVORITES;
207 Cursor cursor = db.rawQuery("PRAGMA table_info(favorites);", null);
208 if (cursor != null) {
209 boolean exists = cursor.getCount() > 0;
210 cursor.close();
211 if (!exists) return null;
212 } else {
213 return null;
214 }
215 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800216
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700217 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
218 qb.setTables(args.table);
219
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700220 Cursor ret = qb.query(db, select, args.where, args.args, null, null, sort);
221 ret.setNotificationUri(getContext().getContentResolver(), url);
222 return ret;
223 }
224
225 @Override
226 public String getType(Uri url) {
227 // If SqlArguments supplies a where clause, then it must be an item
228 // (because we aren't supplying our own where clause).
229 SqlArguments args = new SqlArguments(url, null, null);
230 if (TextUtils.isEmpty(args.where)) {
231 return "vnd.android.cursor.dir/" + args.table;
232 } else {
233 return "vnd.android.cursor.item/" + args.table;
234 }
235 }
236
237 @Override
238 public int bulkInsert(Uri uri, ContentValues[] values) {
239 SqlArguments args = new SqlArguments(uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 if (TABLE_FAVORITES.equals(args.table)) {
241 return 0;
242 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700243 checkWritePermissions(args);
244
245 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
246 db.beginTransaction();
247 try {
248 int numValues = values.length;
249 for (int i = 0; i < numValues; i++) {
250 if (db.insert(args.table, null, values[i]) < 0) return 0;
251 if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + values[i]);
252 }
253 db.setTransactionSuccessful();
254 } finally {
255 db.endTransaction();
256 }
257
258 sendNotify(uri);
259 return values.length;
260 }
261
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700262 /*
263 * Used to parse changes to the value of Settings.Secure.LOCATION_PROVIDERS_ALLOWED.
264 * This setting contains a list of the currently enabled location providers.
265 * But helper functions in android.providers.Settings can enable or disable
266 * a single provider by using a "+" or "-" prefix before the provider name.
267 */
268 private boolean parseProviderList(Uri url, ContentValues initialValues) {
269 String value = initialValues.getAsString(Settings.Secure.VALUE);
270 String newProviders = null;
271 if (value != null && value.length() > 1) {
272 char prefix = value.charAt(0);
273 if (prefix == '+' || prefix == '-') {
274 // skip prefix
275 value = value.substring(1);
276
277 // read list of enabled providers into "providers"
278 String providers = "";
279 String[] columns = {Settings.Secure.VALUE};
280 String where = Settings.Secure.NAME + "=\'" + Settings.Secure.LOCATION_PROVIDERS_ALLOWED + "\'";
281 Cursor cursor = query(url, columns, where, null, null);
282 if (cursor != null && cursor.getCount() == 1) {
283 try {
284 cursor.moveToFirst();
285 providers = cursor.getString(0);
286 } finally {
287 cursor.close();
288 }
289 }
290
291 int index = providers.indexOf(value);
292 int end = index + value.length();
293 // check for commas to avoid matching on partial string
294 if (index > 0 && providers.charAt(index - 1) != ',') index = -1;
295 if (end < providers.length() && providers.charAt(end) != ',') index = -1;
296
297 if (prefix == '+' && index < 0) {
298 // append the provider to the list if not present
299 if (providers.length() == 0) {
300 newProviders = value;
301 } else {
302 newProviders = providers + ',' + value;
303 }
304 } else if (prefix == '-' && index >= 0) {
305 // remove the provider from the list if present
306 // remove leading and trailing commas
307 if (index > 0) index--;
308 if (end < providers.length()) end++;
309
310 newProviders = providers.substring(0, index);
311 if (end < providers.length()) {
312 newProviders += providers.substring(end);
313 }
314 } else {
315 // nothing changed, so no need to update the database
316 return false;
317 }
318
319 if (newProviders != null) {
320 initialValues.put(Settings.Secure.VALUE, newProviders);
321 }
322 }
323 }
324
325 return true;
326 }
327
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700328 @Override
329 public Uri insert(Uri url, ContentValues initialValues) {
330 SqlArguments args = new SqlArguments(url);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 if (TABLE_FAVORITES.equals(args.table)) {
332 return null;
333 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700334 checkWritePermissions(args);
335
Mike Lockwoodbd2a7122009-04-02 23:41:33 -0700336 // Special case LOCATION_PROVIDERS_ALLOWED.
337 // Support enabling/disabling a single provider (using "+" or "-" prefix)
338 String name = initialValues.getAsString(Settings.Secure.NAME);
339 if (Settings.Secure.LOCATION_PROVIDERS_ALLOWED.equals(name)) {
340 if (!parseProviderList(url, initialValues)) return null;
341 }
342
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700343 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
344 final long rowId = db.insert(args.table, null, initialValues);
345 if (rowId <= 0) return null;
346
347 if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + initialValues);
348 url = getUriFor(url, initialValues, rowId);
349 sendNotify(url);
350 return url;
351 }
352
353 @Override
354 public int delete(Uri url, String where, String[] whereArgs) {
355 SqlArguments args = new SqlArguments(url, where, whereArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 if (TABLE_FAVORITES.equals(args.table)) {
357 return 0;
358 } else if (TABLE_OLD_FAVORITES.equals(args.table)) {
359 args.table = TABLE_FAVORITES;
360 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700361 checkWritePermissions(args);
362
363 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
364 int count = db.delete(args.table, args.where, args.args);
365 if (count > 0) sendNotify(url);
366 if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) deleted");
367 return count;
368 }
369
370 @Override
371 public int update(Uri url, ContentValues initialValues, String where, String[] whereArgs) {
372 SqlArguments args = new SqlArguments(url, where, whereArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 if (TABLE_FAVORITES.equals(args.table)) {
374 return 0;
375 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700376 checkWritePermissions(args);
377
378 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
379 int count = db.update(args.table, initialValues, args.where, args.args);
380 if (count > 0) sendNotify(url);
381 if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) <- " + initialValues);
382 return count;
383 }
384
385 @Override
386 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
387
388 /*
389 * When a client attempts to openFile the default ringtone or
390 * notification setting Uri, we will proxy the call to the current
391 * default ringtone's Uri (if it is in the DRM or media provider).
392 */
393 int ringtoneType = RingtoneManager.getDefaultType(uri);
394 // Above call returns -1 if the Uri doesn't match a default type
395 if (ringtoneType != -1) {
396 Context context = getContext();
397
398 // Get the current value for the default sound
399 Uri soundUri = RingtoneManager.getActualDefaultRingtoneUri(context, ringtoneType);
400 if (soundUri == null) {
401 // Fallback on any valid ringtone Uri
402 soundUri = RingtoneManager.getValidRingtoneUri(context);
403 }
404
405 if (soundUri != null) {
406 // Only proxy the openFile call to drm or media providers
407 String authority = soundUri.getAuthority();
408 boolean isDrmAuthority = authority.equals(DrmStore.AUTHORITY);
409 if (isDrmAuthority || authority.equals(MediaStore.AUTHORITY)) {
410
411 if (isDrmAuthority) {
412 try {
413 // Check DRM access permission here, since once we
414 // do the below call the DRM will be checking our
415 // permission, not our caller's permission
416 DrmStore.enforceAccessDrmPermission(context);
417 } catch (SecurityException e) {
418 throw new FileNotFoundException(e.getMessage());
419 }
420 }
421
422 return context.getContentResolver().openFileDescriptor(soundUri, mode);
423 }
424 }
425 }
426
427 return super.openFile(uri, mode);
428 }
429}