-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathContentProvider.java
More file actions
188 lines (146 loc) · 5.65 KB
/
ContentProvider.java
File metadata and controls
188 lines (146 loc) · 5.65 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
package com.activeandroid.content;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.util.SparseArray;
import com.activeandroid.ActiveAndroid;
import com.activeandroid.Cache;
import com.activeandroid.Configuration;
import com.activeandroid.Model;
import com.activeandroid.TableInfo;
public class ContentProvider extends android.content.ContentProvider {
//////////////////////////////////////////////////////////////////////////////////////
// PRIVATE CONSTANTS
//////////////////////////////////////////////////////////////////////////////////////
private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
private static final SparseArray<Class<? extends Model>> TYPE_CODES = new SparseArray<Class<? extends Model>>();
//////////////////////////////////////////////////////////////////////////////////////
// PRIVATE MEMBERS
//////////////////////////////////////////////////////////////////////////////////////
private static String sAuthority;
private static SparseArray<String> sMimeTypeCache = new SparseArray<String>();
//////////////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
//////////////////////////////////////////////////////////////////////////////////////
@Override
public boolean onCreate() {
ActiveAndroid.initialize(getConfiguration());
sAuthority = getAuthority();
final List<TableInfo> tableInfos = new ArrayList<TableInfo>(Cache.getTableInfos());
final int size = tableInfos.size();
for (int i = 0; i < size; i++) {
final TableInfo tableInfo = tableInfos.get(i);
final int tableKey = (i * 2) + 1;
final int itemKey = (i * 2) + 2;
// content://<authority>/<table>
URI_MATCHER.addURI(sAuthority, tableInfo.getTableName().toLowerCase(), tableKey);
TYPE_CODES.put(tableKey, tableInfo.getType());
// content://<authority>/<table>/<id>
URI_MATCHER.addURI(sAuthority, tableInfo.getTableName().toLowerCase() + "/#", itemKey);
TYPE_CODES.put(itemKey, tableInfo.getType());
}
return true;
}
@Override
public String getType(Uri uri) {
final int match = URI_MATCHER.match(uri);
String cachedMimeType = sMimeTypeCache.get(match);
if (cachedMimeType != null) {
return cachedMimeType;
}
final Class<? extends Model> type = getModelType(uri);
final boolean single = ((match % 2) == 0);
StringBuilder mimeType = new StringBuilder();
mimeType.append("vnd");
mimeType.append(".");
mimeType.append(sAuthority);
mimeType.append(".");
mimeType.append(single ? "item" : "dir");
mimeType.append("/");
mimeType.append("vnd");
mimeType.append(".");
mimeType.append(sAuthority);
mimeType.append(".");
mimeType.append(Cache.getTableName(type));
sMimeTypeCache.append(match, mimeType.toString());
return mimeType.toString();
}
// SQLite methods
@Override
public Uri insert(Uri uri, ContentValues values) {
final Class<? extends Model> type = getModelType(uri);
final Long id = Cache.openDatabase().insert(Cache.getTableName(type), null, values);
if (id != null && id > 0) {
Uri retUri = createUri(type, id);
notifyChange(retUri);
return retUri;
}
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
final Class<? extends Model> type = getModelType(uri);
final int count = Cache.openDatabase().update(Cache.getTableName(type), values, selection, selectionArgs);
notifyChange(uri);
return count;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
final Class<? extends Model> type = getModelType(uri);
final int count = Cache.openDatabase().delete(Cache.getTableName(type), selection, selectionArgs);
notifyChange(uri);
return count;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
final Class<? extends Model> type = getModelType(uri);
final Cursor cursor = Cache.openDatabase().query(
Cache.getTableName(type),
projection,
selection,
selectionArgs,
null,
null,
sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
//////////////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
//////////////////////////////////////////////////////////////////////////////////////
public static Uri createUri(Class<? extends Model> type, Long id) {
Uri.Builder uriBuilder = new Uri.Builder();
uriBuilder.authority(sAuthority);
uriBuilder.scheme("content");
uriBuilder.appendPath(Cache.getTableName(type).toLowerCase());
if (id != null) {
uriBuilder.appendPath(id.toString());
}
return uriBuilder.build();
}
//////////////////////////////////////////////////////////////////////////////////////
// PROTECTED METHODS
//////////////////////////////////////////////////////////////////////////////////////
protected String getAuthority() {
return getContext().getPackageName();
}
protected Configuration getConfiguration() {
return new Configuration.Builder(getContext()).create();
}
//////////////////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
//////////////////////////////////////////////////////////////////////////////////////
private Class<? extends Model> getModelType(Uri uri) {
final int code = URI_MATCHER.match(uri);
if (code != UriMatcher.NO_MATCH) {
return TYPE_CODES.get(code);
}
return null;
}
private void notifyChange(Uri uri) {
getContext().getContentResolver().notifyChange(uri, null);
}
}