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