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.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 |
torben |
233 |
|
31 |
torben |
228 |
public static final int DOWNLOAD = 1010; |
32 |
|
|
public static final int ABOUT = 1011; |
33 |
torben |
233 |
|
34 |
torben |
229 |
public static final int DOWNLOAD_SUCCESS = 0; |
35 |
|
|
public static final int DOWNLOAD_FAILED = 1; |
36 |
torben |
233 |
|
37 |
torben |
228 |
public static final String PREF_LAST_DB_UPDATE = "LastDbUpdate"; |
38 |
|
|
public static final String PREFS = "PeoplePrefs"; |
39 |
torben |
233 |
|
40 |
torben |
228 |
PeopleDatabase peopleDB; |
41 |
|
|
|
42 |
torben |
233 |
ProgressDialog dialog; |
43 |
torben |
228 |
SimpleCursorAdapter adapter; |
44 |
|
|
|
45 |
torben |
235 |
EditText searchWidget; |
46 |
|
|
|
47 |
torben |
233 |
@Override |
48 |
|
|
public void onCreate(Bundle savedInstanceState) { |
49 |
|
|
super.onCreate(savedInstanceState); |
50 |
|
|
setContentView(R.layout.main); |
51 |
torben |
228 |
|
52 |
torben |
233 |
|
53 |
|
|
peopleDB = new PeopleDatabase(this); |
54 |
|
|
|
55 |
|
|
|
56 |
|
|
adapter = new SimpleCursorAdapter(this, |
57 |
torben |
259 |
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 |
torben |
233 |
|
62 |
|
|
setListAdapter( adapter ) ; |
63 |
|
|
|
64 |
torben |
235 |
searchWidget = (EditText) findViewById( R.id.entry); |
65 |
|
|
searchWidget.addTextChangedListener( new TextWatcher() { |
66 |
torben |
233 |
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 |
torben |
228 |
protected void finalize() throws Throwable { |
86 |
torben |
233 |
peopleDB.close(); |
87 |
torben |
228 |
super.finalize(); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
public void onEmptyDB() |
93 |
torben |
233 |
{ |
94 |
torben |
228 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
95 |
|
|
builder.setMessage("Database is empty, download data now?") |
96 |
torben |
233 |
.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 |
torben |
228 |
|
111 |
torben |
233 |
public void searchStringChanged(String search) |
112 |
|
|
{ |
113 |
|
|
Cursor names = null; |
114 |
|
|
if (search.trim().length() >= 2) |
115 |
|
|
names = peopleDB.getPeopleList(search); |
116 |
torben |
228 |
|
117 |
torben |
233 |
adapter.changeCursor(names); |
118 |
|
|
adapter.notifyDataSetChanged(); |
119 |
|
|
} |
120 |
torben |
228 |
|
121 |
torben |
233 |
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 |
torben |
228 |
@Override |
130 |
|
|
public boolean onOptionsItemSelected(MenuItem item) { |
131 |
torben |
233 |
|
132 |
torben |
228 |
switch (item.getItemId()) { |
133 |
|
|
case DOWNLOAD: |
134 |
|
|
loadDatabase(); |
135 |
|
|
return true; |
136 |
|
|
case ABOUT: |
137 |
|
|
showAbout(); |
138 |
|
|
return true; |
139 |
|
|
} |
140 |
torben |
233 |
|
141 |
torben |
228 |
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 |
torben |
233 |
} |
151 |
torben |
228 |
|
152 |
torben |
233 |
|
153 |
torben |
228 |
public void showAbout() { |
154 |
|
|
SharedPreferences settings = getSharedPreferences(PREFS, 0); |
155 |
|
|
StringBuffer message = new StringBuffer(); |
156 |
|
|
message.append("Grundfos People v0.1\n"); |
157 |
torben |
232 |
message.append("By Torben Hørup Nielsen\n"); |
158 |
torben |
228 |
message.append("Database size: ").append(peopleDB.getPeopleCount()).append("\n"); |
159 |
|
|
message.append("Last DB update: ").append( settings.getString(PREF_LAST_DB_UPDATE, " - never -")); |
160 |
torben |
233 |
|
161 |
torben |
228 |
showMessage(message.toString()); |
162 |
|
|
|
163 |
|
|
} |
164 |
torben |
233 |
|
165 |
torben |
228 |
public void showMessage(String message) { |
166 |
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
167 |
|
|
builder.setMessage(message) |
168 |
torben |
233 |
.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 |
torben |
228 |
} |
177 |
torben |
233 |
|
178 |
torben |
228 |
public void loadDatabase() |
179 |
|
|
{ |
180 |
torben |
233 |
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 |
torben |
232 |
} |
201 |
|
|
|
202 |
torben |
233 |
|
203 |
torben |
232 |
public void startLoadDatabase() |
204 |
|
|
{ |
205 |
torben |
230 |
dialog = new ProgressDialog(this); |
206 |
torben |
233 |
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); |
207 |
|
|
dialog.setMessage("Loading..."); |
208 |
torben |
230 |
dialog.setCancelable(false); |
209 |
|
|
dialog.show(); |
210 |
|
|
|
211 |
torben |
228 |
Thread t = new Thread(reloadDB); |
212 |
|
|
t.start(); |
213 |
|
|
} |
214 |
|
|
|
215 |
torben |
230 |
private Handler progressHandler = new Handler() { |
216 |
|
|
public void handleMessage(Message msg) { |
217 |
|
|
if (msg.what == 0) |
218 |
torben |
233 |
dialog.setMax( msg.arg1 ); |
219 |
torben |
230 |
else |
220 |
torben |
233 |
dialog.setProgress(msg.arg1); |
221 |
torben |
230 |
} |
222 |
|
|
}; |
223 |
torben |
228 |
|
224 |
torben |
233 |
private Handler dbUpdateHandler = new Handler() { |
225 |
torben |
228 |
|
226 |
torben |
233 |
@Override |
227 |
|
|
public void handleMessage(Message msg) { |
228 |
|
|
dialog.dismiss(); |
229 |
|
|
if (msg.what == DOWNLOAD_SUCCESS) { |
230 |
|
|
Date d = new Date(); |
231 |
torben |
228 |
|
232 |
torben |
233 |
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 |
torben |
235 |
|
238 |
|
|
searchStringChanged(searchWidget.getText().toString()); //re-run the search after DB reload |
239 |
|
|
|
240 |
torben |
233 |
} else { |
241 |
|
|
showMessage("Download failed"); |
242 |
|
|
|
243 |
|
|
} |
244 |
|
|
} |
245 |
|
|
}; |
246 |
|
|
|
247 |
|
|
Runnable reloadDB = new Runnable() { |
248 |
torben |
228 |
public void run() { |
249 |
torben |
231 |
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
250 |
|
|
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); |
251 |
torben |
228 |
try |
252 |
|
|
{ |
253 |
torben |
233 |
wl.acquire(); |
254 |
torben |
231 |
|
255 |
torben |
230 |
peopleDB.loadData(progressHandler); |
256 |
torben |
233 |
dbUpdateHandler.sendEmptyMessage(DOWNLOAD_SUCCESS); |
257 |
torben |
228 |
} catch (Exception e) { |
258 |
torben |
230 |
Log.e("PeopleList", "reloadDB", e); |
259 |
torben |
229 |
dbUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILED); |
260 |
torben |
231 |
} finally { |
261 |
torben |
233 |
wl.release(); |
262 |
torben |
231 |
} |
263 |
torben |
228 |
} |
264 |
torben |
233 |
}; |
265 |
torben |
228 |
} |