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.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 |
|
34 |
public static final int DOWNLOAD = 1010; |
35 |
public static final int ABOUT = 1011; |
36 |
|
37 |
public static final int DOWNLOAD_SUCCESS = 0; |
38 |
public static final int DOWNLOAD_FAILED = 1; |
39 |
|
40 |
public static final String PREF_LAST_DB_UPDATE = "LastDbUpdate"; |
41 |
public static final String PREFS = "PeoplePrefs"; |
42 |
|
43 |
PeopleDatabase peopleDB; |
44 |
|
45 |
ProgressDialog dialog; |
46 |
SimpleCursorAdapter adapter; |
47 |
|
48 |
EditText searchWidget; |
49 |
|
50 |
@Override |
51 |
public void onCreate(Bundle savedInstanceState) { |
52 |
super.onCreate(savedInstanceState); |
53 |
setContentView(R.layout.main); |
54 |
|
55 |
|
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 |
searchWidget = (EditText) findViewById( R.id.entry); |
72 |
searchWidget.addTextChangedListener( new TextWatcher() { |
73 |
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 |
protected void finalize() throws Throwable { |
93 |
peopleDB.close(); |
94 |
super.finalize(); |
95 |
} |
96 |
|
97 |
|
98 |
|
99 |
public void onEmptyDB() |
100 |
{ |
101 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
102 |
builder.setMessage("Database is empty, download data now?") |
103 |
.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 |
|
118 |
public void searchStringChanged(String search) |
119 |
{ |
120 |
Cursor names = null; |
121 |
if (search.trim().length() >= 2) |
122 |
names = peopleDB.getPeopleList(search); |
123 |
|
124 |
adapter.changeCursor(names); |
125 |
adapter.notifyDataSetChanged(); |
126 |
} |
127 |
|
128 |
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 |
@Override |
137 |
public boolean onOptionsItemSelected(MenuItem item) { |
138 |
|
139 |
switch (item.getItemId()) { |
140 |
case DOWNLOAD: |
141 |
loadDatabase(); |
142 |
return true; |
143 |
case ABOUT: |
144 |
showAbout(); |
145 |
return true; |
146 |
} |
147 |
|
148 |
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 |
} |
158 |
|
159 |
|
160 |
public void showAbout() { |
161 |
SharedPreferences settings = getSharedPreferences(PREFS, 0); |
162 |
StringBuffer message = new StringBuffer(); |
163 |
message.append("Grundfos People v0.1\n"); |
164 |
message.append("By Torben Hørup Nielsen\n"); |
165 |
message.append("Database size: ").append(peopleDB.getPeopleCount()).append("\n"); |
166 |
message.append("Last DB update: ").append( settings.getString(PREF_LAST_DB_UPDATE, " - never -")); |
167 |
|
168 |
showMessage(message.toString()); |
169 |
|
170 |
} |
171 |
|
172 |
public void showMessage(String message) { |
173 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
174 |
builder.setMessage(message) |
175 |
.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 |
} |
184 |
|
185 |
public void loadDatabase() |
186 |
{ |
187 |
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 |
} |
208 |
|
209 |
|
210 |
public void startLoadDatabase() |
211 |
{ |
212 |
dialog = new ProgressDialog(this); |
213 |
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); |
214 |
dialog.setMessage("Loading..."); |
215 |
dialog.setCancelable(false); |
216 |
dialog.show(); |
217 |
|
218 |
Thread t = new Thread(reloadDB); |
219 |
t.start(); |
220 |
} |
221 |
|
222 |
private Handler progressHandler = new Handler() { |
223 |
public void handleMessage(Message msg) { |
224 |
if (msg.what == 0) |
225 |
dialog.setMax( msg.arg1 ); |
226 |
else |
227 |
dialog.setProgress(msg.arg1); |
228 |
} |
229 |
}; |
230 |
|
231 |
private Handler dbUpdateHandler = new Handler() { |
232 |
|
233 |
@Override |
234 |
public void handleMessage(Message msg) { |
235 |
dialog.dismiss(); |
236 |
if (msg.what == DOWNLOAD_SUCCESS) { |
237 |
Date d = new Date(); |
238 |
|
239 |
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 |
|
245 |
searchStringChanged(searchWidget.getText().toString()); //re-run the search after DB reload |
246 |
|
247 |
} else { |
248 |
showMessage("Download failed"); |
249 |
|
250 |
} |
251 |
} |
252 |
}; |
253 |
|
254 |
Runnable reloadDB = new Runnable() { |
255 |
public void run() { |
256 |
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
257 |
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); |
258 |
try |
259 |
{ |
260 |
wl.acquire(); |
261 |
|
262 |
peopleDB.loadData(progressHandler); |
263 |
dbUpdateHandler.sendEmptyMessage(DOWNLOAD_SUCCESS); |
264 |
} catch (Exception e) { |
265 |
Log.e("PeopleList", "reloadDB", e); |
266 |
dbUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILED); |
267 |
} finally { |
268 |
wl.release(); |
269 |
} |
270 |
} |
271 |
}; |
272 |
} |