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.cdma;
18
19import android.os.Bundle;
20import android.telephony.CellLocation;
21
22/**
23 * Represents the cell location on a CDMA phone.
24 */
25public 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     */
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     */
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     */
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     */
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     */
105    public int getBaseStationLongitude() {
106        return this.mBaseStationLongitude;
107    }
108
109    /**
110     * @return cdma system identification number, -1 if unknown
111     */
112    public int getSystemId() {
113        return this.mSystemId;
114    }
115
116    /**
117     * @return cdma network identification number, -1 if unknown
118     */
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    public void setStateInvalid() {
127        this.mBaseStationId = -1;
128        this.mBaseStationLatitude = INVALID_LAT_LONG;
129        this.mBaseStationLongitude = INVALID_LAT_LONG;
130        this.mSystemId = -1;
131        this.mNetworkId = -1;
132    }
133
134    /**
135     * Set the cell location data.
136     */
137     public void setCellLocationData(int baseStationId, int baseStationLatitude,
138         int baseStationLongitude) {
139         // The following values have to be written in the correct sequence
140         this.mBaseStationId = baseStationId;
141         this.mBaseStationLatitude = baseStationLatitude;   //values[2];
142         this.mBaseStationLongitude = baseStationLongitude; //values[3];
143    }
144
145    /**
146     * Set the cell location data.
147     */
148     public void setCellLocationData(int baseStationId, int baseStationLatitude,
149         int baseStationLongitude, int systemId, int networkId) {
150         // The following values have to be written in the correct sequence
151         this.mBaseStationId = baseStationId;
152         this.mBaseStationLatitude = baseStationLatitude;   //values[2];
153         this.mBaseStationLongitude = baseStationLongitude; //values[3];
154         this.mSystemId = systemId;
155         this.mNetworkId = networkId;
156    }
157
158    @Override
159    public int hashCode() {
160        return this.mBaseStationId ^ this.mBaseStationLatitude ^ this.mBaseStationLongitude
161                ^ this.mSystemId ^ this.mNetworkId;
162    }
163
164    @Override
165    public boolean equals(Object o) {
166        CdmaCellLocation s;
167
168        try {
169            s = (CdmaCellLocation)o;
170        } catch (ClassCastException ex) {
171            return false;
172        }
173
174        if (o == null) {
175            return false;
176        }
177
178        return (equalsHandlesNulls(this.mBaseStationId, s.mBaseStationId) &&
179                equalsHandlesNulls(this.mBaseStationLatitude, s.mBaseStationLatitude) &&
180                equalsHandlesNulls(this.mBaseStationLongitude, s.mBaseStationLongitude) &&
181                equalsHandlesNulls(this.mSystemId, s.mSystemId) &&
182                equalsHandlesNulls(this.mNetworkId, s.mNetworkId)
183        );
184    }
185
186    @Override
187    public String toString() {
188        return "[" + this.mBaseStationId + ","
189                   + this.mBaseStationLatitude + ","
190                   + this.mBaseStationLongitude + ","
191                   + this.mSystemId + ","
192                   + this.mNetworkId + "]";
193    }
194
195    /**
196     * Test whether two objects hold the same data values or both are null
197     *
198     * @param a first obj
199     * @param b second obj
200     * @return true if two objects equal or both are null
201     */
202    private static boolean equalsHandlesNulls(Object a, Object b) {
203        return (a == null) ? (b == null) : a.equals (b);
204    }
205
206    /**
207     * Fill the cell location data into the intent notifier Bundle based on service state
208     *
209     * @param bundleToFill intent notifier Bundle
210     */
211    public void fillInNotifierBundle(Bundle bundleToFill) {
212        bundleToFill.putInt("baseStationId", this.mBaseStationId);
213        bundleToFill.putInt("baseStationLatitude", this.mBaseStationLatitude);
214        bundleToFill.putInt("baseStationLongitude", this.mBaseStationLongitude);
215        bundleToFill.putInt("systemId", this.mSystemId);
216        bundleToFill.putInt("networkId", this.mNetworkId);
217    }
218
219    /**
220     * @hide
221     */
222    public boolean isEmpty() {
223        return (this.mBaseStationId == -1 &&
224                this.mBaseStationLatitude == INVALID_LAT_LONG &&
225                this.mBaseStationLongitude == INVALID_LAT_LONG &&
226                this.mSystemId == -1 &&
227                this.mNetworkId == -1);
228    }
229
230    /**
231     * Converts latitude or longitude from 0.25 seconds (as defined in the
232     * 3GPP2 C.S0005-A v6.0 standard) to decimal degrees
233     *
234     * @param quartSec latitude or longitude in 0.25 seconds units
235     * @return latitude or longitude in decimal degrees units
236     * @throws IllegalArgumentException if value is less than -2592000,
237     *                                  greater than 2592000, or is not a number.
238     */
239    public static double convertQuartSecToDecDegrees(int quartSec) {
240        if(Double.isNaN(quartSec) || quartSec < -2592000 || quartSec > 2592000){
241            // Invalid value
242            throw new IllegalArgumentException("Invalid coordiante value:" + quartSec);
243        }
244        return ((double)quartSec) / (3600 * 4);
245    }
246
247}
248
249
250