1 /*
2  * Copyright (C) 2022 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.server.telecom.stats;
18 
19 import android.content.pm.PackageManager;
20 import android.telecom.DisconnectCause;
21 import android.telecom.Log;
22 
23 import com.android.server.telecom.CallState;
24 import com.android.server.telecom.TelecomStatsLog;
25 
26 /**
27  * Collects and stores data for CallStateChanged atom for each call, and provide a
28  * method to write the data to statsd whenever the call state changes.
29  */
30 public class CallStateChangedAtomWriter {
31     private boolean mIsSelfManaged = false;
32     private boolean mIsExternalCall = false;
33     private boolean mIsEmergencyCall = false;
34     private int mUid = -1;
35     private int mDurationSeconds = 0;
36     private int mExistingCallCount = 0;
37     private int mHeldCallCount = 0;
38     private CallFailureCause mStartFailCause = CallFailureCause.NONE;
39     private DisconnectCause mDisconnectCause = new DisconnectCause(DisconnectCause.UNKNOWN);
40 
41     /**
42      * Write collected data and current call state to statsd.
43      *
44      * @param state Current call state.
45      */
write(int state)46     public void write(int state) {
47         TelecomStatsLog.write(TelecomStatsLog.CALL_STATE_CHANGED,
48                 state,
49                 state == CallState.DISCONNECTED ?
50                     mDisconnectCause.getCode() : DisconnectCause.UNKNOWN,
51                 mIsSelfManaged,
52                 mIsExternalCall,
53                 mIsEmergencyCall,
54                 mUid,
55                 state == CallState.DISCONNECTED ? mDurationSeconds : 0,
56                 mExistingCallCount,
57                 mHeldCallCount,
58                 state == CallState.DISCONNECTED ?
59                     mStartFailCause.getCode() : CallFailureCause.NONE.getCode());
60     }
61 
setSelfManaged(boolean isSelfManaged)62     public CallStateChangedAtomWriter setSelfManaged(boolean isSelfManaged) {
63         mIsSelfManaged = isSelfManaged;
64         return this;
65     }
66 
setExternalCall(boolean isExternalCall)67     public CallStateChangedAtomWriter setExternalCall(boolean isExternalCall) {
68         mIsExternalCall = isExternalCall;
69         return this;
70     }
71 
setEmergencyCall(boolean isEmergencyCall)72     public CallStateChangedAtomWriter setEmergencyCall(boolean isEmergencyCall) {
73         mIsEmergencyCall = isEmergencyCall;
74         return this;
75     }
76 
setUid(int uid)77     public CallStateChangedAtomWriter setUid(int uid) {
78         mUid = uid;
79         return this;
80     }
81 
setUid(String packageName, PackageManager pm)82     public CallStateChangedAtomWriter setUid(String packageName, PackageManager pm) {
83         try {
84             final int uid = pm.getPackageUid(packageName, PackageManager.PackageInfoFlags.of(0));
85             return setUid(uid);
86 
87         } catch (PackageManager.NameNotFoundException e) {
88             Log.e(this, e, "Could not find the package");
89         }
90         return setUid(-1);
91     }
92 
setDurationSeconds(int duration)93     public CallStateChangedAtomWriter setDurationSeconds(int duration) {
94         if (duration >= 0) {
95             mDurationSeconds = duration;
96         }
97         return this;
98     }
99 
setExistingCallCount(int count)100     public CallStateChangedAtomWriter setExistingCallCount(int count) {
101         mExistingCallCount = count;
102         return this;
103     }
104 
increaseHeldCallCount()105     public CallStateChangedAtomWriter increaseHeldCallCount() {
106         mHeldCallCount++;
107         return this;
108     }
109 
setDisconnectCause(DisconnectCause cause)110     public CallStateChangedAtomWriter setDisconnectCause(DisconnectCause cause) {
111         mDisconnectCause = cause;
112         return this;
113     }
114 
setStartFailCause(CallFailureCause cause)115     public CallStateChangedAtomWriter setStartFailCause(CallFailureCause cause) {
116         mStartFailCause = cause;
117         return this;
118     }
119 }
120