summaryrefslogtreecommitdiffstats
path: root/telephony/java/android/telephony/CellInfo.java
blob: 9bea30c2d70dc03eac993d1ed356fe656d412cb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.telephony;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Represent one snapshot observation of one cell info
 * which contains the time of observation.
 *
 * @hide Pending API review
 */
public final class CellInfo implements Parcelable {
    // Type to distinguish where time stamp gets recorded.
    public static final int CELL_INFO_TIMESTAMP_TYPE_UNKNOWN = 0;
    public static final int CELL_INFO_TIMESTAMP_TYPE_ANTENNA = 1;
    public static final int CELL_INFO_TIMESTAMP_TYPE_MODEM = 2;
    public static final int CELL_INFO_TIMESTAMP_TYPE_OEM_RIL = 3;
    public static final int CELL_INFO_TIMESTAMP_TYPE_JAVA_RIL = 4;

    // Observation time stamped as type in nanoseconds since boot
    private final long mTimeStamp;
    // Where time stamp gets recorded.
    // Value of CELL_INFO_TIMESTAMP_TYPE_XXXX
    private final int mTimeStampType;

    private final boolean mRegistered;

    private final SignalStrength mStrength;
    private final long mTimingAdvance;

    private final int mCellIdentityType;
    private final CellIdentity mCellIdentity;

    /**
     * Public constructor
     * @param timeStampType is one of CELL_INFO_TIMESTAMP_TYPE_XXXX
     * @param timeStamp is observation time in nanoseconds since boot
     * @param timingAdv is observed timing advance
     * @param registered is true when register to this cellIdentity
     * @param strength is observed signal strength
     * @param cellIdentity is observed mobile cell
     */
    public CellInfo(int timeStampType, long timeStamp, long timingAdv,
            boolean registered, SignalStrength strength,
            CellIdentity cellIdentity) {

        if (timeStampType < CELL_INFO_TIMESTAMP_TYPE_UNKNOWN ||
                timeStampType > CELL_INFO_TIMESTAMP_TYPE_JAVA_RIL) {
            mTimeStampType = CELL_INFO_TIMESTAMP_TYPE_UNKNOWN;
        } else {
            mTimeStampType = timeStampType;
        }

        mRegistered = registered;
        mTimeStamp = timeStamp;
        mTimingAdvance = timingAdv;
        mStrength = new SignalStrength(strength);

        mCellIdentityType = cellIdentity.getCellIdType();
        // TODO: make defense copy
        mCellIdentity = cellIdentity;
    }

    public CellInfo(CellInfo ci) {
        this.mTimeStampType = ci.mTimeStampType;
        this.mRegistered = ci.mRegistered;
        this.mTimeStamp = ci.mTimeStamp;
        this.mTimingAdvance = ci.mTimingAdvance;
        this.mCellIdentityType = ci.mCellIdentityType;
        this.mStrength = new SignalStrength(ci.mStrength);
        switch(mCellIdentityType) {
            case CellIdentity.CELLID_TYPE_GSM:
                mCellIdentity = new GsmCellIdentity((GsmCellIdentity)ci.mCellIdentity);
                break;
            default:
                mCellIdentity = null;
        }
    }

    private CellInfo(Parcel in) {
        mTimeStampType = in.readInt();
        mRegistered = (in.readInt() == 1) ? true : false;
        mTimeStamp = in.readLong();
        mTimingAdvance = in.readLong();
        mCellIdentityType = in.readInt();
        mStrength = SignalStrength.CREATOR.createFromParcel(in);
        switch(mCellIdentityType) {
            case CellIdentity.CELLID_TYPE_GSM:
                mCellIdentity = GsmCellIdentity.CREATOR.createFromParcel(in);
                break;
            default:
                mCellIdentity = null;
        }
    }

    /**
     * @return the observation time in nanoseconds since boot
     */
    public long getTimeStamp() {
        return mTimeStamp;
    }

    /**
     * @return Where time stamp gets recorded.
     * one of CELL_INFO_TIMESTAMP_TYPE_XXXX
     */
    public int getTimeStampType() {
        return mTimeStampType;
    }

    /**
     * @return true when register to this cellIdentity
     */
    public boolean isRegistered() {
        return mRegistered;
    }

    /**
     * @return observed timing advance
     */
    public long getTimingAdvance() {
        return mTimingAdvance;
    }

    /**
     * @return observed signal strength
     */
    public SignalStrength getSignalStrength() {
        // make a defense copy
        return new SignalStrength(mStrength);
    }

    /**
     * @return observed cell identity
     */
    public CellIdentity getCellIdentity() {
        // TODO: make a defense copy
        return mCellIdentity;
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();

        sb.append("TimeStampType: ");
        switch(mTimeStampType) {
            case 1:
                sb.append("antenna");
                break;
            case 2:
                sb.append("modem");
                break;
            case 3:
                sb.append("oem_ril");
                break;
            case 4:
                sb.append("java_ril");
                break;
            default:
                sb.append("unknown");
        }
        sb.append(", TimeStamp: ").append(mTimeStamp).append(" ns");
        sb.append(", Registered: ").append(mRegistered ? "YES" : "NO");
        sb.append(", TimingAdvance: ").append(mTimingAdvance);
        sb.append(", Strength : " + mStrength);
        sb.append(", Cell Iden: " + mCellIdentity);

        return sb.toString();
    }

    /** Implement the Parcelable interface {@hide} */
    @Override
    public int describeContents() {
        return 0;
    }

    /** Implement the Parcelable interface {@hide} */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mTimeStampType);
        dest.writeInt(mRegistered ? 1 : 0);
        dest.writeLong(mTimeStamp);
        dest.writeLong(mTimingAdvance);
        dest.writeInt(mCellIdentityType);
        mStrength.writeToParcel(dest, flags);
        mCellIdentity.writeToParcel(dest, flags);
    }

    /** Implement the Parcelable interface {@hide} */
    public static final Creator<CellInfo> CREATOR =
            new Creator<CellInfo>() {
        @Override
        public CellInfo createFromParcel(Parcel in) {
            return new CellInfo(in);
        }

        @Override
        public CellInfo[] newArray(int size) {
            return new CellInfo[size];
        }
    };
}