1 /* 2 * Copyright (C) 2022 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.settings.fuelgauge.batteryusage; 18 19 import androidx.annotation.NonNull; 20 import androidx.core.util.Preconditions; 21 22 import java.util.Arrays; 23 import java.util.List; 24 import java.util.Locale; 25 import java.util.Objects; 26 27 /** The view model of {@code BatteryChartView} */ 28 class BatteryChartViewModel { 29 private static final String TAG = "BatteryChartViewModel"; 30 31 public static final int SELECTED_INDEX_ALL = -1; 32 public static final int SELECTED_INDEX_INVALID = -2; 33 34 // We need at least 2 levels to draw a trapezoid. 35 private static final int MIN_LEVELS_DATA_SIZE = 2; 36 37 enum AxisLabelPosition { 38 BETWEEN_TRAPEZOIDS, 39 CENTER_OF_TRAPEZOIDS, 40 } 41 42 interface LabelTextGenerator { 43 /** Generate the label text. The text may be abbreviated to save space. */ generateText(List<Long> timestamps, int index)44 String generateText(List<Long> timestamps, int index); 45 46 /** Generate the full text for accessibility. */ generateFullText(List<Long> timestamps, int index)47 String generateFullText(List<Long> timestamps, int index); 48 } 49 50 private final List<Integer> mLevels; 51 private final List<Long> mTimestamps; 52 private final AxisLabelPosition mAxisLabelPosition; 53 private final LabelTextGenerator mLabelTextGenerator; 54 private final String[] mTexts; 55 private final String[] mFullTexts; 56 57 private int mSelectedIndex = SELECTED_INDEX_ALL; 58 private int mHighlightSlotIndex = SELECTED_INDEX_INVALID; 59 BatteryChartViewModel(@onNull List<Integer> levels, @NonNull List<Long> timestamps, @NonNull AxisLabelPosition axisLabelPosition, @NonNull LabelTextGenerator labelTextGenerator)60 BatteryChartViewModel(@NonNull List<Integer> levels, @NonNull List<Long> timestamps, 61 @NonNull AxisLabelPosition axisLabelPosition, 62 @NonNull LabelTextGenerator labelTextGenerator) { 63 Preconditions.checkArgument( 64 levels.size() == timestamps.size() && levels.size() >= MIN_LEVELS_DATA_SIZE, 65 String.format(Locale.ENGLISH, 66 "Invalid BatteryChartViewModel levels.size: %d, timestamps.size: %d.", 67 levels.size(), timestamps.size())); 68 mLevels = levels; 69 mTimestamps = timestamps; 70 mAxisLabelPosition = axisLabelPosition; 71 mLabelTextGenerator = labelTextGenerator; 72 mTexts = new String[size()]; 73 mFullTexts = new String[size()]; 74 } 75 size()76 public int size() { 77 return mLevels.size(); 78 } 79 getLevel(int index)80 public Integer getLevel(int index) { 81 return mLevels.get(index); 82 } 83 getText(int index)84 public String getText(int index) { 85 if (mTexts[index] == null) { 86 mTexts[index] = mLabelTextGenerator.generateText(mTimestamps, index); 87 } 88 return mTexts[index]; 89 } 90 getFullText(int index)91 public String getFullText(int index) { 92 if (mFullTexts[index] == null) { 93 mFullTexts[index] = mLabelTextGenerator.generateFullText(mTimestamps, index); 94 } 95 return mFullTexts[index]; 96 } 97 axisLabelPosition()98 public AxisLabelPosition axisLabelPosition() { 99 return mAxisLabelPosition; 100 } 101 selectedIndex()102 public int selectedIndex() { 103 return mSelectedIndex; 104 } 105 setSelectedIndex(int index)106 public void setSelectedIndex(int index) { 107 mSelectedIndex = index; 108 } 109 getHighlightSlotIndex()110 public int getHighlightSlotIndex() { 111 return mHighlightSlotIndex; 112 } 113 setHighlightSlotIndex(int index)114 public void setHighlightSlotIndex(int index) { 115 mHighlightSlotIndex = index; 116 } 117 118 @Override hashCode()119 public int hashCode() { 120 return Objects.hash(mLevels, mTimestamps, mSelectedIndex, mAxisLabelPosition); 121 } 122 123 @Override equals(Object other)124 public boolean equals(Object other) { 125 if (this == other) { 126 return true; 127 } else if (!(other instanceof BatteryChartViewModel)) { 128 return false; 129 } 130 final BatteryChartViewModel batteryChartViewModel = (BatteryChartViewModel) other; 131 return Objects.equals(mLevels, batteryChartViewModel.mLevels) 132 && Objects.equals(mTimestamps, batteryChartViewModel.mTimestamps) 133 && mAxisLabelPosition == batteryChartViewModel.mAxisLabelPosition 134 && mSelectedIndex == batteryChartViewModel.mSelectedIndex; 135 } 136 137 @Override toString()138 public String toString() { 139 // Generate all the texts and full texts. 140 for (int i = 0; i < size(); i++) { 141 getText(i); 142 getFullText(i); 143 } 144 145 return new StringBuilder() 146 .append("levels: " + Objects.toString(mLevels)) 147 .append(", timestamps: " + Objects.toString(mTimestamps)) 148 .append(", texts: " + Arrays.toString(mTexts)) 149 .append(", fullTexts: " + Arrays.toString(mFullTexts)) 150 .append(", axisLabelPosition: " + mAxisLabelPosition) 151 .append(", selectedIndex: " + mSelectedIndex) 152 .toString(); 153 } 154 } 155