-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppProvider.java
More file actions
211 lines (178 loc) · 8.05 KB
/
Copy pathAppProvider.java
File metadata and controls
211 lines (178 loc) · 8.05 KB
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
package com.sachet.database2;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
public class AppProvider extends ContentProvider{
private static final String TAG = "AppProvider";
private static AppDatabase mOpenHelper = null;
private static UriMatcher sUriMatcher = buildUri();
public static final String CONTENT_AUTHORITY = "com.sachet.database2.provider";
public static final Uri CONTENT_AUTHORITY_URI = Uri.parse("content://"+CONTENT_AUTHORITY);
private static final int TASK = 100;
private static final int TASK_ID = 101;
private static UriMatcher buildUri(){
final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//eg content://com.sachet.databases.provider/Tasks
uriMatcher.addURI(CONTENT_AUTHORITY,TaskContracts.TABLE_NAME, TASK);
//eg content://com.sachet.databases.provider/Tasks/8 -- if we have something like this as the uri
uriMatcher.addURI(CONTENT_AUTHORITY,TaskContracts.TABLE_NAME+"/#", TASK_ID);
return uriMatcher;
}
@Override
public boolean onCreate() {
mOpenHelper = AppDatabase.getInstance(getContext());
return true;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
Log.d(TAG, "query: method called with Uri "+uri);
final int match = sUriMatcher.match(uri);
Log.d(TAG, "query: the match is "+match);
SQLiteQueryBuilder sqLiteQueryBuilder = new SQLiteQueryBuilder();
String selectionQuery = null;
switch (match){
case TASK:
sqLiteQueryBuilder.setTables(TaskContracts.TABLE_NAME);
break;
case TASK_ID:
sqLiteQueryBuilder.setTables(TaskContracts.TABLE_NAME);
long id = TaskContracts.getTaskId(uri);
selectionQuery = TaskContracts.Columns._ID+" = "+id;
// if(selection != null && selection.length()>0){
// selectionQuery += selection;
// }
sqLiteQueryBuilder.appendWhere(selectionQuery);
break;
default:
throw new IllegalArgumentException("Invalid match parameter "+match);
}
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
// return sqLiteQueryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
Cursor cursor = sqLiteQueryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
Log.d(TAG, "query: number of rows in returned cursor "+cursor.getCount());
cursor.setNotificationUri(getContext().getContentResolver(), uri);//register to watch content provider for changes
//sets notification to any listener that have registered and the cursorloader are automatically registered
/**
* next we make sure that all those methods that can make changes to the database call the content resolver
* notify change method to notify the listeners that there have been a change
*/
return cursor;
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
Log.d(TAG, "insert: starts with Uri "+uri.toString());
final int match = sUriMatcher.match(uri);
Log.d(TAG, "insert: the match code is "+match);
SQLiteDatabase db;
long uri1;
Uri return1;
switch (match){
case TASK:
db = mOpenHelper.getWritableDatabase();
uri1 = db.insert(TaskContracts.TABLE_NAME, null, values);
if(uri1 > 0){
return1 = TaskContracts.buildUri(uri1);
}else{
throw new android.database.SQLException("Failed to insert into the Uri "+uri.toString());
}
break;
default:
throw new IllegalArgumentException("Insert called with unknown arguments");
}
if(uri1 > 0){
Log.d(TAG, "insert: Setting notify changed with "+uri);
getContext().getContentResolver().notifyChange(uri, null);
}else{
Log.d(TAG, "insert: nothing inserted");
}
Log.d(TAG, "insert: returning "+return1.toString());
return return1;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
Log.d(TAG, "delete: starts with uri "+uri.toString());
final int match = sUriMatcher.match(uri);
Log.d(TAG, "delete: match code is "+uri);
int count;
SQLiteDatabase db;
switch(match){
case TASK:
db = mOpenHelper.getWritableDatabase();
count = db.delete(TaskContracts.TABLE_NAME, selection, selectionArgs);
break;
case TASK_ID:
db = mOpenHelper.getWritableDatabase();
long id = TaskContracts.getTaskId(uri);
String selectionCriteria = TaskContracts.Columns._ID+" = "+ id;
// if(selection!=null||selection.length()>0){
// selectionCriteria += "AND ("+selection+");";
// }
count = db.delete(TaskContracts.TABLE_NAME, selectionCriteria, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown Uri "+uri.toString());
}
if(count > 0){
Log.d(TAG, "delete: Setting notify Change with "+uri);
getContext().getContentResolver().notifyChange(uri, null);
}else{
Log.d(TAG, "delete: nothing was deleted");
}
return count;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
Log.d(TAG, "update: starts with uri "+uri.toString());
final int match = sUriMatcher.match(uri);
Log.d(TAG, "update: match code is "+match);
int count;
SQLiteDatabase db;
String selectionCriteria;
switch (match){
case TASK:
db = mOpenHelper.getWritableDatabase();
Log.d(TAG, "update: selected "+selection);
count = db.update(TaskContracts.TABLE_NAME, values, selection, selectionArgs);
break;
case TASK_ID:
db = mOpenHelper.getWritableDatabase();
long taskId = TaskContracts.getTaskId(uri);
selectionCriteria = TaskContracts.Columns._ID+" = "+taskId;
//
// if(selection!=null&& selection.length()>0){
// selectionCriteria += "AND ("+selection+");";
// }
count = db.update(TaskContracts.TABLE_NAME, values, selectionCriteria, selectionArgs);
break;
default:
throw new IllegalArgumentException("Called with wrong uri "+uri.toString());
}
Log.d(TAG, "update: returning "+count);
if(count > 0){
Log.d(TAG, "update: Setting notify Change with "+uri);
getContext().getContentResolver().notifyChange(uri, null);
}else{
Log.d(TAG, "update: nothing was deleted");
}
return count;
}
}
//The built in cursorLoader class takes care of registering itself as a listener with the content resolver that it queries
/**
* so now when theres a change to the database the cursorloader will receive notification that the data has changed and it will trigger
* a call in our case to th mainActivityFragments onLoadFinished method
*/