blob: 03dfd3ee60800a1616148e081b294ea71bb418d3 [file] [log] [blame]
tmfang2badb3e2018-12-10 00:25:02 +08001/*
2 * Copyright (C) 2018 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
17package com.android.settingslib.widget;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.util.AttributeSet;
22import android.view.Gravity;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.widget.ImageView;
26import android.widget.LinearLayout;
27import android.widget.TextView;
28
29import androidx.annotation.ColorInt;
30import androidx.annotation.VisibleForTesting;
31
32/**
Fan Zhang7989dfc2018-12-19 12:03:53 -080033 * {@link View} for a single vertical bar with icon and summary.
tmfang2badb3e2018-12-10 00:25:02 +080034 */
35public class BarView extends LinearLayout {
36
37 private static final String TAG = "BarView";
38
39 private View mBarView;
40 private ImageView mIcon;
41 private TextView mBarTitle;
42 private TextView mBarSummary;
43
tmfang2badb3e2018-12-10 00:25:02 +080044 public BarView(Context context) {
45 super(context);
46 init();
47 }
48
tmfang2badb3e2018-12-10 00:25:02 +080049 public BarView(Context context, AttributeSet attrs) {
50 super(context, attrs);
51 init();
52
53 // Get accent color
54 TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.colorAccent});
55 @ColorInt final int colorAccent = a.getColor(0, 0);
56
57 // Get bar color from layout XML
58 a = context.obtainStyledAttributes(attrs, R.styleable.SettingsBarView);
59 @ColorInt final int barColor = a.getColor(R.styleable.SettingsBarView_barColor,
60 colorAccent);
61 a.recycle();
62
63 mBarView.setBackgroundColor(barColor);
64 }
65
66 /**
Fan Zhang7989dfc2018-12-19 12:03:53 -080067 * Updates the view with a {@link BarViewInfo}.
tmfang2badb3e2018-12-10 00:25:02 +080068 */
Fan Zhang7989dfc2018-12-19 12:03:53 -080069 void updateView(BarViewInfo barViewInfo) {
tmfang7081e7f2018-12-19 14:14:34 +080070 setOnClickListener(barViewInfo.getClickListener());
tmfang2badb3e2018-12-10 00:25:02 +080071 //Set height of bar view
Fan Zhang7989dfc2018-12-19 12:03:53 -080072 mBarView.getLayoutParams().height = barViewInfo.getNormalizedHeight();
tmfang2badb3e2018-12-10 00:25:02 +080073 mIcon.setImageDrawable(barViewInfo.getIcon());
74 // For now, we use the bar number as title.
Fan Zhang7989dfc2018-12-19 12:03:53 -080075 mBarTitle.setText(Integer.toString(barViewInfo.getHeight()));
76 mBarSummary.setText(barViewInfo.getSummary());
Joel Galenson9842425f2019-03-05 14:14:14 -080077 mIcon.setContentDescription(barViewInfo.getContentDescription());
tmfang2badb3e2018-12-10 00:25:02 +080078 }
79
80 @VisibleForTesting
81 CharSequence getTitle() {
82 return mBarTitle.getText();
83 }
84
85 @VisibleForTesting
86 CharSequence getSummary() {
87 return mBarSummary.getText();
88 }
89
90 private void init() {
91 LayoutInflater.from(getContext()).inflate(R.layout.settings_bar_view, this);
92 setOrientation(LinearLayout.VERTICAL);
tmfangb052cfa2019-02-23 15:08:04 +080093 setGravity(Gravity.CENTER | Gravity.BOTTOM);
tmfang2badb3e2018-12-10 00:25:02 +080094
95 mBarView = findViewById(R.id.bar_view);
tmfang7081e7f2018-12-19 14:14:34 +080096 mIcon = findViewById(R.id.icon_view);
97 mBarTitle = findViewById(R.id.bar_title);
98 mBarSummary = findViewById(R.id.bar_summary);
tmfang2badb3e2018-12-10 00:25:02 +080099 }
100
101 private void setOnClickListner(View.OnClickListener listener) {
102 mBarView.setOnClickListener(listener);
103 }
104}