1 /*
2  * Copyright (C) 2019 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.view.View;
20 
21 import androidx.annotation.NonNull;
22 import androidx.annotation.Nullable;
23 import androidx.annotation.StringRes;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 /**
29  * BarChartInfo is responsible for storing information about {@link BarChartPreference}.
30  */
31 public class BarChartInfo {
32     @StringRes
33     private final int mTitle;
34     @StringRes
35     private final int mDetails;
36     @StringRes
37     private final int mEmptyText;
38     private final View.OnClickListener mDetailsOnClickListener;
39 
40     private BarViewInfo[] mBarViewInfos;
41 
42     /**
43      * Gets the resource id for the title shown in {@link BarChartPreference}.
44      *
45      * @return the string resource id for title.
46      */
getTitle()47     public int getTitle() {
48         return mTitle;
49     }
50 
51     /**
52      * Gets the resource id for the details shown in {@link BarChartPreference}.
53      *
54      * @return the string resource id for details.
55      */
getDetails()56     public int getDetails() {
57         return mDetails;
58     }
59 
60     /**
61      * Gets the resource id for the empty text shown in {@link BarChartPreference} when there is no
62      * any bar view in {@link BarChartPreference}.
63      *
64      * @return the string resource id for empty text.
65      */
getEmptyText()66     public int getEmptyText() {
67         return mEmptyText;
68     }
69 
70     /**
71      * Gets the click listener for the details view.
72      *
73      * @return click listener for details view.
74      */
getDetailsOnClickListener()75     public View.OnClickListener getDetailsOnClickListener() {
76         return mDetailsOnClickListener;
77     }
78 
79     /**
80      * Gets an array which contains up to four {@link BarViewInfo}
81      *
82      * @return an array holding the current all {@link BarViewInfo} state of the bar chart.
83      */
getBarViewInfos()84     public BarViewInfo[] getBarViewInfos() {
85         return mBarViewInfos;
86     }
87 
setBarViewInfos(BarViewInfo[] barViewInfos)88     void setBarViewInfos(BarViewInfo[] barViewInfos) {
89         mBarViewInfos = barViewInfos;
90     }
91 
BarChartInfo(Builder builder)92     private BarChartInfo(Builder builder) {
93         mTitle = builder.mTitle;
94         mDetails = builder.mDetails;
95         mEmptyText = builder.mEmptyText;
96         mDetailsOnClickListener = builder.mDetailsOnClickListener;
97 
98         if (builder.mBarViewInfos != null) {
99             mBarViewInfos = builder.mBarViewInfos.stream().toArray(BarViewInfo[]::new);
100         }
101     }
102 
103     /**
104      * Builder class for {@link BarChartInfo}
105      */
106     public static class Builder {
107         @StringRes
108         private int mTitle;
109         @StringRes
110         private int mDetails;
111         @StringRes
112         private int mEmptyText;
113         private View.OnClickListener mDetailsOnClickListener;
114         private List<BarViewInfo> mBarViewInfos;
115 
116         /**
117          * Creates an instance of a {@link BarChartInfo} based on the current builder settings.
118          *
119          * @return The {@link BarChartInfo}.
120          */
build()121         public BarChartInfo build() {
122             if (mTitle == 0) {
123                 throw new IllegalStateException("You must call Builder#setTitle() once.");
124             }
125             return new BarChartInfo(this);
126         }
127 
128         /**
129          * Sets the string resource id for the title.
130          */
setTitle(@tringRes int title)131         public Builder setTitle(@StringRes int title) {
132             mTitle = title;
133             return this;
134         }
135 
136         /**
137          * Sets the string resource id for the details.
138          */
setDetails(@tringRes int details)139         public Builder setDetails(@StringRes int details) {
140             mDetails = details;
141             return this;
142         }
143 
144         /**
145          * Sets the string resource id for the empty text.
146          */
setEmptyText(@tringRes int emptyText)147         public Builder setEmptyText(@StringRes int emptyText) {
148             mEmptyText = emptyText;
149             return this;
150         }
151 
152         /**
153          * Sets the click listener for details view.
154          */
setDetailsOnClickListener( @ullable View.OnClickListener clickListener)155         public Builder setDetailsOnClickListener(
156                 @Nullable View.OnClickListener clickListener) {
157             mDetailsOnClickListener = clickListener;
158             return this;
159         }
160 
161         /**
162          * Adds a {@link BarViewInfo} for {@link BarChartPreference}.
163          * Maximum of 4 {@link BarViewInfo} can be added.
164          */
addBarViewInfo(@onNull BarViewInfo barViewInfo)165         public Builder addBarViewInfo(@NonNull BarViewInfo barViewInfo) {
166             if (mBarViewInfos == null) {
167                 mBarViewInfos = new ArrayList<>();
168             }
169             if (mBarViewInfos.size() >= BarChartPreference.MAXIMUM_BAR_VIEWS) {
170                 throw new IllegalStateException("We only support up to four bar views");
171             }
172             mBarViewInfos.add(barViewInfo);
173             return this;
174         }
175     }
176 }
177