blob: 9ae464b0cc246b8752d4ac56413660ffefbc082e [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
19import android.app.SearchManager;
20import android.content.ContentResolver;
21import android.content.Context;
22import android.database.Cursor;
23import android.graphics.drawable.Drawable;
24import android.util.AttributeSet;
Michael Kolbc7485ae2010-09-03 10:10:58 -070025import android.util.Log;
Michael Kolbfe251992010-07-08 15:41:55 -070026import android.view.KeyEvent;
27import android.view.LayoutInflater;
Michael Kolbc7485ae2010-09-03 10:10:58 -070028import android.view.MotionEvent;
Michael Kolbfe251992010-07-08 15:41:55 -070029import android.view.View;
Michael Kolba2b2ba82010-08-04 17:54:03 -070030import android.view.View.OnFocusChangeListener;
Michael Kolbfe251992010-07-08 15:41:55 -070031import android.view.ViewGroup;
32import android.view.inputmethod.InputMethodManager;
33import android.widget.AdapterView;
Michael Kolba2b2ba82010-08-04 17:54:03 -070034import android.widget.AdapterView.OnItemClickListener;
Michael Kolbfe251992010-07-08 15:41:55 -070035import android.widget.AutoCompleteTextView;
36import android.widget.CursorAdapter;
37import android.widget.Filterable;
38import android.widget.ImageView;
39import android.widget.TextView;
Michael Kolbed217742010-08-10 17:52:34 -070040import android.widget.TextView.OnEditorActionListener;
Michael Kolbfe251992010-07-08 15:41:55 -070041
42/**
43 * url/search input view
44 * handling suggestions
45 */
Michael Kolba2b2ba82010-08-04 17:54:03 -070046public class UrlInputView extends AutoCompleteTextView
Michael Kolbed217742010-08-10 17:52:34 -070047 implements OnFocusChangeListener, OnItemClickListener, OnEditorActionListener {
Michael Kolbfe251992010-07-08 15:41:55 -070048
49 private UrlInputListener mListener;
50 private InputMethodManager mInputManager;
51 private SuggestionsAdapter mAdapter;
Michael Kolbc7485ae2010-09-03 10:10:58 -070052
53 private OnFocusChangeListener mWrappedFocusListener;
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 Kolbfe251992010-07-08 15:41:55 -070074 final ContentResolver cr = mContext.getContentResolver();
75 mAdapter = new SuggestionsAdapter(mContext,
76 BrowserProvider.getBookmarksSuggestions(cr, null));
77 setAdapter(mAdapter);
Michael Kolba2b2ba82010-08-04 17:54:03 -070078 setOnItemClickListener(this);
Michael Kolbc7485ae2010-09-03 10:10:58 -070079 setSelectAllOnFocus(false);
80
81 }
82
83 @Override
84 public void setOnFocusChangeListener(OnFocusChangeListener focusListener) {
85 mWrappedFocusListener = focusListener;
Michael Kolbfe251992010-07-08 15:41:55 -070086 }
87
Michael Kolba2b2ba82010-08-04 17:54:03 -070088 @Override
Michael Kolbed217742010-08-10 17:52:34 -070089 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
90 finishInput(getText().toString());
91 return true;
92 }
Michael Kolbc7485ae2010-09-03 10:10:58 -070093
94 @Override
95 public boolean onTouchEvent(MotionEvent evt) {
96
97 if ((evt.getAction() == MotionEvent.ACTION_DOWN) && !this.hasFocus()) {
98 Log.i("test","onTouch");
99 selectAll();
100 requestFocus();
101 return true;
102 } else {
103 return super.onTouchEvent(evt);
104 }
105 }
106
Michael Kolbed217742010-08-10 17:52:34 -0700107 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700108 public void onFocusChange(View v, boolean hasFocus) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700109 if (hasFocus) {
110 forceIme();
111 } else {
112 finishInput(null);
113 }
Michael Kolbc7485ae2010-09-03 10:10:58 -0700114 if (mWrappedFocusListener != null) {
115 mWrappedFocusListener.onFocusChange(v, hasFocus);
116 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700117 }
118
119 @Override
120 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
121 String url = mAdapter.getViewString(view);
122 finishInput(url);
123 }
124
125
Michael Kolbfe251992010-07-08 15:41:55 -0700126 public void setUrlInputListener(UrlInputListener listener) {
127 mListener = listener;
128 }
129
130 public void forceIme() {
131 mInputManager.showSoftInput(this, 0);
132 }
133
134 private void finishInput(String url) {
135 this.dismissDropDown();
Michael Kolbc7485ae2010-09-03 10:10:58 -0700136 this.setSelection(0,0);
Michael Kolbfe251992010-07-08 15:41:55 -0700137 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
138 if (url == null) {
139 mListener.onDismiss();
140 } else {
141 mListener.onAction(url);
142 }
Michael Kolbfe251992010-07-08 15:41:55 -0700143 }
144
145 @Override
146 public boolean onKeyPreIme(int keyCode, KeyEvent evt) {
147 if (keyCode == KeyEvent.KEYCODE_BACK) {
148 // catch back key in order to do slightly more cleanup than usual
149 finishInput(null);
150 return true;
151 }
152 return super.onKeyPreIme(keyCode, evt);
153 }
154
155 interface UrlInputListener {
Michael Kolbfe251992010-07-08 15:41:55 -0700156 public void onDismiss();
Michael Kolbfe251992010-07-08 15:41:55 -0700157 public void onAction(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700158 }
159
160 /**
161 * adapter used by suggestion dropdown
162 */
163 class SuggestionsAdapter extends CursorAdapter implements Filterable {
164
165 private Cursor mLastCursor;
166 private ContentResolver mContent;
167 private int mIndexText1;
168 private int mIndexText2;
169 private int mIndexIcon;
170
171 public SuggestionsAdapter(Context context, Cursor c) {
172 super(context, c);
173 mContent = context.getContentResolver();
174 mIndexText1 = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1);
175 mIndexText2 = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL);
176 mIndexIcon = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_ICON_1);
177 }
178
179 public String getViewString(View view) {
180 TextView tv2 = (TextView) view.findViewById(android.R.id.text2);
181 if (tv2.getText().length() > 0) {
182 return tv2.getText().toString();
183 } else {
184 TextView tv1 = (TextView) view.findViewById(android.R.id.text1);
185 return tv1.getText().toString();
186 }
187 }
188
189 @Override
190 public View newView(Context context, Cursor cursor, ViewGroup parent) {
191 final LayoutInflater inflater = LayoutInflater.from(context);
192 final View view = inflater.inflate(
193 R.layout.simple_dropdown_item_2line, parent, false);
194 bindView(view, context, cursor);
195 return view;
196 }
197
198 @Override
199 public void bindView(View view, Context context, Cursor cursor) {
200 TextView tv1 = (TextView) view.findViewById(android.R.id.text1);
201 TextView tv2 = (TextView) view.findViewById(android.R.id.text2);
202 ImageView ic1 = (ImageView) view.findViewById(R.id.icon1);
203 tv1.setText(cursor.getString(mIndexText1));
204 String url = cursor.getString(mIndexText2);
205 tv2.setText((url != null) ? url : "");
206 // assume an id
207 try {
208 int id = Integer.parseInt(cursor.getString(mIndexIcon));
209 Drawable d = context.getResources().getDrawable(id);
210 ic1.setImageDrawable(d);
211 } catch (NumberFormatException nfx) {
212 }
213 }
214
215 @Override
216 public String convertToString(Cursor cursor) {
217 return cursor.getString(mIndexText1);
218 }
219
220 @Override
221 public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
222 if (getFilterQueryProvider() != null) {
223 return getFilterQueryProvider().runQuery(constraint);
224 }
225 mLastCursor = BrowserProvider.getBookmarksSuggestions(mContent,
226 (constraint != null) ? constraint.toString() : null);
227 return mLastCursor;
228 }
229
230 }
231
232}