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