• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 androidx.appcompat.widget.SearchView;
22 import androidx.appcompat.widget.SearchView.OnQueryTextListener;
23 import android.text.InputType;
24 import android.view.Menu;
25 import android.view.MenuItem;
26 import android.view.View;
27 import android.view.inputmethod.EditorInfo;
28 
29 import com.android.deskclock.R;
30 
31 import static android.view.Menu.FIRST;
32 import static android.view.Menu.NONE;
33 
34 /**
35  * {@link MenuItemController} for search menu.
36  */
37 public final class SearchMenuItemController implements MenuItemController {
38 
39     private static final String KEY_SEARCH_QUERY = "search_query";
40     private static final String KEY_SEARCH_MODE = "search_mode";
41 
42     private static final int SEARCH_MENU_RES_ID = R.id.menu_item_search;
43 
44     private final Context mContext;
45     private final SearchView.OnQueryTextListener mQueryListener;
46     private final SearchModeChangeListener mSearchModeChangeListener;
47 
48     private String mQuery = "";
49     private boolean mSearchMode;
50 
SearchMenuItemController(Context context, OnQueryTextListener queryListener, Bundle savedState)51     public SearchMenuItemController(Context context, OnQueryTextListener queryListener,
52             Bundle savedState) {
53         mContext = context;
54         mSearchModeChangeListener = new SearchModeChangeListener();
55         mQueryListener = queryListener;
56 
57         if (savedState != null) {
58             mSearchMode = savedState.getBoolean(KEY_SEARCH_MODE, false);
59             mQuery = savedState.getString(KEY_SEARCH_QUERY, "");
60         }
61     }
62 
saveInstance(Bundle outState)63     public void saveInstance(Bundle outState) {
64         outState.putString(KEY_SEARCH_QUERY, mQuery);
65         outState.putBoolean(KEY_SEARCH_MODE, mSearchMode);
66     }
67 
68     @Override
getId()69     public int getId() {
70         return SEARCH_MENU_RES_ID;
71     }
72 
73     @Override
onCreateOptionsItem(Menu menu)74     public void onCreateOptionsItem(Menu menu) {
75         final SearchView searchView = new SearchView(mContext);
76         searchView.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
77         searchView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
78         searchView.setQuery(mQuery, false);
79         searchView.setOnCloseListener(mSearchModeChangeListener);
80         searchView.setOnSearchClickListener(mSearchModeChangeListener);
81         searchView.setOnQueryTextListener(mQueryListener);
82 
83         menu.add(NONE, SEARCH_MENU_RES_ID, FIRST, android.R.string.search_go)
84                 .setActionView(searchView)
85                 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
86 
87         if (mSearchMode) {
88             searchView.requestFocus();
89             searchView.setIconified(false);
90         }
91     }
92 
93     @Override
onPrepareOptionsItem(MenuItem item)94     public void onPrepareOptionsItem(MenuItem item) {
95     }
96 
97     @Override
onOptionsItemSelected(MenuItem item)98     public boolean onOptionsItemSelected(MenuItem item) {
99         // The search view is handled by {@link #mSearchListener}. Skip handling here.
100         return false;
101     }
102 
getQueryText()103     public String getQueryText() {
104         return mQuery;
105     }
106 
setQueryText(String query)107     public void setQueryText(String query) {
108         mQuery = query;
109     }
110 
111     /**
112      * Listener for user actions on search view.
113      */
114     private final class SearchModeChangeListener implements View.OnClickListener,
115             SearchView.OnCloseListener {
116         @Override
onClick(View v)117         public void onClick(View v) {
118             mSearchMode = true;
119         }
120 
121         @Override
onClose()122         public boolean onClose() {
123             mSearchMode = false;
124             return false;
125         }
126     }
127 }
128