blob: 8662f55ae57c0589105ab2be74d8455de2b0c955 [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.ContentResolver;
22import android.content.Context;
Michael Kolb21ce4d22010-09-15 14:55:05 -070023import android.content.res.Configuration;
Michael Kolbfe251992010-07-08 15:41:55 -070024import android.util.AttributeSet;
Michael Kolb513286f2010-09-09 12:55:12 -070025import android.view.ActionMode;
Michael Kolbfe251992010-07-08 15:41:55 -070026import 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;
Michael Kolbfe251992010-07-08 15:41:55 -070030import android.widget.AutoCompleteTextView;
Michael Kolbfe251992010-07-08 15:41:55 -070031import android.widget.TextView;
Michael Kolbed217742010-08-10 17:52:34 -070032import android.widget.TextView.OnEditorActionListener;
Michael Kolbfe251992010-07-08 15:41:55 -070033
34/**
35 * url/search input view
36 * handling suggestions
37 */
Michael Kolba2b2ba82010-08-04 17:54:03 -070038public class UrlInputView extends AutoCompleteTextView
Michael Kolb21ce4d22010-09-15 14:55:05 -070039 implements OnFocusChangeListener, OnEditorActionListener, CompletionListener {
Michael Kolbfe251992010-07-08 15:41:55 -070040
41 private UrlInputListener mListener;
42 private InputMethodManager mInputManager;
43 private SuggestionsAdapter mAdapter;
Michael Kolbc7485ae2010-09-03 10:10:58 -070044 private OnFocusChangeListener mWrappedFocusListener;
Michael Kolb21ce4d22010-09-15 14:55:05 -070045 private View mContainer;
46 private boolean mLandscape;
Michael Kolbfe251992010-07-08 15:41:55 -070047
48 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
49 super(context, attrs, defStyle);
50 init(context);
51 }
52
53 public UrlInputView(Context context, AttributeSet attrs) {
54 super(context, attrs);
55 init(context);
56 }
57
58 public UrlInputView(Context context) {
59 super(context);
60 init(context);
61 }
62
63 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -070064 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -070065 setOnEditorActionListener(this);
Michael Kolbc7485ae2010-09-03 10:10:58 -070066 super.setOnFocusChangeListener(this);
Michael Kolbfe251992010-07-08 15:41:55 -070067 final ContentResolver cr = mContext.getContentResolver();
Michael Kolb21ce4d22010-09-15 14:55:05 -070068 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -070069 setAdapter(mAdapter);
Michael Kolbc7485ae2010-09-03 10:10:58 -070070 setSelectAllOnFocus(false);
Michael Kolb21ce4d22010-09-15 14:55:05 -070071 onConfigurationChanged(ctx.getResources().getConfiguration());
72 }
73
74 void setContainer(View container) {
75 mContainer = container;
76 }
77
78 @Override
79 protected void onConfigurationChanged(Configuration config) {
80 mLandscape = (config.orientation &
81 Configuration.ORIENTATION_LANDSCAPE) > 0;
82 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
83 dismissDropDown();
84 getFilter().filter(getText());
85 }
86 }
87
88 @Override
89 public void showDropDown() {
90 int width = mContainer.getWidth();
91 if ((mAdapter.getLeftCount() == 0) || (mAdapter.getRightCount() == 0)) {
92 width = width / 2;
93 }
94 setDropDownWidth(width);
95 setDropDownHorizontalOffset(-getLeft());
96 mAdapter.setLandscapeMode(mLandscape);
97 super.showDropDown();
Michael Kolb513286f2010-09-09 12:55:12 -070098 }
99
100 @Override
101 public ActionMode startActionMode(ActionMode.Callback callback) {
102 // suppress selection action mode
103 return null;
Michael Kolbc7485ae2010-09-03 10:10:58 -0700104 }
105
106 @Override
107 public void setOnFocusChangeListener(OnFocusChangeListener focusListener) {
108 mWrappedFocusListener = focusListener;
Michael Kolbfe251992010-07-08 15:41:55 -0700109 }
110
Michael Kolba2b2ba82010-08-04 17:54:03 -0700111 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700112 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
113 finishInput(getText().toString());
114 return true;
115 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700116
Michael Kolbc7485ae2010-09-03 10:10:58 -0700117 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700118 public void onFocusChange(View v, boolean hasFocus) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700119 if (hasFocus) {
120 forceIme();
121 } else {
122 finishInput(null);
123 }
Michael Kolbc7485ae2010-09-03 10:10:58 -0700124 if (mWrappedFocusListener != null) {
125 mWrappedFocusListener.onFocusChange(v, hasFocus);
126 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700127 }
128
Michael Kolbfe251992010-07-08 15:41:55 -0700129 public void setUrlInputListener(UrlInputListener listener) {
130 mListener = listener;
131 }
132
133 public void forceIme() {
134 mInputManager.showSoftInput(this, 0);
135 }
136
137 private void finishInput(String url) {
138 this.dismissDropDown();
Michael Kolbc7485ae2010-09-03 10:10:58 -0700139 this.setSelection(0,0);
Michael Kolbfe251992010-07-08 15:41:55 -0700140 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
141 if (url == null) {
142 mListener.onDismiss();
143 } else {
144 mListener.onAction(url);
145 }
Michael Kolbfe251992010-07-08 15:41:55 -0700146 }
147
Michael Kolb21ce4d22010-09-15 14:55:05 -0700148 // Completion Listener
149
150 @Override
151 public void onSearch(String search) {
152 mListener.onEdit(search);
153 }
154
155 @Override
156 public void onSelect(String url) {
157 finishInput(url);
158 }
159
Michael Kolbfe251992010-07-08 15:41:55 -0700160 @Override
161 public boolean onKeyPreIme(int keyCode, KeyEvent evt) {
162 if (keyCode == KeyEvent.KEYCODE_BACK) {
163 // catch back key in order to do slightly more cleanup than usual
164 finishInput(null);
165 return true;
166 }
167 return super.onKeyPreIme(keyCode, evt);
168 }
169
170 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700171
Michael Kolbfe251992010-07-08 15:41:55 -0700172 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700173
Michael Kolbfe251992010-07-08 15:41:55 -0700174 public void onAction(String text);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700175
Michael Kolb513286f2010-09-09 12:55:12 -0700176 public void onEdit(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700177
178 }
179
180}