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