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