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.annotation.Nullable;
20import android.os.Parcel;
21import android.text.TextUtils;
22
23import java.util.Objects;
24
25/**
26 * CellIdentity is to represent a unique CDMA cell
27 */
28public final class CellIdentityCdma extends CellIdentity {
29    private static final String TAG = CellIdentityCdma.class.getSimpleName();
30    private static final boolean DBG = false;
31
32    // Network Id 0..65535
33    private final int mNetworkId;
34    // CDMA System Id 0..32767
35    private final int mSystemId;
36    // Base Station Id 0..65535
37    private final int mBasestationId;
38    /**
39     * Longitude is a decimal number as specified in 3GPP2 C.S0005-A v6.0.
40     * It is represented in units of 0.25 seconds and ranges from -2592000
41     * to 2592000, both values inclusive (corresponding to a range of -180
42     * to +180 degrees).
43     */
44    private final int mLongitude;
45    /**
46     * Latitude is a decimal number as specified in 3GPP2 C.S0005-A v6.0.
47     * It is represented in units of 0.25 seconds and ranges from -1296000
48     * to 1296000, both values inclusive (corresponding to a range of -90
49     * to +90 degrees).
50     */
51    private final int mLatitude;
52
53    /**
54     * @hide
55     */
56    public CellIdentityCdma() {
57        super(TAG, TYPE_CDMA, null, null, null, null);
58        mNetworkId = Integer.MAX_VALUE;
59        mSystemId = Integer.MAX_VALUE;
60        mBasestationId = Integer.MAX_VALUE;
61        mLongitude = Integer.MAX_VALUE;
62        mLatitude = Integer.MAX_VALUE;
63    }
64
65    /**
66     * public constructor
67     * @param nid Network Id 0..65535
68     * @param sid CDMA System Id 0..32767
69     * @param bid Base Station Id 0..65535
70     * @param lon Longitude is a decimal number ranges from -2592000
71     *        to 2592000
72     * @param lat Latitude is a decimal number ranges from -1296000
73     *        to 1296000
74     *
75     * @hide
76     */
77    public CellIdentityCdma(int nid, int sid, int bid, int lon, int lat) {
78        this(nid, sid, bid, lon, lat, null, null);
79    }
80
81    /**
82     * public constructor
83     * @param nid Network Id 0..65535
84     * @param sid CDMA System Id 0..32767
85     * @param bid Base Station Id 0..65535
86     * @param lon Longitude is a decimal number ranges from -2592000
87     *        to 2592000
88     * @param lat Latitude is a decimal number ranges from -1296000
89     *        to 1296000
90     * @param alphal long alpha Operator Name String or Enhanced Operator Name String
91     * @param alphas short alpha Operator Name String or Enhanced Operator Name String
92     *
93     * @hide
94     */
95    public CellIdentityCdma(int nid, int sid, int bid, int lon, int lat, String alphal,
96                             String alphas) {
97        super(TAG, TYPE_CDMA, null, null, alphal, alphas);
98        mNetworkId = nid;
99        mSystemId = sid;
100        mBasestationId = bid;
101        if (!isNullIsland(lat, lon)) {
102            mLongitude = lon;
103            mLatitude = lat;
104        } else {
105            mLongitude = mLatitude = Integer.MAX_VALUE;
106        }
107    }
108
109    private CellIdentityCdma(CellIdentityCdma cid) {
110        this(cid.mNetworkId, cid.mSystemId, cid.mBasestationId, cid.mLongitude, cid.mLatitude,
111                cid.mAlphaLong, cid.mAlphaShort);
112    }
113
114    CellIdentityCdma copy() {
115        return new CellIdentityCdma(this);
116    }
117
118    /**
119     * Take the latitude and longitude in 1/4 seconds and see if
120     * the reported location is on Null Island.
121     *
122     * @return whether the reported Lat/Long are for Null Island
123     *
124     * @hide
125     */
126    private boolean isNullIsland(int lat, int lon) {
127        return Math.abs(lat) <= 1 && Math.abs(lon) <= 1;
128    }
129
130    /**
131     * @return Network Id 0..65535, Integer.MAX_VALUE if unknown
132     */
133    public int getNetworkId() {
134        return mNetworkId;
135    }
136
137    /**
138     * @return System Id 0..32767, Integer.MAX_VALUE if unknown
139     */
140    public int getSystemId() {
141        return mSystemId;
142    }
143
144    /**
145     * @return Base Station Id 0..65535, Integer.MAX_VALUE if unknown
146     */
147    public int getBasestationId() {
148        return mBasestationId;
149    }
150
151    /**
152     * @return Base station longitude, which is a decimal number as
153     * specified in 3GPP2 C.S0005-A v6.0. It is represented in units
154     * of 0.25 seconds and ranges from -2592000 to 2592000, both
155     * values inclusive (corresponding to a range of -180
156     * to +180 degrees). Integer.MAX_VALUE if unknown.
157     */
158    public int getLongitude() {
159        return mLongitude;
160    }
161
162    /**
163     * @return Base station latitude, which is a decimal number as
164     * specified in 3GPP2 C.S0005-A v6.0. It is represented in units
165     * of 0.25 seconds and ranges from -1296000 to 1296000, both
166     * values inclusive (corresponding to a range of -90
167     * to +90 degrees). Integer.MAX_VALUE if unknown.
168     */
169    public int getLatitude() {
170        return mLatitude;
171    }
172
173    @Override
174    public int hashCode() {
175        return Objects.hash(mNetworkId, mSystemId, mBasestationId, mLatitude, mLongitude,
176                super.hashCode());
177    }
178
179    @Override
180    public boolean equals(Object other) {
181        if (this == other) {
182            return true;
183        }
184
185        if (!(other instanceof CellIdentityCdma)) {
186            return false;
187        }
188
189        CellIdentityCdma o = (CellIdentityCdma) other;
190
191        return mNetworkId == o.mNetworkId
192                && mSystemId == o.mSystemId
193                && mBasestationId == o.mBasestationId
194                && mLatitude == o.mLatitude
195                && mLongitude == o.mLongitude
196                && super.equals(other);
197    }
198
199    @Override
200    public String toString() {
201        return new StringBuilder(TAG)
202        .append(":{ mNetworkId=").append(mNetworkId)
203        .append(" mSystemId=").append(mSystemId)
204        .append(" mBasestationId=").append(mBasestationId)
205        .append(" mLongitude=").append(mLongitude)
206        .append(" mLatitude=").append(mLatitude)
207        .append(" mAlphaLong=").append(mAlphaLong)
208        .append(" mAlphaShort=").append(mAlphaShort)
209        .append("}").toString();
210    }
211
212    /** Implement the Parcelable interface */
213    @Override
214    public void writeToParcel(Parcel dest, int flags) {
215        if (DBG) log("writeToParcel(Parcel, int): " + toString());
216        super.writeToParcel(dest, TYPE_CDMA);
217        dest.writeInt(mNetworkId);
218        dest.writeInt(mSystemId);
219        dest.writeInt(mBasestationId);
220        dest.writeInt(mLongitude);
221        dest.writeInt(mLatitude);
222    }
223
224    /** Construct from Parcel, type has already been processed */
225    private CellIdentityCdma(Parcel in) {
226        super(TAG, TYPE_CDMA, in);
227        mNetworkId = in.readInt();
228        mSystemId = in.readInt();
229        mBasestationId = in.readInt();
230        mLongitude = in.readInt();
231        mLatitude = in.readInt();
232
233        if (DBG) log(toString());
234    }
235
236    /** Implement the Parcelable interface */
237    @SuppressWarnings("hiding")
238    public static final Creator<CellIdentityCdma> CREATOR =
239            new Creator<CellIdentityCdma>() {
240        @Override
241        public CellIdentityCdma createFromParcel(Parcel in) {
242            in.readInt();   // skip
243            return createFromParcelBody(in);
244        }
245
246        @Override
247        public CellIdentityCdma[] newArray(int size) {
248            return new CellIdentityCdma[size];
249        }
250    };
251
252    /** @hide */
253    protected static CellIdentityCdma createFromParcelBody(Parcel in) {
254        return new CellIdentityCdma(in);
255    }
256}
257