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