MessageList.java revision 9151d86e680f8b80ccc2e0bce32a2752c131c317
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.email.activity;
18
19import com.android.email.Controller;
20import com.android.email.Email;
21import com.android.email.R;
22import com.android.email.Utility;
23import com.android.email.activity.setup.AccountSettings;
24import com.android.email.mail.AuthenticationFailedException;
25import com.android.email.mail.CertificateValidationException;
26import com.android.email.mail.MessagingException;
27import com.android.email.provider.EmailContent;
28import com.android.email.provider.EmailContent.Account;
29import com.android.email.provider.EmailContent.AccountColumns;
30import com.android.email.provider.EmailContent.Mailbox;
31import com.android.email.provider.EmailContent.MailboxColumns;
32import com.android.email.provider.EmailContent.MessageColumns;
33import com.android.email.service.MailService;
34
35import android.app.ListActivity;
36import android.app.NotificationManager;
37import android.content.ContentResolver;
38import android.content.ContentUris;
39import android.content.Context;
40import android.content.Intent;
41import android.content.res.ColorStateList;
42import android.content.res.Resources;
43import android.content.res.TypedArray;
44import android.content.res.Resources.Theme;
45import android.database.Cursor;
46import android.graphics.Typeface;
47import android.graphics.drawable.Drawable;
48import android.net.Uri;
49import android.os.AsyncTask;
50import android.os.Bundle;
51import android.os.Handler;
52import android.os.SystemClock;
53import android.util.Log;
54import android.view.ContextMenu;
55import android.view.LayoutInflater;
56import android.view.Menu;
57import android.view.MenuItem;
58import android.view.View;
59import android.view.ViewGroup;
60import android.view.Window;
61import android.view.ContextMenu.ContextMenuInfo;
62import android.view.View.OnClickListener;
63import android.view.animation.AnimationUtils;
64import android.widget.AdapterView;
65import android.widget.Button;
66import android.widget.CursorAdapter;
67import android.widget.ImageView;
68import android.widget.ListView;
69import android.widget.ProgressBar;
70import android.widget.TextView;
71import android.widget.Toast;
72import android.widget.AdapterView.OnItemClickListener;
73
74import java.util.Date;
75import java.util.HashSet;
76import java.util.Set;
77import java.util.Timer;
78import java.util.TimerTask;
79
80public class MessageList extends ListActivity implements OnItemClickListener, OnClickListener {
81    // Intent extras (internal to this activity)
82    private static final String EXTRA_ACCOUNT_ID = "com.android.email.activity._ACCOUNT_ID";
83    private static final String EXTRA_MAILBOX_TYPE = "com.android.email.activity.MAILBOX_TYPE";
84    private static final String EXTRA_MAILBOX_ID = "com.android.email.activity.MAILBOX_ID";
85    private static final String STATE_SELECTED_ITEM_TOP =
86        "com.android.email.activity.MessageList.selectedItemTop";
87    private static final String STATE_SELECTED_POSITION =
88        "com.android.email.activity.MessageList.selectedPosition";
89    private static final String STATE_CHECKED_ITEMS =
90        "com.android.email.activity.MessageList.checkedItems";
91
92    // UI support
93    private ListView mListView;
94    private View mMultiSelectPanel;
95    private Button mReadUnreadButton;
96    private Button mFavoriteButton;
97    private Button mDeleteButton;
98    private View mListFooterView;
99    private TextView mListFooterText;
100    private View mListFooterProgress;
101    private TextView mErrorBanner;
102
103    private static final int LIST_FOOTER_MODE_NONE = 0;
104    private static final int LIST_FOOTER_MODE_REFRESH = 1;
105    private static final int LIST_FOOTER_MODE_MORE = 2;
106    private static final int LIST_FOOTER_MODE_SEND = 3;
107    private int mListFooterMode;
108
109    private MessageListAdapter mListAdapter;
110    private MessageListHandler mHandler = new MessageListHandler();
111    private Controller mController = Controller.getInstance(getApplication());
112    private ControllerResults mControllerCallback = new ControllerResults();
113    private TextView mLeftTitle;
114    private TextView mRightTitle;
115    private ProgressBar mProgressIcon;
116
117    private static final int[] mColorChipResIds = new int[] {
118        R.drawable.appointment_indicator_leftside_1,
119        R.drawable.appointment_indicator_leftside_2,
120        R.drawable.appointment_indicator_leftside_3,
121        R.drawable.appointment_indicator_leftside_4,
122        R.drawable.appointment_indicator_leftside_5,
123        R.drawable.appointment_indicator_leftside_6,
124        R.drawable.appointment_indicator_leftside_7,
125        R.drawable.appointment_indicator_leftside_8,
126        R.drawable.appointment_indicator_leftside_9,
127        R.drawable.appointment_indicator_leftside_10,
128        R.drawable.appointment_indicator_leftside_11,
129        R.drawable.appointment_indicator_leftside_12,
130        R.drawable.appointment_indicator_leftside_13,
131        R.drawable.appointment_indicator_leftside_14,
132        R.drawable.appointment_indicator_leftside_15,
133        R.drawable.appointment_indicator_leftside_16,
134        R.drawable.appointment_indicator_leftside_17,
135        R.drawable.appointment_indicator_leftside_18,
136        R.drawable.appointment_indicator_leftside_19,
137        R.drawable.appointment_indicator_leftside_20,
138        R.drawable.appointment_indicator_leftside_21,
139    };
140
141    // DB access
142    private ContentResolver mResolver;
143    private long mMailboxId;
144    private LoadMessagesTask mLoadMessagesTask;
145    private FindMailboxTask mFindMailboxTask;
146    private SetTitleTask mSetTitleTask;
147    private SetFooterTask mSetFooterTask;
148
149    public final static String[] MAILBOX_FIND_INBOX_PROJECTION = new String[] {
150        EmailContent.RECORD_ID, MailboxColumns.TYPE, MailboxColumns.FLAG_VISIBLE
151    };
152
153    private static final int MAILBOX_NAME_COLUMN_ID = 0;
154    private static final int MAILBOX_NAME_COLUMN_ACCOUNT_KEY = 1;
155    private static final int MAILBOX_NAME_COLUMN_TYPE = 2;
156    private static final String[] MAILBOX_NAME_PROJECTION = new String[] {
157            MailboxColumns.DISPLAY_NAME, MailboxColumns.ACCOUNT_KEY,
158            MailboxColumns.TYPE};
159
160    private static final int ACCOUNT_DISPLAY_NAME_COLUMN_ID = 0;
161    private static final String[] ACCOUNT_NAME_PROJECTION = new String[] {
162            AccountColumns.DISPLAY_NAME };
163
164    private static final String ID_SELECTION = EmailContent.RECORD_ID + "=?";
165
166    private Boolean mPushModeMailbox = null;
167    private int mSavedItemTop = 0;
168    private int mSavedItemPosition = -1;
169    private boolean mCanAutoRefresh = false;
170
171    /* package */ static final String[] MESSAGE_PROJECTION = new String[] {
172        EmailContent.RECORD_ID, MessageColumns.MAILBOX_KEY, MessageColumns.ACCOUNT_KEY,
173        MessageColumns.DISPLAY_NAME, MessageColumns.SUBJECT, MessageColumns.TIMESTAMP,
174        MessageColumns.FLAG_READ, MessageColumns.FLAG_FAVORITE, MessageColumns.FLAG_ATTACHMENT,
175    };
176
177    /**
178     * Open a specific mailbox.
179     *
180     * TODO This should just shortcut to a more generic version that can accept a list of
181     * accounts/mailboxes (e.g. merged inboxes).
182     *
183     * @param context
184     * @param id mailbox key
185     */
186    public static void actionHandleMailbox(Context context, long id) {
187        Intent intent = new Intent(context, MessageList.class);
188        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
189        intent.putExtra(EXTRA_MAILBOX_ID, id);
190        context.startActivity(intent);
191    }
192
193    /**
194     * Open a specific mailbox by account & type
195     *
196     * @param context The caller's context (for generating an intent)
197     * @param accountId The account to open
198     * @param mailboxType the type of mailbox to open (e.g. @see EmailContent.Mailbox.TYPE_INBOX)
199     */
200    public static void actionHandleAccount(Context context, long accountId, int mailboxType) {
201        Intent intent = new Intent(context, MessageList.class);
202        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
203        intent.putExtra(EXTRA_ACCOUNT_ID, accountId);
204        intent.putExtra(EXTRA_MAILBOX_TYPE, mailboxType);
205        context.startActivity(intent);
206    }
207
208    /**
209     * Return an intent to open a specific mailbox by account & type.  It will also clear
210     * notifications.
211     *
212     * @param context The caller's context (for generating an intent)
213     * @param accountId The account to open, or -1
214     * @param mailboxId the ID of the mailbox to open, or -1
215     * @param mailboxType the type of mailbox to open (e.g. @see Mailbox.TYPE_INBOX) or -1
216     */
217    public static Intent actionHandleAccountIntent(Context context, long accountId,
218            long mailboxId, int mailboxType) {
219        Intent intent = new Intent(context, MessageList.class);
220        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
221        intent.putExtra(EXTRA_ACCOUNT_ID, accountId);
222        intent.putExtra(EXTRA_MAILBOX_ID, mailboxId);
223        intent.putExtra(EXTRA_MAILBOX_TYPE, mailboxType);
224        return intent;
225    }
226
227    /**
228     * Used for generating lightweight (Uri-only) intents.
229     *
230     * @param context Calling context for building the intent
231     * @param accountId The account of interest
232     * @param mailboxType The folder name to open (typically Mailbox.TYPE_INBOX)
233     * @return an Intent which can be used to view that account
234     */
235    public static Intent actionHandleAccountUriIntent(Context context, long accountId,
236            int mailboxType) {
237        Intent i = actionHandleAccountIntent(context, accountId, -1, mailboxType);
238        i.removeExtra(EXTRA_ACCOUNT_ID);
239        Uri uri = ContentUris.withAppendedId(Account.CONTENT_URI, accountId);
240        i.setData(uri);
241        return i;
242    }
243
244    @Override
245    public void onCreate(Bundle icicle) {
246        super.onCreate(icicle);
247
248        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
249        setContentView(R.layout.message_list);
250        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
251                R.layout.list_title);
252
253        mCanAutoRefresh = true;
254        mListView = getListView();
255        mMultiSelectPanel = findViewById(R.id.footer_organize);
256        mReadUnreadButton = (Button) findViewById(R.id.btn_read_unread);
257        mFavoriteButton = (Button) findViewById(R.id.btn_multi_favorite);
258        mDeleteButton = (Button) findViewById(R.id.btn_multi_delete);
259        mLeftTitle = (TextView) findViewById(R.id.title_left_text);
260        mRightTitle = (TextView) findViewById(R.id.title_right_text);
261        mProgressIcon = (ProgressBar) findViewById(R.id.title_progress_icon);
262        mErrorBanner = (TextView) findViewById(R.id.connection_error_text);
263
264        mReadUnreadButton.setOnClickListener(this);
265        mFavoriteButton.setOnClickListener(this);
266        mDeleteButton.setOnClickListener(this);
267
268        mListView.setOnItemClickListener(this);
269        mListView.setItemsCanFocus(false);
270        registerForContextMenu(mListView);
271
272        mListAdapter = new MessageListAdapter(this);
273        setListAdapter(mListAdapter);
274
275        mResolver = getContentResolver();
276
277        // TODO extend this to properly deal with multiple mailboxes, cursor, etc.
278
279        // Select 'by id' or 'by type' or 'by uri' mode and launch appropriate queries
280
281        mMailboxId = getIntent().getLongExtra(EXTRA_MAILBOX_ID, -1);
282        if (mMailboxId != -1) {
283            // Specific mailbox ID was provided - go directly to it
284            mSetTitleTask = new SetTitleTask(mMailboxId);
285            mSetTitleTask.execute();
286            mLoadMessagesTask = new LoadMessagesTask(mMailboxId, -1);
287            mLoadMessagesTask.execute();
288            addFooterView(mMailboxId, -1, -1);
289        } else {
290            long accountId = -1;
291            int mailboxType = getIntent().getIntExtra(EXTRA_MAILBOX_TYPE, Mailbox.TYPE_INBOX);
292            Uri uri = getIntent().getData();
293            if (uri != null
294                    && "content".equals(uri.getScheme())
295                    && EmailContent.AUTHORITY.equals(uri.getAuthority())) {
296                // A content URI was provided - try to look up the account
297                String accountIdString = uri.getPathSegments().get(1);
298                if (accountIdString != null) {
299                    accountId = Long.parseLong(accountIdString);
300                }
301                mFindMailboxTask = new FindMailboxTask(accountId, mailboxType, false);
302                mFindMailboxTask.execute();
303            } else {
304                // Go by account id + type
305                accountId = getIntent().getLongExtra(EXTRA_ACCOUNT_ID, -1);
306                mFindMailboxTask = new FindMailboxTask(accountId, mailboxType, true);
307                mFindMailboxTask.execute();
308            }
309            addFooterView(-1, accountId, mailboxType);
310        }
311        // TODO set title to "account > mailbox (#unread)"
312    }
313
314    @Override
315    public void onPause() {
316        super.onPause();
317        mController.removeResultCallback(mControllerCallback);
318    }
319
320    @Override
321    public void onResume() {
322        super.onResume();
323        mController.addResultCallback(mControllerCallback);
324
325        // clear notifications here
326        NotificationManager notificationManager = (NotificationManager)
327                getSystemService(Context.NOTIFICATION_SERVICE);
328        notificationManager.cancel(MailService.NEW_MESSAGE_NOTIFICATION_ID);
329        restoreListPosition();
330        autoRefreshStaleMailbox();
331    }
332
333    @Override
334    protected void onDestroy() {
335        super.onDestroy();
336
337        if (mLoadMessagesTask != null &&
338                mLoadMessagesTask.getStatus() != LoadMessagesTask.Status.FINISHED) {
339            mLoadMessagesTask.cancel(true);
340            mLoadMessagesTask = null;
341        }
342        if (mFindMailboxTask != null &&
343                mFindMailboxTask.getStatus() != FindMailboxTask.Status.FINISHED) {
344            mFindMailboxTask.cancel(true);
345            mFindMailboxTask = null;
346        }
347        if (mSetTitleTask != null &&
348                mSetTitleTask.getStatus() != SetTitleTask.Status.FINISHED) {
349            mSetTitleTask.cancel(true);
350            mSetTitleTask = null;
351        }
352        if (mSetFooterTask != null &&
353                mSetFooterTask.getStatus() != SetTitleTask.Status.FINISHED) {
354            mSetFooterTask.cancel(true);
355            mSetFooterTask = null;
356        }
357    }
358
359    @Override
360    protected void onSaveInstanceState(Bundle outState) {
361        super.onSaveInstanceState(outState);
362        saveListPosition();
363        outState.putInt(STATE_SELECTED_POSITION, mSavedItemPosition);
364        outState.putInt(STATE_SELECTED_ITEM_TOP, mSavedItemTop);
365        Set<Long> checkedset = mListAdapter.getSelectedSet();
366        long[] checkedarray = new long[checkedset.size()];
367        int i = 0;
368        for (Long l : checkedset) {
369            checkedarray[i] = l;
370            i++;
371        }
372        outState.putLongArray(STATE_CHECKED_ITEMS, checkedarray);
373    }
374
375    @Override
376    protected void onRestoreInstanceState(Bundle savedInstanceState) {
377        super.onRestoreInstanceState(savedInstanceState);
378        mSavedItemTop = savedInstanceState.getInt(STATE_SELECTED_ITEM_TOP, 0);
379        mSavedItemPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION, -1);
380        Set<Long> checkedset = mListAdapter.getSelectedSet();
381        for (long l: savedInstanceState.getLongArray(STATE_CHECKED_ITEMS)) {
382            checkedset.add(l);
383        }
384    }
385
386    private void saveListPosition() {
387        mSavedItemPosition = getListView().getSelectedItemPosition();
388        if (mSavedItemPosition >= 0 && getListView().isSelected()) {
389            mSavedItemTop = getListView().getSelectedView().getTop();
390        } else {
391            mSavedItemPosition = getListView().getFirstVisiblePosition();
392            if (mSavedItemPosition >= 0) {
393                mSavedItemTop = 0;
394                View topChild = getListView().getChildAt(0);
395                if (topChild != null) {
396                    mSavedItemTop = topChild.getTop();
397                }
398            }
399        }
400    }
401
402    private void restoreListPosition() {
403        if (mSavedItemPosition >= 0 && mSavedItemPosition < getListView().getCount()) {
404            getListView().setSelectionFromTop(mSavedItemPosition, mSavedItemTop);
405            mSavedItemPosition = -1;
406            mSavedItemTop = 0;
407        }
408    }
409
410    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
411        if (view != mListFooterView) {
412            MessageListItem itemView = (MessageListItem) view;
413            onOpenMessage(id, itemView.mMailboxId);
414        } else {
415            doFooterClick();
416        }
417    }
418
419    public void onClick(View v) {
420        switch (v.getId()) {
421            case R.id.btn_read_unread:
422                onMultiToggleRead(mListAdapter.getSelectedSet());
423                break;
424            case R.id.btn_multi_favorite:
425                onMultiToggleFavorite(mListAdapter.getSelectedSet());
426                break;
427            case R.id.btn_multi_delete:
428                onMultiDelete(mListAdapter.getSelectedSet());
429                break;
430        }
431    }
432
433    @Override
434    public boolean onCreateOptionsMenu(Menu menu) {
435        super.onCreateOptionsMenu(menu);
436        if (mMailboxId < 0) {
437            getMenuInflater().inflate(R.menu.message_list_option_smart_folder, menu);
438        } else {
439            getMenuInflater().inflate(R.menu.message_list_option, menu);
440        }
441        return true;
442    }
443
444    @Override
445    public boolean onPrepareOptionsMenu(Menu menu) {
446        boolean showDeselect = mListAdapter.getSelectedSet().size() > 0;
447        menu.setGroupVisible(R.id.deselect_all_group, showDeselect);
448        return true;
449    }
450
451    @Override
452    public boolean onOptionsItemSelected(MenuItem item) {
453        switch (item.getItemId()) {
454            case R.id.refresh:
455                onRefresh();
456                return true;
457            case R.id.folders:
458                onFolders();
459                return true;
460            case R.id.accounts:
461                onAccounts();
462                return true;
463            case R.id.compose:
464                onCompose();
465                return true;
466            case R.id.account_settings:
467                onEditAccount();
468                return true;
469            case R.id.deselect_all:
470                onDeselectAll();
471                return true;
472            default:
473                return super.onOptionsItemSelected(item);
474        }
475    }
476
477    @Override
478    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
479        super.onCreateContextMenu(menu, v, menuInfo);
480
481        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
482        // There is no context menu for the list footer
483        if (info.targetView == mListFooterView) {
484            return;
485        }
486        MessageListItem itemView = (MessageListItem) info.targetView;
487
488        Cursor c = (Cursor) mListView.getItemAtPosition(info.position);
489        String messageName = c.getString(MessageListAdapter.COLUMN_SUBJECT);
490
491        menu.setHeaderTitle(messageName);
492
493        // TODO: There is probably a special context menu for the trash
494        Mailbox mailbox = Mailbox.restoreMailboxWithId(this, itemView.mMailboxId);
495
496        switch (mailbox.mType) {
497            case EmailContent.Mailbox.TYPE_DRAFTS:
498                getMenuInflater().inflate(R.menu.message_list_context_drafts, menu);
499                break;
500            case EmailContent.Mailbox.TYPE_OUTBOX:
501                getMenuInflater().inflate(R.menu.message_list_context_outbox, menu);
502                break;
503            case EmailContent.Mailbox.TYPE_TRASH:
504                getMenuInflater().inflate(R.menu.message_list_context_trash, menu);
505                break;
506            default:
507                getMenuInflater().inflate(R.menu.message_list_context, menu);
508                // The default menu contains "mark as read".  If the message is read, change
509                // the menu text to "mark as unread."
510                if (itemView.mRead) {
511                    menu.findItem(R.id.mark_as_read).setTitle(R.string.mark_as_unread_action);
512                }
513                break;
514        }
515    }
516
517    @Override
518    public boolean onContextItemSelected(MenuItem item) {
519        AdapterView.AdapterContextMenuInfo info =
520            (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
521        MessageListItem itemView = (MessageListItem) info.targetView;
522
523        switch (item.getItemId()) {
524            case R.id.open:
525                onOpenMessage(info.id, itemView.mMailboxId);
526                break;
527            case R.id.delete:
528                onDelete(info.id, itemView.mAccountId);
529                break;
530            case R.id.reply:
531                onReply(itemView.mMessageId);
532                break;
533            case R.id.reply_all:
534                onReplyAll(itemView.mMessageId);
535                break;
536            case R.id.forward:
537                onForward(itemView.mMessageId);
538                break;
539            case R.id.mark_as_read:
540                onSetMessageRead(info.id, !itemView.mRead);
541                break;
542        }
543        return super.onContextItemSelected(item);
544    }
545
546    private void onRefresh() {
547        // TODO: Should not be reading from DB in UI thread - need a cleaner way to get accountId
548        if (mMailboxId >= 0) {
549            Mailbox mailbox = Mailbox.restoreMailboxWithId(this, mMailboxId);
550            if (mailbox != null) {
551                mController.updateMailbox(mailbox.mAccountKey, mMailboxId, mControllerCallback);
552            }
553        }
554    }
555
556    private void onFolders() {
557        if (mMailboxId >= 0) {
558            // TODO smaller projection
559            Mailbox mailbox = Mailbox.restoreMailboxWithId(this, mMailboxId);
560            MailboxList.actionHandleAccount(this, mailbox.mAccountKey);
561            finish();
562        }
563    }
564
565    private void onAccounts() {
566        AccountFolderList.actionShowAccounts(this);
567        finish();
568    }
569
570    private long lookupAccountIdFromMailboxId(long mailboxId) {
571        // TODO: Select correct account to send from when there are multiple mailboxes
572        // TODO: Should not be reading from DB in UI thread
573        if (mailboxId < 0) {
574            return -1; // no info, default account
575        }
576        EmailContent.Mailbox mailbox =
577            EmailContent.Mailbox.restoreMailboxWithId(this, mailboxId);
578        if (mailbox == null) {
579            return -2;
580        }
581        return mailbox.mAccountKey;
582    }
583
584    private void onCompose() {
585        long accountKey = lookupAccountIdFromMailboxId(mMailboxId);
586        if (accountKey > -2) {
587            MessageCompose.actionCompose(this, accountKey);
588        } else {
589            finish();
590        }
591    }
592
593    private void onEditAccount() {
594        long accountKey = lookupAccountIdFromMailboxId(mMailboxId);
595        if (accountKey > -2) {
596            AccountSettings.actionSettings(this, accountKey);
597        } else {
598            finish();
599        }
600    }
601
602    private void onDeselectAll() {
603        mListAdapter.getSelectedSet().clear();
604        mListView.invalidateViews();
605        showMultiPanel(false);
606    }
607
608    private void onOpenMessage(long messageId, long mailboxId) {
609        // TODO: Should not be reading from DB in UI thread
610        EmailContent.Mailbox mailbox = EmailContent.Mailbox.restoreMailboxWithId(this, mailboxId);
611
612        if (mailbox.mType == EmailContent.Mailbox.TYPE_DRAFTS) {
613            MessageCompose.actionEditDraft(this, messageId);
614        } else {
615            final boolean disableReply = (mailbox.mType == EmailContent.Mailbox.TYPE_TRASH);
616            // WARNING: here we pass mMailboxId, which can be the negative id of a compound
617            // mailbox, instead of the mailboxId of the particular message that is opened
618            MessageView.actionView(this, messageId, mMailboxId, disableReply);
619        }
620    }
621
622    private void onReply(long messageId) {
623        MessageCompose.actionReply(this, messageId, false);
624    }
625
626    private void onReplyAll(long messageId) {
627        MessageCompose.actionReply(this, messageId, true);
628    }
629
630    private void onForward(long messageId) {
631        MessageCompose.actionForward(this, messageId);
632    }
633
634    private void onLoadMoreMessages() {
635        if (mMailboxId >= 0) {
636            mController.loadMoreMessages(mMailboxId, mControllerCallback);
637        }
638    }
639
640    private void onSendPendingMessages() {
641        if (mMailboxId == Mailbox.QUERY_ALL_OUTBOX) {
642            // For the combined Outbox, we loop through all accounts and send the messages
643            Cursor c = mResolver.query(Account.CONTENT_URI, Account.ID_PROJECTION,
644                    null, null, null);
645            try {
646                while (c.moveToNext()) {
647                    long accountId = c.getLong(Account.ID_PROJECTION_COLUMN);
648                    mController.sendPendingMessages(accountId, mControllerCallback);
649                }
650            } finally {
651                c.close();
652            }
653        } else {
654            long accountKey = lookupAccountIdFromMailboxId(mMailboxId);
655            if (accountKey > -2) {
656                mController.sendPendingMessages(accountKey, mControllerCallback);
657            } else {
658                finish();
659            }
660        }
661    }
662
663    private void onDelete(long messageId, long accountId) {
664        mController.deleteMessage(messageId, accountId);
665        Toast.makeText(this, R.string.message_deleted_toast, Toast.LENGTH_SHORT).show();
666    }
667
668    private void onSetMessageRead(long messageId, boolean newRead) {
669        mController.setMessageRead(messageId, newRead);
670    }
671
672    private void onSetMessageFavorite(long messageId, boolean newFavorite) {
673        mController.setMessageFavorite(messageId, newFavorite);
674    }
675
676    /**
677     * Toggles a set read/unread states.  Note, the default behavior is "mark unread", so the
678     * sense of the helper methods is "true=unread".
679     *
680     * @param selectedSet The current list of selected items
681     */
682    private void onMultiToggleRead(Set<Long> selectedSet) {
683        toggleMultiple(selectedSet, new MultiToggleHelper() {
684
685            public boolean getField(long messageId, Cursor c) {
686                return c.getInt(MessageListAdapter.COLUMN_READ) == 0;
687            }
688
689            public boolean setField(long messageId, Cursor c, boolean newValue) {
690                boolean oldValue = getField(messageId, c);
691                if (oldValue != newValue) {
692                    onSetMessageRead(messageId, !newValue);
693                    return true;
694                }
695                return false;
696            }
697        });
698    }
699
700    /**
701     * Toggles a set of favorites (stars)
702     *
703     * @param selectedSet The current list of selected items
704     */
705    private void onMultiToggleFavorite(Set<Long> selectedSet) {
706        toggleMultiple(selectedSet, new MultiToggleHelper() {
707
708            public boolean getField(long messageId, Cursor c) {
709                return c.getInt(MessageListAdapter.COLUMN_FAVORITE) != 0;
710            }
711
712            public boolean setField(long messageId, Cursor c, boolean newValue) {
713                boolean oldValue = getField(messageId, c);
714                if (oldValue != newValue) {
715                    onSetMessageFavorite(messageId, newValue);
716                    return true;
717                }
718                return false;
719            }
720        });
721    }
722
723    private void onMultiDelete(Set<Long> selectedSet) {
724        // Clone the set, because deleting is going to thrash things
725        HashSet<Long> cloneSet = new HashSet<Long>(selectedSet);
726        for (Long id : cloneSet) {
727            mController.deleteMessage(id, -1);
728        }
729        // TODO: count messages and show "n messages deleted"
730        Toast.makeText(this, R.string.message_deleted_toast, Toast.LENGTH_SHORT).show();
731        selectedSet.clear();
732        showMultiPanel(false);
733    }
734
735    private interface MultiToggleHelper {
736        /**
737         * Return true if the field of interest is "set".  If one or more are false, then our
738         * bulk action will be to "set".  If all are set, our bulk action will be to "clear".
739         * @param messageId the message id of the current message
740         * @param c the cursor, positioned to the item of interest
741         * @return true if the field at this row is "set"
742         */
743        public boolean getField(long messageId, Cursor c);
744
745        /**
746         * Set or clear the field of interest.  Return true if a change was made.
747         * @param messageId the message id of the current message
748         * @param c the cursor, positioned to the item of interest
749         * @param newValue the new value to be set at this row
750         * @return true if a change was actually made
751         */
752        public boolean setField(long messageId, Cursor c, boolean newValue);
753    }
754
755    /**
756     * Toggle multiple fields in a message, using the following logic:  If one or more fields
757     * are "clear", then "set" them.  If all fields are "set", then "clear" them all.
758     *
759     * @param selectedSet the set of messages that are selected
760     * @param helper functions to implement the specific getter & setter
761     * @return the number of messages that were updated
762     */
763    private int toggleMultiple(Set<Long> selectedSet, MultiToggleHelper helper) {
764        Cursor c = mListAdapter.getCursor();
765        boolean anyWereFound = false;
766        boolean allWereSet = true;
767
768        c.moveToPosition(-1);
769        while (c.moveToNext()) {
770            long id = c.getInt(MessageListAdapter.COLUMN_ID);
771            if (selectedSet.contains(Long.valueOf(id))) {
772                anyWereFound = true;
773                if (!helper.getField(id, c)) {
774                    allWereSet = false;
775                    break;
776                }
777            }
778        }
779
780        int numChanged = 0;
781
782        if (anyWereFound) {
783            boolean newValue = !allWereSet;
784            c.moveToPosition(-1);
785            while (c.moveToNext()) {
786                long id = c.getInt(MessageListAdapter.COLUMN_ID);
787                if (selectedSet.contains(Long.valueOf(id))) {
788                    if (helper.setField(id, c, newValue)) {
789                        ++numChanged;
790                    }
791                }
792            }
793        }
794
795        return numChanged;
796    }
797
798    /**
799     * Test selected messages for showing appropriate labels
800     * @param selectedSet
801     * @param column_id
802     * @param defaultflag
803     * @return true when the specified flagged message is selected
804     */
805    private boolean testMultiple(Set<Long> selectedSet, int column_id, boolean defaultflag) {
806        Cursor c = mListAdapter.getCursor();
807        if (c == null || c.isClosed()) {
808            return false;
809        }
810        c.moveToPosition(-1);
811        while (c.moveToNext()) {
812            long id = c.getInt(MessageListAdapter.COLUMN_ID);
813            if (selectedSet.contains(Long.valueOf(id))) {
814                if (c.getInt(column_id) == (defaultflag? 1 : 0)) {
815                    return true;
816                }
817            }
818        }
819        return false;
820    }
821
822    /**
823     * Implements a timed refresh of "stale" mailboxes.  This should only happen when
824     * multiple conditions are true, including:
825     *   Only when the user explicitly opens the mailbox (not onResume, for example)
826     *   Only for real, non-push mailboxes
827     *   Only when the mailbox is "stale" (currently set to 5 minutes since last refresh)
828     */
829    private void autoRefreshStaleMailbox() {
830        if (!mCanAutoRefresh
831                || (mListAdapter.getCursor() == null) // Check if messages info is loaded
832                || (mPushModeMailbox != null && mPushModeMailbox) // Check the push mode
833                || (mMailboxId < 0)) { // Check if this mailbox is synthetic/combined
834            return;
835        }
836        mCanAutoRefresh = false;
837        if (!Email.mailboxRequiresRefresh(mMailboxId)) {
838            return;
839        }
840        onRefresh();
841    }
842
843    private void updateFooterButtonNames () {
844        // Show "unread_action" when one or more read messages are selected.
845        if (testMultiple(mListAdapter.getSelectedSet(), MessageListAdapter.COLUMN_READ, true)) {
846            mReadUnreadButton.setText(R.string.unread_action);
847        } else {
848            mReadUnreadButton.setText(R.string.read_action);
849        }
850        // Show "set_star_action" when one or more un-starred messages are selected.
851        if (testMultiple(mListAdapter.getSelectedSet(),
852                MessageListAdapter.COLUMN_FAVORITE, false)) {
853            mFavoriteButton.setText(R.string.set_star_action);
854        } else {
855            mFavoriteButton.setText(R.string.remove_star_action);
856        }
857    }
858
859    /**
860     * Show or hide the panel of multi-select options
861     */
862    private void showMultiPanel(boolean show) {
863        if (show && mMultiSelectPanel.getVisibility() != View.VISIBLE) {
864            mMultiSelectPanel.setVisibility(View.VISIBLE);
865            mMultiSelectPanel.startAnimation(
866                    AnimationUtils.loadAnimation(this, R.anim.footer_appear));
867        } else if (!show && mMultiSelectPanel.getVisibility() != View.GONE) {
868            mMultiSelectPanel.setVisibility(View.GONE);
869            mMultiSelectPanel.startAnimation(
870                        AnimationUtils.loadAnimation(this, R.anim.footer_disappear));
871        }
872        if (show) {
873            updateFooterButtonNames();
874        }
875    }
876
877    /**
878     * Add the fixed footer view if appropriate (not always - not all accounts & mailboxes).
879     *
880     * Here are some rules (finish this list):
881     *
882     * Any merged, synced box (except send):  refresh
883     * Any push-mode account:  refresh
884     * Any non-push-mode account:  load more
885     * Any outbox (send again):
886     *
887     * @param mailboxId the ID of the mailbox
888     */
889    private void addFooterView(long mailboxId, long accountId, int mailboxType) {
890        // first, look for shortcuts that don't need us to spin up a DB access task
891        if (mailboxId == Mailbox.QUERY_ALL_INBOXES
892                || mailboxId == Mailbox.QUERY_ALL_UNREAD
893                || mailboxId == Mailbox.QUERY_ALL_FAVORITES) {
894            finishFooterView(LIST_FOOTER_MODE_REFRESH);
895            return;
896        }
897        if (mailboxId == Mailbox.QUERY_ALL_DRAFTS || mailboxType == Mailbox.TYPE_DRAFTS) {
898            finishFooterView(LIST_FOOTER_MODE_NONE);
899            return;
900        }
901        if (mailboxId == Mailbox.QUERY_ALL_OUTBOX || mailboxType == Mailbox.TYPE_OUTBOX) {
902            finishFooterView(LIST_FOOTER_MODE_SEND);
903            return;
904        }
905
906        // We don't know enough to select the footer command type (yet), so we'll
907        // launch an async task to do the remaining lookups and decide what to do
908        mSetFooterTask = new SetFooterTask();
909        mSetFooterTask.execute(mailboxId, accountId);
910    }
911
912    private final static String[] MAILBOX_ACCOUNT_AND_TYPE_PROJECTION =
913        new String[] { MailboxColumns.ACCOUNT_KEY, MailboxColumns.TYPE };
914
915    private class SetFooterTask extends AsyncTask<Long, Void, Integer> {
916        /**
917         * There are two operational modes here, requiring different lookup.
918         * mailboxIs != -1:  A specific mailbox - check its type, then look up its account
919         * accountId != -1:  A specific account - look up the account
920         */
921        @Override
922        protected Integer doInBackground(Long... params) {
923            long mailboxId = params[0];
924            long accountId = params[1];
925            int mailboxType = -1;
926            if (mailboxId != -1) {
927                try {
928                    Uri uri = ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailboxId);
929                    Cursor c = mResolver.query(uri, MAILBOX_ACCOUNT_AND_TYPE_PROJECTION,
930                            null, null, null);
931                    if (c.moveToFirst()) {
932                        try {
933                            accountId = c.getLong(0);
934                            mailboxType = c.getInt(1);
935                        } finally {
936                            c.close();
937                        }
938                    }
939                } catch (IllegalArgumentException iae) {
940                    // can't do any more here
941                    return LIST_FOOTER_MODE_NONE;
942                }
943            }
944            switch (mailboxType) {
945                case Mailbox.TYPE_OUTBOX:
946                    return LIST_FOOTER_MODE_SEND;
947                case Mailbox.TYPE_DRAFTS:
948                    return LIST_FOOTER_MODE_NONE;
949            }
950            if (accountId != -1) {
951                // This is inefficient but the best fix is not here but in isMessagingController
952                Account account = Account.restoreAccountWithId(MessageList.this, accountId);
953                if (account != null) {
954                    mPushModeMailbox = account.mSyncInterval == Account.CHECK_INTERVAL_PUSH;
955                    if (MessageList.this.mController.isMessagingController(account)) {
956                        return LIST_FOOTER_MODE_MORE;       // IMAP or POP
957                    } else {
958                        return LIST_FOOTER_MODE_NONE;    // EAS
959                    }
960                }
961            }
962            return LIST_FOOTER_MODE_NONE;
963        }
964
965        @Override
966        protected void onPostExecute(Integer listFooterMode) {
967            if (listFooterMode == null) {
968                return;
969            }
970            finishFooterView(listFooterMode);
971        }
972    }
973
974    /**
975     * Add the fixed footer view as specified, and set up the test as well.
976     *
977     * @param listFooterMode the footer mode we've determined should be used for this list
978     */
979    private void finishFooterView(int listFooterMode) {
980        mListFooterMode = listFooterMode;
981        if (mListFooterMode != LIST_FOOTER_MODE_NONE) {
982            mListFooterView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
983                    .inflate(R.layout.message_list_item_footer, mListView, false);
984            mList.addFooterView(mListFooterView);
985            setListAdapter(mListAdapter);
986
987            mListFooterProgress = mListFooterView.findViewById(R.id.progress);
988            mListFooterText = (TextView) mListFooterView.findViewById(R.id.main_text);
989            setListFooterText(false);
990        }
991    }
992
993    /**
994     * Set the list footer text based on mode and "active" status
995     */
996    private void setListFooterText(boolean active) {
997        if (mListFooterMode != LIST_FOOTER_MODE_NONE) {
998            int footerTextId = 0;
999            switch (mListFooterMode) {
1000                case LIST_FOOTER_MODE_REFRESH:
1001                    footerTextId = active ? R.string.status_loading_more
1002                                          : R.string.refresh_action;
1003                    break;
1004                case LIST_FOOTER_MODE_MORE:
1005                    footerTextId = active ? R.string.status_loading_more
1006                                          : R.string.message_list_load_more_messages_action;
1007                    break;
1008                case LIST_FOOTER_MODE_SEND:
1009                    footerTextId = active ? R.string.status_sending_messages
1010                                          : R.string.message_list_send_pending_messages_action;
1011                    break;
1012            }
1013            mListFooterText.setText(footerTextId);
1014        }
1015    }
1016
1017    /**
1018     * Handle a click in the list footer, which changes meaning depending on what we're looking at.
1019     */
1020    private void doFooterClick() {
1021        switch (mListFooterMode) {
1022            case LIST_FOOTER_MODE_NONE:         // should never happen
1023                break;
1024            case LIST_FOOTER_MODE_REFRESH:
1025                onRefresh();
1026                break;
1027            case LIST_FOOTER_MODE_MORE:
1028                onLoadMoreMessages();
1029                break;
1030            case LIST_FOOTER_MODE_SEND:
1031                onSendPendingMessages();
1032                break;
1033        }
1034    }
1035
1036    /**
1037     * Async task for finding a single mailbox by type (possibly even going to the network).
1038     *
1039     * This is much too complex, as implemented.  It uses this AsyncTask to check for a mailbox,
1040     * then (if not found) a Controller call to refresh mailboxes from the server, and a handler
1041     * to relaunch this task (a 2nd time) to read the results of the network refresh.  The core
1042     * problem is that we have two different non-UI-thread jobs (reading DB and reading network)
1043     * and two different paradigms for dealing with them.  Some unification would be needed here
1044     * to make this cleaner.
1045     *
1046     * TODO: If this problem spreads to other operations, find a cleaner way to handle it.
1047     */
1048    private class FindMailboxTask extends AsyncTask<Void, Void, Long> {
1049
1050        private long mAccountId;
1051        private int mMailboxType;
1052        private boolean mOkToRecurse;
1053
1054        /**
1055         * Special constructor to cache some local info
1056         */
1057        public FindMailboxTask(long accountId, int mailboxType, boolean okToRecurse) {
1058            mAccountId = accountId;
1059            mMailboxType = mailboxType;
1060            mOkToRecurse = okToRecurse;
1061        }
1062
1063        @Override
1064        protected Long doInBackground(Void... params) {
1065            // See if we can find the requested mailbox in the DB.
1066            long mailboxId = Mailbox.findMailboxOfType(MessageList.this, mAccountId, mMailboxType);
1067            if (mailboxId == Mailbox.NO_MAILBOX && mOkToRecurse) {
1068                // Not found - launch network lookup
1069                mControllerCallback.mWaitForMailboxType = mMailboxType;
1070                mController.updateMailboxList(mAccountId, mControllerCallback);
1071            }
1072            return mailboxId;
1073        }
1074
1075        @Override
1076        protected void onPostExecute(Long mailboxId) {
1077            if (mailboxId == null) {
1078                return;
1079            }
1080            if (mailboxId != Mailbox.NO_MAILBOX) {
1081                mMailboxId = mailboxId;
1082                mSetTitleTask = new SetTitleTask(mMailboxId);
1083                mSetTitleTask.execute();
1084                mLoadMessagesTask = new LoadMessagesTask(mMailboxId, mAccountId);
1085                mLoadMessagesTask.execute();
1086            }
1087        }
1088    }
1089
1090    /**
1091     * Async task for loading a single folder out of the UI thread
1092     *
1093     * The code here (for merged boxes) is a placeholder/hack and should be replaced.  Some
1094     * specific notes:
1095     * TODO:  Move the double query into a specialized URI that returns all inbox messages
1096     * and do the dirty work in raw SQL in the provider.
1097     * TODO:  Generalize the query generation so we can reuse it in MessageView (for next/prev)
1098     */
1099    private class LoadMessagesTask extends AsyncTask<Void, Void, Cursor> {
1100
1101        private long mMailboxKey;
1102        private long mAccountKey;
1103
1104        /**
1105         * Special constructor to cache some local info
1106         */
1107        public LoadMessagesTask(long mailboxKey, long accountKey) {
1108            mMailboxKey = mailboxKey;
1109            mAccountKey = accountKey;
1110        }
1111
1112        @Override
1113        protected Cursor doInBackground(Void... params) {
1114            String selection =
1115                Utility.buildMailboxIdSelection(MessageList.this.mResolver, mMailboxKey);
1116            Cursor c = MessageList.this.managedQuery(
1117                    EmailContent.Message.CONTENT_URI, MESSAGE_PROJECTION,
1118                    selection, null, EmailContent.MessageColumns.TIMESTAMP + " DESC");
1119            return c;
1120        }
1121
1122        @Override
1123        protected void onPostExecute(Cursor cursor) {
1124            if (cursor == null || cursor.isClosed()) {
1125                return;
1126            }
1127            MessageList.this.mListAdapter.changeCursor(cursor);
1128            // changeCursor occurs the jumping of position in ListView, so it's need to restore
1129            // the position;
1130            restoreListPosition();
1131            autoRefreshStaleMailbox();
1132            // Reset the "new messages" count in the service, since we're seeing them now
1133            if (mMailboxKey == Mailbox.QUERY_ALL_INBOXES) {
1134                MailService.resetNewMessageCount(MessageList.this, -1);
1135            } else if (mMailboxKey >= 0 && mAccountKey != -1) {
1136                MailService.resetNewMessageCount(MessageList.this, mAccountKey);
1137            }
1138        }
1139    }
1140
1141    private class SetTitleTask extends AsyncTask<Void, Void, String[]> {
1142
1143        private long mMailboxKey;
1144
1145        public SetTitleTask(long mailboxKey) {
1146            mMailboxKey = mailboxKey;
1147        }
1148
1149        @Override
1150        protected String[] doInBackground(Void... params) {
1151            // Check special Mailboxes
1152            if (mMailboxKey == Mailbox.QUERY_ALL_INBOXES) {
1153                return new String[] {null,
1154                        getString(R.string.account_folder_list_summary_inbox)};
1155            } else if (mMailboxKey == Mailbox.QUERY_ALL_FAVORITES) {
1156                return new String[] {null,
1157                        getString(R.string.account_folder_list_summary_starred)};
1158            } else if (mMailboxKey == Mailbox.QUERY_ALL_DRAFTS) {
1159                return new String[] {null,
1160                        getString(R.string.account_folder_list_summary_drafts)};
1161            } else if (mMailboxKey == Mailbox.QUERY_ALL_OUTBOX) {
1162                return new String[] {null,
1163                        getString(R.string.account_folder_list_summary_outbox)};
1164            }
1165            String accountName = null;
1166            String mailboxName = null;
1167            String accountKey = null;
1168            Cursor c = MessageList.this.mResolver.query(Mailbox.CONTENT_URI,
1169                    MAILBOX_NAME_PROJECTION, ID_SELECTION,
1170                    new String[] { Long.toString(mMailboxKey) }, null);
1171            try {
1172                if (c.moveToFirst()) {
1173                    mailboxName = Utility.FolderProperties.getInstance(MessageList.this)
1174                            .getDisplayName(c.getInt(MAILBOX_NAME_COLUMN_TYPE));
1175                    if (mailboxName == null) {
1176                        mailboxName = c.getString(MAILBOX_NAME_COLUMN_ID);
1177                    }
1178                    accountKey = c.getString(MAILBOX_NAME_COLUMN_ACCOUNT_KEY);
1179                }
1180            } finally {
1181                c.close();
1182            }
1183            if (accountKey != null) {
1184                c = MessageList.this.mResolver.query(Account.CONTENT_URI,
1185                        ACCOUNT_NAME_PROJECTION, ID_SELECTION, new String[] { accountKey },
1186                        null);
1187                try {
1188                    if (c.moveToFirst()) {
1189                        accountName = c.getString(ACCOUNT_DISPLAY_NAME_COLUMN_ID);
1190                    }
1191                } finally {
1192                    c.close();
1193                }
1194            }
1195            return new String[] {accountName, mailboxName};
1196        }
1197
1198        @Override
1199        protected void onPostExecute(String[] names) {
1200            if (names == null) {
1201                return;
1202            }
1203            if (names[0] != null) {
1204                mRightTitle.setText(names[0]);
1205            }
1206            if (names[1] != null) {
1207                mLeftTitle.setText(names[1]);
1208            }
1209        }
1210    }
1211
1212    /**
1213     * Handler for UI-thread operations (when called from callbacks or any other threads)
1214     */
1215    class MessageListHandler extends Handler {
1216        private static final int MSG_PROGRESS = 1;
1217        private static final int MSG_LOOKUP_MAILBOX_TYPE = 2;
1218        private static final int MSG_ERROR_BANNER = 3;
1219        private static final int MSG_REQUERY_LIST = 4;
1220
1221        @Override
1222        public void handleMessage(android.os.Message msg) {
1223            switch (msg.what) {
1224                case MSG_PROGRESS:
1225                    boolean visible = (msg.arg1 != 0);
1226                    if (visible) {
1227                        mProgressIcon.setVisibility(View.VISIBLE);
1228                    } else {
1229                        mProgressIcon.setVisibility(View.GONE);
1230                    }
1231                    if (mListFooterProgress != null) {
1232                        mListFooterProgress.setVisibility(visible ? View.VISIBLE : View.GONE);
1233                    }
1234                    setListFooterText(visible);
1235                    break;
1236                case MSG_LOOKUP_MAILBOX_TYPE:
1237                    // kill running async task, if any
1238                    if (mFindMailboxTask != null &&
1239                            mFindMailboxTask.getStatus() != FindMailboxTask.Status.FINISHED) {
1240                        mFindMailboxTask.cancel(true);
1241                        mFindMailboxTask = null;
1242                    }
1243                    // start new one.  do not recurse back to controller.
1244                    long accountId = ((Long)msg.obj).longValue();
1245                    int mailboxType = msg.arg1;
1246                    mFindMailboxTask = new FindMailboxTask(accountId, mailboxType, false);
1247                    mFindMailboxTask.execute();
1248                    break;
1249                case MSG_ERROR_BANNER:
1250                    String message = (String) msg.obj;
1251                    boolean isVisible = mErrorBanner.getVisibility() == View.VISIBLE;
1252                    if (message != null) {
1253                        mErrorBanner.setText(message);
1254                        if (!isVisible) {
1255                            mErrorBanner.setVisibility(View.VISIBLE);
1256                            mErrorBanner.startAnimation(
1257                                    AnimationUtils.loadAnimation(
1258                                            MessageList.this, R.anim.header_appear));
1259                        }
1260                    } else {
1261                        if (isVisible) {
1262                            mErrorBanner.setVisibility(View.GONE);
1263                            mErrorBanner.startAnimation(
1264                                    AnimationUtils.loadAnimation(
1265                                            MessageList.this, R.anim.header_disappear));
1266                        }
1267                    }
1268                    break;
1269                case MSG_REQUERY_LIST:
1270                    mListAdapter.doRequery();
1271                    if (mMultiSelectPanel.getVisibility() == View.VISIBLE) {
1272                        updateFooterButtonNames();
1273                    }
1274                    break;
1275                default:
1276                    super.handleMessage(msg);
1277            }
1278        }
1279
1280        /**
1281         * Call from any thread to start/stop progress indicator(s)
1282         * @param progress true to start, false to stop
1283         */
1284        public void progress(boolean progress) {
1285            android.os.Message msg = android.os.Message.obtain();
1286            msg.what = MSG_PROGRESS;
1287            msg.arg1 = progress ? 1 : 0;
1288            sendMessage(msg);
1289        }
1290
1291        /**
1292         * Called from any thread to look for a mailbox of a specific type.  This is designed
1293         * to be called from the Controller's MailboxList callback;  It instructs the async task
1294         * not to recurse, in case the mailbox is not found after this.
1295         *
1296         * See FindMailboxTask for more notes on this handler.
1297         */
1298        public void lookupMailboxType(long accountId, int mailboxType) {
1299            android.os.Message msg = android.os.Message.obtain();
1300            msg.what = MSG_LOOKUP_MAILBOX_TYPE;
1301            msg.arg1 = mailboxType;
1302            msg.obj = Long.valueOf(accountId);
1303            sendMessage(msg);
1304        }
1305
1306        /**
1307         * Called from any thread to show or hide the connection error banner.
1308         * @param message error text or null to hide the box
1309         */
1310        public void showErrorBanner(String message) {
1311            android.os.Message msg = android.os.Message.obtain();
1312            msg.what = MSG_ERROR_BANNER;
1313            msg.obj = message;
1314            sendMessage(msg);
1315        }
1316
1317        /**
1318         * Called from any thread to signal that the list adapter should requery and update.
1319         */
1320        public void requeryList() {
1321            sendEmptyMessage(MSG_REQUERY_LIST);
1322        }
1323    }
1324
1325    /**
1326     * Callback for async Controller results.
1327     */
1328    private class ControllerResults implements Controller.Result {
1329
1330        // This is used to alter the connection banner operation for sending messages
1331        MessagingException mSendMessageException;
1332
1333        // These are preset for use by updateMailboxListCallback
1334        int mWaitForMailboxType = -1;
1335
1336        // TODO check accountKey and only react to relevant notifications
1337        public void updateMailboxListCallback(MessagingException result, long accountKey,
1338                int progress) {
1339            // no updateBanner here, we are only listing a single mailbox
1340            updateProgress(result, progress);
1341            if (progress == 100) {
1342                mHandler.lookupMailboxType(accountKey, mWaitForMailboxType);
1343            }
1344        }
1345
1346        // TODO check accountKey and only react to relevant notifications
1347        public void updateMailboxCallback(MessagingException result, long accountKey,
1348                long mailboxKey, int progress, int numNewMessages) {
1349            updateBanner(result, progress, mailboxKey);
1350            if (result != null || progress == 100) {
1351                Email.updateMailboxRefreshTime(mMailboxId);
1352            }
1353            updateProgress(result, progress);
1354        }
1355
1356        public void loadMessageForViewCallback(MessagingException result, long messageId,
1357                int progress) {
1358        }
1359
1360        public void loadAttachmentCallback(MessagingException result, long messageId,
1361                long attachmentId, int progress) {
1362        }
1363
1364        public void serviceCheckMailCallback(MessagingException result, long accountId,
1365                long mailboxId, int progress, long tag) {
1366        }
1367
1368        /**
1369         * We alter the updateBanner hysteresis here to capture any failures and handle
1370         * them just once at the end.  This callback is overly overloaded:
1371         *  result == null, messageId == -1, progress == 0:     start batch send
1372         *  result == null, messageId == xx, progress == 0:     start sending one message
1373         *  result == xxxx, messageId == xx, progress == 0;     failed sending one message
1374         *  result == null, messageId == -1, progres == 100;    finish sending batch
1375         */
1376        public void sendMailCallback(MessagingException result, long accountId, long messageId,
1377                int progress) {
1378            if (mListFooterMode == LIST_FOOTER_MODE_SEND) {
1379                // reset captured error when we start sending one or more messages
1380                if (messageId == -1 && result == null && progress == 0) {
1381                    mSendMessageException = null;
1382                }
1383                // capture first exception that comes along
1384                if (result != null && mSendMessageException == null) {
1385                    mSendMessageException = result;
1386                }
1387                // if we're completing the sequence, change the banner state
1388                if (messageId == -1 && progress == 100) {
1389                    updateBanner(mSendMessageException, progress, mMailboxId);
1390                }
1391                // always update the spinner, which has less state to worry about
1392                updateProgress(result, progress);
1393            }
1394        }
1395
1396        private void updateProgress(MessagingException result, int progress) {
1397            if (result != null || progress == 100) {
1398                mHandler.progress(false);
1399            } else if (progress == 0) {
1400                mHandler.progress(true);
1401            }
1402        }
1403
1404        /**
1405         * Show or hide the connection error banner, and convert the various MessagingException
1406         * variants into localizable text.  There is hysteresis in the show/hide logic:  Once shown,
1407         * the banner will remain visible until some progress is made on the connection.  The
1408         * goal is to keep it from flickering during retries in a bad connection state.
1409         *
1410         * @param result
1411         * @param progress
1412         */
1413        private void updateBanner(MessagingException result, int progress, long mailboxKey) {
1414            if (mailboxKey != mMailboxId) {
1415                return;
1416            }
1417            if (result != null) {
1418                int id = R.string.status_network_error;
1419                if (result instanceof AuthenticationFailedException) {
1420                    id = R.string.account_setup_failed_dlg_auth_message;
1421                } else if (result instanceof CertificateValidationException) {
1422                    id = R.string.account_setup_failed_dlg_certificate_message;
1423                } else {
1424                    switch (result.getExceptionType()) {
1425                        case MessagingException.IOERROR:
1426                            id = R.string.account_setup_failed_ioerror;
1427                            break;
1428                        case MessagingException.TLS_REQUIRED:
1429                            id = R.string.account_setup_failed_tls_required;
1430                            break;
1431                        case MessagingException.AUTH_REQUIRED:
1432                            id = R.string.account_setup_failed_auth_required;
1433                            break;
1434                        case MessagingException.GENERAL_SECURITY:
1435                            id = R.string.account_setup_failed_security;
1436                            break;
1437                    }
1438                }
1439                mHandler.showErrorBanner(getString(id));
1440            } else if (progress > 0) {
1441                mHandler.showErrorBanner(null);
1442            }
1443        }
1444    }
1445
1446    /**
1447     * This class implements the adapter for displaying messages based on cursors.
1448     */
1449    /* package */ class MessageListAdapter extends CursorAdapter {
1450
1451        public static final int COLUMN_ID = 0;
1452        public static final int COLUMN_MAILBOX_KEY = 1;
1453        public static final int COLUMN_ACCOUNT_KEY = 2;
1454        public static final int COLUMN_DISPLAY_NAME = 3;
1455        public static final int COLUMN_SUBJECT = 4;
1456        public static final int COLUMN_DATE = 5;
1457        public static final int COLUMN_READ = 6;
1458        public static final int COLUMN_FAVORITE = 7;
1459        public static final int COLUMN_ATTACHMENTS = 8;
1460
1461        Context mContext;
1462        private LayoutInflater mInflater;
1463        private Drawable mAttachmentIcon;
1464        private Drawable mFavoriteIconOn;
1465        private Drawable mFavoriteIconOff;
1466        private Drawable mSelectedIconOn;
1467        private Drawable mSelectedIconOff;
1468
1469        private ColorStateList mTextColorPrimary;
1470        private ColorStateList mTextColorSecondary;
1471
1472        // Timer to control the refresh rate of the list
1473        private final RefreshTimer mRefreshTimer = new RefreshTimer();
1474        // Last time we allowed a refresh of the list
1475        private long mLastRefreshTime = 0;
1476        // How long we want to wait for refreshes (a good starting guess)
1477        // I suspect this could be lowered down to even 1000 or so, but this seems ok for now
1478        private static final long REFRESH_INTERVAL_MS = 2500;
1479
1480        private java.text.DateFormat mDateFormat;
1481        private java.text.DateFormat mTimeFormat;
1482
1483        private HashSet<Long> mChecked = new HashSet<Long>();
1484
1485        public MessageListAdapter(Context context) {
1486            super(context, null, true);
1487            mContext = context;
1488            mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1489
1490            Resources resources = context.getResources();
1491            mAttachmentIcon = resources.getDrawable(R.drawable.ic_mms_attachment_small);
1492            mFavoriteIconOn = resources.getDrawable(R.drawable.btn_star_big_buttonless_dark_on);
1493            mFavoriteIconOff = resources.getDrawable(R.drawable.btn_star_big_buttonless_dark_off);
1494            mSelectedIconOn = resources.getDrawable(R.drawable.btn_check_buttonless_dark_on);
1495            mSelectedIconOff = resources.getDrawable(R.drawable.btn_check_buttonless_dark_off);
1496
1497            Theme theme = context.getTheme();
1498            TypedArray array;
1499            array = theme.obtainStyledAttributes(new int[] { android.R.attr.textColorPrimary });
1500            mTextColorPrimary = resources.getColorStateList(array.getResourceId(0, 0));
1501            array = theme.obtainStyledAttributes(new int[] { android.R.attr.textColorSecondary });
1502            mTextColorSecondary = resources.getColorStateList(array.getResourceId(0, 0));
1503
1504            mDateFormat = android.text.format.DateFormat.getDateFormat(context);    // short date
1505            mTimeFormat = android.text.format.DateFormat.getTimeFormat(context);    // 12/24 time
1506        }
1507
1508        /**
1509         * We override onContentChange to throttle the refresh, which can happen way too often
1510         * on syncing a large list (up to many times per second).  This will prevent ANR's during
1511         * initial sync and potentially at other times as well.
1512         */
1513        @Override
1514        protected synchronized void onContentChanged() {
1515            if (mCursor != null && !mCursor.isClosed()) {
1516                long sinceRefresh = SystemClock.elapsedRealtime() - mLastRefreshTime;
1517                mRefreshTimer.schedule(REFRESH_INTERVAL_MS - sinceRefresh);
1518            }
1519        }
1520
1521        /**
1522         * Called in UI thread only, from Handler, to complete the requery that we
1523         * intercepted in onContentChanged().
1524         */
1525        public void doRequery() {
1526            if (mCursor != null && !mCursor.isClosed()) {
1527                mDataValid = mCursor.requery();
1528                notifyDataSetChanged();
1529            }
1530        }
1531
1532        class RefreshTimer extends Timer {
1533            private TimerTask timerTask = null;
1534
1535            protected void clear() {
1536                timerTask = null;
1537            }
1538
1539            protected synchronized void schedule(long delay) {
1540                if (timerTask != null) return;
1541                if (delay < 0) {
1542                    refreshList();
1543                } else {
1544                    timerTask = new RefreshTimerTask();
1545                    schedule(timerTask, delay);
1546                }
1547            }
1548        }
1549
1550        class RefreshTimerTask extends TimerTask {
1551            @Override
1552            public void run() {
1553                refreshList();
1554            }
1555        }
1556
1557        /**
1558         * Do the work of requerying the list and notifying the UI of changed data
1559         * Make sure we call notifyDataSetChanged on the UI thread.
1560         */
1561        private synchronized void refreshList() {
1562            if (Email.LOGD) {
1563                Log.d("messageList", "refresh: "
1564                        + (SystemClock.elapsedRealtime() - mLastRefreshTime) + "ms");
1565            }
1566            mHandler.requeryList();
1567            mLastRefreshTime = SystemClock.elapsedRealtime();
1568            mRefreshTimer.clear();
1569        }
1570
1571        public Set<Long> getSelectedSet() {
1572            return mChecked;
1573        }
1574
1575        @Override
1576        public void bindView(View view, Context context, Cursor cursor) {
1577            // Reset the view (in case it was recycled) and prepare for binding
1578            MessageListItem itemView = (MessageListItem) view;
1579            itemView.bindViewInit(this, true);
1580
1581            // Load the public fields in the view (for later use)
1582            itemView.mMessageId = cursor.getLong(COLUMN_ID);
1583            itemView.mMailboxId = cursor.getLong(COLUMN_MAILBOX_KEY);
1584            itemView.mAccountId = cursor.getLong(COLUMN_ACCOUNT_KEY);
1585            itemView.mRead = cursor.getInt(COLUMN_READ) != 0;
1586            itemView.mFavorite = cursor.getInt(COLUMN_FAVORITE) != 0;
1587            itemView.mSelected = mChecked.contains(Long.valueOf(itemView.mMessageId));
1588
1589            // Load the UI
1590            View chipView = view.findViewById(R.id.chip);
1591            int chipResId = mColorChipResIds[(int)itemView.mAccountId % mColorChipResIds.length];
1592            chipView.setBackgroundResource(chipResId);
1593
1594            TextView fromView = (TextView) view.findViewById(R.id.from);
1595            String text = cursor.getString(COLUMN_DISPLAY_NAME);
1596            fromView.setText(text);
1597
1598            TextView subjectView = (TextView) view.findViewById(R.id.subject);
1599            text = cursor.getString(COLUMN_SUBJECT);
1600            subjectView.setText(text);
1601
1602            boolean hasAttachments = cursor.getInt(COLUMN_ATTACHMENTS) != 0;
1603            subjectView.setCompoundDrawablesWithIntrinsicBounds(null, null,
1604                    hasAttachments ? mAttachmentIcon : null, null);
1605
1606            // TODO ui spec suggests "time", "day", "date" - implement "day"
1607            TextView dateView = (TextView) view.findViewById(R.id.date);
1608            long timestamp = cursor.getLong(COLUMN_DATE);
1609            Date date = new Date(timestamp);
1610            if (Utility.isDateToday(date)) {
1611                text = mTimeFormat.format(date);
1612            } else {
1613                text = mDateFormat.format(date);
1614            }
1615            dateView.setText(text);
1616
1617            if (itemView.mRead) {
1618                subjectView.setTypeface(Typeface.DEFAULT);
1619                fromView.setTypeface(Typeface.DEFAULT);
1620                fromView.setTextColor(mTextColorSecondary);
1621                view.setBackgroundDrawable(context.getResources().getDrawable(
1622                        R.drawable.message_list_item_background_read));
1623            } else {
1624                subjectView.setTypeface(Typeface.DEFAULT_BOLD);
1625                fromView.setTypeface(Typeface.DEFAULT_BOLD);
1626                fromView.setTextColor(mTextColorPrimary);
1627                view.setBackgroundDrawable(context.getResources().getDrawable(
1628                        R.drawable.message_list_item_background_unread));
1629            }
1630
1631            ImageView selectedView = (ImageView) view.findViewById(R.id.selected);
1632            selectedView.setImageDrawable(itemView.mSelected ? mSelectedIconOn : mSelectedIconOff);
1633
1634            ImageView favoriteView = (ImageView) view.findViewById(R.id.favorite);
1635            favoriteView.setImageDrawable(itemView.mFavorite ? mFavoriteIconOn : mFavoriteIconOff);
1636        }
1637
1638        @Override
1639        public View newView(Context context, Cursor cursor, ViewGroup parent) {
1640            return mInflater.inflate(R.layout.message_list_item, parent, false);
1641        }
1642
1643        /**
1644         * This is used as a callback from the list items, to set the selected state
1645         *
1646         * @param itemView the item being changed
1647         * @param newSelected the new value of the selected flag (checkbox state)
1648         */
1649        public void updateSelected(MessageListItem itemView, boolean newSelected) {
1650            ImageView selectedView = (ImageView) itemView.findViewById(R.id.selected);
1651            selectedView.setImageDrawable(newSelected ? mSelectedIconOn : mSelectedIconOff);
1652
1653            // Set checkbox state in list, and show/hide panel if necessary
1654            Long id = Long.valueOf(itemView.mMessageId);
1655            if (newSelected) {
1656                mChecked.add(id);
1657            } else {
1658                mChecked.remove(id);
1659            }
1660
1661            MessageList.this.showMultiPanel(mChecked.size() > 0);
1662        }
1663
1664        /**
1665         * This is used as a callback from the list items, to set the favorite state
1666         *
1667         * @param itemView the item being changed
1668         * @param newFavorite the new value of the favorite flag (star state)
1669         */
1670        public void updateFavorite(MessageListItem itemView, boolean newFavorite) {
1671            ImageView favoriteView = (ImageView) itemView.findViewById(R.id.favorite);
1672            favoriteView.setImageDrawable(newFavorite ? mFavoriteIconOn : mFavoriteIconOff);
1673            onSetMessageFavorite(itemView.mMessageId, newFavorite);
1674        }
1675    }
1676}
1677