1 /* 2 * Copyright (C) 2020 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.content.Context 20 import android.os.Bundle 21 import android.text.InputType 22 import android.view.Menu 23 import android.view.Menu.FIRST 24 import android.view.Menu.NONE 25 import android.view.MenuItem 26 import android.view.View 27 import android.view.inputmethod.EditorInfo 28 import androidx.appcompat.widget.SearchView 29 import androidx.appcompat.widget.SearchView.OnQueryTextListener 30 31 import com.android.deskclock.R 32 33 /** 34 * [MenuItemController] for search menu. 35 */ 36 class SearchMenuItemController( 37 private val context: Context, 38 private val queryListener: OnQueryTextListener, 39 savedState: Bundle? 40 ) : MenuItemController { 41 42 override val id: Int = R.id.menu_item_search 43 44 private val mSearchModeChangeListener: SearchModeChangeListener = SearchModeChangeListener() 45 46 var queryText = "" 47 private var mSearchMode = false 48 49 init { 50 if (savedState != null) { 51 mSearchMode = savedState.getBoolean(KEY_SEARCH_MODE, false) 52 queryText = savedState.getString(KEY_SEARCH_QUERY, "") 53 } 54 } 55 saveInstancenull56 fun saveInstance(outState: Bundle) { 57 outState.putString(KEY_SEARCH_QUERY, queryText) 58 outState.putBoolean(KEY_SEARCH_MODE, mSearchMode) 59 } 60 onCreateOptionsItemnull61 override fun onCreateOptionsItem(menu: Menu) { 62 val searchView = SearchView(context) 63 searchView.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI) 64 searchView.setInputType(InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_CAP_WORDS) 65 searchView.setQuery(queryText, false) 66 searchView.setOnCloseListener(mSearchModeChangeListener) 67 searchView.setOnSearchClickListener(mSearchModeChangeListener) 68 searchView.setOnQueryTextListener(queryListener) 69 70 menu.add(NONE, id, FIRST, android.R.string.search_go) 71 .setActionView(searchView) 72 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM) 73 74 if (mSearchMode) { 75 searchView.requestFocus() 76 searchView.setIconified(false) 77 } 78 } 79 onPrepareOptionsItemnull80 override fun onPrepareOptionsItem(item: MenuItem) { 81 } 82 onOptionsItemSelectednull83 override fun onOptionsItemSelected(item: MenuItem): Boolean { 84 // The search view is handled by {@link #mSearchListener}. Skip handling here. 85 return false 86 } 87 88 /** 89 * Listener for user actions on search view. 90 */ 91 private inner class SearchModeChangeListener 92 : View.OnClickListener, SearchView.OnCloseListener { 93 onClicknull94 override fun onClick(v: View?) { 95 mSearchMode = true 96 } 97 onClosenull98 override fun onClose(): Boolean { 99 mSearchMode = false 100 return false 101 } 102 } 103 104 companion object { 105 private const val KEY_SEARCH_QUERY = "search_query" 106 private const val KEY_SEARCH_MODE = "search_mode" 107 } 108 }