summaryrefslogtreecommitdiffstats
path: root/src/com/android/mail/ui/MailActionBar.java
blob: af170283a201e488c504aa825567924082d35ba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * Copyright (C) 2012 Google Inc.
 * Licensed to The Android Open Source Project.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.mail.ui;

import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.android.mail.R;
import com.android.mail.AccountSpinnerAdapter;
import com.android.mail.ConversationListContext;
import com.android.mail.providers.Account;
import com.android.mail.providers.UIProvider.AccountCapabilities;
import com.android.mail.providers.Folder;

/**
 * View to manage the various states of the Mail Action Bar
 *
 * TODO(viki): Include ConversationSubjectDisplayer here as well.
 */
public final class MailActionBar extends LinearLayout implements ActionBarView {
    /**
     * This interface is used to send notifications back to the calling
     * activity. MenuHandler takes care of updating the provider, so this
     * interface should be used for notification purposes only (such as updating
     * the UI).
     */
    // TODO(viki): This callback is currently unused and may be entirely unnecessary in the new
    // code, where the Actionbar is switched into navigation mode, relying on the framework for most
    // heavy lifting. Also, we can switch ViewMode to the appropriate mode and rely on all UI
    // components updating through ViewMode change listeners.
    public interface Callback {
        /**
         * Enter search mode
         */
        void enterSearchMode();

        /**
         * Exits search mode
         */
        void exitSearchMode();

        /**
         * Returns the current account.
         */
        Account getCurrentAccount();

        /**
         * Called when the TwoPaneActionBar wants to get the current conversation list context.
         */
        ConversationListContext getCurrentListContext();

        /**
         * Invoked when the user is already viewing search results
         * and enters a new query.
         * @param string Query
         */
        void reloadSearch(String string);

        void showFolderList();

        void startActionBarStatusCursorLoader(String account);

        void stopActionBarStatusCursorLoader(String account);
    }

    private String[] mAccountNames;
    private ActionBar mActionBar;
    private RestrictedActivity mActivity;
    private ActivityController mCallback;
    private View mFolderView;
    /**
     * The current mode of the ActionBar. This references constants in {@link ViewMode}
     */
    private int mMode = ViewMode.UNKNOWN;

    private MenuItem mRefreshItem;

    private MenuItem mSearch;
    SpinnerAdapter mSpinner;
    /**
     * The account currently being shown
     */
    private Account mAccount;

    // TODO(viki): This is a SnippetTextView in the Gmail source code. Resolve.
    private TextView mSubjectView;

    public MailActionBar(Context context) {
        this(context, null);
    }

    public MailActionBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MailActionBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean createOptionsMenu(Menu menu) {
        // If the mode is valid, then set the initial menu
        if (mMode == ViewMode.UNKNOWN) {
            return false;
        }
        mActivity.getMenuInflater().inflate(getOptionsMenuId(), menu);
        // mSearch = menu.findItem(R.id.search);
        // mRefreshItem = menu.findItem(R.id.refresh);
        return true;
    }

    @Override
    public int getOptionsMenuId() {
        // Relies on the ordering of the view modes, since they are integer constants.
        final int[] modeMenu = {
                // 0: UNKNOWN
                R.menu.conversation_list_menu,
                // 1: CONVERSATION
                R.menu.conversation_actions,
                // 2: CONVERSATION_LIST
                R.menu.conversation_list_menu,
                // 3: FOLDER_LIST
                R.menu.folder_list_menu,
                // 4: SEARCH_RESULTS
                R.menu.conversation_list_menu
        };
        return modeMenu[mMode];
    }

    @Override
    public void handleRestore(Bundle savedInstanceState) {
    }

    @Override
    public void handleSaveInstanceState(Bundle outState) {
    }

    @Override
    public void initialize(RestrictedActivity activity, ActivityController callback, ViewMode viewMode,
            ActionBar actionBar) {
        mActionBar = actionBar;
        mCallback = callback;
        mActivity = activity;

        // Set the mode to Navigation mode
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        mSpinner = new AccountSpinnerAdapter(getContext());
        mActionBar.setListNavigationCallbacks(mSpinner, this);
    }

    @Override
    public boolean onNavigationItemSelected(int position, long id) {
        final int type = mSpinner.getItemViewType(position);
        switch (type) {
            case AccountSpinnerAdapter.TYPE_ACCOUNT:
                mCallback.onAccountChanged((Account) mSpinner.getItem(position));
                // Get the capabilities associated with this account.
                final Object item = mSpinner.getItem(position);
                assert (item instanceof Account);
                mAccount = (Account) item;
                break;
        }
        return false;
    }

    @Override
    public void onPause() {
    }

    @Override
    public void onResume() {
    }

    @Override
    public void onStatusResult(String account, int status) {
        // Update the inbox folder if required
        mCallback.stopActionBarStatusCursorLoader(account);
    }

    @Override
    public void onViewModeChanged(int newMode) {
        mMode = newMode;

        // Always update the options menu and redraw. This will read the new mode and redraw
        // the options menu.
        mActivity.invalidateOptionsMenu();
    }

    /**
     * If shouldSetView is true, then the view is made visible, otherwise its visiblity is View.GONE
     * @param view the view whose visibility is modified
     * @param shouldSetView if true, the view is made visible, GONE otherwise
     */
    private void setVisibility(int resourceId, boolean shouldSetView) {
        final View view = findViewById(resourceId);
        assert (view != null);
        final int visibility = shouldSetView ? View.VISIBLE : View.GONE;
        view.setVisibility(visibility);
    }

    @Override
    public boolean prepareOptionsMenu(Menu menu) {
        // We start out with every option enabled. Based on the current view, we disable actions
        // that are possible.
        if (mSubjectView != null){
            mSubjectView.setVisibility(GONE);
        }
        if (mFolderView != null){
            mFolderView.setVisibility(GONE);
        }

        switch (mMode){
            case ViewMode.UNKNOWN:
                if (mSearch != null){
                    mSearch.collapseActionView();
                }
                break;
            case ViewMode.CONVERSATION_LIST:
                // Show compose, search, labels, and sync based on the account
                // The only option that needs to be disabled is search
                setVisibility(R.id.search, mAccount.supportsCapability(
                        AccountCapabilities.FOLDER_SERVER_SEARCH));
                break;
            case ViewMode.CONVERSATION:
                setVisibility(R.id.y_button, mAccount.supportsCapability(
                        AccountCapabilities.ARCHIVE));
                setVisibility(R.id.report_spam, mAccount.supportsCapability(
                        AccountCapabilities.REPORT_SPAM));
                setVisibility(R.id.mute, mAccount.supportsCapability(AccountCapabilities.MUTE));
                break;
            case ViewMode.SEARCH_RESULTS:
                mActionBar.setDisplayHomeAsUpEnabled(true);
                if (mSearch != null) {
                    mSearch.collapseActionView();
                }
            case ViewMode.FOLDER_LIST:
                break;
        }
        return false;
    }

    @Override
    public void removeBackButton() {
        if (mActionBar == null) {
            return;
        }
        mActionBar.setDisplayOptions(
                ActionBar.DISPLAY_SHOW_HOME,
                ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
        mActivity.getActionBar().setHomeButtonEnabled(false);
    }

    @Override
    public void setBackButton() {
        if (mActionBar == null){
            return;
        }
        mActionBar.setDisplayOptions(
                ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME,
                ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
        mActivity.getActionBar().setHomeButtonEnabled(true);
    }

    @Override
    public void setFolder(String folder) {
        // TODO(viki): Add this functionality to change the label.
    }

    @Override
    public void updateActionBar(String[] accounts, String currentAccount) {
    }
}