ServiceState.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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.Bundle;
20import android.os.Parcel;
21import android.os.Parcelable;
22import com.android.internal.telephony.Phone;
23
24/**
25 * Contains phone state and service related information.
26 *
27 * The following phone information is included in returned ServiceState:
28 *
29 * <ul>
30 *   <li>Service state: IN_SERVICE, OUT_OF_SERVICE, EMERGENCY_ONLY, POWER_OFF
31 *   <li>Roaming indicator
32 *   <li>Operator name, short name and numeric id
33 *   <li>Network selection mode
34 * </ul>
35 */
36public class ServiceState implements Parcelable {
37
38    /**
39     * Normal operation condition, the phone is registered
40     * with an operator either in home network or in roaming.
41     */
42    public static final int STATE_IN_SERVICE = 0;
43
44    /**
45     * Phone is not registered with any operator, the phone
46     * can be currently searching a new operator to register to, or not
47     * searching to registration at all, or registration is denied, or radio
48     * signal is not available.
49     */
50    public static final int STATE_OUT_OF_SERVICE = 1;
51
52    /**
53     * The phone is registered and locked.  Only emergency numbers are allowed. {@more}
54     */
55    public static final int STATE_EMERGENCY_ONLY = 2;
56
57    /**
58     * Radio of telephony is explictly powered off.
59     */
60    public static final int STATE_POWER_OFF = 3;
61
62    private int mState = STATE_OUT_OF_SERVICE;
63    private boolean mRoaming;
64    private String mOperatorAlphaLong;
65    private String mOperatorAlphaShort;
66    private String mOperatorNumeric;
67    private boolean mIsManualNetworkSelection;
68
69    /**
70     * Create a new ServiceState from a intent notifier Bundle
71     *
72     * This method is used by PhoneStateIntentReceiver and maybe by
73     * external applications.
74     *
75     * @param m Bundle from intent notifier
76     * @return newly created ServiceState
77     * @hide
78     */
79    public static ServiceState newFromBundle(Bundle m) {
80        ServiceState ret;
81        ret = new ServiceState();
82        ret.setFromNotifierBundle(m);
83        return ret;
84    }
85
86    /**
87     * Empty constructor
88     */
89    public ServiceState() {
90    }
91
92    /**
93     * Copy constructors
94     *
95     * @param s Source service state
96     */
97    public ServiceState(ServiceState s) {
98        copyFrom(s);
99    }
100
101    protected void copyFrom(ServiceState s) {
102        mState = s.mState;
103        mRoaming = s.mRoaming;
104        mOperatorAlphaLong = s.mOperatorAlphaLong;
105        mOperatorAlphaShort = s.mOperatorAlphaShort;
106        mOperatorNumeric = s.mOperatorNumeric;
107        mIsManualNetworkSelection = s.mIsManualNetworkSelection;
108    }
109
110    /**
111     * Construct a ServiceState object from the given parcel.
112     */
113    public ServiceState(Parcel in) {
114        mState = in.readInt();
115        mRoaming = in.readInt() != 0;
116        mOperatorAlphaLong = in.readString();
117        mOperatorAlphaShort = in.readString();
118        mOperatorNumeric = in.readString();
119        mIsManualNetworkSelection = in.readInt() != 0;
120    }
121
122    public void writeToParcel(Parcel out, int flags) {
123        out.writeInt(mState);
124        out.writeInt(mRoaming ? 1 : 0);
125        out.writeString(mOperatorAlphaLong);
126        out.writeString(mOperatorAlphaShort);
127        out.writeString(mOperatorNumeric);
128        out.writeInt(mIsManualNetworkSelection ? 1 : 0);
129    }
130
131    public int describeContents() {
132        return 0;
133    }
134
135    public static final Parcelable.Creator<ServiceState> CREATOR = new Parcelable.Creator() {
136        public ServiceState createFromParcel(Parcel in) {
137            return new ServiceState(in);
138        }
139
140        public ServiceState[] newArray(int size) {
141            return new ServiceState[size];
142        }
143    };
144
145    /**
146     * Get current servcie state of phone
147     *
148     * @see #STATE_IN_SERVICE
149     * @see #STATE_OUT_OF_SERVICE
150     * @see #STATE_EMERGENCY_ONLY
151     * @see #STATE_POWER_OFF
152     */
153    public int getState() {
154        return mState;
155    }
156
157    /**
158     * Get current roaming indicator of phone
159     * (note: not just decoding from TS 27.007 7.2)
160     *
161     * @return true if TS 27.007 7.2 roaming is true
162     *              and ONS is different from SPN
163     *
164     */
165    public boolean getRoaming() {
166        return mRoaming;
167    }
168
169    /**
170     * Get current registered operator name in long alphanumeric format
171     *
172     * In GSM/UMTS, long format can be upto 16 characters long
173     *
174     * @return long name of operator, null if unregistered or unknown
175     */
176    public String getOperatorAlphaLong() {
177        return mOperatorAlphaLong;
178    }
179
180    /**
181     * Get current registered operator name in short lphanumeric format
182     *
183     * In GSM/UMST, short format can be upto 8 characters long
184     *
185     * @return short name of operator, null if unregistered or unknown
186     */
187    public String getOperatorAlphaShort() {
188        return mOperatorAlphaShort;
189    }
190
191    /**
192     * Get current registered operator numeric id
193     *
194     * In GSM/UMTS, numeric format is 3 digit country code plus 2 or 3 digit
195     * network code
196     *
197     * The country code can be decoded using MccTable.countryCodeForMcc()
198     *
199     * @return numeric format of operator, null if unregistered or unknown
200     */
201    public String getOperatorNumeric() {
202        return mOperatorNumeric;
203    }
204
205    /**
206     * Get current network selection mode
207     *
208     * @return true if manual mode, false if automatic mode
209     */
210    public boolean getIsManualSelection() {
211        return mIsManualNetworkSelection;
212    }
213
214    @Override
215    public int hashCode() {
216        return (mState * 0x1234)
217                + (mRoaming ? 1 : 0)
218                + (mIsManualNetworkSelection ? 1 : 0)
219                + ((null == mOperatorAlphaLong) ? 0 : mOperatorAlphaLong.hashCode())
220                + ((null == mOperatorAlphaShort) ? 0 : mOperatorAlphaShort.hashCode())
221                + ((null == mOperatorNumeric) ? 0 : mOperatorNumeric.hashCode());
222    }
223
224    @Override
225    public boolean equals (Object o) {
226        ServiceState s;
227
228        try {
229            s = (ServiceState) o;
230        } catch (ClassCastException ex) {
231            return false;
232        }
233
234        if (o == null) {
235            return false;
236        }
237
238        return mState == s.mState
239                && mRoaming == s.mRoaming
240                && mIsManualNetworkSelection == s.mIsManualNetworkSelection
241                && equalsHandlesNulls(mOperatorAlphaLong, s.mOperatorAlphaLong)
242                && equalsHandlesNulls(mOperatorAlphaShort, s.mOperatorAlphaShort)
243                && equalsHandlesNulls(mOperatorNumeric, s.mOperatorNumeric);
244    }
245
246    @Override
247    public String toString() {
248        return mState + " " + (mRoaming ? "roaming" : "home")
249                + " " + mOperatorAlphaLong
250                + " " + mOperatorAlphaShort
251                + " " + mOperatorNumeric
252                + " " + (mIsManualNetworkSelection ? "(manual)" : "");
253    }
254
255    public void setStateOutOfService() {
256        mState = STATE_OUT_OF_SERVICE;
257        mRoaming = false;
258        mOperatorAlphaLong = null;
259        mOperatorAlphaShort = null;
260        mOperatorNumeric = null;
261        mIsManualNetworkSelection = false;
262    }
263
264    public void setStateOff() {
265        mState = STATE_POWER_OFF;
266        mRoaming = false;
267        mOperatorAlphaLong = null;
268        mOperatorAlphaShort = null;
269        mOperatorNumeric = null;
270        mIsManualNetworkSelection = false;
271    }
272
273    public void setState(int state) {
274        mState = state;
275    }
276
277    public void setRoaming(boolean roaming) {
278        mRoaming = roaming;
279    }
280
281    public void setOperatorName(String longName, String shortName, String numeric) {
282        mOperatorAlphaLong = longName;
283        mOperatorAlphaShort = shortName;
284        mOperatorNumeric = numeric;
285    }
286
287    public void setIsManualSelection(boolean isManual) {
288        mIsManualNetworkSelection = isManual;
289    }
290
291    /**
292     * Test whether two objects hold the same data values or both are null
293     *
294     * @param a first obj
295     * @param b second obj
296     * @return true if two objects equal or both are null
297     */
298    private static boolean equalsHandlesNulls (Object a, Object b) {
299        return (a == null) ? (b == null) : a.equals (b);
300    }
301
302    /**
303     * Set ServiceState based on intent notifier map
304     *
305     * @param m intent notifier map
306     * @hide
307     */
308    private void setFromNotifierBundle(Bundle m) {
309        mState = m.getInt("state");
310        mRoaming = m.getBoolean("roaming");
311        mOperatorAlphaLong = m.getString("operator-alpha-long");
312        mOperatorAlphaShort = m.getString("operator-alpha-short");
313        mOperatorNumeric = m.getString("operator-numeric");
314        mIsManualNetworkSelection = m.getBoolean("manual");
315    }
316
317    /**
318     * Set intent notifier Bundle based on service state
319     *
320     * @param m intent notifier Bundle
321     * @hide
322     */
323    public void fillInNotifierBundle(Bundle m) {
324        m.putInt("state", mState);
325        m.putBoolean("roaming", Boolean.valueOf(mRoaming));
326        m.putString("operator-alpha-long", mOperatorAlphaLong);
327        m.putString("operator-alpha-short", mOperatorAlphaShort);
328        m.putString("operator-numeric", mOperatorNumeric);
329        m.putBoolean("manual", Boolean.valueOf(mIsManualNetworkSelection));
330    }
331}
332