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.text.TextUtils;
22 import android.util.ArraySet;
23 
24 import androidx.annotation.VisibleForTesting;
25 
26 import java.util.Objects;
27 
28 /** Model class stores app info(e.g. package name, type..) that used in battery tip */
29 public class AppInfo implements Comparable<AppInfo>, Parcelable {
30     public final String packageName;
31 
32     /**
33      * Anomaly type of the app
34      *
35      * @see StatsManagerConfig.AnomalyType
36      */
37     public final ArraySet<Integer> anomalyTypes;
38 
39     public final long screenOnTimeMs;
40     public final int uid;
41 
AppInfo(AppInfo.Builder builder)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
AppInfo(Parcel in)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
compareTo(AppInfo o)58     public int compareTo(AppInfo o) {
59         return Long.compare(screenOnTimeMs, o.screenOnTimeMs);
60     }
61 
62     @Override
describeContents()63     public int describeContents() {
64         return 0;
65     }
66 
67     @Override
writeToParcel(Parcel dest, int flags)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
toString()76     public String toString() {
77         return "packageName="
78                 + packageName
79                 + ",anomalyTypes="
80                 + anomalyTypes
81                 + ",screenTime="
82                 + screenOnTimeMs;
83     }
84 
85     @Override
equals(Object obj)86     public boolean equals(Object obj) {
87         if (this == obj) {
88             return true;
89         }
90         if (!(obj instanceof AppInfo)) {
91             return false;
92         }
93 
94         AppInfo other = (AppInfo) obj;
95         return Objects.equals(anomalyTypes, other.anomalyTypes)
96                 && uid == other.uid
97                 && screenOnTimeMs == other.screenOnTimeMs
98                 && TextUtils.equals(packageName, other.packageName);
99     }
100 
101     public static final Parcelable.Creator CREATOR =
102             new Parcelable.Creator() {
103                 public AppInfo createFromParcel(Parcel in) {
104                     return new AppInfo(in);
105                 }
106 
107                 public AppInfo[] newArray(int size) {
108                     return new AppInfo[size];
109                 }
110             };
111 
112     public static final class Builder {
113         private ArraySet<Integer> mAnomalyTypes = new ArraySet<>();
114         private String mPackageName;
115         private long mScreenOnTimeMs;
116         private int mUid;
117 
addAnomalyType(int type)118         public Builder addAnomalyType(int type) {
119             mAnomalyTypes.add(type);
120             return this;
121         }
122 
setPackageName(String packageName)123         public Builder setPackageName(String packageName) {
124             mPackageName = packageName;
125             return this;
126         }
127 
setScreenOnTimeMs(long screenOnTimeMs)128         public Builder setScreenOnTimeMs(long screenOnTimeMs) {
129             mScreenOnTimeMs = screenOnTimeMs;
130             return this;
131         }
132 
setUid(int uid)133         public Builder setUid(int uid) {
134             mUid = uid;
135             return this;
136         }
137 
build()138         public AppInfo build() {
139             return new AppInfo(this);
140         }
141     }
142 }
143