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.gsm;
18 
19 import android.os.Bundle;
20 import android.telephony.CellLocation;
21 
22 /**
23  * Represents the cell location on a GSM phone.
24  */
25 public class GsmCellLocation extends CellLocation {
26     private int mLac;
27     private int mCid;
28     private int mPsc;
29 
30     /**
31      * Empty constructor.  Initializes the LAC and CID to -1.
32      */
GsmCellLocation()33     public GsmCellLocation() {
34         mLac = -1;
35         mCid = -1;
36         mPsc = -1;
37     }
38 
39     /**
40      * Initialize the object from a bundle.
41      */
GsmCellLocation(Bundle bundle)42     public GsmCellLocation(Bundle bundle) {
43         mLac = bundle.getInt("lac", -1);
44         mCid = bundle.getInt("cid", -1);
45         mPsc = bundle.getInt("psc", -1);
46     }
47 
48     /**
49      * @return gsm location area code, -1 if unknown, 0xffff max legal value
50      */
getLac()51     public int getLac() {
52         return mLac;
53     }
54 
55     /**
56      * @return gsm cell id, -1 if unknown, 0xffff max legal value
57      */
getCid()58     public int getCid() {
59         return mCid;
60     }
61 
62     /**
63      * On a UMTS network, returns the primary scrambling code of the serving
64      * cell.
65      *
66      * @return primary scrambling code for UMTS, -1 if unknown or GSM
67      */
getPsc()68     public int getPsc() {
69         return mPsc;
70     }
71 
72     /**
73      * Invalidate this object.  The location area code and the cell id are set to -1.
74      */
75     @Override
setStateInvalid()76     public void setStateInvalid() {
77         mLac = -1;
78         mCid = -1;
79         mPsc = -1;
80     }
81 
82     /**
83      * Set the location area code and the cell id.
84      */
setLacAndCid(int lac, int cid)85     public void setLacAndCid(int lac, int cid) {
86         mLac = lac;
87         mCid = cid;
88     }
89 
90     /**
91      * Set the primary scrambling code.
92      * @hide
93      */
setPsc(int psc)94     public void setPsc(int psc) {
95         mPsc = psc;
96     }
97 
98     @Override
hashCode()99     public int hashCode() {
100         return mLac ^ mCid;
101     }
102 
103     @Override
equals(Object o)104     public boolean equals(Object o) {
105         GsmCellLocation s;
106 
107         try {
108             s = (GsmCellLocation)o;
109         } catch (ClassCastException ex) {
110             return false;
111         }
112 
113         if (o == null) {
114             return false;
115         }
116 
117         return equalsHandlesNulls(mLac, s.mLac) && equalsHandlesNulls(mCid, s.mCid)
118             && equalsHandlesNulls(mPsc, s.mPsc);
119     }
120 
121     @Override
toString()122     public String toString() {
123         return "["+ mLac + "," + mCid + "," + mPsc + "]";
124     }
125 
126     /**
127      * Test whether two objects hold the same data values or both are null
128      *
129      * @param a first obj
130      * @param b second obj
131      * @return true if two objects equal or both are null
132      */
equalsHandlesNulls(Object a, Object b)133     private static boolean equalsHandlesNulls(Object a, Object b) {
134         return (a == null) ? (b == null) : a.equals (b);
135     }
136 
137     /**
138      * Set intent notifier Bundle based on service state
139      *
140      * @param m intent notifier Bundle
141      */
fillInNotifierBundle(Bundle m)142     public void fillInNotifierBundle(Bundle m) {
143         m.putInt("lac", mLac);
144         m.putInt("cid", mCid);
145         m.putInt("psc", mPsc);
146     }
147 
148     /**
149      * @hide
150      */
isEmpty()151     public boolean isEmpty() {
152         return (mLac == -1 && mCid == -1 && mPsc == -1);
153     }
154 }
155