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

  ViewVC Help
Powered by ViewVC 1.1.20