summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/preferences/ModifiableHintPreference.java
blob: c545472e240c306d3e0121932e05c765dd6435ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.preferences;

import org.mozilla.gecko.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.preference.Preference;
import android.text.Spanned;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

class ModifiableHintPreference extends Preference {
    private static final String LOGTAG = "ModifiableHintPref";
    private final Context mContext;

    private final String MATCH_STRING = "%I";
    private final int RESID_TEXT_VIEW = R.id.label_search_hint;
    private final int RESID_DRAWABLE = R.drawable.ab_add_search_engine;
    private final double SCALE_FACTOR = 0.5;

    public ModifiableHintPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public ModifiableHintPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }

    @Override
    protected View onCreateView(ViewGroup parent) {
        View thisView = super.onCreateView(parent);
        configurePreferenceView(thisView);
        return thisView;
    }

    private void configurePreferenceView(View view) {
        TextView textView = (TextView) view.findViewById(RESID_TEXT_VIEW);
        String searchHint = textView.getText().toString();

        // Use an ImageSpan to include the "add search" icon in the Tip.
        int imageSpanIndex = searchHint.indexOf(MATCH_STRING);
        if (imageSpanIndex != -1) {
            // Scale the resource.
            Drawable drawable = mContext.getResources().getDrawable(RESID_DRAWABLE);
            drawable.setBounds(0, 0, (int) (drawable.getIntrinsicWidth() * SCALE_FACTOR),
                               (int) (drawable.getIntrinsicHeight() * SCALE_FACTOR));

            ImageSpan searchIcon = new ImageSpan(drawable);
            final SpannableStringBuilder hintBuilder = new SpannableStringBuilder(searchHint);

            // Insert the image.
            hintBuilder.setSpan(searchIcon, imageSpanIndex, imageSpanIndex + MATCH_STRING.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            textView.setText(hintBuilder, TextView.BufferType.SPANNABLE);
        }
    }
}