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.graphics.drawable.Drawable;
20 import android.view.View;
21 
22 import androidx.annotation.IntRange;
23 import androidx.annotation.Nullable;
24 
25 import java.util.Comparator;
26 import com.android.settingslib.widget.preference.barchart.R;
27 /**
28  * A class responsible for saving bar view information.
29  */
30 public class BarViewInfo implements Comparable<BarViewInfo> {
31 
32     private final Drawable mIcon;
33     private View.OnClickListener mClickListener;
34     private CharSequence mTitle;
35     private CharSequence mSummary;
36     private @Nullable CharSequence mContentDescription;
37     // A number indicates this bar's height. The larger number shows a higher bar view.
38     private int mHeight;
39     // A real height of bar view.
40     private int mNormalizedHeight;
41 
42     /**
43      * Construct a BarViewInfo instance.
44      *
45      * @param icon      The icon of bar view.
46      * @param barHeight The height of bar view. Larger number shows a higher bar view.
47      * @param title     The string for title.  If this is null, use the height of the bar.
48      * @param summary   The string for summary.
49      * @param contentDescription Optional text that briefly describes the contents of the icon.
50      */
BarViewInfo(Drawable icon, @IntRange(from = 0) int barHeight, @Nullable CharSequence title, CharSequence summary, @Nullable CharSequence contentDescription)51     public BarViewInfo(Drawable icon, @IntRange(from = 0) int barHeight,
52             @Nullable CharSequence title, CharSequence summary,
53             @Nullable CharSequence contentDescription) {
54         mIcon = icon;
55         mHeight = barHeight;
56         mTitle = title;
57         mSummary = summary;
58         mContentDescription = contentDescription;
59     }
60 
61     /**
62      * Set a click listener for bar view.
63      */
setClickListener(@ullable View.OnClickListener listener)64     public void setClickListener(@Nullable View.OnClickListener listener) {
65         mClickListener = listener;
66     }
67 
68     @Override
compareTo(BarViewInfo other)69     public int compareTo(BarViewInfo other) {
70         // Descending order
71         return Comparator.comparingInt((BarViewInfo barViewInfo) -> barViewInfo.mHeight)
72                 .compare(other, this);
73     }
74 
setHeight(@ntRangefrom = 0) int height)75     void setHeight(@IntRange(from = 0) int height) {
76         mHeight = height;
77     }
78 
setTitle(CharSequence title)79     void setTitle(CharSequence title) {
80         mTitle = title;
81     }
82 
setSummary(CharSequence summary)83     void setSummary(CharSequence summary) {
84         mSummary = summary;
85     }
86 
getIcon()87     Drawable getIcon() {
88         return mIcon;
89     }
90 
getHeight()91     int getHeight() {
92         return mHeight;
93     }
94 
getClickListener()95     View.OnClickListener getClickListener() {
96         return mClickListener;
97     }
98 
99     @Nullable
getTitle()100     CharSequence getTitle() {
101         return mTitle;
102     }
103 
getSummary()104     CharSequence getSummary() {
105         return mSummary;
106     }
107 
getContentDescription()108     public @Nullable CharSequence getContentDescription() {
109         return mContentDescription;
110     }
111 
setNormalizedHeight(@ntRangefrom = 0) int barHeight)112     void setNormalizedHeight(@IntRange(from = 0) int barHeight) {
113         mNormalizedHeight = barHeight;
114     }
115 
getNormalizedHeight()116     int getNormalizedHeight() {
117         return mNormalizedHeight;
118     }
119 }
120