• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.deskclock;
18 
19 import android.content.ContentProvider;
20 import android.content.ContentValues;
21 import android.content.Context;
22 import android.content.UriMatcher;
23 import android.database.Cursor;
24 import android.database.SQLException;
25 import android.database.sqlite.SQLiteDatabase;
26 import android.database.sqlite.SQLiteQueryBuilder;
27 import android.net.Uri;
28 import android.text.TextUtils;
29 
30 public class AlarmProvider extends ContentProvider {
31     private AlarmDatabaseHelper mOpenHelper;
32 
33     private static final int ALARMS = 1;
34     private static final int ALARMS_ID = 2;
35     private static final UriMatcher sURLMatcher = new UriMatcher(
36             UriMatcher.NO_MATCH);
37 
38     static {
39         sURLMatcher.addURI("com.android.deskclock", "alarm", ALARMS);
40         sURLMatcher.addURI("com.android.deskclock", "alarm/#", ALARMS_ID);
41     }
42 
AlarmProvider()43     public AlarmProvider() {
44     }
45 
46     @Override
onCreate()47     public boolean onCreate() {
48         mOpenHelper = new AlarmDatabaseHelper(getContext());
49         return true;
50     }
51 
52     @Override
query(Uri url, String[] projectionIn, String selection, String[] selectionArgs, String sort)53     public Cursor query(Uri url, String[] projectionIn, String selection,
54             String[] selectionArgs, String sort) {
55         SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
56 
57         // Generate the body of the query
58         int match = sURLMatcher.match(url);
59         switch (match) {
60             case ALARMS:
61                 qb.setTables("alarms");
62                 break;
63             case ALARMS_ID:
64                 qb.setTables("alarms");
65                 qb.appendWhere("_id=");
66                 qb.appendWhere(url.getPathSegments().get(1));
67                 break;
68             default:
69                 throw new IllegalArgumentException("Unknown URL " + url);
70         }
71 
72         SQLiteDatabase db = mOpenHelper.getReadableDatabase();
73         Cursor ret = qb.query(db, projectionIn, selection, selectionArgs,
74                               null, null, sort);
75 
76         if (ret == null) {
77             if (Log.LOGV) Log.v("Alarms.query: failed");
78         } else {
79             ret.setNotificationUri(getContext().getContentResolver(), url);
80         }
81 
82         return ret;
83     }
84 
85     @Override
getType(Uri url)86     public String getType(Uri url) {
87         int match = sURLMatcher.match(url);
88         switch (match) {
89             case ALARMS:
90                 return "vnd.android.cursor.dir/alarms";
91             case ALARMS_ID:
92                 return "vnd.android.cursor.item/alarms";
93             default:
94                 throw new IllegalArgumentException("Unknown URL");
95         }
96     }
97 
98     @Override
update(Uri url, ContentValues values, String where, String[] whereArgs)99     public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
100         int count;
101         long rowId = 0;
102         int match = sURLMatcher.match(url);
103         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
104         switch (match) {
105             case ALARMS_ID: {
106                 String segment = url.getPathSegments().get(1);
107                 rowId = Long.parseLong(segment);
108                 count = db.update("alarms", values, "_id=" + rowId, null);
109                 break;
110             }
111             default: {
112                 throw new UnsupportedOperationException(
113                         "Cannot update URL: " + url);
114             }
115         }
116         if (Log.LOGV) Log.v("*** notifyChange() rowId: " + rowId + " url " + url);
117         getContext().getContentResolver().notifyChange(url, null);
118         return count;
119     }
120 
121     @Override
insert(Uri url, ContentValues initialValues)122     public Uri insert(Uri url, ContentValues initialValues) {
123         if (sURLMatcher.match(url) != ALARMS) {
124             throw new IllegalArgumentException("Cannot insert into URL: " + url);
125         }
126 
127         Uri newUrl = mOpenHelper.commonInsert(initialValues);
128         getContext().getContentResolver().notifyChange(newUrl, null);
129         return newUrl;
130     }
131 
delete(Uri url, String where, String[] whereArgs)132     public int delete(Uri url, String where, String[] whereArgs) {
133         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
134         int count;
135         long rowId = 0;
136         switch (sURLMatcher.match(url)) {
137             case ALARMS:
138                 count = db.delete("alarms", where, whereArgs);
139                 break;
140             case ALARMS_ID:
141                 String segment = url.getPathSegments().get(1);
142                 rowId = Long.parseLong(segment);
143                 if (TextUtils.isEmpty(where)) {
144                     where = "_id=" + segment;
145                 } else {
146                     where = "_id=" + segment + " AND (" + where + ")";
147                 }
148                 count = db.delete("alarms", where, whereArgs);
149                 break;
150             default:
151                 throw new IllegalArgumentException("Cannot delete from URL: " + url);
152         }
153 
154         getContext().getContentResolver().notifyChange(url, null);
155         return count;
156     }
157 }
158