1/*
2 * Copyright (C) 2013 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.annotation.Nullable;
20import android.os.Parcel;
21import android.text.TextUtils;
22
23import java.util.Objects;
24
25/**
26 * CellIdentity to represent a unique UMTS cell
27 */
28public final class CellIdentityWcdma extends CellIdentity {
29    private static final String TAG = CellIdentityWcdma.class.getSimpleName();
30    private static final boolean DBG = false;
31
32    // 16-bit Location Area Code, 0..65535
33    private final int mLac;
34    // 28-bit UMTS Cell Identity described in TS 25.331, 0..268435455
35    private final int mCid;
36    // 9-bit UMTS Primary Scrambling Code described in TS 25.331, 0..511
37    private final int mPsc;
38    // 16-bit UMTS Absolute RF Channel Number
39    private final int mUarfcn;
40
41    /**
42     * @hide
43     */
44    public CellIdentityWcdma() {
45        super(TAG, TYPE_TDSCDMA, null, null, null, null);
46        mLac = Integer.MAX_VALUE;
47        mCid = Integer.MAX_VALUE;
48        mPsc = Integer.MAX_VALUE;
49        mUarfcn = Integer.MAX_VALUE;
50    }
51    /**
52     * public constructor
53     * @param mcc 3-digit Mobile Country Code, 0..999
54     * @param mnc 2 or 3-digit Mobile Network Code, 0..999
55     * @param lac 16-bit Location Area Code, 0..65535
56     * @param cid 28-bit UMTS Cell Identity
57     * @param psc 9-bit UMTS Primary Scrambling Code
58     *
59     * @hide
60     */
61    public CellIdentityWcdma (int mcc, int mnc, int lac, int cid, int psc) {
62        this(lac, cid, psc, Integer.MAX_VALUE, String.valueOf(mcc), String.valueOf(mnc),
63                null, null);
64    }
65
66    /**
67     * public constructor
68     * @param mcc 3-digit Mobile Country Code, 0..999
69     * @param mnc 2 or 3-digit Mobile Network Code, 0..999
70     * @param lac 16-bit Location Area Code, 0..65535
71     * @param cid 28-bit UMTS Cell Identity
72     * @param psc 9-bit UMTS Primary Scrambling Code
73     * @param uarfcn 16-bit UMTS Absolute RF Channel Number
74     *
75     * @hide
76     */
77    public CellIdentityWcdma (int mcc, int mnc, int lac, int cid, int psc, int uarfcn) {
78        this(lac, cid, psc, uarfcn, String.valueOf(mcc), String.valueOf(mnc), null, null);
79    }
80
81    /**
82     * public constructor
83     * @param lac 16-bit Location Area Code, 0..65535
84     * @param cid 28-bit UMTS Cell Identity
85     * @param psc 9-bit UMTS Primary Scrambling Code
86     * @param uarfcn 16-bit UMTS Absolute RF Channel Number
87     * @param mccStr 3-digit Mobile Country Code in string format
88     * @param mncStr 2 or 3-digit Mobile Network Code in string format
89     * @param alphal long alpha Operator Name String or Enhanced Operator Name String
90     * @param alphas short alpha Operator Name String or Enhanced Operator Name String
91     *
92     * @hide
93     */
94    public CellIdentityWcdma (int lac, int cid, int psc, int uarfcn,
95                              String mccStr, String mncStr, String alphal, String alphas) {
96        super(TAG, TYPE_WCDMA, mccStr, mncStr, alphal, alphas);
97        mLac = lac;
98        mCid = cid;
99        mPsc = psc;
100        mUarfcn = uarfcn;
101    }
102
103    private CellIdentityWcdma(CellIdentityWcdma cid) {
104        this(cid.mLac, cid.mCid, cid.mPsc, cid.mUarfcn, cid.mMccStr,
105                cid.mMncStr, cid.mAlphaLong, cid.mAlphaShort);
106    }
107
108    CellIdentityWcdma copy() {
109        return new CellIdentityWcdma(this);
110    }
111
112    /**
113     * @return 3-digit Mobile Country Code, 0..999, Integer.MAX_VALUE if unknown
114     * @deprecated Use {@link #getMccString} instead.
115     */
116    @Deprecated
117    public int getMcc() {
118        return (mMccStr != null) ? Integer.valueOf(mMccStr) : Integer.MAX_VALUE;
119    }
120
121    /**
122     * @return 2 or 3-digit Mobile Network Code, 0..999, Integer.MAX_VALUE if unknown
123     * @deprecated Use {@link #getMncString} instead.
124     */
125    @Deprecated
126    public int getMnc() {
127        return (mMncStr != null) ? Integer.valueOf(mMncStr) : Integer.MAX_VALUE;
128    }
129
130    /**
131     * @return 16-bit Location Area Code, 0..65535, Integer.MAX_VALUE if unknown
132     */
133    public int getLac() {
134        return mLac;
135    }
136
137    /**
138     * @return CID
139     * 28-bit UMTS Cell Identity described in TS 25.331, 0..268435455, Integer.MAX_VALUE if unknown
140     */
141    public int getCid() {
142        return mCid;
143    }
144
145    /**
146     * @return 9-bit UMTS Primary Scrambling Code described in TS 25.331, 0..511, Integer.MAX_VALUE
147     * if unknown
148     */
149    public int getPsc() {
150        return mPsc;
151    }
152
153    /**
154     * @return Mobile Country Code in string version, null if unknown
155     */
156    public String getMccString() {
157        return mMccStr;
158    }
159
160    /**
161     * @return Mobile Network Code in string version, null if unknown
162     */
163    public String getMncString() {
164        return mMncStr;
165    }
166
167    /**
168     * @return a 5 or 6 character string (MCC+MNC), null if any field is unknown
169     */
170    public String getMobileNetworkOperator() {
171        return (mMccStr == null || mMncStr == null) ? null : mMccStr + mMncStr;
172    }
173
174    @Override
175    public int hashCode() {
176        return Objects.hash(mLac, mCid, mPsc, super.hashCode());
177    }
178
179    /**
180     * @return 16-bit UMTS Absolute RF Channel Number, Integer.MAX_VALUE if unknown
181     */
182    public int getUarfcn() {
183        return mUarfcn;
184    }
185
186    /** @hide */
187    @Override
188    public int getChannelNumber() {
189        return mUarfcn;
190    }
191
192    @Override
193    public boolean equals(Object other) {
194        if (this == other) {
195            return true;
196        }
197
198        if (!(other instanceof CellIdentityWcdma)) {
199            return false;
200        }
201
202        CellIdentityWcdma o = (CellIdentityWcdma) other;
203        return mLac == o.mLac
204                && mCid == o.mCid
205                && mPsc == o.mPsc
206                && mUarfcn == o.mUarfcn
207                && TextUtils.equals(mMccStr, o.mMccStr)
208                && TextUtils.equals(mMncStr, o.mMncStr)
209                && super.equals(other);
210    }
211
212    @Override
213    public String toString() {
214        return new StringBuilder(TAG)
215        .append(":{ mLac=").append(mLac)
216        .append(" mCid=").append(mCid)
217        .append(" mPsc=").append(mPsc)
218        .append(" mUarfcn=").append(mUarfcn)
219        .append(" mMcc=").append(mMccStr)
220        .append(" mMnc=").append(mMncStr)
221        .append(" mAlphaLong=").append(mAlphaLong)
222        .append(" mAlphaShort=").append(mAlphaShort)
223        .append("}").toString();
224    }
225
226    /** Implement the Parcelable interface */
227    @Override
228    public void writeToParcel(Parcel dest, int flags) {
229        if (DBG) log("writeToParcel(Parcel, int): " + toString());
230        super.writeToParcel(dest, TYPE_WCDMA);
231        dest.writeInt(mLac);
232        dest.writeInt(mCid);
233        dest.writeInt(mPsc);
234        dest.writeInt(mUarfcn);
235    }
236
237    /** Construct from Parcel, type has already been processed */
238    private CellIdentityWcdma(Parcel in) {
239        super(TAG, TYPE_WCDMA, in);
240        mLac = in.readInt();
241        mCid = in.readInt();
242        mPsc = in.readInt();
243        mUarfcn = in.readInt();
244        if (DBG) log(toString());
245    }
246
247    /** Implement the Parcelable interface */
248    @SuppressWarnings("hiding")
249    public static final Creator<CellIdentityWcdma> CREATOR =
250            new Creator<CellIdentityWcdma>() {
251                @Override
252                public CellIdentityWcdma createFromParcel(Parcel in) {
253                    in.readInt();   // skip
254                    return createFromParcelBody(in);
255                }
256
257                @Override
258                public CellIdentityWcdma[] newArray(int size) {
259                    return new CellIdentityWcdma[size];
260                }
261            };
262
263    /** @hide */
264    protected static CellIdentityWcdma createFromParcelBody(Parcel in) {
265        return new CellIdentityWcdma(in);
266    }
267}