Best Android App InstaSquare Lite for full-size publication of photos in Instagram, Facebook, Twitter. Run the android application on PC. You can through the android emulator Droid4X.

root/trunk/rapidandroid/org.rapidandroid/src/org/rapidandroid/activity/Dashboard.java @ 133

Revision 133, 25.4 KB (checked in by dmyung, 10 months ago)

applying GPL license to source

Line 
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 */
22package org.rapidandroid.activity;
23
24import java.text.ParseException;
25import java.util.Calendar;
26import java.util.Date;
27
28import org.rapidandroid.R;
29import org.rapidandroid.content.translation.ModelTranslator;
30import org.rapidandroid.data.controller.DashboardDataLayer;
31import org.rapidandroid.data.controller.MessageDataReporter;
32import org.rapidandroid.data.controller.ParsedDataReporter;
33import org.rapidandroid.view.SingleRowHeaderView;
34import org.rapidandroid.view.adapter.FormDataGridCursorAdapter;
35import org.rapidandroid.view.adapter.MessageCursorAdapter;
36import org.rapidandroid.view.adapter.SummaryCursorAdapter;
37import org.rapidsms.java.core.Constants;
38import org.rapidsms.java.core.model.Form;
39import org.rapidsms.java.core.model.Message;
40
41import android.app.Activity;
42import android.app.AlertDialog;
43import android.app.AlertDialog.Builder;
44import android.content.Intent;
45import android.database.Cursor;
46import android.os.Bundle;
47import android.os.Handler;
48import android.util.DisplayMetrics;
49import android.view.Menu;
50import android.view.MenuItem;
51import android.view.View;
52import android.view.Window;
53import android.view.View.OnClickListener;
54import android.widget.AdapterView;
55import android.widget.ArrayAdapter;
56import android.widget.ImageButton;
57import android.widget.ListView;
58import android.widget.RadioButton;
59import android.widget.Spinner;
60import android.widget.TableLayout;
61import android.widget.TextView;
62import android.widget.ViewSwitcher;
63import 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 *
75 */
76public class Dashboard extends Activity {
77
78        /*
79         * (non-Javadoc)
80         *
81         * @see android.app.Activity#onRestart()
82         */
83        @Override
84        protected void onRestart() {
85                // TODO Auto-generated method stub
86                super.onRestart();
87        }
88
89        /*
90         * (non-Javadoc)
91         *
92         * @see android.app.Activity#onResume()
93         */
94        @Override
95        protected void onResume() {
96                // TODO Auto-generated method stub
97                super.onResume();
98        }
99
100        private SingleRowHeaderView headerView;
101        private SummaryCursorAdapter summaryView;
102        private FormDataGridCursorAdapter rowView;
103        private MessageCursorAdapter messageCursorAdapter;
104
105        private ViewSwitcher mViewSwitcher;
106        private TableLayout mHeaderTable;
107        private int traceCount = 0;
108
109        // private ProgressDialog mLoadingDialog;
110
111        private Form mChosenForm = null;
112        private boolean mShowAllMessages = false;
113
114        private static final int ACTIVITY_CREATE = 0;
115        private static final int ACTIVITY_FORM_REVIEW = 1;
116        // private static final int ACTIVITY_DATERANGE = 2;
117        private static final int ACTIVITY_CHARTS = 3; // this and ACTIVITY_CHARTS
118
119        private static final int MENU_CREATE_ID = Menu.FIRST;
120        private static final int MENU_FORM_REVIEW_ID = Menu.FIRST + 1;
121        private static final int MENU_CHANGE_DATERANGE = Menu.FIRST + 2;
122        private static final int MENU_CHARTS_ID = Menu.FIRST + 3;
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_DATERANGE:
405                        // if (extras != null) {
406                        // mStartDate = new
407                        // Date(extras.getLong(DateRange.ResultParams.RESULT_START_DATE));
408                        // mEndDate = new
409                        // Date(extras.getLong(DateRange.ResultParams.RESULT_END_DATE));
410                        // resetCursor = true;
411                        // beginListViewReload();
412                        //
413                        // }
414                        // break;
415                }
416        }
417
418        @Override
419        public boolean onCreateOptionsMenu(Menu menu) {
420                // add images:
421                // http://developerlife.com/tutorials/?p=304
422                super.onCreateOptionsMenu(menu);
423                menu.add(0, MENU_CREATE_ID, 0, R.string.dashboard_menu_create).setIcon(android.R.drawable.ic_menu_add);
424                menu.add(0, MENU_FORM_REVIEW_ID, 0, R.string.dashboard_menu_edit).setIcon(android.R.drawable.ic_menu_agenda);
425                // menu.add(0, MENU_CHANGE_DATERANGE, 0,
426                // R.string.chart_menu_change_parameters.setIcon(android.R.drawable.ic_menu_recent_history);
427                menu.add(0, MENU_CHARTS_ID, 0, R.string.dashboard_menu_view).setIcon(android.R.drawable.ic_menu_sort_by_size);
428                // menu.add(0, MENU_SHOW_REPORTS, 0,
429                // R.string.dashboard_menu_show_reports);
430                return true;
431        }
432
433        @Override
434        public boolean onOptionsItemSelected(MenuItem item) {
435                // TODO Auto-generated method stub
436                super.onOptionsItemSelected(item);
437                switch (item.getItemId()) {
438                        case MENU_CREATE_ID:
439                                startActivityFormCreate();
440                                return true;
441                        case MENU_FORM_REVIEW_ID:
442                                startActivityFormReview();
443                                return true;
444
445                                // case MENU_CHANGE_DATERANGE:
446                                // startDateRangeActivity();
447                                // return true;
448                        case MENU_CHARTS_ID:
449                                startActivityChart();
450                                return true;
451                }
452                return true;
453        }
454
455        @Override
456        public boolean onPrepareOptionsMenu(Menu menu) {
457                // Flip the enabled status of menu items depending on selection of a
458                // form
459                super.onPrepareOptionsMenu(menu);
460
461                boolean formOptionsEnabled = false;
462                if (this.mChosenForm != null) {
463                        formOptionsEnabled = true;
464                }
465
466                MenuItem editMenu = menu.findItem(MENU_FORM_REVIEW_ID);
467                editMenu.setEnabled(formOptionsEnabled);
468                MenuItem viewMenu = menu.findItem(MENU_CHARTS_ID);
469
470                return true;
471        }
472
473        // @Override
474        // // http://www.anddev.org/tinytutcontextmenu_for_listview-t4019.html
475        // // UGH, things changed from .9 to 1.0
476        // public boolean onContextItemSelected(MenuItem item) {
477        // AdapterView.AdapterContextMenuInfo menuInfo =
478        // (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
479        // switch (item.getItemId()) {
480        // case CONTEXT_ITEM_SUMMARY_VIEW:
481        // mFormViewMode = LISTVIEW_MODE_SUMMARY_VIEW;
482        // break;
483        // case CONTEXT_ITEM_TABLE_VIEW:
484        // mFormViewMode = LISTVIEW_MODE_TABLE_VIEW;
485        // break;
486        // default:
487        // return super.onContextItemSelected(item);
488        // }
489        // this.resetCursor = false;
490        // beginListViewReload();
491        // return true;
492        // }
493
494        /**
495         * @deprecated
496         */
497        @Deprecated
498        private void startActivityDateRange() {
499                Intent i = new Intent(this, DateRange.class);
500                // Date endDate = java.sql.Date.
501                Date endDate = new Date();
502                if (mChosenForm != null) {
503                        endDate = ParsedDataReporter.getOldestMessageDate(this, mChosenForm);
504                        if (endDate.equals(Constants.NULLDATE)) {
505                                Builder noDateDialog = new AlertDialog.Builder(this);
506                                noDateDialog.setPositiveButton("Ok", null);
507                                noDateDialog.setTitle("Alert");
508                                noDateDialog.setMessage("This form has no messages or data to chart");
509                                noDateDialog.show();
510                                return;
511                        }
512                } else {
513                        endDate = MessageDataReporter.getOldestMessageDate(this);
514                }
515                i.putExtra(DateRange.CallParams.ACTIVITY_ARG_STARTDATE, endDate.getTime());
516                // startActivityForResult(i, ACTIVITY_DATERANGE);
517
518        }
519
520        // Start the form edit/create activity
521        private void startActivityFormReview() {
522                Intent i;
523                i = new Intent(this, FormReviewer.class);
524                i.putExtra(FormReviewer.CallParams.REVIEW_FORM, mChosenForm.getFormId());
525                startActivityForResult(i, ACTIVITY_FORM_REVIEW);
526        }
527
528        // Start the form edit/create activity
529        private void startActivityFormCreate() {
530                Intent i;
531                i = new Intent(this, FormCreator.class);
532                startActivityForResult(i, ACTIVITY_CREATE);
533        }
534
535        private void startActivityChart() {
536                // Debug.stopMethodTracing();
537                if (mListviewCursor == null) {
538                        Builder noDataDialog = new AlertDialog.Builder(this);
539                        noDataDialog.setPositiveButton("Ok", null);
540                        noDataDialog.setTitle("Alert");
541                        noDataDialog.setMessage("There is no data to chart.");
542                        noDataDialog.show();
543                        return;
544                }
545
546                Intent i = new Intent(this, ChartData.class);
547                Date now = new Date();
548                i.putExtra(ChartData.CallParams.END_DATE, now.getTime());
549                // we want to chart for a form
550                if (mChosenForm != null && !mShowAllMessages) {
551                        Date startDate = ParsedDataReporter.getOldestMessageDate(this, mChosenForm);
552                        if (startDate.equals(Constants.NULLDATE)) {
553                                Builder noDateDialog = new AlertDialog.Builder(this);
554                                noDateDialog.setPositiveButton("Ok", null);
555                                noDateDialog.setTitle("Alert");
556                                noDateDialog.setMessage("This form has no messages or data to chart");
557                                noDateDialog.show();
558                                return;
559                        }
560
561                        if (mListviewCursor.getCount() > 0) {
562                                mListviewCursor.moveToLast();
563                                // int msg_id =
564                                // mListviewCursor.getInt(Message.COL_PARSED_MESSAGE_ID);
565                                String datestring = mListviewCursor.getString(mListviewCursor.getColumnCount()
566                                                + Message.COL_JOINED_MESSAGE_TIME);
567
568                                try {
569                                        startDate = Message.SQLDateFormatter.parse(datestring);
570                                } catch (ParseException e) {
571                                        // TODO Auto-generated catch block
572                                        e.printStackTrace();
573                                }
574                                // Message m = MessageTranslator.GetMessage(this, msg_id);
575
576                        } else {
577                                Calendar startCal = Calendar.getInstance();
578                                startCal.add(Calendar.DATE, -7);
579                                startDate = startCal.getTime();
580                        }
581                        i.putExtra(ChartData.CallParams.START_DATE, startDate.getTime());
582                        i.putExtra(ChartData.CallParams.CHART_FORM, mChosenForm.getFormId());
583                } else if (mShowAllMessages) {
584                        // Chart for messages
585                        Date startDate = null;
586                        boolean setDate = false;
587                        if (mListviewCursor.getCount() > 0) {
588                                mListviewCursor.moveToLast();
589                                try {
590                                        startDate = Message.SQLDateFormatter.parse(mListviewCursor.getString(Message.COL_TIME));
591                                } catch (ParseException e) {
592                                        setDate = true;
593                                }
594                        } else {
595                                setDate = true;
596                        }
597                        if (setDate) {
598                                Calendar startCal = Calendar.getInstance();
599                                startCal.add(Calendar.DATE, -7);
600                                startDate = startCal.getTime();
601                        }
602
603                        mListviewCursor.moveToLast();
604                        i.putExtra(ChartData.CallParams.START_DATE, startDate.getTime());
605
606                        i.putExtra(ChartData.CallParams.CHART_MESSAGES, true);
607                }
608
609                // i.putExtra(ChartData.CallParams.START_DATE, mStartDate.getTime());
610                // i.putExtra(ChartData.CallParams.END_DATE, mEndDate.getTime());
611                startActivityForResult(i, ACTIVITY_CHARTS);
612        }
613
614        // This is a call to the DB to get all the forms that this form can support.
615        private void initFormSpinner() {
616                // The steps:
617                // get the spinner control from the layouts
618                Spinner spin_forms = (Spinner) findViewById(R.id.cbx_forms);
619                // Get an array of forms from the DB
620                // in the current iteration, it's mForms
621                this.mAllForms = ModelTranslator.getAllForms();
622
623                String[] monitors = new String[mAllForms.length + 1];
624
625                for (int i = 0; i < mAllForms.length; i++) {
626                        monitors[i] = "Form: " + mAllForms[i].getFormName();
627                }
628
629                // add some special selections:
630                monitors[monitors.length - 1] = "Show all Messages";
631                // monitors[monitors.length - 1] = "Show Monitors";
632
633                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, monitors);
634                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
635                // apply it to the spinner
636                spin_forms.setAdapter(adapter);
637        }
638
639        private void spinnerItemSelected(int position) {
640                if (position == mAllForms.length) {
641                        // if it's forms+1, then it's ALL messages
642                        mChosenForm = null;
643                        this.mShowAllMessages = true;
644                        resetCursor = true;
645                        beginListViewReload();
646                        // loadListViewWithRawMessages();
647
648                } else {
649                        this.mShowAllMessages = false;
650                        mChosenForm = mAllForms[position];
651                        resetCursor = true;
652                        beginListViewReload();
653                }
654        }
655
656        private synchronized void finishListViewReload() {
657                if (mListviewCursor == null) {
658                        return;
659                }
660                TextView lbl_recents = (TextView) findViewById(R.id.lbl_dashboardmessages);
661
662                lbl_recents.setText(this.mListviewCursor.getCount() + " Messages");
663
664                ListView lsv = (ListView) findViewById(R.id.lsv_dashboardmessages);
665
666                if (mChosenForm != null && !mShowAllMessages) {
667                        loadListViewWithFormData();
668                } else if (mShowAllMessages && mChosenForm == null) {
669                        this.mBtnViewModeSwitcher.setVisibility(View.INVISIBLE);
670                        this.messageCursorAdapter = new MessageCursorAdapter(this, mListviewCursor);
671                        lsv.setAdapter(messageCursorAdapter);
672                }
673                // lsv.setVisibility(View.VISIBLE);
674
675                // Debug.stopMethodTracing();
676        }
677
678        final Handler mDashboardHandler = new Handler();
679        final Runnable mUpdateResults = new Runnable() {
680                public void run() {
681                        // while(!mIsInitializing) {
682                        finishListViewReload();
683                        mViewSwitcher.showNext();
684                        // }
685                }
686        };
687
688        private synchronized void beginListViewReload() {
689                // Debug.startMethodTracing("listview_load" + traceCount++);
690                switch (mFormViewMode) {
691                        case LISTVIEW_MODE_SUMMARY_VIEW:
692                                mBtnViewModeSwitcher.setImageResource(R.drawable.summaryview);
693                                break;
694                        case LISTVIEW_MODE_TABLE_VIEW:
695                                mBtnViewModeSwitcher.setImageResource(R.drawable.gridview);
696                                break;
697                }
698
699                this.mIsInitializing = true;
700                TextView lbl_recents = (TextView) findViewById(R.id.lbl_dashboardmessages);
701                lbl_recents.setText(TXT_WAIT);
702                mViewSwitcher.showNext();
703                resetListAdapters();
704                new Thread(new Runnable() {
705                        public void run() {
706                                fillCursorInBackground();
707                                mIsInitializing = false;
708                                // finishListViewReload();//might puke
709                                mDashboardHandler.post(mUpdateResults);
710                        }
711                }).start();
712        }
713
714        private synchronized void fillCursorInBackground() {
715                if (mListviewCursor == null) {
716                        if (mChosenForm != null && !mShowAllMessages) {
717                                mListviewCursor = DashboardDataLayer.getCursorForFormData(this, mChosenForm, mListCount);
718                        } else if (mShowAllMessages && mChosenForm == null) {
719                                mListviewCursor = DashboardDataLayer.getCursorForRawMessages(this, mListCount);
720                        }
721                }
722        }
723
724        // this is a call to the DB to update the ListView with the messages for a
725        // selected form
726        private void loadListViewWithFormData() {
727                ListView lsv = (ListView) findViewById(R.id.lsv_dashboardmessages);
728                this.mBtnViewModeSwitcher.setVisibility(View.VISIBLE);
729                if (mChosenForm == null) {
730                        lsv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
731                                                                                                        new String[] { "Select an item" }));
732                } else {
733
734                        if (mListviewCursor.getCount() == 0) {
735                                lsv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
736                                                                                                                new String[] { "No data" }));
737                                return;
738                        }
739
740                        /*
741                         * if we want to get super fancy, we can do a join to make it all
742                         * accessible in one cursor instead of having to requery select
743                         * formdata_bednets.,
744                         * rapidandroid_message.message,rapidandroid_message.time from
745                         * formdata_bednets join rapidandroid_message on
746                         * (formdata_bednets.message_id = rapidandroid_message._id)
747                         */
748
749                        if (this.mFormViewMode == Dashboard.LISTVIEW_MODE_SUMMARY_VIEW) {
750                                this.summaryView = new SummaryCursorAdapter(this, mListviewCursor, mChosenForm);
751                                lsv.setAdapter(summaryView);
752                        } else if (this.mFormViewMode == Dashboard.LISTVIEW_MODE_TABLE_VIEW) {
753                                if (this.headerView == null) {
754                                        headerView = new SingleRowHeaderView(this, mChosenForm, mScreenWidth);
755                                        mHeaderTable.addView(headerView);
756                                        int colcount = headerView.getColCount();
757                                        for (int i = 0; i < colcount; i++) {
758                                                mHeaderTable.setColumnShrinkable(i, true);
759                                        }
760                                }
761                                rowView = new FormDataGridCursorAdapter(this, mChosenForm, mListviewCursor, mScreenWidth);
762                                lsv.setAdapter(rowView);
763                        }
764                }
765
766        }
767
768        /**
769         * @param changedforms
770         */
771        private void resetListAdapters() {
772                ListView lsv = (ListView) findViewById(R.id.lsv_dashboardmessages);
773
774                if (this.headerView != null) {
775                        mHeaderTable.removeAllViews();
776                        headerView = null;
777                }
778
779                if (rowView != null) {
780
781                        rowView = null;
782                }
783                if (summaryView != null) {
784                        summaryView = null;
785                }
786                if (messageCursorAdapter != null) {
787                        messageCursorAdapter = null;
788                }
789                // monitorCursorAdapter
790
791                if (resetCursor) {
792                        // need to reset the cursor
793                        if (mListviewCursor != null) {
794                                mListviewCursor.close();
795                                mListviewCursor = null;
796                        }
797                        resetCursor = false;
798                }
799        }
800
801}
Note: See TracBrowser for help on using the browser.