blob: 77d7dafddf22808fe4a1c2382151973e658f7096 [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;
25import android.view.KeyEvent;
26import android.view.LayoutInflater;
27import 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.ViewGroup;
30import android.view.inputmethod.InputMethodManager;
31import android.widget.AdapterView;
Michael Kolba2b2ba82010-08-04 17:54:03 -070032import android.widget.AdapterView.OnItemClickListener;
Michael Kolbfe251992010-07-08 15:41:55 -070033import android.widget.AutoCompleteTextView;
34import android.widget.CursorAdapter;
35import android.widget.Filterable;
36import android.widget.ImageView;
37import android.widget.TextView;
Michael Kolbed217742010-08-10 17:52:34 -070038import android.widget.TextView.OnEditorActionListener;
Michael Kolbfe251992010-07-08 15:41:55 -070039
40/**
41 * url/search input view
42 * handling suggestions
43 */
Michael Kolba2b2ba82010-08-04 17:54:03 -070044public class UrlInputView extends AutoCompleteTextView
Michael Kolbed217742010-08-10 17:52:34 -070045 implements OnFocusChangeListener, OnItemClickListener, OnEditorActionListener {
Michael Kolbfe251992010-07-08 15:41:55 -070046
47 private UrlInputListener mListener;
48 private InputMethodManager mInputManager;
49 private SuggestionsAdapter mAdapter;
50 private Drawable mFocusDrawable;
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) {
68 mFocusDrawable = ctx.getResources().getDrawable(R.drawable.textfield_stroke);
Michael Kolbfe251992010-07-08 15:41:55 -070069 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -070070 setOnEditorActionListener(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -070071 setOnFocusChangeListener(this);
Michael Kolbfe251992010-07-08 15:41:55 -070072 final ContentResolver cr = mContext.getContentResolver();
73 mAdapter = new SuggestionsAdapter(mContext,
74 BrowserProvider.getBookmarksSuggestions(cr, null));
75 setAdapter(mAdapter);
Michael Kolba2b2ba82010-08-04 17:54:03 -070076 setOnItemClickListener(this);
Michael Kolbfe251992010-07-08 15:41:55 -070077 setSelectAllOnFocus(true);
78 }
79
Michael Kolba2b2ba82010-08-04 17:54:03 -070080 @Override
Michael Kolbed217742010-08-10 17:52:34 -070081 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
82 finishInput(getText().toString());
83 return true;
84 }
85
86 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -070087 public void onFocusChange(View v, boolean hasFocus) {
88 setBackgroundDrawable(hasFocus ? mFocusDrawable : null);
89 if (hasFocus) {
90 forceIme();
91 } else {
92 finishInput(null);
93 }
94 }
95
96 @Override
97 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
98 String url = mAdapter.getViewString(view);
99 finishInput(url);
100 }
101
102
Michael Kolbfe251992010-07-08 15:41:55 -0700103 public void setUrlInputListener(UrlInputListener listener) {
104 mListener = listener;
105 }
106
107 public void forceIme() {
108 mInputManager.showSoftInput(this, 0);
109 }
110
111 private void finishInput(String url) {
112 this.dismissDropDown();
113 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
114 if (url == null) {
115 mListener.onDismiss();
116 } else {
117 mListener.onAction(url);
118 }
Michael Kolbfe251992010-07-08 15:41:55 -0700119 }
120
121 @Override
122 public boolean onKeyPreIme(int keyCode, KeyEvent evt) {
123 if (keyCode == KeyEvent.KEYCODE_BACK) {
124 // catch back key in order to do slightly more cleanup than usual
125 finishInput(null);
126 return true;
127 }
128 return super.onKeyPreIme(keyCode, evt);
129 }
130
131 interface UrlInputListener {
Michael Kolbfe251992010-07-08 15:41:55 -0700132 public void onDismiss();
Michael Kolbfe251992010-07-08 15:41:55 -0700133 public void onAction(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700134 }
135
136 /**
137 * adapter used by suggestion dropdown
138 */
139 class SuggestionsAdapter extends CursorAdapter implements Filterable {
140
141 private Cursor mLastCursor;
142 private ContentResolver mContent;
143 private int mIndexText1;
144 private int mIndexText2;
145 private int mIndexIcon;
146
147 public SuggestionsAdapter(Context context, Cursor c) {
148 super(context, c);
149 mContent = context.getContentResolver();
150 mIndexText1 = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1);
151 mIndexText2 = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL);
152 mIndexIcon = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_ICON_1);
153 }
154
155 public String getViewString(View view) {
156 TextView tv2 = (TextView) view.findViewById(android.R.id.text2);
157 if (tv2.getText().length() > 0) {
158 return tv2.getText().toString();
159 } else {
160 TextView tv1 = (TextView) view.findViewById(android.R.id.text1);
161 return tv1.getText().toString();
162 }
163 }
164
165 @Override
166 public View newView(Context context, Cursor cursor, ViewGroup parent) {
167 final LayoutInflater inflater = LayoutInflater.from(context);
168 final View view = inflater.inflate(
169 R.layout.simple_dropdown_item_2line, parent, false);
170 bindView(view, context, cursor);
171 return view;
172 }
173
174 @Override
175 public void bindView(View view, Context context, Cursor cursor) {
176 TextView tv1 = (TextView) view.findViewById(android.R.id.text1);
177 TextView tv2 = (TextView) view.findViewById(android.R.id.text2);
178 ImageView ic1 = (ImageView) view.findViewById(R.id.icon1);
179 tv1.setText(cursor.getString(mIndexText1));
180 String url = cursor.getString(mIndexText2);
181 tv2.setText((url != null) ? url : "");
182 // assume an id
183 try {
184 int id = Integer.parseInt(cursor.getString(mIndexIcon));
185 Drawable d = context.getResources().getDrawable(id);
186 ic1.setImageDrawable(d);
187 } catch (NumberFormatException nfx) {
188 }
189 }
190
191 @Override
192 public String convertToString(Cursor cursor) {
193 return cursor.getString(mIndexText1);
194 }
195
196 @Override
197 public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
198 if (getFilterQueryProvider() != null) {
199 return getFilterQueryProvider().runQuery(constraint);
200 }
201 mLastCursor = BrowserProvider.getBookmarksSuggestions(mContent,
202 (constraint != null) ? constraint.toString() : null);
203 return mLastCursor;
204 }
205
206 }
207
208}