/[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 229 - (hide annotations) (download)
Tue Aug 4 14:14:14 2009 UTC (14 years, 10 months ago) by torben
File size: 6495 byte(s)


1 torben 228 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 torben 229 public static final int DOWNLOAD_SUCCESS = 0;
37     public static final int DOWNLOAD_FAILED = 1;
38    
39 torben 228 public static final String PREF_LAST_DB_UPDATE = "LastDbUpdate";
40     public static final String PREFS = "PeoplePrefs";
41    
42     PeopleDatabase peopleDB;
43    
44     Dialog dialog;
45     SimpleCursorAdapter adapter;
46    
47     @Override
48     public void onCreate(Bundle savedInstanceState) {
49     super.onCreate(savedInstanceState);
50     setContentView(R.layout.main);
51    
52     peopleDB = new PeopleDatabase(this);
53    
54    
55     adapter = new SimpleCursorAdapter(this,
56     R.layout.row, // Use a template
57     // that displays a
58     // text view
59     null , // Give the cursor to the list adatper
60     new String[] { "name", "inits", "company" }, // Map the NAME column in the
61     // people database to...
62     new int[] {R.id.name, R.id.init, R.id.company}); // The "text1" view defined in
63     // the XML template
64    
65     setListAdapter( adapter ) ;
66    
67     EditText et = (EditText) findViewById( R.id.entry);
68     et.setOnEditorActionListener( new OnEditorActionListener() {
69     public boolean onEditorAction (TextView v, int actionId, KeyEvent event) {
70     Log.e("Atest", "asd: " + event );
71     return true;
72     }
73    
74     });
75     et.addTextChangedListener( new TextWatcher() {
76     public void afterTextChanged(Editable s) {
77     searchStringChanged(s.toString());
78     }
79     public void onTextChanged(CharSequence s, int start, int before, int count){
80     }
81     public void beforeTextChanged(CharSequence s, int start, int count, int after){
82     }
83     });
84    
85    
86     if ( peopleDB.getPeopleCount() == 0)
87     onEmptyDB();
88    
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    
166     public void showAbout() {
167     SharedPreferences settings = getSharedPreferences(PREFS, 0);
168     StringBuffer message = new StringBuffer();
169     message.append("Grundfos People v0.1\n");
170     message.append("By Torben Hørup Nielsen\n");
171     message.append("Database size: ").append(peopleDB.getPeopleCount()).append("\n");
172     message.append("Last DB update: ").append( settings.getString(PREF_LAST_DB_UPDATE, " - never -"));
173    
174     showMessage(message.toString());
175    
176     }
177    
178     public void showMessage(String message) {
179     AlertDialog.Builder builder = new AlertDialog.Builder(this);
180     builder.setMessage(message)
181     .setCancelable(false)
182     .setPositiveButton("OK", new DialogInterface.OnClickListener() {
183     public void onClick(DialogInterface dialog, int id) {
184     dialog.dismiss();
185     }
186     })
187     .show();
188    
189     }
190    
191     public void loadDatabase()
192     {
193     dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
194     Thread t = new Thread(reloadDB);
195     t.start();
196     }
197    
198    
199     private Handler dbUpdateHandler = new Handler() {
200    
201     @Override
202     public void handleMessage(Message msg) {
203     dialog.dismiss();
204 torben 229 if (msg.what == DOWNLOAD_SUCCESS) {
205 torben 228 Date d = new Date();
206    
207     SharedPreferences settings = getSharedPreferences(PREFS, 0);
208    
209     SharedPreferences.Editor ed = settings.edit();
210     ed.putString(PREF_LAST_DB_UPDATE, d.toLocaleString());
211     ed.commit();
212     } else {
213     showMessage("Download failed");
214    
215     }
216     }
217     };
218    
219     Runnable reloadDB = new Runnable() {
220     public void run() {
221     try
222     {
223     peopleDB.loadData();
224 torben 229 dbUpdateHandler.sendEmptyMessage(DOWNLOAD_SUCCESS);
225 torben 228 } catch (Exception e) {
226 torben 229 dbUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILED);
227 torben 228 }
228     }
229     };
230     }

  ViewVC Help
Powered by ViewVC 1.1.20