package com.grundfos.android.people; import java.util.Date; import android.app.AlertDialog; import android.app.ListActivity; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.net.ConnectivityManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.PowerManager; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; 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; 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 protected void finalize() throws Throwable { 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); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case DOWNLOAD: loadDatabase(); return true; case ABOUT: showAbout(); return true; } return super.onOptionsItemSelected(item); } @Override public boolean onCreateOptionsMenu(Menu menu) { 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(); message.append("Grundfos People v0.1\n"); 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(); } 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(); } } public void startLoadDatabase() { dialog = new ProgressDialog(this); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setMessage("Loading..."); dialog.setCancelable(false); dialog.show(); Thread t = new Thread(reloadDB); t.start(); } private Handler progressHandler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 0) dialog.setMax( msg.arg1 ); else 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"); } } }; 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(); peopleDB.loadData(progressHandler); dbUpdateHandler.sendEmptyMessage(DOWNLOAD_SUCCESS); } catch (Exception e) { Log.e("PeopleList", "reloadDB", e); dbUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILED); } finally { wl.release(); } } }; }