summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Bjorn Bringert <bringert@android.com> 2010-03-02 11:16:17 +0000
committer Bjorn Bringert <bringert@android.com> 2010-03-03 09:20:36 +0000
commit0b49ab5cae98d5f27b490b6de35d92e7a63a2e64 (patch)
treeb1e26e54eec07b623e20af711d9d41c318a05739
parent97106ab5af0fdba00d6dbda4767b953e0a5829f4 (diff)
Add SearchManager.SUGGEST_COLUMN_TEXT_2_URL
This column overrides SUGGEST_COLUMN_TEXT_2. SearchDialog and QuickSearchBox render the value of this column as a URL in green. Part of the fix for http://b/issue?id=2380681 Change-Id: I6735e0eba90e24c81f9e72520f257e5e61796d7a
-rw-r--r--api/current.xml11
-rw-r--r--core/java/android/app/SearchManager.java10
-rw-r--r--core/java/android/app/SuggestionsAdapter.java75
3 files changed, 67 insertions, 29 deletions
diff --git a/api/current.xml b/api/current.xml
index be38d49c5734..1f7be9ed9d95 100644
--- a/api/current.xml
+++ b/api/current.xml
@@ -24473,6 +24473,17 @@
visibility="public"
>
</field>
+<field name="SUGGEST_COLUMN_TEXT_2_URL"
+ type="java.lang.String"
+ transient="false"
+ volatile="false"
+ value="&quot;suggest_text_2_url&quot;"
+ static="true"
+ final="true"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</field>
<field name="SUGGEST_MIME_TYPE"
type="java.lang.String"
transient="false"
diff --git a/core/java/android/app/SearchManager.java b/core/java/android/app/SearchManager.java
index b54e53dee6f4..67f9629a5ece 100644
--- a/core/java/android/app/SearchManager.java
+++ b/core/java/android/app/SearchManager.java
@@ -1355,6 +1355,16 @@ public class SearchManager
* a much smaller appearance.
*/
public final static String SUGGEST_COLUMN_TEXT_2 = "suggest_text_2";
+
+ /**
+ * Column name for suggestions cursor. <i>Optional.</i> This is a URL that will be shown
+ * as the second line of text instead of {@link #SUGGEST_COLUMN_TEXT_2}. This is a separate
+ * column so that the search UI knows to display the text as a URL, e.g. by using a different
+ * color. If this column is absent, or has the value {@code null},
+ * {@link #SUGGEST_COLUMN_TEXT_2} will be used instead.
+ */
+ public final static String SUGGEST_COLUMN_TEXT_2_URL = "suggest_text_2_url";
+
/**
* Column name for suggestions cursor. <i>Optional.</i> If your cursor includes this column,
* then all suggestions will be provided in a format that includes space for two small icons,
diff --git a/core/java/android/app/SuggestionsAdapter.java b/core/java/android/app/SuggestionsAdapter.java
index 57795d11b912..07e979374a89 100644
--- a/core/java/android/app/SuggestionsAdapter.java
+++ b/core/java/android/app/SuggestionsAdapter.java
@@ -16,6 +16,8 @@
package android.app;
+import com.android.internal.R;
+
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
@@ -23,16 +25,20 @@ import android.content.ContentResolver.OpenResourceIdResult;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
+import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.net.Uri;
-import android.text.Html;
+import android.text.Spannable;
+import android.text.SpannableString;
import android.text.TextUtils;
+import android.text.style.TextAppearanceSpan;
import android.util.Log;
import android.util.SparseArray;
+import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
@@ -64,10 +70,13 @@ class SuggestionsAdapter extends ResourceCursorAdapter {
private SparseArray<Drawable.ConstantState> mBackgroundsCache;
private boolean mClosed = false;
+ // URL color
+ private ColorStateList mUrlColor;
+
// Cached column indexes, updated when the cursor changes.
- private int mFormatCol;
private int mText1Col;
private int mText2Col;
+ private int mText2UrlCol;
private int mIconName1Col;
private int mIconName2Col;
private int mBackgroundColorCol;
@@ -188,9 +197,9 @@ class SuggestionsAdapter extends ResourceCursorAdapter {
super.changeCursor(c);
if (c != null) {
- mFormatCol = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_FORMAT);
mText1Col = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1);
mText2Col = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2);
+ mText2UrlCol = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL);
mIconName1Col = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_ICON_1);
mIconName2Col = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_ICON_2);
mBackgroundColorCol = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_BACKGROUND_COLOR);
@@ -239,9 +248,20 @@ class SuggestionsAdapter extends ResourceCursorAdapter {
Drawable background = getItemBackground(backgroundColor);
view.setBackgroundDrawable(background);
- final boolean isHtml = mFormatCol > 0 && "html".equals(cursor.getString(mFormatCol));
- setViewText(cursor, views.mText1, mText1Col, isHtml);
- setViewText(cursor, views.mText2, mText2Col, isHtml);
+ if (views.mText1 != null) {
+ String text1 = getStringOrNull(cursor, mText1Col);
+ setViewText(views.mText1, text1);
+ }
+ if (views.mText2 != null) {
+ // First check TEXT_2_URL
+ CharSequence text2 = getStringOrNull(cursor, mText2UrlCol);
+ if (text2 != null) {
+ text2 = formatUrl(text2);
+ } else {
+ text2 = getStringOrNull(cursor, mText2Col);
+ }
+ setViewText(views.mText2, text2);
+ }
if (views.mIcon1 != null) {
setViewDrawable(views.mIcon1, getIcon1(cursor));
@@ -251,6 +271,21 @@ class SuggestionsAdapter extends ResourceCursorAdapter {
}
}
+ private CharSequence formatUrl(CharSequence url) {
+ if (mUrlColor == null) {
+ // Lazily get the URL color from the current theme.
+ TypedValue colorValue = new TypedValue();
+ mContext.getTheme().resolveAttribute(R.attr.textColorSearchUrl, colorValue, true);
+ mUrlColor = mContext.getResources().getColorStateList(colorValue.resourceId);
+ }
+
+ SpannableString text = new SpannableString(url);
+ text.setSpan(new TextAppearanceSpan(null, 0, 0, mUrlColor, null),
+ 0, url.length(),
+ Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
+ return text;
+ }
+
/**
* Gets a drawable with no color when selected or pressed, and the given color when
* neither selected nor pressed.
@@ -278,19 +313,7 @@ class SuggestionsAdapter extends ResourceCursorAdapter {
}
}
- private void setViewText(Cursor cursor, TextView v, int textCol, boolean isHtml) {
- if (v == null) {
- return;
- }
- CharSequence text = null;
- if (textCol >= 0) {
- String str = cursor.getString(textCol);
- if (isHtml && looksLikeHtml(str)) {
- text = Html.fromHtml(str);
- } else {
- text = str;
- }
- }
+ private void setViewText(TextView v, CharSequence text) {
// Set the text even if it's null, since we need to clear any previous text.
v.setText(text);
@@ -301,15 +324,6 @@ class SuggestionsAdapter extends ResourceCursorAdapter {
}
}
- private static boolean looksLikeHtml(String str) {
- if (TextUtils.isEmpty(str)) return false;
- for (int i = str.length() - 1; i >= 0; i--) {
- char c = str.charAt(i);
- if (c == '<' || c == '&') return true;
- }
- return false;
- }
-
private Drawable getIcon1(Cursor cursor) {
if (mIconName1Col < 0) {
return null;
@@ -617,6 +631,10 @@ class SuggestionsAdapter extends ResourceCursorAdapter {
*/
public static String getColumnString(Cursor cursor, String columnName) {
int col = cursor.getColumnIndex(columnName);
+ return getStringOrNull(cursor, col);
+ }
+
+ private static String getStringOrNull(Cursor cursor, int col) {
if (col == NONE) {
return null;
}
@@ -629,5 +647,4 @@ class SuggestionsAdapter extends ResourceCursorAdapter {
return null;
}
}
-
}