-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathStringsContentProvider.java
142 lines (99 loc) · 3.38 KB
/
StringsContentProvider.java
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
package course.examples.contentproviders.stringcontentprovider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.util.SparseArray;
// Note: Currently, this data does not persist across device reboot
public class StringsContentProvider extends ContentProvider {
// Data storage
private static final SparseArray<DataRecord> db = new SparseArray<DataRecord>();
@SuppressWarnings("unused")
private static final String TAG = "StringsContentProvider";
// Delete some or all data items
@Override
public synchronized int delete(Uri uri, String selection,
String[] selectionArgs) {
int numRecordsRemoved = 0;
// If last segment is the table name, delete all data items
if (isTableUri(uri)) {
numRecordsRemoved = db.size();
db.clear();
// If last segment is the digit, delete data item with that ID
} else if (isItemUri(uri)) {
Integer requestId = Integer.parseInt(uri.getLastPathSegment());
if (null != db.get(requestId)) {
db.remove(requestId);
numRecordsRemoved++;
}
}
//return number of items deleted
return numRecordsRemoved;
}
// Return MIME type for given uri
@Override
public synchronized String getType(Uri uri) {
String contentType = DataContract.CONTENT_ITEM_TYPE;
if (isTableUri(uri)) {
contentType = DataContract.CONTENT_DIR_TYPE;
}
return contentType;
}
// Insert specified value into ContentProvider
@Override
public synchronized Uri insert(Uri uri, ContentValues value) {
if (value.containsKey(DataContract.DATA)) {
DataRecord dataRecord = new DataRecord(value.getAsString(DataContract.DATA));
db.put(dataRecord.getID(), dataRecord);
// return Uri associated with newly-added data item
return Uri.withAppendedPath(DataContract.CONTENT_URI,
String.valueOf(dataRecord.getID()));
}
return null;
}
// return all or some rows from ContentProvider based on specified Uri
// all other parameters are ignored
@Override
public synchronized Cursor query(Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder) {
// Create simple cursor
MatrixCursor cursor = new MatrixCursor(DataContract.ALL_COLUMNS);
if (isTableUri(uri)) {
// Add all rows to cursor
for (int idx = 0; idx < db.size(); idx++) {
DataRecord dataRecord = db.get(db.keyAt(idx));
cursor.addRow(new Object[] { dataRecord.getID(),
dataRecord.getData() });
}
} else if (isItemUri(uri)){
// Add single row to cursor
Integer requestId = Integer.parseInt(uri.getLastPathSegment());
if (null != db.get(requestId)) {
DataRecord dr = db.get(requestId);
cursor.addRow(new Object[] { dr.getID(), dr.getData() });
}
}
return cursor;
}
// Ignore request
@Override
public synchronized int update(Uri uri, ContentValues values,
String selection, String[] selectionArgs) {
return 0;
}
// Initialize ContentProvider
// Nothing to do in this case
@Override
public boolean onCreate() {
return true;
}
// Does last segment of the Uri match a string of digits?
private boolean isItemUri(Uri uri) {
return uri.getLastPathSegment().matches("\\d+");
}
// Is the last segment of the Uri the name of the data table?
private boolean isTableUri(Uri uri) {
return uri.getLastPathSegment().equals(DataContract.DATA_TABLE);
}
}