DefaultPhoneNotifier.java revision 7961ac2c8d2e50d879bc6d5b272b7d972a335c0e
1/* 2 * Copyright (C) 2006 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.internal.telephony; 18 19import android.net.LinkCapabilities; 20import android.net.LinkProperties; 21import android.os.Bundle; 22import android.os.RemoteException; 23import android.os.ServiceManager; 24import android.telephony.CellInfo; 25import android.telephony.ServiceState; 26import android.telephony.TelephonyManager; 27 28import com.android.internal.telephony.ITelephonyRegistry; 29 30import java.util.List; 31 32/** 33 * broadcast intents 34 */ 35public class DefaultPhoneNotifier implements PhoneNotifier { 36 37 private ITelephonyRegistry mRegistry; 38 39 /*package*/ 40 DefaultPhoneNotifier() { 41 mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService( 42 "telephony.registry")); 43 } 44 45 @Override 46 public void notifyPhoneState(Phone sender) { 47 Call ringingCall = sender.getRingingCall(); 48 String incomingNumber = ""; 49 if (ringingCall != null && ringingCall.getEarliestConnection() != null){ 50 incomingNumber = ringingCall.getEarliestConnection().getAddress(); 51 } 52 try { 53 if (mRegistry != null) { 54 mRegistry.notifyCallState(convertCallState(sender.getState()), incomingNumber); 55 } 56 } catch (RemoteException ex) { 57 // system process is dead 58 } 59 } 60 61 @Override 62 public void notifyServiceState(Phone sender) { 63 ServiceState ss = sender.getServiceState(); 64 if (ss == null) { 65 ss = new ServiceState(); 66 ss.setStateOutOfService(); 67 } 68 try { 69 if (mRegistry != null) { 70 mRegistry.notifyServiceState(ss); 71 } 72 } catch (RemoteException ex) { 73 // system process is dead 74 } 75 } 76 77 @Override 78 public void notifySignalStrength(Phone sender) { 79 try { 80 if (mRegistry != null) { 81 mRegistry.notifySignalStrength(sender.getSignalStrength()); 82 } 83 } catch (RemoteException ex) { 84 // system process is dead 85 } 86 } 87 88 @Override 89 public void notifyMessageWaitingChanged(Phone sender) { 90 try { 91 if (mRegistry != null) { 92 mRegistry.notifyMessageWaitingChanged(sender.getMessageWaitingIndicator()); 93 } 94 } catch (RemoteException ex) { 95 // system process is dead 96 } 97 } 98 99 @Override 100 public void notifyCallForwardingChanged(Phone sender) { 101 try { 102 if (mRegistry != null) { 103 mRegistry.notifyCallForwardingChanged(sender.getCallForwardingIndicator()); 104 } 105 } catch (RemoteException ex) { 106 // system process is dead 107 } 108 } 109 110 @Override 111 public void notifyDataActivity(Phone sender) { 112 try { 113 if (mRegistry != null) { 114 mRegistry.notifyDataActivity(convertDataActivityState(sender.getDataActivityState())); 115 } 116 } catch (RemoteException ex) { 117 // system process is dead 118 } 119 } 120 121 @Override 122 public void notifyDataConnection(Phone sender, String reason, String apnType, 123 PhoneConstants.DataState state) { 124 doNotifyDataConnection(sender, reason, apnType, state); 125 } 126 127 private void doNotifyDataConnection(Phone sender, String reason, String apnType, 128 PhoneConstants.DataState state) { 129 // TODO 130 // use apnType as the key to which connection we're talking about. 131 // pass apnType back up to fetch particular for this one. 132 TelephonyManager telephony = TelephonyManager.getDefault(); 133 LinkProperties linkProperties = null; 134 LinkCapabilities linkCapabilities = null; 135 boolean roaming = false; 136 137 if (state == PhoneConstants.DataState.CONNECTED) { 138 linkProperties = sender.getLinkProperties(apnType); 139 linkCapabilities = sender.getLinkCapabilities(apnType); 140 } 141 ServiceState ss = sender.getServiceState(); 142 if (ss != null) roaming = ss.getRoaming(); 143 144 try { 145 if (mRegistry != null) { 146 mRegistry.notifyDataConnection( 147 convertDataState(state), 148 sender.isDataConnectivityPossible(apnType), reason, 149 sender.getActiveApnHost(apnType), 150 apnType, 151 linkProperties, 152 linkCapabilities, 153 ((telephony!=null) ? telephony.getNetworkType() : 154 TelephonyManager.NETWORK_TYPE_UNKNOWN), 155 roaming); 156 } 157 } catch (RemoteException ex) { 158 // system process is dead 159 } 160 } 161 162 @Override 163 public void notifyDataConnectionFailed(Phone sender, String reason, String apnType) { 164 try { 165 if (mRegistry != null) { 166 mRegistry.notifyDataConnectionFailed(reason, apnType); 167 } 168 } catch (RemoteException ex) { 169 // system process is dead 170 } 171 } 172 173 @Override 174 public void notifyCellLocation(Phone sender) { 175 Bundle data = new Bundle(); 176 sender.getCellLocation().fillInNotifierBundle(data); 177 try { 178 if (mRegistry != null) { 179 mRegistry.notifyCellLocation(data); 180 } 181 } catch (RemoteException ex) { 182 // system process is dead 183 } 184 } 185 186 @Override 187 public void notifyCellInfo(Phone sender, List<CellInfo> cellInfo) { 188 try { 189 if (mRegistry != null) { 190 mRegistry.notifyCellInfo(cellInfo); 191 } 192 } catch (RemoteException ex) { 193 194 } 195 } 196 197 @Override 198 public void notifyOtaspChanged(Phone sender, int otaspMode) { 199 try { 200 if (mRegistry != null) { 201 mRegistry.notifyOtaspChanged(otaspMode); 202 } 203 } catch (RemoteException ex) { 204 // system process is dead 205 } 206 } 207 208 /** 209 * Convert the {@link PhoneConstants.State} enum into the TelephonyManager.CALL_STATE_* 210 * constants for the public API. 211 */ 212 public static int convertCallState(PhoneConstants.State state) { 213 switch (state) { 214 case RINGING: 215 return TelephonyManager.CALL_STATE_RINGING; 216 case OFFHOOK: 217 return TelephonyManager.CALL_STATE_OFFHOOK; 218 default: 219 return TelephonyManager.CALL_STATE_IDLE; 220 } 221 } 222 223 /** 224 * Convert the TelephonyManager.CALL_STATE_* constants into the 225 * {@link PhoneConstants.State} enum for the public API. 226 */ 227 public static PhoneConstants.State convertCallState(int state) { 228 switch (state) { 229 case TelephonyManager.CALL_STATE_RINGING: 230 return PhoneConstants.State.RINGING; 231 case TelephonyManager.CALL_STATE_OFFHOOK: 232 return PhoneConstants.State.OFFHOOK; 233 default: 234 return PhoneConstants.State.IDLE; 235 } 236 } 237 238 /** 239 * Convert the {@link PhoneConstants.DataState} enum into the TelephonyManager.DATA_* constants 240 * for the public API. 241 */ 242 public static int convertDataState(PhoneConstants.DataState state) { 243 switch (state) { 244 case CONNECTING: 245 return TelephonyManager.DATA_CONNECTING; 246 case CONNECTED: 247 return TelephonyManager.DATA_CONNECTED; 248 case SUSPENDED: 249 return TelephonyManager.DATA_SUSPENDED; 250 default: 251 return TelephonyManager.DATA_DISCONNECTED; 252 } 253 } 254 255 /** 256 * Convert the TelephonyManager.DATA_* constants into {@link PhoneConstants.DataState} enum 257 * for the public API. 258 */ 259 public static PhoneConstants.DataState convertDataState(int state) { 260 switch (state) { 261 case TelephonyManager.DATA_CONNECTING: 262 return PhoneConstants.DataState.CONNECTING; 263 case TelephonyManager.DATA_CONNECTED: 264 return PhoneConstants.DataState.CONNECTED; 265 case TelephonyManager.DATA_SUSPENDED: 266 return PhoneConstants.DataState.SUSPENDED; 267 default: 268 return PhoneConstants.DataState.DISCONNECTED; 269 } 270 } 271 272 /** 273 * Convert the {@link Phone.DataActivityState} enum into the TelephonyManager.DATA_* constants 274 * for the public API. 275 */ 276 public static int convertDataActivityState(Phone.DataActivityState state) { 277 switch (state) { 278 case DATAIN: 279 return TelephonyManager.DATA_ACTIVITY_IN; 280 case DATAOUT: 281 return TelephonyManager.DATA_ACTIVITY_OUT; 282 case DATAINANDOUT: 283 return TelephonyManager.DATA_ACTIVITY_INOUT; 284 case DORMANT: 285 return TelephonyManager.DATA_ACTIVITY_DORMANT; 286 default: 287 return TelephonyManager.DATA_ACTIVITY_NONE; 288 } 289 } 290 291 /** 292 * Convert the TelephonyManager.DATA_* constants into the {@link Phone.DataActivityState} enum 293 * for the public API. 294 */ 295 public static Phone.DataActivityState convertDataActivityState(int state) { 296 switch (state) { 297 case TelephonyManager.DATA_ACTIVITY_IN: 298 return Phone.DataActivityState.DATAIN; 299 case TelephonyManager.DATA_ACTIVITY_OUT: 300 return Phone.DataActivityState.DATAOUT; 301 case TelephonyManager.DATA_ACTIVITY_INOUT: 302 return Phone.DataActivityState.DATAINANDOUT; 303 case TelephonyManager.DATA_ACTIVITY_DORMANT: 304 return Phone.DataActivityState.DORMANT; 305 default: 306 return Phone.DataActivityState.NONE; 307 } 308 } 309} 310