summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid A. Madore <david@procyon.(none)>2010-04-25 17:30:26 +0200
committerDavid A. Madore <david@procyon.(none)>2010-04-25 17:30:26 +0200
commitcd58d91f8befc8536e94f4e5b4f55f50bea05c33 (patch)
tree3336103e6b901dec5e6de264f46567e76dbb0895
parent0e64b9e42047a96e26395002b8287a70f2da2eea (diff)
downloadUnicodeMap-cd58d91f8befc8536e94f4e5b4f55f50bea05c33.tar.gz
UnicodeMap-cd58d91f8befc8536e94f4e5b4f55f50bea05c33.tar.bz2
UnicodeMap-cd58d91f8befc8536e94f4e5b4f55f50bea05c33.zip
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).
-rw-r--r--src/org/madore/android/unicodeMap/UnicodeDatabase.java39
-rw-r--r--src/org/madore/android/unicodeMap/UnicodeMapActivity.java56
2 files changed, 60 insertions, 35 deletions
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<UnicodeRange>(this, R.layout.list_item, list));
ListView lv = getListView();
lv.setTextFilterEnabled(true);