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