• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 package com.android.documentsui.inspector;
17 
18 import android.content.Context;
19 import android.content.res.ColorStateList;
20 import android.content.res.Resources;
21 import android.content.res.TypedArray;
22 import android.graphics.Paint;
23 import android.text.Selection;
24 import android.text.Spannable;
25 import android.util.AttributeSet;
26 import android.view.View;
27 import android.view.textclassifier.TextClassifier;
28 import android.widget.LinearLayout;
29 import android.widget.TextView;
30 
31 import androidx.annotation.Nullable;
32 import androidx.annotation.StringRes;
33 
34 import com.android.documentsui.R;
35 
36 /**
37  * Class representing a row in the table.
38  */
39 public class KeyValueRow extends LinearLayout {
40 
41     private final Resources mRes;
42     private @Nullable ColorStateList mDefaultTextColor;
43     private @Nullable TextClassifier mClassifier;
44 
KeyValueRow(Context context)45     public KeyValueRow(Context context) {
46         this(context, null);
47     }
48 
KeyValueRow(Context context, AttributeSet attrs)49     public KeyValueRow(Context context, AttributeSet attrs) {
50         this(context, attrs, 0);
51     }
52 
KeyValueRow(Context context, AttributeSet attrs, int defStyleAttr)53     public KeyValueRow(Context context, AttributeSet attrs, int defStyleAttr) {
54         super(context, attrs, defStyleAttr);
55         mRes = context.getResources();
56     }
57 
setTextClassifier(TextClassifier classifier)58     public void setTextClassifier(TextClassifier classifier) {
59         mClassifier = classifier;
60     }
61 
62     /**
63      * Sets the raw value of the key. Only localized values
64      * should be passed.
65      */
setKey(CharSequence key)66     public void setKey(CharSequence key) {
67         ((TextView) findViewById(R.id.table_row_key)).setText(key);
68     }
69 
setKey(@tringRes int id)70     public void setKey(@StringRes int id) {
71         setKey(mRes.getString(id));
72     }
73 
setValue(CharSequence value)74     public void setValue(CharSequence value) {
75         TextView text = ((TextView) findViewById(R.id.table_row_value));
76         text.setText(value);
77         text.setTextClassifier(mClassifier);
78         text.setOnLongClickListener((View view) -> {
79 
80             CharSequence textValue = text.getText();
81             if (textValue instanceof Spannable) {
82                 Spannable spn = (Spannable) textValue;
83                 Selection.selectAll(spn);
84             }
85             // we still want the default selection arrows and menu after we specified to select
86             // all text in the TextView.
87             return false;
88         });
89     }
90 
91     @Override
hasOnClickListeners()92     public boolean hasOnClickListeners() {
93         TextView value = findViewById(R.id.table_row_value);
94         return value.hasOnClickListeners();
95     }
96 
97     @Override
setOnClickListener(OnClickListener callback)98     public void setOnClickListener(OnClickListener callback) {
99         TextView clickable = ((TextView) findViewById(R.id.table_row_value));
100         mDefaultTextColor = clickable.getTextColors();
101         TypedArray ta = getContext().obtainStyledAttributes(R.styleable.TextAppearance);
102         int linkColor = ta.getColor(R.styleable.TextAppearance_android_textColorLink,
103                 mDefaultTextColor.getDefaultColor());
104         ta.recycle();
105         clickable.setTextColor(linkColor);
106         clickable.setPaintFlags(clickable.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
107         clickable.setOnClickListener(callback);
108     }
109 
removeOnClickListener()110     public void removeOnClickListener() {
111         TextView reset = ((TextView) findViewById(R.id.table_row_value));
112         if (mDefaultTextColor != null) {
113             reset.setTextColor(mDefaultTextColor);
114         }
115         reset.setPaintFlags(reset.getPaintFlags() & ~Paint.UNDERLINE_TEXT_FLAG);
116         reset.setOnClickListener(null);
117         reset.setClickable(false);
118     }
119 }
120