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