CellInfo.java revision b208a24cf521401912cfce16fce550a995cf1250
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;
21
22/**
23 * Immutable cell information from a point in time.
24 *
25 * @hide
26 */
27public class CellInfo implements Parcelable {
28
29    // Type fields for parceling
30    protected static final int TYPE_GSM = 1;
31    protected static final int TYPE_CDMA = 2;
32    protected static final int TYPE_LTE = 3;
33
34    // Type to distinguish where time stamp gets recorded.
35
36    /** @hide */
37    public static final int TIMESTAMP_TYPE_UNKNOWN = 0;
38    /** @hide */
39    public static final int TIMESTAMP_TYPE_ANTENNA = 1;
40    /** @hide */
41    public static final int TIMESTAMP_TYPE_MODEM = 2;
42    /** @hide */
43    public static final int TIMESTAMP_TYPE_OEM_RIL = 3;
44    /** @hide */
45    public static final int TIMESTAMP_TYPE_JAVA_RIL = 4;
46
47    // True if device is mRegistered to the mobile network
48    private boolean mRegistered;
49
50    // Observation time stamped as type in nanoseconds since boot
51    private long mTimeStamp;
52
53    // Where time stamp gets recorded.
54    // Value of TIMESTAMP_TYPE_XXXX
55    private int mTimeStampType;
56
57    protected CellInfo() {
58        this.mRegistered = false;
59        this.mTimeStampType = TIMESTAMP_TYPE_UNKNOWN;
60        this.mTimeStamp = Long.MAX_VALUE;
61    }
62
63    protected CellInfo(CellInfo ci) {
64        this.mRegistered = ci.mRegistered;
65        this.mTimeStampType = ci.mTimeStampType;
66        this.mTimeStamp = ci.mTimeStamp;
67    }
68
69    /** True if this cell is registered to the mobile network */
70    public boolean isRegistered() {
71        return mRegistered;
72    }
73    /** @hide */
74    public void setRegisterd(boolean registered) {
75        mRegistered = registered;
76    }
77
78    /** Approximate time of this cell information in nanos since boot */
79    public long getTimeStamp() {
80        return mTimeStamp;
81    }
82    /** @hide */
83    public void setTimeStamp(long timeStamp) {
84        mTimeStamp = timeStamp;
85    }
86
87    /**
88     * Where time stamp gets recorded.
89     * @return one of TIMESTAMP_TYPE_XXXX
90     *
91     * @hide
92     */
93    public int getTimeStampType() {
94        return mTimeStampType;
95    }
96    /** @hide */
97    public void setTimeStampType(int timeStampType) {
98        if (timeStampType < TIMESTAMP_TYPE_UNKNOWN || timeStampType > TIMESTAMP_TYPE_JAVA_RIL) {
99            mTimeStampType = TIMESTAMP_TYPE_UNKNOWN;
100        } else {
101            mTimeStampType = timeStampType;
102        }
103    }
104
105    @Override
106    public int hashCode() {
107        int primeNum = 31;
108        return ((mRegistered ? 0 : 1) * primeNum) + ((int)(mTimeStamp / 1000) * primeNum)
109                + (mTimeStampType * primeNum);
110    }
111
112    @Override
113    public boolean equals(Object other) {
114        if (other == null) {
115            return false;
116        }
117        if (this == other) {
118            return true;
119        }
120        try {
121            CellInfo o = (CellInfo) other;
122            return mRegistered == o.mRegistered
123                    && mTimeStamp == o.mTimeStamp && mTimeStampType == o.mTimeStampType;
124        } catch (ClassCastException e) {
125            return false;
126        }
127    }
128
129    private static String timeStampTypeToString(int type) {
130        switch (type) {
131            case 1:
132                return "antenna";
133            case 2:
134                return "modem";
135            case 3:
136                return "oem_ril";
137            case 4:
138                return "java_ril";
139            default:
140                return "unknown";
141        }
142    }
143
144    @Override
145    public String toString() {
146        StringBuffer sb = new StringBuffer();
147        String timeStampType;
148
149        sb.append(" mRegistered=").append(mRegistered ? "YES" : "NO");
150        timeStampType = timeStampTypeToString(mTimeStampType);
151        sb.append(" mTimeStampType=").append(timeStampType);
152        sb.append(" mTimeStamp=").append(mTimeStamp).append("ns");
153
154        return sb.toString();
155    }
156
157    /** Implement the Parcelable interface */
158    @Override
159    public int describeContents() {
160        return 0;
161    }
162
163    /** Implement the Parcelable interface */
164    @Override
165    public void writeToParcel(Parcel dest, int flags) {
166        dest.writeInt(mRegistered ? 1 : 0);
167        dest.writeInt(mTimeStampType);
168        dest.writeLong(mTimeStamp);
169    }
170
171    protected CellInfo(Parcel in) {
172        mRegistered = (in.readInt() == 1) ? true : false;
173        mTimeStampType = in.readInt();
174        mTimeStamp = in.readLong();
175    }
176
177    /** Implement the Parcelable interface */
178    public static final Creator<CellInfo> CREATOR = new Creator<CellInfo>() {
179        @Override
180        public CellInfo createFromParcel(Parcel in) {
181                int type = in.readInt();
182                switch (type) {
183                    case TYPE_GSM: return CellInfoGsm.createFromParcelBody(in);
184                    case TYPE_CDMA: return CellInfoCdma.createFromParcelBody(in);
185                    case TYPE_LTE: return CellInfoLte.createFromParcelBody(in);
186                    default: throw new RuntimeException("Bad CellInfo Parcel");
187                }
188        }
189
190        @Override
191        public CellInfo[] newArray(int size) {
192            return new CellInfo[size];
193        }
194    };
195}
196