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     * 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     */
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    public void setStateInvalid() {
76        mLac = -1;
77        mCid = -1;
78        mPsc = -1;
79    }
80
81    /**
82     * Set the location area code and the cell id.
83     */
84    public void setLacAndCid(int lac, int cid) {
85        mLac = lac;
86        mCid = cid;
87    }
88
89    /**
90     * Set the primary scrambling code.
91     * @hide
92     */
93    public void setPsc(int psc) {
94        mPsc = psc;
95    }
96
97    @Override
98    public int hashCode() {
99        return mLac ^ mCid;
100    }
101
102    @Override
103    public boolean equals(Object o) {
104        GsmCellLocation s;
105
106        try {
107            s = (GsmCellLocation)o;
108        } catch (ClassCastException ex) {
109            return false;
110        }
111
112        if (o == null) {
113            return false;
114        }
115
116        return equalsHandlesNulls(mLac, s.mLac) && equalsHandlesNulls(mCid, s.mCid)
117            && equalsHandlesNulls(mPsc, s.mPsc);
118    }
119
120    @Override
121    public String toString() {
122        return "["+ mLac + "," + mCid + "," + mPsc + "]";
123    }
124
125    /**
126     * Test whether two objects hold the same data values or both are null
127     *
128     * @param a first obj
129     * @param b second obj
130     * @return true if two objects equal or both are null
131     */
132    private static boolean equalsHandlesNulls(Object a, Object b) {
133        return (a == null) ? (b == null) : a.equals (b);
134    }
135
136    /**
137     * Set intent notifier Bundle based on service state
138     *
139     * @param m intent notifier Bundle
140     */
141    public void fillInNotifierBundle(Bundle m) {
142        m.putInt("lac", mLac);
143        m.putInt("cid", mCid);
144        m.putInt("psc", mPsc);
145    }
146
147    /**
148     * @hide
149     */
150    public boolean isEmpty() {
151        return (mLac == -1 && mCid == -1 && mPsc == -1);
152    }
153}
154