OSDN Git Service

Fix the build.
[android-x86/packages-apps-DeskClock.git] / src / com / android / deskclock / AlarmProvider.java
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.ContentUris;
21 import android.content.ContentValues;
22 import android.content.Context;
23 import android.content.UriMatcher;
24 import android.database.Cursor;
25 import android.database.SQLException;
26 import android.database.sqlite.SQLiteDatabase;
27 import android.database.sqlite.SQLiteOpenHelper;
28 import android.database.sqlite.SQLiteQueryBuilder;
29 import android.net.Uri;
30 import android.text.TextUtils;
31
32 public class AlarmProvider extends ContentProvider {
33     private SQLiteOpenHelper mOpenHelper;
34
35     private static final int ALARMS = 1;
36     private static final int ALARMS_ID = 2;
37     private static final UriMatcher sURLMatcher = new UriMatcher(
38             UriMatcher.NO_MATCH);
39
40     static {
41         sURLMatcher.addURI("com.android.deskclock", "alarm", ALARMS);
42         sURLMatcher.addURI("com.android.deskclock", "alarm/#", ALARMS_ID);
43     }
44
45     private static class DatabaseHelper extends SQLiteOpenHelper {
46         private static final String DATABASE_NAME = "alarms.db";
47         private static final int DATABASE_VERSION = 5;
48
49         public DatabaseHelper(Context context) {
50             super(context, DATABASE_NAME, null, DATABASE_VERSION);
51         }
52
53         @Override
54         public void onCreate(SQLiteDatabase db) {
55             db.execSQL("CREATE TABLE alarms (" +
56                        "_id INTEGER PRIMARY KEY," +
57                        "hour INTEGER, " +
58                        "minutes INTEGER, " +
59                        "daysofweek INTEGER, " +
60                        "alarmtime INTEGER, " +
61                        "enabled INTEGER, " +
62                        "vibrate INTEGER, " +
63                        "message TEXT, " +
64                        "alert TEXT);");
65
66             // insert default alarms
67             String insertMe = "INSERT INTO alarms " +
68                     "(hour, minutes, daysofweek, alarmtime, enabled, vibrate, message, alert) " +
69                     "VALUES ";
70             db.execSQL(insertMe + "(7, 0, 127, 0, 0, 1, '', '');");
71             db.execSQL(insertMe + "(8, 30, 31, 0, 0, 1, '', '');");
72             db.execSQL(insertMe + "(9, 00, 0, 0, 0, 1, '', '');");
73         }
74
75         @Override
76         public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
77             if (Log.LOGV) Log.v(
78                     "Upgrading alarms database from version " +
79                     oldVersion + " to " + currentVersion +
80                     ", which will destroy all old data");
81             db.execSQL("DROP TABLE IF EXISTS alarms");
82             onCreate(db);
83         }
84     }
85
86     public AlarmProvider() {
87     }
88
89     @Override
90     public boolean onCreate() {
91         mOpenHelper = new DatabaseHelper(getContext());
92         return true;
93     }
94
95     @Override
96     public Cursor query(Uri url, String[] projectionIn, String selection,
97             String[] selectionArgs, String sort) {
98         SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
99
100         // Generate the body of the query
101         int match = sURLMatcher.match(url);
102         switch (match) {
103             case ALARMS:
104                 qb.setTables("alarms");
105                 break;
106             case ALARMS_ID:
107                 qb.setTables("alarms");
108                 qb.appendWhere("_id=");
109                 qb.appendWhere(url.getPathSegments().get(1));
110                 break;
111             default:
112                 throw new IllegalArgumentException("Unknown URL " + url);
113         }
114
115         SQLiteDatabase db = mOpenHelper.getReadableDatabase();
116         Cursor ret = qb.query(db, projectionIn, selection, selectionArgs,
117                               null, null, sort);
118
119         if (ret == null) {
120             if (Log.LOGV) Log.v("Alarms.query: failed");
121         } else {
122             ret.setNotificationUri(getContext().getContentResolver(), url);
123         }
124
125         return ret;
126     }
127
128     @Override
129     public String getType(Uri url) {
130         int match = sURLMatcher.match(url);
131         switch (match) {
132             case ALARMS:
133                 return "vnd.android.cursor.dir/alarms";
134             case ALARMS_ID:
135                 return "vnd.android.cursor.item/alarms";
136             default:
137                 throw new IllegalArgumentException("Unknown URL");
138         }
139     }
140
141     @Override
142     public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
143         int count;
144         long rowId = 0;
145         int match = sURLMatcher.match(url);
146         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
147         switch (match) {
148             case ALARMS_ID: {
149                 String segment = url.getPathSegments().get(1);
150                 rowId = Long.parseLong(segment);
151                 count = db.update("alarms", values, "_id=" + rowId, null);
152                 break;
153             }
154             default: {
155                 throw new UnsupportedOperationException(
156                         "Cannot update URL: " + url);
157             }
158         }
159         if (Log.LOGV) Log.v("*** notifyChange() rowId: " + rowId + " url " + url);
160         getContext().getContentResolver().notifyChange(url, null);
161         return count;
162     }
163
164     @Override
165     public Uri insert(Uri url, ContentValues initialValues) {
166         if (sURLMatcher.match(url) != ALARMS) {
167             throw new IllegalArgumentException("Cannot insert into URL: " + url);
168         }
169
170         ContentValues values;
171         if (initialValues != null)
172             values = new ContentValues(initialValues);
173         else
174             values = new ContentValues();
175
176         if (!values.containsKey(Alarm.Columns.HOUR))
177             values.put(Alarm.Columns.HOUR, 0);
178
179         if (!values.containsKey(Alarm.Columns.MINUTES))
180             values.put(Alarm.Columns.MINUTES, 0);
181
182         if (!values.containsKey(Alarm.Columns.DAYS_OF_WEEK))
183             values.put(Alarm.Columns.DAYS_OF_WEEK, 0);
184
185         if (!values.containsKey(Alarm.Columns.ALARM_TIME))
186             values.put(Alarm.Columns.ALARM_TIME, 0);
187
188         if (!values.containsKey(Alarm.Columns.ENABLED))
189             values.put(Alarm.Columns.ENABLED, 0);
190
191         if (!values.containsKey(Alarm.Columns.VIBRATE))
192             values.put(Alarm.Columns.VIBRATE, 1);
193
194         if (!values.containsKey(Alarm.Columns.MESSAGE))
195             values.put(Alarm.Columns.MESSAGE, "");
196
197         if (!values.containsKey(Alarm.Columns.ALERT))
198             values.put(Alarm.Columns.ALERT, "");
199
200         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
201         long rowId = db.insert("alarms", Alarm.Columns.MESSAGE, values);
202         if (rowId < 0) {
203             throw new SQLException("Failed to insert row into " + url);
204         }
205         if (Log.LOGV) Log.v("Added alarm rowId = " + rowId);
206
207         Uri newUrl = ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, rowId);
208         getContext().getContentResolver().notifyChange(newUrl, null);
209         return newUrl;
210     }
211
212     public int delete(Uri url, String where, String[] whereArgs) {
213         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
214         int count;
215         long rowId = 0;
216         switch (sURLMatcher.match(url)) {
217             case ALARMS:
218                 count = db.delete("alarms", where, whereArgs);
219                 break;
220             case ALARMS_ID:
221                 String segment = url.getPathSegments().get(1);
222                 rowId = Long.parseLong(segment);
223                 if (TextUtils.isEmpty(where)) {
224                     where = "_id=" + segment;
225                 } else {
226                     where = "_id=" + segment + " AND (" + where + ")";
227                 }
228                 count = db.delete("alarms", where, whereArgs);
229                 break;
230             default:
231                 throw new IllegalArgumentException("Cannot delete from URL: " + url);
232         }
233
234         getContext().getContentResolver().notifyChange(url, null);
235         return count;
236     }
237 }