Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2012 Google Inc.
      3  * Licensed to The Android Open Source Project.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.mail.ui;
     19 
     20 import android.content.Context;
     21 import android.text.TextUtils;
     22 import android.util.AttributeSet;
     23 import android.view.Menu;
     24 import android.view.MenuItem;
     25 import android.widget.SearchView;
     26 
     27 import com.android.mail.ConversationListContext;
     28 import com.android.mail.utils.Utils;
     29 import com.android.mail.R;
     30 
     31 /**
     32  * This class is used to show a custom actionbar for the search activity. This doesn't have any
     33  * custom views, but it shows/hides various menu items based on the viewmode.
     34  */
     35 public class SearchMailActionBarView extends MailActionBarView {
     36 
     37     public SearchMailActionBarView(Context context) {
     38         this(context, null);
     39     }
     40 
     41     public SearchMailActionBarView(Context context, AttributeSet attrs) {
     42         this(context, attrs, 0);
     43     }
     44 
     45     public SearchMailActionBarView(Context context, AttributeSet attrs, int defStyle) {
     46         super(context, attrs, defStyle);
     47     }
     48 
     49     @Override
     50     public boolean onPrepareOptionsMenu(Menu menu) {
     51         super.onPrepareOptionsMenu(menu);
     52         Utils.setMenuItemVisibility(menu, R.id.manage_folders_item, false);
     53         switch (getMode()) {
     54             case ViewMode.SEARCH_RESULTS_LIST:
     55                 setSearchQueryTerm();
     56                 mActionBar.setDisplayHomeAsUpEnabled(true);
     57                 // And immediately give up focus to avoid keyboard popping and suggestions.
     58                 clearSearchFocus();
     59                 break;
     60             case ViewMode.SEARCH_RESULTS_CONVERSATION:
     61                 if (mIsOnTablet) {
     62                     setSearchQueryTerm();
     63                 }
     64                 mActionBar.setDisplayHomeAsUpEnabled(true);
     65                 // And immediately give up focus to avoid keyboard popping and suggestions.
     66                 clearSearchFocus();
     67                 break;
     68         }
     69         return false;
     70     }
     71 
     72     @Override
     73     public void onViewModeChanged(int newMode) {
     74         super.onViewModeChanged(newMode);
     75         switch (getMode()) {
     76             case ViewMode.SEARCH_RESULTS_LIST:
     77                 setEmptyMode();
     78                 break;
     79         }
     80     }
     81 
     82     /**
     83      * Remove focus from the search field to avoid
     84      * 1. The keyboard popping in and out.
     85      * 2. The search suggestions shown up.
     86      */
     87     private void clearSearchFocus() {
     88         // Remove focus from the search action menu in search results mode so
     89         // the IME and the suggestions don't get in the way.
     90         final MenuItem search = getSearch();
     91         if (search != null) {
     92             final SearchView searchWidget = (SearchView) search.getActionView();
     93             searchWidget.clearFocus();
     94         }
     95     }
     96 
     97     /**
     98      * Sets the query term in the text field, so the user can see what was searched for.
     99      */
    100     private void setSearchQueryTerm() {
    101         final MenuItem search = getSearch();
    102         if (search != null) {
    103             search.expandActionView();
    104             final String query = mActivity.getIntent().getStringExtra(
    105                     ConversationListContext.EXTRA_SEARCH_QUERY);
    106             final SearchView searchWidget = (SearchView) search.getActionView();
    107             if (!TextUtils.isEmpty(query)) {
    108                 searchWidget.setQuery(query, false);
    109             }
    110         }
    111     }
    112 
    113     @Override
    114     public boolean onMenuItemActionCollapse(MenuItem item) {
    115         super.onMenuItemActionCollapse(item);
    116         // When we are in the search activity, back closes the search action mode. At that point
    117         // we want to quit the activity entirely.
    118         final int mode = getMode();
    119         if (mode == ViewMode.SEARCH_RESULTS_LIST
    120                 || (Utils.showTwoPaneSearchResults(getContext())
    121                         && mode == ViewMode.SEARCH_RESULTS_CONVERSATION)) {
    122 
    123             // When the action menu is collapsed, the search activity has finished.  We should exit
    124             // search at this point
    125             mController.exitSearchMode();
    126         }
    127         // The return value here is whether we want to collapse the action mode. Since we want to
    128         // collapse the action mode, we should return true.
    129         return true;
    130     }
    131 }
    132