blob: 333a450e0a80e2b68f1cc319114050a3a1770739 [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
19import android.content.ContentProvider;
20import android.content.ContentUris;
21import android.content.ContentValues;
22import android.content.Context;
23import android.content.pm.PackageManager;
24import android.database.Cursor;
25import android.database.sqlite.SQLiteDatabase;
26import android.database.sqlite.SQLiteQueryBuilder;
27import android.media.RingtoneManager;
28import android.net.Uri;
29import android.os.ParcelFileDescriptor;
30import android.os.SystemProperties;
31import android.provider.DrmStore;
32import android.provider.MediaStore;
33import android.provider.Settings;
34import android.text.TextUtils;
35import android.util.Log;
36
37import java.io.FileNotFoundException;
38
39public class SettingsProvider extends ContentProvider {
40 private static final String TAG = "SettingsProvider";
41 private static final boolean LOCAL_LOGV = false;
42
43 private static final String TABLE_FAVORITES = "favorites";
44 private static final String TABLE_OLD_FAVORITES = "old_favorites";
45
46 private DatabaseHelper mOpenHelper;
47
48 /**
49 * Decode a content URL into the table, projection, and arguments
50 * used to access the corresponding database rows.
51 */
52 private static class SqlArguments {
53 public String table;
54 public final String where;
55 public final String[] args;
56
57 /** Operate on existing rows. */
58 SqlArguments(Uri url, String where, String[] args) {
59 if (url.getPathSegments().size() == 1) {
60 this.table = url.getPathSegments().get(0);
61 this.where = where;
62 this.args = args;
63 } else if (url.getPathSegments().size() != 2) {
64 throw new IllegalArgumentException("Invalid URI: " + url);
65 } else if (!TextUtils.isEmpty(where)) {
66 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
67 } else {
68 this.table = url.getPathSegments().get(0);
69 if ("gservices".equals(this.table) || "system".equals(this.table)
70 || "secure".equals(this.table)) {
71 this.where = Settings.NameValueTable.NAME + "=?";
72 this.args = new String[] { url.getPathSegments().get(1) };
73 } else {
74 this.where = "_id=" + ContentUris.parseId(url);
75 this.args = null;
76 }
77 }
78 }
79
80 /** Insert new rows (no where clause allowed). */
81 SqlArguments(Uri url) {
82 if (url.getPathSegments().size() == 1) {
83 this.table = url.getPathSegments().get(0);
84 this.where = null;
85 this.args = null;
86 } else {
87 throw new IllegalArgumentException("Invalid URI: " + url);
88 }
89 }
90 }
91
92 /**
93 * Get the content URI of a row added to a table.
94 * @param tableUri of the entire table
95 * @param values found in the row
96 * @param rowId of the row
97 * @return the content URI for this particular row
98 */
99 private Uri getUriFor(Uri tableUri, ContentValues values, long rowId) {
100 if (tableUri.getPathSegments().size() != 1) {
101 throw new IllegalArgumentException("Invalid URI: " + tableUri);
102 }
103 String table = tableUri.getPathSegments().get(0);
104 if ("gservices".equals(table) || "system".equals(table)
105 || "secure".equals(table)) {
106 String name = values.getAsString(Settings.NameValueTable.NAME);
107 return Uri.withAppendedPath(tableUri, name);
108 } else {
109 return ContentUris.withAppendedId(tableUri, rowId);
110 }
111 }
112
113 /**
114 * Send a notification when a particular content URI changes.
115 * Modify the system property used to communicate the version of
116 * this table, for tables which have such a property. (The Settings
117 * contract class uses these to provide client-side caches.)
118 * @param uri to send notifications for
119 */
120 private void sendNotify(Uri uri) {
121 // Update the system property *first*, so if someone is listening for
122 // a notification and then using the contract class to get their data,
123 // the system property will be updated and they'll get the new data.
124
125 String property = null, table = uri.getPathSegments().get(0);
126 if (table.equals("system")) {
127 property = Settings.System.SYS_PROP_SETTING_VERSION;
128 } else if (table.equals("secure")) {
129 property = Settings.Secure.SYS_PROP_SETTING_VERSION;
130 } else if (table.equals("gservices")) {
131 property = Settings.Gservices.SYS_PROP_SETTING_VERSION;
132 }
133
134 if (property != null) {
135 long version = SystemProperties.getLong(property, 0) + 1;
136 if (LOCAL_LOGV) Log.v(TAG, "property: " + property + "=" + version);
137 SystemProperties.set(property, Long.toString(version));
138 }
139
140 // Now send the notification through the content framework.
141
142 String notify = uri.getQueryParameter("notify");
143 if (notify == null || "true".equals(notify)) {
144 getContext().getContentResolver().notifyChange(uri, null);
145 if (LOCAL_LOGV) Log.v(TAG, "notifying: " + uri);
146 } else {
147 if (LOCAL_LOGV) Log.v(TAG, "notification suppressed: " + uri);
148 }
149 }
150
151 /**
152 * Make sure the caller has permission to write this data.
153 * @param args supplied by the caller
154 * @throws SecurityException if the caller is forbidden to write.
155 */
156 private void checkWritePermissions(SqlArguments args) {
157 if ("secure".equals(args.table) &&
158 getContext().checkCallingOrSelfPermission(
159 android.Manifest.permission.WRITE_SECURE_SETTINGS) !=
160 PackageManager.PERMISSION_GRANTED) {
161 throw new SecurityException("Cannot write secure settings table");
162
163 // TODO: Move gservices into its own provider so we don't need this nonsense.
164 } else if ("gservices".equals(args.table) &&
165 getContext().checkCallingOrSelfPermission(
166 android.Manifest.permission.WRITE_GSERVICES) !=
167 PackageManager.PERMISSION_GRANTED) {
168 throw new SecurityException("Cannot write gservices table");
169 }
170 }
171
172 @Override
173 public boolean onCreate() {
174 mOpenHelper = new DatabaseHelper(getContext());
175 return true;
176 }
177
178 @Override
179 public Cursor query(Uri url, String[] select, String where, String[] whereArgs, String sort) {
180 SqlArguments args = new SqlArguments(url, where, whereArgs);
181 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
182
183 // The favorites table was moved from this provider to a provider inside Home
184 // Home still need to query this table to upgrade from pre-cupcake builds
185 // However, a cupcake+ build with no data does not contain this table which will
186 // cause an exception in the SQL stack. The following line is a special case to
187 // let the caller of the query have a chance to recover and avoid the exception
188 if (TABLE_FAVORITES.equals(args.table)) {
189 return null;
190 } else if (TABLE_OLD_FAVORITES.equals(args.table)) {
191 args.table = TABLE_FAVORITES;
192 Cursor cursor = db.rawQuery("PRAGMA table_info(favorites);", null);
193 if (cursor != null) {
194 boolean exists = cursor.getCount() > 0;
195 cursor.close();
196 if (!exists) return null;
197 } else {
198 return null;
199 }
200 }
201
202 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
203 qb.setTables(args.table);
204
205 Cursor ret = qb.query(db, select, args.where, args.args, null, null, sort);
206 ret.setNotificationUri(getContext().getContentResolver(), url);
207 return ret;
208 }
209
210 @Override
211 public String getType(Uri url) {
212 // If SqlArguments supplies a where clause, then it must be an item
213 // (because we aren't supplying our own where clause).
214 SqlArguments args = new SqlArguments(url, null, null);
215 if (TextUtils.isEmpty(args.where)) {
216 return "vnd.android.cursor.dir/" + args.table;
217 } else {
218 return "vnd.android.cursor.item/" + args.table;
219 }
220 }
221
222 @Override
223 public int bulkInsert(Uri uri, ContentValues[] values) {
224 SqlArguments args = new SqlArguments(uri);
225 if (TABLE_FAVORITES.equals(args.table)) {
226 return 0;
227 }
228 checkWritePermissions(args);
229
230 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
231 db.beginTransaction();
232 try {
233 int numValues = values.length;
234 for (int i = 0; i < numValues; i++) {
235 if (db.insert(args.table, null, values[i]) < 0) return 0;
236 if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + values[i]);
237 }
238 db.setTransactionSuccessful();
239 } finally {
240 db.endTransaction();
241 }
242
243 sendNotify(uri);
244 return values.length;
245 }
246
247 @Override
248 public Uri insert(Uri url, ContentValues initialValues) {
249 SqlArguments args = new SqlArguments(url);
250 if (TABLE_FAVORITES.equals(args.table)) {
251 return null;
252 }
253 checkWritePermissions(args);
254
255 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
256 final long rowId = db.insert(args.table, null, initialValues);
257 if (rowId <= 0) return null;
258
259 if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + initialValues);
260 url = getUriFor(url, initialValues, rowId);
261 sendNotify(url);
262 return url;
263 }
264
265 @Override
266 public int delete(Uri url, String where, String[] whereArgs) {
267 SqlArguments args = new SqlArguments(url, where, whereArgs);
268 if (TABLE_FAVORITES.equals(args.table)) {
269 return 0;
270 } else if (TABLE_OLD_FAVORITES.equals(args.table)) {
271 args.table = TABLE_FAVORITES;
272 }
273 checkWritePermissions(args);
274
275 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
276 int count = db.delete(args.table, args.where, args.args);
277 if (count > 0) sendNotify(url);
278 if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) deleted");
279 return count;
280 }
281
282 @Override
283 public int update(Uri url, ContentValues initialValues, String where, String[] whereArgs) {
284 SqlArguments args = new SqlArguments(url, where, whereArgs);
285 if (TABLE_FAVORITES.equals(args.table)) {
286 return 0;
287 }
288 checkWritePermissions(args);
289
290 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
291 int count = db.update(args.table, initialValues, args.where, args.args);
292 if (count > 0) sendNotify(url);
293 if (LOCAL_LOGV) Log.v(TAG, args.table + ": " + count + " row(s) <- " + initialValues);
294 return count;
295 }
296
297 @Override
298 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
299
300 /*
301 * When a client attempts to openFile the default ringtone or
302 * notification setting Uri, we will proxy the call to the current
303 * default ringtone's Uri (if it is in the DRM or media provider).
304 */
305 int ringtoneType = RingtoneManager.getDefaultType(uri);
306 // Above call returns -1 if the Uri doesn't match a default type
307 if (ringtoneType != -1) {
308 Context context = getContext();
309
310 // Get the current value for the default sound
311 Uri soundUri = RingtoneManager.getActualDefaultRingtoneUri(context, ringtoneType);
312 if (soundUri == null) {
313 // Fallback on any valid ringtone Uri
314 soundUri = RingtoneManager.getValidRingtoneUri(context);
315 }
316
317 if (soundUri != null) {
318 // Only proxy the openFile call to drm or media providers
319 String authority = soundUri.getAuthority();
320 boolean isDrmAuthority = authority.equals(DrmStore.AUTHORITY);
321 if (isDrmAuthority || authority.equals(MediaStore.AUTHORITY)) {
322
323 if (isDrmAuthority) {
324 try {
325 // Check DRM access permission here, since once we
326 // do the below call the DRM will be checking our
327 // permission, not our caller's permission
328 DrmStore.enforceAccessDrmPermission(context);
329 } catch (SecurityException e) {
330 throw new FileNotFoundException(e.getMessage());
331 }
332 }
333
334 return context.getContentResolver().openFileDescriptor(soundUri, mode);
335 }
336 }
337 }
338
339 return super.openFile(uri, mode);
340 }
341}