CellSignalStrengthGsm.java revision 82e6ee62b2be3c716c215e4ba72c8a12c58620fb
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.util.Log;
22
23/**
24 * LTE signal strength related information.
25 */
26public class CellSignalStrengthGsm extends CellSignalStrength implements Parcelable {
27
28    private static final String LOG_TAG = "CellSignalStrengthGsm";
29    private static final boolean DBG = false;
30
31    private static final int GSM_SIGNAL_STRENGTH_GREAT = 12;
32    private static final int GSM_SIGNAL_STRENGTH_GOOD = 8;
33    private static final int GSM_SIGNAL_STRENGTH_MODERATE = 8;
34
35    private int mSignalStrength; // Valid values are (0-31, 99) as defined in TS 27.007 8.5
36    private int mBitErrorRate;   // bit error rate (0-7, 99) as defined in TS 27.007 8.5
37
38    /**
39     * Empty constructor
40     *
41     * @hide
42     */
43    public CellSignalStrengthGsm() {
44        setDefaultValues();
45    }
46
47    /**
48     * Constructor
49     *
50     * @hide
51     */
52    public CellSignalStrengthGsm(int ss, int ber) {
53        initialize(ss, ber);
54    }
55
56    /**
57     * Copy constructors
58     *
59     * @param s Source SignalStrength
60     *
61     * @hide
62     */
63    public CellSignalStrengthGsm(CellSignalStrengthGsm s) {
64        copyFrom(s);
65    }
66
67    /**
68     * Initialize all the values
69     *
70     * @param SignalStrength
71     *
72     * @hide
73     */
74    public void initialize(int ss, int ber) {
75        mSignalStrength = ss;
76        mBitErrorRate = ber;
77    }
78
79    /**
80     * @hide
81     */
82    protected void copyFrom(CellSignalStrengthGsm s) {
83        mSignalStrength = s.mSignalStrength;
84        mBitErrorRate = s.mBitErrorRate;
85    }
86
87    /**
88     * @hide
89     */
90    @Override
91    public CellSignalStrengthGsm copy() {
92        return new CellSignalStrengthGsm(this);
93    }
94
95    /** @hide */
96    @Override
97    public void setDefaultValues() {
98        mSignalStrength = Integer.MAX_VALUE;
99        mBitErrorRate = Integer.MAX_VALUE;
100    }
101
102    /**
103     * Get signal level as an int from 0..4
104     */
105    @Override
106    public int getLevel() {
107        int level;
108
109        // ASU ranges from 0 to 31 - TS 27.007 Sec 8.5
110        // asu = 0 (-113dB or less) is very weak
111        // signal, its better to show 0 bars to the user in such cases.
112        // asu = 99 is a special case, where the signal strength is unknown.
113        int asu = mSignalStrength;
114        if (asu <= 2 || asu == 99) level = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
115        else if (asu >= GSM_SIGNAL_STRENGTH_GREAT) level = SIGNAL_STRENGTH_GREAT;
116        else if (asu >= GSM_SIGNAL_STRENGTH_GOOD)  level = SIGNAL_STRENGTH_GOOD;
117        else if (asu >= GSM_SIGNAL_STRENGTH_MODERATE)  level = SIGNAL_STRENGTH_MODERATE;
118        else level = SIGNAL_STRENGTH_POOR;
119        if (DBG) log("getLevel=" + level);
120        return level;
121    }
122
123    /**
124     * Get the signal strength as dBm
125     */
126    @Override
127    public int getDbm() {
128        int dBm;
129
130        int level = mSignalStrength;
131        int asu = (level == 99 ? Integer.MAX_VALUE : level);
132        if (asu != Integer.MAX_VALUE) {
133            dBm = -113 + (2 * asu);
134        } else {
135            dBm = Integer.MAX_VALUE;
136        }
137        if (DBG) log("getDbm=" + dBm);
138        return dBm;
139    }
140
141    /**
142     * Get the LTE signal level as an asu value between 0..97, 99 is unknown
143     * Asu is calculated based on 3GPP RSRP. Refer to 3GPP 27.007 (Ver 10.3.0) Sec 8.69
144     */
145    @Override
146    public int getAsuLevel() {
147        // ASU ranges from 0 to 31 - TS 27.007 Sec 8.5
148        // asu = 0 (-113dB or less) is very weak
149        // signal, its better to show 0 bars to the user in such cases.
150        // asu = 99 is a special case, where the signal strength is unknown.
151        int level = mSignalStrength;
152        if (DBG) log("getAsuLevel=" + level);
153        return level;
154    }
155
156    @Override
157    public int hashCode() {
158        int primeNum = 31;
159        return (mSignalStrength * primeNum) + (mBitErrorRate * primeNum);
160    }
161
162    @Override
163    public boolean equals (Object o) {
164        CellSignalStrengthGsm s;
165
166        try {
167            s = (CellSignalStrengthGsm) o;
168        } catch (ClassCastException ex) {
169            return false;
170        }
171
172        if (o == null) {
173            return false;
174        }
175
176        return mSignalStrength == s.mSignalStrength && mBitErrorRate == s.mBitErrorRate;
177    }
178
179    /**
180     * @return string representation.
181     */
182    @Override
183    public String toString() {
184        return "CellSignalStrengthGsm:"
185                + " ss=" + mSignalStrength
186                + " ber=" + mBitErrorRate;
187    }
188
189    /** Implement the Parcelable interface */
190    @Override
191    public void writeToParcel(Parcel dest, int flags) {
192        if (DBG) log("writeToParcel(Parcel, int): " + toString());
193        dest.writeInt(CellSignalStrength.TYPE_GSM);
194        dest.writeInt(mSignalStrength);
195        dest.writeInt(mBitErrorRate);
196    }
197
198    /**
199     * Construct a SignalStrength object from the given parcel
200     * where the token is already been processed.
201     */
202    private CellSignalStrengthGsm(Parcel in) {
203        mSignalStrength = in.readInt();
204        mBitErrorRate = in.readInt();
205        if (DBG) log("CellSignalStrengthGsm(Parcel): " + toString());
206    }
207
208    /** Implement the Parcelable interface */
209    @Override
210    public int describeContents() {
211        return 0;
212    }
213
214    /** Implement the Parcelable interface */
215    @SuppressWarnings("hiding")
216    public static final Parcelable.Creator<CellSignalStrengthGsm> CREATOR =
217            new Parcelable.Creator<CellSignalStrengthGsm>() {
218        @Override
219        public CellSignalStrengthGsm createFromParcel(Parcel in) {
220            if (in.readInt() != CellSignalStrength.TYPE_GSM) {
221                throw new RuntimeException("Expecting TYPE_GSM");
222            }
223            return createFromParcelBody(in);
224        }
225
226        @Override
227        public CellSignalStrengthGsm[] newArray(int size) {
228            return new CellSignalStrengthGsm[size];
229        }
230    };
231
232    /** @hide */
233    public static CellSignalStrengthGsm createFromParcelBody(Parcel in) {
234        return new CellSignalStrengthGsm(in);
235    }
236
237    /**
238     * log
239     */
240    private static void log(String s) {
241        Log.w(LOG_TAG, s);
242    }
243}
244