1 /*
2  * Copyright (C) 2021 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 android.os;
18 
19 import android.annotation.NonNull;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 
24 /**
25  * Snapshot of wake lock stats.
26  *  @hide
27  */
28 public final class WakeLockStats implements Parcelable {
29 
30     /** @hide */
31     public static class WakeLock {
32         public final int uid;
33         @NonNull
34         public final String name;
35         public final int timesAcquired;
36         public final long totalTimeHeldMs;
37 
38         /**
39          * Time in milliseconds that the lock has been held or 0 if not currently holding the lock
40          */
41         public final long timeHeldMs;
42 
WakeLock(int uid, @NonNull String name, int timesAcquired, long totalTimeHeldMs, long timeHeldMs)43         public WakeLock(int uid, @NonNull String name, int timesAcquired, long totalTimeHeldMs,
44                 long timeHeldMs) {
45             this.uid = uid;
46             this.name = name;
47             this.timesAcquired = timesAcquired;
48             this.totalTimeHeldMs = totalTimeHeldMs;
49             this.timeHeldMs = timeHeldMs;
50         }
51 
WakeLock(Parcel in)52         private WakeLock(Parcel in) {
53             uid = in.readInt();
54             name = in.readString();
55             timesAcquired = in.readInt();
56             totalTimeHeldMs = in.readLong();
57             timeHeldMs = in.readLong();
58         }
59 
writeToParcel(Parcel out)60         private void writeToParcel(Parcel out) {
61             out.writeInt(uid);
62             out.writeString(name);
63             out.writeInt(timesAcquired);
64             out.writeLong(totalTimeHeldMs);
65             out.writeLong(timeHeldMs);
66         }
67 
68         @Override
toString()69         public String toString() {
70             return "WakeLock{"
71                     + "uid=" + uid
72                     + ", name='" + name + '\''
73                     + ", timesAcquired=" + timesAcquired
74                     + ", totalTimeHeldMs=" + totalTimeHeldMs
75                     + ", timeHeldMs=" + timeHeldMs
76                     + '}';
77         }
78     }
79 
80     private final List<WakeLock> mWakeLocks;
81 
82     /** @hide **/
WakeLockStats(@onNull List<WakeLock> wakeLocks)83     public WakeLockStats(@NonNull List<WakeLock> wakeLocks) {
84         mWakeLocks = wakeLocks;
85     }
86 
87     @NonNull
getWakeLocks()88     public List<WakeLock> getWakeLocks() {
89         return mWakeLocks;
90     }
91 
WakeLockStats(Parcel in)92     private WakeLockStats(Parcel in) {
93         final int size = in.readInt();
94         mWakeLocks = new ArrayList<>(size);
95         for (int i = 0; i < size; i++) {
96             mWakeLocks.add(new WakeLock(in));
97         }
98     }
99 
100     @Override
writeToParcel(@onNull Parcel out, int flags)101     public void writeToParcel(@NonNull Parcel out, int flags) {
102         final int size = mWakeLocks.size();
103         out.writeInt(size);
104         for (int i = 0; i < size; i++) {
105             WakeLock stats = mWakeLocks.get(i);
106             stats.writeToParcel(out);
107         }
108     }
109 
110     @NonNull
111     public static final Creator<WakeLockStats> CREATOR =
112             new Creator<WakeLockStats>() {
113                 public WakeLockStats createFromParcel(Parcel in) {
114                     return new WakeLockStats(in);
115                 }
116 
117                 public WakeLockStats[] newArray(int size) {
118                     return new WakeLockStats[size];
119                 }
120             };
121 
122     @Override
describeContents()123     public int describeContents() {
124         return 0;
125     }
126 
127     @Override
toString()128     public String toString() {
129         return "WakeLockStats " + mWakeLocks;
130     }
131 }
132