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.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.VisibleForTesting; 23 24 import com.android.internal.logging.nano.MetricsProto; 25 import com.android.settings.R; 26 import com.android.settings.fuelgauge.batterytip.AppInfo; 27 28 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 29 30 import java.util.List; 31 32 /** 33 * Tip to show general summary about battery life 34 */ 35 public class HighUsageTip extends BatteryTip { 36 37 private final long mLastFullChargeTimeMs; 38 @VisibleForTesting 39 final List<AppInfo> mHighUsageAppList; 40 41 public HighUsageTip(long lastFullChargeTimeMs, List<AppInfo> appList) { 42 super(TipType.HIGH_DEVICE_USAGE, appList.isEmpty() ? StateType.INVISIBLE : StateType.NEW, 43 true /* showDialog */); 44 mLastFullChargeTimeMs = lastFullChargeTimeMs; 45 mHighUsageAppList = appList; 46 } 47 48 @VisibleForTesting 49 HighUsageTip(Parcel in) { 50 super(in); 51 mLastFullChargeTimeMs = in.readLong(); 52 mHighUsageAppList = in.createTypedArrayList(AppInfo.CREATOR); 53 } 54 55 @Override 56 public void writeToParcel(Parcel dest, int flags) { 57 super.writeToParcel(dest, flags); 58 dest.writeLong(mLastFullChargeTimeMs); 59 dest.writeTypedList(mHighUsageAppList); 60 } 61 62 @Override 63 public CharSequence getTitle(Context context) { 64 return context.getString(R.string.battery_tip_high_usage_title); 65 } 66 67 @Override 68 public CharSequence getSummary(Context context) { 69 return context.getString(R.string.battery_tip_high_usage_summary); 70 } 71 72 @Override 73 public int getIconId() { 74 return R.drawable.ic_perm_device_information_red_24dp; 75 } 76 77 @Override 78 public void updateState(BatteryTip tip) { 79 mState = tip.mState; 80 } 81 82 @Override 83 public void log(Context context, MetricsFeatureProvider metricsFeatureProvider) { 84 metricsFeatureProvider.action(context, MetricsProto.MetricsEvent.ACTION_HIGH_USAGE_TIP, 85 mState); 86 for (int i = 0, size = mHighUsageAppList.size(); i < size; i++) { 87 final AppInfo appInfo = mHighUsageAppList.get(i); 88 metricsFeatureProvider.action(context, 89 MetricsProto.MetricsEvent.ACTION_HIGH_USAGE_TIP_LIST, 90 appInfo.packageName); 91 } 92 } 93 94 public long getLastFullChargeTimeMs() { 95 return mLastFullChargeTimeMs; 96 } 97 98 public List<AppInfo> getHighUsageAppList() { 99 return mHighUsageAppList; 100 } 101 102 @Override 103 public String toString() { 104 final StringBuilder stringBuilder = new StringBuilder(super.toString()); 105 stringBuilder.append(" {"); 106 for (int i = 0, size = mHighUsageAppList.size(); i < size; i++) { 107 final AppInfo appInfo = mHighUsageAppList.get(i); 108 stringBuilder.append(" " + appInfo.toString() + " "); 109 } 110 stringBuilder.append('}'); 111 112 return stringBuilder.toString(); 113 } 114 115 public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 116 public BatteryTip createFromParcel(Parcel in) { 117 return new HighUsageTip(in); 118 } 119 120 public BatteryTip[] newArray(int size) { 121 return new HighUsageTip[size]; 122 } 123 }; 124 125 } 126