blob: 7dc2ed4c71b2eba0939ca6e928fb36c037c83361 [file] [log] [blame]
Michael Kolbfe251992010-07-08 15:41:55 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Michael Kolb21ce4d22010-09-15 14:55:05 -070019import com.android.browser.SuggestionsAdapter.CompletionListener;
20
Michael Kolbfe251992010-07-08 15:41:55 -070021import android.content.Context;
Michael Kolb21ce4d22010-09-15 14:55:05 -070022import android.content.res.Configuration;
John Reckdcda1d52010-11-29 10:05:54 -080023import android.text.TextUtils;
Michael Kolbfe251992010-07-08 15:41:55 -070024import android.util.AttributeSet;
25import android.view.KeyEvent;
Michael Kolbfe251992010-07-08 15:41:55 -070026import android.view.View;
Michael Kolba2b2ba82010-08-04 17:54:03 -070027import android.view.View.OnFocusChangeListener;
Michael Kolbfe251992010-07-08 15:41:55 -070028import android.view.inputmethod.InputMethodManager;
Michael Kolbfe251992010-07-08 15:41:55 -070029import android.widget.AutoCompleteTextView;
Michael Kolbfe251992010-07-08 15:41:55 -070030import android.widget.TextView;
Michael Kolbed217742010-08-10 17:52:34 -070031import android.widget.TextView.OnEditorActionListener;
Michael Kolbfe251992010-07-08 15:41:55 -070032
Michael Kolbcfa3af52010-12-14 10:36:11 -080033import java.util.List;
34
Michael Kolbfe251992010-07-08 15:41:55 -070035/**
36 * url/search input view
37 * handling suggestions
38 */
Michael Kolba2b2ba82010-08-04 17:54:03 -070039public class UrlInputView extends AutoCompleteTextView
Michael Kolbba99c5d2010-11-29 14:57:41 -080040 implements OnFocusChangeListener, OnEditorActionListener,
Michael Kolbbd2dd642011-01-13 13:01:30 -080041 CompletionListener {
Michael Kolbfe251992010-07-08 15:41:55 -070042
Michael Kolb257cc2c2010-12-09 09:45:52 -080043
44 static final String TYPED = "browser-type";
45 static final String SUGGESTED = "browser-suggest";
Michael Kolbbd2dd642011-01-13 13:01:30 -080046 static final String VOICE = "voice-search";
Michael Kolb257cc2c2010-12-09 09:45:52 -080047
Michael Kolbfe251992010-07-08 15:41:55 -070048 private UrlInputListener mListener;
49 private InputMethodManager mInputManager;
50 private SuggestionsAdapter mAdapter;
Michael Kolbc7485ae2010-09-03 10:10:58 -070051 private OnFocusChangeListener mWrappedFocusListener;
Michael Kolb21ce4d22010-09-15 14:55:05 -070052 private View mContainer;
53 private boolean mLandscape;
Michael Kolbcfa3af52010-12-14 10:36:11 -080054 private boolean mInVoiceMode;
Michael Kolbfe251992010-07-08 15:41:55 -070055
56 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
57 super(context, attrs, defStyle);
58 init(context);
59 }
60
61 public UrlInputView(Context context, AttributeSet attrs) {
62 super(context, attrs);
63 init(context);
64 }
65
66 public UrlInputView(Context context) {
67 super(context);
68 init(context);
69 }
70
71 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -070072 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -070073 setOnEditorActionListener(this);
Michael Kolbc7485ae2010-09-03 10:10:58 -070074 super.setOnFocusChangeListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -070075 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -070076 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -080077 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -070078 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -080079 setThreshold(1);
Michael Kolb21ce4d22010-09-15 14:55:05 -070080 }
81
Michael Kolbba99c5d2010-11-29 14:57:41 -080082 void setController(UiController controller) {
83 UrlSelectionActionMode urlSelectionMode
84 = new UrlSelectionActionMode(controller);
85 setCustomSelectionActionModeCallback(urlSelectionMode);
86 }
87
Michael Kolb21ce4d22010-09-15 14:55:05 -070088 void setContainer(View container) {
89 mContainer = container;
90 }
91
Michael Kolbcfa3af52010-12-14 10:36:11 -080092 void setVoiceResults(List<String> voiceResults) {
93 mAdapter.setVoiceResults(voiceResults);
94 mInVoiceMode = (voiceResults != null);
95 }
96
Michael Kolb21ce4d22010-09-15 14:55:05 -070097 @Override
98 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -080099 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700100 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800101 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800102 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700103 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800104 setupDropDown();
John Reckad373302010-12-17 15:28:13 -0800105 performFiltering(getText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700106 }
107 }
108
109 @Override
110 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800111 setupDropDown();
112 super.showDropDown();
113 }
114
115 @Override
116 public void dismissDropDown() {
117 super.dismissDropDown();
118 mAdapter.clearCache();
119 }
120
121 private void setupDropDown() {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700122 int width = mContainer.getWidth();
Michael Kolbc5998c22010-10-10 16:04:35 -0700123 if (width != getDropDownWidth()) {
124 setDropDownWidth(width);
125 }
126 if (getLeft() != -getDropDownHorizontalOffset()) {
127 setDropDownHorizontalOffset(-getLeft());
128 }
Michael Kolb31d469b2010-12-09 20:49:54 -0800129 setDropDownVerticalOffset(8);
Michael Kolb513286f2010-09-09 12:55:12 -0700130 }
131
132 @Override
Michael Kolbc7485ae2010-09-03 10:10:58 -0700133 public void setOnFocusChangeListener(OnFocusChangeListener focusListener) {
134 mWrappedFocusListener = focusListener;
Michael Kolbfe251992010-07-08 15:41:55 -0700135 }
136
Michael Kolba2b2ba82010-08-04 17:54:03 -0700137 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700138 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800139 finishInput(getText().toString(), null, TYPED);
Michael Kolbed217742010-08-10 17:52:34 -0700140 return true;
141 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700142
Michael Kolbc7485ae2010-09-03 10:10:58 -0700143 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700144 public void onFocusChange(View v, boolean hasFocus) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700145 if (hasFocus) {
146 forceIme();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800147 if (mInVoiceMode) {
148 performFiltering(getText().toString(), 0);
149 showDropDown();
150 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700151 } else {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800152 finishInput(null, null, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700153 }
Michael Kolbc7485ae2010-09-03 10:10:58 -0700154 if (mWrappedFocusListener != null) {
155 mWrappedFocusListener.onFocusChange(v, hasFocus);
156 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700157 }
158
Michael Kolbfe251992010-07-08 15:41:55 -0700159 public void setUrlInputListener(UrlInputListener listener) {
160 mListener = listener;
161 }
162
163 public void forceIme() {
164 mInputManager.showSoftInput(this, 0);
165 }
166
Michael Kolb257cc2c2010-12-09 09:45:52 -0800167 private void finishInput(String url, String extra, String source) {
Michael Kolbfe251992010-07-08 15:41:55 -0700168 this.dismissDropDown();
169 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800170 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700171 mListener.onDismiss();
172 } else {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800173 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700174 }
Michael Kolbfe251992010-07-08 15:41:55 -0700175 }
176
Michael Kolb21ce4d22010-09-15 14:55:05 -0700177 // Completion Listener
178
179 @Override
180 public void onSearch(String search) {
181 mListener.onEdit(search);
182 }
183
184 @Override
Michael Kolbbd2dd642011-01-13 13:01:30 -0800185 public void onSelect(String url, int type, String extra) {
186 finishInput(url, extra, (type == SuggestionsAdapter.TYPE_VOICE_SEARCH)
187 ? VOICE : SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700188 }
189
Michael Kolbfe251992010-07-08 15:41:55 -0700190 @Override
191 public boolean onKeyPreIme(int keyCode, KeyEvent evt) {
192 if (keyCode == KeyEvent.KEYCODE_BACK) {
193 // catch back key in order to do slightly more cleanup than usual
Michael Kolb257cc2c2010-12-09 09:45:52 -0800194 finishInput(null, null, null);
Michael Kolbfe251992010-07-08 15:41:55 -0700195 return true;
196 }
197 return super.onKeyPreIme(keyCode, evt);
198 }
199
200 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700201
Michael Kolbfe251992010-07-08 15:41:55 -0700202 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700203
Michael Kolb257cc2c2010-12-09 09:45:52 -0800204 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700205
Michael Kolb513286f2010-09-09 12:55:12 -0700206 public void onEdit(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700207
208 }
209
John Reck1605bef2011-01-10 18:11:18 -0800210 public void setReverseResults(boolean reverse) {
211 mAdapter.setReverseResults(reverse);
212 }
213
Michael Kolbfe251992010-07-08 15:41:55 -0700214}