tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settingslib.widget; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.TypedArray; |
| 21 | import android.util.AttributeSet; |
| 22 | import android.view.Gravity; |
| 23 | import android.view.LayoutInflater; |
| 24 | import android.view.View; |
| 25 | import android.widget.ImageView; |
| 26 | import android.widget.LinearLayout; |
| 27 | import android.widget.TextView; |
| 28 | |
| 29 | import androidx.annotation.ColorInt; |
| 30 | import androidx.annotation.VisibleForTesting; |
| 31 | |
| 32 | /** |
Fan Zhang | 7989dfc | 2018-12-19 12:03:53 -0800 | [diff] [blame] | 33 | * {@link View} for a single vertical bar with icon and summary. |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 34 | */ |
| 35 | public 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 | |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 44 | public BarView(Context context) { |
| 45 | super(context); |
| 46 | init(); |
| 47 | } |
| 48 | |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 49 | 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 Zhang | 7989dfc | 2018-12-19 12:03:53 -0800 | [diff] [blame] | 67 | * Updates the view with a {@link BarViewInfo}. |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 68 | */ |
Fan Zhang | 7989dfc | 2018-12-19 12:03:53 -0800 | [diff] [blame] | 69 | void updateView(BarViewInfo barViewInfo) { |
tmfang | 7081e7f | 2018-12-19 14:14:34 +0800 | [diff] [blame] | 70 | setOnClickListener(barViewInfo.getClickListener()); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 71 | //Set height of bar view |
Fan Zhang | 7989dfc | 2018-12-19 12:03:53 -0800 | [diff] [blame] | 72 | mBarView.getLayoutParams().height = barViewInfo.getNormalizedHeight(); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 73 | mIcon.setImageDrawable(barViewInfo.getIcon()); |
| 74 | // For now, we use the bar number as title. |
Fan Zhang | 7989dfc | 2018-12-19 12:03:53 -0800 | [diff] [blame] | 75 | mBarTitle.setText(Integer.toString(barViewInfo.getHeight())); |
| 76 | mBarSummary.setText(barViewInfo.getSummary()); |
Joel Galenson | 9842425f | 2019-03-05 14:14:14 -0800 | [diff] [blame] | 77 | mIcon.setContentDescription(barViewInfo.getContentDescription()); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 78 | } |
| 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); |
tmfang | b052cfa | 2019-02-23 15:08:04 +0800 | [diff] [blame] | 93 | setGravity(Gravity.CENTER | Gravity.BOTTOM); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 94 | |
| 95 | mBarView = findViewById(R.id.bar_view); |
tmfang | 7081e7f | 2018-12-19 14:14:34 +0800 | [diff] [blame] | 96 | mIcon = findViewById(R.id.icon_view); |
| 97 | mBarTitle = findViewById(R.id.bar_title); |
| 98 | mBarSummary = findViewById(R.id.bar_summary); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | private void setOnClickListner(View.OnClickListener listener) { |
| 102 | mBarView.setOnClickListener(listener); |
| 103 | } |
| 104 | } |