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 17 package android.telephony.cdma; 18 19 import android.os.Bundle; 20 import android.telephony.CellLocation; 21 22 /** 23 * Represents the cell location on a CDMA phone. 24 */ 25 public class CdmaCellLocation extends CellLocation { 26 private int mBaseStationId = -1; 27 28 /** 29 * @hide 30 */ 31 public final static int INVALID_LAT_LONG = Integer.MAX_VALUE; 32 33 /** 34 * Latitude is a decimal number as specified in 3GPP2 C.S0005-A v6.0. 35 * It is represented in units of 0.25 seconds and ranges from -1296000 36 * to 1296000, both values inclusive (corresponding to a range of -90 37 * to +90 degrees). Integer.MAX_VALUE is considered invalid value. 38 */ 39 private int mBaseStationLatitude = INVALID_LAT_LONG; 40 41 /** 42 * Longitude is a decimal number as specified in 3GPP2 C.S0005-A v6.0. 43 * It is represented in units of 0.25 seconds and ranges from -2592000 44 * to 2592000, both values inclusive (corresponding to a range of -180 45 * to +180 degrees). Integer.MAX_VALUE is considered invalid value. 46 */ 47 private int mBaseStationLongitude = INVALID_LAT_LONG; 48 49 private int mSystemId = -1; 50 private int mNetworkId = -1; 51 52 /** 53 * Empty constructor. 54 * Initializes the BID, SID, NID and base station latitude and longitude 55 * to invalid values. 56 */ CdmaCellLocation()57 public CdmaCellLocation() { 58 this.mBaseStationId = -1; 59 this.mBaseStationLatitude = INVALID_LAT_LONG; 60 this.mBaseStationLongitude = INVALID_LAT_LONG; 61 this.mSystemId = -1; 62 this.mNetworkId = -1; 63 } 64 65 /** 66 * Initialize the object from a bundle. 67 */ CdmaCellLocation(Bundle bundle)68 public CdmaCellLocation(Bundle bundle) { 69 this.mBaseStationId = bundle.getInt("baseStationId", mBaseStationId); 70 this.mBaseStationLatitude = bundle.getInt("baseStationLatitude", mBaseStationLatitude); 71 this.mBaseStationLongitude = bundle.getInt("baseStationLongitude", mBaseStationLongitude); 72 this.mSystemId = bundle.getInt("systemId", mSystemId); 73 this.mNetworkId = bundle.getInt("networkId", mNetworkId); 74 } 75 76 /** 77 * @return cdma base station identification number, -1 if unknown 78 */ getBaseStationId()79 public int getBaseStationId() { 80 return this.mBaseStationId; 81 } 82 83 /** 84 * Latitude is a decimal number as specified in 3GPP2 C.S0005-A v6.0. 85 * (http://www.3gpp2.org/public_html/specs/C.S0005-A_v6.0.pdf) 86 * It is represented in units of 0.25 seconds and ranges from -1296000 87 * to 1296000, both values inclusive (corresponding to a range of -90 88 * to +90 degrees). Integer.MAX_VALUE is considered invalid value. 89 * 90 * @return cdma base station latitude in units of 0.25 seconds, Integer.MAX_VALUE if unknown 91 */ getBaseStationLatitude()92 public int getBaseStationLatitude() { 93 return this.mBaseStationLatitude; 94 } 95 96 /** 97 * Longitude is a decimal number as specified in 3GPP2 C.S0005-A v6.0. 98 * (http://www.3gpp2.org/public_html/specs/C.S0005-A_v6.0.pdf) 99 * It is represented in units of 0.25 seconds and ranges from -2592000 100 * to 2592000, both values inclusive (corresponding to a range of -180 101 * to +180 degrees). Integer.MAX_VALUE is considered invalid value. 102 * 103 * @return cdma base station longitude in units of 0.25 seconds, Integer.MAX_VALUE if unknown 104 */ getBaseStationLongitude()105 public int getBaseStationLongitude() { 106 return this.mBaseStationLongitude; 107 } 108 109 /** 110 * @return cdma system identification number, -1 if unknown 111 */ getSystemId()112 public int getSystemId() { 113 return this.mSystemId; 114 } 115 116 /** 117 * @return cdma network identification number, -1 if unknown 118 */ getNetworkId()119 public int getNetworkId() { 120 return this.mNetworkId; 121 } 122 123 /** 124 * Invalidate this object. The cell location data is set to invalid values. 125 */ 126 @Override setStateInvalid()127 public void setStateInvalid() { 128 this.mBaseStationId = -1; 129 this.mBaseStationLatitude = INVALID_LAT_LONG; 130 this.mBaseStationLongitude = INVALID_LAT_LONG; 131 this.mSystemId = -1; 132 this.mNetworkId = -1; 133 } 134 135 /** 136 * Set the cell location data. 137 */ setCellLocationData(int baseStationId, int baseStationLatitude, int baseStationLongitude)138 public void setCellLocationData(int baseStationId, int baseStationLatitude, 139 int baseStationLongitude) { 140 // The following values have to be written in the correct sequence 141 this.mBaseStationId = baseStationId; 142 this.mBaseStationLatitude = baseStationLatitude; //values[2]; 143 this.mBaseStationLongitude = baseStationLongitude; //values[3]; 144 } 145 146 /** 147 * Set the cell location data. 148 */ setCellLocationData(int baseStationId, int baseStationLatitude, int baseStationLongitude, int systemId, int networkId)149 public void setCellLocationData(int baseStationId, int baseStationLatitude, 150 int baseStationLongitude, int systemId, int networkId) { 151 // The following values have to be written in the correct sequence 152 this.mBaseStationId = baseStationId; 153 this.mBaseStationLatitude = baseStationLatitude; //values[2]; 154 this.mBaseStationLongitude = baseStationLongitude; //values[3]; 155 this.mSystemId = systemId; 156 this.mNetworkId = networkId; 157 } 158 159 @Override hashCode()160 public int hashCode() { 161 return this.mBaseStationId ^ this.mBaseStationLatitude ^ this.mBaseStationLongitude 162 ^ this.mSystemId ^ this.mNetworkId; 163 } 164 165 @Override equals(Object o)166 public boolean equals(Object o) { 167 CdmaCellLocation s; 168 169 try { 170 s = (CdmaCellLocation)o; 171 } catch (ClassCastException ex) { 172 return false; 173 } 174 175 if (o == null) { 176 return false; 177 } 178 179 return (equalsHandlesNulls(this.mBaseStationId, s.mBaseStationId) && 180 equalsHandlesNulls(this.mBaseStationLatitude, s.mBaseStationLatitude) && 181 equalsHandlesNulls(this.mBaseStationLongitude, s.mBaseStationLongitude) && 182 equalsHandlesNulls(this.mSystemId, s.mSystemId) && 183 equalsHandlesNulls(this.mNetworkId, s.mNetworkId) 184 ); 185 } 186 187 @Override toString()188 public String toString() { 189 return "[" + this.mBaseStationId + "," 190 + this.mBaseStationLatitude + "," 191 + this.mBaseStationLongitude + "," 192 + this.mSystemId + "," 193 + this.mNetworkId + "]"; 194 } 195 196 /** 197 * Test whether two objects hold the same data values or both are null 198 * 199 * @param a first obj 200 * @param b second obj 201 * @return true if two objects equal or both are null 202 */ equalsHandlesNulls(Object a, Object b)203 private static boolean equalsHandlesNulls(Object a, Object b) { 204 return (a == null) ? (b == null) : a.equals (b); 205 } 206 207 /** 208 * Fill the cell location data into the intent notifier Bundle based on service state 209 * 210 * @param bundleToFill intent notifier Bundle 211 */ fillInNotifierBundle(Bundle bundleToFill)212 public void fillInNotifierBundle(Bundle bundleToFill) { 213 bundleToFill.putInt("baseStationId", this.mBaseStationId); 214 bundleToFill.putInt("baseStationLatitude", this.mBaseStationLatitude); 215 bundleToFill.putInt("baseStationLongitude", this.mBaseStationLongitude); 216 bundleToFill.putInt("systemId", this.mSystemId); 217 bundleToFill.putInt("networkId", this.mNetworkId); 218 } 219 220 /** 221 * @hide 222 */ isEmpty()223 public boolean isEmpty() { 224 return (this.mBaseStationId == -1 && 225 this.mBaseStationLatitude == INVALID_LAT_LONG && 226 this.mBaseStationLongitude == INVALID_LAT_LONG && 227 this.mSystemId == -1 && 228 this.mNetworkId == -1); 229 } 230 231 /** 232 * Converts latitude or longitude from 0.25 seconds (as defined in the 233 * 3GPP2 C.S0005-A v6.0 standard) to decimal degrees 234 * 235 * @param quartSec latitude or longitude in 0.25 seconds units 236 * @return latitude or longitude in decimal degrees units 237 * @throws IllegalArgumentException if value is less than -2592000, 238 * greater than 2592000, or is not a number. 239 */ convertQuartSecToDecDegrees(int quartSec)240 public static double convertQuartSecToDecDegrees(int quartSec) { 241 if(Double.isNaN(quartSec) || quartSec < -2592000 || quartSec > 2592000){ 242 // Invalid value 243 throw new IllegalArgumentException("Invalid coordiante value:" + quartSec); 244 } 245 return ((double)quartSec) / (3600 * 4); 246 } 247 248 } 249 250 251