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