diff options
author | David A. Madore <david+git@madore.org> | 2010-04-25 00:33:15 +0200 |
---|---|---|
committer | David A. Madore <david+git@madore.org> | 2010-04-25 00:33:15 +0200 |
commit | 2521f18e396b989043387ef0cd14eeb101c14263 (patch) | |
tree | 85ce8fbcae41fdf441d8d6f1d4cefc86b742be72 /src/org/madore | |
parent | f62aafcf44f4a724c82c8c92df155a119052ad31 (diff) | |
download | UnicodeMap-2521f18e396b989043387ef0cd14eeb101c14263.tar.gz UnicodeMap-2521f18e396b989043387ef0cd14eeb101c14263.tar.bz2 UnicodeMap-2521f18e396b989043387ef0cd14eeb101c14263.zip |
Use a separate thread for populating database. Still doesn't work.
I don't understand the point of this whole rigmarole, nor do I
understand why it doesn't work... :-(
Diffstat (limited to 'src/org/madore')
-rw-r--r-- | src/org/madore/android/unicodeMap/UnicodeDatabase.java | 51 | ||||
-rw-r--r-- | src/org/madore/android/unicodeMap/UnicodeMapActivity.java | 26 |
2 files changed, 54 insertions, 23 deletions
diff --git a/src/org/madore/android/unicodeMap/UnicodeDatabase.java b/src/org/madore/android/unicodeMap/UnicodeDatabase.java index ef12b66..b0b5c77 100644 --- a/src/org/madore/android/unicodeMap/UnicodeDatabase.java +++ b/src/org/madore/android/unicodeMap/UnicodeDatabase.java @@ -8,10 +8,12 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.sql.SQLException; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; import android.content.Context; import android.content.res.AssetManager; import android.app.Activity; -import android.app.ProgressDialog; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteStatement; @@ -59,35 +61,38 @@ public class UnicodeDatabase { db = dbinit.getReadableDatabase(); } - public void populate() { + protected void reportProgress(Handler progressHandler, + int done, int total) { + Message msg = progressHandler.obtainMessage(); + Bundle b = new Bundle(); + b.putInt("done", done); + b.putInt("total", total); + msg.setData(b); + progressHandler.sendMessage(msg); + } + + public void populate(Handler progressHandler) { SQLiteDatabase db = dbinit.getWritableDatabase(); db.execSQL("DELETE FROM "+UNICODE_TABLE_NAME); AssetManager amgr = activity.getAssets(); SQLiteStatement cmd = db.compileStatement("INSERT INTO "+UNICODE_TABLE_NAME +" VALUES ( ? , ? , ? )"); - ProgressDialog progress = new ProgressDialog(activity); - progress.setOwnerActivity(activity); - progress.setTitle("Loading Unicode database"); - progress.setMessage("Please wait"); - progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); - progress.show(); + final String[] pieces = { + "UnicodeData-00.txt", "UnicodeData-01.txt", + "UnicodeData-02.txt", "UnicodeData-03.txt", + "UnicodeData-04.txt", "UnicodeData-05.txt", + "UnicodeData-06.txt", "UnicodeData-07.txt", + "UnicodeData-08.txt", "UnicodeData-09.txt", + "UnicodeData-10.txt", "UnicodeData-11.txt", + "UnicodeData-12.txt", "UnicodeData-13.txt", + "UnicodeData-14.txt", "UnicodeData-15.txt", + "UnicodeData-16.txt", "UnicodeData-17.txt", + "UnicodeData-18.txt", "UnicodeData-19.txt" + }; try { - final String[] pieces = { - "UnicodeData-00.txt", "UnicodeData-01.txt", - "UnicodeData-02.txt", "UnicodeData-03.txt", - "UnicodeData-04.txt", "UnicodeData-05.txt", - "UnicodeData-06.txt", "UnicodeData-07.txt", - "UnicodeData-08.txt", "UnicodeData-09.txt", - "UnicodeData-10.txt", "UnicodeData-11.txt", - "UnicodeData-12.txt", "UnicodeData-13.txt", - "UnicodeData-14.txt", "UnicodeData-15.txt", - "UnicodeData-16.txt", "UnicodeData-17.txt", - "UnicodeData-18.txt", "UnicodeData-19.txt" - }; for ( int cnt=0 ; cnt<pieces.length ; cnt++ ) { - progress.show(); - progress.setProgress(cnt*10000/pieces.length); + reportProgress(progressHandler, cnt, pieces.length); String file = pieces[cnt]; BufferedReader rd = new BufferedReader(new InputStreamReader(amgr.open(file), @@ -112,7 +117,7 @@ public class UnicodeDatabase { } catch (IOException e) { throw new RuntimeException(e); } - progress.dismiss(); + reportProgress(progressHandler, pieces.length, pieces.length); db.close(); } diff --git a/src/org/madore/android/unicodeMap/UnicodeMapActivity.java b/src/org/madore/android/unicodeMap/UnicodeMapActivity.java index fbaced4..7d70186 100644 --- a/src/org/madore/android/unicodeMap/UnicodeMapActivity.java +++ b/src/org/madore/android/unicodeMap/UnicodeMapActivity.java @@ -5,9 +5,12 @@ import java.util.List; import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.os.Handler; +import android.os.Message; import android.view.View; import android.widget.*; import android.app.ListActivity; +import android.app.ProgressDialog; public final class UnicodeMapActivity extends ListActivity { @@ -36,6 +39,29 @@ public final class UnicodeMapActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + final ProgressDialog progress = new ProgressDialog(this); + progress.setOwnerActivity(this); + progress.setTitle("Loading Unicode database"); + progress.setMessage("Please wait"); + progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + progress.show(); + final Handler handler = new Handler() { + public void handleMessage(Message msg) { + int done = msg.getData().getInt("done"); + int total = msg.getData().getInt("total"); + progress.setProgress(done*10000/total); + if ( done >= total ) + progress.dismiss(); + } + }; + final Thread thr = new Thread() { + public void run() { + UnicodeDatabase db + = new UnicodeDatabase(UnicodeMapActivity.this); + db.populate(handler); + } + }; + thr.start(); setListAdapter(new ArrayAdapter<UnicodeRange>(this, R.layout.list_item, list)); ListView lv = getListView(); lv.setTextFilterEnabled(true); |