blob: 505662dcbacbe2f0b682b6356dc7d4d48c9bd332 [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,
41 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";
46
Michael Kolbfe251992010-07-08 15:41:55 -070047 private UrlInputListener mListener;
48 private InputMethodManager mInputManager;
49 private SuggestionsAdapter mAdapter;
Michael Kolbc7485ae2010-09-03 10:10:58 -070050 private OnFocusChangeListener mWrappedFocusListener;
Michael Kolb21ce4d22010-09-15 14:55:05 -070051 private View mContainer;
52 private boolean mLandscape;
Michael Kolbcfa3af52010-12-14 10:36:11 -080053 private boolean mInVoiceMode;
Michael Kolbfe251992010-07-08 15:41:55 -070054
55 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
56 super(context, attrs, defStyle);
57 init(context);
58 }
59
60 public UrlInputView(Context context, AttributeSet attrs) {
61 super(context, attrs);
62 init(context);
63 }
64
65 public UrlInputView(Context context) {
66 super(context);
67 init(context);
68 }
69
70 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -070071 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -070072 setOnEditorActionListener(this);
Michael Kolbc7485ae2010-09-03 10:10:58 -070073 super.setOnFocusChangeListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -070074 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -070075 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -080076 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -070077 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -080078 setThreshold(1);
Michael Kolb21ce4d22010-09-15 14:55:05 -070079 }
80
Michael Kolbba99c5d2010-11-29 14:57:41 -080081 void setController(UiController controller) {
82 UrlSelectionActionMode urlSelectionMode
83 = new UrlSelectionActionMode(controller);
84 setCustomSelectionActionModeCallback(urlSelectionMode);
85 }
86
Michael Kolb21ce4d22010-09-15 14:55:05 -070087 void setContainer(View container) {
88 mContainer = container;
89 }
90
Michael Kolbcfa3af52010-12-14 10:36:11 -080091 void setVoiceResults(List<String> voiceResults) {
92 mAdapter.setVoiceResults(voiceResults);
93 mInVoiceMode = (voiceResults != null);
94 }
95
Michael Kolb21ce4d22010-09-15 14:55:05 -070096 @Override
97 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -080098 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -070099 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800100 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800101 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700102 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800103 setupDropDown();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700104 }
105 }
106
107 @Override
108 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800109 setupDropDown();
110 super.showDropDown();
111 }
112
113 @Override
114 public void dismissDropDown() {
115 super.dismissDropDown();
116 mAdapter.clearCache();
117 }
118
119 private void setupDropDown() {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700120 int width = mContainer.getWidth();
Michael Kolbc5998c22010-10-10 16:04:35 -0700121 if (width != getDropDownWidth()) {
122 setDropDownWidth(width);
123 }
124 if (getLeft() != -getDropDownHorizontalOffset()) {
125 setDropDownHorizontalOffset(-getLeft());
126 }
Michael Kolb31d469b2010-12-09 20:49:54 -0800127 setDropDownVerticalOffset(8);
Michael Kolb513286f2010-09-09 12:55:12 -0700128 }
129
130 @Override
Michael Kolbc7485ae2010-09-03 10:10:58 -0700131 public void setOnFocusChangeListener(OnFocusChangeListener focusListener) {
132 mWrappedFocusListener = focusListener;
Michael Kolbfe251992010-07-08 15:41:55 -0700133 }
134
Michael Kolba2b2ba82010-08-04 17:54:03 -0700135 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700136 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800137 finishInput(getText().toString(), null, TYPED);
Michael Kolbed217742010-08-10 17:52:34 -0700138 return true;
139 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700140
Michael Kolbc7485ae2010-09-03 10:10:58 -0700141 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700142 public void onFocusChange(View v, boolean hasFocus) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700143 if (hasFocus) {
144 forceIme();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800145 if (mInVoiceMode) {
146 performFiltering(getText().toString(), 0);
147 showDropDown();
148 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700149 } else {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800150 finishInput(null, null, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700151 }
Michael Kolbc7485ae2010-09-03 10:10:58 -0700152 if (mWrappedFocusListener != null) {
153 mWrappedFocusListener.onFocusChange(v, hasFocus);
154 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700155 }
156
Michael Kolbfe251992010-07-08 15:41:55 -0700157 public void setUrlInputListener(UrlInputListener listener) {
158 mListener = listener;
159 }
160
161 public void forceIme() {
162 mInputManager.showSoftInput(this, 0);
163 }
164
Michael Kolb257cc2c2010-12-09 09:45:52 -0800165 private void finishInput(String url, String extra, String source) {
Michael Kolbfe251992010-07-08 15:41:55 -0700166 this.dismissDropDown();
167 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800168 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700169 mListener.onDismiss();
170 } else {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800171 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700172 }
Michael Kolbfe251992010-07-08 15:41:55 -0700173 }
174
Michael Kolb21ce4d22010-09-15 14:55:05 -0700175 // Completion Listener
176
177 @Override
178 public void onSearch(String search) {
179 mListener.onEdit(search);
180 }
181
182 @Override
John Reck40f720e2010-11-10 11:57:04 -0800183 public void onSelect(String url, String extra) {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800184 finishInput(url, extra, SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700185 }
186
Michael Kolbfe251992010-07-08 15:41:55 -0700187 @Override
188 public boolean onKeyPreIme(int keyCode, KeyEvent evt) {
189 if (keyCode == KeyEvent.KEYCODE_BACK) {
190 // catch back key in order to do slightly more cleanup than usual
Michael Kolb257cc2c2010-12-09 09:45:52 -0800191 finishInput(null, null, null);
Michael Kolbfe251992010-07-08 15:41:55 -0700192 return true;
193 }
194 return super.onKeyPreIme(keyCode, evt);
195 }
196
197 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700198
Michael Kolbfe251992010-07-08 15:41:55 -0700199 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700200
Michael Kolb257cc2c2010-12-09 09:45:52 -0800201 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700202
Michael Kolb513286f2010-09-09 12:55:12 -0700203 public void onEdit(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700204
205 }
206
207}