本來(lái)EL的基本功能在周三已經(jīng)完成了,打算開(kāi)始準(zhǔn)備EL所需要的數(shù)據(jù)進(jìn)行實(shí)測(cè)了,但卻出現(xiàn)個(gè)大麻煩,這兩個(gè)晚上不得不進(jìn)行大量的'重構(gòu)'工作(實(shí)際就是因?yàn)榍懊嫱祽?導(dǎo)致后面重寫(xiě)了)...
先說(shuō)下大麻煩的來(lái)由 -- EL需要兩個(gè)數(shù)據(jù)庫(kù),一個(gè)在應(yīng)用目錄下,叫做lac2數(shù)據(jù)庫(kù);另外一個(gè)在sdcard上,叫做el數(shù)據(jù)庫(kù); 這樣分開(kāi)原因是因?yàn)閑l數(shù)據(jù)庫(kù)是用戶(hù)數(shù)據(jù)庫(kù),不想因?yàn)橛脩?hù)刪除應(yīng)用,或者重裝系統(tǒng)(估計(jì)一周刷一次ROM的只有我吧..)而使其數(shù)據(jù)丟失. 起初想著兩個(gè)數(shù)據(jù)庫(kù)分別被UI和Service使用,并不存在交叉,沒(méi)必要使用ContentProvider那么一大套框架,所以直接使用兩個(gè)簡(jiǎn)單的SQLiteDatabase對(duì)象鏈接/訪(fǎng)問(wèn)各自的數(shù)據(jù)庫(kù)了.
現(xiàn)在說(shuō)下麻煩 -- 隨著EL的實(shí)現(xiàn),有了一個(gè)不錯(cuò)的idea, 如果Service層可以使用el數(shù)據(jù)庫(kù),將可以增強(qiáng)一個(gè)功能, 于是很開(kāi)心地在Service添加上了通過(guò)UI層對(duì)el數(shù)據(jù)庫(kù)的訪(fǎng)問(wèn)接口;哈,問(wèn)題來(lái)了 -- UI跟Service層是不一樣的,是可以被Destroy的. 如果UI都被Destroy,那el數(shù)據(jù)庫(kù)還訪(fǎng)問(wèn)個(gè)毛啊...
于是在添加這個(gè)功能與重寫(xiě)數(shù)據(jù)庫(kù)訪(fǎng)問(wèn)部分之間糾結(jié)了一晚之后,'毅然'決定'選擇前者,'重構(gòu)'后者了...反正這個(gè)跨層訪(fǎng)問(wèn)數(shù)據(jù)庫(kù)資源早晚要面對(duì)的,總是使用cache和aidl接口訪(fǎng)問(wèn)只會(huì)讓代碼更加混亂.
ContentProvider是個(gè)好東西,就像總有人說(shuō)的一樣,其在android的架子中與Activity,Intent等概念是在同一層的.通過(guò)與Uri, Resolver等的配合,使得app可以非常靈活和方便的訪(fǎng)問(wèn)數(shù)據(jù). 實(shí)際都知道ContentProvider,但真寫(xiě)起來(lái)會(huì)發(fā)現(xiàn)要實(shí)現(xiàn)自己的ContentProvider,還是非常的'羅嗦'的..
現(xiàn)在EL實(shí)現(xiàn)了一個(gè)ContentProvider,貼在下面,供大家吐槽. 雖然還沒(méi)有全部完成,但該有的基本元素都有了,還包括了一些'雕蟲(chóng)小技',比如 -- 咋使用'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 ContentProvider框架非常的方便,使用時(shí)只要簡(jiǎn)單填充具體的數(shù)據(jù)操作皆可;但也有人認(rèn)為其接口定義的不夠靈活,比如在query()接口中,
只有selection,sortOrder參數(shù),如果需要'group by'操作怎么辦? 再直接點(diǎn),怎么通過(guò)ContentProvider接口傳入個(gè)自定義的sql語(yǔ)句呢,像rawQuery()? 實(shí)際這些都可以搞定的 -- ContentProvider就是個(gè)接口封裝,最終的訪(fǎng)問(wèn)還是需要SQLiteDatabase對(duì)象來(lái)完成,那么只要SQLiteDatabase可以實(shí)現(xiàn)的功能,就有辦法通過(guò)ContentProvider傳給SQLiteDatabase. 咋說(shuō)呢, 多想想如何利用那個(gè)URI呀....