/[projects]/android/People/src/com/grundfos/android/people/PeopleList.java
ViewVC logotype

Diff of /android/People/src/com/grundfos/android/people/PeopleList.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 232 by torben, Wed Aug 5 10:56:02 2009 UTC revision 233 by torben, Wed Aug 5 11:14:04 2009 UTC
# Line 3  package com.grundfos.android.people; Line 3  package com.grundfos.android.people;
3  import java.util.Date;  import java.util.Date;
4    
5  import android.app.AlertDialog;  import android.app.AlertDialog;
 import android.app.Dialog;  
6  import android.app.ListActivity;  import android.app.ListActivity;
7  import android.app.ProgressDialog;  import android.app.ProgressDialog;
8  import android.content.Context;  import android.content.Context;
# Line 12  import android.content.Intent; Line 11  import android.content.Intent;
11  import android.content.SharedPreferences;  import android.content.SharedPreferences;
12  import android.database.Cursor;  import android.database.Cursor;
13  import android.net.ConnectivityManager;  import android.net.ConnectivityManager;
 import android.net.NetworkInfo;  
14  import android.os.Bundle;  import android.os.Bundle;
15  import android.os.Handler;  import android.os.Handler;
16  import android.os.Message;  import android.os.Message;
# Line 32  import android.widget.TextView.OnEditorA Line 30  import android.widget.TextView.OnEditorA
30    
31  public class PeopleList extends ListActivity {  public class PeopleList extends ListActivity {
32    
33            
34          public static final int DOWNLOAD = 1010;          public static final int DOWNLOAD = 1010;
35          public static final int ABOUT = 1011;          public static final int ABOUT = 1011;
36            
37          public static final int DOWNLOAD_SUCCESS = 0;          public static final int DOWNLOAD_SUCCESS = 0;
38          public static final int DOWNLOAD_FAILED = 1;          public static final int DOWNLOAD_FAILED = 1;
39            
40          public static final String PREF_LAST_DB_UPDATE = "LastDbUpdate";          public static final String PREF_LAST_DB_UPDATE = "LastDbUpdate";
41          public static final String PREFS = "PeoplePrefs";          public static final String PREFS = "PeoplePrefs";
42            
43          PeopleDatabase peopleDB;          PeopleDatabase peopleDB;
44    
45          Dialog dialog;          ProgressDialog dialog;
46          SimpleCursorAdapter adapter;          SimpleCursorAdapter adapter;
47        
48      @Override          @Override
49      public void onCreate(Bundle savedInstanceState) {          public void onCreate(Bundle savedInstanceState) {
50          super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
51          setContentView(R.layout.main);                  setContentView(R.layout.main);
52            
53            
54          peopleDB = new PeopleDatabase(this);                  peopleDB = new PeopleDatabase(this);
55            
56            
57          adapter = new SimpleCursorAdapter(this,                  adapter = new SimpleCursorAdapter(this,
58                      R.layout.row, // Use a template                                  R.layout.row, // Use a template
59                                                            // that displays a                                  // that displays a
60                                                            // text view                                  // text view
61                      null , // Give the cursor to the list adatper                                  null , // Give the cursor to the list adatper
62                      new String[] { "name", "inits", "company" }, // Map the NAME column in the                                  new String[] { "name", "inits", "company" }, // Map the NAME column in the
63                                                           // people database to...                                  // people database to...
64                      new int[] {R.id.name, R.id.init, R.id.company}); // The "text1" view defined in                                  new int[] {R.id.name, R.id.init, R.id.company}); // The "text1" view defined in
65                                                       // the XML template                  // the XML template
66    
67           setListAdapter( adapter ) ;                  setListAdapter( adapter ) ;
68            
69          EditText et = (EditText) findViewById( R.id.entry);                  EditText et = (EditText) findViewById( R.id.entry);
70          et.setOnEditorActionListener( new OnEditorActionListener() {                  et.setOnEditorActionListener( new OnEditorActionListener() {
71                  public boolean  onEditorAction  (TextView v, int actionId, KeyEvent event) {                          public boolean  onEditorAction  (TextView v, int actionId, KeyEvent event) {
72                          Log.e("Atest", "asd: " + event );                                  Log.e("Atest", "asd: " + event );
73                          return true;                                  return true;
74                  }                          }
75                    
76          });                  });
77          et.addTextChangedListener( new TextWatcher() {                  et.addTextChangedListener( new TextWatcher() {
78                  public void afterTextChanged(Editable s) {                          public void afterTextChanged(Editable s) {
79                          searchStringChanged(s.toString());                                  searchStringChanged(s.toString());
80                  }                          }
81                  public void onTextChanged(CharSequence s, int start, int before, int count){                          public void onTextChanged(CharSequence s, int start, int before, int count){
82                  }                          }
83                  public void beforeTextChanged(CharSequence s, int start, int count, int after){                          public void beforeTextChanged(CharSequence s, int start, int count, int after){
84                  }                          }
85          });                  });
86            
87                            if ( peopleDB.getPeopleCount() == 0)
88          if ( peopleDB.getPeopleCount() == 0)                          onEmptyDB();
89                  onEmptyDB();  
90                            Log.i("PeopleList", "Activity started");        
91                    }
92          Log.i("PeopleList", "Activity started");          
93      }  
94        
95    
96                @Override
       
     @Override  
97          protected void finalize() throws Throwable {          protected void finalize() throws Throwable {
98          peopleDB.close();                  peopleDB.close();
99                  super.finalize();                  super.finalize();
100          }          }
101    
102    
103    
104          public void onEmptyDB()          public void onEmptyDB()
105      {          {
106                  AlertDialog.Builder builder = new AlertDialog.Builder(this);                  AlertDialog.Builder builder = new AlertDialog.Builder(this);
107                  builder.setMessage("Database is empty, download data now?")                  builder.setMessage("Database is empty, download data now?")
108                         .setCancelable(false)                  .setCancelable(false)
109                         .setPositiveButton("Yes", new DialogInterface.OnClickListener() {                  .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
110                             public void onClick(DialogInterface dialog, int id) {                          public void onClick(DialogInterface dialog, int id) {
111                                  dialog.dismiss();                                  dialog.dismiss();
112                                  loadDatabase();                                  loadDatabase();
113                             }                          }
114                         })                  })
115                         .setNegativeButton("No", new DialogInterface.OnClickListener() {                  .setNegativeButton("No", new DialogInterface.OnClickListener() {
116                             public void onClick(DialogInterface dialog, int id) {                          public void onClick(DialogInterface dialog, int id) {
117                                  dialog.cancel();                                  dialog.cancel();
118                             }                          }
119                         })                  })
120                         .show();                      .show();    
121      }          }
122        
123      public void searchStringChanged(String search)          public void searchStringChanged(String search)
124      {          {
125          Cursor names = null;                  Cursor names = null;
126          if (search.trim().length() >= 2)                  if (search.trim().length() >= 2)
127                  names = peopleDB.getPeopleList(search);                          names = peopleDB.getPeopleList(search);
128    
129          adapter.changeCursor(names);                  adapter.changeCursor(names);
130          adapter.notifyDataSetChanged();                  adapter.notifyDataSetChanged();
131      }          }
132        
133      protected void  onListItemClick  (ListView l, View v, int position, long id)          protected void  onListItemClick  (ListView l, View v, int position, long id)
134      {          {
135          Intent intent = new Intent(this, PeopleDetails.class);                  Intent intent = new Intent(this, PeopleDetails.class);
136          intent.putExtra("id", id);                  intent.putExtra("id", id);
137          startActivity(intent);                  startActivity(intent);
138      }          }
139    
140    
141          @Override          @Override
142          public boolean onOptionsItemSelected(MenuItem item) {          public boolean onOptionsItemSelected(MenuItem item) {
143                    
144                  switch (item.getItemId()) {                  switch (item.getItemId()) {
145                  case DOWNLOAD:                  case DOWNLOAD:
146                          loadDatabase();                          loadDatabase();
# Line 153  public class PeopleList extends ListActi Line 149  public class PeopleList extends ListActi
149                          showAbout();                          showAbout();
150                          return true;                          return true;
151                  }                  }
152                    
153                  return super.onOptionsItemSelected(item);                  return super.onOptionsItemSelected(item);
154          }          }
155    
# Line 163  public class PeopleList extends ListActi Line 159  public class PeopleList extends ListActi
159                  menu.add(0, DOWNLOAD, 0, "Download DB");                  menu.add(0, DOWNLOAD, 0, "Download DB");
160                  menu.add(0, ABOUT, 0, "About");                  menu.add(0, ABOUT, 0, "About");
161                  return true;                  return true;
   
162          }          }
163            
164            
165          public void showAbout() {          public void showAbout() {
166                  SharedPreferences settings = getSharedPreferences(PREFS, 0);                  SharedPreferences settings = getSharedPreferences(PREFS, 0);
167                  StringBuffer message = new StringBuffer();                  StringBuffer message = new StringBuffer();
# Line 174  public class PeopleList extends ListActi Line 169  public class PeopleList extends ListActi
169                  message.append("By Torben Hørup Nielsen\n");                  message.append("By Torben Hørup Nielsen\n");
170                  message.append("Database size: ").append(peopleDB.getPeopleCount()).append("\n");                  message.append("Database size: ").append(peopleDB.getPeopleCount()).append("\n");
171                  message.append("Last DB update: ").append( settings.getString(PREF_LAST_DB_UPDATE, " - never -"));                  message.append("Last DB update: ").append( settings.getString(PREF_LAST_DB_UPDATE, " - never -"));
172                    
173                  showMessage(message.toString());                  showMessage(message.toString());
174    
175          }          }
176            
177          public void showMessage(String message) {          public void showMessage(String message) {
178                  AlertDialog.Builder builder = new AlertDialog.Builder(this);                  AlertDialog.Builder builder = new AlertDialog.Builder(this);
179                  builder.setMessage(message)                  builder.setMessage(message)
180                         .setCancelable(false)                  .setCancelable(false)
181                         .setPositiveButton("OK", new DialogInterface.OnClickListener() {                  .setPositiveButton("OK", new DialogInterface.OnClickListener() {
182                             public void onClick(DialogInterface dialog, int id) {                          public void onClick(DialogInterface dialog, int id) {
183                                  dialog.dismiss();                                  dialog.dismiss();
184                             }                          }
185                         })                  })
186                         .show();                  .show();
187                    
188          }          }
189            
190          public void loadDatabase()          public void loadDatabase()
191          {          {
192          ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);                  ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
193          if (cm.getActiveNetworkInfo().getType() != ConnectivityManager.TYPE_WIFI) {                  if (cm.getActiveNetworkInfo().getType() != ConnectivityManager.TYPE_WIFI) {
194                  AlertDialog.Builder builder = new AlertDialog.Builder(this);                          AlertDialog.Builder builder = new AlertDialog.Builder(this);
195                  builder.setMessage("You are not connected to a WiFi network. It is not recommended to load a DB via mobile network")                          builder.setMessage("You are not connected to a WiFi network. It is not recommended to load a DB via mobile network")
196                         .setCancelable(false)                          .setCancelable(false)
197                         .setPositiveButton("Continue", new DialogInterface.OnClickListener() {                          .setPositiveButton("Continue", new DialogInterface.OnClickListener() {
198                             public void onClick(DialogInterface dialog, int id) {                                  public void onClick(DialogInterface dialog, int id) {
199                                  dialog.dismiss();                                          dialog.dismiss();
200                                  startLoadDatabase();                                          startLoadDatabase();
201                             }                                  }
202                         })                          })
203                         .setNegativeButton("Abort", new DialogInterface.OnClickListener() {                          .setNegativeButton("Abort", new DialogInterface.OnClickListener() {
204                             public void onClick(DialogInterface dialog, int id) {                                  public void onClick(DialogInterface dialog, int id) {
205                                  dialog.cancel();                                          dialog.cancel();
206                             }                                  }
207                         })                          })
208                         .show();                              .show();    
209          } else {                  } else {
210                  startLoadDatabase();                          startLoadDatabase();
211          }                  }
212          }          }
213            
214    
215          public void startLoadDatabase()          public void startLoadDatabase()
216          {          {
217                  dialog = new ProgressDialog(this);                  dialog = new ProgressDialog(this);
218                  ((ProgressDialog)dialog).setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);                  dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
219                  ((ProgressDialog)dialog).setMessage("Loading...");                  dialog.setMessage("Loading...");
220                  dialog.setCancelable(false);                  dialog.setCancelable(false);
221                  dialog.show();                  dialog.show();
222    
# Line 231  public class PeopleList extends ListActi Line 226  public class PeopleList extends ListActi
226    
227          private Handler progressHandler = new Handler() {          private Handler progressHandler = new Handler() {
228                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
                         ProgressDialog dlg = (ProgressDialog) dialog;  
229                          if (msg.what == 0)                          if (msg.what == 0)
230                                  dlg.setMax( msg.arg1 );                                  dialog.setMax( msg.arg1 );
231                          else                          else
232                                  dlg.setProgress(msg.arg1);                                  dialog.setProgress(msg.arg1);
233                  }                  }
234          };          };
           
     private Handler dbUpdateHandler = new Handler() {  
235    
236          @Override          private Handler dbUpdateHandler = new Handler() {
237          public void handleMessage(Message msg) {  
238                  dialog.dismiss();                  @Override
239              if (msg.what == DOWNLOAD_SUCCESS) {                  public void handleMessage(Message msg) {
240                  Date d = new Date();                          dialog.dismiss();
241                                            if (msg.what == DOWNLOAD_SUCCESS) {
242                  SharedPreferences settings = getSharedPreferences(PREFS, 0);                                  Date d = new Date();
243    
244                  SharedPreferences.Editor ed = settings.edit();                                                    SharedPreferences settings = getSharedPreferences(PREFS, 0);
245                  ed.putString(PREF_LAST_DB_UPDATE, d.toLocaleString());  
246                  ed.commit();                                  SharedPreferences.Editor ed = settings.edit();                  
247              } else {                                  ed.putString(PREF_LAST_DB_UPDATE, d.toLocaleString());
248                  showMessage("Download failed");                                  ed.commit();
249                                            } else {
250              }                                  showMessage("Download failed");
251         }  
252      };                          }
253                    }
254            };
255    
256      Runnable reloadDB = new Runnable() {          Runnable reloadDB = new Runnable() {
257                  public void run() {                  public void run() {
258                          PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);                          PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
259                          PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");                          PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
260                          try                          try
261                          {                          {
262                                    wl.acquire();
                                  wl.acquire();  
   
263    
264                                  peopleDB.loadData(progressHandler);                                  peopleDB.loadData(progressHandler);
265                          dbUpdateHandler.sendEmptyMessage(DOWNLOAD_SUCCESS);                                  dbUpdateHandler.sendEmptyMessage(DOWNLOAD_SUCCESS);
266                          } catch (Exception e) {                          } catch (Exception e) {
267                                  Log.e("PeopleList", "reloadDB", e);                                  Log.e("PeopleList", "reloadDB", e);
268                                  dbUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILED);                                  dbUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILED);
269                          } finally {                          } finally {
270                                   wl.release();                                  wl.release();
271                          }                          }
272                  }                  }
273      };          };
274  }  }

Legend:
Removed from v.232  
changed lines
  Added in v.233

  ViewVC Help
Powered by ViewVC 1.1.20