mobile/android/base/preferences/ModifiableHintPreference.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/preferences/ModifiableHintPreference.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,67 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko.preferences;
    1.10 +
    1.11 +import org.mozilla.gecko.R;
    1.12 +
    1.13 +import android.content.Context;
    1.14 +import android.graphics.drawable.Drawable;
    1.15 +import android.preference.Preference;
    1.16 +import android.text.Spanned;
    1.17 +import android.text.SpannableStringBuilder;
    1.18 +import android.text.style.ImageSpan;
    1.19 +import android.util.AttributeSet;
    1.20 +import android.view.View;
    1.21 +import android.view.ViewGroup;
    1.22 +import android.widget.TextView;
    1.23 +
    1.24 +class ModifiableHintPreference extends Preference {
    1.25 +    private static final String LOGTAG = "ModifiableHintPref";
    1.26 +    private final Context mContext;
    1.27 +
    1.28 +    private final String MATCH_STRING = "%I";
    1.29 +    private final int RESID_TEXT_VIEW = R.id.label_search_hint;
    1.30 +    private final int RESID_DRAWABLE = R.drawable.ab_add_search_engine;
    1.31 +    private final double SCALE_FACTOR = 0.5;
    1.32 +
    1.33 +    public ModifiableHintPreference(Context context, AttributeSet attrs) {
    1.34 +        super(context, attrs);
    1.35 +        mContext = context;
    1.36 +    }
    1.37 +
    1.38 +    public ModifiableHintPreference(Context context, AttributeSet attrs, int defStyle) {
    1.39 +        super(context, attrs, defStyle);
    1.40 +        mContext = context;
    1.41 +    }
    1.42 +
    1.43 +    @Override
    1.44 +    protected View onCreateView(ViewGroup parent) {
    1.45 +        View thisView = super.onCreateView(parent);
    1.46 +        configurePreferenceView(thisView);
    1.47 +        return thisView;
    1.48 +    }
    1.49 +
    1.50 +    private void configurePreferenceView(View view) {
    1.51 +        TextView textView = (TextView) view.findViewById(RESID_TEXT_VIEW);
    1.52 +        String searchHint = textView.getText().toString();
    1.53 +
    1.54 +        // Use an ImageSpan to include the "add search" icon in the Tip.
    1.55 +        int imageSpanIndex = searchHint.indexOf(MATCH_STRING);
    1.56 +        if (imageSpanIndex != -1) {
    1.57 +            // Scale the resource.
    1.58 +            Drawable drawable = mContext.getResources().getDrawable(RESID_DRAWABLE);
    1.59 +            drawable.setBounds(0, 0, (int) (drawable.getIntrinsicWidth() * SCALE_FACTOR),
    1.60 +                               (int) (drawable.getIntrinsicHeight() * SCALE_FACTOR));
    1.61 +
    1.62 +            ImageSpan searchIcon = new ImageSpan(drawable);
    1.63 +            final SpannableStringBuilder hintBuilder = new SpannableStringBuilder(searchHint);
    1.64 +
    1.65 +            // Insert the image.
    1.66 +            hintBuilder.setSpan(searchIcon, imageSpanIndex, imageSpanIndex + MATCH_STRING.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    1.67 +            textView.setText(hintBuilder, TextView.BufferType.SPANNABLE);
    1.68 +        }
    1.69 +    }
    1.70 +}

mercurial