1 /*
2  * Copyright (C) 2016 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.telephony;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 import android.telephony.TelephonyHistogram;
22 import android.util.SparseArray;
23 import java.util.ArrayList;
24 import java.util.List;
25 
26 /**
27  * Parcelable class to store Client request statistics information.
28  *
29  * @hide
30  */
31 public final class ClientRequestStats implements Parcelable {
32     public static final @android.annotation.NonNull Parcelable.Creator<ClientRequestStats> CREATOR =
33             new Parcelable.Creator<ClientRequestStats>() {
34 
35                 public ClientRequestStats createFromParcel(Parcel in) {
36                     return new ClientRequestStats(in);
37                 }
38 
39                 public ClientRequestStats[] newArray(int size) {
40                     return new ClientRequestStats[size];
41                 }
42             };
43     private static final int REQUEST_HISTOGRAM_BUCKET_COUNT = 5;
44     private String mCallingPackage;
45     /* completed requests wake lock time in milli seconds */
46     private long mCompletedRequestsWakelockTime = 0;
47     private long mCompletedRequestsCount = 0;
48     private long mPendingRequestsWakelockTime = 0;
49     private long mPendingRequestsCount = 0;
50     private SparseArray<TelephonyHistogram> mRequestHistograms =
51             new SparseArray<TelephonyHistogram>();
52 
ClientRequestStats(Parcel in)53     public ClientRequestStats(Parcel in) {
54         readFromParcel(in);
55     }
56 
ClientRequestStats()57     public ClientRequestStats() {
58     }
59 
ClientRequestStats(ClientRequestStats clientRequestStats)60     public ClientRequestStats(ClientRequestStats clientRequestStats) {
61         mCallingPackage = clientRequestStats.getCallingPackage();
62         mCompletedRequestsCount = clientRequestStats.getCompletedRequestsCount();
63         mCompletedRequestsWakelockTime = clientRequestStats.getCompletedRequestsWakelockTime();
64         mPendingRequestsCount = clientRequestStats.getPendingRequestsCount();
65         mPendingRequestsWakelockTime = clientRequestStats.getPendingRequestsWakelockTime();
66 
67         List<TelephonyHistogram> list = clientRequestStats.getRequestHistograms();
68         for (TelephonyHistogram entry : list) {
69             mRequestHistograms.put(entry.getId(), entry);
70         }
71     }
72 
getCallingPackage()73     public String getCallingPackage() {
74         return mCallingPackage;
75     }
76 
setCallingPackage(String mCallingPackage)77     public void setCallingPackage(String mCallingPackage) {
78         this.mCallingPackage = mCallingPackage;
79     }
80 
getCompletedRequestsWakelockTime()81     public long getCompletedRequestsWakelockTime() {
82         return mCompletedRequestsWakelockTime;
83     }
84 
addCompletedWakelockTime(long completedRequestsWakelockTime)85     public void addCompletedWakelockTime(long completedRequestsWakelockTime) {
86         this.mCompletedRequestsWakelockTime += completedRequestsWakelockTime;
87     }
88 
getPendingRequestsWakelockTime()89     public long getPendingRequestsWakelockTime() {
90         return mPendingRequestsWakelockTime;
91     }
92 
setPendingRequestsWakelockTime(long pendingRequestsWakelockTime)93     public void setPendingRequestsWakelockTime(long pendingRequestsWakelockTime) {
94         this.mPendingRequestsWakelockTime = pendingRequestsWakelockTime;
95     }
96 
getCompletedRequestsCount()97     public long getCompletedRequestsCount() {
98         return mCompletedRequestsCount;
99     }
100 
incrementCompletedRequestsCount()101     public void incrementCompletedRequestsCount() {
102         this.mCompletedRequestsCount++;
103     }
104 
getPendingRequestsCount()105     public long getPendingRequestsCount() {
106         return mPendingRequestsCount;
107     }
108 
setPendingRequestsCount(long pendingRequestsCount)109     public void setPendingRequestsCount(long pendingRequestsCount) {
110         this.mPendingRequestsCount = pendingRequestsCount;
111     }
112 
getRequestHistograms()113     public List<TelephonyHistogram> getRequestHistograms() {
114         List<TelephonyHistogram> list;
115         synchronized (mRequestHistograms) {
116             list = new ArrayList<>(mRequestHistograms.size());
117             for (int i = 0; i < mRequestHistograms.size(); i++) {
118                 TelephonyHistogram entry = new TelephonyHistogram(mRequestHistograms.valueAt(i));
119                 list.add(entry);
120             }
121         }
122         return list;
123     }
124 
updateRequestHistograms(int requestId, int time)125     public void updateRequestHistograms(int requestId, int time) {
126         synchronized (mRequestHistograms) {
127             TelephonyHistogram entry = mRequestHistograms.get(requestId);
128             if (entry == null) {
129                 entry = new TelephonyHistogram(TelephonyHistogram.TELEPHONY_CATEGORY_RIL,
130                         requestId, REQUEST_HISTOGRAM_BUCKET_COUNT);
131                 mRequestHistograms.put(requestId, entry);
132             }
133             entry.addTimeTaken(time);
134         }
135     }
136 
137     @Override
toString()138     public String toString() {
139         return "ClientRequestStats{" +
140                 "mCallingPackage='" + mCallingPackage + '\'' +
141                 ", mCompletedRequestsWakelockTime=" + mCompletedRequestsWakelockTime +
142                 ", mCompletedRequestsCount=" + mCompletedRequestsCount +
143                 ", mPendingRequestsWakelockTime=" + mPendingRequestsWakelockTime +
144                 ", mPendingRequestsCount=" + mPendingRequestsCount +
145                 '}';
146     }
147 
148     @Override
describeContents()149     public int describeContents() {
150         return 0;
151     }
152 
readFromParcel(Parcel in)153     public void readFromParcel(Parcel in) {
154         mCallingPackage = in.readString();
155         mCompletedRequestsWakelockTime = in.readLong();
156         mCompletedRequestsCount = in.readLong();
157         mPendingRequestsWakelockTime = in.readLong();
158         mPendingRequestsCount = in.readLong();
159         ArrayList<TelephonyHistogram> requestHistograms = new ArrayList<TelephonyHistogram>();
160         in.readTypedList(requestHistograms, TelephonyHistogram.CREATOR);
161         for (TelephonyHistogram h : requestHistograms) {
162             mRequestHistograms.put(h.getId(), h);
163         }
164     }
165 
166     @Override
writeToParcel(Parcel dest, int flags)167     public void writeToParcel(Parcel dest, int flags) {
168         dest.writeString(mCallingPackage);
169         dest.writeLong(mCompletedRequestsWakelockTime);
170         dest.writeLong(mCompletedRequestsCount);
171         dest.writeLong(mPendingRequestsWakelockTime);
172         dest.writeLong(mPendingRequestsCount);
173         dest.writeTypedList(getRequestHistograms());
174     }
175 }
176