blob: dd54c1692ed92bd77957c80c79a27c5a5dfc6016 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.telephony.TelephonyManager;
33import android.text.TextUtils;
Joe Onorato8a9b2202010-02-26 18:56:32 -080034import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36import java.util.ArrayList;
37import java.io.FileDescriptor;
38import java.io.PrintWriter;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070039import java.net.NetworkInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41import com.android.internal.app.IBatteryStats;
42import com.android.internal.telephony.ITelephonyRegistry;
43import com.android.internal.telephony.IPhoneStateListener;
44import com.android.internal.telephony.DefaultPhoneNotifier;
45import com.android.internal.telephony.Phone;
Wink Savillec9330dd2011-01-12 13:37:38 -080046import com.android.internal.telephony.ServiceStateTracker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import com.android.internal.telephony.TelephonyIntents;
48import com.android.server.am.BatteryStatsService;
49
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050/**
Wink Savillee9b06d72009-05-18 21:47:50 -070051 * Since phone process can be restarted, this class provides a centralized place
52 * that applications can register and be called back from.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 */
54class TelephonyRegistry extends ITelephonyRegistry.Stub {
55 private static final String TAG = "TelephonyRegistry";
56
57 private static class Record {
58 String pkgForDebug;
Wink Savillee9b06d72009-05-18 21:47:50 -070059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 IBinder binder;
Wink Savillee9b06d72009-05-18 21:47:50 -070061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 IPhoneStateListener callback;
Wink Savillee9b06d72009-05-18 21:47:50 -070063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 int events;
65 }
66
67 private final Context mContext;
Wink Savillee9b06d72009-05-18 21:47:50 -070068
Joe Onorato163d8d92010-10-21 13:21:20 -040069 // access should be inside synchronized (mRecords) for these two fields
70 private final ArrayList<IBinder> mRemoveList = new ArrayList<IBinder>();
71 private final ArrayList<Record> mRecords = new ArrayList<Record>();
Wink Savillee9b06d72009-05-18 21:47:50 -070072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 private final IBatteryStats mBatteryStats;
74
75 private int mCallState = TelephonyManager.CALL_STATE_IDLE;
Wink Savillee9b06d72009-05-18 21:47:50 -070076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 private String mCallIncomingNumber = "";
Wink Savillee9b06d72009-05-18 21:47:50 -070078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 private ServiceState mServiceState = new ServiceState();
Wink Savillee9b06d72009-05-18 21:47:50 -070080
81 private SignalStrength mSignalStrength = new SignalStrength();
82
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 private boolean mMessageWaiting = false;
Wink Savillee9b06d72009-05-18 21:47:50 -070084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 private boolean mCallForwarding = false;
Wink Savillee9b06d72009-05-18 21:47:50 -070086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 private int mDataActivity = TelephonyManager.DATA_ACTIVITY_NONE;
Wink Savillee9b06d72009-05-18 21:47:50 -070088
Robert Greenwalt8e7e0a92010-11-09 10:24:40 -080089 private int mDataConnectionState = TelephonyManager.DATA_UNKNOWN;
Wink Savillee9b06d72009-05-18 21:47:50 -070090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 private boolean mDataConnectionPossible = false;
Wink Savillee9b06d72009-05-18 21:47:50 -070092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 private String mDataConnectionReason = "";
Wink Savillee9b06d72009-05-18 21:47:50 -070094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 private String mDataConnectionApn = "";
Wink Savillee9b06d72009-05-18 21:47:50 -070096
Robert Greenwalt02648a42010-05-18 10:52:51 -070097 private ArrayList<String> mConnectedApns;
Robert Greenwalt42acef32009-08-12 16:08:25 -070098
Wink Savillef61101f2010-09-16 16:36:42 -070099 private LinkProperties mDataConnectionLinkProperties;
100
101 private LinkCapabilities mDataConnectionLinkCapabilities;
Wink Savillee9b06d72009-05-18 21:47:50 -0700102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 private Bundle mCellLocation = new Bundle();
104
Robert Greenwalt98e0b142009-10-08 21:15:52 -0700105 private int mDataConnectionNetworkType;
106
Wink Savillec9330dd2011-01-12 13:37:38 -0800107 private int mOtaspMode = ServiceStateTracker.OTASP_UNKNOWN;
Wink Savillefd2d0132010-10-28 14:22:26 -0700108
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700109 static final int PHONE_STATE_PERMISSION_MASK =
110 PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR |
111 PhoneStateListener.LISTEN_CALL_STATE |
112 PhoneStateListener.LISTEN_DATA_ACTIVITY |
113 PhoneStateListener.LISTEN_DATA_CONNECTION_STATE |
114 PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR;
115
Wink Savillee9b06d72009-05-18 21:47:50 -0700116 // we keep a copy of all of the state so we can send it out when folks
117 // register for it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 //
Wink Savillee9b06d72009-05-18 21:47:50 -0700119 // In these calls we call with the lock held. This is safe becasuse remote
120 // calls go through a oneway interface and local calls going through a
121 // handler before they get to app code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
123 TelephonyRegistry(Context context) {
David 'Digit' Turner4ef8ec32009-09-25 11:33:24 -0700124 CellLocation location = CellLocation.getEmpty();
125
126 // Note that location can be null for non-phone builds like
127 // like the generic one.
128 if (location != null) {
129 location.fillInNotifierBundle(mCellLocation);
130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 mContext = context;
132 mBatteryStats = BatteryStatsService.getService();
Robert Greenwalt02648a42010-05-18 10:52:51 -0700133 mConnectedApns = new ArrayList<String>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 }
135
136 public void listen(String pkgForDebug, IPhoneStateListener callback, int events,
137 boolean notifyNow) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800138 // Slog.d(TAG, "listen pkg=" + pkgForDebug + " events=0x" +
Wink Savillee9b06d72009-05-18 21:47:50 -0700139 // Integer.toHexString(events));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 if (events != 0) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700141 /* Checks permission and throws Security exception */
142 checkListenerPermission(events);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
144 synchronized (mRecords) {
145 // register
146 Record r = null;
147 find_and_add: {
148 IBinder b = callback.asBinder();
149 final int N = mRecords.size();
Wink Savillee9b06d72009-05-18 21:47:50 -0700150 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 r = mRecords.get(i);
152 if (b == r.binder) {
153 break find_and_add;
154 }
155 }
156 r = new Record();
157 r.binder = b;
158 r.callback = callback;
159 r.pkgForDebug = pkgForDebug;
160 mRecords.add(r);
161 }
162 int send = events & (events ^ r.events);
163 r.events = events;
164 if (notifyNow) {
165 if ((events & PhoneStateListener.LISTEN_SERVICE_STATE) != 0) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400166 try {
167 r.callback.onServiceStateChanged(new ServiceState(mServiceState));
168 } catch (RemoteException ex) {
169 remove(r.binder);
170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 }
172 if ((events & PhoneStateListener.LISTEN_SIGNAL_STRENGTH) != 0) {
173 try {
Wink Savillee9b06d72009-05-18 21:47:50 -0700174 int gsmSignalStrength = mSignalStrength.getGsmSignalStrength();
175 r.callback.onSignalStrengthChanged((gsmSignalStrength == 99 ? -1
176 : gsmSignalStrength));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 } catch (RemoteException ex) {
178 remove(r.binder);
179 }
180 }
181 if ((events & PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR) != 0) {
182 try {
183 r.callback.onMessageWaitingIndicatorChanged(mMessageWaiting);
184 } catch (RemoteException ex) {
185 remove(r.binder);
186 }
187 }
188 if ((events & PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR) != 0) {
189 try {
190 r.callback.onCallForwardingIndicatorChanged(mCallForwarding);
191 } catch (RemoteException ex) {
192 remove(r.binder);
193 }
194 }
195 if ((events & PhoneStateListener.LISTEN_CELL_LOCATION) != 0) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400196 try {
197 r.callback.onCellLocationChanged(new Bundle(mCellLocation));
198 } catch (RemoteException ex) {
199 remove(r.binder);
200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 }
202 if ((events & PhoneStateListener.LISTEN_CALL_STATE) != 0) {
203 try {
204 r.callback.onCallStateChanged(mCallState, mCallIncomingNumber);
205 } catch (RemoteException ex) {
206 remove(r.binder);
207 }
208 }
209 if ((events & PhoneStateListener.LISTEN_DATA_CONNECTION_STATE) != 0) {
210 try {
Robert Greenwalt98e0b142009-10-08 21:15:52 -0700211 r.callback.onDataConnectionStateChanged(mDataConnectionState,
212 mDataConnectionNetworkType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 } catch (RemoteException ex) {
214 remove(r.binder);
215 }
216 }
217 if ((events & PhoneStateListener.LISTEN_DATA_ACTIVITY) != 0) {
218 try {
219 r.callback.onDataActivity(mDataActivity);
220 } catch (RemoteException ex) {
221 remove(r.binder);
222 }
223 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700224 if ((events & PhoneStateListener.LISTEN_SIGNAL_STRENGTHS) != 0) {
225 try {
226 r.callback.onSignalStrengthsChanged(mSignalStrength);
227 } catch (RemoteException ex) {
228 remove(r.binder);
229 }
230 }
Wink Savillefd2d0132010-10-28 14:22:26 -0700231 if ((events & PhoneStateListener.LISTEN_OTASP_CHANGED) != 0) {
232 try {
233 r.callback.onOtaspChanged(mOtaspMode);
234 } catch (RemoteException ex) {
235 remove(r.binder);
236 }
237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 }
239 }
240 } else {
241 remove(callback.asBinder());
242 }
243 }
244
245 private void remove(IBinder binder) {
246 synchronized (mRecords) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700247 final int recordCount = mRecords.size();
248 for (int i = 0; i < recordCount; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 if (mRecords.get(i).binder == binder) {
250 mRecords.remove(i);
251 return;
252 }
253 }
254 }
255 }
256
257 public void notifyCallState(int state, String incomingNumber) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700258 if (!checkNotifyPermission("notifyCallState()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700259 return;
260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 synchronized (mRecords) {
262 mCallState = state;
263 mCallIncomingNumber = incomingNumber;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700264 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 if ((r.events & PhoneStateListener.LISTEN_CALL_STATE) != 0) {
266 try {
267 r.callback.onCallStateChanged(state, incomingNumber);
268 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400269 mRemoveList.add(r.binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 }
271 }
272 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400273 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 }
275 broadcastCallStateChanged(state, incomingNumber);
276 }
277
278 public void notifyServiceState(ServiceState state) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700279 if (!checkNotifyPermission("notifyServiceState()")){
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700280 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 synchronized (mRecords) {
283 mServiceState = state;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700284 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 if ((r.events & PhoneStateListener.LISTEN_SERVICE_STATE) != 0) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400286 try {
287 r.callback.onServiceStateChanged(new ServiceState(state));
288 } catch (RemoteException ex) {
289 mRemoveList.add(r.binder);
290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 }
292 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400293 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 }
295 broadcastServiceStateChanged(state);
296 }
297
Wink Savillee9b06d72009-05-18 21:47:50 -0700298 public void notifySignalStrength(SignalStrength signalStrength) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700299 if (!checkNotifyPermission("notifySignalStrength()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700300 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 synchronized (mRecords) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700303 mSignalStrength = signalStrength;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700304 for (Record r : mRecords) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700305 if ((r.events & PhoneStateListener.LISTEN_SIGNAL_STRENGTHS) != 0) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400306 try {
307 r.callback.onSignalStrengthsChanged(new SignalStrength(signalStrength));
308 } catch (RemoteException ex) {
309 mRemoveList.add(r.binder);
310 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 if ((r.events & PhoneStateListener.LISTEN_SIGNAL_STRENGTH) != 0) {
313 try {
Wink Savillee9b06d72009-05-18 21:47:50 -0700314 int gsmSignalStrength = signalStrength.getGsmSignalStrength();
315 r.callback.onSignalStrengthChanged((gsmSignalStrength == 99 ? -1
316 : gsmSignalStrength));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400318 mRemoveList.add(r.binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 }
320 }
321 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400322 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700324 broadcastSignalStrengthChanged(signalStrength);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 }
326
327 public void notifyMessageWaitingChanged(boolean mwi) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700328 if (!checkNotifyPermission("notifyMessageWaitingChanged()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700329 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 synchronized (mRecords) {
332 mMessageWaiting = mwi;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700333 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 if ((r.events & PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR) != 0) {
335 try {
336 r.callback.onMessageWaitingIndicatorChanged(mwi);
337 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400338 mRemoveList.add(r.binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 }
340 }
341 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400342 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 }
344 }
345
346 public void notifyCallForwardingChanged(boolean cfi) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700347 if (!checkNotifyPermission("notifyCallForwardingChanged()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700348 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 synchronized (mRecords) {
351 mCallForwarding = cfi;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700352 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 if ((r.events & PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR) != 0) {
354 try {
355 r.callback.onCallForwardingIndicatorChanged(cfi);
356 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400357 mRemoveList.add(r.binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 }
359 }
360 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400361 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 }
363 }
364
365 public void notifyDataActivity(int state) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700366 if (!checkNotifyPermission("notifyDataActivity()" )) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700367 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 synchronized (mRecords) {
370 mDataActivity = state;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700371 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 if ((r.events & PhoneStateListener.LISTEN_DATA_ACTIVITY) != 0) {
373 try {
374 r.callback.onDataActivity(state);
375 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400376 mRemoveList.add(r.binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 }
378 }
379 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400380 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 }
382 }
383
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700384 public void notifyDataConnection(int state, boolean isDataConnectivityPossible,
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700385 String reason, String apn, String apnType, LinkProperties linkProperties,
Wink Savillef61101f2010-09-16 16:36:42 -0700386 LinkCapabilities linkCapabilities, int networkType) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700387 if (!checkNotifyPermission("notifyDataConnection()" )) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700388 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700389 }
Joe Onorato431bb222010-10-18 19:13:23 -0400390 Slog.i(TAG, "notifyDataConnection: state=" + state + " isDataConnectivityPossible="
Wink Saville26f5a382010-11-24 16:44:29 -0800391 + isDataConnectivityPossible + " reason='" + reason
392 + "' apn='" + apn + "' apnType=" + apnType + " networkType=" + networkType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 synchronized (mRecords) {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700394 boolean modified = false;
395 if (state == TelephonyManager.DATA_CONNECTED) {
396 if (!mConnectedApns.contains(apnType)) {
397 mConnectedApns.add(apnType);
398 if (mDataConnectionState != state) {
399 mDataConnectionState = state;
400 modified = true;
401 }
402 }
403 } else {
Robert Greenwalt8e7e0a92010-11-09 10:24:40 -0800404 if (mConnectedApns.remove(apnType)) {
405 if (mConnectedApns.isEmpty()) {
406 mDataConnectionState = state;
407 modified = true;
408 } else {
409 // leave mDataConnectionState as is and
410 // send out the new status for the APN in question.
411 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700412 }
413 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700414 mDataConnectionPossible = isDataConnectivityPossible;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 mDataConnectionReason = reason;
Wink Savillef61101f2010-09-16 16:36:42 -0700416 mDataConnectionLinkProperties = linkProperties;
417 mDataConnectionLinkCapabilities = linkCapabilities;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700418 if (mDataConnectionNetworkType != networkType) {
419 mDataConnectionNetworkType = networkType;
Robert Greenwalt8e7e0a92010-11-09 10:24:40 -0800420 // need to tell registered listeners about the new network type
Robert Greenwalt02648a42010-05-18 10:52:51 -0700421 modified = true;
422 }
423 if (modified) {
Robert Greenwalt8e7e0a92010-11-09 10:24:40 -0800424 Slog.d(TAG, "onDataConnectionStateChanged(" + state + ", " + networkType + ")");
Robert Greenwalt02648a42010-05-18 10:52:51 -0700425 for (Record r : mRecords) {
426 if ((r.events & PhoneStateListener.LISTEN_DATA_CONNECTION_STATE) != 0) {
427 try {
428 r.callback.onDataConnectionStateChanged(state, networkType);
429 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400430 mRemoveList.add(r.binder);
Robert Greenwalt02648a42010-05-18 10:52:51 -0700431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 }
433 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400434 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 }
436 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700437 broadcastDataConnectionStateChanged(state, isDataConnectivityPossible, reason, apn,
Wink Savillef61101f2010-09-16 16:36:42 -0700438 apnType, linkProperties, linkCapabilities);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 }
440
Robert Greenwalt02648a42010-05-18 10:52:51 -0700441 public void notifyDataConnectionFailed(String reason, String apnType) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700442 if (!checkNotifyPermission("notifyDataConnectionFailed()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700443 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 /*
Robert Greenwalt02648a42010-05-18 10:52:51 -0700446 * This is commented out because there is no onDataConnectionFailed callback
447 * in PhoneStateListener. There should be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 synchronized (mRecords) {
449 mDataConnectionFailedReason = reason;
450 final int N = mRecords.size();
451 for (int i=N-1; i>=0; i--) {
452 Record r = mRecords.get(i);
453 if ((r.events & PhoneStateListener.LISTEN_DATA_CONNECTION_FAILED) != 0) {
454 // XXX
455 }
456 }
457 }
458 */
Robert Greenwalt02648a42010-05-18 10:52:51 -0700459 broadcastDataConnectionFailed(reason, apnType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
461
462 public void notifyCellLocation(Bundle cellLocation) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700463 if (!checkNotifyPermission("notifyCellLocation()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700464 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 synchronized (mRecords) {
467 mCellLocation = cellLocation;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700468 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 if ((r.events & PhoneStateListener.LISTEN_CELL_LOCATION) != 0) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400470 try {
471 r.callback.onCellLocationChanged(new Bundle(cellLocation));
472 } catch (RemoteException ex) {
473 mRemoveList.add(r.binder);
474 }
475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 }
477 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400478 handleRemoveListLocked();
Wink Savillee9b06d72009-05-18 21:47:50 -0700479 }
480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481
Wink Savillefd2d0132010-10-28 14:22:26 -0700482 public void notifyOtaspChanged(int otaspMode) {
483 if (!checkNotifyPermission("notifyOtaspChanged()" )) {
484 return;
485 }
486 synchronized (mRecords) {
487 mOtaspMode = otaspMode;
488 for (Record r : mRecords) {
489 if ((r.events & PhoneStateListener.LISTEN_OTASP_CHANGED) != 0) {
490 try {
491 r.callback.onOtaspChanged(otaspMode);
492 } catch (RemoteException ex) {
493 mRemoveList.add(r.binder);
494 }
495 }
496 }
497 handleRemoveListLocked();
498 }
499 }
500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 @Override
502 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
503 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
504 != PackageManager.PERMISSION_GRANTED) {
505 pw.println("Permission Denial: can't dump telephony.registry from from pid="
Wink Savillee9b06d72009-05-18 21:47:50 -0700506 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 return;
508 }
509 synchronized (mRecords) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700510 final int recordCount = mRecords.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 pw.println("last known state:");
512 pw.println(" mCallState=" + mCallState);
513 pw.println(" mCallIncomingNumber=" + mCallIncomingNumber);
514 pw.println(" mServiceState=" + mServiceState);
515 pw.println(" mSignalStrength=" + mSignalStrength);
516 pw.println(" mMessageWaiting=" + mMessageWaiting);
517 pw.println(" mCallForwarding=" + mCallForwarding);
518 pw.println(" mDataActivity=" + mDataActivity);
519 pw.println(" mDataConnectionState=" + mDataConnectionState);
520 pw.println(" mDataConnectionPossible=" + mDataConnectionPossible);
521 pw.println(" mDataConnectionReason=" + mDataConnectionReason);
522 pw.println(" mDataConnectionApn=" + mDataConnectionApn);
Wink Savillef61101f2010-09-16 16:36:42 -0700523 pw.println(" mDataConnectionLinkProperties=" + mDataConnectionLinkProperties);
524 pw.println(" mDataConnectionLinkCapabilities=" + mDataConnectionLinkCapabilities);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 pw.println(" mCellLocation=" + mCellLocation);
Wink Savillee9b06d72009-05-18 21:47:50 -0700526 pw.println("registrations: count=" + recordCount);
Robert Greenwalt02648a42010-05-18 10:52:51 -0700527 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 pw.println(" " + r.pkgForDebug + " 0x" + Integer.toHexString(r.events));
529 }
530 }
531 }
532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 //
534 // the legacy intent broadcasting
535 //
536
537 private void broadcastServiceStateChanged(ServiceState state) {
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700538 long ident = Binder.clearCallingIdentity();
539 try {
Amith Yamasanif37447b2009-10-08 18:28:01 -0700540 mBatteryStats.notePhoneState(state.getState());
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700541 } catch (RemoteException re) {
542 // Can't do much
543 } finally {
544 Binder.restoreCallingIdentity(ident);
545 }
546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
548 Bundle data = new Bundle();
549 state.fillInNotifierBundle(data);
550 intent.putExtras(data);
551 mContext.sendStickyBroadcast(intent);
552 }
553
Wink Savillee9b06d72009-05-18 21:47:50 -0700554 private void broadcastSignalStrengthChanged(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -0700555 long ident = Binder.clearCallingIdentity();
556 try {
Wink Savillee9b06d72009-05-18 21:47:50 -0700557 mBatteryStats.notePhoneSignalStrength(signalStrength);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700558 } catch (RemoteException e) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700559 /* The remote entity disappeared, we can safely ignore the exception. */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700560 } finally {
561 Binder.restoreCallingIdentity(ident);
562 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 Intent intent = new Intent(TelephonyIntents.ACTION_SIGNAL_STRENGTH_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800565 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Wink Savillee9b06d72009-05-18 21:47:50 -0700566 Bundle data = new Bundle();
567 signalStrength.fillInNotifierBundle(data);
568 intent.putExtras(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 mContext.sendStickyBroadcast(intent);
570 }
571
572 private void broadcastCallStateChanged(int state, String incomingNumber) {
573 long ident = Binder.clearCallingIdentity();
574 try {
575 if (state == TelephonyManager.CALL_STATE_IDLE) {
576 mBatteryStats.notePhoneOff();
577 } else {
578 mBatteryStats.notePhoneOn();
579 }
580 } catch (RemoteException e) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700581 /* The remote entity disappeared, we can safely ignore the exception. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 } finally {
583 Binder.restoreCallingIdentity(ident);
584 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 Intent intent = new Intent(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Wink Savillee9b06d72009-05-18 21:47:50 -0700587 intent.putExtra(Phone.STATE_KEY, DefaultPhoneNotifier.convertCallState(state).toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 if (!TextUtils.isEmpty(incomingNumber)) {
589 intent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, incomingNumber);
590 }
591 mContext.sendBroadcast(intent, android.Manifest.permission.READ_PHONE_STATE);
592 }
593
Robert Greenwalt42acef32009-08-12 16:08:25 -0700594 private void broadcastDataConnectionStateChanged(int state,
595 boolean isDataConnectivityPossible,
Wink Savillef61101f2010-09-16 16:36:42 -0700596 String reason, String apn, String apnType, LinkProperties linkProperties,
597 LinkCapabilities linkCapabilities) {
Dianne Hackborn627bba72009-03-24 22:32:56 -0700598 // Note: not reporting to the battery stats service here, because the
599 // status bar takes care of that after taking into account all of the
600 // required info.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 Intent intent = new Intent(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
602 intent.putExtra(Phone.STATE_KEY, DefaultPhoneNotifier.convertDataState(state).toString());
603 if (!isDataConnectivityPossible) {
604 intent.putExtra(Phone.NETWORK_UNAVAILABLE_KEY, true);
605 }
606 if (reason != null) {
607 intent.putExtra(Phone.STATE_CHANGE_REASON_KEY, reason);
608 }
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700609 if (linkProperties != null) {
610 intent.putExtra(Phone.DATA_LINK_PROPERTIES_KEY, linkProperties);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700611 String iface = linkProperties.getInterfaceName();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700612 if (iface != null) {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700613 intent.putExtra(Phone.DATA_IFACE_NAME_KEY, iface);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700614 }
615 }
Wink Savillef61101f2010-09-16 16:36:42 -0700616 if (linkCapabilities != null) {
617 intent.putExtra(Phone.DATA_LINK_CAPABILITIES_KEY, linkCapabilities);
618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 intent.putExtra(Phone.DATA_APN_KEY, apn);
Robert Greenwalt02648a42010-05-18 10:52:51 -0700620 intent.putExtra(Phone.DATA_APN_TYPE_KEY, apnType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 mContext.sendStickyBroadcast(intent);
622 }
623
Robert Greenwalt02648a42010-05-18 10:52:51 -0700624 private void broadcastDataConnectionFailed(String reason, String apnType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 Intent intent = new Intent(TelephonyIntents.ACTION_DATA_CONNECTION_FAILED);
626 intent.putExtra(Phone.FAILURE_REASON_KEY, reason);
Robert Greenwalt02648a42010-05-18 10:52:51 -0700627 intent.putExtra(Phone.DATA_APN_TYPE_KEY, apnType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 mContext.sendStickyBroadcast(intent);
629 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700630
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700631 private boolean checkNotifyPermission(String method) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700632 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
633 == PackageManager.PERMISSION_GRANTED) {
634 return true;
635 }
636 String msg = "Modify Phone State Permission Denial: " + method + " from pid="
Wink Savillee9b06d72009-05-18 21:47:50 -0700637 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800638 Slog.w(TAG, msg);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700639 return false;
640 }
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700641
642 private void checkListenerPermission(int events) {
643 if ((events & PhoneStateListener.LISTEN_CELL_LOCATION) != 0) {
644 mContext.enforceCallingOrSelfPermission(
645 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
646
647 }
648
649 if ((events & PHONE_STATE_PERMISSION_MASK) != 0) {
650 mContext.enforceCallingOrSelfPermission(
651 android.Manifest.permission.READ_PHONE_STATE, null);
652 }
653 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400654
655 private void handleRemoveListLocked() {
656 if (mRemoveList.size() > 0) {
657 for (IBinder b: mRemoveList) {
658 remove(b);
659 }
660 mRemoveList.clear();
661 }
662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663}