1 /*
2  * Copyright (C) 2017 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.googlecode.android_scripting.facade.telephony;
18 
19 import android.telephony.DataConnectionRealTimeInfo;
20 import android.telephony.PhysicalChannelConfig;
21 import android.telephony.PreciseCallState;
22 import android.telephony.ServiceState;
23 import android.telephony.TelephonyDisplayInfo;
24 
25 import com.googlecode.android_scripting.jsonrpc.JsonSerializable;
26 
27 import java.util.List;
28 
29 import org.json.JSONArray;
30 import org.json.JSONException;
31 import org.json.JSONObject;
32 
33 public class TelephonyEvents {
34 
35     public static class CallStateEvent implements JsonSerializable {
36         private String mCallState;
37         private String mIncomingNumber;
38         private int mSubscriptionId;
39 
CallStateEvent(int state, String incomingNumber, int subscriptionId)40         CallStateEvent(int state, String incomingNumber, int subscriptionId) {
41             mCallState = null;
42             mIncomingNumber = TelephonyUtils.formatIncomingNumber(
43                     incomingNumber);
44             mCallState = TelephonyUtils.getTelephonyCallStateString(
45                     state);
46             mSubscriptionId = subscriptionId;
47         }
48 
getIncomingNumber()49         public String getIncomingNumber() {
50             return mIncomingNumber;
51         }
52 
getSubscriptionId()53         public int getSubscriptionId() {
54             return mSubscriptionId;
55         }
56 
toJSON()57         public JSONObject toJSON() throws JSONException {
58             JSONObject callState = new JSONObject();
59 
60             callState.put(
61                     TelephonyConstants.CallStateContainer.SUBSCRIPTION_ID,
62                     mSubscriptionId);
63             callState.put(
64                     TelephonyConstants.CallStateContainer.INCOMING_NUMBER,
65                     mIncomingNumber);
66             callState.put(TelephonyConstants.CallStateContainer.CALL_STATE,
67                     mCallState);
68 
69             return callState;
70         }
71     }
72 
73     public static class PreciseCallStateEvent implements JsonSerializable {
74         private PreciseCallState mPreciseCallState;
75         private String mPreciseCallStateString;
76         private String mType;
77         private int mCause;
78         private int mSubscriptionId;
79 
PreciseCallStateEvent(int newState, String type, PreciseCallState preciseCallState, int subscriptionId)80         PreciseCallStateEvent(int newState, String type,
81                 PreciseCallState preciseCallState, int subscriptionId) {
82             mPreciseCallStateString = TelephonyUtils.getPreciseCallStateString(
83                     newState);
84             mPreciseCallState = preciseCallState;
85             mType = type;
86             mSubscriptionId = subscriptionId;
87             mCause = preciseCallState.getPreciseDisconnectCause();
88         }
89 
getType()90         public String getType() {
91             return mType;
92         }
93 
getSubscriptionId()94         public int getSubscriptionId() {
95             return mSubscriptionId;
96         }
97 
getPreciseCallState()98         public PreciseCallState getPreciseCallState() {
99             return mPreciseCallState;
100         }
101 
getCause()102         public int getCause() {
103             return mCause;
104         }
105 
toJSON()106         public JSONObject toJSON() throws JSONException {
107             JSONObject preciseCallState = new JSONObject();
108 
109             preciseCallState.put(
110                     TelephonyConstants.PreciseCallStateContainer.SUBSCRIPTION_ID,
111                     mSubscriptionId);
112             preciseCallState.put(
113                     TelephonyConstants.PreciseCallStateContainer.TYPE, mType);
114             preciseCallState.put(
115                     TelephonyConstants.PreciseCallStateContainer.PRECISE_CALL_STATE,
116                     mPreciseCallStateString);
117             preciseCallState.put(
118                     TelephonyConstants.PreciseCallStateContainer.CAUSE, mCause);
119 
120             return preciseCallState;
121         }
122     }
123 
124     public static class DataConnectionRealTimeInfoEvent implements JsonSerializable {
125         private DataConnectionRealTimeInfo mDataConnectionRealTimeInfo;
126         private String mDataConnectionPowerState;
127         private int mSubscriptionId;
128         private long mTime;
129 
DataConnectionRealTimeInfoEvent( DataConnectionRealTimeInfo dataConnectionRealTimeInfo, int subscriptionId)130         DataConnectionRealTimeInfoEvent(
131                 DataConnectionRealTimeInfo dataConnectionRealTimeInfo,
132                 int subscriptionId) {
133             mTime = dataConnectionRealTimeInfo.getTime();
134             mSubscriptionId = subscriptionId;
135             mDataConnectionPowerState = TelephonyUtils.getDcPowerStateString(
136                     dataConnectionRealTimeInfo.getDcPowerState());
137             mDataConnectionRealTimeInfo = dataConnectionRealTimeInfo;
138         }
139 
getSubscriptionId()140         public int getSubscriptionId() {
141             return mSubscriptionId;
142         }
143 
getTime()144         public long getTime() {
145             return mTime;
146         }
147 
toJSON()148         public JSONObject toJSON() throws JSONException {
149             JSONObject dataConnectionRealTimeInfo = new JSONObject();
150 
151             dataConnectionRealTimeInfo.put(
152                     TelephonyConstants.DataConnectionRealTimeInfoContainer.SUBSCRIPTION_ID,
153                     mSubscriptionId);
154             dataConnectionRealTimeInfo.put(
155                     TelephonyConstants.DataConnectionRealTimeInfoContainer.TIME,
156                     mTime);
157             dataConnectionRealTimeInfo.put(
158                     TelephonyConstants.DataConnectionRealTimeInfoContainer.DATA_CONNECTION_POWER_STATE,
159                     mDataConnectionPowerState);
160 
161             return dataConnectionRealTimeInfo;
162         }
163     }
164 
165     public static class DataConnectionStateEvent implements JsonSerializable {
166         private String mDataConnectionState;
167         private int mSubscriptionId;
168         private int mState;
169         private String mDataNetworkType;
170 
DataConnectionStateEvent(int state, String dataNetworkType, int subscriptionId)171         DataConnectionStateEvent(int state, String dataNetworkType,
172                 int subscriptionId) {
173             mSubscriptionId = subscriptionId;
174             mDataConnectionState = TelephonyUtils.getDataConnectionStateString(
175                     state);
176             mDataNetworkType = dataNetworkType;
177             mState = state;
178         }
179 
getSubscriptionId()180         public int getSubscriptionId() {
181             return mSubscriptionId;
182         }
183 
getState()184         public int getState() {
185             return mState;
186         }
187 
getDataNetworkType()188         public String getDataNetworkType() {
189             return mDataNetworkType;
190         }
191 
toJSON()192         public JSONObject toJSON() throws JSONException {
193             JSONObject dataConnectionState = new JSONObject();
194 
195             dataConnectionState.put(
196                     TelephonyConstants.DataConnectionStateContainer.SUBSCRIPTION_ID,
197                     mSubscriptionId);
198             dataConnectionState.put(
199                     TelephonyConstants.DataConnectionStateContainer.DATA_CONNECTION_STATE,
200                     mDataConnectionState);
201             dataConnectionState.put(
202                     TelephonyConstants.DataConnectionStateContainer.DATA_NETWORK_TYPE,
203                     mDataNetworkType);
204             dataConnectionState.put(
205                     TelephonyConstants.DataConnectionStateContainer.STATE_CODE,
206                     mState);
207 
208             return dataConnectionState;
209         }
210     }
211 
212     public static class ServiceStateEvent implements JsonSerializable {
213         private String mServiceStateString;
214         private int mSubscriptionId;
215         private ServiceState mServiceState;
216 
ServiceStateEvent(ServiceState serviceState, int subscriptionId)217         ServiceStateEvent(ServiceState serviceState, int subscriptionId) {
218             mServiceState = serviceState;
219             mSubscriptionId = subscriptionId;
220             mServiceStateString = TelephonyUtils.getNetworkStateString(
221                     serviceState.getState());
222             if (mServiceStateString.equals(
223                     TelephonyConstants.SERVICE_STATE_OUT_OF_SERVICE) &&
224                     serviceState.isEmergencyOnly()) {
225                 mServiceStateString = TelephonyConstants.SERVICE_STATE_EMERGENCY_ONLY;
226             }
227         }
228 
getSubscriptionId()229         public int getSubscriptionId() {
230             return mSubscriptionId;
231         }
232 
getServiceState()233         public ServiceState getServiceState() {
234             return mServiceState;
235         }
236 
toJSON()237         public JSONObject toJSON() throws JSONException {
238             JSONObject serviceState =
239                     com.googlecode.android_scripting.jsonrpc.JsonBuilder.buildServiceState(
240                             mServiceState);
241             serviceState.put(
242                     TelephonyConstants.ServiceStateContainer.SUBSCRIPTION_ID,
243                     mSubscriptionId);
244 
245             return serviceState;
246         }
247     }
248 
249     public static class MessageWaitingIndicatorEvent implements JsonSerializable {
250         private boolean mMessageWaitingIndicator;
251 
MessageWaitingIndicatorEvent(boolean messageWaitingIndicator)252         MessageWaitingIndicatorEvent(boolean messageWaitingIndicator) {
253             mMessageWaitingIndicator = messageWaitingIndicator;
254         }
255 
getMessageWaitingIndicator()256         public boolean getMessageWaitingIndicator() {
257             return mMessageWaitingIndicator;
258         }
259 
toJSON()260         public JSONObject toJSON() throws JSONException {
261             JSONObject messageWaitingIndicator = new JSONObject();
262 
263             messageWaitingIndicator.put(
264                     TelephonyConstants.MessageWaitingIndicatorContainer.IS_MESSAGE_WAITING,
265                     mMessageWaitingIndicator);
266 
267             return messageWaitingIndicator;
268         }
269     }
270 
271     public static class DisplayInfoChangedEvent implements JsonSerializable {
272         private TelephonyDisplayInfo mDisplayInfoString;
273         private int mSubscriptionId;
274         private String mOverrideDataNetworkType;
275         private String mDataNetworkType;
276 
DisplayInfoChangedEvent(TelephonyDisplayInfo DisplayInfoString, int subscriptionId)277         DisplayInfoChangedEvent(TelephonyDisplayInfo DisplayInfoString, int subscriptionId) {
278             mDisplayInfoString = DisplayInfoString;
279             mSubscriptionId = subscriptionId;
280             mOverrideDataNetworkType = TelephonyUtils.getDisplayInfoString(
281                     DisplayInfoString.getOverrideNetworkType());
282             mDataNetworkType = TelephonyUtils.getNetworkTypeString(
283                     DisplayInfoString.getNetworkType());
284         }
285 
getOverrideDataNetworkType()286         public String getOverrideDataNetworkType() {
287             return mOverrideDataNetworkType;
288         }
289 
getSubscriptionId()290         public int getSubscriptionId() {
291             return mSubscriptionId;
292         }
293 
getDataNetworkType()294         public String getDataNetworkType() {
295             return mDataNetworkType;
296         }
297 
toJSON()298         public JSONObject toJSON() throws JSONException {
299             JSONObject displayInfoState = new JSONObject();
300 
301             displayInfoState.put(
302                     TelephonyConstants.DisplayInfoContainer.OVERRIDE,
303                     mOverrideDataNetworkType);
304 
305             displayInfoState.put(
306                     TelephonyConstants.DisplayInfoContainer.NETWORK,
307                     mDataNetworkType);
308 
309             displayInfoState.put(
310                     TelephonyConstants.DisplayInfoContainer.SUBSCRIPTION_ID,
311                     mSubscriptionId);
312 
313             return displayInfoState;
314         }
315     }
316 
317     public static class PhysicalChannelConfigChangedEvent implements JsonSerializable {
318         private final List<PhysicalChannelConfig> mConfigs;
319 
PhysicalChannelConfigChangedEvent(List<PhysicalChannelConfig> configs)320         PhysicalChannelConfigChangedEvent(List<PhysicalChannelConfig> configs) {
321             mConfigs = configs;
322         }
323 
getConfigs()324         List<PhysicalChannelConfig> getConfigs() {
325             return mConfigs;
326         }
327 
toJSON()328         public JSONObject toJSON() throws JSONException {
329             JSONArray jsonConfigs = new JSONArray();
330             for(PhysicalChannelConfig c : mConfigs) {
331                 JSONObject cfg  = new JSONObject();
332                 cfg.put(
333                         TelephonyConstants.PhysicalChannelConfigContainer.CELL_BANDWIDTH_DOWNLINK,
334                         c.getCellBandwidthDownlinkKhz());
335                 cfg.put(
336                         TelephonyConstants.PhysicalChannelConfigContainer.CONNECTION_STATUS,
337                         c.getConnectionStatus());
338                jsonConfigs.put(cfg);
339             }
340             return new JSONObject().put(
341                     TelephonyConstants.PhysicalChannelConfigContainer.CONFIGS, jsonConfigs);
342         }
343     }
344 }
345