summaryrefslogtreecommitdiffstats
path: root/src/com/android/mail/ui/MaterialSearchViewController.java
blob: 9e03a660780d598c51d3500f854a10887c2336a7 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * Copyright (C) 2014 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.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.android.mail.ConversationListContext;
import com.android.mail.R;
import com.android.mail.providers.SearchRecentSuggestionsProvider;
import com.android.mail.utils.ViewUtils;

import java.util.Locale;

/**
 * Controller for interactions between ActivityController and our custom search views.
 */
public class MaterialSearchViewController implements ViewMode.ModeChangeListener,
        TwoPaneLayout.ConversationListLayoutListener {
    private static final long FADE_IN_OUT_DURATION_MS = 150;

    // The controller is not in search mode. Both search action bar and the suggestion list
    // are not visible to the user.
    public static final int SEARCH_VIEW_STATE_GONE = 0;
    // The controller is actively in search (as in the action bar is focused and the user can type
    // into the search query). Both the search action bar and the suggestion list are visible.
    public static final int SEARCH_VIEW_STATE_VISIBLE = 1;
    // The controller is in a search ViewMode but not actively searching. This is relevant when
    // we have to show the search actionbar on top while the user is not interacting with it.
    public static final int SEARCH_VIEW_STATE_ONLY_ACTIONBAR = 2;

    private static final String EXTRA_CONTROLLER_STATE = "extraSearchViewControllerViewState";

    private MailActivity mActivity;
    private ActivityController mController;

    private SearchRecentSuggestionsProvider mSuggestionsProvider;

    private MaterialSearchActionView mSearchActionView;
    private MaterialSearchSuggestionsList mSearchSuggestionList;

    private int mViewMode;
    private int mControllerState;
    private int mEndXCoordForTabletLandscape;

    private boolean mSavePending;
    private boolean mDestroyProvider;

    public MaterialSearchViewController(MailActivity activity, ActivityController controller,
            Intent intent, Bundle savedInstanceState) {
        mActivity = activity;
        mController = controller;

        final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        final boolean supportVoice =
                voiceIntent.resolveActivity(mActivity.getPackageManager()) != null;

        mSuggestionsProvider = mActivity.getSuggestionsProvider();
        mSearchSuggestionList = (MaterialSearchSuggestionsList) mActivity.findViewById(
                R.id.search_overlay_view);
        mSearchSuggestionList.setController(this, mSuggestionsProvider);
        mSearchActionView = (MaterialSearchActionView) mActivity.findViewById(
                R.id.search_actionbar_view);
        mSearchActionView.setController(this, intent.getStringExtra(
                ConversationListContext.EXTRA_SEARCH_QUERY), supportVoice);

        if (savedInstanceState != null && savedInstanceState.containsKey(EXTRA_CONTROLLER_STATE)) {
            mControllerState = savedInstanceState.getInt(EXTRA_CONTROLLER_STATE);
        }

        mActivity.getViewMode().addListener(this);
    }

    /**
     * This controller should not be used after this is called.
     */
    public void onDestroy() {
        mDestroyProvider = mSavePending;
        if (!mSavePending) {
            mSuggestionsProvider.cleanup();
        }
        mActivity.getViewMode().removeListener(this);
        mActivity = null;
        mController = null;
        mSearchActionView = null;
        mSearchSuggestionList = null;
    }

    public void saveState(Bundle outState) {
        outState.putInt(EXTRA_CONTROLLER_STATE, mControllerState);
    }

    @Override
    public void onViewModeChanged(int newMode) {
        final int oldMode = mViewMode;
        mViewMode = newMode;
        // Never animate visibility changes that are caused by view state changes.
        if (mController.shouldShowSearchBarByDefault(mViewMode)) {
            showSearchActionBar(SEARCH_VIEW_STATE_ONLY_ACTIONBAR, false /* animate */);
        } else if (oldMode == ViewMode.UNKNOWN) {
            showSearchActionBar(mControllerState, false /* animate */);
        } else {
            showSearchActionBar(SEARCH_VIEW_STATE_GONE, false /* animate */);
        }
    }

    @Override
    public void onConversationListLayout(int xEnd, boolean drawerOpen) {
        // Only care about the first layout
        if (mEndXCoordForTabletLandscape != xEnd) {
            // This is called when we get into tablet landscape mode
            mEndXCoordForTabletLandscape = xEnd;
            if (ViewMode.isSearchMode(mViewMode)) {
                final int defaultVisibility = mController.shouldShowSearchBarByDefault(mViewMode) ?
                        View.VISIBLE : View.GONE;
                setViewVisibilityAndAlpha(mSearchActionView,
                        drawerOpen ? View.INVISIBLE : defaultVisibility);
            }
            adjustViewForTwoPaneLandscape();
        }
    }

    public boolean handleBackPress() {
        final boolean shouldShowSearchBar = mController.shouldShowSearchBarByDefault(mViewMode);
        if (shouldShowSearchBar && mSearchSuggestionList.isShown()) {
            showSearchActionBar(SEARCH_VIEW_STATE_ONLY_ACTIONBAR);
            return true;
        } else if (!shouldShowSearchBar && mSearchActionView.isShown()) {
            showSearchActionBar(SEARCH_VIEW_STATE_GONE);
            return true;
        }
        return false;
    }

    /**
     * Set the new visibility state of the search controller.
     * @param state the new view state, must be one of the following options:
     *   {@link MaterialSearchViewController#SEARCH_VIEW_STATE_ONLY_ACTIONBAR},
     *   {@link MaterialSearchViewController#SEARCH_VIEW_STATE_VISIBLE},
     *   {@link MaterialSearchViewController#SEARCH_VIEW_STATE_GONE},
     */
    public void showSearchActionBar(int state) {
        // By default animate the visibility changes
        showSearchActionBar(state, true /* animate */);
    }

    /**
     * @param animate if true, the search bar and suggestion list will fade in/out of view.
     */
    public void showSearchActionBar(int state, boolean animate) {
        mControllerState = state;

        // ACTIONBAR is only applicable in search mode
        final boolean onlyActionBar = state == SEARCH_VIEW_STATE_ONLY_ACTIONBAR &&
                mController.shouldShowSearchBarByDefault(mViewMode);
        final boolean isStateVisible = state == SEARCH_VIEW_STATE_VISIBLE;

        final boolean isSearchBarVisible = isStateVisible || onlyActionBar;

        final int searchBarVisibility = isSearchBarVisible ? View.VISIBLE : View.GONE;
        final int suggestionListVisibility = isStateVisible ? View.VISIBLE : View.GONE;
        if (animate) {
            fadeInOutView(mSearchActionView, searchBarVisibility);
            fadeInOutView(mSearchSuggestionList, suggestionListVisibility);
        } else {
            setViewVisibilityAndAlpha(mSearchActionView, searchBarVisibility);
            setViewVisibilityAndAlpha(mSearchSuggestionList, suggestionListVisibility);
        }
        mSearchActionView.focusSearchBar(isStateVisible);

        final boolean useDefaultColor = !isSearchBarVisible || shouldAlignWithTl();
        final int statusBarColor = useDefaultColor ? R.color.mail_activity_status_bar_color :
                R.color.search_status_bar_color;
        ViewUtils.setStatusBarColor(mActivity, statusBarColor);

        // Specific actions for each view state
        if (onlyActionBar) {
            adjustViewForTwoPaneLandscape();
        } else if (isStateVisible) {
            // Set to default layout/assets
            mSearchActionView.adjustViewForTwoPaneLandscape(false /* do not align */, 0);
        } else {
            // For non-search view mode, clear the query term for search
            if (!ViewMode.isSearchMode(mViewMode)) {
                mSearchActionView.clearSearchQuery();
            }
        }
    }

    /**
     * Helper function to fade in/out the provided view by animating alpha.
     */
    private void fadeInOutView(final View v, final int visibility) {
        if (visibility == View.VISIBLE) {
            v.setVisibility(View.VISIBLE);
            v.animate()
                    .alpha(1f)
                    .setDuration(FADE_IN_OUT_DURATION_MS)
                    .setListener(null);
        } else {
            v.animate()
                    .alpha(0f)
                    .setDuration(FADE_IN_OUT_DURATION_MS)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            v.setVisibility(visibility);
                        }
                    });
        }
    }

    /**
     * Sets the view's visibility and alpha so that we are guaranteed that alpha = 1 when the view
     * is visible, and alpha = 0 otherwise.
     */
    private void setViewVisibilityAndAlpha(View v, int visibility) {
        v.setVisibility(visibility);
        if (visibility == View.VISIBLE) {
            v.setAlpha(1f);
        } else {
            v.setAlpha(0f);
        }
    }

    private boolean shouldAlignWithTl() {
        return mController.isTwoPaneLandscape() &&
                mControllerState == SEARCH_VIEW_STATE_ONLY_ACTIONBAR &&
                ViewMode.isSearchMode(mViewMode);
    }

    private void adjustViewForTwoPaneLandscape() {
        // Try to adjust if the layout happened already
        if (mEndXCoordForTabletLandscape != 0) {
            mSearchActionView.adjustViewForTwoPaneLandscape(shouldAlignWithTl(),
                    mEndXCoordForTabletLandscape);
        }
    }

    public void onQueryTextChanged(String query) {
        mSearchSuggestionList.setQuery(query);
    }

    public void onSearchCanceled() {
        // Special case search mode
        if (ViewMode.isSearchMode(mViewMode)) {
            mActivity.setResult(Activity.RESULT_OK);
            mActivity.finish();
        } else {
            mSearchActionView.clearSearchQuery();
            showSearchActionBar(SEARCH_VIEW_STATE_GONE);
        }
    }

    public void onSearchPerformed(String query) {
        query = query.trim();
        if (!TextUtils.isEmpty(query)) {
            mSearchActionView.clearSearchQuery();
            mController.executeSearch(query);
        }
    }

    public void onVoiceSearch() {
        final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault().getLanguage());

        // Some devices do not support the voice-to-speech functionality.
        try {
            mActivity.startActivityForResult(intent,
                    AbstractActivityController.VOICE_SEARCH_REQUEST_CODE);
        } catch (ActivityNotFoundException e) {
            final String toast =
                    mActivity.getResources().getString(R.string.voice_search_not_supported);
            Toast.makeText(mActivity, toast, Toast.LENGTH_LONG).show();
        }
    }

    public void saveRecentQuery(String query) {
        new SaveRecentQueryTask().execute(query);
    }

    // static asynctask to save the query in the background.
    private class SaveRecentQueryTask extends AsyncTask<String, Void, Void> {

        @Override
        protected void onPreExecute() {
            mSavePending = true;
        }

        @Override
        protected Void doInBackground(String... args) {
            mSuggestionsProvider.saveRecentQuery(args[0]);
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            if (mDestroyProvider) {
                mSuggestionsProvider.cleanup();
                mDestroyProvider = false;
            }
            mSavePending = false;
        }
    }
}