blob: e62e4b7aead211077666cdbe9d7246b5a5b747b1 (
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
|
package com.mapswithme.maps.widget;
import android.app.Activity;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import com.mapswithme.maps.R;
import com.mapswithme.util.InputUtils;
import com.mapswithme.util.StringUtils;
import com.mapswithme.util.UiUtils;
public class SearchToolbarController extends ToolbarController
implements View.OnClickListener
{
public interface Container
{
SearchToolbarController getController();
}
private final EditText mQuery;
private final View mProgress;
private final View mClear;
private final View mVoiceInput;
private final boolean mVoiceInputSupported = InputUtils.isVoiceInputSupported(mActivity);
private final TextWatcher mTextWatcher = new StringUtils.SimpleTextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
updateButtons(TextUtils.isEmpty(s));
SearchToolbarController.this.onTextChanged(s.toString());
}
};
public SearchToolbarController(View root, Activity activity)
{
super(root, activity);
mQuery = (EditText) mToolbar.findViewById(R.id.query);
mQuery.setOnClickListener(this);
mQuery.addTextChangedListener(mTextWatcher);
mQuery.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
boolean isSearchDown = (event != null &&
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_SEARCH);
boolean isSearchAction = (actionId == EditorInfo.IME_ACTION_SEARCH);
return (isSearchDown || isSearchAction) && onStartSearchClick();
}
});
mProgress = mToolbar.findViewById(R.id.progress);
mVoiceInput = mToolbar.findViewById(R.id.voice_input);
mVoiceInput.setOnClickListener(this);
mClear = mToolbar.findViewById(R.id.clear);
mClear.setOnClickListener(this);
showProgress(false);
updateButtons(true);
}
private void updateButtons(boolean queryEmpty)
{
UiUtils.showIf(queryEmpty && mVoiceInputSupported, mVoiceInput);
UiUtils.showIf(!queryEmpty, mClear);
}
protected void onQueryClick(String query) {}
protected void onTextChanged(String query) {}
protected boolean onStartSearchClick()
{
return true;
}
protected void onClearClick()
{
clear();
}
protected void onVoiceInputClick() {}
public String getQuery()
{
return mQuery.getText().toString().trim();
}
public void setQuery(CharSequence query)
{
final String text = query.toString().trim();
mQuery.setText(text);
if (!TextUtils.isEmpty(text))
mQuery.selectAll();
}
public void clear()
{
setQuery("");
}
public void activate()
{
mQuery.requestFocus();
InputUtils.showKeyboard(mQuery);
}
public void deactivate()
{
InputUtils.hideKeyboard(mQuery);
InputUtils.removeFocusEditTextHack(mQuery);
}
public void showProgress(boolean show)
{
UiUtils.showIf(show, mProgress);
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.query:
onQueryClick(getQuery());
break;
case R.id.clear:
onClearClick();
break;
case R.id.voice_input:
onVoiceInputClick();
break;
}
}
}
|