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