DefaultSuggestionView.java revision 5229b06f00d20aac76cd8519b37f2a579d61c54f
1/* 2 * Copyright (C) 2009 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.quicksearchbox.ui; 18 19import com.android.quicksearchbox.QsbApplication; 20import com.android.quicksearchbox.R; 21import com.android.quicksearchbox.Source; 22import com.android.quicksearchbox.Suggestion; 23import com.android.quicksearchbox.SuggestionFormatter; 24 25import android.content.Context; 26import android.content.res.ColorStateList; 27import android.graphics.drawable.Drawable; 28import android.text.Html; 29import android.text.Spannable; 30import android.text.SpannableString; 31import android.text.TextUtils; 32import android.text.style.TextAppearanceSpan; 33import android.util.AttributeSet; 34import android.util.Log; 35import android.view.KeyEvent; 36import android.view.View; 37import android.widget.ImageView; 38import android.widget.RelativeLayout; 39import android.widget.TextView; 40 41/** 42 * View for the items in the suggestions list. This includes promoted suggestions, 43 * sources, and suggestions under each source. 44 */ 45public class DefaultSuggestionView extends RelativeLayout implements SuggestionView { 46 47 private static final boolean DBG = false; 48 private static final String TAG = "QSB.SuggestionView"; 49 50 public static final String VIEW_ID = "default"; 51 52 private TextView mText1; 53 private TextView mText2; 54 private ImageView mIcon1; 55 private ImageView mIcon2; 56 private final SuggestionFormatter mSuggestionFormatter; 57 private boolean mRefineable; 58 private int mPosition; 59 private SuggestionsAdapter mAdapter; 60 private KeyListener mKeyListener; 61 62 public DefaultSuggestionView(Context context, AttributeSet attrs, int defStyle) { 63 super(context, attrs, defStyle); 64 mSuggestionFormatter = QsbApplication.get(context).getSuggestionFormatter(); 65 } 66 67 public DefaultSuggestionView(Context context, AttributeSet attrs) { 68 super(context, attrs); 69 mSuggestionFormatter = QsbApplication.get(context).getSuggestionFormatter(); 70 } 71 72 public DefaultSuggestionView(Context context) { 73 super(context); 74 mSuggestionFormatter = QsbApplication.get(context).getSuggestionFormatter(); 75 } 76 77 @Override 78 protected void onFinishInflate() { 79 super.onFinishInflate(); 80 mText1 = (TextView) findViewById(R.id.text1); 81 mText2 = (TextView) findViewById(R.id.text2); 82 mIcon1 = (ImageView) findViewById(R.id.icon1); 83 mIcon2 = (ImageView) findViewById(R.id.icon2); 84 // for some reason, creating mKeyListener inside the constructor causes it not to work. 85 mKeyListener = new KeyListener(); 86 87 setOnKeyListener(mKeyListener); 88 } 89 90 public void bindAsSuggestion(Suggestion suggestion, String userQuery) { 91 setOnClickListener(new ClickListener()); 92 setOnLongClickListener(new LongClickListener()); 93 94 CharSequence text1 = formatText(suggestion.getSuggestionText1(), suggestion, userQuery); 95 CharSequence text2 = suggestion.getSuggestionText2Url(); 96 if (text2 != null) { 97 text2 = formatUrl(text2); 98 } else { 99 text2 = formatText(suggestion.getSuggestionText2(), suggestion, null); 100 } 101 Drawable icon1 = getSuggestionDrawableIcon1(suggestion); 102 Drawable icon2 = getSuggestionDrawableIcon2(suggestion); 103 if (DBG) { 104 Log.d(TAG, "bindAsSuggestion(), text1=" + text1 + ",text2=" + text2 105 + ",icon1=" + icon1 + ",icon2=" + icon2); 106 } 107 // If there is no text for the second line, allow the first line to be up to two lines 108 if (TextUtils.isEmpty(text2)) { 109 mText1.setSingleLine(false); 110 mText1.setMaxLines(2); 111 mText1.setEllipsize(TextUtils.TruncateAt.START); 112 } else { 113 mText1.setSingleLine(true); 114 mText1.setMaxLines(1); 115 mText1.setEllipsize(TextUtils.TruncateAt.MIDDLE); 116 } 117 setText1(text1); 118 setText2(text2); 119 setIcon1(icon1); 120 setIcon2(icon2, null); 121 updateRefinable(suggestion); 122 } 123 124 public void bindAdapter(SuggestionsAdapter adapter, int position) { 125 mAdapter = adapter; 126 mPosition = position; 127 } 128 129 protected void updateRefinable(Suggestion suggestion) { 130 mRefineable = 131 suggestion.isWebSearchSuggestion() 132 && mIcon2.getDrawable() == null 133 && !TextUtils.isEmpty(suggestion.getSuggestionQuery()); 134 setRefinable(suggestion, mRefineable); 135 } 136 137 protected void setRefinable(Suggestion suggestion, boolean refinable) { 138 if (refinable) { 139 mIcon2.setOnClickListener(new View.OnClickListener() { 140 public void onClick(View v) { 141 onSuggestionQueryRefineClicked(); 142 } 143 }); 144 mIcon2.setFocusable(true); 145 mIcon2.setOnKeyListener(mKeyListener); 146 Drawable icon2 = getContext().getResources().getDrawable(R.drawable.edit_query); 147 Drawable background = 148 getContext().getResources().getDrawable(R.drawable.edit_query_background); 149 setIcon2(icon2, background); 150 } else { 151 mIcon2.setOnClickListener(null); 152 mIcon2.setFocusable(false); 153 mIcon2.setOnKeyListener(null); 154 } 155 } 156 157 private CharSequence formatUrl(CharSequence url) { 158 SpannableString text = new SpannableString(url); 159 ColorStateList colors = getResources().getColorStateList(R.color.url_text); 160 text.setSpan(new TextAppearanceSpan(null, 0, 0, colors, null), 161 0, url.length(), 162 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 163 return text; 164 } 165 166 public Drawable getSuggestionDrawableIcon1(Suggestion suggestion) { 167 Source source = suggestion.getSuggestionSource(); 168 String iconId = suggestion.getSuggestionIcon1(); 169 Drawable icon1 = iconId == null ? null : source.getIcon(iconId); 170 return icon1 == null ? source.getSourceIcon() : icon1; 171 } 172 173 public Drawable getSuggestionDrawableIcon2(Suggestion suggestion) { 174 Source source = suggestion.getSuggestionSource(); 175 String iconId = suggestion.getSuggestionIcon2(); 176 return iconId == null ? null : source.getIcon(iconId); 177 } 178 179 private CharSequence formatText(String str, Suggestion suggestion, 180 String userQuery) { 181 boolean isHtml = "html".equals(suggestion.getSuggestionFormat()); 182 if (isHtml && looksLikeHtml(str)) { 183 return Html.fromHtml(str); 184 } else if (suggestion.isWebSearchSuggestion() && !TextUtils.isEmpty(userQuery)) { 185 return mSuggestionFormatter.formatSuggestion(userQuery, str); 186 } else { 187 return str; 188 } 189 } 190 191 private boolean looksLikeHtml(String str) { 192 if (TextUtils.isEmpty(str)) return false; 193 for (int i = str.length() - 1; i >= 0; i--) { 194 char c = str.charAt(i); 195 if (c == '>' || c == '&') return true; 196 } 197 return false; 198 } 199 200 /** 201 * Sets the first text line. 202 */ 203 private void setText1(CharSequence text) { 204 mText1.setText(text); 205 } 206 207 /** 208 * Sets the second text line. 209 */ 210 private void setText2(CharSequence text) { 211 mText2.setText(text); 212 if (TextUtils.isEmpty(text)) { 213 mText2.setVisibility(GONE); 214 } else { 215 mText2.setVisibility(VISIBLE); 216 } 217 } 218 219 /** 220 * Sets the left-hand-side icon. 221 */ 222 private void setIcon1(Drawable icon) { 223 setViewDrawable(mIcon1, icon); 224 } 225 226 /** 227 * Sets the right-hand-side icon and its background. 228 */ 229 private void setIcon2(Drawable icon, Drawable background) { 230 setViewDrawable(mIcon2, icon); 231 mIcon2.setBackgroundDrawable(background); 232 } 233 234 /** 235 * Sets the drawable in an image view, makes sure the view is only visible if there 236 * is a drawable. 237 */ 238 private static void setViewDrawable(ImageView v, Drawable drawable) { 239 // Set the icon even if the drawable is null, since we need to clear any 240 // previous icon. 241 v.setImageDrawable(drawable); 242 243 if (drawable == null) { 244 v.setVisibility(View.GONE); 245 } else { 246 v.setVisibility(View.VISIBLE); 247 248 // This is a hack to get any animated drawables (like a 'working' spinner) 249 // to animate. You have to setVisible true on an AnimationDrawable to get 250 // it to start animating, but it must first have been false or else the 251 // call to setVisible will be ineffective. We need to clear up the story 252 // about animated drawables in the future, see http://b/1878430. 253 drawable.setVisible(false, false); 254 drawable.setVisible(true, false); 255 } 256 } 257 258 protected void onSuggestionClicked() { 259 if (mAdapter != null) { 260 mAdapter.onSuggestionClicked(mPosition); 261 } 262 } 263 264 protected void onSuggestionQuickContactClicked() { 265 if (mAdapter != null) { 266 mAdapter.onSuggestionQuickContactClicked(mPosition); 267 } 268 } 269 270 protected boolean onSuggestionLongClicked() { 271 if (mAdapter != null) { 272 return mAdapter.onSuggestionLongClicked(mPosition); 273 } 274 return false; 275 } 276 277 protected void onSuggestionQueryRefineClicked() { 278 if (mAdapter != null) { 279 mAdapter.onSuggestionQueryRefineClicked(mPosition); 280 } 281 } 282 283 private class ClickListener implements OnClickListener { 284 public void onClick(View v) { 285 onSuggestionClicked(); 286 } 287 } 288 289 private class LongClickListener implements OnLongClickListener { 290 public boolean onLongClick(View v) { 291 return onSuggestionLongClicked(); 292 } 293 } 294 295 private class KeyListener implements View.OnKeyListener { 296 public boolean onKey(View v, int keyCode, KeyEvent event) { 297 boolean consumed = false; 298 if (event.getAction() == KeyEvent.ACTION_DOWN) { 299 if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && v != mIcon2) { 300 consumed = mIcon2.requestFocus(); 301 if (DBG) Log.d(TAG, "onKey Icon2 accepted focus: " + consumed); 302 } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && v == mIcon2) { 303 consumed = requestFocus(); 304 if (DBG) Log.d(TAG, "onKey SuggestionView accepted focus: " + consumed); 305 } 306 } 307 return consumed; 308 } 309 } 310 311} 312