mobile/android/base/preferences/ModifiableHintPreference.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial