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