|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.preferences; |
|
7 |
|
8 import org.mozilla.gecko.R; |
|
9 |
|
10 import android.content.Context; |
|
11 import android.graphics.drawable.Drawable; |
|
12 import android.preference.Preference; |
|
13 import android.text.Spanned; |
|
14 import android.text.SpannableStringBuilder; |
|
15 import android.text.style.ImageSpan; |
|
16 import android.util.AttributeSet; |
|
17 import android.view.View; |
|
18 import android.view.ViewGroup; |
|
19 import android.widget.TextView; |
|
20 |
|
21 class ModifiableHintPreference extends Preference { |
|
22 private static final String LOGTAG = "ModifiableHintPref"; |
|
23 private final Context mContext; |
|
24 |
|
25 private final String MATCH_STRING = "%I"; |
|
26 private final int RESID_TEXT_VIEW = R.id.label_search_hint; |
|
27 private final int RESID_DRAWABLE = R.drawable.ab_add_search_engine; |
|
28 private final double SCALE_FACTOR = 0.5; |
|
29 |
|
30 public ModifiableHintPreference(Context context, AttributeSet attrs) { |
|
31 super(context, attrs); |
|
32 mContext = context; |
|
33 } |
|
34 |
|
35 public ModifiableHintPreference(Context context, AttributeSet attrs, int defStyle) { |
|
36 super(context, attrs, defStyle); |
|
37 mContext = context; |
|
38 } |
|
39 |
|
40 @Override |
|
41 protected View onCreateView(ViewGroup parent) { |
|
42 View thisView = super.onCreateView(parent); |
|
43 configurePreferenceView(thisView); |
|
44 return thisView; |
|
45 } |
|
46 |
|
47 private void configurePreferenceView(View view) { |
|
48 TextView textView = (TextView) view.findViewById(RESID_TEXT_VIEW); |
|
49 String searchHint = textView.getText().toString(); |
|
50 |
|
51 // Use an ImageSpan to include the "add search" icon in the Tip. |
|
52 int imageSpanIndex = searchHint.indexOf(MATCH_STRING); |
|
53 if (imageSpanIndex != -1) { |
|
54 // Scale the resource. |
|
55 Drawable drawable = mContext.getResources().getDrawable(RESID_DRAWABLE); |
|
56 drawable.setBounds(0, 0, (int) (drawable.getIntrinsicWidth() * SCALE_FACTOR), |
|
57 (int) (drawable.getIntrinsicHeight() * SCALE_FACTOR)); |
|
58 |
|
59 ImageSpan searchIcon = new ImageSpan(drawable); |
|
60 final SpannableStringBuilder hintBuilder = new SpannableStringBuilder(searchHint); |
|
61 |
|
62 // Insert the image. |
|
63 hintBuilder.setSpan(searchIcon, imageSpanIndex, imageSpanIndex + MATCH_STRING.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); |
|
64 textView.setText(hintBuilder, TextView.BufferType.SPANNABLE); |
|
65 } |
|
66 } |
|
67 } |