DefaultSuggestionView.java revision 883c1bf364e38c5b133afb55f8493a14b65f4dd4
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.R; 20import com.android.quicksearchbox.Source; 21import com.android.quicksearchbox.SuggestionCursor; 22 23import android.content.Context; 24import android.graphics.drawable.Drawable; 25import android.text.Html; 26import android.text.TextUtils; 27import android.util.AttributeSet; 28import android.util.Log; 29import android.view.View; 30import android.widget.ImageView; 31import android.widget.RelativeLayout; 32import android.widget.TextView; 33 34/** 35 * View for the items in the suggestions list. This includes promoted suggestions, 36 * sources, and suggestions under each source. 37 * 38 */ 39public class DefaultSuggestionView extends RelativeLayout implements SuggestionView { 40 41 private static final boolean DBG = false; 42 private static final String TAG = "QSB.SuggestionView"; 43 44 private TextView mText1; 45 private TextView mText2; 46 private ImageView mIcon1; 47 private ImageView mIcon2; 48 49 public DefaultSuggestionView(Context context, AttributeSet attrs, int defStyle) { 50 super(context, attrs, defStyle); 51 } 52 53 public DefaultSuggestionView(Context context, AttributeSet attrs) { 54 super(context, attrs); 55 } 56 57 public DefaultSuggestionView(Context context) { 58 super(context); 59 } 60 61 @Override 62 protected void onFinishInflate() { 63 super.onFinishInflate(); 64 mText1 = (TextView) findViewById(R.id.text1); 65 mText2 = (TextView) findViewById(R.id.text2); 66 mIcon1 = (ImageView) findViewById(R.id.icon1); 67 mIcon2 = (ImageView) findViewById(R.id.icon2); 68 } 69 70 public void bindAsSuggestion(SuggestionCursor suggestion) { 71 String format = suggestion.getSuggestionFormat(); 72 CharSequence text1 = formatText(suggestion.getSuggestionText1(), format); 73 CharSequence text2 = formatText(suggestion.getSuggestionText2(), format); 74 Drawable icon1 = getSuggestionDrawableIcon1(suggestion); 75 Drawable icon2 = getSuggestionDrawableIcon2(suggestion); 76 if (DBG) { 77 Log.d(TAG, "bindAsSuggestion(), text1=" + text1 + ",text2=" + text2 78 + ",icon1=" + icon1 + ",icon2=" + icon2); 79 } 80 setText1(text1); 81 setText2(text2); 82 setIcon1(icon1); 83 setIcon2(icon2); 84 } 85 86 public Drawable getSuggestionDrawableIcon1(SuggestionCursor suggestion) { 87 Source source = suggestion.getSuggestionSource(); 88 String icon1Id = suggestion.getSuggestionIcon1(); 89 Drawable icon1 = source.getIcon(icon1Id); 90 return icon1 == null ? source.getSourceIcon() : icon1; 91 } 92 93 public Drawable getSuggestionDrawableIcon2(SuggestionCursor suggestion) { 94 Source source = suggestion.getSuggestionSource(); 95 return source.getIcon(suggestion.getSuggestionIcon2()); 96 } 97 98 private CharSequence formatText(String str, String format) { 99 boolean isHtml = "html".equals(format); 100 if (isHtml && looksLikeHtml(str)) { 101 return Html.fromHtml(str); 102 } else { 103 return str; 104 } 105 } 106 107 private boolean looksLikeHtml(String str) { 108 if (TextUtils.isEmpty(str)) return false; 109 for (int i = str.length() - 1; i >= 0; i--) { 110 char c = str.charAt(i); 111 if (c == '>' || c == '&') return true; 112 } 113 return false; 114 } 115 116 public void setText1(CharSequence text) { 117 mText1.setText(text); 118 } 119 120 public void setText2(CharSequence text) { 121 mText2.setText(text); 122 if (TextUtils.isEmpty(text)) { 123 mText2.setVisibility(GONE); 124 } else { 125 mText2.setVisibility(VISIBLE); 126 } 127 } 128 129 public void setIcon1(Drawable icon) { 130 setViewDrawable(mIcon1, icon); 131 } 132 133 public void setIcon2(Drawable icon) { 134 setViewDrawable(mIcon2, icon); 135 } 136 137 /** 138 * Sets the drawable in an image view, makes sure the view is only visible if there 139 * is a drawable. 140 */ 141 private static void setViewDrawable(ImageView v, Drawable drawable) { 142 // Set the icon even if the drawable is null, since we need to clear any 143 // previous icon. 144 v.setImageDrawable(drawable); 145 146 if (drawable == null) { 147 v.setVisibility(View.GONE); 148 } else { 149 v.setVisibility(View.VISIBLE); 150 151 // This is a hack to get any animated drawables (like a 'working' spinner) 152 // to animate. You have to setVisible true on an AnimationDrawable to get 153 // it to start animating, but it must first have been false or else the 154 // call to setVisible will be ineffective. We need to clear up the story 155 // about animated drawables in the future, see http://b/1878430. 156 drawable.setVisible(false, false); 157 drawable.setVisible(true, false); 158 } 159 } 160 161} 162