The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
| 21 | import android.content.pm.PackageManager; |
| 22 | import android.os.Binder; |
| 23 | import android.os.Bundle; |
| 24 | import android.os.IBinder; |
| 25 | import android.os.RemoteException; |
| 26 | import android.telephony.CellLocation; |
| 27 | import android.telephony.PhoneStateListener; |
| 28 | import android.telephony.ServiceState; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 29 | import android.telephony.SignalStrength; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.telephony.TelephonyManager; |
| 31 | import android.text.TextUtils; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 32 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
| 34 | import java.util.ArrayList; |
| 35 | import java.io.FileDescriptor; |
| 36 | import java.io.PrintWriter; |
| 37 | |
| 38 | import com.android.internal.app.IBatteryStats; |
| 39 | import com.android.internal.telephony.ITelephonyRegistry; |
| 40 | import com.android.internal.telephony.IPhoneStateListener; |
| 41 | import com.android.internal.telephony.DefaultPhoneNotifier; |
| 42 | import com.android.internal.telephony.Phone; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import com.android.internal.telephony.TelephonyIntents; |
| 44 | import com.android.server.am.BatteryStatsService; |
| 45 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | /** |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 47 | * Since phone process can be restarted, this class provides a centralized place |
| 48 | * that applications can register and be called back from. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | */ |
| 50 | class TelephonyRegistry extends ITelephonyRegistry.Stub { |
| 51 | private static final String TAG = "TelephonyRegistry"; |
| 52 | |
| 53 | private static class Record { |
| 54 | String pkgForDebug; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 55 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | IBinder binder; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 57 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | IPhoneStateListener callback; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 59 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | int events; |
| 61 | } |
| 62 | |
| 63 | private final Context mContext; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 64 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | private final ArrayList<Record> mRecords = new ArrayList(); |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 66 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | private final IBatteryStats mBatteryStats; |
| 68 | |
| 69 | private int mCallState = TelephonyManager.CALL_STATE_IDLE; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 70 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | private String mCallIncomingNumber = ""; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 72 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | private ServiceState mServiceState = new ServiceState(); |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 74 | |
| 75 | private SignalStrength mSignalStrength = new SignalStrength(); |
| 76 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | private boolean mMessageWaiting = false; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 78 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | private boolean mCallForwarding = false; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 80 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | private int mDataActivity = TelephonyManager.DATA_ACTIVITY_NONE; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 82 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | private int mDataConnectionState = TelephonyManager.DATA_CONNECTED; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 84 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | private boolean mDataConnectionPossible = false; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 86 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | private String mDataConnectionReason = ""; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 88 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | private String mDataConnectionApn = ""; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 90 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | private String mDataConnectionInterfaceName = ""; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 92 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | private Bundle mCellLocation = new Bundle(); |
| 94 | |
Jaikumar Ganesh | 4551565 | 2009-04-23 15:20:21 -0700 | [diff] [blame^] | 95 | static final int PHONE_STATE_PERMISSION_MASK = |
| 96 | PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR | |
| 97 | PhoneStateListener.LISTEN_CALL_STATE | |
| 98 | PhoneStateListener.LISTEN_DATA_ACTIVITY | |
| 99 | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE | |
| 100 | PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR; |
| 101 | |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 102 | // we keep a copy of all of the state so we can send it out when folks |
| 103 | // register for it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | // |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 105 | // In these calls we call with the lock held. This is safe becasuse remote |
| 106 | // calls go through a oneway interface and local calls going through a |
| 107 | // handler before they get to app code. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | |
| 109 | TelephonyRegistry(Context context) { |
| 110 | CellLocation.getEmpty().fillInNotifierBundle(mCellLocation); |
| 111 | mContext = context; |
| 112 | mBatteryStats = BatteryStatsService.getService(); |
| 113 | } |
| 114 | |
| 115 | public void listen(String pkgForDebug, IPhoneStateListener callback, int events, |
| 116 | boolean notifyNow) { |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 117 | // Log.d(TAG, "listen pkg=" + pkgForDebug + " events=0x" + |
| 118 | // Integer.toHexString(events)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | if (events != 0) { |
Jaikumar Ganesh | 4551565 | 2009-04-23 15:20:21 -0700 | [diff] [blame^] | 120 | /* Checks permission and throws Security exception */ |
| 121 | checkListenerPermission(events); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | |
| 123 | synchronized (mRecords) { |
| 124 | // register |
| 125 | Record r = null; |
| 126 | find_and_add: { |
| 127 | IBinder b = callback.asBinder(); |
| 128 | final int N = mRecords.size(); |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 129 | for (int i = 0; i < N; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | r = mRecords.get(i); |
| 131 | if (b == r.binder) { |
| 132 | break find_and_add; |
| 133 | } |
| 134 | } |
| 135 | r = new Record(); |
| 136 | r.binder = b; |
| 137 | r.callback = callback; |
| 138 | r.pkgForDebug = pkgForDebug; |
| 139 | mRecords.add(r); |
| 140 | } |
| 141 | int send = events & (events ^ r.events); |
| 142 | r.events = events; |
| 143 | if (notifyNow) { |
| 144 | if ((events & PhoneStateListener.LISTEN_SERVICE_STATE) != 0) { |
| 145 | sendServiceState(r, mServiceState); |
| 146 | } |
| 147 | if ((events & PhoneStateListener.LISTEN_SIGNAL_STRENGTH) != 0) { |
| 148 | try { |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 149 | int gsmSignalStrength = mSignalStrength.getGsmSignalStrength(); |
| 150 | r.callback.onSignalStrengthChanged((gsmSignalStrength == 99 ? -1 |
| 151 | : gsmSignalStrength)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | } catch (RemoteException ex) { |
| 153 | remove(r.binder); |
| 154 | } |
| 155 | } |
| 156 | if ((events & PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR) != 0) { |
| 157 | try { |
| 158 | r.callback.onMessageWaitingIndicatorChanged(mMessageWaiting); |
| 159 | } catch (RemoteException ex) { |
| 160 | remove(r.binder); |
| 161 | } |
| 162 | } |
| 163 | if ((events & PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR) != 0) { |
| 164 | try { |
| 165 | r.callback.onCallForwardingIndicatorChanged(mCallForwarding); |
| 166 | } catch (RemoteException ex) { |
| 167 | remove(r.binder); |
| 168 | } |
| 169 | } |
| 170 | if ((events & PhoneStateListener.LISTEN_CELL_LOCATION) != 0) { |
| 171 | sendCellLocation(r, mCellLocation); |
| 172 | } |
| 173 | if ((events & PhoneStateListener.LISTEN_CALL_STATE) != 0) { |
| 174 | try { |
| 175 | r.callback.onCallStateChanged(mCallState, mCallIncomingNumber); |
| 176 | } catch (RemoteException ex) { |
| 177 | remove(r.binder); |
| 178 | } |
| 179 | } |
| 180 | if ((events & PhoneStateListener.LISTEN_DATA_CONNECTION_STATE) != 0) { |
| 181 | try { |
| 182 | r.callback.onDataConnectionStateChanged(mDataConnectionState); |
| 183 | } catch (RemoteException ex) { |
| 184 | remove(r.binder); |
| 185 | } |
| 186 | } |
| 187 | if ((events & PhoneStateListener.LISTEN_DATA_ACTIVITY) != 0) { |
| 188 | try { |
| 189 | r.callback.onDataActivity(mDataActivity); |
| 190 | } catch (RemoteException ex) { |
| 191 | remove(r.binder); |
| 192 | } |
| 193 | } |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 194 | if ((events & PhoneStateListener.LISTEN_SIGNAL_STRENGTHS) != 0) { |
| 195 | try { |
| 196 | r.callback.onSignalStrengthsChanged(mSignalStrength); |
| 197 | } catch (RemoteException ex) { |
| 198 | remove(r.binder); |
| 199 | } |
| 200 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | } else { |
| 204 | remove(callback.asBinder()); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | private void remove(IBinder binder) { |
| 209 | synchronized (mRecords) { |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 210 | final int recordCount = mRecords.size(); |
| 211 | for (int i = 0; i < recordCount; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | if (mRecords.get(i).binder == binder) { |
| 213 | mRecords.remove(i); |
| 214 | return; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | public void notifyCallState(int state, String incomingNumber) { |
Jaikumar Ganesh | 4551565 | 2009-04-23 15:20:21 -0700 | [diff] [blame^] | 221 | if (!checkNotifyPermission("notifyCallState()")) { |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 222 | return; |
| 223 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | synchronized (mRecords) { |
| 225 | mCallState = state; |
| 226 | mCallIncomingNumber = incomingNumber; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 227 | for (int i = mRecords.size() - 1; i >= 0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | Record r = mRecords.get(i); |
| 229 | if ((r.events & PhoneStateListener.LISTEN_CALL_STATE) != 0) { |
| 230 | try { |
| 231 | r.callback.onCallStateChanged(state, incomingNumber); |
| 232 | } catch (RemoteException ex) { |
| 233 | remove(r.binder); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | broadcastCallStateChanged(state, incomingNumber); |
| 239 | } |
| 240 | |
| 241 | public void notifyServiceState(ServiceState state) { |
Jaikumar Ganesh | 4551565 | 2009-04-23 15:20:21 -0700 | [diff] [blame^] | 242 | if (!checkNotifyPermission("notifyServiceState()")){ |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 243 | return; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 244 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | synchronized (mRecords) { |
| 246 | mServiceState = state; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 247 | for (int i = mRecords.size() - 1; i >= 0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | Record r = mRecords.get(i); |
| 249 | if ((r.events & PhoneStateListener.LISTEN_SERVICE_STATE) != 0) { |
| 250 | sendServiceState(r, state); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | broadcastServiceStateChanged(state); |
| 255 | } |
| 256 | |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 257 | public void notifySignalStrength(SignalStrength signalStrength) { |
Jaikumar Ganesh | 4551565 | 2009-04-23 15:20:21 -0700 | [diff] [blame^] | 258 | if (!checkNotifyPermission("notifySignalStrength()")) { |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 259 | return; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 260 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | synchronized (mRecords) { |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 262 | mSignalStrength = signalStrength; |
| 263 | for (int i = mRecords.size() - 1; i >= 0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | Record r = mRecords.get(i); |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 265 | if ((r.events & PhoneStateListener.LISTEN_SIGNAL_STRENGTHS) != 0) { |
| 266 | sendSignalStrength(r, signalStrength); |
| 267 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | if ((r.events & PhoneStateListener.LISTEN_SIGNAL_STRENGTH) != 0) { |
| 269 | try { |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 270 | int gsmSignalStrength = signalStrength.getGsmSignalStrength(); |
| 271 | r.callback.onSignalStrengthChanged((gsmSignalStrength == 99 ? -1 |
| 272 | : gsmSignalStrength)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | } catch (RemoteException ex) { |
| 274 | remove(r.binder); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | } |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 279 | broadcastSignalStrengthChanged(signalStrength); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | public void notifyMessageWaitingChanged(boolean mwi) { |
Jaikumar Ganesh | 4551565 | 2009-04-23 15:20:21 -0700 | [diff] [blame^] | 283 | if (!checkNotifyPermission("notifyMessageWaitingChanged()")) { |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 284 | return; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 285 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | synchronized (mRecords) { |
| 287 | mMessageWaiting = mwi; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 288 | for (int i = mRecords.size() - 1; i >= 0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | Record r = mRecords.get(i); |
| 290 | if ((r.events & PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR) != 0) { |
| 291 | try { |
| 292 | r.callback.onMessageWaitingIndicatorChanged(mwi); |
| 293 | } catch (RemoteException ex) { |
| 294 | remove(r.binder); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | public void notifyCallForwardingChanged(boolean cfi) { |
Jaikumar Ganesh | 4551565 | 2009-04-23 15:20:21 -0700 | [diff] [blame^] | 302 | if (!checkNotifyPermission("notifyCallForwardingChanged()")) { |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 303 | return; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 304 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | synchronized (mRecords) { |
| 306 | mCallForwarding = cfi; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 307 | for (int i = mRecords.size() - 1; i >= 0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | Record r = mRecords.get(i); |
| 309 | if ((r.events & PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR) != 0) { |
| 310 | try { |
| 311 | r.callback.onCallForwardingIndicatorChanged(cfi); |
| 312 | } catch (RemoteException ex) { |
| 313 | remove(r.binder); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | public void notifyDataActivity(int state) { |
Jaikumar Ganesh | 4551565 | 2009-04-23 15:20:21 -0700 | [diff] [blame^] | 321 | if (!checkNotifyPermission("notifyDataActivity()" )) { |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 322 | return; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 323 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | synchronized (mRecords) { |
| 325 | mDataActivity = state; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 326 | for (int i = mRecords.size() - 1; i >= 0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | Record r = mRecords.get(i); |
| 328 | if ((r.events & PhoneStateListener.LISTEN_DATA_ACTIVITY) != 0) { |
| 329 | try { |
| 330 | r.callback.onDataActivity(state); |
| 331 | } catch (RemoteException ex) { |
| 332 | remove(r.binder); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
Jaikumar Ganesh | 4551565 | 2009-04-23 15:20:21 -0700 | [diff] [blame^] | 339 | public void notifyDataConnection(int state, boolean isDataConnectivityPossible, |
| 340 | String reason, String apn, String interfaceName) { |
| 341 | if (!checkNotifyPermission("notifyDataConnection()" )) { |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 342 | return; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 343 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | synchronized (mRecords) { |
| 345 | mDataConnectionState = state; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 346 | mDataConnectionPossible = isDataConnectivityPossible; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | mDataConnectionReason = reason; |
| 348 | mDataConnectionApn = apn; |
| 349 | mDataConnectionInterfaceName = interfaceName; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 350 | for (int i = mRecords.size() - 1; i >= 0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | Record r = mRecords.get(i); |
| 352 | if ((r.events & PhoneStateListener.LISTEN_DATA_CONNECTION_STATE) != 0) { |
| 353 | try { |
| 354 | r.callback.onDataConnectionStateChanged(state); |
| 355 | } catch (RemoteException ex) { |
| 356 | remove(r.binder); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | } |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 361 | broadcastDataConnectionStateChanged(state, isDataConnectivityPossible, reason, apn, |
| 362 | interfaceName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | public void notifyDataConnectionFailed(String reason) { |
Jaikumar Ganesh | 4551565 | 2009-04-23 15:20:21 -0700 | [diff] [blame^] | 366 | if (!checkNotifyPermission("notifyDataConnectionFailed()")) { |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 367 | return; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 368 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | /* |
| 370 | * This is commented out because there is on onDataConnectionFailed callback |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 371 | * on PhoneStateListener. There should be |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | synchronized (mRecords) { |
| 373 | mDataConnectionFailedReason = reason; |
| 374 | final int N = mRecords.size(); |
| 375 | for (int i=N-1; i>=0; i--) { |
| 376 | Record r = mRecords.get(i); |
| 377 | if ((r.events & PhoneStateListener.LISTEN_DATA_CONNECTION_FAILED) != 0) { |
| 378 | // XXX |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | */ |
| 383 | broadcastDataConnectionFailed(reason); |
| 384 | } |
| 385 | |
| 386 | public void notifyCellLocation(Bundle cellLocation) { |
Jaikumar Ganesh | 4551565 | 2009-04-23 15:20:21 -0700 | [diff] [blame^] | 387 | if (!checkNotifyPermission("notifyCellLocation()")) { |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 388 | return; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 389 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | synchronized (mRecords) { |
| 391 | mCellLocation = cellLocation; |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 392 | for (int i = mRecords.size() - 1; i >= 0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | Record r = mRecords.get(i); |
| 394 | if ((r.events & PhoneStateListener.LISTEN_CELL_LOCATION) != 0) { |
| 395 | sendCellLocation(r, cellLocation); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 401 | /** |
| 402 | * Copy the service state object so they can't mess it up in the local calls |
| 403 | */ |
Jaikumar Ganesh | 4551565 | 2009-04-23 15:20:21 -0700 | [diff] [blame^] | 404 | public void sendServiceState(Record r, ServiceState state) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | try { |
| 406 | r.callback.onServiceStateChanged(new ServiceState(state)); |
| 407 | } catch (RemoteException ex) { |
| 408 | remove(r.binder); |
| 409 | } |
| 410 | } |
| 411 | |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 412 | private void sendCellLocation(Record r, Bundle cellLocation) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | try { |
| 414 | r.callback.onCellLocationChanged(new Bundle(cellLocation)); |
| 415 | } catch (RemoteException ex) { |
| 416 | remove(r.binder); |
| 417 | } |
| 418 | } |
| 419 | |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 420 | private void sendSignalStrength(Record r, SignalStrength signalStrength) { |
| 421 | try { |
| 422 | r.callback.onSignalStrengthsChanged(new SignalStrength(signalStrength)); |
| 423 | } catch (RemoteException ex) { |
| 424 | remove(r.binder); |
| 425 | } |
| 426 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | |
| 428 | @Override |
| 429 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 430 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 431 | != PackageManager.PERMISSION_GRANTED) { |
| 432 | pw.println("Permission Denial: can't dump telephony.registry from from pid=" |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 433 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | synchronized (mRecords) { |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 437 | final int recordCount = mRecords.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | pw.println("last known state:"); |
| 439 | pw.println(" mCallState=" + mCallState); |
| 440 | pw.println(" mCallIncomingNumber=" + mCallIncomingNumber); |
| 441 | pw.println(" mServiceState=" + mServiceState); |
| 442 | pw.println(" mSignalStrength=" + mSignalStrength); |
| 443 | pw.println(" mMessageWaiting=" + mMessageWaiting); |
| 444 | pw.println(" mCallForwarding=" + mCallForwarding); |
| 445 | pw.println(" mDataActivity=" + mDataActivity); |
| 446 | pw.println(" mDataConnectionState=" + mDataConnectionState); |
| 447 | pw.println(" mDataConnectionPossible=" + mDataConnectionPossible); |
| 448 | pw.println(" mDataConnectionReason=" + mDataConnectionReason); |
| 449 | pw.println(" mDataConnectionApn=" + mDataConnectionApn); |
| 450 | pw.println(" mDataConnectionInterfaceName=" + mDataConnectionInterfaceName); |
| 451 | pw.println(" mCellLocation=" + mCellLocation); |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 452 | pw.println("registrations: count=" + recordCount); |
| 453 | for (int i = 0; i < recordCount; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | Record r = mRecords.get(i); |
| 455 | pw.println(" " + r.pkgForDebug + " 0x" + Integer.toHexString(r.events)); |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | // |
| 461 | // the legacy intent broadcasting |
| 462 | // |
| 463 | |
| 464 | private void broadcastServiceStateChanged(ServiceState state) { |
| 465 | Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED); |
| 466 | Bundle data = new Bundle(); |
| 467 | state.fillInNotifierBundle(data); |
| 468 | intent.putExtras(data); |
| 469 | mContext.sendStickyBroadcast(intent); |
| 470 | } |
| 471 | |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 472 | private void broadcastSignalStrengthChanged(SignalStrength signalStrength) { |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 473 | long ident = Binder.clearCallingIdentity(); |
| 474 | try { |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 475 | mBatteryStats.notePhoneSignalStrength(signalStrength); |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 476 | } catch (RemoteException e) { |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 477 | /* The remote entity disappeared, we can safely ignore the exception. */ |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 478 | } finally { |
| 479 | Binder.restoreCallingIdentity(ident); |
| 480 | } |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 481 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | Intent intent = new Intent(TelephonyIntents.ACTION_SIGNAL_STRENGTH_CHANGED); |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 483 | Bundle data = new Bundle(); |
| 484 | signalStrength.fillInNotifierBundle(data); |
| 485 | intent.putExtras(data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | mContext.sendStickyBroadcast(intent); |
| 487 | } |
| 488 | |
| 489 | private void broadcastCallStateChanged(int state, String incomingNumber) { |
| 490 | long ident = Binder.clearCallingIdentity(); |
| 491 | try { |
| 492 | if (state == TelephonyManager.CALL_STATE_IDLE) { |
| 493 | mBatteryStats.notePhoneOff(); |
| 494 | } else { |
| 495 | mBatteryStats.notePhoneOn(); |
| 496 | } |
| 497 | } catch (RemoteException e) { |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 498 | /* The remote entity disappeared, we can safely ignore the exception. */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | } finally { |
| 500 | Binder.restoreCallingIdentity(ident); |
| 501 | } |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 502 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | Intent intent = new Intent(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 504 | intent.putExtra(Phone.STATE_KEY, DefaultPhoneNotifier.convertCallState(state).toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | if (!TextUtils.isEmpty(incomingNumber)) { |
| 506 | intent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, incomingNumber); |
| 507 | } |
| 508 | mContext.sendBroadcast(intent, android.Manifest.permission.READ_PHONE_STATE); |
| 509 | } |
| 510 | |
| 511 | private void broadcastDataConnectionStateChanged(int state, boolean isDataConnectivityPossible, |
| 512 | String reason, String apn, String interfaceName) { |
Dianne Hackborn | 627bba7 | 2009-03-24 22:32:56 -0700 | [diff] [blame] | 513 | // Note: not reporting to the battery stats service here, because the |
| 514 | // status bar takes care of that after taking into account all of the |
| 515 | // required info. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | Intent intent = new Intent(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED); |
| 517 | intent.putExtra(Phone.STATE_KEY, DefaultPhoneNotifier.convertDataState(state).toString()); |
| 518 | if (!isDataConnectivityPossible) { |
| 519 | intent.putExtra(Phone.NETWORK_UNAVAILABLE_KEY, true); |
| 520 | } |
| 521 | if (reason != null) { |
| 522 | intent.putExtra(Phone.STATE_CHANGE_REASON_KEY, reason); |
| 523 | } |
| 524 | intent.putExtra(Phone.DATA_APN_KEY, apn); |
| 525 | intent.putExtra(Phone.DATA_IFACE_NAME_KEY, interfaceName); |
| 526 | mContext.sendStickyBroadcast(intent); |
| 527 | } |
| 528 | |
| 529 | private void broadcastDataConnectionFailed(String reason) { |
| 530 | Intent intent = new Intent(TelephonyIntents.ACTION_DATA_CONNECTION_FAILED); |
| 531 | intent.putExtra(Phone.FAILURE_REASON_KEY, reason); |
| 532 | mContext.sendStickyBroadcast(intent); |
| 533 | } |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 534 | |
Jaikumar Ganesh | 4551565 | 2009-04-23 15:20:21 -0700 | [diff] [blame^] | 535 | private boolean checkNotifyPermission(String method) { |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 536 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE) |
| 537 | == PackageManager.PERMISSION_GRANTED) { |
| 538 | return true; |
| 539 | } |
| 540 | String msg = "Modify Phone State Permission Denial: " + method + " from pid=" |
Wink Saville | e9b06d7 | 2009-05-18 21:47:50 -0700 | [diff] [blame] | 541 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid(); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 542 | Log.w(TAG, msg); |
| 543 | return false; |
| 544 | } |
Jaikumar Ganesh | 4551565 | 2009-04-23 15:20:21 -0700 | [diff] [blame^] | 545 | |
| 546 | private void checkListenerPermission(int events) { |
| 547 | if ((events & PhoneStateListener.LISTEN_CELL_LOCATION) != 0) { |
| 548 | mContext.enforceCallingOrSelfPermission( |
| 549 | android.Manifest.permission.ACCESS_COARSE_LOCATION, null); |
| 550 | |
| 551 | } |
| 552 | |
| 553 | if ((events & PHONE_STATE_PERMISSION_MASK) != 0) { |
| 554 | mContext.enforceCallingOrSelfPermission( |
| 555 | android.Manifest.permission.READ_PHONE_STATE, null); |
| 556 | } |
| 557 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | } |