TelephonyRegistryMock.java revision 7a477263108748903cf5a4151a4e8b739f12264a
1/* 2 * Copyright (C) 2006 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 17package com.android.internal.telephony.mocks; 18 19import android.content.Context; 20import android.content.Intent; 21import android.net.LinkProperties; 22import android.net.NetworkCapabilities; 23import android.os.Bundle; 24import android.os.IBinder; 25import android.os.RemoteException; 26import android.os.UserHandle; 27import android.telephony.CellInfo; 28import android.telephony.DataConnectionRealTimeInfo; 29import android.telephony.ServiceState; 30import android.telephony.SignalStrength; 31import android.telephony.SubscriptionManager; 32import android.telephony.VoLteServiceState; 33import com.android.internal.telephony.IPhoneStateListener; 34import com.android.internal.telephony.IOnSubscriptionsChangedListener; 35import com.android.internal.telephony.ITelephonyRegistry; 36 37import java.util.ArrayList; 38import java.util.List; 39 40public class TelephonyRegistryMock extends ITelephonyRegistry.Stub { 41 42 private static class Record { 43 String callingPackage; 44 45 IBinder binder; 46 47 IPhoneStateListener callback; 48 IOnSubscriptionsChangedListener onSubscriptionsChangedListenerCallback; 49 50 int callerUserId; 51 52 int events; 53 54 int subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; 55 56 int phoneId = SubscriptionManager.INVALID_PHONE_INDEX; 57 58 boolean canReadPhoneState; 59 60 boolean matchPhoneStateListenerEvent(int events) { 61 return (callback != null) && ((events & this.events) != 0); 62 } 63 64 boolean matchOnSubscriptionsChangedListener() { 65 return (onSubscriptionsChangedListenerCallback != null); 66 } 67 68 @Override 69 public String toString() { 70 return "{callingPackage=" + callingPackage + " binder=" + binder 71 + " callback=" + callback 72 + " onSubscriptionsChangedListenererCallback=" 73 + onSubscriptionsChangedListenerCallback 74 + " callerUserId=" + callerUserId + " subId=" + subId + " phoneId=" + phoneId 75 + " events=" + Integer.toHexString(events) 76 + " canReadPhoneState=" + canReadPhoneState + "}"; 77 } 78 } 79 80 private final ArrayList<IBinder> mRemoveList = new ArrayList<IBinder>(); 81 private final ArrayList<Record> mRecords = new ArrayList<Record>(); 82 private boolean hasNotifySubscriptionInfoChangedOccurred = false; 83 84 public TelephonyRegistryMock() { 85 } 86 87 private void remove(IBinder binder) { 88 synchronized (mRecords) { 89 final int recordCount = mRecords.size(); 90 for (int i = 0; i < recordCount; i++) { 91 if (mRecords.get(i).binder == binder) { 92 mRecords.remove(i); 93 return; 94 } 95 } 96 } 97 } 98 99 private void handleRemoveListLocked() { 100 int size = mRemoveList.size(); 101 if (size > 0) { 102 for (IBinder b: mRemoveList) { 103 remove(b); 104 } 105 mRemoveList.clear(); 106 } 107 } 108 109 110 @Override 111 public void addOnSubscriptionsChangedListener(String callingPackage, 112 IOnSubscriptionsChangedListener callback) { 113 Record r; 114 115 synchronized (mRecords) { 116 // register 117 find_and_add: { 118 IBinder b = callback.asBinder(); 119 final int N = mRecords.size(); 120 for (int i = 0; i < N; i++) { 121 r = mRecords.get(i); 122 if (b == r.binder) { 123 break find_and_add; 124 } 125 } 126 r = new Record(); 127 r.binder = b; 128 mRecords.add(r); 129 } 130 131 r.onSubscriptionsChangedListenerCallback = callback; 132 r.callingPackage = callingPackage; 133 r.callerUserId = UserHandle.getCallingUserId(); 134 r.events = 0; 135 r.canReadPhoneState = true; // permission has been enforced above 136 // Always notify when registration occurs if there has been a notification. 137 if (hasNotifySubscriptionInfoChangedOccurred) { 138 try { 139 r.onSubscriptionsChangedListenerCallback.onSubscriptionsChanged(); 140 } catch (RemoteException e) { 141 remove(r.binder); 142 } 143 } else { 144 //log("listen oscl: hasNotifySubscriptionInfoChangedOccurred==false no callback"); 145 } 146 } 147 148 } 149 150 @Override 151 public void removeOnSubscriptionsChangedListener(String pkgForDebug, 152 IOnSubscriptionsChangedListener callback) { 153 remove(callback.asBinder()); 154 } 155 156 @Override 157 public void notifySubscriptionInfoChanged() { 158 synchronized (mRecords) { 159 if (!hasNotifySubscriptionInfoChangedOccurred) { 160 //log("notifySubscriptionInfoChanged: first invocation mRecords.size=" 161 // + mRecords.size()); 162 } 163 hasNotifySubscriptionInfoChangedOccurred = true; 164 mRemoveList.clear(); 165 for (Record r : mRecords) { 166 if (r.matchOnSubscriptionsChangedListener()) { 167 try { 168 r.onSubscriptionsChangedListenerCallback.onSubscriptionsChanged(); 169 } catch (RemoteException ex) { 170 mRemoveList.add(r.binder); 171 } 172 } 173 } 174 handleRemoveListLocked(); 175 } 176 } 177 178 @Override 179 public void listen(String pkg, IPhoneStateListener callback, int events, boolean notifyNow) { 180 throw new RuntimeException("Not implemented"); 181 } 182 183 @Override 184 public void listenForSubscriber(int subId, String pkg, IPhoneStateListener callback, int events, 185 boolean notifyNow) { 186 throw new RuntimeException("Not implemented"); 187 } 188 189 @Override 190 public void notifyCallState(int state, String incomingNumber) { 191 throw new RuntimeException("Not implemented"); 192 } 193 194 @Override 195 public void notifyCallStateForSubscriber(int subId, int state, String incomingNumber) { 196 throw new RuntimeException("Not implemented"); 197 } 198 199 @Override 200 public void notifyServiceStateForPhoneId(int phoneId, int subId, ServiceState state) { 201 throw new RuntimeException("Not implemented"); 202 } 203 204 @Override 205 public void notifySignalStrength(SignalStrength signalStrength) { 206 throw new RuntimeException("Not implemented"); 207 } 208 209 @Override 210 public void notifySignalStrengthForSubscriber(int subId, SignalStrength signalStrength) { 211 throw new RuntimeException("Not implemented"); 212 } 213 214 @Override 215 public void notifyMessageWaitingChangedForPhoneId(int phoneId, int subId, boolean mwi) { 216 throw new RuntimeException("Not implemented"); 217 } 218 219 @Override 220 public void notifyCallForwardingChanged(boolean cfi) { 221 throw new RuntimeException("Not implemented"); 222 } 223 224 @Override 225 public void notifyCallForwardingChangedForSubscriber(int subId, boolean cfi) { 226 throw new RuntimeException("Not implemented"); 227 } 228 229 @Override 230 public void notifyDataActivity(int state) { 231 throw new RuntimeException("Not implemented"); 232 } 233 234 @Override 235 public void notifyDataActivityForSubscriber(int subId, int state) { 236 throw new RuntimeException("Not implemented"); 237 } 238 239 @Override 240 public void notifyDataConnection(int state, boolean isDataConnectivityPossible, 241 String reason, String apn, String apnType, LinkProperties linkProperties, 242 NetworkCapabilities networkCapabilities, int networkType, boolean roaming) { 243 throw new RuntimeException("Not implemented"); 244 } 245 246 @Override 247 public void notifyDataConnectionForSubscriber(int subId, int state, 248 boolean isDataConnectivityPossible, String reason, String apn, String apnType, 249 LinkProperties linkProperties, NetworkCapabilities networkCapabilities, 250 int networkType, boolean roaming) { 251 throw new RuntimeException("Not implemented"); 252 } 253 254 @Override 255 public void notifyDataConnectionFailed(String reason, String apnType) { 256 throw new RuntimeException("Not implemented"); 257 } 258 259 @Override 260 public void notifyDataConnectionFailedForSubscriber(int subId, String reason, String apnType) { 261 throw new RuntimeException("Not implemented"); 262 } 263 264 @Override 265 public void notifyCellLocation(Bundle cellLocation) { 266 throw new RuntimeException("Not implemented"); 267 } 268 269 @Override 270 public void notifyCellLocationForSubscriber(int subId, Bundle cellLocation) { 271 throw new RuntimeException("Not implemented"); 272 } 273 274 @Override 275 public void notifyOtaspChanged(int otaspMode) { 276 throw new RuntimeException("Not implemented"); 277 } 278 279 @Override 280 public void notifyCellInfo(List<CellInfo> cellInfo) { 281 throw new RuntimeException("Not implemented"); 282 } 283 284 @Override 285 public void notifyPreciseCallState(int ringingCallState, int foregroundCallState, 286 int backgroundCallState) { 287 throw new RuntimeException("Not implemented"); 288 } 289 290 @Override 291 public void notifyDisconnectCause(int disconnectCause, int preciseDisconnectCause) { 292 throw new RuntimeException("Not implemented"); 293 } 294 295 @Override 296 public void notifyPreciseDataConnectionFailed(String reason, String apnType, String apn, 297 String failCause) { 298 throw new RuntimeException("Not implemented"); 299 } 300 301 @Override 302 public void notifyCellInfoForSubscriber(int subId, List<CellInfo> cellInfo) { 303 throw new RuntimeException("Not implemented"); 304 } 305 306 @Override 307 public void notifyVoLteServiceStateChanged(VoLteServiceState lteState) { 308 throw new RuntimeException("Not implemented"); 309 } 310 311 @Override 312 public void notifyOemHookRawEventForSubscriber(int subId, byte[] rawData) { 313 throw new RuntimeException("Not implemented"); 314 } 315 316 @Override 317 public void notifyCarrierNetworkChange(boolean active) { 318 throw new RuntimeException("Not implemented"); 319 } 320} 321