NeighboringCellInfo.java revision a09b4d2a611a7606e8fc8c73a24bd941b6fc173f
1/*
2 * Copyright (C) 2006 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 static android.telephony.TelephonyManager.NETWORK_TYPE_UNKNOWN;
22import static android.telephony.TelephonyManager.NETWORK_TYPE_EDGE;
23import static android.telephony.TelephonyManager.NETWORK_TYPE_GPRS;
24import static android.telephony.TelephonyManager.NETWORK_TYPE_UMTS;
25import static android.telephony.TelephonyManager.NETWORK_TYPE_HSDPA;
26import static android.telephony.TelephonyManager.NETWORK_TYPE_HSUPA;
27import static android.telephony.TelephonyManager.NETWORK_TYPE_HSPA;
28
29
30
31/**
32 * Represents the neighboring cell information, including
33 * Received Signal Strength and Cell ID location.
34 */
35public class NeighboringCellInfo implements Parcelable
36{
37    /**
38     * Signal strength is not available
39     */
40    static final public int UNKNOWN_RSSI = 99;
41    /**
42     * Cell location is not available
43     */
44    static final public int UNKNOWN_CID = -1;
45
46    /**
47     * In GSM, mRssi is the Received RSSI;
48     * In UMTS, mRssi is the Level index of CPICH Received Signal Code Power
49     */
50    private int mRssi;
51    /**
52     * CID in 16 bits format in GSM. Return UNKNOWN_CID in UMTS and CMDA.
53     */
54    private int mCid;
55    /**
56     * LAC in 16 bits format in GSM. Return UNKNOWN_CID in UMTS and CMDA.
57     */
58    private int mLac;
59    /**
60     * Primary Scrambling Code in 9 bits format in UMTS
61     * Return UNKNOWN_CID in GSM and CMDA.
62     */
63    private int mPsc;
64    /**
65     * Radio network type, value is one of following
66     * TelephonyManager.NETWORK_TYPE_XXXXXX.
67     */
68    private int mNetworkType;
69
70    /**
71     * Empty constructor.  Initializes the RSSI and CID.
72     *
73     * NeighboringCellInfo is one time shot for the neighboring cells based on
74     * the radio network type at that moment. Its constructor needs radio network
75     * type.
76     *
77     * @deprecated by {@link #NeighboringCellInfo(int, String, int)}
78     */
79    @Deprecated
80    public NeighboringCellInfo() {
81        mRssi = UNKNOWN_RSSI;
82        mLac = UNKNOWN_CID;
83        mCid = UNKNOWN_CID;
84        mPsc = UNKNOWN_CID;
85        mNetworkType = NETWORK_TYPE_UNKNOWN;
86    }
87
88    /**
89     * Initialize the object from rssi and cid.
90     *
91     * NeighboringCellInfo is one time shot for the neighboring cells based on
92     * the radio network type at that moment. Its constructor needs radio network
93     * type.
94     *
95     * @deprecated by {@link #NeighboringCellInfo(int, String, int)}
96     */
97    @Deprecated
98    public NeighboringCellInfo(int rssi, int cid) {
99        mRssi = rssi;
100        mCid = cid;
101    }
102
103    /**
104     * Initialize the object from rssi, location string, and radioType
105     * radioType is one of following
106     * {@link TelephonyManager#NETWORK_TYPE_GPRS TelephonyManager.NETWORK_TYPE_GPRS},
107     * {@link TelephonyManager#NETWORK_TYPE_EDGE TelephonyManager.NETWORK_TYPE_EDGE},
108     * {@link TelephonyManager#NETWORK_TYPE_UMTS TelephonyManager.NETWORK_TYPE_UMTS},
109     * {@link TelephonyManager#NETWORK_TYPE_HSDPA TelephonyManager.NETWORK_TYPE_HSDPA},
110     * {@link TelephonyManager#NETWORK_TYPE_HSUPA TelephonyManager.NETWORK_TYPE_HSUPA},
111     * and {@link TelephonyManager#NETWORK_TYPE_HSPA TelephonyManager.NETWORK_TYPE_HSPA}.
112     */
113    public NeighboringCellInfo(int rssi, String location, int radioType) {
114        // set default value
115        mRssi = rssi;
116        mNetworkType = NETWORK_TYPE_UNKNOWN;
117        mPsc = UNKNOWN_CID;
118        mLac = UNKNOWN_CID;
119        mCid = UNKNOWN_CID;
120
121
122        // pad location string with leading "0"
123        int l = location.length();
124        if (l > 8) return;
125        if (l < 8) {
126            for (int i = 0; i < (8-l); i++) {
127                location = "0" + location;
128            }
129        }
130        // TODO - handle LTE and eHRPD (or find they can't be supported)
131        try {// set LAC/CID or PSC based on radioType
132            switch (radioType) {
133            case NETWORK_TYPE_GPRS:
134            case NETWORK_TYPE_EDGE:
135                mNetworkType = radioType;
136                // check if 0xFFFFFFFF for UNKNOWN_CID
137                if (!location.equalsIgnoreCase("FFFFFFFF")) {
138                    mCid = Integer.parseInt(location.substring(4), 16);
139                    mLac = Integer.parseInt(location.substring(0, 4), 16);
140                }
141                break;
142            case NETWORK_TYPE_UMTS:
143            case NETWORK_TYPE_HSDPA:
144            case NETWORK_TYPE_HSUPA:
145            case NETWORK_TYPE_HSPA:
146                mNetworkType = radioType;
147                mPsc = Integer.parseInt(location, 16);
148                break;
149            }
150        } catch (NumberFormatException e) {
151            // parsing location error
152            mPsc = UNKNOWN_CID;
153            mLac = UNKNOWN_CID;
154            mCid = UNKNOWN_CID;
155            mNetworkType = NETWORK_TYPE_UNKNOWN;
156        }
157    }
158
159    /**
160     * Initialize the object from a parcel.
161     */
162    public NeighboringCellInfo(Parcel in) {
163        mRssi = in.readInt();
164        mLac = in.readInt();
165        mCid = in.readInt();
166        mPsc = in.readInt();
167        mNetworkType = in.readInt();
168    }
169
170    /**
171     * @return received signal strength or UNKNOWN_RSSI if unknown
172     *
173     * For GSM, it is in "asu" ranging from 0 to 31 (dBm = -113 + 2*asu)
174     * 0 means "-113 dBm or less" and 31 means "-51 dBm or greater"
175     * For UMTS, it is the Level index of CPICH RSCP defined in TS 25.125
176     */
177    public int getRssi() {
178        return mRssi;
179    }
180
181    /**
182     * @return LAC in GSM, 0xffff max legal value
183     *  UNKNOWN_CID if in UMTS or CMDA or unknown
184     */
185    public int getLac() {
186        return mLac;
187    }
188
189    /**
190     * @return cell id in GSM, 0xffff max legal value
191     *  UNKNOWN_CID if in UMTS or CDMA or unknown
192     */
193    public int getCid() {
194        return mCid;
195    }
196
197    /**
198     * @return Primary Scrambling Code in 9 bits format in UMTS, 0x1ff max value
199     *  UNKNOWN_CID if in GSM or CMDA or unknown
200     */
201    public int getPsc() {
202        return mPsc;
203    }
204
205    /**
206     * @return Radio network type while neighboring cell location is stored.
207     *
208     * Return {@link TelephonyManager#NETWORK_TYPE_UNKNOWN TelephonyManager.NETWORK_TYPE_UNKNOWN}
209     * means that the location information is unavailable.
210     *
211     * Return {@link TelephonyManager#NETWORK_TYPE_GPRS TelephonyManager.NETWORK_TYPE_GPRS} or
212     * {@link TelephonyManager#NETWORK_TYPE_EDGE TelephonyManager.NETWORK_TYPE_EDGE}
213     * means that Neighboring Cell information is stored for GSM network, in
214     * which {@link NeighboringCellInfo#getLac NeighboringCellInfo.getLac} and
215     * {@link NeighboringCellInfo#getCid NeighboringCellInfo.getCid} should be
216     * called to access location.
217     *
218     * Return {@link TelephonyManager#NETWORK_TYPE_UMTS TelephonyManager.NETWORK_TYPE_UMTS},
219     * {@link TelephonyManager#NETWORK_TYPE_HSDPA TelephonyManager.NETWORK_TYPE_HSDPA},
220     * {@link TelephonyManager#NETWORK_TYPE_HSUPA TelephonyManager.NETWORK_TYPE_HSUPA},
221     * or {@link TelephonyManager#NETWORK_TYPE_HSPA TelephonyManager.NETWORK_TYPE_HSPA}
222     * means that Neighboring Cell information is stored for UMTS network, in
223     * which {@link NeighboringCellInfo#getPsc NeighboringCellInfo.getPsc}
224     * should be called to access location.
225     */
226    public int getNetworkType() {
227        return mNetworkType;
228    }
229    /**
230     * Set the cell id.
231     *
232     * NeighboringCellInfo is a one time shot for the neighboring cells based on
233     * the radio network type at that moment. It shouldn't be changed after
234     * creation.
235     *
236     * @deprecated cid value passed as in location parameter passed to constructor
237     *              {@link #NeighboringCellInfo(int, String, int)}
238     */
239    @Deprecated
240    public void setCid(int cid) {
241        mCid = cid;
242    }
243
244    /**
245     * Set the signal strength of the cell.
246     *
247     * NeighboringCellInfo is a one time shot for the neighboring cells based on
248     * the radio network type at that moment. It shouldn't be changed after
249     * creation.
250     *
251     * @deprecated initial rssi value passed as parameter to constructor
252     *              {@link #NeighboringCellInfo(int, String, int)}
253     */
254    @Deprecated
255    public void setRssi(int rssi) {
256        mRssi = rssi;
257    }
258
259    @Override
260    public String toString() {
261        StringBuilder sb = new StringBuilder();
262
263        sb.append("[");
264        if (mPsc != UNKNOWN_CID) {
265            sb.append(Integer.toHexString(mPsc))
266                    .append("@").append(((mRssi == UNKNOWN_RSSI)? "-" : mRssi));
267        } else if(mLac != UNKNOWN_CID && mCid != UNKNOWN_CID) {
268            sb.append(Integer.toHexString(mLac))
269                    .append(Integer.toHexString(mCid))
270                    .append("@").append(((mRssi == UNKNOWN_RSSI)? "-" : mRssi));
271        }
272        sb.append("]");
273
274        return sb.toString();
275    }
276
277    public int describeContents() {
278        return 0;
279    }
280
281    public void writeToParcel(Parcel dest, int flags) {
282        dest.writeInt(mRssi);
283        dest.writeInt(mLac);
284        dest.writeInt(mCid);
285        dest.writeInt(mPsc);
286        dest.writeInt(mNetworkType);
287    }
288
289    public static final Parcelable.Creator<NeighboringCellInfo> CREATOR
290    = new Parcelable.Creator<NeighboringCellInfo>() {
291        public NeighboringCellInfo createFromParcel(Parcel in) {
292            return new NeighboringCellInfo(in);
293        }
294
295        public NeighboringCellInfo[] newArray(int size) {
296            return new NeighboringCellInfo[size];
297        }
298    };
299}
300