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 com.googlecode.android_scripting.facade.EventFacade; 20 import android.os.Looper; 21 import android.telephony.CellInfo; 22 import android.telephony.DataConnectionRealTimeInfo; 23 import android.telephony.PhoneStateListener; 24 import android.telephony.PreciseCallState; 25 import android.telephony.ServiceState; 26 import android.telephony.SignalStrength; 27 import android.telephony.SubscriptionManager; 28 import android.telephony.TelephonyManager; 29 import android.telephony.VoLteServiceState; 30 31 import java.util.List; 32 33 /** 34 * Store all subclasses of PhoneStateListener here. 35 */ 36 public class TelephonyStateListeners { 37 38 public static class CallStateChangeListener extends PhoneStateListener { 39 40 private final EventFacade mEventFacade; 41 public static final int sListeningStates = PhoneStateListener.LISTEN_CALL_STATE | 42 PhoneStateListener.LISTEN_PRECISE_CALL_STATE; 43 44 public boolean listenForeground = true; 45 public boolean listenRinging = false; 46 public boolean listenBackground = false; 47 public int subscriptionId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; 48 CallStateChangeListener(EventFacade ef)49 public CallStateChangeListener(EventFacade ef) { 50 super(); 51 mEventFacade = ef; 52 subscriptionId = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID; 53 } 54 CallStateChangeListener(EventFacade ef, int subId)55 public CallStateChangeListener(EventFacade ef, int subId) { 56 super(subId); 57 mEventFacade = ef; 58 subscriptionId = subId; 59 } 60 CallStateChangeListener(EventFacade ef, int subId, Looper looper)61 public CallStateChangeListener(EventFacade ef, int subId, Looper looper) { 62 super(subId, looper); 63 mEventFacade = ef; 64 subscriptionId = subId; 65 } 66 67 @Override onCallStateChanged(int state, String incomingNumber)68 public void onCallStateChanged(int state, String incomingNumber) { 69 mEventFacade.postEvent(TelephonyConstants.EventCallStateChanged, 70 new TelephonyEvents.CallStateEvent( 71 state, incomingNumber, subscriptionId)); 72 } 73 74 @Override onPreciseCallStateChanged(PreciseCallState callState)75 public void onPreciseCallStateChanged(PreciseCallState callState) { 76 int foregroundState = callState.getForegroundCallState(); 77 int ringingState = callState.getRingingCallState(); 78 int backgroundState = callState.getBackgroundCallState(); 79 if (listenForeground && 80 foregroundState != PreciseCallState.PRECISE_CALL_STATE_NOT_VALID) { 81 processCallState(foregroundState, 82 TelephonyConstants.PRECISE_CALL_STATE_LISTEN_LEVEL_FOREGROUND, 83 callState); 84 } 85 if (listenRinging && 86 ringingState != PreciseCallState.PRECISE_CALL_STATE_NOT_VALID) { 87 processCallState(ringingState, 88 TelephonyConstants.PRECISE_CALL_STATE_LISTEN_LEVEL_RINGING, 89 callState); 90 } 91 if (listenBackground && 92 backgroundState != PreciseCallState.PRECISE_CALL_STATE_NOT_VALID) { 93 processCallState(backgroundState, 94 TelephonyConstants.PRECISE_CALL_STATE_LISTEN_LEVEL_BACKGROUND, 95 callState); 96 } 97 } 98 processCallState( int newState, String which, PreciseCallState callState)99 private void processCallState( 100 int newState, String which, PreciseCallState callState) { 101 mEventFacade.postEvent(TelephonyConstants.EventPreciseStateChanged, 102 new TelephonyEvents.PreciseCallStateEvent( 103 newState, which, callState, subscriptionId)); 104 } 105 } 106 107 public static class DataConnectionRealTimeInfoChangeListener extends PhoneStateListener { 108 109 private final EventFacade mEventFacade; 110 public static final int sListeningStates = 111 PhoneStateListener.LISTEN_DATA_CONNECTION_REAL_TIME_INFO; 112 public int subscriptionId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; 113 DataConnectionRealTimeInfoChangeListener(EventFacade ef)114 public DataConnectionRealTimeInfoChangeListener(EventFacade ef) { 115 super(); 116 mEventFacade = ef; 117 subscriptionId = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID; 118 } 119 DataConnectionRealTimeInfoChangeListener(EventFacade ef, int subId)120 public DataConnectionRealTimeInfoChangeListener(EventFacade ef, int subId) { 121 super(subId); 122 mEventFacade = ef; 123 subscriptionId = subId; 124 } 125 DataConnectionRealTimeInfoChangeListener(EventFacade ef, int subId, Looper looper)126 public DataConnectionRealTimeInfoChangeListener(EventFacade ef, int subId, Looper looper) { 127 super(subId, looper); 128 mEventFacade = ef; 129 subscriptionId = subId; 130 } 131 132 @Override onDataConnectionRealTimeInfoChanged( DataConnectionRealTimeInfo dcRtInfo)133 public void onDataConnectionRealTimeInfoChanged( 134 DataConnectionRealTimeInfo dcRtInfo) { 135 mEventFacade.postEvent( 136 TelephonyConstants.EventDataConnectionRealTimeInfoChanged, 137 new TelephonyEvents.DataConnectionRealTimeInfoEvent( 138 dcRtInfo, subscriptionId)); 139 } 140 } 141 142 public static class DataConnectionStateChangeListener extends PhoneStateListener { 143 144 private final EventFacade mEventFacade; 145 private final TelephonyManager mTelephonyManager; 146 public static final int sListeningStates = 147 PhoneStateListener.LISTEN_DATA_CONNECTION_STATE; 148 public int subscriptionId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; 149 DataConnectionStateChangeListener(EventFacade ef, TelephonyManager tm)150 public DataConnectionStateChangeListener(EventFacade ef, TelephonyManager tm) { 151 super(); 152 mEventFacade = ef; 153 mTelephonyManager = tm; 154 subscriptionId = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID; 155 } 156 DataConnectionStateChangeListener(EventFacade ef, TelephonyManager tm, int subId)157 public DataConnectionStateChangeListener(EventFacade ef, TelephonyManager tm, int subId) { 158 super(subId); 159 mEventFacade = ef; 160 mTelephonyManager = tm; 161 subscriptionId = subId; 162 } 163 DataConnectionStateChangeListener( EventFacade ef, TelephonyManager tm, int subId, Looper looper)164 public DataConnectionStateChangeListener( 165 EventFacade ef, TelephonyManager tm, int subId, Looper looper) { 166 super(subId, looper); 167 mEventFacade = ef; 168 mTelephonyManager = tm; 169 subscriptionId = subId; 170 } 171 172 @Override onDataConnectionStateChanged(int state)173 public void onDataConnectionStateChanged(int state) { 174 mEventFacade.postEvent( 175 TelephonyConstants.EventDataConnectionStateChanged, 176 new TelephonyEvents.DataConnectionStateEvent(state, 177 TelephonyUtils.getNetworkTypeString( 178 mTelephonyManager.getDataNetworkType()), 179 subscriptionId)); 180 } 181 } 182 183 public static class ServiceStateChangeListener extends PhoneStateListener { 184 185 private final EventFacade mEventFacade; 186 public static final int sListeningStates = PhoneStateListener.LISTEN_SERVICE_STATE; 187 public int subscriptionId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; 188 ServiceStateChangeListener(EventFacade ef)189 public ServiceStateChangeListener(EventFacade ef) { 190 super(); 191 mEventFacade = ef; 192 subscriptionId = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID; 193 } 194 ServiceStateChangeListener(EventFacade ef, int subId)195 public ServiceStateChangeListener(EventFacade ef, int subId) { 196 super(subId); 197 mEventFacade = ef; 198 subscriptionId = subId; 199 } 200 ServiceStateChangeListener(EventFacade ef, int subId, Looper looper)201 public ServiceStateChangeListener(EventFacade ef, int subId, Looper looper) { 202 super(subId, looper); 203 mEventFacade = ef; 204 subscriptionId = subId; 205 } 206 207 @Override onServiceStateChanged(ServiceState serviceState)208 public void onServiceStateChanged(ServiceState serviceState) { 209 mEventFacade.postEvent(TelephonyConstants.EventServiceStateChanged, 210 new TelephonyEvents.ServiceStateEvent( 211 serviceState, subscriptionId)); 212 } 213 214 } 215 216 public static class CellInfoChangeListener 217 extends PhoneStateListener { 218 219 private final EventFacade mEventFacade; 220 CellInfoChangeListener(EventFacade ef)221 public CellInfoChangeListener(EventFacade ef) { 222 super(); 223 mEventFacade = ef; 224 } 225 CellInfoChangeListener(EventFacade ef, int subId)226 public CellInfoChangeListener(EventFacade ef, int subId) { 227 super(subId); 228 mEventFacade = ef; 229 } 230 CellInfoChangeListener(EventFacade ef, int subId, Looper looper)231 public CellInfoChangeListener(EventFacade ef, int subId, Looper looper) { 232 super(subId, looper); 233 mEventFacade = ef; 234 } 235 236 @Override onCellInfoChanged(List<CellInfo> cellInfo)237 public void onCellInfoChanged(List<CellInfo> cellInfo) { 238 mEventFacade.postEvent( 239 TelephonyConstants.EventCellInfoChanged, cellInfo); 240 } 241 } 242 243 public static class VolteServiceStateChangeListener 244 extends PhoneStateListener { 245 246 private final EventFacade mEventFacade; 247 VolteServiceStateChangeListener(EventFacade ef)248 public VolteServiceStateChangeListener(EventFacade ef) { 249 super(); 250 mEventFacade = ef; 251 } 252 VolteServiceStateChangeListener(EventFacade ef, int subId)253 public VolteServiceStateChangeListener(EventFacade ef, int subId) { 254 super(subId); 255 mEventFacade = ef; 256 } 257 VolteServiceStateChangeListener(EventFacade ef, int subId, Looper looper)258 public VolteServiceStateChangeListener(EventFacade ef, int subId, Looper looper) { 259 super(subId, looper); 260 mEventFacade = ef; 261 } 262 263 @Override onVoLteServiceStateChanged(VoLteServiceState volteInfo)264 public void onVoLteServiceStateChanged(VoLteServiceState volteInfo) { 265 mEventFacade.postEvent( 266 TelephonyConstants.EventVolteServiceStateChanged, 267 volteInfo); 268 } 269 } 270 271 public static class VoiceMailStateChangeListener extends PhoneStateListener { 272 273 private final EventFacade mEventFacade; 274 275 public static final int sListeningStates = 276 PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR; 277 VoiceMailStateChangeListener(EventFacade ef)278 public VoiceMailStateChangeListener(EventFacade ef) { 279 super(); 280 mEventFacade = ef; 281 } 282 VoiceMailStateChangeListener(EventFacade ef, int subId)283 public VoiceMailStateChangeListener(EventFacade ef, int subId) { 284 super(subId); 285 mEventFacade = ef; 286 } 287 VoiceMailStateChangeListener(EventFacade ef, int subId, Looper looper)288 public VoiceMailStateChangeListener(EventFacade ef, int subId, Looper looper) { 289 super(subId, looper); 290 mEventFacade = ef; 291 } 292 293 @Override onMessageWaitingIndicatorChanged(boolean messageWaitingIndicator)294 public void onMessageWaitingIndicatorChanged(boolean messageWaitingIndicator) { 295 mEventFacade.postEvent( 296 TelephonyConstants.EventMessageWaitingIndicatorChanged, 297 new TelephonyEvents.MessageWaitingIndicatorEvent( 298 messageWaitingIndicator)); 299 } 300 } 301 302 303 public static class SignalStrengthChangeListener extends PhoneStateListener { 304 305 private final EventFacade mEventFacade; 306 public SignalStrength mSignalStrengths; 307 public static final int sListeningStates = PhoneStateListener.LISTEN_SIGNAL_STRENGTHS; SignalStrengthChangeListener(EventFacade ef)308 public SignalStrengthChangeListener(EventFacade ef) { 309 super(); 310 mEventFacade = ef; 311 } 312 SignalStrengthChangeListener(EventFacade ef, int subId)313 public SignalStrengthChangeListener(EventFacade ef, int subId) { 314 super(subId); 315 mEventFacade = ef; 316 } 317 SignalStrengthChangeListener(EventFacade ef, int subId, Looper looper)318 public SignalStrengthChangeListener(EventFacade ef, int subId, Looper looper) { 319 super(subId, looper); 320 mEventFacade = ef; 321 } 322 323 @Override onSignalStrengthsChanged(SignalStrength signalStrength)324 public void onSignalStrengthsChanged(SignalStrength signalStrength) { 325 mSignalStrengths = signalStrength; 326 mEventFacade.postEvent( 327 TelephonyConstants.EventSignalStrengthChanged, signalStrength); 328 } 329 } 330 331 } 332