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.PhysicalChannelConfig; 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 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 50 public CallStateChangeListener(EventFacade ef) { 51 super(); 52 mEventFacade = ef; 53 subscriptionId = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID; 54 } 55 56 public CallStateChangeListener(EventFacade ef, int subId) { 57 super(); 58 mEventFacade = ef; 59 subscriptionId = subId; 60 } 61 62 public CallStateChangeListener(EventFacade ef, int subId, Looper looper) { 63 super(looper); 64 mEventFacade = ef; 65 subscriptionId = subId; 66 } 67 68 @Override 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 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 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 115 public DataConnectionRealTimeInfoChangeListener(EventFacade ef) { 116 super(); 117 mEventFacade = ef; 118 subscriptionId = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID; 119 } 120 121 public DataConnectionRealTimeInfoChangeListener(EventFacade ef, int subId) { 122 super(); 123 mEventFacade = ef; 124 subscriptionId = subId; 125 } 126 127 public DataConnectionRealTimeInfoChangeListener(EventFacade ef, int subId, Looper looper) { 128 super(looper); 129 mEventFacade = ef; 130 subscriptionId = subId; 131 } 132 133 @Override 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 151 public DataConnectionStateChangeListener(EventFacade ef, TelephonyManager tm) { 152 super(); 153 mEventFacade = ef; 154 mTelephonyManager = tm; 155 subscriptionId = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID; 156 } 157 158 public DataConnectionStateChangeListener(EventFacade ef, TelephonyManager tm, int subId) { 159 super(); 160 mEventFacade = ef; 161 mTelephonyManager = tm; 162 subscriptionId = subId; 163 } 164 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 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 192 public ActiveDataSubIdChangeListener(EventFacade ef, TelephonyManager tm) { 193 super(); 194 mEventFacade = ef; 195 mTelephonyManager = tm; 196 subscriptionId = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID; 197 } 198 199 public ActiveDataSubIdChangeListener(EventFacade ef, TelephonyManager tm, int subId) { 200 super(); 201 mEventFacade = ef; 202 mTelephonyManager = tm; 203 subscriptionId = subId; 204 } 205 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 215 public void onActiveDataSubscriptionIdChanged(int subId) { 216 mEventFacade.postEvent( 217 TelephonyConstants.EventActiveDataSubIdChanged, subId); 218 } 219 } 220 221 public static class ServiceStateChangeListener extends PhoneStateListener { 222 223 private final EventFacade mEventFacade; 224 public static final int sListeningStates = PhoneStateListener.LISTEN_SERVICE_STATE; 225 public int subscriptionId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; 226 227 public ServiceStateChangeListener(EventFacade ef) { 228 super(); 229 mEventFacade = ef; 230 subscriptionId = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID; 231 } 232 233 public ServiceStateChangeListener(EventFacade ef, int subId) { 234 super(); 235 mEventFacade = ef; 236 subscriptionId = subId; 237 } 238 239 public ServiceStateChangeListener(EventFacade ef, int subId, Looper looper) { 240 super(looper); 241 mEventFacade = ef; 242 subscriptionId = subId; 243 } 244 245 @Override 246 public void onServiceStateChanged(ServiceState serviceState) { 247 mEventFacade.postEvent(TelephonyConstants.EventServiceStateChanged, 248 new TelephonyEvents.ServiceStateEvent( 249 serviceState, subscriptionId)); 250 } 251 252 } 253 254 public static class CellInfoChangeListener 255 extends PhoneStateListener { 256 257 private final EventFacade mEventFacade; 258 259 public CellInfoChangeListener(EventFacade ef) { 260 super(); 261 mEventFacade = ef; 262 } 263 264 public CellInfoChangeListener(EventFacade ef, int subId) { 265 super(); 266 mEventFacade = ef; 267 } 268 269 public CellInfoChangeListener(EventFacade ef, int subId, Looper looper) { 270 super(looper); 271 mEventFacade = ef; 272 } 273 274 @Override 275 public void onCellInfoChanged(List<CellInfo> cellInfo) { 276 mEventFacade.postEvent( 277 TelephonyConstants.EventCellInfoChanged, cellInfo); 278 } 279 } 280 281 public static class SrvccStateChangeListener 282 extends PhoneStateListener { 283 284 private final EventFacade mEventFacade; 285 286 public SrvccStateChangeListener(EventFacade ef) { 287 super(); 288 mEventFacade = ef; 289 } 290 291 public SrvccStateChangeListener(EventFacade ef, int subId) { 292 super(); 293 mEventFacade = ef; 294 } 295 296 public SrvccStateChangeListener(EventFacade ef, int subId, Looper looper) { 297 super(looper); 298 mEventFacade = ef; 299 } 300 301 @Override 302 public void onSrvccStateChanged(int srvccState) { 303 mEventFacade.postEvent( 304 TelephonyConstants.EventSrvccStateChanged, 305 srvccState); 306 } 307 } 308 309 public static class VoiceMailStateChangeListener extends PhoneStateListener { 310 311 private final EventFacade mEventFacade; 312 313 public static final int sListeningStates = 314 PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR; 315 316 public VoiceMailStateChangeListener(EventFacade ef) { 317 super(); 318 mEventFacade = ef; 319 } 320 321 public VoiceMailStateChangeListener(EventFacade ef, int subId) { 322 super(); 323 mEventFacade = ef; 324 } 325 326 public VoiceMailStateChangeListener(EventFacade ef, int subId, Looper looper) { 327 super(looper); 328 mEventFacade = ef; 329 } 330 331 @Override 332 public void onMessageWaitingIndicatorChanged(boolean messageWaitingIndicator) { 333 mEventFacade.postEvent( 334 TelephonyConstants.EventMessageWaitingIndicatorChanged, 335 new TelephonyEvents.MessageWaitingIndicatorEvent( 336 messageWaitingIndicator)); 337 } 338 } 339 340 341 public static class SignalStrengthChangeListener extends PhoneStateListener { 342 343 private final EventFacade mEventFacade; 344 public SignalStrength mSignalStrengths; 345 public static final int sListeningStates = PhoneStateListener.LISTEN_SIGNAL_STRENGTHS; 346 public SignalStrengthChangeListener(EventFacade ef) { 347 super(); 348 mEventFacade = ef; 349 } 350 351 public SignalStrengthChangeListener(EventFacade ef, int subId) { 352 super(); 353 mEventFacade = ef; 354 } 355 356 public SignalStrengthChangeListener(EventFacade ef, int subId, Looper looper) { 357 super(looper); 358 mEventFacade = ef; 359 } 360 361 @Override 362 public void onSignalStrengthsChanged(SignalStrength signalStrength) { 363 mSignalStrengths = signalStrength; 364 mEventFacade.postEvent( 365 TelephonyConstants.EventSignalStrengthChanged, signalStrength); 366 } 367 } 368 369 public static class PhysicalChannelConfigurationChangeListener extends PhoneStateListener { 370 371 private final EventFacade mEventFacade; 372 public List<PhysicalChannelConfig> mConfigs; 373 public static final int sListeningStates = 374 PhoneStateListener.LISTEN_PHYSICAL_CHANNEL_CONFIGURATION; 375 public PhysicalChannelConfigurationChangeListener(EventFacade ef) { 376 super(); 377 mEventFacade = ef; 378 } 379 380 public PhysicalChannelConfigurationChangeListener(EventFacade ef, int subId) { 381 super(); 382 mEventFacade = ef; 383 } 384 385 public PhysicalChannelConfigurationChangeListener( 386 EventFacade ef, int subId, Looper looper) { 387 super(looper); 388 mEventFacade = ef; 389 } 390 391 @Override 392 public void onPhysicalChannelConfigurationChanged(List<PhysicalChannelConfig> config) { 393 mEventFacade.postEvent( 394 TelephonyConstants.EventPhysicalChannelConfigChanged, config); 395 } 396 } 397 398 } 399