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