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.Resources; 21 import android.util.AttributeSet; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.view.textclassifier.TextClassifier; 26 import android.widget.LinearLayout; 27 import android.widget.TextView; 28 29 import com.android.documentsui.R; 30 import com.android.documentsui.inspector.InspectorController.TableDisplay; 31 32 import java.util.HashMap; 33 import java.util.Map; 34 35 /** 36 * Organizes and Displays the basic details about a file 37 */ 38 public class TableView extends LinearLayout implements TableDisplay { 39 40 private final LayoutInflater mInflater; 41 42 private final Map<CharSequence, KeyValueRow> mRows = new HashMap<>(); 43 private final Resources mRes; 44 private final Map<CharSequence, TextView> mTitles = new HashMap<>(); 45 private final TextClassifier mClassifier; 46 TableView(Context context)47 public TableView(Context context) { 48 this(context, null); 49 } 50 TableView(Context context, AttributeSet attrs)51 public TableView(Context context, AttributeSet attrs) { 52 this(context, attrs, 0); 53 } 54 TableView(Context context, AttributeSet attrs, int defStyleAttr)55 public TableView(Context context, AttributeSet attrs, int defStyleAttr) { 56 super(context, attrs, defStyleAttr); 57 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 58 mRes = context.getResources(); 59 mClassifier = GpsCoordinatesTextClassifier.create(context); 60 } 61 setTitle(@tringRes int title, boolean showDivider)62 void setTitle(@StringRes int title, boolean showDivider) { 63 putTitle(mContext.getResources().getString(title), showDivider); 64 } 65 66 // A naughty title method (that takes strings, not message ids), mostly for DebugView. putTitle(CharSequence title, boolean showDivider)67 protected void putTitle(CharSequence title, boolean showDivider) { 68 TextView view = mTitles.get(title); 69 if (view == null) { 70 LinearLayout layout = 71 (LinearLayout) mInflater.inflate(R.layout.inspector_section_title, null); 72 if (!showDivider) { 73 layout.setDividerDrawable(null); 74 } 75 view = (TextView) layout.findViewById(R.id.inspector_header_title); 76 addView(layout); 77 mTitles.put(title, view); 78 } 79 view.setText(title); 80 } 81 createKeyValueRow(ViewGroup parent)82 protected KeyValueRow createKeyValueRow(ViewGroup parent) { 83 KeyValueRow row = (KeyValueRow) mInflater.inflate(R.layout.table_key_value_row, null); 84 parent.addView(row); 85 row.setTextClassifier(mClassifier); 86 return row; 87 } 88 89 /** 90 * Puts or updates a value in the table view. 91 */ 92 @Override put(@tringRes int keyId, CharSequence value)93 public void put(@StringRes int keyId, CharSequence value) { 94 put(mRes.getString(keyId), value); 95 } 96 97 98 /** 99 * Puts or updates a value in the table view. 100 */ put(CharSequence key, CharSequence value)101 protected KeyValueRow put(CharSequence key, CharSequence value) { 102 KeyValueRow row = mRows.get(key); 103 104 if (row == null) { 105 row = createKeyValueRow(this); 106 row.setKey(key); 107 mRows.put(key, row); 108 } else { 109 row.removeOnClickListener(); 110 } 111 112 row.setValue(value); 113 row.setTextClassifier(mClassifier); 114 return row; 115 } 116 117 @Override put(@tringRes int keyId, CharSequence value, OnClickListener callback)118 public void put(@StringRes int keyId, CharSequence value, OnClickListener callback) { 119 put(keyId, value); 120 mRows.get(mRes.getString(keyId)).setOnClickListener(callback); 121 } 122 123 @Override isEmpty()124 public boolean isEmpty() { 125 return mRows.isEmpty(); 126 } 127 128 @Override setVisible(boolean visible)129 public void setVisible(boolean visible) { 130 setVisibility(visible ? View.VISIBLE : View.GONE); 131 } 132 } 133