ServiceState.java revision e9b06d754af03faf27012fbed1e7559ec1ba7c79
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 android.telephony; 18 19import android.os.Bundle; 20import android.os.Parcel; 21import android.os.Parcelable; 22import android.util.Log; 23 24/** 25 * Contains phone state and service related information. 26 * 27 * The following phone information is included in returned ServiceState: 28 * 29 * <ul> 30 * <li>Service state: IN_SERVICE, OUT_OF_SERVICE, EMERGENCY_ONLY, POWER_OFF 31 * <li>Roaming indicator 32 * <li>Operator name, short name and numeric id 33 * <li>Network selection mode 34 * </ul> 35 */ 36public class ServiceState implements Parcelable { 37 38 static final String LOG_TAG = "PHONE"; 39 40 /** 41 * Normal operation condition, the phone is registered 42 * with an operator either in home network or in roaming. 43 */ 44 public static final int STATE_IN_SERVICE = 0; 45 46 /** 47 * Phone is not registered with any operator, the phone 48 * can be currently searching a new operator to register to, or not 49 * searching to registration at all, or registration is denied, or radio 50 * signal is not available. 51 */ 52 public static final int STATE_OUT_OF_SERVICE = 1; 53 54 /** 55 * The phone is registered and locked. Only emergency numbers are allowed. {@more} 56 */ 57 public static final int STATE_EMERGENCY_ONLY = 2; 58 59 /** 60 * Radio of telephony is explictly powered off. 61 */ 62 public static final int STATE_POWER_OFF = 3; 63 64 65 /** 66 * Available radio technologies for GSM, UMTS and CDMA. 67 */ 68 /** @hide */ 69 public static final int RADIO_TECHNOLOGY_UNKNOWN = 0; 70 /** @hide */ 71 public static final int RADIO_TECHNOLOGY_GPRS = 1; 72 /** @hide */ 73 public static final int RADIO_TECHNOLOGY_EDGE = 2; 74 /** @hide */ 75 public static final int RADIO_TECHNOLOGY_UMTS = 3; 76 /** @hide */ 77 public static final int RADIO_TECHNOLOGY_IS95A = 4; 78 /** @hide */ 79 public static final int RADIO_TECHNOLOGY_IS95B = 5; 80 /** @hide */ 81 public static final int RADIO_TECHNOLOGY_1xRTT = 6; 82 /** @hide */ 83 public static final int RADIO_TECHNOLOGY_EVDO_0 = 7; 84 /** @hide */ 85 public static final int RADIO_TECHNOLOGY_EVDO_A = 8; 86 87 /** 88 * Available registration states for GSM, UMTS and CDMA. 89 */ 90 /** @hide */ 91 public static final int REGISTRATION_STATE_NOT_REGISTERED_AND_NOT_SEARCHING = 0; 92 /** @hide */ 93 public static final int REGISTRATION_STATE_HOME_NETWORK = 1; 94 /** @hide */ 95 public static final int REGISTRATION_STATE_NOT_REGISTERED_AND_SEARCHING = 2; 96 /** @hide */ 97 public static final int REGISTRATION_STATE_REGISTRATION_DENIED = 3; 98 /** @hide */ 99 public static final int REGISTRATION_STATE_UNKNOWN = 4; 100 /** @hide */ 101 public static final int REGISTRATION_STATE_ROAMING = 5; 102 103 private int mState = STATE_OUT_OF_SERVICE; 104 private boolean mRoaming; 105 private String mOperatorAlphaLong; 106 private String mOperatorAlphaShort; 107 private String mOperatorNumeric; 108 private boolean mIsManualNetworkSelection; 109 110 //***** CDMA 111 private int mRadioTechnology; 112 private boolean mCssIndicator; 113 private int mNetworkId; 114 private int mSystemId; 115 private int mCdmaRoamingIndicator; 116 private int mCdmaDefaultRoamingIndicator; 117 118 /** 119 * Create a new ServiceState from a intent notifier Bundle 120 * 121 * This method is used by PhoneStateIntentReceiver and maybe by 122 * external applications. 123 * 124 * @param m Bundle from intent notifier 125 * @return newly created ServiceState 126 * @hide 127 */ 128 public static ServiceState newFromBundle(Bundle m) { 129 ServiceState ret; 130 ret = new ServiceState(); 131 ret.setFromNotifierBundle(m); 132 return ret; 133 } 134 135 /** 136 * Empty constructor 137 */ 138 public ServiceState() { 139 } 140 141 /** 142 * Copy constructors 143 * 144 * @param s Source service state 145 */ 146 public ServiceState(ServiceState s) { 147 copyFrom(s); 148 } 149 150 protected void copyFrom(ServiceState s) { 151 mState = s.mState; 152 mRoaming = s.mRoaming; 153 mOperatorAlphaLong = s.mOperatorAlphaLong; 154 mOperatorAlphaShort = s.mOperatorAlphaShort; 155 mOperatorNumeric = s.mOperatorNumeric; 156 mIsManualNetworkSelection = s.mIsManualNetworkSelection; 157 mRadioTechnology = s.mRadioTechnology; 158 mCssIndicator = s.mCssIndicator; 159 mNetworkId = s.mNetworkId; 160 mSystemId = s.mSystemId; 161 mCdmaRoamingIndicator = s.mCdmaRoamingIndicator; 162 mCdmaDefaultRoamingIndicator = s.mCdmaDefaultRoamingIndicator; 163 } 164 165 /** 166 * Construct a ServiceState object from the given parcel. 167 */ 168 public ServiceState(Parcel in) { 169 mState = in.readInt(); 170 mRoaming = in.readInt() != 0; 171 mOperatorAlphaLong = in.readString(); 172 mOperatorAlphaShort = in.readString(); 173 mOperatorNumeric = in.readString(); 174 mIsManualNetworkSelection = in.readInt() != 0; 175 mRadioTechnology = in.readInt(); 176 mCssIndicator = (in.readInt() != 0); 177 mNetworkId = in.readInt(); 178 mSystemId = in.readInt(); 179 mCdmaRoamingIndicator = in.readInt(); 180 mCdmaDefaultRoamingIndicator = in.readInt(); 181 } 182 183 public void writeToParcel(Parcel out, int flags) { 184 out.writeInt(mState); 185 out.writeInt(mRoaming ? 1 : 0); 186 out.writeString(mOperatorAlphaLong); 187 out.writeString(mOperatorAlphaShort); 188 out.writeString(mOperatorNumeric); 189 out.writeInt(mIsManualNetworkSelection ? 1 : 0); 190 out.writeInt(mRadioTechnology); 191 out.writeInt(mCssIndicator ? 1 : 0); 192 out.writeInt(mNetworkId); 193 out.writeInt(mSystemId); 194 out.writeInt(mCdmaRoamingIndicator); 195 out.writeInt(mCdmaDefaultRoamingIndicator); 196 } 197 198 public int describeContents() { 199 return 0; 200 } 201 202 public static final Parcelable.Creator<ServiceState> CREATOR = new Parcelable.Creator() { 203 public ServiceState createFromParcel(Parcel in) { 204 return new ServiceState(in); 205 } 206 207 public ServiceState[] newArray(int size) { 208 return new ServiceState[size]; 209 } 210 }; 211 212 /** 213 * Get current servcie state of phone 214 * 215 * @see #STATE_IN_SERVICE 216 * @see #STATE_OUT_OF_SERVICE 217 * @see #STATE_EMERGENCY_ONLY 218 * @see #STATE_POWER_OFF 219 */ 220 public int getState() { 221 return mState; 222 } 223 224 /** 225 * Get current roaming indicator of phone 226 * (note: not just decoding from TS 27.007 7.2) 227 * 228 * @return true if TS 27.007 7.2 roaming is true 229 * and ONS is different from SPN 230 * 231 */ 232 public boolean getRoaming() { 233 return mRoaming; 234 } 235 236 /** 237 * @hide 238 */ 239 public int getCdmaRoamingIndicator(){ 240 return this.mCdmaRoamingIndicator; 241 } 242 243 /** 244 * @hide 245 */ 246 public int getCdmaDefaultRoamingIndicator(){ 247 return this.mCdmaDefaultRoamingIndicator; 248 } 249 250 /** 251 * Get current registered operator name in long alphanumeric format 252 * 253 * In GSM/UMTS, long format can be upto 16 characters long 254 * In CDMA, returns the ERI text, if set, otherwise the ONS 255 * 256 * @return long name of operator, null if unregistered or unknown 257 */ 258 public String getOperatorAlphaLong() { 259 return mOperatorAlphaLong; 260 } 261 262 /** 263 * Get current registered operator name in short lphanumeric format 264 * 265 * In GSM/UMST, short format can be upto 8 characters long 266 * 267 * @return short name of operator, null if unregistered or unknown 268 */ 269 public String getOperatorAlphaShort() { 270 return mOperatorAlphaShort; 271 } 272 273 /** 274 * Get current registered operator numeric id 275 * 276 * In GSM/UMTS, numeric format is 3 digit country code plus 2 or 3 digit 277 * network code 278 * 279 * The country code can be decoded using MccTable.countryCodeForMcc() 280 * 281 * @return numeric format of operator, null if unregistered or unknown 282 */ 283 public String getOperatorNumeric() { 284 return mOperatorNumeric; 285 } 286 287 /** 288 * Get current network selection mode 289 * 290 * @return true if manual mode, false if automatic mode 291 */ 292 public boolean getIsManualSelection() { 293 return mIsManualNetworkSelection; 294 } 295 296 @Override 297 public int hashCode() { 298 return ((mState * 0x1234) 299 + (mRoaming ? 1 : 0) 300 + (mIsManualNetworkSelection ? 1 : 0) 301 + ((null == mOperatorAlphaLong) ? 0 : mOperatorAlphaLong.hashCode()) 302 + ((null == mOperatorAlphaShort) ? 0 : mOperatorAlphaShort.hashCode()) 303 + ((null == mOperatorNumeric) ? 0 : mOperatorNumeric.hashCode()) 304 + mCdmaRoamingIndicator 305 + mCdmaDefaultRoamingIndicator); 306 } 307 308 @Override 309 public boolean equals (Object o) { 310 ServiceState s; 311 312 try { 313 s = (ServiceState) o; 314 } catch (ClassCastException ex) { 315 return false; 316 } 317 318 if (o == null) { 319 return false; 320 } 321 322 return (mState == s.mState 323 && mRoaming == s.mRoaming 324 && mIsManualNetworkSelection == s.mIsManualNetworkSelection 325 && equalsHandlesNulls(mOperatorAlphaLong, s.mOperatorAlphaLong) 326 && equalsHandlesNulls(mOperatorAlphaShort, s.mOperatorAlphaShort) 327 && equalsHandlesNulls(mOperatorNumeric, s.mOperatorNumeric) 328 && equalsHandlesNulls(mRadioTechnology, s.mRadioTechnology) 329 && equalsHandlesNulls(mCssIndicator, s.mCssIndicator) 330 && equalsHandlesNulls(mNetworkId, s.mNetworkId) 331 && equalsHandlesNulls(mSystemId, s.mSystemId) 332 && equalsHandlesNulls(mCdmaRoamingIndicator, s.mCdmaRoamingIndicator) 333 && equalsHandlesNulls(mCdmaDefaultRoamingIndicator, 334 s.mCdmaDefaultRoamingIndicator)); 335 } 336 337 @Override 338 public String toString() { 339 String radioTechnology = new String("Error in radioTechnology"); 340 341 switch(this.mRadioTechnology) { 342 case 0: 343 radioTechnology = "Unknown"; 344 break; 345 case 1: 346 radioTechnology = "GPRS"; 347 break; 348 case 2: 349 radioTechnology = "EDGE"; 350 break; 351 case 3: 352 radioTechnology = "UMTS"; 353 break; 354 case 4: 355 radioTechnology = "IS95A"; 356 break; 357 case 5: 358 radioTechnology = "IS95B"; 359 break; 360 case 6: 361 radioTechnology = "1xRTT"; 362 break; 363 case 7: 364 radioTechnology = "EvDo rev. 0"; 365 break; 366 case 8: 367 radioTechnology = "EvDo rev. A"; 368 break; 369 default: 370 Log.w(LOG_TAG, "mRadioTechnology variable out of range."); 371 break; 372 } 373 374 return (mState + " " + (mRoaming ? "roaming" : "home") 375 + " " + mOperatorAlphaLong 376 + " " + mOperatorAlphaShort 377 + " " + mOperatorNumeric 378 + " " + (mIsManualNetworkSelection ? "(manual)" : "") 379 + " " + radioTechnology 380 + " " + (mCssIndicator ? "CSS supported" : "CSS not supported") 381 + " " + mNetworkId 382 + " " + mSystemId 383 + "RoamInd: " + mCdmaRoamingIndicator 384 + "DefRoamInd: " + mCdmaDefaultRoamingIndicator); 385 } 386 387 public void setStateOutOfService() { 388 mState = STATE_OUT_OF_SERVICE; 389 mRoaming = false; 390 mOperatorAlphaLong = null; 391 mOperatorAlphaShort = null; 392 mOperatorNumeric = null; 393 mIsManualNetworkSelection = false; 394 mRadioTechnology = 0; 395 mCssIndicator = false; 396 mNetworkId = -1; 397 mSystemId = -1; 398 mCdmaRoamingIndicator = -1; 399 mCdmaDefaultRoamingIndicator = -1; 400 } 401 402 public void setStateOff() { 403 mState = STATE_POWER_OFF; 404 mRoaming = false; 405 mOperatorAlphaLong = null; 406 mOperatorAlphaShort = null; 407 mOperatorNumeric = null; 408 mIsManualNetworkSelection = false; 409 mRadioTechnology = 0; 410 mCssIndicator = false; 411 mNetworkId = -1; 412 mSystemId = -1; 413 mCdmaRoamingIndicator = -1; 414 mCdmaDefaultRoamingIndicator = -1; 415 } 416 417 public void setState(int state) { 418 mState = state; 419 } 420 421 public void setRoaming(boolean roaming) { 422 mRoaming = roaming; 423 } 424 425 /** 426 * @hide 427 */ 428 public void setCdmaRoamingIndicator(int roaming) { 429 this.mCdmaRoamingIndicator = roaming; 430 } 431 432 /** 433 * @hide 434 */ 435 public void setCdmaDefaultRoamingIndicator (int roaming) { 436 this.mCdmaDefaultRoamingIndicator = roaming; 437 } 438 439 public void setOperatorName(String longName, String shortName, String numeric) { 440 mOperatorAlphaLong = longName; 441 mOperatorAlphaShort = shortName; 442 mOperatorNumeric = numeric; 443 } 444 445 /** 446 * In CDMA mOperatorAlphaLong can be set from the ERI 447 * text, this is done from the CDMAPhone and not from the CdmaServiceStateTracker 448 * 449 * @hide 450 */ 451 public void setCdmaEriText(String longName) { 452 mOperatorAlphaLong = longName; 453 } 454 455 public void setIsManualSelection(boolean isManual) { 456 mIsManualNetworkSelection = isManual; 457 } 458 459 /** 460 * Test whether two objects hold the same data values or both are null 461 * 462 * @param a first obj 463 * @param b second obj 464 * @return true if two objects equal or both are null 465 */ 466 private static boolean equalsHandlesNulls (Object a, Object b) { 467 return (a == null) ? (b == null) : a.equals (b); 468 } 469 470 /** 471 * Set ServiceState based on intent notifier map 472 * 473 * @param m intent notifier map 474 * @hide 475 */ 476 private void setFromNotifierBundle(Bundle m) { 477 mState = m.getInt("state"); 478 mRoaming = m.getBoolean("roaming"); 479 mOperatorAlphaLong = m.getString("operator-alpha-long"); 480 mOperatorAlphaShort = m.getString("operator-alpha-short"); 481 mOperatorNumeric = m.getString("operator-numeric"); 482 mIsManualNetworkSelection = m.getBoolean("manual"); 483 mRadioTechnology = m.getInt("radioTechnology"); 484 mCssIndicator = m.getBoolean("cssIndicator"); 485 mNetworkId = m.getInt("networkId"); 486 mSystemId = m.getInt("systemId"); 487 mCdmaRoamingIndicator = m.getInt("cdmaRoamingIndicator"); 488 mCdmaDefaultRoamingIndicator = m.getInt("cdmaDefaultRoamingIndicator"); 489 } 490 491 /** 492 * Set intent notifier Bundle based on service state 493 * 494 * @param m intent notifier Bundle 495 * @hide 496 */ 497 public void fillInNotifierBundle(Bundle m) { 498 m.putInt("state", mState); 499 m.putBoolean("roaming", Boolean.valueOf(mRoaming)); 500 m.putString("operator-alpha-long", mOperatorAlphaLong); 501 m.putString("operator-alpha-short", mOperatorAlphaShort); 502 m.putString("operator-numeric", mOperatorNumeric); 503 m.putBoolean("manual", Boolean.valueOf(mIsManualNetworkSelection)); 504 m.putInt("radioTechnology", mRadioTechnology); 505 m.putBoolean("cssIndicator", mCssIndicator); 506 m.putInt("networkId", mNetworkId); 507 m.putInt("systemId", mSystemId); 508 m.putInt("cdmaRoamingIndicator", mCdmaRoamingIndicator); 509 m.putInt("cdmaDefaultRoamingIndicator", mCdmaDefaultRoamingIndicator); 510 } 511 512 //***** CDMA 513 /** @hide */ 514 public void setRadioTechnology(int state) { 515 this.mRadioTechnology = state; 516 } 517 518 /** @hide */ 519 public void setCssIndicator(int css) { 520 this.mCssIndicator = (css != 0); 521 } 522 523 /** @hide */ 524 public void setSystemAndNetworkId(int systemId, int networkId) { 525 this.mSystemId = systemId; 526 this.mNetworkId = networkId; 527 } 528 529 /** @hide */ 530 public int getRadioTechnology() { 531 return this.mRadioTechnology; 532 } 533 534 /** @hide */ 535 public int getCssIndicator() { 536 return this.mCssIndicator ? 1 : 0; 537 } 538 539 /** @hide */ 540 public int getNetworkId() { 541 return this.mNetworkId; 542 } 543 544 /** @hide */ 545 public int getSystemId() { 546 return this.mSystemId; 547 } 548} 549