Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.app.SearchManager; |
| 20 | import android.content.ContentResolver; |
| 21 | import android.content.Context; |
| 22 | import android.database.Cursor; |
| 23 | import android.graphics.drawable.Drawable; |
| 24 | import android.util.AttributeSet; |
| 25 | import android.view.KeyEvent; |
| 26 | import android.view.LayoutInflater; |
| 27 | import android.view.View; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 28 | import android.view.View.OnFocusChangeListener; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 29 | import android.view.ViewGroup; |
| 30 | import android.view.inputmethod.InputMethodManager; |
| 31 | import android.widget.AdapterView; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 32 | import android.widget.AdapterView.OnItemClickListener; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 33 | import android.widget.AutoCompleteTextView; |
| 34 | import android.widget.CursorAdapter; |
| 35 | import android.widget.Filterable; |
| 36 | import android.widget.ImageView; |
| 37 | import android.widget.TextView; |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame^] | 38 | import android.widget.TextView.OnEditorActionListener; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * url/search input view |
| 42 | * handling suggestions |
| 43 | */ |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 44 | public class UrlInputView extends AutoCompleteTextView |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame^] | 45 | implements OnFocusChangeListener, OnItemClickListener, OnEditorActionListener { |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 46 | |
| 47 | private UrlInputListener mListener; |
| 48 | private InputMethodManager mInputManager; |
| 49 | private SuggestionsAdapter mAdapter; |
| 50 | private Drawable mFocusDrawable; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 51 | |
| 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 Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 69 | mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE); |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame^] | 70 | setOnEditorActionListener(this); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 71 | setOnFocusChangeListener(this); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 72 | final ContentResolver cr = mContext.getContentResolver(); |
| 73 | mAdapter = new SuggestionsAdapter(mContext, |
| 74 | BrowserProvider.getBookmarksSuggestions(cr, null)); |
| 75 | setAdapter(mAdapter); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 76 | setOnItemClickListener(this); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 77 | setSelectAllOnFocus(true); |
| 78 | } |
| 79 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 80 | @Override |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame^] | 81 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 82 | finishInput(getText().toString()); |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | @Override |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 87 | 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 Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 103 | 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 Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 119 | } |
| 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 Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 132 | public void onDismiss(); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 133 | public void onAction(String text); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 134 | } |
| 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 | } |