本來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呀....