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.graphics.drawable.Drawable; |
| 20 | import android.view.View; |
| 21 | |
| 22 | import androidx.annotation.IntRange; |
| 23 | import androidx.annotation.Nullable; |
| 24 | import androidx.annotation.StringRes; |
| 25 | |
| 26 | import java.util.Comparator; |
| 27 | |
| 28 | /** |
| 29 | * A class responsible for saving bar view information. |
| 30 | */ |
| 31 | public class BarViewInfo implements Comparable<BarViewInfo> { |
| 32 | |
| 33 | private final Drawable mIcon; |
tmfang | 7081e7f | 2018-12-19 14:14:34 +0800 | [diff] [blame] | 34 | private View.OnClickListener mClickListener; |
Fan Zhang | 7989dfc | 2018-12-19 12:03:53 -0800 | [diff] [blame] | 35 | @StringRes |
| 36 | private int mSummary; |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 37 | // A number indicates this bar's height. The larger number shows a higher bar view. |
Fan Zhang | 7989dfc | 2018-12-19 12:03:53 -0800 | [diff] [blame] | 38 | private int mHeight; |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 39 | // A real height of bar view. |
Fan Zhang | 7989dfc | 2018-12-19 12:03:53 -0800 | [diff] [blame] | 40 | private int mNormalizedHeight; |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Construct a BarViewInfo instance. |
| 44 | * |
Fan Zhang | 7989dfc | 2018-12-19 12:03:53 -0800 | [diff] [blame] | 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 summary The string resource id for summary. |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 48 | */ |
Fan Zhang | 7989dfc | 2018-12-19 12:03:53 -0800 | [diff] [blame] | 49 | public BarViewInfo(Drawable icon, @IntRange(from = 0) int barHeight, @StringRes int summary) { |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 50 | mIcon = icon; |
Fan Zhang | 7989dfc | 2018-12-19 12:03:53 -0800 | [diff] [blame] | 51 | mHeight = barHeight; |
| 52 | mSummary = summary; |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 53 | } |
| 54 | |
tmfang | 7081e7f | 2018-12-19 14:14:34 +0800 | [diff] [blame] | 55 | /** |
| 56 | * Set a click listener for bar view. |
| 57 | */ |
| 58 | public void setClickListener(@Nullable View.OnClickListener listener) { |
| 59 | mClickListener = listener; |
| 60 | } |
| 61 | |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 62 | @Override |
| 63 | public int compareTo(BarViewInfo other) { |
| 64 | // Descending order |
Fan Zhang | 7989dfc | 2018-12-19 12:03:53 -0800 | [diff] [blame] | 65 | return Comparator.comparingInt((BarViewInfo barViewInfo) -> barViewInfo.mHeight) |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 66 | .compare(other, this); |
| 67 | } |
| 68 | |
Fan Zhang | 7989dfc | 2018-12-19 12:03:53 -0800 | [diff] [blame] | 69 | void setHeight(@IntRange(from = 0) int height) { |
| 70 | mHeight = height; |
| 71 | } |
| 72 | |
| 73 | void setSummary(@StringRes int resId) { |
| 74 | mSummary = resId; |
| 75 | } |
| 76 | |
Fan Zhang | 7989dfc | 2018-12-19 12:03:53 -0800 | [diff] [blame] | 77 | Drawable getIcon() { |
| 78 | return mIcon; |
| 79 | } |
| 80 | |
| 81 | int getHeight() { |
| 82 | return mHeight; |
| 83 | } |
| 84 | |
| 85 | View.OnClickListener getClickListener() { |
tmfang | 7081e7f | 2018-12-19 14:14:34 +0800 | [diff] [blame] | 86 | return mClickListener; |
Fan Zhang | 7989dfc | 2018-12-19 12:03:53 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @StringRes |
| 90 | int getSummary() { |
| 91 | return mSummary; |
| 92 | } |
| 93 | |
| 94 | void setNormalizedHeight(@IntRange(from = 0) int barHeight) { |
| 95 | mNormalizedHeight = barHeight; |
| 96 | } |
| 97 | |
| 98 | int getNormalizedHeight() { |
| 99 | return mNormalizedHeight; |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 100 | } |
| 101 | } |