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