1 /* 2 * Copyright (C) 2016 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.googlecode.android_scripting.facade.telephony; 18 19 import org.json.JSONException; 20 import org.json.JSONObject; 21 import android.telephony.DataConnectionRealTimeInfo; 22 import android.telephony.PreciseCallState; 23 import android.telephony.ServiceState; 24 import com.googlecode.android_scripting.jsonrpc.JsonSerializable; 25 import com.googlecode.android_scripting.facade.telephony.TelephonyConstants; 26 import com.googlecode.android_scripting.facade.telephony.TelephonyUtils; 27 28 public class TelephonyEvents { 29 30 public static class CallStateEvent implements JsonSerializable { 31 private String mCallState; 32 private String mIncomingNumber; 33 private int mSubscriptionId; 34 35 CallStateEvent(int state, String incomingNumber, int subscriptionId) { 36 mCallState = null; 37 mIncomingNumber = TelephonyUtils.formatIncomingNumber( 38 incomingNumber); 39 mCallState = TelephonyUtils.getTelephonyCallStateString( 40 state); 41 mSubscriptionId = subscriptionId; 42 } 43 44 public String getIncomingNumber() { 45 return mIncomingNumber; 46 } 47 48 public int getSubscriptionId() { 49 return mSubscriptionId; 50 } 51 52 public JSONObject toJSON() throws JSONException { 53 JSONObject callState = new JSONObject(); 54 55 callState.put( 56 TelephonyConstants.CallStateContainer.SUBSCRIPTION_ID, 57 mSubscriptionId); 58 callState.put( 59 TelephonyConstants.CallStateContainer.INCOMING_NUMBER, 60 mIncomingNumber); 61 callState.put(TelephonyConstants.CallStateContainer.CALL_STATE, 62 mCallState); 63 64 return callState; 65 } 66 } 67 68 public static class PreciseCallStateEvent implements JsonSerializable { 69 private PreciseCallState mPreciseCallState; 70 private String mPreciseCallStateString; 71 private String mType; 72 private int mCause; 73 private int mSubscriptionId; 74 75 PreciseCallStateEvent(int newState, String type, 76 PreciseCallState preciseCallState, int subscriptionId) { 77 mPreciseCallStateString = TelephonyUtils.getPreciseCallStateString( 78 newState); 79 mPreciseCallState = preciseCallState; 80 mType = type; 81 mSubscriptionId = subscriptionId; 82 mCause = preciseCallState.getPreciseDisconnectCause(); 83 } 84 85 public String getType() { 86 return mType; 87 } 88 89 public int getSubscriptionId() { 90 return mSubscriptionId; 91 } 92 93 public PreciseCallState getPreciseCallState() { 94 return mPreciseCallState; 95 } 96 97 public int getCause() { 98 return mCause; 99 } 100 101 public JSONObject toJSON() throws JSONException { 102 JSONObject preciseCallState = new JSONObject(); 103 104 preciseCallState.put( 105 TelephonyConstants.PreciseCallStateContainer.SUBSCRIPTION_ID, 106 mSubscriptionId); 107 preciseCallState.put( 108 TelephonyConstants.PreciseCallStateContainer.TYPE, mType); 109 preciseCallState.put( 110 TelephonyConstants.PreciseCallStateContainer.PRECISE_CALL_STATE, 111 mPreciseCallStateString); 112 preciseCallState.put( 113 TelephonyConstants.PreciseCallStateContainer.CAUSE, mCause); 114 115 return preciseCallState; 116 } 117 } 118 119 public static class DataConnectionRealTimeInfoEvent implements JsonSerializable { 120 private DataConnectionRealTimeInfo mDataConnectionRealTimeInfo; 121 private String mDataConnectionPowerState; 122 private int mSubscriptionId; 123 private long mTime; 124 125 DataConnectionRealTimeInfoEvent( 126 DataConnectionRealTimeInfo dataConnectionRealTimeInfo, 127 int subscriptionId) { 128 mTime = dataConnectionRealTimeInfo.getTime(); 129 mSubscriptionId = subscriptionId; 130 mDataConnectionPowerState = TelephonyUtils.getDcPowerStateString( 131 dataConnectionRealTimeInfo.getDcPowerState()); 132 mDataConnectionRealTimeInfo = dataConnectionRealTimeInfo; 133 } 134 135 public int getSubscriptionId() { 136 return mSubscriptionId; 137 } 138 139 public long getTime() { 140 return mTime; 141 } 142 143 public JSONObject toJSON() throws JSONException { 144 JSONObject dataConnectionRealTimeInfo = new JSONObject(); 145 146 dataConnectionRealTimeInfo.put( 147 TelephonyConstants.DataConnectionRealTimeInfoContainer.SUBSCRIPTION_ID, 148 mSubscriptionId); 149 dataConnectionRealTimeInfo.put( 150 TelephonyConstants.DataConnectionRealTimeInfoContainer.TIME, 151 mTime); 152 dataConnectionRealTimeInfo.put( 153 TelephonyConstants.DataConnectionRealTimeInfoContainer.DATA_CONNECTION_POWER_STATE, 154 mDataConnectionPowerState); 155 156 return dataConnectionRealTimeInfo; 157 } 158 } 159 160 public static class DataConnectionStateEvent implements JsonSerializable { 161 private String mDataConnectionState; 162 private int mSubscriptionId; 163 private int mState; 164 private String mDataNetworkType; 165 166 DataConnectionStateEvent(int state, String dataNetworkType, 167 int subscriptionId) { 168 mSubscriptionId = subscriptionId; 169 mDataConnectionState = TelephonyUtils.getDataConnectionStateString( 170 state); 171 mDataNetworkType = dataNetworkType; 172 mState = state; 173 } 174 175 public int getSubscriptionId() { 176 return mSubscriptionId; 177 } 178 179 public int getState() { 180 return mState; 181 } 182 183 public String getDataNetworkType() { 184 return mDataNetworkType; 185 } 186 187 public JSONObject toJSON() throws JSONException { 188 JSONObject dataConnectionState = new JSONObject(); 189 190 dataConnectionState.put( 191 TelephonyConstants.DataConnectionStateContainer.SUBSCRIPTION_ID, 192 mSubscriptionId); 193 dataConnectionState.put( 194 TelephonyConstants.DataConnectionStateContainer.DATA_CONNECTION_STATE, 195 mDataConnectionState); 196 dataConnectionState.put( 197 TelephonyConstants.DataConnectionStateContainer.DATA_NETWORK_TYPE, 198 mDataNetworkType); 199 dataConnectionState.put( 200 TelephonyConstants.DataConnectionStateContainer.STATE_CODE, 201 mState); 202 203 return dataConnectionState; 204 } 205 } 206 207 public static class ServiceStateEvent implements JsonSerializable { 208 private String mServiceStateString; 209 private int mSubscriptionId; 210 private ServiceState mServiceState; 211 212 ServiceStateEvent(ServiceState serviceState, int subscriptionId) { 213 mServiceState = serviceState; 214 mSubscriptionId = subscriptionId; 215 mServiceStateString = TelephonyUtils.getNetworkStateString( 216 serviceState.getState()); 217 if (mServiceStateString.equals( 218 TelephonyConstants.SERVICE_STATE_OUT_OF_SERVICE) && 219 serviceState.isEmergencyOnly()) { 220 mServiceStateString = TelephonyConstants.SERVICE_STATE_EMERGENCY_ONLY; 221 } 222 } 223 224 public int getSubscriptionId() { 225 return mSubscriptionId; 226 } 227 228 public ServiceState getServiceState() { 229 return mServiceState; 230 } 231 232 public JSONObject toJSON() throws JSONException { 233 JSONObject serviceState = new JSONObject(); 234 235 serviceState.put( 236 TelephonyConstants.ServiceStateContainer.SUBSCRIPTION_ID, 237 mSubscriptionId); 238 serviceState.put( 239 TelephonyConstants.ServiceStateContainer.VOICE_REG_STATE, 240 TelephonyUtils.getNetworkStateString( 241 mServiceState.getVoiceRegState())); 242 serviceState.put( 243 TelephonyConstants.ServiceStateContainer.VOICE_NETWORK_TYPE, 244 TelephonyUtils.getNetworkTypeString( 245 mServiceState.getVoiceNetworkType())); 246 serviceState.put( 247 TelephonyConstants.ServiceStateContainer.DATA_REG_STATE, 248 TelephonyUtils.getNetworkStateString( 249 mServiceState.getDataRegState())); 250 serviceState.put( 251 TelephonyConstants.ServiceStateContainer.DATA_NETWORK_TYPE, 252 TelephonyUtils.getNetworkTypeString( 253 mServiceState.getDataNetworkType())); 254 serviceState.put( 255 TelephonyConstants.ServiceStateContainer.OPERATOR_NAME, 256 mServiceState.getOperatorAlphaLong()); 257 serviceState.put( 258 TelephonyConstants.ServiceStateContainer.OPERATOR_ID, 259 mServiceState.getOperatorNumeric()); 260 serviceState.put( 261 TelephonyConstants.ServiceStateContainer.IS_MANUAL_NW_SELECTION, 262 mServiceState.getIsManualSelection()); 263 serviceState.put( 264 TelephonyConstants.ServiceStateContainer.ROAMING, 265 mServiceState.getRoaming()); 266 serviceState.put( 267 TelephonyConstants.ServiceStateContainer.IS_EMERGENCY_ONLY, 268 mServiceState.isEmergencyOnly()); 269 serviceState.put( 270 TelephonyConstants.ServiceStateContainer.NETWORK_ID, 271 mServiceState.getNetworkId()); 272 serviceState.put( 273 TelephonyConstants.ServiceStateContainer.SYSTEM_ID, 274 mServiceState.getSystemId()); 275 serviceState.put( 276 TelephonyConstants.ServiceStateContainer.SERVICE_STATE, 277 mServiceStateString); 278 279 return serviceState; 280 } 281 } 282 283 public static class MessageWaitingIndicatorEvent implements JsonSerializable { 284 private boolean mMessageWaitingIndicator; 285 286 MessageWaitingIndicatorEvent(boolean messageWaitingIndicator) { 287 mMessageWaitingIndicator = messageWaitingIndicator; 288 } 289 290 public boolean getMessageWaitingIndicator() { 291 return mMessageWaitingIndicator; 292 } 293 294 public JSONObject toJSON() throws JSONException { 295 JSONObject messageWaitingIndicator = new JSONObject(); 296 297 messageWaitingIndicator.put( 298 TelephonyConstants.MessageWaitingIndicatorContainer.IS_MESSAGE_WAITING, 299 mMessageWaitingIndicator); 300 301 return messageWaitingIndicator; 302 } 303 } 304 } 305