blob: 2e29f261cbbd6818eee1ff28e2ecffa888be0244 [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;
Michael Kolbfe251992010-07-08 15:41:55 -070023import android.util.AttributeSet;
Michael Kolb513286f2010-09-09 12:55:12 -070024import android.view.ActionMode;
Michael Kolbfe251992010-07-08 15:41:55 -070025import 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 Kolb21ce4d22010-09-15 14:55:05 -070038 implements OnFocusChangeListener, OnEditorActionListener, CompletionListener {
Michael Kolbfe251992010-07-08 15:41:55 -070039
40 private UrlInputListener mListener;
41 private InputMethodManager mInputManager;
42 private SuggestionsAdapter mAdapter;
Michael Kolbc7485ae2010-09-03 10:10:58 -070043 private OnFocusChangeListener mWrappedFocusListener;
Michael Kolb21ce4d22010-09-15 14:55:05 -070044 private View mContainer;
45 private boolean mLandscape;
Michael Kolbfe251992010-07-08 15:41:55 -070046
47 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
48 super(context, attrs, defStyle);
49 init(context);
50 }
51
52 public UrlInputView(Context context, AttributeSet attrs) {
53 super(context, attrs);
54 init(context);
55 }
56
57 public UrlInputView(Context context) {
58 super(context);
59 init(context);
60 }
61
62 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -070063 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -070064 setOnEditorActionListener(this);
Michael Kolbc7485ae2010-09-03 10:10:58 -070065 super.setOnFocusChangeListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -070066 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -070067 setAdapter(mAdapter);
Michael Kolbc7485ae2010-09-03 10:10:58 -070068 setSelectAllOnFocus(false);
Michael Kolb21ce4d22010-09-15 14:55:05 -070069 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -080070 setThreshold(1);
Michael Kolb21ce4d22010-09-15 14:55:05 -070071 }
72
73 void setContainer(View container) {
74 mContainer = container;
75 }
76
77 @Override
78 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -080079 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -070080 mLandscape = (config.orientation &
81 Configuration.ORIENTATION_LANDSCAPE) > 0;
John Reck35defff2010-11-11 14:06:45 -080082 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -070083 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -080084 setupDropDown();
Michael Kolb21ce4d22010-09-15 14:55:05 -070085 }
86 }
87
88 @Override
89 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -080090 setupDropDown();
91 super.showDropDown();
92 }
93
94 @Override
95 public void dismissDropDown() {
96 super.dismissDropDown();
97 mAdapter.clearCache();
98 }
99
100 private void setupDropDown() {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700101 int width = mContainer.getWidth();
Michael Kolbc5998c22010-10-10 16:04:35 -0700102 if (width != getDropDownWidth()) {
103 setDropDownWidth(width);
104 }
105 if (getLeft() != -getDropDownHorizontalOffset()) {
106 setDropDownHorizontalOffset(-getLeft());
107 }
Michael Kolb513286f2010-09-09 12:55:12 -0700108 }
109
110 @Override
111 public ActionMode startActionMode(ActionMode.Callback callback) {
112 // suppress selection action mode
113 return null;
Michael Kolbc7485ae2010-09-03 10:10:58 -0700114 }
115
116 @Override
117 public void setOnFocusChangeListener(OnFocusChangeListener focusListener) {
118 mWrappedFocusListener = focusListener;
Michael Kolbfe251992010-07-08 15:41:55 -0700119 }
120
Michael Kolba2b2ba82010-08-04 17:54:03 -0700121 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700122 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
John Reck40f720e2010-11-10 11:57:04 -0800123 finishInput(getText().toString(), null);
Michael Kolbed217742010-08-10 17:52:34 -0700124 return true;
125 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700126
Michael Kolbc7485ae2010-09-03 10:10:58 -0700127 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700128 public void onFocusChange(View v, boolean hasFocus) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700129 if (hasFocus) {
130 forceIme();
131 } else {
John Reck40f720e2010-11-10 11:57:04 -0800132 finishInput(null, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700133 }
Michael Kolbc7485ae2010-09-03 10:10:58 -0700134 if (mWrappedFocusListener != null) {
135 mWrappedFocusListener.onFocusChange(v, hasFocus);
136 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700137 }
138
Michael Kolbfe251992010-07-08 15:41:55 -0700139 public void setUrlInputListener(UrlInputListener listener) {
140 mListener = listener;
141 }
142
143 public void forceIme() {
144 mInputManager.showSoftInput(this, 0);
145 }
146
John Reck40f720e2010-11-10 11:57:04 -0800147 private void finishInput(String url, String extra) {
Michael Kolbfe251992010-07-08 15:41:55 -0700148 this.dismissDropDown();
Michael Kolbc7485ae2010-09-03 10:10:58 -0700149 this.setSelection(0,0);
Michael Kolbfe251992010-07-08 15:41:55 -0700150 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
151 if (url == null) {
152 mListener.onDismiss();
153 } else {
John Reck40f720e2010-11-10 11:57:04 -0800154 mListener.onAction(url, extra);
Michael Kolbfe251992010-07-08 15:41:55 -0700155 }
Michael Kolbfe251992010-07-08 15:41:55 -0700156 }
157
Michael Kolb21ce4d22010-09-15 14:55:05 -0700158 // Completion Listener
159
160 @Override
161 public void onSearch(String search) {
162 mListener.onEdit(search);
163 }
164
165 @Override
John Reck40f720e2010-11-10 11:57:04 -0800166 public void onSelect(String url, String extra) {
167 finishInput(url, extra);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700168 }
169
Michael Kolbfe251992010-07-08 15:41:55 -0700170 @Override
171 public boolean onKeyPreIme(int keyCode, KeyEvent evt) {
172 if (keyCode == KeyEvent.KEYCODE_BACK) {
173 // catch back key in order to do slightly more cleanup than usual
John Reck40f720e2010-11-10 11:57:04 -0800174 finishInput(null, null);
Michael Kolbfe251992010-07-08 15:41:55 -0700175 return true;
176 }
177 return super.onKeyPreIme(keyCode, evt);
178 }
179
180 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700181
Michael Kolbfe251992010-07-08 15:41:55 -0700182 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700183
John Reck40f720e2010-11-10 11:57:04 -0800184 public void onAction(String text, String extra);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700185
Michael Kolb513286f2010-09-09 12:55:12 -0700186 public void onEdit(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700187
188 }
189
190}