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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20