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 BatteryChartViewModel(@onNull List<Integer> levels, @NonNull List<Long> timestamps, @NonNull AxisLabelPosition axisLabelPosition, @NonNull LabelTextGenerator labelTextGenerator)59 BatteryChartViewModel(@NonNull List<Integer> levels, @NonNull List<Long> timestamps, 60 @NonNull AxisLabelPosition axisLabelPosition, 61 @NonNull LabelTextGenerator labelTextGenerator) { 62 Preconditions.checkArgument( 63 levels.size() == timestamps.size() && levels.size() >= MIN_LEVELS_DATA_SIZE, 64 String.format(Locale.ENGLISH, 65 "Invalid BatteryChartViewModel levels.size: %d, timestamps.size: %d.", 66 levels.size(), timestamps.size())); 67 mLevels = levels; 68 mTimestamps = timestamps; 69 mAxisLabelPosition = axisLabelPosition; 70 mLabelTextGenerator = labelTextGenerator; 71 mTexts = new String[size()]; 72 mFullTexts = new String[size()]; 73 } 74 size()75 public int size() { 76 return mLevels.size(); 77 } 78 getLevel(int index)79 public Integer getLevel(int index) { 80 return mLevels.get(index); 81 } 82 getText(int index)83 public String getText(int index) { 84 if (mTexts[index] == null) { 85 mTexts[index] = mLabelTextGenerator.generateText(mTimestamps, index); 86 } 87 return mTexts[index]; 88 } 89 getFullText(int index)90 public String getFullText(int index) { 91 if (mFullTexts[index] == null) { 92 mFullTexts[index] = mLabelTextGenerator.generateFullText(mTimestamps, index); 93 } 94 return mFullTexts[index]; 95 } 96 axisLabelPosition()97 public AxisLabelPosition axisLabelPosition() { 98 return mAxisLabelPosition; 99 } 100 selectedIndex()101 public int selectedIndex() { 102 return mSelectedIndex; 103 } 104 setSelectedIndex(int index)105 public void setSelectedIndex(int index) { 106 mSelectedIndex = index; 107 } 108 109 @Override hashCode()110 public int hashCode() { 111 return Objects.hash(mLevels, mTimestamps, mSelectedIndex, mAxisLabelPosition); 112 } 113 114 @Override equals(Object other)115 public boolean equals(Object other) { 116 if (this == other) { 117 return true; 118 } else if (!(other instanceof BatteryChartViewModel)) { 119 return false; 120 } 121 final BatteryChartViewModel batteryChartViewModel = (BatteryChartViewModel) other; 122 return Objects.equals(mLevels, batteryChartViewModel.mLevels) 123 && Objects.equals(mTimestamps, batteryChartViewModel.mTimestamps) 124 && mAxisLabelPosition == batteryChartViewModel.mAxisLabelPosition 125 && mSelectedIndex == batteryChartViewModel.mSelectedIndex; 126 } 127 128 @Override toString()129 public String toString() { 130 // Generate all the texts and full texts. 131 for (int i = 0; i < size(); i++) { 132 getText(i); 133 getFullText(i); 134 } 135 136 return new StringBuilder() 137 .append("levels: " + Objects.toString(mLevels)) 138 .append(", timestamps: " + Objects.toString(mTimestamps)) 139 .append(", texts: " + Arrays.toString(mTexts)) 140 .append(", fullTexts: " + Arrays.toString(mFullTexts)) 141 .append(", axisLabelPosition: " + mAxisLabelPosition) 142 .append(", selectedIndex: " + mSelectedIndex) 143 .toString(); 144 } 145 } 146