/[projects]/android/People/src/com/grundfos/android/people/PeopleList.java
ViewVC logotype

Annotation of /android/People/src/com/grundfos/android/people/PeopleList.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 233 - (hide annotations) (download)
Wed Aug 5 11:14:04 2009 UTC (14 years, 9 months ago) by torben
File size: 7130 byte(s)
Minor changes
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.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 torben 233
34 torben 228 public static final int DOWNLOAD = 1010;
35     public static final int ABOUT = 1011;
36 torben 233
37 torben 229 public static final int DOWNLOAD_SUCCESS = 0;
38     public static final int DOWNLOAD_FAILED = 1;
39 torben 233
40 torben 228 public static final String PREF_LAST_DB_UPDATE = "LastDbUpdate";
41     public static final String PREFS = "PeoplePrefs";
42 torben 233
43 torben 228 PeopleDatabase peopleDB;
44    
45 torben 233 ProgressDialog dialog;
46 torben 228 SimpleCursorAdapter adapter;
47    
48 torben 233 @Override
49     public void onCreate(Bundle savedInstanceState) {
50     super.onCreate(savedInstanceState);
51     setContentView(R.layout.main);
52 torben 228
53 torben 233
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 torben 228 protected void finalize() throws Throwable {
98 torben 233 peopleDB.close();
99 torben 228 super.finalize();
100     }
101    
102    
103    
104     public void onEmptyDB()
105 torben 233 {
106 torben 228 AlertDialog.Builder builder = new AlertDialog.Builder(this);
107     builder.setMessage("Database is empty, download data now?")
108 torben 233 .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 torben 228
123 torben 233 public void searchStringChanged(String search)
124     {
125     Cursor names = null;
126     if (search.trim().length() >= 2)
127     names = peopleDB.getPeopleList(search);
128 torben 228
129 torben 233 adapter.changeCursor(names);
130     adapter.notifyDataSetChanged();
131     }
132 torben 228
133 torben 233 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 torben 228 @Override
142     public boolean onOptionsItemSelected(MenuItem item) {
143 torben 233
144 torben 228 switch (item.getItemId()) {
145     case DOWNLOAD:
146     loadDatabase();
147     return true;
148     case ABOUT:
149     showAbout();
150     return true;
151     }
152 torben 233
153 torben 228 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 torben 233 }
163 torben 228
164 torben 233
165 torben 228 public void showAbout() {
166     SharedPreferences settings = getSharedPreferences(PREFS, 0);
167     StringBuffer message = new StringBuffer();
168     message.append("Grundfos People v0.1\n");
169 torben 232 message.append("By Torben Hørup Nielsen\n");
170 torben 228 message.append("Database size: ").append(peopleDB.getPeopleCount()).append("\n");
171     message.append("Last DB update: ").append( settings.getString(PREF_LAST_DB_UPDATE, " - never -"));
172 torben 233
173 torben 228 showMessage(message.toString());
174    
175     }
176 torben 233
177 torben 228 public void showMessage(String message) {
178     AlertDialog.Builder builder = new AlertDialog.Builder(this);
179     builder.setMessage(message)
180 torben 233 .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 torben 228 }
189 torben 233
190 torben 228 public void loadDatabase()
191     {
192 torben 233 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 torben 232 }
213    
214 torben 233
215 torben 232 public void startLoadDatabase()
216     {
217 torben 230 dialog = new ProgressDialog(this);
218 torben 233 dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
219     dialog.setMessage("Loading...");
220 torben 230 dialog.setCancelable(false);
221     dialog.show();
222    
223 torben 228 Thread t = new Thread(reloadDB);
224     t.start();
225     }
226    
227 torben 230 private Handler progressHandler = new Handler() {
228     public void handleMessage(Message msg) {
229     if (msg.what == 0)
230 torben 233 dialog.setMax( msg.arg1 );
231 torben 230 else
232 torben 233 dialog.setProgress(msg.arg1);
233 torben 230 }
234     };
235 torben 228
236 torben 233 private Handler dbUpdateHandler = new Handler() {
237 torben 228
238 torben 233 @Override
239     public void handleMessage(Message msg) {
240     dialog.dismiss();
241     if (msg.what == DOWNLOAD_SUCCESS) {
242     Date d = new Date();
243 torben 228
244 torben 233 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 torben 228 public void run() {
258 torben 231 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
259     PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
260 torben 228 try
261     {
262 torben 233 wl.acquire();
263 torben 231
264 torben 230 peopleDB.loadData(progressHandler);
265 torben 233 dbUpdateHandler.sendEmptyMessage(DOWNLOAD_SUCCESS);
266 torben 228 } catch (Exception e) {
267 torben 230 Log.e("PeopleList", "reloadDB", e);
268 torben 229 dbUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILED);
269 torben 231 } finally {
270 torben 233 wl.release();
271 torben 231 }
272 torben 228 }
273 torben 233 };
274 torben 228 }

  ViewVC Help
Powered by ViewVC 1.1.20