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