1 |
package com.grundfos.android.people; |
2 |
|
3 |
import java.io.File; |
4 |
import java.util.Date; |
5 |
|
6 |
import android.app.AlertDialog; |
7 |
import android.app.Dialog; |
8 |
import android.app.ListActivity; |
9 |
import android.app.ProgressDialog; |
10 |
import android.content.DialogInterface; |
11 |
import android.content.Intent; |
12 |
import android.content.SharedPreferences; |
13 |
import android.database.Cursor; |
14 |
import android.os.Bundle; |
15 |
import android.os.Handler; |
16 |
import android.os.Message; |
17 |
import android.text.Editable; |
18 |
import android.text.TextWatcher; |
19 |
import android.util.Log; |
20 |
import android.view.KeyEvent; |
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 |
import android.widget.TextView; |
28 |
import android.widget.TextView.OnEditorActionListener; |
29 |
|
30 |
public class PeopleList extends ListActivity { |
31 |
|
32 |
|
33 |
public static final int DOWNLOAD = 1010; |
34 |
public static final int ABOUT = 1011; |
35 |
|
36 |
public static final String PREF_LAST_DB_UPDATE = "LastDbUpdate"; |
37 |
public static final String PREFS = "PeoplePrefs"; |
38 |
|
39 |
PeopleDatabase peopleDB; |
40 |
|
41 |
Dialog dialog; |
42 |
SimpleCursorAdapter adapter; |
43 |
|
44 |
@Override |
45 |
public void onCreate(Bundle savedInstanceState) { |
46 |
super.onCreate(savedInstanceState); |
47 |
setContentView(R.layout.main); |
48 |
|
49 |
peopleDB = new PeopleDatabase(this); |
50 |
|
51 |
|
52 |
adapter = new SimpleCursorAdapter(this, |
53 |
R.layout.row, // Use a template |
54 |
// that displays a |
55 |
// text view |
56 |
null , // Give the cursor to the list adatper |
57 |
new String[] { "name", "inits", "company" }, // Map the NAME column in the |
58 |
// people database to... |
59 |
new int[] {R.id.name, R.id.init, R.id.company}); // The "text1" view defined in |
60 |
// the XML template |
61 |
|
62 |
setListAdapter( adapter ) ; |
63 |
|
64 |
EditText et = (EditText) findViewById( R.id.entry); |
65 |
et.setOnEditorActionListener( new OnEditorActionListener() { |
66 |
public boolean onEditorAction (TextView v, int actionId, KeyEvent event) { |
67 |
Log.e("Atest", "asd: " + event ); |
68 |
return true; |
69 |
} |
70 |
|
71 |
}); |
72 |
et.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 |
|
83 |
if ( peopleDB.getPeopleCount() == 0) |
84 |
onEmptyDB(); |
85 |
|
86 |
|
87 |
Log.i("PeopleList", "Activity started"); |
88 |
} |
89 |
|
90 |
|
91 |
|
92 |
|
93 |
@Override |
94 |
protected void finalize() throws Throwable { |
95 |
peopleDB.close(); |
96 |
super.finalize(); |
97 |
} |
98 |
|
99 |
|
100 |
|
101 |
public void onEmptyDB() |
102 |
{ |
103 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
104 |
builder.setMessage("Database is empty, download data now?") |
105 |
.setCancelable(false) |
106 |
.setPositiveButton("Yes", new DialogInterface.OnClickListener() { |
107 |
public void onClick(DialogInterface dialog, int id) { |
108 |
dialog.dismiss(); |
109 |
loadDatabase(); |
110 |
} |
111 |
}) |
112 |
.setNegativeButton("No", new DialogInterface.OnClickListener() { |
113 |
public void onClick(DialogInterface dialog, int id) { |
114 |
dialog.cancel(); |
115 |
} |
116 |
}) |
117 |
.show(); |
118 |
} |
119 |
|
120 |
public void searchStringChanged(String search) |
121 |
{ |
122 |
Cursor names = null; |
123 |
if (search.trim().length() >= 2) |
124 |
names = peopleDB.getPeopleList(search); |
125 |
|
126 |
adapter.changeCursor(names); |
127 |
adapter.notifyDataSetChanged(); |
128 |
} |
129 |
|
130 |
protected void onListItemClick (ListView l, View v, int position, long id) |
131 |
{ |
132 |
Intent intent = new Intent(this, PeopleDetails.class); |
133 |
intent.putExtra("id", id); |
134 |
startActivity(intent); |
135 |
} |
136 |
|
137 |
|
138 |
@Override |
139 |
public boolean onOptionsItemSelected(MenuItem item) { |
140 |
|
141 |
switch (item.getItemId()) { |
142 |
case DOWNLOAD: |
143 |
loadDatabase(); |
144 |
return true; |
145 |
case ABOUT: |
146 |
showAbout(); |
147 |
return true; |
148 |
} |
149 |
|
150 |
return super.onOptionsItemSelected(item); |
151 |
} |
152 |
|
153 |
|
154 |
@Override |
155 |
public boolean onCreateOptionsMenu(Menu menu) { |
156 |
menu.add(0, DOWNLOAD, 0, "Download DB"); |
157 |
menu.add(0, ABOUT, 0, "About"); |
158 |
return true; |
159 |
|
160 |
} |
161 |
|
162 |
|
163 |
public void showAbout() { |
164 |
SharedPreferences settings = getSharedPreferences(PREFS, 0); |
165 |
StringBuffer message = new StringBuffer(); |
166 |
message.append("Grundfos People v0.1\n"); |
167 |
message.append("By Torben Hørup Nielsen\n"); |
168 |
message.append("Database size: ").append(peopleDB.getPeopleCount()).append("\n"); |
169 |
message.append("Last DB update: ").append( settings.getString(PREF_LAST_DB_UPDATE, " - never -")); |
170 |
|
171 |
showMessage(message.toString()); |
172 |
|
173 |
} |
174 |
|
175 |
public void showMessage(String message) { |
176 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
177 |
builder.setMessage(message) |
178 |
.setCancelable(false) |
179 |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
180 |
public void onClick(DialogInterface dialog, int id) { |
181 |
dialog.dismiss(); |
182 |
} |
183 |
}) |
184 |
.show(); |
185 |
|
186 |
} |
187 |
|
188 |
public void loadDatabase() |
189 |
{ |
190 |
dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true); |
191 |
Thread t = new Thread(reloadDB); |
192 |
t.start(); |
193 |
} |
194 |
|
195 |
|
196 |
private Handler dbUpdateHandler = new Handler() { |
197 |
|
198 |
@Override |
199 |
public void handleMessage(Message msg) { |
200 |
dialog.dismiss(); |
201 |
if (msg.what == 0) { //success |
202 |
Date d = new Date(); |
203 |
|
204 |
SharedPreferences settings = getSharedPreferences(PREFS, 0); |
205 |
|
206 |
SharedPreferences.Editor ed = settings.edit(); |
207 |
ed.putString(PREF_LAST_DB_UPDATE, d.toLocaleString()); |
208 |
ed.commit(); |
209 |
} else { |
210 |
showMessage("Download failed"); |
211 |
|
212 |
} |
213 |
} |
214 |
}; |
215 |
|
216 |
Runnable reloadDB = new Runnable() { |
217 |
public void run() { |
218 |
try |
219 |
{ |
220 |
peopleDB.loadData(); |
221 |
dbUpdateHandler.sendEmptyMessage(0); |
222 |
} catch (Exception e) { |
223 |
dbUpdateHandler.sendEmptyMessage(1); |
224 |
} |
225 |
} |
226 |
}; |
227 |
} |