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