1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.accessibility; 18 19 import android.content.Context; 20 import android.text.Html; 21 import android.text.TextUtils; 22 import android.widget.TextView; 23 24 import androidx.preference.PreferenceViewHolder; 25 26 /** 27 * A custom {@link android.widget.TextView} preference that shows html text with a custom tag 28 * filter. 29 */ 30 public final class HtmlTextPreference extends StaticTextPreference { 31 32 private int mFlag = Html.FROM_HTML_MODE_COMPACT; 33 private Html.ImageGetter mImageGetter; 34 private Html.TagHandler mTagHandler; 35 HtmlTextPreference(Context context)36 HtmlTextPreference(Context context) { 37 super(context); 38 } 39 40 @Override onBindViewHolder(PreferenceViewHolder holder)41 public void onBindViewHolder(PreferenceViewHolder holder) { 42 super.onBindViewHolder(holder); 43 44 final TextView summaryView = holder.itemView.findViewById(android.R.id.summary); 45 if (summaryView != null && !TextUtils.isEmpty(getSummary())) { 46 summaryView.setText( 47 Html.fromHtml(getSummary().toString(), mFlag, mImageGetter, mTagHandler)); 48 } 49 } 50 51 /** 52 * Sets the flag to which text format to be applied. 53 * 54 * @param flag to indicate that html text format 55 */ setFlag(int flag)56 public void setFlag(int flag) { 57 if (flag != mFlag) { 58 mFlag = flag; 59 notifyChanged(); 60 } 61 } 62 63 /** 64 * Sets image getter and help to load corresponding images when parsing. 65 * 66 * @param imageGetter to load image by image tag content 67 */ setImageGetter(Html.ImageGetter imageGetter)68 public void setImageGetter(Html.ImageGetter imageGetter) { 69 if (imageGetter != null && !imageGetter.equals(mImageGetter)) { 70 mImageGetter = imageGetter; 71 notifyChanged(); 72 } 73 } 74 75 /** 76 * Sets tag handler to handle the unsupported tags. 77 * 78 * @param tagHandler the handler for unhandled tags 79 */ setTagHandler(Html.TagHandler tagHandler)80 public void setTagHandler(Html.TagHandler tagHandler) { 81 if (tagHandler != null && !tagHandler.equals(mTagHandler)) { 82 mTagHandler = tagHandler; 83 notifyChanged(); 84 } 85 } 86 } 87