CellIdentityGsm.java revision e3a9cbc052db7f00a96cc7b9580097d67538f360
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.Parcel;
20import android.os.Parcelable;
21import android.telephony.Rlog;
22
23/**
24 * CellIdentity to represent a unique GSM cell
25 */
26public final class CellIdentityGsm implements Parcelable {
27
28    private static final String LOG_TAG = "CellIdentityGsm";
29    private static final boolean DBG = false;
30
31    // 3-digit Mobile Country Code, 0..999
32    private final int mMcc;
33    // 2 or 3-digit Mobile Network Code, 0..999
34    private final int mMnc;
35    // 16-bit Location Area Code, 0..65535
36    private final int mLac;
37    // 16-bit GSM Cell Identity described in TS 27.007, 0..65535
38    private final int mCid;
39
40    /**
41     * @hide
42     */
43    public CellIdentityGsm() {
44        mMcc = Integer.MAX_VALUE;
45        mMnc = Integer.MAX_VALUE;
46        mLac = Integer.MAX_VALUE;
47        mCid = Integer.MAX_VALUE;
48    }
49    /**
50     * public constructor
51     * @param mcc 3-digit Mobile Country Code, 0..999
52     * @param mnc 2 or 3-digit Mobile Network Code, 0..999
53     * @param lac 16-bit Location Area Code, 0..65535
54     * @param cid 16-bit GSM Cell Identity or 28-bit UMTS Cell Identity
55     *
56     * @hide
57     */
58    public CellIdentityGsm (int mcc, int mnc, int lac, int cid) {
59        mMcc = mcc;
60        mMnc = mnc;
61        mLac = lac;
62        mCid = cid;
63    }
64
65    private CellIdentityGsm(CellIdentityGsm cid) {
66        mMcc = cid.mMcc;
67        mMnc = cid.mMnc;
68        mLac = cid.mLac;
69        mCid = cid.mCid;
70    }
71
72    CellIdentityGsm copy() {
73       return new CellIdentityGsm(this);
74    }
75
76    /**
77     * @return 3-digit Mobile Country Code, 0..999, Integer.MAX_VALUE if unknown
78     */
79    public int getMcc() {
80        return mMcc;
81    }
82
83    /**
84     * @return 2 or 3-digit Mobile Network Code, 0..999, Integer.MAX_VALUE if unknown
85     */
86    public int getMnc() {
87        return mMnc;
88    }
89
90    /**
91     * @return 16-bit Location Area Code, 0..65535, Integer.MAX_VALUE if unknown
92     */
93    public int getLac() {
94        return mLac;
95    }
96
97    /**
98     * @return CID
99     * Either 16-bit GSM Cell Identity described
100     * in TS 27.007, 0..65535, Integer.MAX_VALUE if unknown
101     */
102    public int getCid() {
103        return mCid;
104    }
105
106    /**
107     * @return Integer.MAX_VALUE, undefined for GSM
108     */
109    @Deprecated
110    public int getPsc() {
111        return Integer.MAX_VALUE;
112    }
113
114    @Override
115    public int hashCode() {
116        int primeNum = 31;
117        return (mMcc * primeNum) + (mMnc * primeNum) + (mLac * primeNum) + (mCid * primeNum);
118    }
119
120    @Override
121    public boolean equals(Object other) {
122        if (super.equals(other)) {
123            try {
124                CellIdentityGsm o = (CellIdentityGsm)other;
125                return mMcc == o.mMcc &&
126                        mMnc == o.mMnc &&
127                        mLac == o.mLac &&
128                        mCid == o.mCid;
129            } catch (ClassCastException e) {
130                return false;
131            }
132        } else {
133            return false;
134        }
135    }
136
137    @Override
138    public String toString() {
139        StringBuilder sb = new StringBuilder("CellIdentityGsm:{");
140        sb.append(" mMcc=").append(mMcc);
141        sb.append(" mMnc=").append(mMnc);
142        sb.append(" mLac=").append(mLac);
143        sb.append(" mCid=").append(mCid);
144        sb.append("}");
145
146        return sb.toString();
147    }
148
149    /** Implement the Parcelable interface */
150    @Override
151    public int describeContents() {
152        return 0;
153    }
154
155    /** Implement the Parcelable interface */
156    @Override
157    public void writeToParcel(Parcel dest, int flags) {
158        if (DBG) log("writeToParcel(Parcel, int): " + toString());
159        dest.writeInt(mMcc);
160        dest.writeInt(mMnc);
161        dest.writeInt(mLac);
162        dest.writeInt(mCid);
163    }
164
165    /** Construct from Parcel, type has already been processed */
166    private CellIdentityGsm(Parcel in) {
167        mMcc = in.readInt();
168        mMnc = in.readInt();
169        mLac = in.readInt();
170        mCid = in.readInt();
171        if (DBG) log("CellIdentityGsm(Parcel): " + toString());
172    }
173
174    /** Implement the Parcelable interface */
175    @SuppressWarnings("hiding")
176    public static final Creator<CellIdentityGsm> CREATOR =
177            new Creator<CellIdentityGsm>() {
178        @Override
179        public CellIdentityGsm createFromParcel(Parcel in) {
180            return new CellIdentityGsm(in);
181        }
182
183        @Override
184        public CellIdentityGsm[] newArray(int size) {
185            return new CellIdentityGsm[size];
186        }
187    };
188
189    /**
190     * log
191     */
192    private static void log(String s) {
193        Rlog.w(LOG_TAG, s);
194    }
195}
196