1 | /* |
---|
2 | * rapidandroid - SMS gateway for the android platform |
---|
3 | * Copyright (C) 2009 Dimagi Inc., UNICEF |
---|
4 | * |
---|
5 | * This program is free software: you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU General Public License as published by |
---|
7 | * the Free Software Foundation, either version 3 of the License, or |
---|
8 | * (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | * |
---|
18 | */ |
---|
19 | /** |
---|
20 | * |
---|
21 | */ |
---|
22 | package org.rapidandroid.activity; |
---|
23 | |
---|
24 | import java.text.ParseException; |
---|
25 | import java.util.Calendar; |
---|
26 | import java.util.Date; |
---|
27 | |
---|
28 | import org.rapidandroid.R; |
---|
29 | import org.rapidandroid.content.translation.ModelTranslator; |
---|
30 | import org.rapidandroid.data.controller.DashboardDataLayer; |
---|
31 | import org.rapidandroid.data.controller.MessageDataReporter; |
---|
32 | import org.rapidandroid.data.controller.ParsedDataReporter; |
---|
33 | import org.rapidandroid.view.SingleRowHeaderView; |
---|
34 | import org.rapidandroid.view.adapter.FormDataGridCursorAdapter; |
---|
35 | import org.rapidandroid.view.adapter.MessageCursorAdapter; |
---|
36 | import org.rapidandroid.view.adapter.SummaryCursorAdapter; |
---|
37 | import org.rapidsms.java.core.Constants; |
---|
38 | import org.rapidsms.java.core.model.Form; |
---|
39 | import org.rapidsms.java.core.model.Message; |
---|
40 | |
---|
41 | import android.app.Activity; |
---|
42 | import android.app.AlertDialog; |
---|
43 | import android.app.AlertDialog.Builder; |
---|
44 | import android.content.Intent; |
---|
45 | import android.database.Cursor; |
---|
46 | import android.os.Bundle; |
---|
47 | import android.os.Handler; |
---|
48 | import android.util.DisplayMetrics; |
---|
49 | import android.view.Menu; |
---|
50 | import android.view.MenuItem; |
---|
51 | import android.view.View; |
---|
52 | import android.view.Window; |
---|
53 | import android.view.View.OnClickListener; |
---|
54 | import android.widget.AdapterView; |
---|
55 | import android.widget.ArrayAdapter; |
---|
56 | import android.widget.ImageButton; |
---|
57 | import android.widget.ListView; |
---|
58 | import android.widget.RadioButton; |
---|
59 | import android.widget.Spinner; |
---|
60 | import android.widget.TableLayout; |
---|
61 | import android.widget.TextView; |
---|
62 | import android.widget.ViewSwitcher; |
---|
63 | import android.widget.AdapterView.OnItemClickListener; |
---|
64 | |
---|
65 | /** |
---|
66 | * Main entry point activity for RapidAndroid. It is a simple view with a |
---|
67 | * pulldown for for form type, and a listview of messages below that pertain to |
---|
68 | * that message. |
---|
69 | * |
---|
70 | * @author Daniel Myung [email protected] |
---|
71 | * @created Jan 9, 2009 |
---|
72 | * |
---|
73 | */ |
---|
74 | public class Dashboard extends Activity { |
---|
75 | |
---|
76 | /* |
---|
77 | * (non-Javadoc) |
---|
78 | * |
---|
79 | * @see android.app.Activity#onRestart() |
---|
80 | */ |
---|
81 | @Override |
---|
82 | protected void onRestart() { |
---|
83 | // TODO Auto-generated method stub |
---|
84 | super.onRestart(); |
---|
85 | } |
---|
86 | |
---|
87 | /* |
---|
88 | * (non-Javadoc) |
---|
89 | * |
---|
90 | * @see android.app.Activity#onResume() |
---|
91 | */ |
---|
92 | @Override |
---|
93 | protected void onResume() { |
---|
94 | // TODO Auto-generated method stub |
---|
95 | super.onResume(); |
---|
96 | } |
---|
97 | |
---|
98 | private SingleRowHeaderView headerView; |
---|
99 | private SummaryCursorAdapter summaryView; |
---|
100 | private FormDataGridCursorAdapter rowView; |
---|
101 | private MessageCursorAdapter messageCursorAdapter; |
---|
102 | |
---|
103 | private ViewSwitcher mViewSwitcher; |
---|
104 | private TableLayout mHeaderTable; |
---|
105 | private int traceCount = 0; |
---|
106 | |
---|
107 | // private ProgressDialog mLoadingDialog; |
---|
108 | |
---|
109 | private Form mChosenForm = null; |
---|
110 | private boolean mShowAllMessages = false; |
---|
111 | |
---|
112 | private static final int ACTIVITY_CREATE = 0; |
---|
113 | private static final int ACTIVITY_FORM_REVIEW = 1; |
---|
114 | // private static final int ACTIVITY_DATERANGE = 2; |
---|
115 | private static final int ACTIVITY_CHARTS = 3; // this and ACTIVITY_CHARTS |
---|
116 | private static final int ACTIVITY_GLOBALSETTINGS = 4; |
---|
117 | |
---|
118 | private static final int MENU_CREATE_ID = Menu.FIRST; |
---|
119 | private static final int MENU_FORM_REVIEW_ID = Menu.FIRST + 1; |
---|
120 | private static final int MENU_CHANGE_DATERANGE = Menu.FIRST + 2; |
---|
121 | private static final int MENU_CHARTS_ID = Menu.FIRST + 3; |
---|
122 | private static final int MENU_GLOBAL_SETTINGS = Menu.FIRST + 4; |
---|
123 | // private static final int MENU_SHOW_REPORTS = Menu.FIRST + 3; |
---|
124 | // private static final int MENU_EXIT = Menu.FIRST + 3; //waitaminute, we |
---|
125 | // don't want to exit this thing, do we? |
---|
126 | |
---|
127 | private static final String STATE_DATE_START = "startdate"; |
---|
128 | private static final String STATE_DATE_END = "enddate"; |
---|
129 | private static final String STATE_SPINNER_POSITION = "spinneritem"; |
---|
130 | private static final String STATE_SELECTED_FORM = "selectedform"; |
---|
131 | private static final String STATE_LSV_POSITION = "listposition"; |
---|
132 | private static final String STATE_LSV_VIEWMODE = "viewmode"; |
---|
133 | private static final String STATE_RAD_INDEX = "radselected"; |
---|
134 | |
---|
135 | // private static final int CONTEXT_ITEM_SUMMARY_VIEW = Menu.FIRST; |
---|
136 | // private static final int CONTEXT_ITEM_TABLE_VIEW = Menu.FIRST + 1; |
---|
137 | // private static final int CONTEXT_ITEM_TEST3 = ContextMenu.FIRST + 2; |
---|
138 | // private static final int CONTEXT_ITEM_TEST4 = ContextMenu.FIRST + 3; |
---|
139 | |
---|
140 | private static final int LISTVIEW_MODE_SUMMARY_VIEW = 0; |
---|
141 | private static final int LISTVIEW_MODE_TABLE_VIEW = 1; |
---|
142 | // private static final int LISTVIEW_MODE_SUMMARY_VIEW = 0; |
---|
143 | |
---|
144 | private static final int SHOW_ALL = 5000; |
---|
145 | private static final CharSequence TXT_WAIT = "Please Wait..."; |
---|
146 | |
---|
147 | private int mFormViewMode = LISTVIEW_MODE_TABLE_VIEW; |
---|
148 | private ImageButton mBtnViewModeSwitcher; |
---|
149 | |
---|
150 | private Form[] mAllForms; |
---|
151 | |
---|
152 | boolean mIsInitializing = false; |
---|
153 | boolean resetCursor = true; |
---|
154 | Cursor mListviewCursor = null; |
---|
155 | |
---|
156 | // private Date mStartDate = Constants.NULLDATE; |
---|
157 | // private Date mEndDate = Constants.NULLDATE; |
---|
158 | |
---|
159 | private int mScreenWidth; |
---|
160 | private int mListCount = 100; |
---|
161 | private RadioButton rb100; |
---|
162 | private RadioButton rb500; |
---|
163 | private RadioButton rball; |
---|
164 | |
---|
165 | private OnClickListener radioClickListener = new OnClickListener() { |
---|
166 | |
---|
167 | public void onClick(View v) { |
---|
168 | RadioButton buttonView = (RadioButton) v; |
---|
169 | if (buttonView.equals(rb100)) { |
---|
170 | mListCount = 100; |
---|
171 | rb100.setChecked(true); |
---|
172 | rb500.setChecked(false); |
---|
173 | rball.setChecked(false); |
---|
174 | } else if (buttonView.equals(rb500)) { |
---|
175 | mListCount = 500; |
---|
176 | rb100.setChecked(false); |
---|
177 | rb500.setChecked(true); |
---|
178 | rball.setChecked(false); |
---|
179 | |
---|
180 | } else if (buttonView.equals(rball)) { |
---|
181 | mListCount = SHOW_ALL; |
---|
182 | rb100.setChecked(false); |
---|
183 | rb500.setChecked(false); |
---|
184 | rball.setChecked(true); |
---|
185 | |
---|
186 | } |
---|
187 | if (!mIsInitializing) { |
---|
188 | resetCursor = true; |
---|
189 | beginListViewReload(); |
---|
190 | } |
---|
191 | |
---|
192 | } |
---|
193 | |
---|
194 | }; |
---|
195 | |
---|
196 | @Override |
---|
197 | protected void onCreate(Bundle savedInstanceState) { |
---|
198 | super.onCreate(savedInstanceState); |
---|
199 | requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); |
---|
200 | setTitle("RapidAndroid :: Dashboard"); |
---|
201 | setContentView(R.layout.dashboard); |
---|
202 | |
---|
203 | this.initFormSpinner(); |
---|
204 | // Set the event listeners for the spinner and the listview |
---|
205 | Spinner spin_forms = (Spinner) findViewById(R.id.cbx_forms); |
---|
206 | spin_forms.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { |
---|
207 | public void onItemSelected(AdapterView<?> parent, View theview, int position, long rowid) { |
---|
208 | spinnerItemSelected(position); |
---|
209 | } |
---|
210 | |
---|
211 | public void onNothingSelected(AdapterView<?> parent) { |
---|
212 | // blow away the listview's items |
---|
213 | mChosenForm = null; |
---|
214 | resetCursor = true; |
---|
215 | loadListViewWithFormData(); |
---|
216 | } |
---|
217 | }); |
---|
218 | |
---|
219 | // add some events to the listview |
---|
220 | ListView lsv = (ListView) findViewById(R.id.lsv_dashboardmessages); |
---|
221 | |
---|
222 | DisplayMetrics dm = new DisplayMetrics(); |
---|
223 | getWindowManager().getDefaultDisplay().getMetrics(dm); |
---|
224 | mScreenWidth = dm.widthPixels - 8; |
---|
225 | |
---|
226 | lsv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); |
---|
227 | |
---|
228 | // // bind a context menu |
---|
229 | // lsv.setOnCreateContextMenuListener(new |
---|
230 | // View.OnCreateContextMenuListener() { |
---|
231 | // public void onCreateContextMenu(ContextMenu menu, View v, |
---|
232 | // ContextMenuInfo menuInfo) { |
---|
233 | // if (mChosenForm != null) { |
---|
234 | // menu.add(0, CONTEXT_ITEM_SUMMARY_VIEW, 0, "Summary View"); |
---|
235 | // menu.add(0, CONTEXT_ITEM_TABLE_VIEW, 0, "Table View"); |
---|
236 | // } else { |
---|
237 | // menu.clear(); |
---|
238 | // } |
---|
239 | // } |
---|
240 | // }); |
---|
241 | |
---|
242 | lsv.setOnItemClickListener(new OnItemClickListener() { |
---|
243 | public void onItemClick(AdapterView<?> adapter, View view, int position, long row) { |
---|
244 | if (adapter.getAdapter().getClass().equals(SummaryCursorAdapter.class)) { |
---|
245 | ((SummaryCursorAdapter) adapter.getAdapter()).toggle(position); |
---|
246 | } |
---|
247 | } |
---|
248 | }); |
---|
249 | rb100 = (RadioButton) findViewById(R.id.dashboard_rad_100); |
---|
250 | rb100.setOnClickListener(radioClickListener); |
---|
251 | |
---|
252 | rb500 = (RadioButton) findViewById(R.id.dashboard_rad_500); |
---|
253 | rb500.setOnClickListener(radioClickListener); |
---|
254 | |
---|
255 | rball = (RadioButton) findViewById(R.id.dashboard_rad_all); |
---|
256 | rball.setOnClickListener(radioClickListener); |
---|
257 | |
---|
258 | rb100.setChecked(true); |
---|
259 | |
---|
260 | // by default on startup: |
---|
261 | // mEndDate = new Date(); |
---|
262 | // mStartDate = new Date(); |
---|
263 | // mStartDate.setDate(mEndDate.getDate() - 7); |
---|
264 | |
---|
265 | mViewSwitcher = (ViewSwitcher) findViewById(R.id.dashboard_switcher); |
---|
266 | |
---|
267 | mHeaderTable = (TableLayout) findViewById(R.id.dashboard_headertbl); |
---|
268 | // these animations are too fracking slow |
---|
269 | // Animation in = AnimationUtils.loadAnimation(this, |
---|
270 | // android.R.anim.fade_in); |
---|
271 | // Animation out = AnimationUtils.loadAnimation(this, |
---|
272 | // android.R.anim.fade_out); |
---|
273 | // mViewSwitcher.setInAnimation(in); |
---|
274 | // mViewSwitcher.setOutAnimation(out); |
---|
275 | |
---|
276 | this.mBtnViewModeSwitcher = (ImageButton) findViewById(R.id.btn_switch_mode); |
---|
277 | mBtnViewModeSwitcher.setOnClickListener(new OnClickListener() { |
---|
278 | |
---|
279 | public void onClick(View v) { |
---|
280 | // this is on click, so we want to toggle it! |
---|
281 | switch (mFormViewMode) { |
---|
282 | case LISTVIEW_MODE_SUMMARY_VIEW: |
---|
283 | mFormViewMode = LISTVIEW_MODE_TABLE_VIEW; |
---|
284 | |
---|
285 | break; |
---|
286 | case LISTVIEW_MODE_TABLE_VIEW: |
---|
287 | mFormViewMode = LISTVIEW_MODE_SUMMARY_VIEW; |
---|
288 | |
---|
289 | break; |
---|
290 | } |
---|
291 | resetCursor = false; |
---|
292 | beginListViewReload(); |
---|
293 | } |
---|
294 | }); |
---|
295 | } |
---|
296 | |
---|
297 | /* |
---|
298 | * (non-Javadoc) |
---|
299 | * |
---|
300 | * @see android.app.Activity#onRestoreInstanceState(android.os.Bundle) |
---|
301 | */ |
---|
302 | @Override |
---|
303 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
---|
304 | super.onRestoreInstanceState(savedInstanceState); |
---|
305 | |
---|
306 | if (savedInstanceState != null) { |
---|
307 | if (savedInstanceState.containsKey(STATE_SPINNER_POSITION) |
---|
308 | // && savedInstanceState.containsKey(STATE_LSV_POSITION) |
---|
309 | && savedInstanceState.containsKey(STATE_LSV_VIEWMODE) |
---|
310 | && savedInstanceState.containsKey(STATE_RAD_INDEX) // savedInstanceState.containsKey(STATE_DATE_START) |
---|
311 | // && |
---|
312 | // savedInstanceState.containsKey(STATE_DATE_END) |
---|
313 | // STATE_RAD_COUNT |
---|
314 | // && savedInstanceState.containsKey(STATE_SELECTED_FORM) |
---|
315 | ) { |
---|
316 | |
---|
317 | // mStartDate.setTime(savedInstanceState.getLong(STATE_DATE_START)); |
---|
318 | // mEndDate.setTime(savedInstanceState.getLong(STATE_DATE_END)); |
---|
319 | |
---|
320 | mIsInitializing = true; |
---|
321 | int chosenRadio = savedInstanceState.getInt(STATE_RAD_INDEX); |
---|
322 | if (chosenRadio == 0) { |
---|
323 | rb100.setChecked(true); |
---|
324 | this.mListCount = 100; |
---|
325 | } else if (chosenRadio == 1) { |
---|
326 | rb500.setChecked(true); |
---|
327 | this.mListCount = 500; |
---|
328 | } else if (chosenRadio == 2) { |
---|
329 | rball.setChecked(true); |
---|
330 | this.mListCount = 5000; |
---|
331 | } |
---|
332 | |
---|
333 | mIsInitializing = false; |
---|
334 | mFormViewMode = savedInstanceState.getInt(STATE_LSV_VIEWMODE); |
---|
335 | |
---|
336 | Spinner spin_forms = (Spinner) findViewById(R.id.cbx_forms); |
---|
337 | spin_forms.setSelection(savedInstanceState.getInt(STATE_SPINNER_POSITION)); |
---|
338 | } |
---|
339 | |
---|
340 | // String from = savedInstanceState.getString("from"); |
---|
341 | // String body = savedInstanceState.getString("body"); |
---|
342 | // //dialogMessage = "SMS :: " + from + " : " + body; |
---|
343 | // //showDialog(160); |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | /* |
---|
348 | * (non-Javadoc) |
---|
349 | * |
---|
350 | * @see android.app.Activity#onSaveInstanceState(android.os.Bundle) |
---|
351 | */ |
---|
352 | @Override |
---|
353 | protected void onSaveInstanceState(Bundle outState) { |
---|
354 | // TODO Auto-generated method stub |
---|
355 | super.onSaveInstanceState(outState); |
---|
356 | // outState.putLong(STATE_DATE_START, mStartDate.getTime()); |
---|
357 | // outState.putLong(STATE_DATE_END, mEndDate.getTime()); |
---|
358 | |
---|
359 | int chosenRadio = 0; |
---|
360 | |
---|
361 | if (rb100.isChecked()) { |
---|
362 | chosenRadio = 0; |
---|
363 | } else if (rb500.isChecked()) { |
---|
364 | chosenRadio = 1; |
---|
365 | } else if (rball.isChecked()) { |
---|
366 | chosenRadio = 2; |
---|
367 | } |
---|
368 | outState.putInt(STATE_RAD_INDEX, chosenRadio); |
---|
369 | outState.putInt(STATE_LSV_VIEWMODE, mFormViewMode); |
---|
370 | Spinner spin_forms = (Spinner) findViewById(R.id.cbx_forms); |
---|
371 | outState.putInt(STATE_SPINNER_POSITION, spin_forms.getSelectedItemPosition()); |
---|
372 | |
---|
373 | } |
---|
374 | |
---|
375 | @Override |
---|
376 | protected void onActivityResult(int requestCode, int resultCode, Intent intent) { |
---|
377 | super.onActivityResult(requestCode, resultCode, intent); |
---|
378 | Bundle extras = null; |
---|
379 | if (intent != null) { |
---|
380 | extras = intent.getExtras(); // right now this is a case where we |
---|
381 | // don't do much activity back and |
---|
382 | // forth |
---|
383 | } |
---|
384 | |
---|
385 | switch (requestCode) { |
---|
386 | case ACTIVITY_CREATE: |
---|
387 | // we should do an update of the view |
---|
388 | initFormSpinner(); |
---|
389 | resetCursor = true; |
---|
390 | beginListViewReload(); |
---|
391 | break; |
---|
392 | case ACTIVITY_FORM_REVIEW: |
---|
393 | // dialogMessage = "Activity Done"; |
---|
394 | // showDialog(12); |
---|
395 | resetCursor = true; |
---|
396 | beginListViewReload(); |
---|
397 | break; |
---|
398 | case ACTIVITY_CHARTS: |
---|
399 | // dialogMessage = "Activity Done"; |
---|
400 | // showDialog(13); |
---|
401 | resetCursor = true; |
---|
402 | beginListViewReload(); |
---|
403 | break; |
---|
404 | case ACTIVITY_GLOBALSETTINGS: |
---|
405 | resetCursor = true; |
---|
406 | beginListViewReload(); |
---|
407 | break; |
---|
408 | // case ACTIVITY_DATERANGE: |
---|
409 | // if (extras != null) { |
---|
410 | // mStartDate = new |
---|
411 | // Date(extras.getLong(DateRange.ResultParams.RESULT_START_DATE)); |
---|
412 | // mEndDate = new |
---|
413 | // Date(extras.getLong(DateRange.ResultParams.RESULT_END_DATE)); |
---|
414 | // resetCursor = true; |
---|
415 | // beginListViewReload(); |
---|
416 | // |
---|
417 | // } |
---|
418 | // break; |
---|
419 | } |
---|
420 | } |
---|
421 | |
---|
422 | @Override |
---|
423 | public boolean onCreateOptionsMenu(Menu menu) { |
---|
424 | // add images: |
---|
425 | // http://developerlife.com/tutorials/?p=304 |
---|
426 | super.onCreateOptionsMenu(menu); |
---|
427 | menu.add(0, MENU_CREATE_ID, 0, R.string.dashboard_menu_create).setIcon(android.R.drawable.ic_menu_add); |
---|
428 | menu.add(0, MENU_FORM_REVIEW_ID, 0, R.string.dashboard_menu_edit).setIcon(android.R.drawable.ic_menu_agenda); |
---|
429 | // menu.add(0, MENU_CHANGE_DATERANGE, 0, |
---|
430 | // R.string.chart_menu_change_parameters.setIcon(android.R.drawable.ic_menu_recent_history); |
---|
431 | menu.add(0, MENU_CHARTS_ID, 0, R.string.dashboard_menu_view).setIcon(android.R.drawable.ic_menu_sort_by_size); |
---|
432 | menu.add(0, MENU_GLOBAL_SETTINGS, 0, "Change Settings").setIcon(android.R.drawable.ic_menu_preferences); |
---|
433 | // menu.add(0, MENU_SHOW_REPORTS, 0, |
---|
434 | // R.string.dashboard_menu_show_reports); |
---|
435 | return true; |
---|
436 | } |
---|
437 | |
---|
438 | @Override |
---|
439 | public boolean onOptionsItemSelected(MenuItem item) { |
---|
440 | // TODO Auto-generated method stub |
---|
441 | super.onOptionsItemSelected(item); |
---|
442 | switch (item.getItemId()) { |
---|
443 | case MENU_CREATE_ID: |
---|
444 | startActivityFormCreate(); |
---|
445 | return true; |
---|
446 | case MENU_FORM_REVIEW_ID: |
---|
447 | startActivityFormReview(); |
---|
448 | return true; |
---|
449 | |
---|
450 | // case MENU_CHANGE_DATERANGE: |
---|
451 | // startDateRangeActivity(); |
---|
452 | // return true; |
---|
453 | case MENU_CHARTS_ID: |
---|
454 | startActivityChart(); |
---|
455 | return true; |
---|
456 | case MENU_GLOBAL_SETTINGS: |
---|
457 | startActivityGlobalSettings(); |
---|
458 | return true; |
---|
459 | } |
---|
460 | return true; |
---|
461 | } |
---|
462 | |
---|
463 | |
---|
464 | |
---|
465 | @Override |
---|
466 | public boolean onPrepareOptionsMenu(Menu menu) { |
---|
467 | // Flip the enabled status of menu items depending on selection of a |
---|
468 | // form |
---|
469 | super.onPrepareOptionsMenu(menu); |
---|
470 | |
---|
471 | boolean formOptionsEnabled = false; |
---|
472 | if (this.mChosenForm != null) { |
---|
473 | formOptionsEnabled = true; |
---|
474 | } |
---|
475 | |
---|
476 | MenuItem editMenu = menu.findItem(MENU_FORM_REVIEW_ID); |
---|
477 | editMenu.setEnabled(formOptionsEnabled); |
---|
478 | MenuItem viewMenu = menu.findItem(MENU_CHARTS_ID); |
---|
479 | |
---|
480 | return true; |
---|
481 | } |
---|
482 | |
---|
483 | // @Override |
---|
484 | // // http://www.anddev.org/tinytutcontextmenu_for_listview-t4019.html |
---|
485 | // // UGH, things changed from .9 to 1.0 |
---|
486 | // public boolean onContextItemSelected(MenuItem item) { |
---|
487 | // AdapterView.AdapterContextMenuInfo menuInfo = |
---|
488 | // (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); |
---|
489 | // switch (item.getItemId()) { |
---|
490 | // case CONTEXT_ITEM_SUMMARY_VIEW: |
---|
491 | // mFormViewMode = LISTVIEW_MODE_SUMMARY_VIEW; |
---|
492 | // break; |
---|
493 | // case CONTEXT_ITEM_TABLE_VIEW: |
---|
494 | // mFormViewMode = LISTVIEW_MODE_TABLE_VIEW; |
---|
495 | // break; |
---|
496 | // default: |
---|
497 | // return super.onContextItemSelected(item); |
---|
498 | // } |
---|
499 | // this.resetCursor = false; |
---|
500 | // beginListViewReload(); |
---|
501 | // return true; |
---|
502 | // } |
---|
503 | |
---|
504 | /** |
---|
505 | * @deprecated |
---|
506 | */ |
---|
507 | @Deprecated |
---|
508 | private void startActivityDateRange() { |
---|
509 | Intent i = new Intent(this, DateRange.class); |
---|
510 | // Date endDate = java.sql.Date. |
---|
511 | Date endDate = new Date(); |
---|
512 | if (mChosenForm != null) { |
---|
513 | endDate = ParsedDataReporter.getOldestMessageDate(this, mChosenForm); |
---|
514 | if (endDate.equals(Constants.NULLDATE)) { |
---|
515 | Builder noDateDialog = new AlertDialog.Builder(this); |
---|
516 | noDateDialog.setPositiveButton("Ok", null); |
---|
517 | noDateDialog.setTitle("Alert"); |
---|
518 | noDateDialog.setMessage("This form has no messages or data to chart"); |
---|
519 | noDateDialog.show(); |
---|
520 | return; |
---|
521 | } |
---|
522 | } else { |
---|
523 | endDate = MessageDataReporter.getOldestMessageDate(this); |
---|
524 | } |
---|
525 | i.putExtra(DateRange.CallParams.ACTIVITY_ARG_STARTDATE, endDate.getTime()); |
---|
526 | // startActivityForResult(i, ACTIVITY_DATERANGE); |
---|
527 | |
---|
528 | } |
---|
529 | |
---|
530 | /** |
---|
531 | * |
---|
532 | */ |
---|
533 | private void startActivityGlobalSettings() { |
---|
534 | Intent i; |
---|
535 | i = new Intent(this, GlobalSettings.class); |
---|
536 | startActivityForResult(i, ACTIVITY_GLOBALSETTINGS); |
---|
537 | |
---|
538 | } |
---|
539 | |
---|
540 | // Start the form edit/create activity |
---|
541 | private void startActivityFormReview() { |
---|
542 | Intent i; |
---|
543 | i = new Intent(this, FormReviewer.class); |
---|
544 | i.putExtra(FormReviewer.CallParams.REVIEW_FORM, mChosenForm.getFormId()); |
---|
545 | startActivityForResult(i, ACTIVITY_FORM_REVIEW); |
---|
546 | } |
---|
547 | |
---|
548 | // Start the form edit/create activity |
---|
549 | private void startActivityFormCreate() { |
---|
550 | Intent i; |
---|
551 | i = new Intent(this, FormCreator.class); |
---|
552 | startActivityForResult(i, ACTIVITY_CREATE); |
---|
553 | } |
---|
554 | |
---|
555 | private void startActivityChart() { |
---|
556 | // Debug.stopMethodTracing(); |
---|
557 | if (mListviewCursor == null) { |
---|
558 | Builder noDataDialog = new AlertDialog.Builder(this); |
---|
559 | noDataDialog.setPositiveButton("Ok", null); |
---|
560 | noDataDialog.setTitle("Alert"); |
---|
561 | noDataDialog.setMessage("There is no data to chart."); |
---|
562 | noDataDialog.show(); |
---|
563 | return; |
---|
564 | } |
---|
565 | |
---|
566 | Intent i = new Intent(this, ChartData.class); |
---|
567 | Date now = new Date(); |
---|
568 | i.putExtra(ChartData.CallParams.END_DATE, now.getTime()); |
---|
569 | // we want to chart for a form |
---|
570 | if (mChosenForm != null && !mShowAllMessages) { |
---|
571 | Date startDate = ParsedDataReporter.getOldestMessageDate(this, mChosenForm); |
---|
572 | if (startDate.equals(Constants.NULLDATE)) { |
---|
573 | Builder noDateDialog = new AlertDialog.Builder(this); |
---|
574 | noDateDialog.setPositiveButton("Ok", null); |
---|
575 | noDateDialog.setTitle("Alert"); |
---|
576 | noDateDialog.setMessage("This form has no messages or data to chart"); |
---|
577 | noDateDialog.show(); |
---|
578 | return; |
---|
579 | } |
---|
580 | |
---|
581 | if (mListviewCursor.getCount() > 0) { |
---|
582 | mListviewCursor.moveToLast(); |
---|
583 | // int msg_id = |
---|
584 | // mListviewCursor.getInt(Message.COL_PARSED_MESSAGE_ID); |
---|
585 | String datestring = mListviewCursor.getString(mListviewCursor.getColumnCount() |
---|
586 | + Message.COL_JOINED_MESSAGE_TIME); |
---|
587 | |
---|
588 | try { |
---|
589 | startDate = Message.SQLDateFormatter.parse(datestring); |
---|
590 | } catch (ParseException e) { |
---|
591 | // TODO Auto-generated catch block |
---|
592 | e.printStackTrace(); |
---|
593 | } |
---|
594 | // Message m = MessageTranslator.GetMessage(this, msg_id); |
---|
595 | |
---|
596 | } else { |
---|
597 | Calendar startCal = Calendar.getInstance(); |
---|
598 | startCal.add(Calendar.DATE, -7); |
---|
599 | startDate = startCal.getTime(); |
---|
600 | } |
---|
601 | i.putExtra(ChartData.CallParams.START_DATE, startDate.getTime()); |
---|
602 | i.putExtra(ChartData.CallParams.CHART_FORM, mChosenForm.getFormId()); |
---|
603 | } else if (mShowAllMessages) { |
---|
604 | // Chart for messages |
---|
605 | Date startDate = null; |
---|
606 | boolean setDate = false; |
---|
607 | if (mListviewCursor.getCount() > 0) { |
---|
608 | mListviewCursor.moveToLast(); |
---|
609 | try { |
---|
610 | startDate = Message.SQLDateFormatter.parse(mListviewCursor.getString(Message.COL_TIME)); |
---|
611 | } catch (ParseException e) { |
---|
612 | setDate = true; |
---|
613 | } |
---|
614 | } else { |
---|
615 | setDate = true; |
---|
616 | } |
---|
617 | if (setDate) { |
---|
618 | Calendar startCal = Calendar.getInstance(); |
---|
619 | startCal.add(Calendar.DATE, -7); |
---|
620 | startDate = startCal.getTime(); |
---|
621 | } |
---|
622 | |
---|
623 | mListviewCursor.moveToLast(); |
---|
624 | i.putExtra(ChartData.CallParams.START_DATE, startDate.getTime()); |
---|
625 | |
---|
626 | i.putExtra(ChartData.CallParams.CHART_MESSAGES, true); |
---|
627 | } |
---|
628 | |
---|
629 | // i.putExtra(ChartData.CallParams.START_DATE, mStartDate.getTime()); |
---|
630 | // i.putExtra(ChartData.CallParams.END_DATE, mEndDate.getTime()); |
---|
631 | startActivityForResult(i, ACTIVITY_CHARTS); |
---|
632 | } |
---|
633 | |
---|
634 | // This is a call to the DB to get all the forms that this form can support. |
---|
635 | private void initFormSpinner() { |
---|
636 | // The steps: |
---|
637 | // get the spinner control from the layouts |
---|
638 | Spinner spin_forms = (Spinner) findViewById(R.id.cbx_forms); |
---|
639 | // Get an array of forms from the DB |
---|
640 | // in the current iteration, it's mForms |
---|
641 | this.mAllForms = ModelTranslator.getAllForms(); |
---|
642 | |
---|
643 | String[] monitors = new String[mAllForms.length + 1]; |
---|
644 | |
---|
645 | for (int i = 0; i < mAllForms.length; i++) { |
---|
646 | monitors[i] = "Form: " + mAllForms[i].getFormName(); |
---|
647 | } |
---|
648 | |
---|
649 | // add some special selections: |
---|
650 | monitors[monitors.length - 1] = "Show all Messages"; |
---|
651 | // monitors[monitors.length - 1] = "Show Monitors"; |
---|
652 | |
---|
653 | ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, monitors); |
---|
654 | adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
---|
655 | // apply it to the spinner |
---|
656 | spin_forms.setAdapter(adapter); |
---|
657 | } |
---|
658 | |
---|
659 | private void spinnerItemSelected(int position) { |
---|
660 | if (position == mAllForms.length) { |
---|
661 | // if it's forms+1, then it's ALL messages |
---|
662 | mChosenForm = null; |
---|
663 | this.mShowAllMessages = true; |
---|
664 | resetCursor = true; |
---|
665 | beginListViewReload(); |
---|
666 | // loadListViewWithRawMessages(); |
---|
667 | |
---|
668 | } else { |
---|
669 | this.mShowAllMessages = false; |
---|
670 | mChosenForm = mAllForms[position]; |
---|
671 | resetCursor = true; |
---|
672 | beginListViewReload(); |
---|
673 | } |
---|
674 | } |
---|
675 | |
---|
676 | private synchronized void finishListViewReload() { |
---|
677 | if (mListviewCursor == null) { |
---|
678 | return; |
---|
679 | } |
---|
680 | TextView lbl_recents = (TextView) findViewById(R.id.lbl_dashboardmessages); |
---|
681 | |
---|
682 | lbl_recents.setText(this.mListviewCursor.getCount() + " Messages"); |
---|
683 | |
---|
684 | ListView lsv = (ListView) findViewById(R.id.lsv_dashboardmessages); |
---|
685 | |
---|
686 | if (mChosenForm != null && !mShowAllMessages) { |
---|
687 | loadListViewWithFormData(); |
---|
688 | } else if (mShowAllMessages && mChosenForm == null) { |
---|
689 | this.mBtnViewModeSwitcher.setVisibility(View.INVISIBLE); |
---|
690 | this.messageCursorAdapter = new MessageCursorAdapter(this, mListviewCursor); |
---|
691 | lsv.setAdapter(messageCursorAdapter); |
---|
692 | } |
---|
693 | // lsv.setVisibility(View.VISIBLE); |
---|
694 | |
---|
695 | // Debug.stopMethodTracing(); |
---|
696 | } |
---|
697 | |
---|
698 | final Handler mDashboardHandler = new Handler(); |
---|
699 | final Runnable mUpdateResults = new Runnable() { |
---|
700 | public void run() { |
---|
701 | // while(!mIsInitializing) { |
---|
702 | finishListViewReload(); |
---|
703 | mViewSwitcher.showNext(); |
---|
704 | // } |
---|
705 | } |
---|
706 | }; |
---|
707 | |
---|
708 | private synchronized void beginListViewReload() { |
---|
709 | // Debug.startMethodTracing("listview_load" + traceCount++); |
---|
710 | switch (mFormViewMode) { |
---|
711 | case LISTVIEW_MODE_SUMMARY_VIEW: |
---|
712 | mBtnViewModeSwitcher.setImageResource(R.drawable.summaryview); |
---|
713 | break; |
---|
714 | case LISTVIEW_MODE_TABLE_VIEW: |
---|
715 | mBtnViewModeSwitcher.setImageResource(R.drawable.gridview); |
---|
716 | break; |
---|
717 | } |
---|
718 | |
---|
719 | this.mIsInitializing = true; |
---|
720 | TextView lbl_recents = (TextView) findViewById(R.id.lbl_dashboardmessages); |
---|
721 | lbl_recents.setText(TXT_WAIT); |
---|
722 | mViewSwitcher.showNext(); |
---|
723 | resetListAdapters(); |
---|
724 | new Thread(new Runnable() { |
---|
725 | public void run() { |
---|
726 | fillCursorInBackground(); |
---|
727 | mIsInitializing = false; |
---|
728 | // finishListViewReload();//might puke |
---|
729 | mDashboardHandler.post(mUpdateResults); |
---|
730 | } |
---|
731 | }).start(); |
---|
732 | } |
---|
733 | |
---|
734 | private synchronized void fillCursorInBackground() { |
---|
735 | if (mListviewCursor == null) { |
---|
736 | if (mChosenForm != null && !mShowAllMessages) { |
---|
737 | mListviewCursor = DashboardDataLayer.getCursorForFormData(this, mChosenForm, mListCount); |
---|
738 | } else if (mShowAllMessages && mChosenForm == null) { |
---|
739 | mListviewCursor = DashboardDataLayer.getCursorForRawMessages(this, mListCount); |
---|
740 | } |
---|
741 | } |
---|
742 | } |
---|
743 | |
---|
744 | // this is a call to the DB to update the ListView with the messages for a |
---|
745 | // selected form |
---|
746 | private void loadListViewWithFormData() { |
---|
747 | ListView lsv = (ListView) findViewById(R.id.lsv_dashboardmessages); |
---|
748 | this.mBtnViewModeSwitcher.setVisibility(View.VISIBLE); |
---|
749 | if (mChosenForm == null) { |
---|
750 | lsv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, |
---|
751 | new String[] { "Select an item" })); |
---|
752 | } else { |
---|
753 | |
---|
754 | if (mListviewCursor.getCount() == 0) { |
---|
755 | lsv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, |
---|
756 | new String[] { "No data" })); |
---|
757 | return; |
---|
758 | } |
---|
759 | |
---|
760 | /* |
---|
761 | * if we want to get super fancy, we can do a join to make it all |
---|
762 | * accessible in one cursor instead of having to requery select |
---|
763 | * formdata_bednets., |
---|
764 | * rapidandroid_message.message,rapidandroid_message.time from |
---|
765 | * formdata_bednets join rapidandroid_message on |
---|
766 | * (formdata_bednets.message_id = rapidandroid_message._id) |
---|
767 | */ |
---|
768 | |
---|
769 | if (this.mFormViewMode == Dashboard.LISTVIEW_MODE_SUMMARY_VIEW) { |
---|
770 | this.summaryView = new SummaryCursorAdapter(this, mListviewCursor, mChosenForm); |
---|
771 | lsv.setAdapter(summaryView); |
---|
772 | } else if (this.mFormViewMode == Dashboard.LISTVIEW_MODE_TABLE_VIEW) { |
---|
773 | if (this.headerView == null) { |
---|
774 | headerView = new SingleRowHeaderView(this, mChosenForm, mScreenWidth); |
---|
775 | mHeaderTable.addView(headerView); |
---|
776 | int colcount = headerView.getColCount(); |
---|
777 | for (int i = 0; i < colcount; i++) { |
---|
778 | mHeaderTable.setColumnShrinkable(i, true); |
---|
779 | } |
---|
780 | } |
---|
781 | rowView = new FormDataGridCursorAdapter(this, mChosenForm, mListviewCursor, mScreenWidth); |
---|
782 | lsv.setAdapter(rowView); |
---|
783 | } |
---|
784 | } |
---|
785 | |
---|
786 | } |
---|
787 | |
---|
788 | /** |
---|
789 | * @param changedforms |
---|
790 | */ |
---|
791 | private void resetListAdapters() { |
---|
792 | ListView lsv = (ListView) findViewById(R.id.lsv_dashboardmessages); |
---|
793 | |
---|
794 | if (this.headerView != null) { |
---|
795 | mHeaderTable.removeAllViews(); |
---|
796 | headerView = null; |
---|
797 | } |
---|
798 | |
---|
799 | if (rowView != null) { |
---|
800 | |
---|
801 | rowView = null; |
---|
802 | } |
---|
803 | if (summaryView != null) { |
---|
804 | summaryView = null; |
---|
805 | } |
---|
806 | if (messageCursorAdapter != null) { |
---|
807 | messageCursorAdapter = null; |
---|
808 | } |
---|
809 | // monitorCursorAdapter |
---|
810 | |
---|
811 | if (resetCursor) { |
---|
812 | // need to reset the cursor |
---|
813 | if (mListviewCursor != null) { |
---|
814 | mListviewCursor.close(); |
---|
815 | mListviewCursor = null; |
---|
816 | } |
---|
817 | resetCursor = false; |
---|
818 | } |
---|
819 | } |
---|
820 | |
---|
821 | } |
---|