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 */
16package com.android.documentsui.inspector;
17
18import android.annotation.StringRes;
19import android.content.Context;
20import android.content.res.Resources;
21import android.util.AttributeSet;
22import android.view.LayoutInflater;
23import android.view.ViewGroup;
24import android.widget.LinearLayout;
25import android.widget.TextView;
26
27import com.android.documentsui.R;
28
29import java.util.HashMap;
30import java.util.Map;
31
32import javax.annotation.Nullable;
33
34
35/**
36 * Organizes and Displays the basic details about a file
37 */
38public class TableView extends LinearLayout {
39
40    private final LayoutInflater mInflater;
41
42    private final Map<String, KeyValueRow> mRows = new HashMap<>();
43    private final Resources mRes;
44    private @Nullable TextView mTitle;
45
46    public TableView(Context context) {
47        this(context, null);
48    }
49
50    public TableView(Context context, AttributeSet attrs) {
51        this(context, attrs, 0);
52    }
53
54    public TableView(Context context, AttributeSet attrs, int defStyleAttr) {
55        super(context, attrs, defStyleAttr);
56        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
57        mRes = context.getResources();
58    }
59
60    protected void setTitle(ViewGroup parent, @StringRes int title) {
61        if (mTitle == null) {
62            mTitle = (TextView) mInflater.inflate(R.layout.inspector_section_title, null);
63            parent.addView(mTitle);
64        }
65        mTitle.setText(mContext.getResources().getString(title));
66    }
67
68    protected KeyValueRow createKeyValueRow(ViewGroup parent) {
69        KeyValueRow row = (KeyValueRow) mInflater.inflate(R.layout.table_key_value_row, null);
70        parent.addView(row);
71        return row;
72    }
73
74    /**
75     * Puts or updates an value in the table view.
76     */
77    protected void put(@StringRes int keyId, String value) {
78        put(mRes.getString(keyId), value);
79    }
80
81    /**
82     * Puts or updates an value in the table view.
83     */
84    protected void put(String key, String value) {
85        if(mRows.containsKey(key)) {
86            mRows.get(key).setValue(value);
87        } else {
88            KeyValueRow row = createKeyValueRow(this);
89            row.setKey(key);
90            row.setValue(value);
91            mRows.put(key, row);
92        }
93    }
94}
95