Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2018 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.dialer.widget;
     18 
     19 import android.content.Context;
     20 import android.preference.Preference;
     21 import android.text.method.LinkMovementMethod;
     22 import android.util.AttributeSet;
     23 import android.view.View;
     24 import android.widget.TextView;
     25 
     26 /**
     27  * Provides a {@link TextView} inside a preference. Useful for displaying static text which may
     28  * contain hyperlinks.
     29  */
     30 public class TextViewPreference extends Preference {
     31 
     32   /**
     33    * The resource ID of the text to be populated in the {@link TextView} when a resource ID is used.
     34    */
     35   private int textResourceId = 0;
     36 
     37   /** The text to be populated in the {@link TextView} when a {@link CharSequence} is used. */
     38   private CharSequence text;
     39 
     40   /** The {@link TextView} containing the text. */
     41   private TextView textView;
     42 
     43   /**
     44    * Instantiates the {@link TextViewPreference} instance.
     45    *
     46    * @param context The Context this is associated with, through which it can access the current
     47    *     theme, resources, etc.
     48    * @param attrs The attributes of the XML tag that is inflating the preference.
     49    * @param defStyleAttr An attribute in the current theme that contains a reference to a style
     50    *     resource that supplies default values for the view. Can be 0 to not look for defaults.
     51    * @param defStyleRes A resource identifier of a style resource that supplies default values for
     52    *     the view, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not
     53    *     look for defaults.
     54    */
     55   public TextViewPreference(
     56       Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
     57     super(context, attrs, defStyleAttr, defStyleRes);
     58 
     59     setLayoutResource(R.layout.text_view_preference);
     60   }
     61 
     62   /**
     63    * Instantiates the {@link TextViewPreference} instance.
     64    *
     65    * @param context The Context this is associated with, through which it can access the current
     66    *     theme, resources, etc.
     67    * @param attrs The attributes of the XML tag that is inflating the preference.
     68    * @param defStyleAttr An attribute in the current theme that contains a reference to a style
     69    *     resource that supplies default values for the view. Can be 0 to not look for defaults.
     70    */
     71   public TextViewPreference(Context context, AttributeSet attrs, int defStyleAttr) {
     72     this(context, attrs, defStyleAttr, 0);
     73   }
     74 
     75   /**
     76    * Instantiates the {@link TextViewPreference} instance.
     77    *
     78    * @param context The Context this is associated with, through which it can access the current
     79    *     theme, resources, etc.
     80    * @param attrs The attributes of the XML tag that is inflating the preference.
     81    */
     82   public TextViewPreference(Context context, AttributeSet attrs) {
     83     this(context, attrs, android.R.attr.preferenceStyle, 0);
     84   }
     85 
     86   /**
     87    * Instantiates the {@link TextViewPreference} instance.
     88    *
     89    * @param context The Context this is associated with, through which it can access the current
     90    *     theme, resources, etc.
     91    */
     92   public TextViewPreference(Context context) {
     93     super(context, null);
     94 
     95     setLayoutResource(R.layout.text_view_preference);
     96   }
     97 
     98   /**
     99    * Handles binding the preference.
    100    *
    101    * @param view The view.
    102    */
    103   @Override
    104   protected void onBindView(View view) {
    105     super.onBindView(view);
    106     textView = (TextView) view.findViewById(R.id.text);
    107     if (textResourceId != 0) {
    108       setTitle(textResourceId);
    109     } else if (text != null) {
    110       setTitle(text);
    111     }
    112   }
    113 
    114   /**
    115    * Sets the preference title from a {@link CharSequence}.
    116    *
    117    * @param text The text.
    118    */
    119   @Override
    120   public void setTitle(CharSequence text) {
    121     textResourceId = 0;
    122     this.text = text;
    123     if (textView == null) {
    124       return;
    125     }
    126 
    127     textView.setMovementMethod(LinkMovementMethod.getInstance());
    128     textView.setText(text);
    129   }
    130 
    131   /**
    132    * Sets the preference title from a resource id.
    133    *
    134    * @param textResId The string resource Id.
    135    */
    136   @Override
    137   public void setTitle(int textResId) {
    138     textResourceId = textResId;
    139     setTitle(getContext().getString(textResId));
    140   }
    141 }
    142