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