From cd58d91f8befc8536e94f4e5b4f55f50bea05c33 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Sun, 25 Apr 2010 17:30:26 +0200 Subject: Fix various brokennesses. Populate database only when necessary. I had forgotten to call db.setTransactionSuccessful() (but had failed to notice that because the database was already populated by my previous test). --- .../madore/android/unicodeMap/UnicodeDatabase.java | 39 +++++++++++---- .../android/unicodeMap/UnicodeMapActivity.java | 56 ++++++++++++---------- 2 files changed, 60 insertions(+), 35 deletions(-) (limited to 'src/org/madore') diff --git a/src/org/madore/android/unicodeMap/UnicodeDatabase.java b/src/org/madore/android/unicodeMap/UnicodeDatabase.java index c40cf67..b81c99a 100644 --- a/src/org/madore/android/unicodeMap/UnicodeDatabase.java +++ b/src/org/madore/android/unicodeMap/UnicodeDatabase.java @@ -25,7 +25,9 @@ public class UnicodeDatabase { protected static final int DATABASE_VERSION = 1; protected static final String UNICODE_TABLE_NAME = "unicode"; - protected static class DatabaseInit extends SQLiteOpenHelper { + protected boolean needPopulate; + + protected class DatabaseInit extends SQLiteOpenHelper { final Context context; @@ -40,6 +42,7 @@ public class UnicodeDatabase { +"id INTEGER PRIMARY KEY , " +"name TEXT , " +"category TEXT )"); + UnicodeDatabase.this.needPopulate = true; } @Override @@ -59,10 +62,22 @@ public class UnicodeDatabase { this.activity = activity; this.dbinit = new DatabaseInit(activity); db = dbinit.getReadableDatabase(); + if ( ! this.needPopulate ) { + Cursor c = db.rawQuery("SELECT count(*) FROM "+UNICODE_TABLE_NAME, null); + c.moveToFirst(); + int lineCnt = c.getInt(0); + c.close(); + if ( lineCnt == 0 ) + this.needPopulate = true; + } } - protected void reportProgress(Handler progressHandler, - int done, int total) { + public boolean needPopulate() { + return this.needPopulate; + } + + protected static void reportProgress(Handler progressHandler, + int done, int total) { Message msg = progressHandler.obtainMessage(); Bundle b = new Bundle(); b.putInt("done", done); @@ -113,6 +128,8 @@ public class UnicodeDatabase { } rd.close(); } + db.setTransactionSuccessful(); + this.needPopulate = false; } catch (UnsupportedEncodingException e) { throw new AssertionError("US-ASCII encoding unsupported"); } catch (IOException e) { @@ -144,15 +161,12 @@ public class UnicodeDatabase { return ch; } - protected final static String[] countColumns = { "count(*)" }; - public int countRange(int from, int to) { final Cursor c - = db.query(UNICODE_TABLE_NAME, countColumns, - "id >= ? AND id < ?", - new String[] { Integer.toString(from), - Integer.toString(to) }, - null, null, null, null); + = db.rawQuery("SELECT count(*) FROM "+UNICODE_TABLE_NAME + +" WHERE id >= ? AND id < ?", + new String[] { Integer.toString(from), + Integer.toString(to) }); if ( c.getCount() != 1 ) throw new AssertionError("\"SELECT count(*)\" returned no result"); c.moveToFirst(); @@ -207,4 +221,9 @@ public class UnicodeDatabase { return new CursorIterable(c); } + public void close() { + db.close(); + db = null; + } + } diff --git a/src/org/madore/android/unicodeMap/UnicodeMapActivity.java b/src/org/madore/android/unicodeMap/UnicodeMapActivity.java index a1917a4..cedffb8 100644 --- a/src/org/madore/android/unicodeMap/UnicodeMapActivity.java +++ b/src/org/madore/android/unicodeMap/UnicodeMapActivity.java @@ -39,31 +39,37 @@ 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.setCancelable(false); - 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.setMax(total); - progress.setProgress(done); - if ( done >= total ) - progress.dismiss(); - } - }; - final Thread thr = new Thread() { - public void run() { - UnicodeDatabase db - = new UnicodeDatabase(UnicodeMapActivity.this); - db.populate(handler); - } - }; - thr.start(); + android.util.Log.v("UnicodeMapActivity", "creating activity"); + final UnicodeDatabase db = new UnicodeDatabase(this); + final boolean needPopulate = db.needPopulate(); + db.close(); + if ( needPopulate ) { + final ProgressDialog progress = new ProgressDialog(this); + progress.setOwnerActivity(this); + progress.setTitle("Loading Unicode database"); + progress.setMessage("Please wait"); + progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + progress.setCancelable(false); + 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.setMax(total); + progress.setProgress(done); + if ( done >= total ) + progress.dismiss(); + } + }; + final Thread thr = new Thread() { + public void run() { + final UnicodeDatabase db + = new UnicodeDatabase(UnicodeMapActivity.this); + db.populate(handler); + } + }; + thr.start(); + } setListAdapter(new ArrayAdapter(this, R.layout.list_item, list)); ListView lv = getListView(); lv.setTextFilterEnabled(true); -- cgit v1.2.3