--- android/People/src/com/grundfos/android/people/PeopleList.java 2009/08/05 10:56:02 232 +++ android/People/src/com/grundfos/android/people/PeopleList.java 2009/08/05 11:14:04 233 @@ -3,7 +3,6 @@ import java.util.Date; import android.app.AlertDialog; -import android.app.Dialog; import android.app.ListActivity; import android.app.ProgressDialog; import android.content.Context; @@ -12,7 +11,6 @@ import android.content.SharedPreferences; import android.database.Cursor; import android.net.ConnectivityManager; -import android.net.NetworkInfo; import android.os.Bundle; import android.os.Handler; import android.os.Message; @@ -32,119 +30,117 @@ public class PeopleList extends ListActivity { - + public static final int DOWNLOAD = 1010; public static final int ABOUT = 1011; - + public static final int DOWNLOAD_SUCCESS = 0; public static final int DOWNLOAD_FAILED = 1; - + public static final String PREF_LAST_DB_UPDATE = "LastDbUpdate"; public static final String PREFS = "PeoplePrefs"; - + PeopleDatabase peopleDB; - Dialog dialog; + ProgressDialog dialog; SimpleCursorAdapter adapter; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.main); - - - peopleDB = new PeopleDatabase(this); - - - adapter = new SimpleCursorAdapter(this, - R.layout.row, // Use a template - // that displays a - // text view - null , // Give the cursor to the list adatper - new String[] { "name", "inits", "company" }, // Map the NAME column in the - // people database to... - new int[] {R.id.name, R.id.init, R.id.company}); // The "text1" view defined in - // the XML template - - setListAdapter( adapter ) ; - - EditText et = (EditText) findViewById( R.id.entry); - et.setOnEditorActionListener( new OnEditorActionListener() { - public boolean onEditorAction (TextView v, int actionId, KeyEvent event) { - Log.e("Atest", "asd: " + event ); - return true; - } - - }); - et.addTextChangedListener( new TextWatcher() { - public void afterTextChanged(Editable s) { - searchStringChanged(s.toString()); - } - public void onTextChanged(CharSequence s, int start, int before, int count){ - } - public void beforeTextChanged(CharSequence s, int start, int count, int after){ - } - }); - - - if ( peopleDB.getPeopleCount() == 0) - onEmptyDB(); - - - Log.i("PeopleList", "Activity started"); - } - - - - - @Override + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + + + peopleDB = new PeopleDatabase(this); + + + adapter = new SimpleCursorAdapter(this, + R.layout.row, // Use a template + // that displays a + // text view + null , // Give the cursor to the list adatper + new String[] { "name", "inits", "company" }, // Map the NAME column in the + // people database to... + new int[] {R.id.name, R.id.init, R.id.company}); // The "text1" view defined in + // the XML template + + setListAdapter( adapter ) ; + + EditText et = (EditText) findViewById( R.id.entry); + et.setOnEditorActionListener( new OnEditorActionListener() { + public boolean onEditorAction (TextView v, int actionId, KeyEvent event) { + Log.e("Atest", "asd: " + event ); + return true; + } + + }); + et.addTextChangedListener( new TextWatcher() { + public void afterTextChanged(Editable s) { + searchStringChanged(s.toString()); + } + public void onTextChanged(CharSequence s, int start, int before, int count){ + } + public void beforeTextChanged(CharSequence s, int start, int count, int after){ + } + }); + + if ( peopleDB.getPeopleCount() == 0) + onEmptyDB(); + + Log.i("PeopleList", "Activity started"); + } + + + + + @Override protected void finalize() throws Throwable { - peopleDB.close(); + peopleDB.close(); super.finalize(); } public void onEmptyDB() - { + { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Database is empty, download data now?") - .setCancelable(false) - .setPositiveButton("Yes", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.dismiss(); - loadDatabase(); - } - }) - .setNegativeButton("No", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.cancel(); - } - }) - .show(); - } - - public void searchStringChanged(String search) - { - Cursor names = null; - if (search.trim().length() >= 2) - names = peopleDB.getPeopleList(search); - - adapter.changeCursor(names); - adapter.notifyDataSetChanged(); - } - - protected void onListItemClick (ListView l, View v, int position, long id) - { - Intent intent = new Intent(this, PeopleDetails.class); - intent.putExtra("id", id); - startActivity(intent); - } + .setCancelable(false) + .setPositiveButton("Yes", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + loadDatabase(); + } + }) + .setNegativeButton("No", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.cancel(); + } + }) + .show(); + } + + public void searchStringChanged(String search) + { + Cursor names = null; + if (search.trim().length() >= 2) + names = peopleDB.getPeopleList(search); + + adapter.changeCursor(names); + adapter.notifyDataSetChanged(); + } + + protected void onListItemClick (ListView l, View v, int position, long id) + { + Intent intent = new Intent(this, PeopleDetails.class); + intent.putExtra("id", id); + startActivity(intent); + } @Override public boolean onOptionsItemSelected(MenuItem item) { - + switch (item.getItemId()) { case DOWNLOAD: loadDatabase(); @@ -153,7 +149,7 @@ showAbout(); return true; } - + return super.onOptionsItemSelected(item); } @@ -163,10 +159,9 @@ menu.add(0, DOWNLOAD, 0, "Download DB"); menu.add(0, ABOUT, 0, "About"); return true; - } - - + + public void showAbout() { SharedPreferences settings = getSharedPreferences(PREFS, 0); StringBuffer message = new StringBuffer(); @@ -174,54 +169,54 @@ message.append("By Torben Hørup Nielsen\n"); message.append("Database size: ").append(peopleDB.getPeopleCount()).append("\n"); message.append("Last DB update: ").append( settings.getString(PREF_LAST_DB_UPDATE, " - never -")); - + showMessage(message.toString()); } - + public void showMessage(String message) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(message) - .setCancelable(false) - .setPositiveButton("OK", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.dismiss(); - } - }) - .show(); - + .setCancelable(false) + .setPositiveButton("OK", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + } + }) + .show(); + } - + public void loadDatabase() { - ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); - if (cm.getActiveNetworkInfo().getType() != ConnectivityManager.TYPE_WIFI) { - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setMessage("You are not connected to a WiFi network. It is not recommended to load a DB via mobile network") - .setCancelable(false) - .setPositiveButton("Continue", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.dismiss(); - startLoadDatabase(); - } - }) - .setNegativeButton("Abort", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.cancel(); - } - }) - .show(); - } else { - startLoadDatabase(); - } + ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); + if (cm.getActiveNetworkInfo().getType() != ConnectivityManager.TYPE_WIFI) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setMessage("You are not connected to a WiFi network. It is not recommended to load a DB via mobile network") + .setCancelable(false) + .setPositiveButton("Continue", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + startLoadDatabase(); + } + }) + .setNegativeButton("Abort", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.cancel(); + } + }) + .show(); + } else { + startLoadDatabase(); + } } - + public void startLoadDatabase() { dialog = new ProgressDialog(this); - ((ProgressDialog)dialog).setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); - ((ProgressDialog)dialog).setMessage("Loading..."); + dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + dialog.setMessage("Loading..."); dialog.setCancelable(false); dialog.show(); @@ -231,52 +226,49 @@ private Handler progressHandler = new Handler() { public void handleMessage(Message msg) { - ProgressDialog dlg = (ProgressDialog) dialog; if (msg.what == 0) - dlg.setMax( msg.arg1 ); + dialog.setMax( msg.arg1 ); else - dlg.setProgress(msg.arg1); + dialog.setProgress(msg.arg1); } }; - - private Handler dbUpdateHandler = new Handler() { - @Override - public void handleMessage(Message msg) { - dialog.dismiss(); - if (msg.what == DOWNLOAD_SUCCESS) { - Date d = new Date(); - - SharedPreferences settings = getSharedPreferences(PREFS, 0); - - SharedPreferences.Editor ed = settings.edit(); - ed.putString(PREF_LAST_DB_UPDATE, d.toLocaleString()); - ed.commit(); - } else { - showMessage("Download failed"); - - } - } - }; + private Handler dbUpdateHandler = new Handler() { + + @Override + public void handleMessage(Message msg) { + dialog.dismiss(); + if (msg.what == DOWNLOAD_SUCCESS) { + Date d = new Date(); + + SharedPreferences settings = getSharedPreferences(PREFS, 0); + + SharedPreferences.Editor ed = settings.edit(); + ed.putString(PREF_LAST_DB_UPDATE, d.toLocaleString()); + ed.commit(); + } else { + showMessage("Download failed"); + + } + } + }; - Runnable reloadDB = new Runnable() { + Runnable reloadDB = new Runnable() { public void run() { PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); try { - - wl.acquire(); - + wl.acquire(); peopleDB.loadData(progressHandler); - dbUpdateHandler.sendEmptyMessage(DOWNLOAD_SUCCESS); + dbUpdateHandler.sendEmptyMessage(DOWNLOAD_SUCCESS); } catch (Exception e) { Log.e("PeopleList", "reloadDB", e); dbUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILED); } finally { - wl.release(); + wl.release(); } } - }; + }; } \ No newline at end of file