本来EL的基本功能在周三已经完成了,打算开始准备EL所需要的数据进行实测了,但却出现个大麻烦,这两个晚上不得不进行大量的'重构'工作(实际就是因为前面偷懒,导致后面重写了)...
先说下大麻烦的来由 -- EL需要两个数据库,一个在应用目录下,叫做lac2数据库;另外一个在sdcard上,叫做el数据库; 这样分开原因是因为el数据库是用户数据库,不想因为用户删除应用,或者重装系统(估计一周刷一次ROM的只有我吧..)而使其数据丢失. 起初想着两个数据库分别被UI和Service使用,并不存在交叉,没必要使用ContentProvider那么一大套框架,所以直接使用两个简单的SQLiteDatabase对象链接/访问各自的数据库了.
现在说下麻烦 -- 随着EL的实现,有了一个不错的idea, 如果Service层可以使用el数据库,将可以增强一个功能, 于是很开心地在Service添加上了通过UI层对el数据库的访问接口;哈,问题来了 -- UI跟Service层是不一样的,是可以被Destroy的. 如果UI都被Destroy,那el数据库还访问个毛啊...
于是在添加这个功能与重写数据库访问部分之间纠结了一晚之后,'毅然'决定'选择前者,'重构'后者了...反正这个跨层访问数据库资源早晚要面对的,总是使用cache和aidl接口访问只会让代码更加混乱.
ContentProvider是个好东西,就像总有人说的一样,其在android的架子中与Activity,Intent等概念是在同一层的.通过与Uri, Resolver等的配合,使得app可以非常灵活和方便的访问数据. 实际都知道ContentProvider,但真写起来会发现要实现自己的ContentProvider,还是非常的'罗嗦'的..
现在EL实现了一个ContentProvider,贴在下面,供大家吐槽. 虽然还没有全部完成,但该有的基本元素都有了,还包括了一些'雕虫小技',比如 -- 咋使用'join'...
1 package jie.android.el.database;
2 3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 7 import jie.android.el.utils.AssetsHelper;
8 import android.content.ContentProvider;
9 import android.content.ContentUris;
10 import android.content.ContentValues;
11 import android.content.UriMatcher;
12 import android.database.Cursor;
13 import android.database.sqlite.SQLiteDatabase;
14 import android.net.Uri;
15 import android.os.Environment;
16 17 public class ELContentProvider
extends ContentProvider {
18 19 private static final String Tag = ELContentProvider.
class.getSimpleName();
20 21 private static final String AUTHORITY = "jie.android.el";
22 private static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.jie.android.el";
23 private static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.jie.android.el";
24 25 public static final Uri URI_EL_ESL = Uri.parse("content://" + AUTHORITY + "/el/esl");
26 public static final Uri URI_LAC_WORD_INFO = Uri.parse("content://" + AUTHORITY + "/lac/word_info");
27 public static final Uri URI_LAC_SYS_UPDATE = Uri.parse("content://" + AUTHORITY + "/lac/sys_update");
28 public static final Uri URI_LAC_DICT_INFO = Uri.parse("content://" + AUTHORITY + "/lac/dict_info");
29 public static final Uri URI_LAC_WORD_INDEX_JOIN_INFO = Uri.parse("content://" + AUTHORITY + "/lac/word_index_join_info");
30 public static final Uri URI_LAC_BLOCK_INFO = Uri.parse("content://" + AUTHORITY + "/lac/block_info");
31 32 private static final int MATCH_EL_ESL = 10;
33 private static final int MATCH_ITEM_EL_ESL = 11;
34 private static final int MATCH_LAC_WORD_INFO = 20;
35 private static final int MATCH_ITEM_LAC_WORD_INFO = 21;
36 private static final int MATCH_LAC_SYS_UPDATE = 30;
37 private static final int MATCH_LAC_DICT_INFO = 40;
38 private static final int MATCH_LAC_WORD_INDEX_JOIN_INFO = 50;
39 private static final int MATCH_LAC_BLOCK_INFO = 60;
40 41 42 private UriMatcher matcher =
null;
43 private LACDBAccess lacDBAccess =
null;
//should be a subclass of SQLiteOpenHelper
44 private ELDBAccess elDBAccess =
null;
45 46 private SQLiteDatabase db =
null;
47 48 @Override
49 public boolean onCreate() {
50 51 initDatabases();
52 initMatcher();
53 54 return true;
55 }
56 57 @Override
58 public String getType(Uri uri) {
59 int res = matcher.match(uri);
60 switch (res) {
61 case MATCH_EL_ESL:
62 case MATCH_LAC_WORD_INFO:
63 case MATCH_LAC_SYS_UPDATE:
64 case MATCH_LAC_DICT_INFO:
65 case MATCH_LAC_WORD_INDEX_JOIN_INFO:
66 case MATCH_LAC_BLOCK_INFO:
67 return CONTENT_TYPE;
68 case MATCH_ITEM_EL_ESL:
69 case MATCH_ITEM_LAC_WORD_INFO:
70 return CONTENT_ITEM_TYPE;
71 default:
72 throw new IllegalArgumentException("Unknown uri: " + uri);
73 }
74 }
75 76 @Override
77 public Uri insert(Uri uri, ContentValues values) {
78 int res = matcher.match(uri);
79 80 String table =
null;
81 82 switch (res) {
83 case MATCH_LAC_SYS_UPDATE:
84 db = lacDBAccess.getWritableDatabase();
85 table = "sys_update";
86 break;
87 default:
88 throw new IllegalArgumentException("insert() Unknown uri: " + uri);
89 }
90 91 long rowid = db.insert(table,
null, values);
92 return ContentUris.withAppendedId(uri, rowid);
93 }
94 95 @Override
96 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
97 int res = matcher.match(uri);
98 String table =
null;
99 switch (res) {
100 case MATCH_EL_ESL:
101 case MATCH_ITEM_EL_ESL:
102 db = elDBAccess.getReadableDatabase();
103 table = "esl";
104 105 if (res == MATCH_ITEM_EL_ESL) {
106 selection = "idx=?";
107 selectionArgs =
new String[]{String.valueOf(ContentUris.parseId(uri))};
108 }
109 break;
110 case MATCH_LAC_WORD_INFO:
111 case MATCH_ITEM_LAC_WORD_INFO:
112 db = lacDBAccess.getReadableDatabase();
113 table = "word_info";
114 115 if (res == MATCH_ITEM_LAC_WORD_INFO) {
116 selection = "idx=?";
117 selectionArgs =
new String[]{String.valueOf(ContentUris.parseId(uri))};
118 }
119 break;
120 case MATCH_LAC_SYS_UPDATE:
121 db = lacDBAccess.getReadableDatabase();
122 table = "sys_update";
123 break;
124 case MATCH_LAC_DICT_INFO:
125 db = lacDBAccess.getReadableDatabase();
126 table = "dict_info";
127 break;
128 case MATCH_LAC_WORD_INDEX_JOIN_INFO:
129 db = lacDBAccess.getReadableDatabase();
130 table = "word_index_100 inner join word_info on (word_index_100.word_idx=word_info.idx)";
131 break;
132 case MATCH_LAC_BLOCK_INFO:
133 db = lacDBAccess.getReadableDatabase();
134 table = "block_info_100";
135 break;
136 default:
137 throw new IllegalArgumentException("query() Unknown uri: " + uri);
138 }
139 140 return db.query(table, projection, selection, selectionArgs,
null,
null, sortOrder);
141 }
142 143 @Override
144 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
145 int res = matcher.match(uri);
146 String table =
null;
147 switch (res) {
148 case MATCH_LAC_SYS_UPDATE:
149 db = lacDBAccess.getWritableDatabase();
150 table = "sys_update";
151 break;
152 default:
153 throw new IllegalArgumentException("update() Unknown uri: " + uri);
154 }
155 return db.update(table, values, selection, selectionArgs);
156 }
157 158 @Override
159 public int delete(Uri uri, String selection, String[] selectionArgs) {
160 throw new IllegalArgumentException("delete() Unknown uri: " + uri);
161 }
162 163 private void initMatcher() {
164 matcher =
new UriMatcher(UriMatcher.NO_MATCH);
165 166 matcher.addURI(AUTHORITY, "el/esl", MATCH_EL_ESL);
167 matcher.addURI(AUTHORITY, "el/esl/#", MATCH_ITEM_EL_ESL);
168 matcher.addURI(AUTHORITY, "
LingosHook Android Client/word_info", MATCH_LAC_WORD_INFO);
169 matcher.addURI(AUTHORITY, "
LingosHook Android Client/word_info/#", MATCH_ITEM_LAC_WORD_INFO);
170 matcher.addURI(AUTHORITY, "
LingosHook Android Client/sys_update", MATCH_LAC_SYS_UPDATE);
171 matcher.addURI(AUTHORITY, "
LingosHook Android Client/dict_info", MATCH_LAC_DICT_INFO);
172 matcher.addURI(AUTHORITY, "
LingosHook Android Client/word_index_join_info", MATCH_LAC_WORD_INDEX_JOIN_INFO);
173 matcher.addURI(AUTHORITY, "
LingosHook Android Client/block_info", MATCH_LAC_BLOCK_INFO);
174 }
175 176 private void initDatabases() {
177 178 checkLACDatabase();
179 180 String db = Environment.getExternalStorageDirectory() + ELDBAccess.DBFILE;
181 elDBAccess =
new ELDBAccess(
this.getContext(), db);
182 183 db = getContext().getDatabasePath(LACDBAccess.DBFILE).getAbsolutePath();
184 lacDBAccess =
new LACDBAccess(
this.getContext(), db);
185 }
186 187 private void checkLACDatabase() {
188 189 File dbfile = getContext().getDatabasePath(LACDBAccess.DBFILE);
190 if (!dbfile.exists()) {
191 File parent = dbfile.getParentFile();
192 if (!parent.exists()) {
193 parent.mkdirs();
194 }
195 196 InputStream input;
197 try {
198 input = getContext().getAssets().open("lac2.zip");
199 AssetsHelper.UnzipTo(input, parent.getAbsolutePath(),
null);
200 }
catch (IOException e) {
201 e.printStackTrace();
202 }
203 }
204 }
205 }
206 只有selection,sortOrder参数,如果需要'group by'操作怎么办? 再直接点,怎么通过ContentProvider接口传入个自定义的sql语句呢,像rawQuery()? 实际这些都可以搞定的 -- ContentProvider就是个接口封装,最终的访问还是需要SQLiteDatabase对象来完成,那么只要SQLiteDatabase可以实现的功能,就有办法通过ContentProvider传给SQLiteDatabase. 咋说呢, 多想想如何利用那个URI呀....