summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/widget/TextViewPreference.java
blob: 1c479d404964fa5e15984477c33113f6f77300d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dialer.widget;

import android.content.Context;
import android.preference.Preference;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

/**
 * Provides a {@link TextView} inside a preference. Useful for displaying static text which may
 * contain hyperlinks.
 */
public class TextViewPreference extends Preference {

  /**
   * The resource ID of the text to be populated in the {@link TextView} when a resource ID is used.
   */
  private int textResourceId = 0;

  /** The text to be populated in the {@link TextView} when a {@link CharSequence} is used. */
  private CharSequence text;

  /** The {@link TextView} containing the text. */
  private TextView textView;

  /**
   * Instantiates the {@link TextViewPreference} instance.
   *
   * @param context The Context this is associated with, through which it can access the current
   *     theme, resources, etc.
   * @param attrs The attributes of the XML tag that is inflating the preference.
   * @param defStyleAttr An attribute in the current theme that contains a reference to a style
   *     resource that supplies default values for the view. Can be 0 to not look for defaults.
   * @param defStyleRes A resource identifier of a style resource that supplies default values for
   *     the view, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not
   *     look for defaults.
   */
  public TextViewPreference(
      Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);

    setLayoutResource(R.layout.text_view_preference);
  }

  /**
   * Instantiates the {@link TextViewPreference} instance.
   *
   * @param context The Context this is associated with, through which it can access the current
   *     theme, resources, etc.
   * @param attrs The attributes of the XML tag that is inflating the preference.
   * @param defStyleAttr An attribute in the current theme that contains a reference to a style
   *     resource that supplies default values for the view. Can be 0 to not look for defaults.
   */
  public TextViewPreference(Context context, AttributeSet attrs, int defStyleAttr) {
    this(context, attrs, defStyleAttr, 0);
  }

  /**
   * Instantiates the {@link TextViewPreference} instance.
   *
   * @param context The Context this is associated with, through which it can access the current
   *     theme, resources, etc.
   * @param attrs The attributes of the XML tag that is inflating the preference.
   */
  public TextViewPreference(Context context, AttributeSet attrs) {
    this(context, attrs, android.R.attr.preferenceStyle, 0);
  }

  /**
   * Instantiates the {@link TextViewPreference} instance.
   *
   * @param context The Context this is associated with, through which it can access the current
   *     theme, resources, etc.
   */
  public TextViewPreference(Context context) {
    super(context, null);

    setLayoutResource(R.layout.text_view_preference);
  }

  /**
   * Handles binding the preference.
   *
   * @param view The view.
   */
  @Override
  protected void onBindView(View view) {
    super.onBindView(view);
    textView = (TextView) view.findViewById(R.id.text);
    if (textResourceId != 0) {
      setTitle(textResourceId);
    } else if (text != null) {
      setTitle(text);
    } else if (getTitleRes() != 0) {
      setTitle(getTitleRes());
    }
  }

  /**
   * Sets the preference title from a {@link CharSequence}.
   *
   * @param text The text.
   */
  @Override
  public void setTitle(CharSequence text) {
    textResourceId = 0;
    this.text = text;
    if (textView == null) {
      return;
    }

    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setText(text);
  }

  /**
   * Sets the preference title from a resource id.
   *
   * @param textResId The string resource Id.
   */
  @Override
  public void setTitle(int textResId) {
    textResourceId = textResId;
    setTitle(getContext().getString(textResId));
  }
}