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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20