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