1 /* 2 * Copyright (C) 2017 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.batterytip.tips; 18 19 import android.content.Context; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 import android.support.annotation.IdRes; 23 import android.support.annotation.IntDef; 24 import android.support.annotation.VisibleForTesting; 25 import android.support.v7.preference.Preference; 26 import android.util.SparseIntArray; 27 28 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 29 30 import java.lang.annotation.Retention; 31 import java.lang.annotation.RetentionPolicy; 32 33 /** 34 * Base model for a battery tip(e.g. suggest user to turn on battery saver) 35 * 36 * Each {@link BatteryTip} contains basic data(e.g. title, summary, icon) as well as the 37 * pre-defined action(e.g. turn on battery saver) 38 */ 39 public abstract class BatteryTip implements Comparable<BatteryTip>, Parcelable { 40 @Retention(RetentionPolicy.SOURCE) 41 @IntDef({StateType.NEW, 42 StateType.HANDLED, 43 StateType.INVISIBLE}) 44 public @interface StateType { 45 int NEW = 0; 46 int HANDLED = 1; 47 int INVISIBLE = 2; 48 } 49 50 @Retention(RetentionPolicy.SOURCE) 51 @IntDef({TipType.SUMMARY, 52 TipType.BATTERY_SAVER, 53 TipType.HIGH_DEVICE_USAGE, 54 TipType.SMART_BATTERY_MANAGER, 55 TipType.APP_RESTRICTION, 56 TipType.REDUCED_BATTERY, 57 TipType.LOW_BATTERY, 58 TipType.REMOVE_APP_RESTRICTION}) 59 public @interface TipType { 60 int SMART_BATTERY_MANAGER = 0; 61 int APP_RESTRICTION = 1; 62 int HIGH_DEVICE_USAGE = 2; 63 int BATTERY_SAVER = 3; 64 int REDUCED_BATTERY = 4; 65 int LOW_BATTERY = 5; 66 int SUMMARY = 6; 67 int REMOVE_APP_RESTRICTION = 7; 68 } 69 70 @VisibleForTesting 71 static final SparseIntArray TIP_ORDER; 72 static { 73 TIP_ORDER = new SparseIntArray(); 74 TIP_ORDER.append(TipType.APP_RESTRICTION, 0); 75 TIP_ORDER.append(TipType.BATTERY_SAVER, 1); 76 TIP_ORDER.append(TipType.HIGH_DEVICE_USAGE, 2); 77 TIP_ORDER.append(TipType.LOW_BATTERY, 3); 78 TIP_ORDER.append(TipType.SUMMARY, 4); 79 TIP_ORDER.append(TipType.SMART_BATTERY_MANAGER, 5); 80 TIP_ORDER.append(TipType.REDUCED_BATTERY, 6); 81 TIP_ORDER.append(TipType.REMOVE_APP_RESTRICTION, 7); 82 } 83 84 private static final String KEY_PREFIX = "key_battery_tip"; 85 86 protected int mType; 87 protected int mState; 88 protected boolean mShowDialog; 89 /** 90 * Whether we need to update battery tip when configuration change 91 */ 92 protected boolean mNeedUpdate; 93 94 BatteryTip(Parcel in) { 95 mType = in.readInt(); 96 mState = in.readInt(); 97 mShowDialog = in.readBoolean(); 98 mNeedUpdate = in.readBoolean(); 99 } 100 101 BatteryTip(int type, int state, boolean showDialog) { 102 mType = type; 103 mState = state; 104 mShowDialog = showDialog; 105 mNeedUpdate = true; 106 } 107 108 @Override 109 public int describeContents() { 110 return 0; 111 } 112 113 @Override 114 public void writeToParcel(Parcel dest, int flags) { 115 dest.writeInt(mType); 116 dest.writeInt(mState); 117 dest.writeBoolean(mShowDialog); 118 dest.writeBoolean(mNeedUpdate); 119 } 120 121 public abstract CharSequence getTitle(Context context); 122 123 public abstract CharSequence getSummary(Context context); 124 125 @IdRes 126 public abstract int getIconId(); 127 128 /** 129 * Update the current {@link #mState} using the new {@code tip}. 130 * 131 * @param tip used to update 132 */ 133 public abstract void updateState(BatteryTip tip); 134 135 /** 136 * Log the battery tip 137 */ 138 public abstract void log(Context context, MetricsFeatureProvider metricsFeatureProvider); 139 140 public Preference buildPreference(Context context) { 141 Preference preference = new Preference(context); 142 143 preference.setKey(getKey()); 144 preference.setTitle(getTitle(context)); 145 preference.setSummary(getSummary(context)); 146 preference.setIcon(getIconId()); 147 return preference; 148 } 149 150 public boolean shouldShowDialog() { 151 return mShowDialog; 152 } 153 154 public boolean needUpdate() { 155 return mNeedUpdate; 156 } 157 158 public String getKey() { 159 return KEY_PREFIX + mType; 160 } 161 162 public int getType() { 163 return mType; 164 } 165 166 @StateType 167 public int getState() { 168 return mState; 169 } 170 171 public boolean isVisible() { 172 return mState != StateType.INVISIBLE; 173 } 174 175 @Override 176 public int compareTo(BatteryTip o) { 177 return TIP_ORDER.get(mType) - TIP_ORDER.get(o.mType); 178 } 179 180 @Override 181 public String toString() { 182 return "type=" + mType + " state=" + mState; 183 } 184 } 185