--- android/People/src/com/grundfos/android/people/PeopleDatabase.java 2009/08/04 14:14:14 229 +++ android/People/src/com/grundfos/android/people/PeopleDatabase.java 2009/08/05 08:53:33 230 @@ -13,6 +13,8 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteStatement; +import android.os.Handler; +import android.os.Message; import android.util.Log; public class PeopleDatabase { @@ -28,7 +30,7 @@ { super(context, DATABASE_NAME, null, DATABASE_VERSION); } - + @Override public void onCreate(SQLiteDatabase db) { db.beginTransaction(); @@ -58,7 +60,7 @@ } PeopleDatabaseHelper dbHelper; - + public PeopleDatabase(Context context) { dbHelper = new PeopleDatabaseHelper(context); } @@ -135,18 +137,36 @@ } } - private void loadData(String data) + + class DataLoader { - SQLiteDatabase db = dbHelper.getWritableDatabase(); - db.beginTransaction(); - db.execSQL("DELETE FROM people"); - StringTokenizer lines = new StringTokenizer( data, "|" ); - - SQLiteStatement stmt = db.compileStatement("INSERT INTO people (name,inits,title,dept,company,phone,cell,email) VALUES (?,?,?,?,?,?,?,?);"); - while (lines.hasMoreTokens()) - { - String line = lines.nextToken(); + SQLiteDatabase db; + Handler handler; + int count = 0; + int maxCount = 30000; + SQLiteStatement stmt; + + DataLoader(Handler hndl) { + handler = hndl; + } + + private void sendCount() { + Message msg = Message.obtain(); + msg.what = 1; + msg.arg1 = count; + handler.sendMessage(msg); + } + + private void sendMaxCount() + { + Message msg = Message.obtain(); + msg.what = 0; + msg.arg1 = maxCount; + handler.sendMessage(msg); + } + private void insertLine(String line) + { try { StringTokenizer fields = new StringTokenizer(line, ";"); @@ -162,44 +182,66 @@ } catch (Exception e) { Log.e("PeopleDataBase", "Token error: " + line, e); } - } - stmt.close(); - db.setTransactionSuccessful(); - db.endTransaction(); - db.close(); - } + public void loadData() throws Exception + { + try + { + db = dbHelper.getWritableDatabase(); + db.beginTransaction(); + db.execSQL("DELETE FROM people"); + stmt = db.compileStatement("INSERT INTO people (name,inits,title,dept,company,phone,cell,email) VALUES (?,?,?,?,?,?,?,?);"); + + URL url = new URL("http://t-hoerup.dk/peopledata.txt"); + + URLConnection conn = url.openConnection(); + conn.setConnectTimeout(1000); + InputStream is = conn.getInputStream(); + + BufferedReader input = new BufferedReader( new InputStreamReader(is), 32768); + String line; + + + sendMaxCount(); + while ( (line = input.readLine()) != null) { + if (line.charAt(0) == '#') { //Lines starting with # are comments and should not be parsed by loaddata worker func. + if (line.contains("count:")) { + maxCount = Integer.parseInt(line.substring(line.indexOf(':')+1).trim()); + sendMaxCount(); + } + Log.i("PeopleDB", "Count:"+maxCount); + + } else { + count ++; + if ((count % 100) == 0) + sendCount(); + insertLine(line); + } + } + is.close(); + db.setTransactionSuccessful(); + } finally { + stmt.close(); + db.endTransaction(); + db.close(); + } - public void loadData() throws Exception - { - URL url = new URL("http://t-hoerup.dk/peopledata.txt"); + } + } - URLConnection conn = url.openConnection(); - conn.setConnectTimeout(1000); - InputStream is = conn.getInputStream(); - - BufferedReader input = new BufferedReader( new InputStreamReader(is), 32768); - StringBuilder sb = new StringBuilder(1024000); - String line; - - while ( (line = input.readLine()) != null) { - sb.append(line); - sb.append("\n"); - } - - is.close(); - Log.i("PeopleDatabase", "Parsing data"); - loadData(sb.toString()); - Log.i("PeopleDatabase", "Parsing finished"); + public void loadData(Handler hndl) throws Exception + { + DataLoader dl = new DataLoader(hndl); + dl.loadData(); } - + public void close() { dbHelper.close(); } - + }