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