blob: 087e8db2deeb121000cd7651dfb26900ff2e0071 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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.server;
18
19import android.content.Context;
20import android.content.Intent;
21import android.content.pm.PackageManager;
Wink Savillef61101f2010-09-16 16:36:42 -070022import android.net.LinkCapabilities;
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070023import android.net.LinkProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.os.Binder;
25import android.os.Bundle;
26import android.os.IBinder;
27import android.os.RemoteException;
28import android.telephony.CellLocation;
29import android.telephony.PhoneStateListener;
30import android.telephony.ServiceState;
Wink Savillee9b06d72009-05-18 21:47:50 -070031import android.telephony.SignalStrength;
John Wang963db55d2012-03-30 16:04:06 -070032import android.telephony.CellInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.telephony.TelephonyManager;
34import android.text.TextUtils;
Joe Onorato8a9b2202010-02-26 18:56:32 -080035import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37import java.util.ArrayList;
Wink Savilleb208a242012-07-25 14:08:09 -070038import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import java.io.FileDescriptor;
40import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070041import java.net.NetworkInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43import com.android.internal.app.IBatteryStats;
44import com.android.internal.telephony.ITelephonyRegistry;
45import com.android.internal.telephony.IPhoneStateListener;
46import com.android.internal.telephony.DefaultPhoneNotifier;
47import com.android.internal.telephony.Phone;
Wink Savillea639b312012-07-10 12:37:54 -070048import com.android.internal.telephony.PhoneConstants;
Wink Savillec9330dd2011-01-12 13:37:38 -080049import com.android.internal.telephony.ServiceStateTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import com.android.internal.telephony.TelephonyIntents;
51import com.android.server.am.BatteryStatsService;
52
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053/**
Wink Savillee9b06d72009-05-18 21:47:50 -070054 * Since phone process can be restarted, this class provides a centralized place
55 * that applications can register and be called back from.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 */
57class TelephonyRegistry extends ITelephonyRegistry.Stub {
58 private static final String TAG = "TelephonyRegistry";
Wink Savillec9acde92011-09-21 11:05:43 -070059 private static final boolean DBG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
61 private static class Record {
62 String pkgForDebug;
Wink Savillee9b06d72009-05-18 21:47:50 -070063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 IBinder binder;
Wink Savillee9b06d72009-05-18 21:47:50 -070065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 IPhoneStateListener callback;
Wink Savillee9b06d72009-05-18 21:47:50 -070067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 int events;
69 }
70
71 private final Context mContext;
Wink Savillee9b06d72009-05-18 21:47:50 -070072
Joe Onorato163d8d92010-10-21 13:21:20 -040073 // access should be inside synchronized (mRecords) for these two fields
74 private final ArrayList<IBinder> mRemoveList = new ArrayList<IBinder>();
75 private final ArrayList<Record> mRecords = new ArrayList<Record>();
Wink Savillee9b06d72009-05-18 21:47:50 -070076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 private final IBatteryStats mBatteryStats;
78
79 private int mCallState = TelephonyManager.CALL_STATE_IDLE;
Wink Savillee9b06d72009-05-18 21:47:50 -070080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 private String mCallIncomingNumber = "";
Wink Savillee9b06d72009-05-18 21:47:50 -070082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 private ServiceState mServiceState = new ServiceState();
Wink Savillee9b06d72009-05-18 21:47:50 -070084
85 private SignalStrength mSignalStrength = new SignalStrength();
86
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 private boolean mMessageWaiting = false;
Wink Savillee9b06d72009-05-18 21:47:50 -070088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 private boolean mCallForwarding = false;
Wink Savillee9b06d72009-05-18 21:47:50 -070090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 private int mDataActivity = TelephonyManager.DATA_ACTIVITY_NONE;
Wink Savillee9b06d72009-05-18 21:47:50 -070092
Robert Greenwalt8e7e0a92010-11-09 10:24:40 -080093 private int mDataConnectionState = TelephonyManager.DATA_UNKNOWN;
Wink Savillee9b06d72009-05-18 21:47:50 -070094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 private boolean mDataConnectionPossible = false;
Wink Savillee9b06d72009-05-18 21:47:50 -070096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 private String mDataConnectionReason = "";
Wink Savillee9b06d72009-05-18 21:47:50 -070098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 private String mDataConnectionApn = "";
Wink Savillee9b06d72009-05-18 21:47:50 -0700100
Robert Greenwalt02648a42010-05-18 10:52:51 -0700101 private ArrayList<String> mConnectedApns;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700102
Wink Savillef61101f2010-09-16 16:36:42 -0700103 private LinkProperties mDataConnectionLinkProperties;
104
105 private LinkCapabilities mDataConnectionLinkCapabilities;
Wink Savillee9b06d72009-05-18 21:47:50 -0700106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 private Bundle mCellLocation = new Bundle();
108
Robert Greenwalt98e0b142009-10-08 21:15:52 -0700109 private int mDataConnectionNetworkType;
110
Wink Savillec9330dd2011-01-12 13:37:38 -0800111 private int mOtaspMode = ServiceStateTracker.OTASP_UNKNOWN;
Wink Savillefd2d0132010-10-28 14:22:26 -0700112
Wink Savilleb208a242012-07-25 14:08:09 -0700113 private List<CellInfo> mCellInfo = null;
John Wang963db55d2012-03-30 16:04:06 -0700114
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700115 static final int PHONE_STATE_PERMISSION_MASK =
116 PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR |
117 PhoneStateListener.LISTEN_CALL_STATE |
118 PhoneStateListener.LISTEN_DATA_ACTIVITY |
119 PhoneStateListener.LISTEN_DATA_CONNECTION_STATE |
120 PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR;
121
Wink Savillee9b06d72009-05-18 21:47:50 -0700122 // we keep a copy of all of the state so we can send it out when folks
123 // register for it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 //
Wink Savillee9b06d72009-05-18 21:47:50 -0700125 // In these calls we call with the lock held. This is safe becasuse remote
126 // calls go through a oneway interface and local calls going through a
127 // handler before they get to app code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128
129 TelephonyRegistry(Context context) {
David 'Digit' Turner4ef8ec32009-09-25 11:33:24 -0700130 CellLocation location = CellLocation.getEmpty();
131
132 // Note that location can be null for non-phone builds like
133 // like the generic one.
134 if (location != null) {
135 location.fillInNotifierBundle(mCellLocation);
136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 mContext = context;
138 mBatteryStats = BatteryStatsService.getService();
Robert Greenwalt02648a42010-05-18 10:52:51 -0700139 mConnectedApns = new ArrayList<String>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 }
141
142 public void listen(String pkgForDebug, IPhoneStateListener callback, int events,
143 boolean notifyNow) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800144 // Slog.d(TAG, "listen pkg=" + pkgForDebug + " events=0x" +
Wink Savillee9b06d72009-05-18 21:47:50 -0700145 // Integer.toHexString(events));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 if (events != 0) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700147 /* Checks permission and throws Security exception */
148 checkListenerPermission(events);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
150 synchronized (mRecords) {
151 // register
152 Record r = null;
153 find_and_add: {
154 IBinder b = callback.asBinder();
155 final int N = mRecords.size();
Wink Savillee9b06d72009-05-18 21:47:50 -0700156 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 r = mRecords.get(i);
158 if (b == r.binder) {
159 break find_and_add;
160 }
161 }
162 r = new Record();
163 r.binder = b;
164 r.callback = callback;
165 r.pkgForDebug = pkgForDebug;
166 mRecords.add(r);
167 }
168 int send = events & (events ^ r.events);
169 r.events = events;
170 if (notifyNow) {
171 if ((events & PhoneStateListener.LISTEN_SERVICE_STATE) != 0) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400172 try {
173 r.callback.onServiceStateChanged(new ServiceState(mServiceState));
174 } catch (RemoteException ex) {
175 remove(r.binder);
176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 }
178 if ((events & PhoneStateListener.LISTEN_SIGNAL_STRENGTH) != 0) {
179 try {
Wink Savillee9b06d72009-05-18 21:47:50 -0700180 int gsmSignalStrength = mSignalStrength.getGsmSignalStrength();
181 r.callback.onSignalStrengthChanged((gsmSignalStrength == 99 ? -1
182 : gsmSignalStrength));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 } catch (RemoteException ex) {
184 remove(r.binder);
185 }
186 }
187 if ((events & PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR) != 0) {
188 try {
189 r.callback.onMessageWaitingIndicatorChanged(mMessageWaiting);
190 } catch (RemoteException ex) {
191 remove(r.binder);
192 }
193 }
194 if ((events & PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR) != 0) {
195 try {
196 r.callback.onCallForwardingIndicatorChanged(mCallForwarding);
197 } catch (RemoteException ex) {
198 remove(r.binder);
199 }
200 }
201 if ((events & PhoneStateListener.LISTEN_CELL_LOCATION) != 0) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400202 try {
203 r.callback.onCellLocationChanged(new Bundle(mCellLocation));
204 } catch (RemoteException ex) {
205 remove(r.binder);
206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 }
208 if ((events & PhoneStateListener.LISTEN_CALL_STATE) != 0) {
209 try {
210 r.callback.onCallStateChanged(mCallState, mCallIncomingNumber);
211 } catch (RemoteException ex) {
212 remove(r.binder);
213 }
214 }
215 if ((events & PhoneStateListener.LISTEN_DATA_CONNECTION_STATE) != 0) {
216 try {
Robert Greenwalt98e0b142009-10-08 21:15:52 -0700217 r.callback.onDataConnectionStateChanged(mDataConnectionState,
218 mDataConnectionNetworkType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 } catch (RemoteException ex) {
220 remove(r.binder);
221 }
222 }
223 if ((events & PhoneStateListener.LISTEN_DATA_ACTIVITY) != 0) {
224 try {
225 r.callback.onDataActivity(mDataActivity);
226 } catch (RemoteException ex) {
227 remove(r.binder);
228 }
229 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700230 if ((events & PhoneStateListener.LISTEN_SIGNAL_STRENGTHS) != 0) {
231 try {
232 r.callback.onSignalStrengthsChanged(mSignalStrength);
233 } catch (RemoteException ex) {
234 remove(r.binder);
235 }
236 }
Wink Savillefd2d0132010-10-28 14:22:26 -0700237 if ((events & PhoneStateListener.LISTEN_OTASP_CHANGED) != 0) {
238 try {
239 r.callback.onOtaspChanged(mOtaspMode);
240 } catch (RemoteException ex) {
241 remove(r.binder);
242 }
243 }
John Wang963db55d2012-03-30 16:04:06 -0700244 if ((events & PhoneStateListener.LISTEN_CELL_INFO) != 0) {
245 try {
Wink Savilleb208a242012-07-25 14:08:09 -0700246 r.callback.onCellInfoChanged(mCellInfo);
John Wang963db55d2012-03-30 16:04:06 -0700247 } catch (RemoteException ex) {
248 remove(r.binder);
249 }
250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 }
252 }
253 } else {
254 remove(callback.asBinder());
255 }
256 }
257
258 private void remove(IBinder binder) {
259 synchronized (mRecords) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700260 final int recordCount = mRecords.size();
261 for (int i = 0; i < recordCount; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 if (mRecords.get(i).binder == binder) {
263 mRecords.remove(i);
264 return;
265 }
266 }
267 }
268 }
269
270 public void notifyCallState(int state, String incomingNumber) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700271 if (!checkNotifyPermission("notifyCallState()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700272 return;
273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 synchronized (mRecords) {
275 mCallState = state;
276 mCallIncomingNumber = incomingNumber;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700277 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 if ((r.events & PhoneStateListener.LISTEN_CALL_STATE) != 0) {
279 try {
280 r.callback.onCallStateChanged(state, incomingNumber);
281 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400282 mRemoveList.add(r.binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 }
284 }
285 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400286 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 }
288 broadcastCallStateChanged(state, incomingNumber);
289 }
290
291 public void notifyServiceState(ServiceState state) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700292 if (!checkNotifyPermission("notifyServiceState()")){
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700293 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 synchronized (mRecords) {
296 mServiceState = state;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700297 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 if ((r.events & PhoneStateListener.LISTEN_SERVICE_STATE) != 0) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400299 try {
300 r.callback.onServiceStateChanged(new ServiceState(state));
301 } catch (RemoteException ex) {
302 mRemoveList.add(r.binder);
303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 }
305 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400306 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 }
308 broadcastServiceStateChanged(state);
309 }
310
Wink Savillee9b06d72009-05-18 21:47:50 -0700311 public void notifySignalStrength(SignalStrength signalStrength) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700312 if (!checkNotifyPermission("notifySignalStrength()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700313 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 synchronized (mRecords) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700316 mSignalStrength = signalStrength;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700317 for (Record r : mRecords) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700318 if ((r.events & PhoneStateListener.LISTEN_SIGNAL_STRENGTHS) != 0) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400319 try {
320 r.callback.onSignalStrengthsChanged(new SignalStrength(signalStrength));
321 } catch (RemoteException ex) {
322 mRemoveList.add(r.binder);
323 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 if ((r.events & PhoneStateListener.LISTEN_SIGNAL_STRENGTH) != 0) {
326 try {
Wink Savillee9b06d72009-05-18 21:47:50 -0700327 int gsmSignalStrength = signalStrength.getGsmSignalStrength();
328 r.callback.onSignalStrengthChanged((gsmSignalStrength == 99 ? -1
329 : gsmSignalStrength));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400331 mRemoveList.add(r.binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 }
333 }
334 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400335 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700337 broadcastSignalStrengthChanged(signalStrength);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 }
339
Wink Savilleb208a242012-07-25 14:08:09 -0700340 public void notifyCellInfo(List<CellInfo> cellInfo) {
John Wang963db55d2012-03-30 16:04:06 -0700341 if (!checkNotifyPermission("notifyCellInfo()")) {
342 return;
343 }
344
345 synchronized (mRecords) {
346 mCellInfo = cellInfo;
347 for (Record r : mRecords) {
348 if ((r.events & PhoneStateListener.LISTEN_CELL_INFO) != 0) {
349 try {
Wink Savilleb208a242012-07-25 14:08:09 -0700350 r.callback.onCellInfoChanged(cellInfo);
John Wang963db55d2012-03-30 16:04:06 -0700351 } catch (RemoteException ex) {
352 mRemoveList.add(r.binder);
353 }
354 }
355 }
356 handleRemoveListLocked();
357 }
358 }
359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 public void notifyMessageWaitingChanged(boolean mwi) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700361 if (!checkNotifyPermission("notifyMessageWaitingChanged()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700362 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 synchronized (mRecords) {
365 mMessageWaiting = mwi;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700366 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 if ((r.events & PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR) != 0) {
368 try {
369 r.callback.onMessageWaitingIndicatorChanged(mwi);
370 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400371 mRemoveList.add(r.binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 }
373 }
374 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400375 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 }
377 }
378
379 public void notifyCallForwardingChanged(boolean cfi) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700380 if (!checkNotifyPermission("notifyCallForwardingChanged()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700381 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 synchronized (mRecords) {
384 mCallForwarding = cfi;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700385 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 if ((r.events & PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR) != 0) {
387 try {
388 r.callback.onCallForwardingIndicatorChanged(cfi);
389 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400390 mRemoveList.add(r.binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 }
392 }
393 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400394 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 }
396 }
397
398 public void notifyDataActivity(int state) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700399 if (!checkNotifyPermission("notifyDataActivity()" )) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700400 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 synchronized (mRecords) {
403 mDataActivity = state;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700404 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 if ((r.events & PhoneStateListener.LISTEN_DATA_ACTIVITY) != 0) {
406 try {
407 r.callback.onDataActivity(state);
408 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400409 mRemoveList.add(r.binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 }
411 }
412 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400413 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 }
415 }
416
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700417 public void notifyDataConnection(int state, boolean isDataConnectivityPossible,
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700418 String reason, String apn, String apnType, LinkProperties linkProperties,
Robert Greenwalta6d42482011-09-02 15:19:31 -0700419 LinkCapabilities linkCapabilities, int networkType, boolean roaming) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700420 if (!checkNotifyPermission("notifyDataConnection()" )) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700421 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700422 }
Wink Savillec9acde92011-09-21 11:05:43 -0700423 if (DBG) {
424 Slog.i(TAG, "notifyDataConnection: state=" + state + " isDataConnectivityPossible="
Wink Saville26f5a382010-11-24 16:44:29 -0800425 + isDataConnectivityPossible + " reason='" + reason
426 + "' apn='" + apn + "' apnType=" + apnType + " networkType=" + networkType);
Wink Savillec9acde92011-09-21 11:05:43 -0700427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 synchronized (mRecords) {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700429 boolean modified = false;
430 if (state == TelephonyManager.DATA_CONNECTED) {
431 if (!mConnectedApns.contains(apnType)) {
432 mConnectedApns.add(apnType);
433 if (mDataConnectionState != state) {
434 mDataConnectionState = state;
435 modified = true;
436 }
437 }
438 } else {
Robert Greenwalt8e7e0a92010-11-09 10:24:40 -0800439 if (mConnectedApns.remove(apnType)) {
440 if (mConnectedApns.isEmpty()) {
441 mDataConnectionState = state;
442 modified = true;
443 } else {
444 // leave mDataConnectionState as is and
445 // send out the new status for the APN in question.
446 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700447 }
448 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700449 mDataConnectionPossible = isDataConnectivityPossible;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 mDataConnectionReason = reason;
Wink Savillef61101f2010-09-16 16:36:42 -0700451 mDataConnectionLinkProperties = linkProperties;
452 mDataConnectionLinkCapabilities = linkCapabilities;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700453 if (mDataConnectionNetworkType != networkType) {
454 mDataConnectionNetworkType = networkType;
Robert Greenwalt8e7e0a92010-11-09 10:24:40 -0800455 // need to tell registered listeners about the new network type
Robert Greenwalt02648a42010-05-18 10:52:51 -0700456 modified = true;
457 }
458 if (modified) {
Wink Savillec9acde92011-09-21 11:05:43 -0700459 if (DBG) {
460 Slog.d(TAG, "onDataConnectionStateChanged(" + mDataConnectionState
yoonsung.name6fa1202011-08-20 21:39:12 -0700461 + ", " + mDataConnectionNetworkType + ")");
Wink Savillec9acde92011-09-21 11:05:43 -0700462 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700463 for (Record r : mRecords) {
464 if ((r.events & PhoneStateListener.LISTEN_DATA_CONNECTION_STATE) != 0) {
465 try {
yoonsung.name6fa1202011-08-20 21:39:12 -0700466 r.callback.onDataConnectionStateChanged(mDataConnectionState,
467 mDataConnectionNetworkType);
Robert Greenwalt02648a42010-05-18 10:52:51 -0700468 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400469 mRemoveList.add(r.binder);
Robert Greenwalt02648a42010-05-18 10:52:51 -0700470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 }
472 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400473 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 }
475 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700476 broadcastDataConnectionStateChanged(state, isDataConnectivityPossible, reason, apn,
Robert Greenwalta6d42482011-09-02 15:19:31 -0700477 apnType, linkProperties, linkCapabilities, roaming);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 }
479
Robert Greenwalt02648a42010-05-18 10:52:51 -0700480 public void notifyDataConnectionFailed(String reason, String apnType) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700481 if (!checkNotifyPermission("notifyDataConnectionFailed()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700482 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 /*
Robert Greenwalt02648a42010-05-18 10:52:51 -0700485 * This is commented out because there is no onDataConnectionFailed callback
486 * in PhoneStateListener. There should be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 synchronized (mRecords) {
488 mDataConnectionFailedReason = reason;
489 final int N = mRecords.size();
490 for (int i=N-1; i>=0; i--) {
491 Record r = mRecords.get(i);
492 if ((r.events & PhoneStateListener.LISTEN_DATA_CONNECTION_FAILED) != 0) {
493 // XXX
494 }
495 }
496 }
497 */
Robert Greenwalt02648a42010-05-18 10:52:51 -0700498 broadcastDataConnectionFailed(reason, apnType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 }
500
501 public void notifyCellLocation(Bundle cellLocation) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700502 if (!checkNotifyPermission("notifyCellLocation()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700503 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 synchronized (mRecords) {
506 mCellLocation = cellLocation;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700507 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 if ((r.events & PhoneStateListener.LISTEN_CELL_LOCATION) != 0) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400509 try {
510 r.callback.onCellLocationChanged(new Bundle(cellLocation));
511 } catch (RemoteException ex) {
512 mRemoveList.add(r.binder);
513 }
514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 }
516 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400517 handleRemoveListLocked();
Wink Savillee9b06d72009-05-18 21:47:50 -0700518 }
519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520
Wink Savillefd2d0132010-10-28 14:22:26 -0700521 public void notifyOtaspChanged(int otaspMode) {
522 if (!checkNotifyPermission("notifyOtaspChanged()" )) {
523 return;
524 }
525 synchronized (mRecords) {
526 mOtaspMode = otaspMode;
527 for (Record r : mRecords) {
528 if ((r.events & PhoneStateListener.LISTEN_OTASP_CHANGED) != 0) {
529 try {
530 r.callback.onOtaspChanged(otaspMode);
531 } catch (RemoteException ex) {
532 mRemoveList.add(r.binder);
533 }
534 }
535 }
536 handleRemoveListLocked();
537 }
538 }
539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 @Override
541 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
542 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
543 != PackageManager.PERMISSION_GRANTED) {
544 pw.println("Permission Denial: can't dump telephony.registry from from pid="
Wink Savillee9b06d72009-05-18 21:47:50 -0700545 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 return;
547 }
548 synchronized (mRecords) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700549 final int recordCount = mRecords.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 pw.println("last known state:");
551 pw.println(" mCallState=" + mCallState);
552 pw.println(" mCallIncomingNumber=" + mCallIncomingNumber);
553 pw.println(" mServiceState=" + mServiceState);
554 pw.println(" mSignalStrength=" + mSignalStrength);
555 pw.println(" mMessageWaiting=" + mMessageWaiting);
556 pw.println(" mCallForwarding=" + mCallForwarding);
557 pw.println(" mDataActivity=" + mDataActivity);
558 pw.println(" mDataConnectionState=" + mDataConnectionState);
559 pw.println(" mDataConnectionPossible=" + mDataConnectionPossible);
560 pw.println(" mDataConnectionReason=" + mDataConnectionReason);
561 pw.println(" mDataConnectionApn=" + mDataConnectionApn);
Wink Savillef61101f2010-09-16 16:36:42 -0700562 pw.println(" mDataConnectionLinkProperties=" + mDataConnectionLinkProperties);
563 pw.println(" mDataConnectionLinkCapabilities=" + mDataConnectionLinkCapabilities);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 pw.println(" mCellLocation=" + mCellLocation);
John Wang963db55d2012-03-30 16:04:06 -0700565 pw.println(" mCellInfo=" + mCellInfo);
Wink Savillee9b06d72009-05-18 21:47:50 -0700566 pw.println("registrations: count=" + recordCount);
Robert Greenwalt02648a42010-05-18 10:52:51 -0700567 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 pw.println(" " + r.pkgForDebug + " 0x" + Integer.toHexString(r.events));
569 }
570 }
571 }
572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 //
574 // the legacy intent broadcasting
575 //
576
577 private void broadcastServiceStateChanged(ServiceState state) {
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700578 long ident = Binder.clearCallingIdentity();
579 try {
Amith Yamasanif37447b2009-10-08 18:28:01 -0700580 mBatteryStats.notePhoneState(state.getState());
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700581 } catch (RemoteException re) {
582 // Can't do much
583 } finally {
584 Binder.restoreCallingIdentity(ident);
585 }
586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
588 Bundle data = new Bundle();
589 state.fillInNotifierBundle(data);
590 intent.putExtras(data);
591 mContext.sendStickyBroadcast(intent);
592 }
593
Wink Savillee9b06d72009-05-18 21:47:50 -0700594 private void broadcastSignalStrengthChanged(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -0700595 long ident = Binder.clearCallingIdentity();
596 try {
Wink Savillee9b06d72009-05-18 21:47:50 -0700597 mBatteryStats.notePhoneSignalStrength(signalStrength);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700598 } catch (RemoteException e) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700599 /* The remote entity disappeared, we can safely ignore the exception. */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700600 } finally {
601 Binder.restoreCallingIdentity(ident);
602 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 Intent intent = new Intent(TelephonyIntents.ACTION_SIGNAL_STRENGTH_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800605 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Wink Savillee9b06d72009-05-18 21:47:50 -0700606 Bundle data = new Bundle();
607 signalStrength.fillInNotifierBundle(data);
608 intent.putExtras(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 mContext.sendStickyBroadcast(intent);
610 }
611
612 private void broadcastCallStateChanged(int state, String incomingNumber) {
613 long ident = Binder.clearCallingIdentity();
614 try {
615 if (state == TelephonyManager.CALL_STATE_IDLE) {
616 mBatteryStats.notePhoneOff();
617 } else {
618 mBatteryStats.notePhoneOn();
619 }
620 } catch (RemoteException e) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700621 /* The remote entity disappeared, we can safely ignore the exception. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 } finally {
623 Binder.restoreCallingIdentity(ident);
624 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 Intent intent = new Intent(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Wink Savillea639b312012-07-10 12:37:54 -0700627 intent.putExtra(PhoneConstants.STATE_KEY,
628 DefaultPhoneNotifier.convertCallState(state).toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 if (!TextUtils.isEmpty(incomingNumber)) {
630 intent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, incomingNumber);
631 }
632 mContext.sendBroadcast(intent, android.Manifest.permission.READ_PHONE_STATE);
633 }
634
Robert Greenwalt42acef32009-08-12 16:08:25 -0700635 private void broadcastDataConnectionStateChanged(int state,
636 boolean isDataConnectivityPossible,
Wink Savillef61101f2010-09-16 16:36:42 -0700637 String reason, String apn, String apnType, LinkProperties linkProperties,
Robert Greenwalta6d42482011-09-02 15:19:31 -0700638 LinkCapabilities linkCapabilities, boolean roaming) {
Dianne Hackborn627bba72009-03-24 22:32:56 -0700639 // Note: not reporting to the battery stats service here, because the
640 // status bar takes care of that after taking into account all of the
641 // required info.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 Intent intent = new Intent(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
Wink Savillea639b312012-07-10 12:37:54 -0700643 intent.putExtra(PhoneConstants.STATE_KEY,
644 DefaultPhoneNotifier.convertDataState(state).toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 if (!isDataConnectivityPossible) {
Wink Savillea639b312012-07-10 12:37:54 -0700646 intent.putExtra(PhoneConstants.NETWORK_UNAVAILABLE_KEY, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 }
648 if (reason != null) {
Wink Savillea639b312012-07-10 12:37:54 -0700649 intent.putExtra(PhoneConstants.STATE_CHANGE_REASON_KEY, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 }
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700651 if (linkProperties != null) {
Wink Savillea639b312012-07-10 12:37:54 -0700652 intent.putExtra(PhoneConstants.DATA_LINK_PROPERTIES_KEY, linkProperties);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700653 String iface = linkProperties.getInterfaceName();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700654 if (iface != null) {
Wink Savillea639b312012-07-10 12:37:54 -0700655 intent.putExtra(PhoneConstants.DATA_IFACE_NAME_KEY, iface);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700656 }
657 }
Wink Savillef61101f2010-09-16 16:36:42 -0700658 if (linkCapabilities != null) {
Wink Savillea639b312012-07-10 12:37:54 -0700659 intent.putExtra(PhoneConstants.DATA_LINK_CAPABILITIES_KEY, linkCapabilities);
Wink Savillef61101f2010-09-16 16:36:42 -0700660 }
Wink Savillea639b312012-07-10 12:37:54 -0700661 if (roaming) intent.putExtra(PhoneConstants.DATA_NETWORK_ROAMING_KEY, true);
Robert Greenwalta6d42482011-09-02 15:19:31 -0700662
Wink Savillea639b312012-07-10 12:37:54 -0700663 intent.putExtra(PhoneConstants.DATA_APN_KEY, apn);
664 intent.putExtra(PhoneConstants.DATA_APN_TYPE_KEY, apnType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 mContext.sendStickyBroadcast(intent);
666 }
667
Robert Greenwalt02648a42010-05-18 10:52:51 -0700668 private void broadcastDataConnectionFailed(String reason, String apnType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 Intent intent = new Intent(TelephonyIntents.ACTION_DATA_CONNECTION_FAILED);
Wink Savillea639b312012-07-10 12:37:54 -0700670 intent.putExtra(PhoneConstants.FAILURE_REASON_KEY, reason);
671 intent.putExtra(PhoneConstants.DATA_APN_TYPE_KEY, apnType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 mContext.sendStickyBroadcast(intent);
673 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700674
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700675 private boolean checkNotifyPermission(String method) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700676 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
677 == PackageManager.PERMISSION_GRANTED) {
678 return true;
679 }
680 String msg = "Modify Phone State Permission Denial: " + method + " from pid="
Wink Savillee9b06d72009-05-18 21:47:50 -0700681 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid();
Wink Savillec9acde92011-09-21 11:05:43 -0700682 if (DBG) Slog.w(TAG, msg);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700683 return false;
684 }
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700685
686 private void checkListenerPermission(int events) {
687 if ((events & PhoneStateListener.LISTEN_CELL_LOCATION) != 0) {
688 mContext.enforceCallingOrSelfPermission(
689 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
690
691 }
692
John Wang963db55d2012-03-30 16:04:06 -0700693 if ((events & PhoneStateListener.LISTEN_CELL_INFO) != 0) {
694 mContext.enforceCallingOrSelfPermission(
695 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
696
697 }
698
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700699 if ((events & PHONE_STATE_PERMISSION_MASK) != 0) {
700 mContext.enforceCallingOrSelfPermission(
701 android.Manifest.permission.READ_PHONE_STATE, null);
702 }
703 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400704
705 private void handleRemoveListLocked() {
706 if (mRemoveList.size() > 0) {
707 for (IBinder b: mRemoveList) {
708 remove(b);
709 }
710 mRemoveList.clear();
711 }
712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713}