blob: 8aeef3101c61acb77098011921ad6798b1e2eb36 [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;
John Reckad373302010-12-17 15:28:13 -080020import com.android.browser.SuggestionsAdapter.SuggestItem;
Michael Kolb21ce4d22010-09-15 14:55:05 -070021
Michael Kolbfe251992010-07-08 15:41:55 -070022import android.content.Context;
Michael Kolb21ce4d22010-09-15 14:55:05 -070023import android.content.res.Configuration;
John Reckdcda1d52010-11-29 10:05:54 -080024import android.text.TextUtils;
Michael Kolbfe251992010-07-08 15:41:55 -070025import android.util.AttributeSet;
26import android.view.KeyEvent;
Michael Kolbfe251992010-07-08 15:41:55 -070027import android.view.View;
Michael Kolba2b2ba82010-08-04 17:54:03 -070028import android.view.View.OnFocusChangeListener;
Michael Kolbfe251992010-07-08 15:41:55 -070029import android.view.inputmethod.InputMethodManager;
John Reckad373302010-12-17 15:28:13 -080030import android.widget.AdapterView;
31import android.widget.AdapterView.OnItemClickListener;
32import android.widget.AdapterView.OnItemSelectedListener;
Michael Kolbfe251992010-07-08 15:41:55 -070033import android.widget.AutoCompleteTextView;
Michael Kolbfe251992010-07-08 15:41:55 -070034import android.widget.TextView;
Michael Kolbed217742010-08-10 17:52:34 -070035import android.widget.TextView.OnEditorActionListener;
Michael Kolbfe251992010-07-08 15:41:55 -070036
Michael Kolbcfa3af52010-12-14 10:36:11 -080037import java.util.List;
38
Michael Kolbfe251992010-07-08 15:41:55 -070039/**
40 * url/search input view
41 * handling suggestions
42 */
Michael Kolba2b2ba82010-08-04 17:54:03 -070043public class UrlInputView extends AutoCompleteTextView
Michael Kolbba99c5d2010-11-29 14:57:41 -080044 implements OnFocusChangeListener, OnEditorActionListener,
John Reckad373302010-12-17 15:28:13 -080045 CompletionListener, OnItemClickListener {
Michael Kolbfe251992010-07-08 15:41:55 -070046
Michael Kolb257cc2c2010-12-09 09:45:52 -080047
48 static final String TYPED = "browser-type";
49 static final String SUGGESTED = "browser-suggest";
50
Michael Kolbfe251992010-07-08 15:41:55 -070051 private UrlInputListener mListener;
52 private InputMethodManager mInputManager;
53 private SuggestionsAdapter mAdapter;
Michael Kolbc7485ae2010-09-03 10:10:58 -070054 private OnFocusChangeListener mWrappedFocusListener;
Michael Kolb21ce4d22010-09-15 14:55:05 -070055 private View mContainer;
56 private boolean mLandscape;
Michael Kolbcfa3af52010-12-14 10:36:11 -080057 private boolean mInVoiceMode;
Michael Kolbfe251992010-07-08 15:41:55 -070058
59 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
60 super(context, attrs, defStyle);
61 init(context);
62 }
63
64 public UrlInputView(Context context, AttributeSet attrs) {
65 super(context, attrs);
66 init(context);
67 }
68
69 public UrlInputView(Context context) {
70 super(context);
71 init(context);
72 }
73
74 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -070075 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -070076 setOnEditorActionListener(this);
Michael Kolbc7485ae2010-09-03 10:10:58 -070077 super.setOnFocusChangeListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -070078 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -070079 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -080080 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -070081 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -080082 setThreshold(1);
John Reckad373302010-12-17 15:28:13 -080083 setOnItemClickListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -070084 }
85
Michael Kolbba99c5d2010-11-29 14:57:41 -080086 void setController(UiController controller) {
87 UrlSelectionActionMode urlSelectionMode
88 = new UrlSelectionActionMode(controller);
89 setCustomSelectionActionModeCallback(urlSelectionMode);
90 }
91
Michael Kolb21ce4d22010-09-15 14:55:05 -070092 void setContainer(View container) {
93 mContainer = container;
94 }
95
Michael Kolbcfa3af52010-12-14 10:36:11 -080096 void setVoiceResults(List<String> voiceResults) {
97 mAdapter.setVoiceResults(voiceResults);
98 mInVoiceMode = (voiceResults != null);
99 }
100
Michael Kolb21ce4d22010-09-15 14:55:05 -0700101 @Override
102 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800103 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700104 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800105 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800106 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700107 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800108 setupDropDown();
John Reckad373302010-12-17 15:28:13 -0800109 performFiltering(getText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700110 }
111 }
112
113 @Override
114 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800115 setupDropDown();
116 super.showDropDown();
117 }
118
119 @Override
120 public void dismissDropDown() {
121 super.dismissDropDown();
122 mAdapter.clearCache();
123 }
124
125 private void setupDropDown() {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700126 int width = mContainer.getWidth();
Michael Kolbc5998c22010-10-10 16:04:35 -0700127 if (width != getDropDownWidth()) {
128 setDropDownWidth(width);
129 }
130 if (getLeft() != -getDropDownHorizontalOffset()) {
131 setDropDownHorizontalOffset(-getLeft());
132 }
Michael Kolb31d469b2010-12-09 20:49:54 -0800133 setDropDownVerticalOffset(8);
Michael Kolb513286f2010-09-09 12:55:12 -0700134 }
135
136 @Override
Michael Kolbc7485ae2010-09-03 10:10:58 -0700137 public void setOnFocusChangeListener(OnFocusChangeListener focusListener) {
138 mWrappedFocusListener = focusListener;
Michael Kolbfe251992010-07-08 15:41:55 -0700139 }
140
Michael Kolba2b2ba82010-08-04 17:54:03 -0700141 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700142 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800143 finishInput(getText().toString(), null, TYPED);
Michael Kolbed217742010-08-10 17:52:34 -0700144 return true;
145 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700146
Michael Kolbc7485ae2010-09-03 10:10:58 -0700147 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700148 public void onFocusChange(View v, boolean hasFocus) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700149 if (hasFocus) {
150 forceIme();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800151 if (mInVoiceMode) {
152 performFiltering(getText().toString(), 0);
153 showDropDown();
154 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700155 } else {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800156 finishInput(null, null, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700157 }
Michael Kolbc7485ae2010-09-03 10:10:58 -0700158 if (mWrappedFocusListener != null) {
159 mWrappedFocusListener.onFocusChange(v, hasFocus);
160 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700161 }
162
Michael Kolbfe251992010-07-08 15:41:55 -0700163 public void setUrlInputListener(UrlInputListener listener) {
164 mListener = listener;
165 }
166
167 public void forceIme() {
168 mInputManager.showSoftInput(this, 0);
169 }
170
Michael Kolb257cc2c2010-12-09 09:45:52 -0800171 private void finishInput(String url, String extra, String source) {
Michael Kolbfe251992010-07-08 15:41:55 -0700172 this.dismissDropDown();
173 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800174 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700175 mListener.onDismiss();
176 } else {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800177 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700178 }
Michael Kolbfe251992010-07-08 15:41:55 -0700179 }
180
Michael Kolb21ce4d22010-09-15 14:55:05 -0700181 // Completion Listener
182
183 @Override
184 public void onSearch(String search) {
185 mListener.onEdit(search);
186 }
187
188 @Override
John Reck40f720e2010-11-10 11:57:04 -0800189 public void onSelect(String url, String extra) {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800190 finishInput(url, extra, SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700191 }
192
Michael Kolbfe251992010-07-08 15:41:55 -0700193 @Override
194 public boolean onKeyPreIme(int keyCode, KeyEvent evt) {
195 if (keyCode == KeyEvent.KEYCODE_BACK) {
196 // catch back key in order to do slightly more cleanup than usual
Michael Kolb257cc2c2010-12-09 09:45:52 -0800197 finishInput(null, null, null);
Michael Kolbfe251992010-07-08 15:41:55 -0700198 return true;
199 }
200 return super.onKeyPreIme(keyCode, evt);
201 }
202
203 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700204
Michael Kolbfe251992010-07-08 15:41:55 -0700205 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700206
Michael Kolb257cc2c2010-12-09 09:45:52 -0800207 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700208
Michael Kolb513286f2010-09-09 12:55:12 -0700209 public void onEdit(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700210
211 }
212
John Reckad373302010-12-17 15:28:13 -0800213 @Override
214 public void onItemClick(
215 AdapterView<?> parent, View view, int position, long id) {
216 SuggestItem item = mAdapter.getItem(position);
217 onSelect((TextUtils.isEmpty(item.url) ? item.title : item.url),
218 item.extra);
219 }
220
Michael Kolbfe251992010-07-08 15:41:55 -0700221}