michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.preferences; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: michael@0: import android.content.Context; michael@0: import android.graphics.drawable.Drawable; michael@0: import android.preference.Preference; michael@0: import android.text.Spanned; michael@0: import android.text.SpannableStringBuilder; michael@0: import android.text.style.ImageSpan; michael@0: import android.util.AttributeSet; michael@0: import android.view.View; michael@0: import android.view.ViewGroup; michael@0: import android.widget.TextView; michael@0: michael@0: class ModifiableHintPreference extends Preference { michael@0: private static final String LOGTAG = "ModifiableHintPref"; michael@0: private final Context mContext; michael@0: michael@0: private final String MATCH_STRING = "%I"; michael@0: private final int RESID_TEXT_VIEW = R.id.label_search_hint; michael@0: private final int RESID_DRAWABLE = R.drawable.ab_add_search_engine; michael@0: private final double SCALE_FACTOR = 0.5; michael@0: michael@0: public ModifiableHintPreference(Context context, AttributeSet attrs) { michael@0: super(context, attrs); michael@0: mContext = context; michael@0: } michael@0: michael@0: public ModifiableHintPreference(Context context, AttributeSet attrs, int defStyle) { michael@0: super(context, attrs, defStyle); michael@0: mContext = context; michael@0: } michael@0: michael@0: @Override michael@0: protected View onCreateView(ViewGroup parent) { michael@0: View thisView = super.onCreateView(parent); michael@0: configurePreferenceView(thisView); michael@0: return thisView; michael@0: } michael@0: michael@0: private void configurePreferenceView(View view) { michael@0: TextView textView = (TextView) view.findViewById(RESID_TEXT_VIEW); michael@0: String searchHint = textView.getText().toString(); michael@0: michael@0: // Use an ImageSpan to include the "add search" icon in the Tip. michael@0: int imageSpanIndex = searchHint.indexOf(MATCH_STRING); michael@0: if (imageSpanIndex != -1) { michael@0: // Scale the resource. michael@0: Drawable drawable = mContext.getResources().getDrawable(RESID_DRAWABLE); michael@0: drawable.setBounds(0, 0, (int) (drawable.getIntrinsicWidth() * SCALE_FACTOR), michael@0: (int) (drawable.getIntrinsicHeight() * SCALE_FACTOR)); michael@0: michael@0: ImageSpan searchIcon = new ImageSpan(drawable); michael@0: final SpannableStringBuilder hintBuilder = new SpannableStringBuilder(searchHint); michael@0: michael@0: // Insert the image. michael@0: hintBuilder.setSpan(searchIcon, imageSpanIndex, imageSpanIndex + MATCH_STRING.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); michael@0: textView.setText(hintBuilder, TextView.BufferType.SPANNABLE); michael@0: } michael@0: } michael@0: }