blob: 8c8e7250068982d0a9eee984fb46d09f63c2f1c0 [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";
Wink Savillec9acde92011-09-21 11:05:43 -070056 private static final boolean DBG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
58 private static class Record {
59 String pkgForDebug;
Wink Savillee9b06d72009-05-18 21:47:50 -070060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 IBinder binder;
Wink Savillee9b06d72009-05-18 21:47:50 -070062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 IPhoneStateListener callback;
Wink Savillee9b06d72009-05-18 21:47:50 -070064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 int events;
66 }
67
68 private final Context mContext;
Wink Savillee9b06d72009-05-18 21:47:50 -070069
Joe Onorato163d8d92010-10-21 13:21:20 -040070 // access should be inside synchronized (mRecords) for these two fields
71 private final ArrayList<IBinder> mRemoveList = new ArrayList<IBinder>();
72 private final ArrayList<Record> mRecords = new ArrayList<Record>();
Wink Savillee9b06d72009-05-18 21:47:50 -070073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 private final IBatteryStats mBatteryStats;
75
76 private int mCallState = TelephonyManager.CALL_STATE_IDLE;
Wink Savillee9b06d72009-05-18 21:47:50 -070077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 private String mCallIncomingNumber = "";
Wink Savillee9b06d72009-05-18 21:47:50 -070079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 private ServiceState mServiceState = new ServiceState();
Wink Savillee9b06d72009-05-18 21:47:50 -070081
82 private SignalStrength mSignalStrength = new SignalStrength();
83
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 private boolean mMessageWaiting = false;
Wink Savillee9b06d72009-05-18 21:47:50 -070085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 private boolean mCallForwarding = false;
Wink Savillee9b06d72009-05-18 21:47:50 -070087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 private int mDataActivity = TelephonyManager.DATA_ACTIVITY_NONE;
Wink Savillee9b06d72009-05-18 21:47:50 -070089
Robert Greenwalt8e7e0a92010-11-09 10:24:40 -080090 private int mDataConnectionState = TelephonyManager.DATA_UNKNOWN;
Wink Savillee9b06d72009-05-18 21:47:50 -070091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 private boolean mDataConnectionPossible = false;
Wink Savillee9b06d72009-05-18 21:47:50 -070093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 private String mDataConnectionReason = "";
Wink Savillee9b06d72009-05-18 21:47:50 -070095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 private String mDataConnectionApn = "";
Wink Savillee9b06d72009-05-18 21:47:50 -070097
Robert Greenwalt02648a42010-05-18 10:52:51 -070098 private ArrayList<String> mConnectedApns;
Robert Greenwalt42acef32009-08-12 16:08:25 -070099
Wink Savillef61101f2010-09-16 16:36:42 -0700100 private LinkProperties mDataConnectionLinkProperties;
101
102 private LinkCapabilities mDataConnectionLinkCapabilities;
Wink Savillee9b06d72009-05-18 21:47:50 -0700103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 private Bundle mCellLocation = new Bundle();
105
Robert Greenwalt98e0b142009-10-08 21:15:52 -0700106 private int mDataConnectionNetworkType;
107
Wink Savillec9330dd2011-01-12 13:37:38 -0800108 private int mOtaspMode = ServiceStateTracker.OTASP_UNKNOWN;
Wink Savillefd2d0132010-10-28 14:22:26 -0700109
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700110 static final int PHONE_STATE_PERMISSION_MASK =
111 PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR |
112 PhoneStateListener.LISTEN_CALL_STATE |
113 PhoneStateListener.LISTEN_DATA_ACTIVITY |
114 PhoneStateListener.LISTEN_DATA_CONNECTION_STATE |
115 PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR;
116
Wink Savillee9b06d72009-05-18 21:47:50 -0700117 // we keep a copy of all of the state so we can send it out when folks
118 // register for it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 //
Wink Savillee9b06d72009-05-18 21:47:50 -0700120 // In these calls we call with the lock held. This is safe becasuse remote
121 // calls go through a oneway interface and local calls going through a
122 // handler before they get to app code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
124 TelephonyRegistry(Context context) {
David 'Digit' Turner4ef8ec32009-09-25 11:33:24 -0700125 CellLocation location = CellLocation.getEmpty();
126
127 // Note that location can be null for non-phone builds like
128 // like the generic one.
129 if (location != null) {
130 location.fillInNotifierBundle(mCellLocation);
131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 mContext = context;
133 mBatteryStats = BatteryStatsService.getService();
Robert Greenwalt02648a42010-05-18 10:52:51 -0700134 mConnectedApns = new ArrayList<String>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 }
136
137 public void listen(String pkgForDebug, IPhoneStateListener callback, int events,
138 boolean notifyNow) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800139 // Slog.d(TAG, "listen pkg=" + pkgForDebug + " events=0x" +
Wink Savillee9b06d72009-05-18 21:47:50 -0700140 // Integer.toHexString(events));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 if (events != 0) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700142 /* Checks permission and throws Security exception */
143 checkListenerPermission(events);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
145 synchronized (mRecords) {
146 // register
147 Record r = null;
148 find_and_add: {
149 IBinder b = callback.asBinder();
150 final int N = mRecords.size();
Wink Savillee9b06d72009-05-18 21:47:50 -0700151 for (int i = 0; i < N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 r = mRecords.get(i);
153 if (b == r.binder) {
154 break find_and_add;
155 }
156 }
157 r = new Record();
158 r.binder = b;
159 r.callback = callback;
160 r.pkgForDebug = pkgForDebug;
161 mRecords.add(r);
162 }
163 int send = events & (events ^ r.events);
164 r.events = events;
165 if (notifyNow) {
166 if ((events & PhoneStateListener.LISTEN_SERVICE_STATE) != 0) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400167 try {
168 r.callback.onServiceStateChanged(new ServiceState(mServiceState));
169 } catch (RemoteException ex) {
170 remove(r.binder);
171 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 }
173 if ((events & PhoneStateListener.LISTEN_SIGNAL_STRENGTH) != 0) {
174 try {
Wink Savillee9b06d72009-05-18 21:47:50 -0700175 int gsmSignalStrength = mSignalStrength.getGsmSignalStrength();
176 r.callback.onSignalStrengthChanged((gsmSignalStrength == 99 ? -1
177 : gsmSignalStrength));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 } catch (RemoteException ex) {
179 remove(r.binder);
180 }
181 }
182 if ((events & PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR) != 0) {
183 try {
184 r.callback.onMessageWaitingIndicatorChanged(mMessageWaiting);
185 } catch (RemoteException ex) {
186 remove(r.binder);
187 }
188 }
189 if ((events & PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR) != 0) {
190 try {
191 r.callback.onCallForwardingIndicatorChanged(mCallForwarding);
192 } catch (RemoteException ex) {
193 remove(r.binder);
194 }
195 }
196 if ((events & PhoneStateListener.LISTEN_CELL_LOCATION) != 0) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400197 try {
198 r.callback.onCellLocationChanged(new Bundle(mCellLocation));
199 } catch (RemoteException ex) {
200 remove(r.binder);
201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 }
203 if ((events & PhoneStateListener.LISTEN_CALL_STATE) != 0) {
204 try {
205 r.callback.onCallStateChanged(mCallState, mCallIncomingNumber);
206 } catch (RemoteException ex) {
207 remove(r.binder);
208 }
209 }
210 if ((events & PhoneStateListener.LISTEN_DATA_CONNECTION_STATE) != 0) {
211 try {
Robert Greenwalt98e0b142009-10-08 21:15:52 -0700212 r.callback.onDataConnectionStateChanged(mDataConnectionState,
213 mDataConnectionNetworkType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 } catch (RemoteException ex) {
215 remove(r.binder);
216 }
217 }
218 if ((events & PhoneStateListener.LISTEN_DATA_ACTIVITY) != 0) {
219 try {
220 r.callback.onDataActivity(mDataActivity);
221 } catch (RemoteException ex) {
222 remove(r.binder);
223 }
224 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700225 if ((events & PhoneStateListener.LISTEN_SIGNAL_STRENGTHS) != 0) {
226 try {
227 r.callback.onSignalStrengthsChanged(mSignalStrength);
228 } catch (RemoteException ex) {
229 remove(r.binder);
230 }
231 }
Wink Savillefd2d0132010-10-28 14:22:26 -0700232 if ((events & PhoneStateListener.LISTEN_OTASP_CHANGED) != 0) {
233 try {
234 r.callback.onOtaspChanged(mOtaspMode);
235 } catch (RemoteException ex) {
236 remove(r.binder);
237 }
238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 }
240 }
241 } else {
242 remove(callback.asBinder());
243 }
244 }
245
246 private void remove(IBinder binder) {
247 synchronized (mRecords) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700248 final int recordCount = mRecords.size();
249 for (int i = 0; i < recordCount; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 if (mRecords.get(i).binder == binder) {
251 mRecords.remove(i);
252 return;
253 }
254 }
255 }
256 }
257
258 public void notifyCallState(int state, String incomingNumber) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700259 if (!checkNotifyPermission("notifyCallState()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700260 return;
261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 synchronized (mRecords) {
263 mCallState = state;
264 mCallIncomingNumber = incomingNumber;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700265 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 if ((r.events & PhoneStateListener.LISTEN_CALL_STATE) != 0) {
267 try {
268 r.callback.onCallStateChanged(state, incomingNumber);
269 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400270 mRemoveList.add(r.binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 }
272 }
273 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400274 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 }
276 broadcastCallStateChanged(state, incomingNumber);
277 }
278
279 public void notifyServiceState(ServiceState state) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700280 if (!checkNotifyPermission("notifyServiceState()")){
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700281 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 synchronized (mRecords) {
284 mServiceState = state;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700285 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 if ((r.events & PhoneStateListener.LISTEN_SERVICE_STATE) != 0) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400287 try {
288 r.callback.onServiceStateChanged(new ServiceState(state));
289 } catch (RemoteException ex) {
290 mRemoveList.add(r.binder);
291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 }
293 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400294 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 }
296 broadcastServiceStateChanged(state);
297 }
298
Wink Savillee9b06d72009-05-18 21:47:50 -0700299 public void notifySignalStrength(SignalStrength signalStrength) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700300 if (!checkNotifyPermission("notifySignalStrength()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700301 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 synchronized (mRecords) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700304 mSignalStrength = signalStrength;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700305 for (Record r : mRecords) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700306 if ((r.events & PhoneStateListener.LISTEN_SIGNAL_STRENGTHS) != 0) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400307 try {
308 r.callback.onSignalStrengthsChanged(new SignalStrength(signalStrength));
309 } catch (RemoteException ex) {
310 mRemoveList.add(r.binder);
311 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 if ((r.events & PhoneStateListener.LISTEN_SIGNAL_STRENGTH) != 0) {
314 try {
Wink Savillee9b06d72009-05-18 21:47:50 -0700315 int gsmSignalStrength = signalStrength.getGsmSignalStrength();
316 r.callback.onSignalStrengthChanged((gsmSignalStrength == 99 ? -1
317 : gsmSignalStrength));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400319 mRemoveList.add(r.binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 }
321 }
322 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400323 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700325 broadcastSignalStrengthChanged(signalStrength);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 }
327
328 public void notifyMessageWaitingChanged(boolean mwi) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700329 if (!checkNotifyPermission("notifyMessageWaitingChanged()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700330 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 synchronized (mRecords) {
333 mMessageWaiting = mwi;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700334 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 if ((r.events & PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR) != 0) {
336 try {
337 r.callback.onMessageWaitingIndicatorChanged(mwi);
338 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400339 mRemoveList.add(r.binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 }
341 }
342 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400343 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 }
345 }
346
347 public void notifyCallForwardingChanged(boolean cfi) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700348 if (!checkNotifyPermission("notifyCallForwardingChanged()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700349 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700350 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 synchronized (mRecords) {
352 mCallForwarding = cfi;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700353 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 if ((r.events & PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR) != 0) {
355 try {
356 r.callback.onCallForwardingIndicatorChanged(cfi);
357 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400358 mRemoveList.add(r.binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 }
360 }
361 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400362 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 }
364 }
365
366 public void notifyDataActivity(int state) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700367 if (!checkNotifyPermission("notifyDataActivity()" )) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700368 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700369 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 synchronized (mRecords) {
371 mDataActivity = state;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700372 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 if ((r.events & PhoneStateListener.LISTEN_DATA_ACTIVITY) != 0) {
374 try {
375 r.callback.onDataActivity(state);
376 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400377 mRemoveList.add(r.binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 }
379 }
380 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400381 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
383 }
384
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700385 public void notifyDataConnection(int state, boolean isDataConnectivityPossible,
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700386 String reason, String apn, String apnType, LinkProperties linkProperties,
Robert Greenwalta6d42482011-09-02 15:19:31 -0700387 LinkCapabilities linkCapabilities, int networkType, boolean roaming) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700388 if (!checkNotifyPermission("notifyDataConnection()" )) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700389 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700390 }
Wink Savillec9acde92011-09-21 11:05:43 -0700391 if (DBG) {
392 Slog.i(TAG, "notifyDataConnection: state=" + state + " isDataConnectivityPossible="
Wink Saville26f5a382010-11-24 16:44:29 -0800393 + isDataConnectivityPossible + " reason='" + reason
394 + "' apn='" + apn + "' apnType=" + apnType + " networkType=" + networkType);
Wink Savillec9acde92011-09-21 11:05:43 -0700395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 synchronized (mRecords) {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700397 boolean modified = false;
398 if (state == TelephonyManager.DATA_CONNECTED) {
399 if (!mConnectedApns.contains(apnType)) {
400 mConnectedApns.add(apnType);
401 if (mDataConnectionState != state) {
402 mDataConnectionState = state;
403 modified = true;
404 }
405 }
406 } else {
Robert Greenwalt8e7e0a92010-11-09 10:24:40 -0800407 if (mConnectedApns.remove(apnType)) {
408 if (mConnectedApns.isEmpty()) {
409 mDataConnectionState = state;
410 modified = true;
411 } else {
412 // leave mDataConnectionState as is and
413 // send out the new status for the APN in question.
414 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700415 }
416 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700417 mDataConnectionPossible = isDataConnectivityPossible;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 mDataConnectionReason = reason;
Wink Savillef61101f2010-09-16 16:36:42 -0700419 mDataConnectionLinkProperties = linkProperties;
420 mDataConnectionLinkCapabilities = linkCapabilities;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700421 if (mDataConnectionNetworkType != networkType) {
422 mDataConnectionNetworkType = networkType;
Robert Greenwalt8e7e0a92010-11-09 10:24:40 -0800423 // need to tell registered listeners about the new network type
Robert Greenwalt02648a42010-05-18 10:52:51 -0700424 modified = true;
425 }
426 if (modified) {
Wink Savillec9acde92011-09-21 11:05:43 -0700427 if (DBG) {
428 Slog.d(TAG, "onDataConnectionStateChanged(" + mDataConnectionState
yoonsung.name6fa1202011-08-20 21:39:12 -0700429 + ", " + mDataConnectionNetworkType + ")");
Wink Savillec9acde92011-09-21 11:05:43 -0700430 }
Robert Greenwalt02648a42010-05-18 10:52:51 -0700431 for (Record r : mRecords) {
432 if ((r.events & PhoneStateListener.LISTEN_DATA_CONNECTION_STATE) != 0) {
433 try {
yoonsung.name6fa1202011-08-20 21:39:12 -0700434 r.callback.onDataConnectionStateChanged(mDataConnectionState,
435 mDataConnectionNetworkType);
Robert Greenwalt02648a42010-05-18 10:52:51 -0700436 } catch (RemoteException ex) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400437 mRemoveList.add(r.binder);
Robert Greenwalt02648a42010-05-18 10:52:51 -0700438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 }
440 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400441 handleRemoveListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 }
443 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700444 broadcastDataConnectionStateChanged(state, isDataConnectivityPossible, reason, apn,
Robert Greenwalta6d42482011-09-02 15:19:31 -0700445 apnType, linkProperties, linkCapabilities, roaming);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 }
447
Robert Greenwalt02648a42010-05-18 10:52:51 -0700448 public void notifyDataConnectionFailed(String reason, String apnType) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700449 if (!checkNotifyPermission("notifyDataConnectionFailed()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700450 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 /*
Robert Greenwalt02648a42010-05-18 10:52:51 -0700453 * This is commented out because there is no onDataConnectionFailed callback
454 * in PhoneStateListener. There should be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 synchronized (mRecords) {
456 mDataConnectionFailedReason = reason;
457 final int N = mRecords.size();
458 for (int i=N-1; i>=0; i--) {
459 Record r = mRecords.get(i);
460 if ((r.events & PhoneStateListener.LISTEN_DATA_CONNECTION_FAILED) != 0) {
461 // XXX
462 }
463 }
464 }
465 */
Robert Greenwalt02648a42010-05-18 10:52:51 -0700466 broadcastDataConnectionFailed(reason, apnType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 }
468
469 public void notifyCellLocation(Bundle cellLocation) {
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700470 if (!checkNotifyPermission("notifyCellLocation()")) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700471 return;
Wink Savillee9b06d72009-05-18 21:47:50 -0700472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 synchronized (mRecords) {
474 mCellLocation = cellLocation;
Robert Greenwalt02648a42010-05-18 10:52:51 -0700475 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 if ((r.events & PhoneStateListener.LISTEN_CELL_LOCATION) != 0) {
Joe Onorato163d8d92010-10-21 13:21:20 -0400477 try {
478 r.callback.onCellLocationChanged(new Bundle(cellLocation));
479 } catch (RemoteException ex) {
480 mRemoveList.add(r.binder);
481 }
482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 }
484 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400485 handleRemoveListLocked();
Wink Savillee9b06d72009-05-18 21:47:50 -0700486 }
487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488
Wink Savillefd2d0132010-10-28 14:22:26 -0700489 public void notifyOtaspChanged(int otaspMode) {
490 if (!checkNotifyPermission("notifyOtaspChanged()" )) {
491 return;
492 }
493 synchronized (mRecords) {
494 mOtaspMode = otaspMode;
495 for (Record r : mRecords) {
496 if ((r.events & PhoneStateListener.LISTEN_OTASP_CHANGED) != 0) {
497 try {
498 r.callback.onOtaspChanged(otaspMode);
499 } catch (RemoteException ex) {
500 mRemoveList.add(r.binder);
501 }
502 }
503 }
504 handleRemoveListLocked();
505 }
506 }
507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 @Override
509 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
510 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
511 != PackageManager.PERMISSION_GRANTED) {
512 pw.println("Permission Denial: can't dump telephony.registry from from pid="
Wink Savillee9b06d72009-05-18 21:47:50 -0700513 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 return;
515 }
516 synchronized (mRecords) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700517 final int recordCount = mRecords.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 pw.println("last known state:");
519 pw.println(" mCallState=" + mCallState);
520 pw.println(" mCallIncomingNumber=" + mCallIncomingNumber);
521 pw.println(" mServiceState=" + mServiceState);
522 pw.println(" mSignalStrength=" + mSignalStrength);
523 pw.println(" mMessageWaiting=" + mMessageWaiting);
524 pw.println(" mCallForwarding=" + mCallForwarding);
525 pw.println(" mDataActivity=" + mDataActivity);
526 pw.println(" mDataConnectionState=" + mDataConnectionState);
527 pw.println(" mDataConnectionPossible=" + mDataConnectionPossible);
528 pw.println(" mDataConnectionReason=" + mDataConnectionReason);
529 pw.println(" mDataConnectionApn=" + mDataConnectionApn);
Wink Savillef61101f2010-09-16 16:36:42 -0700530 pw.println(" mDataConnectionLinkProperties=" + mDataConnectionLinkProperties);
531 pw.println(" mDataConnectionLinkCapabilities=" + mDataConnectionLinkCapabilities);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 pw.println(" mCellLocation=" + mCellLocation);
Wink Savillee9b06d72009-05-18 21:47:50 -0700533 pw.println("registrations: count=" + recordCount);
Robert Greenwalt02648a42010-05-18 10:52:51 -0700534 for (Record r : mRecords) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 pw.println(" " + r.pkgForDebug + " 0x" + Integer.toHexString(r.events));
536 }
537 }
538 }
539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 //
541 // the legacy intent broadcasting
542 //
543
544 private void broadcastServiceStateChanged(ServiceState state) {
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700545 long ident = Binder.clearCallingIdentity();
546 try {
Amith Yamasanif37447b2009-10-08 18:28:01 -0700547 mBatteryStats.notePhoneState(state.getState());
Amith Yamasani32dbefd2009-06-19 09:21:17 -0700548 } catch (RemoteException re) {
549 // Can't do much
550 } finally {
551 Binder.restoreCallingIdentity(ident);
552 }
553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
555 Bundle data = new Bundle();
556 state.fillInNotifierBundle(data);
557 intent.putExtras(data);
558 mContext.sendStickyBroadcast(intent);
559 }
560
Wink Savillee9b06d72009-05-18 21:47:50 -0700561 private void broadcastSignalStrengthChanged(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -0700562 long ident = Binder.clearCallingIdentity();
563 try {
Wink Savillee9b06d72009-05-18 21:47:50 -0700564 mBatteryStats.notePhoneSignalStrength(signalStrength);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700565 } catch (RemoteException e) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700566 /* The remote entity disappeared, we can safely ignore the exception. */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700567 } finally {
568 Binder.restoreCallingIdentity(ident);
569 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 Intent intent = new Intent(TelephonyIntents.ACTION_SIGNAL_STRENGTH_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800572 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Wink Savillee9b06d72009-05-18 21:47:50 -0700573 Bundle data = new Bundle();
574 signalStrength.fillInNotifierBundle(data);
575 intent.putExtras(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 mContext.sendStickyBroadcast(intent);
577 }
578
579 private void broadcastCallStateChanged(int state, String incomingNumber) {
580 long ident = Binder.clearCallingIdentity();
581 try {
582 if (state == TelephonyManager.CALL_STATE_IDLE) {
583 mBatteryStats.notePhoneOff();
584 } else {
585 mBatteryStats.notePhoneOn();
586 }
587 } catch (RemoteException e) {
Wink Savillee9b06d72009-05-18 21:47:50 -0700588 /* The remote entity disappeared, we can safely ignore the exception. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 } finally {
590 Binder.restoreCallingIdentity(ident);
591 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 Intent intent = new Intent(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Wink Savillee9b06d72009-05-18 21:47:50 -0700594 intent.putExtra(Phone.STATE_KEY, DefaultPhoneNotifier.convertCallState(state).toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 if (!TextUtils.isEmpty(incomingNumber)) {
596 intent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, incomingNumber);
597 }
598 mContext.sendBroadcast(intent, android.Manifest.permission.READ_PHONE_STATE);
599 }
600
Robert Greenwalt42acef32009-08-12 16:08:25 -0700601 private void broadcastDataConnectionStateChanged(int state,
602 boolean isDataConnectivityPossible,
Wink Savillef61101f2010-09-16 16:36:42 -0700603 String reason, String apn, String apnType, LinkProperties linkProperties,
Robert Greenwalta6d42482011-09-02 15:19:31 -0700604 LinkCapabilities linkCapabilities, boolean roaming) {
Dianne Hackborn627bba72009-03-24 22:32:56 -0700605 // Note: not reporting to the battery stats service here, because the
606 // status bar takes care of that after taking into account all of the
607 // required info.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 Intent intent = new Intent(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
609 intent.putExtra(Phone.STATE_KEY, DefaultPhoneNotifier.convertDataState(state).toString());
610 if (!isDataConnectivityPossible) {
611 intent.putExtra(Phone.NETWORK_UNAVAILABLE_KEY, true);
612 }
613 if (reason != null) {
614 intent.putExtra(Phone.STATE_CHANGE_REASON_KEY, reason);
615 }
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700616 if (linkProperties != null) {
617 intent.putExtra(Phone.DATA_LINK_PROPERTIES_KEY, linkProperties);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700618 String iface = linkProperties.getInterfaceName();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700619 if (iface != null) {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700620 intent.putExtra(Phone.DATA_IFACE_NAME_KEY, iface);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700621 }
622 }
Wink Savillef61101f2010-09-16 16:36:42 -0700623 if (linkCapabilities != null) {
624 intent.putExtra(Phone.DATA_LINK_CAPABILITIES_KEY, linkCapabilities);
625 }
Robert Greenwalta6d42482011-09-02 15:19:31 -0700626 if (roaming) intent.putExtra(Phone.DATA_NETWORK_ROAMING_KEY, true);
627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 intent.putExtra(Phone.DATA_APN_KEY, apn);
Robert Greenwalt02648a42010-05-18 10:52:51 -0700629 intent.putExtra(Phone.DATA_APN_TYPE_KEY, apnType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 mContext.sendStickyBroadcast(intent);
631 }
632
Robert Greenwalt02648a42010-05-18 10:52:51 -0700633 private void broadcastDataConnectionFailed(String reason, String apnType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 Intent intent = new Intent(TelephonyIntents.ACTION_DATA_CONNECTION_FAILED);
635 intent.putExtra(Phone.FAILURE_REASON_KEY, reason);
Robert Greenwalt02648a42010-05-18 10:52:51 -0700636 intent.putExtra(Phone.DATA_APN_TYPE_KEY, apnType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 mContext.sendStickyBroadcast(intent);
638 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700639
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700640 private boolean checkNotifyPermission(String method) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700641 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
642 == PackageManager.PERMISSION_GRANTED) {
643 return true;
644 }
645 String msg = "Modify Phone State Permission Denial: " + method + " from pid="
Wink Savillee9b06d72009-05-18 21:47:50 -0700646 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid();
Wink Savillec9acde92011-09-21 11:05:43 -0700647 if (DBG) Slog.w(TAG, msg);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700648 return false;
649 }
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700650
651 private void checkListenerPermission(int events) {
652 if ((events & PhoneStateListener.LISTEN_CELL_LOCATION) != 0) {
653 mContext.enforceCallingOrSelfPermission(
654 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
655
656 }
657
658 if ((events & PHONE_STATE_PERMISSION_MASK) != 0) {
659 mContext.enforceCallingOrSelfPermission(
660 android.Manifest.permission.READ_PHONE_STATE, null);
661 }
662 }
Joe Onorato163d8d92010-10-21 13:21:20 -0400663
664 private void handleRemoveListLocked() {
665 if (mRemoveList.size() > 0) {
666 for (IBinder b: mRemoveList) {
667 remove(b);
668 }
669 mRemoveList.clear();
670 }
671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672}