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.text.TextUtils; 22 import android.util.AttributeSet; 23 import android.view.Gravity; 24 import android.view.LayoutInflater; 25 import android.view.View; 26 import android.widget.ImageView; 27 import android.widget.LinearLayout; 28 import android.widget.TextView; 29 30 import androidx.annotation.ColorInt; 31 import androidx.annotation.VisibleForTesting; 32 import com.android.settingslib.widget.preference.barchart.R; 33 /** 34 * {@link View} for a single vertical bar with icon and summary. 35 */ 36 public class BarView extends LinearLayout { 37 38 private static final String TAG = "BarView"; 39 40 private View mBarView; 41 private ImageView mIcon; 42 private TextView mBarTitle; 43 private TextView mBarSummary; 44 BarView(Context context)45 public BarView(Context context) { 46 super(context); 47 init(); 48 } 49 BarView(Context context, AttributeSet attrs)50 public BarView(Context context, AttributeSet attrs) { 51 super(context, attrs); 52 init(); 53 54 // Get accent color 55 TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.colorAccent}); 56 @ColorInt final int colorAccent = a.getColor(0, 0); 57 58 // Get bar color from layout XML 59 a = context.obtainStyledAttributes(attrs, R.styleable.SettingsBarView); 60 @ColorInt final int barColor = a.getColor(R.styleable.SettingsBarView_barColor, 61 colorAccent); 62 a.recycle(); 63 64 mBarView.setBackgroundColor(barColor); 65 } 66 67 /** 68 * Updates the view with a {@link BarViewInfo}. 69 */ updateView(BarViewInfo barViewInfo)70 void updateView(BarViewInfo barViewInfo) { 71 setOnClickListener(barViewInfo.getClickListener()); 72 //Set height of bar view 73 mBarView.getLayoutParams().height = barViewInfo.getNormalizedHeight(); 74 mIcon.setImageDrawable(barViewInfo.getIcon()); 75 mBarTitle.setText(barViewInfo.getTitle()); 76 mBarSummary.setText(barViewInfo.getSummary()); 77 78 final CharSequence barViewInfoContent = barViewInfo.getContentDescription(); 79 if (!TextUtils.isEmpty(barViewInfoContent) 80 && !TextUtils.equals((barViewInfo.getTitle()), barViewInfoContent)) { 81 mIcon.setContentDescription(barViewInfo.getContentDescription()); 82 } 83 } 84 85 @VisibleForTesting getTitle()86 CharSequence getTitle() { 87 return mBarTitle.getText(); 88 } 89 90 @VisibleForTesting getSummary()91 CharSequence getSummary() { 92 return mBarSummary.getText(); 93 } 94 init()95 private void init() { 96 LayoutInflater.from(getContext()).inflate(R.layout.settings_bar_view, this); 97 setOrientation(LinearLayout.VERTICAL); 98 setGravity(Gravity.CENTER | Gravity.BOTTOM); 99 100 mBarView = findViewById(R.id.bar_view); 101 mIcon = findViewById(R.id.icon_view); 102 mBarTitle = findViewById(R.id.bar_title); 103 mBarSummary = findViewById(R.id.bar_summary); 104 } 105 setOnClickListner(View.OnClickListener listener)106 private void setOnClickListner(View.OnClickListener listener) { 107 mBarView.setOnClickListener(listener); 108 } 109 } 110