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 com.android.internal.telephony.metrics;
18 
19 import static com.android.internal.telephony.nano.TelephonyProto.EmergencyNumberInfo;
20 import static com.android.internal.telephony.nano.TelephonyProto.ImsCapabilities;
21 import static com.android.internal.telephony.nano.TelephonyProto.ImsConnectionState;
22 import static com.android.internal.telephony.nano.TelephonyProto.RilDataCall;
23 import static com.android.internal.telephony.nano.TelephonyProto.SimState;
24 import static com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent;
25 import static com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.CarrierIdMatching;
26 import static com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.CarrierKeyChange;
27 import static com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.ModemRestart;
28 import static com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.NetworkCapabilitiesInfo;
29 import static com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.RilDeactivateDataCall;
30 import static com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.RilSetupDataCall;
31 import static com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.RilSetupDataCallResponse;
32 import static com.android.internal.telephony.nano.TelephonyProto.TelephonyServiceState;
33 import static com.android.internal.telephony.nano.TelephonyProto.TelephonySettings;
34 
35 import android.os.SystemClock;
36 import android.telephony.TelephonyManager;
37 import android.util.SparseArray;
38 
39 import com.android.internal.telephony.nano.TelephonyProto.ActiveSubscriptionInfo;
40 import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.DataSwitch;
41 import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.OnDemandDataSwitch;
42 import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent.Type;
43 
44 import java.util.Arrays;
45 
46 public class TelephonyEventBuilder {
47     private final TelephonyEvent mEvent = new TelephonyEvent();
48 
build()49     public TelephonyEvent build() {
50         return mEvent;
51     }
52 
53     /** The event is not related to any phone id. */
TelephonyEventBuilder()54     public TelephonyEventBuilder() {
55         this(-1 /* phoneId */);
56     }
57 
TelephonyEventBuilder(int phoneId)58     public TelephonyEventBuilder(int phoneId) {
59         this(SystemClock.elapsedRealtime(), phoneId);
60     }
61 
TelephonyEventBuilder(long timestamp, int phoneId)62     public TelephonyEventBuilder(long timestamp, int phoneId) {
63         mEvent.timestampMillis = timestamp;
64         mEvent.phoneId = phoneId;
65     }
66 
setSettings(TelephonySettings settings)67     public TelephonyEventBuilder setSettings(TelephonySettings settings) {
68         mEvent.type = TelephonyEvent.Type.SETTINGS_CHANGED;
69         mEvent.settings = settings;
70         return this;
71     }
72 
setServiceState(TelephonyServiceState state)73     public TelephonyEventBuilder setServiceState(TelephonyServiceState state) {
74         mEvent.type = TelephonyEvent.Type.RIL_SERVICE_STATE_CHANGED;
75         mEvent.serviceState = state;
76         return this;
77     }
78 
setImsConnectionState(ImsConnectionState state)79     public TelephonyEventBuilder setImsConnectionState(ImsConnectionState state) {
80         mEvent.type = TelephonyEvent.Type.IMS_CONNECTION_STATE_CHANGED;
81         mEvent.imsConnectionState = state;
82         return this;
83     }
84 
setImsCapabilities(ImsCapabilities capabilities)85     public TelephonyEventBuilder setImsCapabilities(ImsCapabilities capabilities) {
86         mEvent.type = TelephonyEvent.Type.IMS_CAPABILITIES_CHANGED;
87         mEvent.imsCapabilities = capabilities;
88         return this;
89     }
90 
setDataStallRecoveryAction(int action)91     public TelephonyEventBuilder setDataStallRecoveryAction(int action) {
92         mEvent.type = TelephonyEvent.Type.DATA_STALL_ACTION;
93         mEvent.dataStallAction = action;
94         return this;
95     }
96 
setSignalStrength(int signalstrength)97     public TelephonyEventBuilder setSignalStrength(int signalstrength) {
98         mEvent.type = TelephonyEvent.Type.SIGNAL_STRENGTH;
99         mEvent.signalStrength = signalstrength;
100         return this;
101     }
102 
setSetupDataCall(RilSetupDataCall request)103     public TelephonyEventBuilder setSetupDataCall(RilSetupDataCall request) {
104         mEvent.type = TelephonyEvent.Type.DATA_CALL_SETUP;
105         mEvent.setupDataCall = request;
106         return this;
107     }
108 
setSetupDataCallResponse(RilSetupDataCallResponse rsp)109     public TelephonyEventBuilder setSetupDataCallResponse(RilSetupDataCallResponse rsp) {
110         mEvent.type = TelephonyEvent.Type.DATA_CALL_SETUP_RESPONSE;
111         mEvent.setupDataCallResponse = rsp;
112         return this;
113     }
114 
setDeactivateDataCall(RilDeactivateDataCall request)115     public TelephonyEventBuilder setDeactivateDataCall(RilDeactivateDataCall request) {
116         mEvent.type = TelephonyEvent.Type.DATA_CALL_DEACTIVATE;
117         mEvent.deactivateDataCall = request;
118         return this;
119     }
120 
setDeactivateDataCallResponse(int errno)121     public TelephonyEventBuilder setDeactivateDataCallResponse(int errno) {
122         mEvent.type = TelephonyEvent.Type.DATA_CALL_DEACTIVATE_RESPONSE;
123         mEvent.error = errno;
124         return this;
125     }
126 
setDataCalls(RilDataCall[] dataCalls)127     public TelephonyEventBuilder setDataCalls(RilDataCall[] dataCalls) {
128         mEvent.type = TelephonyEvent.Type.DATA_CALL_LIST_CHANGED;
129         mEvent.dataCalls = dataCalls;
130         return this;
131     }
132 
setNITZ(long timestamp)133     public TelephonyEventBuilder setNITZ(long timestamp) {
134         mEvent.type = TelephonyEvent.Type.NITZ_TIME;
135         mEvent.nitzTimestampMillis = timestamp;
136         return this;
137     }
138 
setModemRestart(ModemRestart modemRestart)139     public TelephonyEventBuilder setModemRestart(ModemRestart modemRestart) {
140         mEvent.type = TelephonyEvent.Type.MODEM_RESTART;
141         mEvent.modemRestart = modemRestart;
142         return this;
143     }
144 
145     /**
146      * Set and build Carrier Id Matching event
147      */
setCarrierIdMatching(CarrierIdMatching carrierIdMatching)148     public TelephonyEventBuilder setCarrierIdMatching(CarrierIdMatching carrierIdMatching) {
149         mEvent.type = TelephonyEvent.Type.CARRIER_ID_MATCHING;
150         mEvent.carrierIdMatching = carrierIdMatching;
151         return this;
152     }
153 
154     /**
155      * Set and build EMERGENCY_NUMBER_REPORT event
156      */
setUpdatedEmergencyNumber( EmergencyNumberInfo emergencyNumberInfo, int emergencyNumberDbVersion)157     public TelephonyEventBuilder setUpdatedEmergencyNumber(
158             EmergencyNumberInfo emergencyNumberInfo,
159             int emergencyNumberDbVersion) {
160         mEvent.type = TelephonyEvent.Type.EMERGENCY_NUMBER_REPORT;
161         mEvent.updatedEmergencyNumber = emergencyNumberInfo;
162         mEvent.emergencyNumberDatabaseVersion = emergencyNumberDbVersion;
163         return this;
164     }
165 
setCarrierKeyChange(CarrierKeyChange carrierKeyChange)166     public TelephonyEventBuilder setCarrierKeyChange(CarrierKeyChange carrierKeyChange) {
167         mEvent.type = TelephonyEvent.Type.CARRIER_KEY_CHANGED;
168         mEvent.carrierKeyChange = carrierKeyChange;
169         return this;
170     }
171 
172     /** Set and build SIM state change event. */
setSimStateChange(SparseArray<Integer> simStates)173     public TelephonyEventBuilder setSimStateChange(SparseArray<Integer> simStates) {
174         int phoneCount = TelephonyManager.getDefault().getPhoneCount();
175         mEvent.simState = new int[phoneCount];
176         Arrays.fill(mEvent.simState, SimState.SIM_STATE_UNKNOWN);
177         mEvent.type = Type.SIM_STATE_CHANGED;
178         for (int i = 0; i < simStates.size(); i++) {
179             int key = simStates.keyAt(i);
180             if (0 <= key && key < phoneCount) {
181                 mEvent.simState[key] = simStates.get(key);
182             }
183         }
184         return this;
185     }
186 
187     /** Set and build subscription info change event. */
setActiveSubscriptionInfoChange(ActiveSubscriptionInfo info)188     public TelephonyEventBuilder setActiveSubscriptionInfoChange(ActiveSubscriptionInfo info) {
189         mEvent.type = Type.ACTIVE_SUBSCRIPTION_INFO_CHANGED;
190         mEvent.activeSubscriptionInfo = info;
191         return this;
192     }
193 
194     /** Set and build enabled modem bitmap change event. */
setEnabledModemBitmap(int enabledModemBitmap)195     public TelephonyEventBuilder setEnabledModemBitmap(int enabledModemBitmap) {
196         mEvent.type = Type.ENABLED_MODEM_CHANGED;
197         mEvent.enabledModemBitmap = enabledModemBitmap;
198         return this;
199     }
200 
201     /** Set and build data switch event. */
setDataSwitch(DataSwitch dataSwitch)202     public TelephonyEventBuilder setDataSwitch(DataSwitch dataSwitch) {
203         mEvent.type = TelephonyEvent.Type.DATA_SWITCH;
204         mEvent.dataSwitch = dataSwitch;
205         return this;
206     }
207 
208     /** Set and build network validation event. */
setNetworkValidate(int networkValidationState)209     public TelephonyEventBuilder setNetworkValidate(int networkValidationState) {
210         mEvent.type = TelephonyEvent.Type.NETWORK_VALIDATE;
211         mEvent.networkValidationState = networkValidationState;
212         return this;
213     }
214 
215     /** Set and build on demand data switch event. */
setOnDemandDataSwitch(OnDemandDataSwitch onDemandDataSwitch)216     public TelephonyEventBuilder setOnDemandDataSwitch(OnDemandDataSwitch onDemandDataSwitch) {
217         mEvent.type = TelephonyEvent.Type.ON_DEMAND_DATA_SWITCH;
218         mEvent.onDemandDataSwitch = onDemandDataSwitch;
219         return this;
220     }
221 
222     /** Set and build network capabilities changed event. */
setNetworkCapabilities( NetworkCapabilitiesInfo networkCapabilities)223     public TelephonyEventBuilder setNetworkCapabilities(
224             NetworkCapabilitiesInfo networkCapabilities) {
225         mEvent.type = TelephonyEvent.Type.NETWORK_CAPABILITIES_CHANGED;
226         mEvent.networkCapabilities = networkCapabilities;
227         return this;
228     }
229 }
230