1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.android.deskclock.actionbarmenu; 18 19 import android.os.Bundle; 20 import android.support.v4.view.MenuItemCompat; 21 import android.support.v7.widget.SearchView; 22 import android.support.v7.widget.SearchView.OnQueryTextListener; 23 import android.view.Menu; 24 import android.view.MenuItem; 25 import android.view.View; 26 import android.view.inputmethod.EditorInfo; 27 28 import com.android.deskclock.R; 29 30 /** 31 * {@link MenuItemController} for search menu. 32 */ 33 public final class SearchMenuItemController extends AbstractMenuItemController { 34 35 private static final String KEY_SEARCH_QUERY = "search_query"; 36 private static final String KEY_SEARCH_MODE = "search_mode"; 37 private static final int SEARCH_MENU_RES_ID = R.id.menu_item_search; 38 private final SearchView.OnQueryTextListener mQueryListener; 39 private final SearchModeChangeListener mSearchModeChangeListener; 40 private String mQuery = ""; 41 private boolean mSearchMode; 42 SearchMenuItemController(OnQueryTextListener queryListener, Bundle savedState)43 public SearchMenuItemController(OnQueryTextListener queryListener, Bundle savedState) { 44 mSearchModeChangeListener = new SearchModeChangeListener(); 45 mQueryListener = queryListener; 46 if (savedState != null) { 47 mSearchMode = savedState.getBoolean(KEY_SEARCH_MODE, false); 48 mQuery = savedState.getString(KEY_SEARCH_QUERY, ""); 49 } 50 } 51 saveInstance(Bundle outState)52 public void saveInstance(Bundle outState) { 53 outState.putString(KEY_SEARCH_QUERY, mQuery); 54 outState.putBoolean(KEY_SEARCH_MODE, mSearchMode); 55 } 56 57 @Override getId()58 public int getId() { 59 return SEARCH_MENU_RES_ID; 60 } 61 62 @Override setInitialState(Menu menu)63 public void setInitialState(Menu menu) { 64 super.setInitialState(menu); 65 final MenuItem search = menu.findItem(SEARCH_MENU_RES_ID); 66 final SearchView searchView = (SearchView) MenuItemCompat.getActionView(search); 67 searchView.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); 68 searchView.setQuery(mQuery, false); 69 searchView.setOnCloseListener(mSearchModeChangeListener); 70 searchView.setOnSearchClickListener(mSearchModeChangeListener); 71 searchView.setOnQueryTextListener(mQueryListener); 72 if (mSearchMode) { 73 searchView.requestFocus(); 74 searchView.setIconified(false); 75 } 76 } 77 78 @Override showMenuItem(Menu menu)79 public void showMenuItem(Menu menu) { 80 menu.findItem(SEARCH_MENU_RES_ID).setVisible(true); 81 } 82 83 @Override handleMenuItemClick(MenuItem item)84 public boolean handleMenuItemClick(MenuItem item) { 85 // The search view is handled by {@link #mSearchListener}. Skip handling here. 86 return false; 87 } 88 getQueryText()89 public String getQueryText() { 90 return mQuery; 91 } 92 setQueryText(String query)93 public void setQueryText(String query) { 94 mQuery = query; 95 } 96 97 /** 98 * Listener for user actions on search view. 99 */ 100 private final class SearchModeChangeListener implements View.OnClickListener, 101 SearchView.OnCloseListener { 102 @Override onClick(View v)103 public void onClick(View v) { 104 mSearchMode = true; 105 } 106 107 @Override onClose()108 public boolean onClose() { 109 mSearchMode = false; 110 return false; 111 } 112 } 113 } 114