1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.deskclock.provider;
import static com.android.deskclock.provider.ClockContract.AlarmsColumns;
import static com.android.deskclock.provider.ClockContract.InstancesColumns;
import static com.android.deskclock.provider.ClockDatabaseHelper.ALARMS_TABLE_NAME;
import static com.android.deskclock.provider.ClockDatabaseHelper.INSTANCES_TABLE_NAME;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.ArrayMap;
import androidx.annotation.NonNull;
import com.android.deskclock.LogUtils;
import com.android.deskclock.Utils;
import java.util.Map;
public class ClockProvider extends ContentProvider {
private ClockDatabaseHelper mOpenHelper;
private static final int ALARMS = 1;
private static final int ALARMS_ID = 2;
private static final int INSTANCES = 3;
private static final int INSTANCES_ID = 4;
private static final int ALARMS_WITH_INSTANCES = 5;
/**
* Projection map used by query for snoozed alarms.
*/
private static final Map<String, String> sAlarmsWithInstancesProjection = new ArrayMap<>();
static {
sAlarmsWithInstancesProjection.put(ALARMS_TABLE_NAME + "." + AlarmsColumns._ID,
ALARMS_TABLE_NAME + "." + AlarmsColumns._ID);
sAlarmsWithInstancesProjection.put(ALARMS_TABLE_NAME + "." + AlarmsColumns.HOUR,
ALARMS_TABLE_NAME + "." + AlarmsColumns.HOUR);
sAlarmsWithInstancesProjection.put(ALARMS_TABLE_NAME + "." + AlarmsColumns.MINUTES,
ALARMS_TABLE_NAME + "." + AlarmsColumns.MINUTES);
sAlarmsWithInstancesProjection.put(ALARMS_TABLE_NAME + "." + AlarmsColumns.DAYS_OF_WEEK,
ALARMS_TABLE_NAME + "." + AlarmsColumns.DAYS_OF_WEEK);
sAlarmsWithInstancesProjection.put(ALARMS_TABLE_NAME + "." + AlarmsColumns.ENABLED,
ALARMS_TABLE_NAME + "." + AlarmsColumns.ENABLED);
sAlarmsWithInstancesProjection.put(ALARMS_TABLE_NAME + "." + AlarmsColumns.VIBRATE,
ALARMS_TABLE_NAME + "." + AlarmsColumns.VIBRATE);
sAlarmsWithInstancesProjection.put(ALARMS_TABLE_NAME + "." + AlarmsColumns.LABEL,
ALARMS_TABLE_NAME + "." + AlarmsColumns.LABEL);
sAlarmsWithInstancesProjection.put(ALARMS_TABLE_NAME + "." + AlarmsColumns.RINGTONE,
ALARMS_TABLE_NAME + "." + AlarmsColumns.RINGTONE);
sAlarmsWithInstancesProjection.put(ALARMS_TABLE_NAME + "." + AlarmsColumns.DELETE_AFTER_USE,
ALARMS_TABLE_NAME + "." + AlarmsColumns.DELETE_AFTER_USE);
sAlarmsWithInstancesProjection.put(ALARMS_TABLE_NAME + "." + AlarmsColumns.INCREASING_VOLUME,
ALARMS_TABLE_NAME + "." + AlarmsColumns.INCREASING_VOLUME);
sAlarmsWithInstancesProjection.put(INSTANCES_TABLE_NAME + "."
+ InstancesColumns.ALARM_STATE,
INSTANCES_TABLE_NAME + "." + InstancesColumns.ALARM_STATE);
sAlarmsWithInstancesProjection.put(INSTANCES_TABLE_NAME + "." + InstancesColumns._ID,
INSTANCES_TABLE_NAME + "." + InstancesColumns._ID);
sAlarmsWithInstancesProjection.put(INSTANCES_TABLE_NAME + "." + InstancesColumns.YEAR,
INSTANCES_TABLE_NAME + "." + InstancesColumns.YEAR);
sAlarmsWithInstancesProjection.put(INSTANCES_TABLE_NAME + "." + InstancesColumns.MONTH,
INSTANCES_TABLE_NAME + "." + InstancesColumns.MONTH);
sAlarmsWithInstancesProjection.put(INSTANCES_TABLE_NAME + "." + InstancesColumns.DAY,
INSTANCES_TABLE_NAME + "." + InstancesColumns.DAY);
sAlarmsWithInstancesProjection.put(INSTANCES_TABLE_NAME + "." + InstancesColumns.HOUR,
INSTANCES_TABLE_NAME + "." + InstancesColumns.HOUR);
sAlarmsWithInstancesProjection.put(INSTANCES_TABLE_NAME + "." + InstancesColumns.MINUTES,
INSTANCES_TABLE_NAME + "." + InstancesColumns.MINUTES);
sAlarmsWithInstancesProjection.put(INSTANCES_TABLE_NAME + "." + InstancesColumns.LABEL,
INSTANCES_TABLE_NAME + "." + InstancesColumns.LABEL);
sAlarmsWithInstancesProjection.put(INSTANCES_TABLE_NAME + "." + InstancesColumns.VIBRATE,
INSTANCES_TABLE_NAME + "." + InstancesColumns.VIBRATE);
}
private static final String ALARM_JOIN_INSTANCE_TABLE_STATEMENT =
ALARMS_TABLE_NAME + " LEFT JOIN " + INSTANCES_TABLE_NAME + " ON (" +
ALARMS_TABLE_NAME + "." + AlarmsColumns._ID + " = " + InstancesColumns.ALARM_ID + ")";
private static final String ALARM_JOIN_INSTANCE_WHERE_STATEMENT =
INSTANCES_TABLE_NAME + "." + InstancesColumns._ID + " IS NULL OR " +
INSTANCES_TABLE_NAME + "." + InstancesColumns._ID + " = (" +
"SELECT " + InstancesColumns._ID +
" FROM " + INSTANCES_TABLE_NAME +
" WHERE " + InstancesColumns.ALARM_ID +
" = " + ALARMS_TABLE_NAME + "." + AlarmsColumns._ID +
" ORDER BY " + InstancesColumns.ALARM_STATE + ", " +
InstancesColumns.YEAR + ", " + InstancesColumns.MONTH + ", " +
InstancesColumns.DAY + " LIMIT 1)";
private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
sURIMatcher.addURI(ClockContract.AUTHORITY, "alarms", ALARMS);
sURIMatcher.addURI(ClockContract.AUTHORITY, "alarms/#", ALARMS_ID);
sURIMatcher.addURI(ClockContract.AUTHORITY, "instances", INSTANCES);
sURIMatcher.addURI(ClockContract.AUTHORITY, "instances/#", INSTANCES_ID);
sURIMatcher.addURI(ClockContract.AUTHORITY, "alarms_with_instances", ALARMS_WITH_INSTANCES);
}
public ClockProvider() {
}
@Override
public boolean onCreate() {
final Context context = getContext();
final Context storageContext;
// All N devices have split storage areas, but we may need to
// migrate existing database into the new device encrypted
// storage area, which is where our data lives from now on.
storageContext = context.createDeviceProtectedStorageContext();
if (!storageContext.moveDatabaseFrom(context, ClockDatabaseHelper.DATABASE_NAME)) {
LogUtils.wtf("Failed to migrate database: %s", ClockDatabaseHelper.DATABASE_NAME);
}
mOpenHelper = new ClockDatabaseHelper(storageContext);
return true;
}
@Override
public Cursor query(@NonNull Uri uri, String[] projectionIn, String selection,
String[] selectionArgs, String sort) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
// Generate the body of the query
int match = sURIMatcher.match(uri);
switch (match) {
case ALARMS:
qb.setTables(ALARMS_TABLE_NAME);
break;
case ALARMS_ID:
qb.setTables(ALARMS_TABLE_NAME);
qb.appendWhere(AlarmsColumns._ID + "=");
qb.appendWhere(uri.getLastPathSegment());
break;
case INSTANCES:
qb.setTables(INSTANCES_TABLE_NAME);
break;
case INSTANCES_ID:
qb.setTables(INSTANCES_TABLE_NAME);
qb.appendWhere(InstancesColumns._ID + "=");
qb.appendWhere(uri.getLastPathSegment());
break;
case ALARMS_WITH_INSTANCES:
qb.setTables(ALARM_JOIN_INSTANCE_TABLE_STATEMENT);
qb.appendWhere(ALARM_JOIN_INSTANCE_WHERE_STATEMENT);
qb.setProjectionMap(sAlarmsWithInstancesProjection);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
Cursor ret = qb.query(db, projectionIn, selection, selectionArgs, null, null, sort);
if (ret == null) {
LogUtils.e("Alarms.query: failed");
} else {
ret.setNotificationUri(getContext().getContentResolver(), uri);
}
return ret;
}
@Override
public String getType(@NonNull Uri uri) {
int match = sURIMatcher.match(uri);
switch (match) {
case ALARMS:
return "vnd.android.cursor.dir/alarms";
case ALARMS_ID:
return "vnd.android.cursor.item/alarms";
case INSTANCES:
return "vnd.android.cursor.dir/instances";
case INSTANCES_ID:
return "vnd.android.cursor.item/instances";
default:
throw new IllegalArgumentException("Unknown URI");
}
}
@Override
public int update(@NonNull Uri uri, ContentValues values, String where, String[] whereArgs) {
int count;
String alarmId;
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
switch (sURIMatcher.match(uri)) {
case ALARMS_ID:
alarmId = uri.getLastPathSegment();
count = db.update(ALARMS_TABLE_NAME, values,
AlarmsColumns._ID + "=" + alarmId,
null);
break;
case INSTANCES_ID:
alarmId = uri.getLastPathSegment();
count = db.update(INSTANCES_TABLE_NAME, values,
InstancesColumns._ID + "=" + alarmId,
null);
break;
default: {
throw new UnsupportedOperationException("Cannot update URI: " + uri);
}
}
LogUtils.v("*** notifyChange() id: " + alarmId + " url " + uri);
notifyChange(getContext().getContentResolver(), uri);
return count;
}
@Override
public Uri insert(@NonNull Uri uri, ContentValues initialValues) {
long rowId;
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
switch (sURIMatcher.match(uri)) {
case ALARMS:
rowId = mOpenHelper.fixAlarmInsert(initialValues);
break;
case INSTANCES:
rowId = db.insert(INSTANCES_TABLE_NAME, null, initialValues);
break;
default:
throw new IllegalArgumentException("Cannot insert from URI: " + uri);
}
Uri uriResult = ContentUris.withAppendedId(uri, rowId);
notifyChange(getContext().getContentResolver(), uriResult);
return uriResult;
}
@Override
public int delete(@NonNull Uri uri, String where, String[] whereArgs) {
int count;
String primaryKey;
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
switch (sURIMatcher.match(uri)) {
case ALARMS:
count = db.delete(ALARMS_TABLE_NAME, where, whereArgs);
break;
case ALARMS_ID:
primaryKey = uri.getLastPathSegment();
if (TextUtils.isEmpty(where)) {
where = AlarmsColumns._ID + "=" + primaryKey;
} else {
where = AlarmsColumns._ID + "=" + primaryKey + " AND (" + where + ")";
}
count = db.delete(ALARMS_TABLE_NAME, where, whereArgs);
break;
case INSTANCES:
count = db.delete(INSTANCES_TABLE_NAME, where, whereArgs);
break;
case INSTANCES_ID:
primaryKey = uri.getLastPathSegment();
if (TextUtils.isEmpty(where)) {
where = InstancesColumns._ID + "=" + primaryKey;
} else {
where = InstancesColumns._ID + "=" + primaryKey + " AND (" + where + ")";
}
count = db.delete(INSTANCES_TABLE_NAME, where, whereArgs);
break;
default:
throw new IllegalArgumentException("Cannot delete from URI: " + uri);
}
notifyChange(getContext().getContentResolver(), uri);
return count;
}
/**
* Notify affected URIs of changes.
*/
private void notifyChange(ContentResolver resolver, Uri uri) {
resolver.notifyChange(uri, null);
final int match = sURIMatcher.match(uri);
// Also notify the joined table of changes to instances or alarms.
if (match == ALARMS || match == INSTANCES || match == ALARMS_ID || match == INSTANCES_ID) {
resolver.notifyChange(AlarmsColumns.ALARMS_WITH_INSTANCES_URI, null);
}
}
}
|