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