1/*
2 * Copyright (C) 2012 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;
18
19import android.os.Bundle;
20import android.os.Parcel;
21import android.os.Parcelable;
22import android.telephony.gsm.GsmCellLocation;
23
24/**
25 * Represents the location and geographical scope of a cell broadcast message.
26 * For GSM/UMTS, the Location Area and Cell ID are set when the broadcast
27 * geographical scope is cell wide or Location Area wide. For CDMA, the
28 * broadcast geographical scope is always PLMN wide.
29 *
30 * @hide
31 */
32public class SmsCbLocation implements Parcelable {
33
34    /** The PLMN. Note that this field may be an empty string, but isn't allowed to be null. */
35    private final String mPlmn;
36
37    private final int mLac;
38    private final int mCid;
39
40    /**
41     * Construct an empty location object. This is used for some test cases, and for
42     * cell broadcasts saved in older versions of the database without location info.
43     */
44    public SmsCbLocation() {
45        mPlmn = "";
46        mLac = -1;
47        mCid = -1;
48    }
49
50    /**
51     * Construct a location object for the PLMN. This class is immutable, so
52     * the same object can be reused for multiple broadcasts.
53     */
54    public SmsCbLocation(String plmn) {
55        mPlmn = plmn;
56        mLac = -1;
57        mCid = -1;
58    }
59
60    /**
61     * Construct a location object for the PLMN, LAC, and Cell ID. This class is immutable, so
62     * the same object can be reused for multiple broadcasts.
63     */
64    public SmsCbLocation(String plmn, int lac, int cid) {
65        mPlmn = plmn;
66        mLac = lac;
67        mCid = cid;
68    }
69
70    /**
71     * Initialize the object from a Parcel.
72     */
73    public SmsCbLocation(Parcel in) {
74        mPlmn = in.readString();
75        mLac = in.readInt();
76        mCid = in.readInt();
77    }
78
79    /**
80     * Returns the MCC/MNC of the network as a String.
81     * @return the PLMN identifier (MCC+MNC) as a String
82     */
83    public String getPlmn() {
84        return mPlmn;
85    }
86
87    /**
88     * Returns the GSM location area code, or UMTS service area code.
89     * @return location area code, -1 if unknown, 0xffff max legal value
90     */
91    public int getLac() {
92        return mLac;
93    }
94
95    /**
96     * Returns the GSM or UMTS cell ID.
97     * @return gsm cell id, -1 if unknown, 0xffff max legal value
98     */
99    public int getCid() {
100        return mCid;
101    }
102
103    @Override
104    public int hashCode() {
105        int hash = mPlmn.hashCode();
106        hash = hash * 31 + mLac;
107        hash = hash * 31 + mCid;
108        return hash;
109    }
110
111    @Override
112    public boolean equals(Object o) {
113        if (o == this) {
114            return true;
115        }
116        if (o == null || !(o instanceof SmsCbLocation)) {
117            return false;
118        }
119        SmsCbLocation other = (SmsCbLocation) o;
120        return mPlmn.equals(other.mPlmn) && mLac == other.mLac && mCid == other.mCid;
121    }
122
123    @Override
124    public String toString() {
125        return '[' + mPlmn + ',' + mLac + ',' + mCid + ']';
126    }
127
128    /**
129     * Test whether this location is within the location area of the specified object.
130     *
131     * @param area the location area to compare with this location
132     * @return true if this location is contained within the specified location area
133     */
134    public boolean isInLocationArea(SmsCbLocation area) {
135        if (mCid != -1 && mCid != area.mCid) {
136            return false;
137        }
138        if (mLac != -1 && mLac != area.mLac) {
139            return false;
140        }
141        return mPlmn.equals(area.mPlmn);
142    }
143
144    /**
145     * Test whether this location is within the location area of the CellLocation.
146     *
147     * @param plmn the PLMN to use for comparison
148     * @param lac the Location Area (GSM) or Service Area (UMTS) to compare with
149     * @param cid the Cell ID to compare with
150     * @return true if this location is contained within the specified PLMN, LAC, and Cell ID
151     */
152    public boolean isInLocationArea(String plmn, int lac, int cid) {
153        if (!mPlmn.equals(plmn)) {
154            return false;
155        }
156
157        if (mLac != -1 && mLac != lac) {
158            return false;
159        }
160
161        if (mCid != -1 && mCid != cid) {
162            return false;
163        }
164
165        return true;
166    }
167
168    /**
169     * Flatten this object into a Parcel.
170     *
171     * @param dest  The Parcel in which the object should be written.
172     * @param flags Additional flags about how the object should be written (ignored).
173     */
174    @Override
175    public void writeToParcel(Parcel dest, int flags) {
176        dest.writeString(mPlmn);
177        dest.writeInt(mLac);
178        dest.writeInt(mCid);
179    }
180
181    public static final Parcelable.Creator<SmsCbLocation> CREATOR
182            = new Parcelable.Creator<SmsCbLocation>() {
183        @Override
184        public SmsCbLocation createFromParcel(Parcel in) {
185            return new SmsCbLocation(in);
186        }
187
188        @Override
189        public SmsCbLocation[] newArray(int size) {
190            return new SmsCbLocation[size];
191        }
192    };
193
194    /**
195     * Describe the kinds of special objects contained in the marshalled representation.
196     * @return a bitmask indicating this Parcelable contains no special objects
197     */
198    @Override
199    public int describeContents() {
200        return 0;
201    }
202}
203