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; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 import android.support.annotation.VisibleForTesting; 22 import android.text.TextUtils; 23 import android.util.ArraySet; 24 25 import com.android.settings.fuelgauge.anomaly.Anomaly; 26 27 import java.util.Objects; 28 29 /** 30 * Model class stores app info(e.g. package name, type..) that used in battery tip 31 */ 32 public class AppInfo implements Comparable<AppInfo>, Parcelable { 33 public final String packageName; 34 /** 35 * Anomaly type of the app 36 * @see Anomaly.AnomalyType 37 */ 38 public final ArraySet<Integer> anomalyTypes; 39 public final long screenOnTimeMs; 40 public final int uid; 41 42 private AppInfo(AppInfo.Builder builder) { 43 packageName = builder.mPackageName; 44 anomalyTypes = builder.mAnomalyTypes; 45 screenOnTimeMs = builder.mScreenOnTimeMs; 46 uid = builder.mUid; 47 } 48 49 @VisibleForTesting 50 AppInfo(Parcel in) { 51 packageName = in.readString(); 52 anomalyTypes = (ArraySet<Integer>) in.readArraySet(null /* loader */); 53 screenOnTimeMs = in.readLong(); 54 uid = in.readInt(); 55 } 56 57 @Override 58 public int compareTo(AppInfo o) { 59 return Long.compare(screenOnTimeMs, o.screenOnTimeMs); 60 } 61 62 @Override 63 public int describeContents() { 64 return 0; 65 } 66 67 @Override 68 public void writeToParcel(Parcel dest, int flags) { 69 dest.writeString(packageName); 70 dest.writeArraySet(anomalyTypes); 71 dest.writeLong(screenOnTimeMs); 72 dest.writeInt(uid); 73 } 74 75 @Override 76 public String toString() { 77 return "packageName=" + packageName + ",anomalyTypes=" + anomalyTypes + ",screenTime=" 78 + screenOnTimeMs; 79 } 80 81 @Override 82 public boolean equals(Object obj) { 83 if (this == obj) { 84 return true; 85 } 86 if (!(obj instanceof AppInfo)) { 87 return false; 88 } 89 90 AppInfo other = (AppInfo) obj; 91 return Objects.equals(anomalyTypes, other.anomalyTypes) 92 && uid == other.uid 93 && screenOnTimeMs == other.screenOnTimeMs 94 && TextUtils.equals(packageName, other.packageName); 95 } 96 97 public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 98 public AppInfo createFromParcel(Parcel in) { 99 return new AppInfo(in); 100 } 101 102 public AppInfo[] newArray(int size) { 103 return new AppInfo[size]; 104 } 105 }; 106 107 public static final class Builder { 108 private ArraySet<Integer> mAnomalyTypes = new ArraySet<>(); 109 private String mPackageName; 110 private long mScreenOnTimeMs; 111 private int mUid; 112 113 public Builder addAnomalyType(int type) { 114 mAnomalyTypes.add(type); 115 return this; 116 } 117 118 public Builder setPackageName(String packageName) { 119 mPackageName = packageName; 120 return this; 121 } 122 123 public Builder setScreenOnTimeMs(long screenOnTimeMs) { 124 mScreenOnTimeMs = screenOnTimeMs; 125 return this; 126 } 127 128 public Builder setUid(int uid) { 129 mUid = uid; 130 return this; 131 } 132 133 public AppInfo build() { 134 return new AppInfo(this); 135 } 136 } 137 }