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