blob: 0abea1254aeaeea450f3657fceca31b72b174924 [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
33/**
34 * url/search input view
35 * handling suggestions
36 */
Michael Kolba2b2ba82010-08-04 17:54:03 -070037public class UrlInputView extends AutoCompleteTextView
Michael Kolbba99c5d2010-11-29 14:57:41 -080038 implements OnFocusChangeListener, OnEditorActionListener,
39 CompletionListener {
Michael Kolbfe251992010-07-08 15:41:55 -070040
Michael Kolb257cc2c2010-12-09 09:45:52 -080041
42 static final String TYPED = "browser-type";
43 static final String SUGGESTED = "browser-suggest";
44
Michael Kolbfe251992010-07-08 15:41:55 -070045 private UrlInputListener mListener;
46 private InputMethodManager mInputManager;
47 private SuggestionsAdapter mAdapter;
Michael Kolbc7485ae2010-09-03 10:10:58 -070048 private OnFocusChangeListener mWrappedFocusListener;
Michael Kolb21ce4d22010-09-15 14:55:05 -070049 private View mContainer;
50 private boolean mLandscape;
Michael Kolbfe251992010-07-08 15:41:55 -070051
52 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
53 super(context, attrs, defStyle);
54 init(context);
55 }
56
57 public UrlInputView(Context context, AttributeSet attrs) {
58 super(context, attrs);
59 init(context);
60 }
61
62 public UrlInputView(Context context) {
63 super(context);
64 init(context);
65 }
66
67 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -070068 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -070069 setOnEditorActionListener(this);
Michael Kolbc7485ae2010-09-03 10:10:58 -070070 super.setOnFocusChangeListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -070071 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -070072 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -080073 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -070074 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -080075 setThreshold(1);
Michael Kolb21ce4d22010-09-15 14:55:05 -070076 }
77
Michael Kolbba99c5d2010-11-29 14:57:41 -080078 void setController(UiController controller) {
79 UrlSelectionActionMode urlSelectionMode
80 = new UrlSelectionActionMode(controller);
81 setCustomSelectionActionModeCallback(urlSelectionMode);
82 }
83
Michael Kolb21ce4d22010-09-15 14:55:05 -070084 void setContainer(View container) {
85 mContainer = container;
86 }
87
88 @Override
89 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -080090 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -070091 mLandscape = (config.orientation &
92 Configuration.ORIENTATION_LANDSCAPE) > 0;
John Reck35defff2010-11-11 14:06:45 -080093 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -070094 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -080095 setupDropDown();
Michael Kolb21ce4d22010-09-15 14:55:05 -070096 }
97 }
98
99 @Override
100 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800101 setupDropDown();
102 super.showDropDown();
103 }
104
105 @Override
106 public void dismissDropDown() {
107 super.dismissDropDown();
108 mAdapter.clearCache();
109 }
110
111 private void setupDropDown() {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700112 int width = mContainer.getWidth();
Michael Kolbc5998c22010-10-10 16:04:35 -0700113 if (width != getDropDownWidth()) {
114 setDropDownWidth(width);
115 }
116 if (getLeft() != -getDropDownHorizontalOffset()) {
117 setDropDownHorizontalOffset(-getLeft());
118 }
Michael Kolb513286f2010-09-09 12:55:12 -0700119 }
120
121 @Override
Michael Kolbc7485ae2010-09-03 10:10:58 -0700122 public void setOnFocusChangeListener(OnFocusChangeListener focusListener) {
123 mWrappedFocusListener = focusListener;
Michael Kolbfe251992010-07-08 15:41:55 -0700124 }
125
Michael Kolba2b2ba82010-08-04 17:54:03 -0700126 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700127 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800128 finishInput(getText().toString(), null, TYPED);
Michael Kolbed217742010-08-10 17:52:34 -0700129 return true;
130 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700131
Michael Kolbc7485ae2010-09-03 10:10:58 -0700132 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700133 public void onFocusChange(View v, boolean hasFocus) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700134 if (hasFocus) {
135 forceIme();
136 } else {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800137 finishInput(null, null, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700138 }
Michael Kolbc7485ae2010-09-03 10:10:58 -0700139 if (mWrappedFocusListener != null) {
140 mWrappedFocusListener.onFocusChange(v, hasFocus);
141 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700142 }
143
Michael Kolbfe251992010-07-08 15:41:55 -0700144 public void setUrlInputListener(UrlInputListener listener) {
145 mListener = listener;
146 }
147
148 public void forceIme() {
149 mInputManager.showSoftInput(this, 0);
150 }
151
Michael Kolb257cc2c2010-12-09 09:45:52 -0800152 private void finishInput(String url, String extra, String source) {
Michael Kolbfe251992010-07-08 15:41:55 -0700153 this.dismissDropDown();
Michael Kolbc7485ae2010-09-03 10:10:58 -0700154 this.setSelection(0,0);
Michael Kolbfe251992010-07-08 15:41:55 -0700155 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800156 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700157 mListener.onDismiss();
158 } else {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800159 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700160 }
Michael Kolbfe251992010-07-08 15:41:55 -0700161 }
162
Michael Kolb21ce4d22010-09-15 14:55:05 -0700163 // Completion Listener
164
165 @Override
166 public void onSearch(String search) {
167 mListener.onEdit(search);
168 }
169
170 @Override
John Reck40f720e2010-11-10 11:57:04 -0800171 public void onSelect(String url, String extra) {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800172 finishInput(url, extra, SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700173 }
174
Michael Kolbfe251992010-07-08 15:41:55 -0700175 @Override
176 public boolean onKeyPreIme(int keyCode, KeyEvent evt) {
177 if (keyCode == KeyEvent.KEYCODE_BACK) {
178 // catch back key in order to do slightly more cleanup than usual
Michael Kolb257cc2c2010-12-09 09:45:52 -0800179 finishInput(null, null, null);
Michael Kolbfe251992010-07-08 15:41:55 -0700180 return true;
181 }
182 return super.onKeyPreIme(keyCode, evt);
183 }
184
185 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700186
Michael Kolbfe251992010-07-08 15:41:55 -0700187 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700188
Michael Kolb257cc2c2010-12-09 09:45:52 -0800189 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700190
Michael Kolb513286f2010-09-09 12:55:12 -0700191 public void onEdit(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700192
193 }
194
195}