GpsClock.java revision ea8a8a6076f04360de2d25b3e5853cde8026cd5f
1/*
2 * Copyright (C) 2014 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.location;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22/**
23 * A class containing a GPS clock timestamp.
24 * It represents a measurement of the GPS receiver's clock.
25 *
26 * @hide
27 */
28public class GpsClock implements Parcelable {
29    // mandatory parameters
30    private long mTimeInNs;
31
32    // optional parameters
33    private boolean mHasLeapSecond;
34    private short mLeapSecond;
35    private boolean mHasTimeUncertaintyInNs;
36    private double mTimeUncertaintyInNs;
37    private boolean mHasBiasInNs;
38    private double mBiasInNs;
39    private boolean mHasBiasUncertaintyInNs;
40    private double mBiasUncertaintyInNs;
41    private boolean mHasDriftInNsPerSec;
42    private double mDriftInNsPerSec;
43    private boolean mHasDriftUncertaintyInNsPerSec;
44    private double mDriftUncertaintyInNsPerSec;
45
46    GpsClock() {
47        reset();
48    }
49
50    /**
51     * Sets all contents to the values stored in the provided object.
52     */
53    public void set(GpsClock clock) {
54        mTimeInNs = clock.mTimeInNs;
55
56        mHasLeapSecond = clock.mHasLeapSecond;
57        mLeapSecond = clock.mLeapSecond;
58        mHasTimeUncertaintyInNs = clock.mHasTimeUncertaintyInNs;
59        mTimeUncertaintyInNs = clock.mTimeUncertaintyInNs;
60        mHasBiasInNs = clock.mHasBiasInNs;
61        mBiasInNs = clock.mBiasInNs;
62        mHasBiasUncertaintyInNs = clock.mHasBiasUncertaintyInNs;
63        mBiasUncertaintyInNs = clock.mBiasUncertaintyInNs;
64        mHasDriftInNsPerSec = clock.mHasDriftInNsPerSec;
65        mDriftInNsPerSec = clock.mDriftInNsPerSec;
66        mHasDriftUncertaintyInNsPerSec = clock.mHasDriftUncertaintyInNsPerSec;
67        mDriftUncertaintyInNsPerSec = clock.mDriftUncertaintyInNsPerSec;
68    }
69
70    /**
71     * Resets all the contents to its original state.
72     */
73    public void reset() {
74        mTimeInNs = Long.MIN_VALUE;
75
76        resetLeapSecond();
77        resetTimeUncertaintyInNs();
78        resetBiasInNs();
79        resetBiasUncertaintyInNs();
80        resetDriftInNsPerSec();
81        resetDriftUncertaintyInNsPerSec();
82    }
83
84    /**
85     * Returns true if {@link #getLeapSecond()} is available, false otherwise.
86     */
87    public boolean hasLeapSecond() {
88        return mHasLeapSecond;
89    }
90
91    /**
92     * Gets the leap second associated with the clock's time.
93     *
94     * The value is only available if {@link #hasLeapSecond()} is true.
95     */
96    public short getLeapSecond() {
97        return mLeapSecond;
98    }
99
100    /**
101     * Sets the leap second associated with the clock's time.
102     */
103    public void setLeapSecond(short leapSecond) {
104        mHasLeapSecond = true;
105        mLeapSecond = leapSecond;
106    }
107
108    /**
109     * Resets the leap second associated with the clock's time.
110     */
111    public void resetLeapSecond() {
112        mHasLeapSecond = false;
113        mLeapSecond = Short.MIN_VALUE;
114    }
115
116    /**
117     * Gets the GPS clock Time in nanoseconds; it represents the uncorrected receiver's GPS time
118     * since 0000Z, January 6, 1980; this is, including {@link #getBiasInNs()}.
119     * The reported time includes {@link #getTimeUncertaintyInNs()}.
120     */
121    public long getTimeInNs() {
122        return mTimeInNs;
123    }
124
125    /**
126     * Sets the GPS clock Time in nanoseconds.
127     */
128    public void setTimeInNs(long timeInNs) {
129        mTimeInNs = timeInNs;
130    }
131
132    /**
133     * Returns true if {@link #getTimeUncertaintyInNs()} is available, false otherwise.
134     */
135    public boolean hasTimeUncertaintyInNs() {
136        return mHasTimeUncertaintyInNs;
137    }
138
139    /**
140     * Gets the clock's time Uncertainty (1-Sigma) in nanoseconds.
141     *
142     * The value is only available if {@link #hasTimeUncertaintyInNs()} is true.
143     */
144    public double getTimeUncertaintyInNs() {
145        return mTimeUncertaintyInNs;
146    }
147
148    /**
149     * Sets the clock's Time Uncertainty (1-Sigma) in nanoseconds.
150     */
151    public void setTimeUncertaintyInNs(double timeUncertaintyInNs) {
152        mHasTimeUncertaintyInNs = true;
153        mTimeUncertaintyInNs = timeUncertaintyInNs;
154    }
155
156    /**
157     * Resets the clock's Time Uncertainty (1-Sigma) in nanoseconds.
158     */
159    public void resetTimeUncertaintyInNs() {
160        mHasTimeUncertaintyInNs = false;
161        mTimeUncertaintyInNs = Double.NaN;
162    }
163
164    /**
165     * Returns true if {@link #getBiasInNs()} is available, false otherwise.
166     */
167    public boolean hasBiasInNs() {
168        return mHasBiasInNs;
169    }
170
171    /**
172     * Gets the clock's Bias in nanoseconds.
173     * The sign of the value (if available), is defined by the following equation:
174     *      true time = time - bias.
175     * The reported bias includes {@link #getBiasUncertaintyInNs()}.
176     *
177     * The value is only available if {@link #hasBiasInNs()} is true.
178     */
179    public Double getBiasInNs() {
180        return mBiasInNs;
181    }
182
183    /**
184     * Sets the clock's Bias in nanoseconds.
185     */
186    public void setBiasInNs(double biasInNs) {
187        mHasBiasInNs = true;
188        mBiasInNs = biasInNs;
189    }
190
191    /**
192     * Resets the clock's Bias in nanoseconds.
193     */
194    public void resetBiasInNs() {
195        mHasBiasInNs = false;
196        mBiasInNs = Double.NaN;
197    }
198
199    /**
200     * Returns true if {@link #getBiasUncertaintyInNs()} is available, false otherwise.
201     */
202    public boolean hasBiasUncertaintyInNs() {
203        return mHasBiasUncertaintyInNs;
204    }
205
206    /**
207     * Gets the clock's Bias Uncertainty (1-Sigma) in nanoseconds.
208     *
209     * The value is only available if {@link #hasBiasUncertaintyInNs()} is true.
210     */
211    public double getBiasUncertaintyInNs() {
212        return mBiasUncertaintyInNs;
213    }
214
215    /**
216     * Sets the clock's Bias Uncertainty (1-Sigma) in nanoseconds.
217     */
218    public void setBiasUncertaintyInNs(double biasUncertaintyInNs) {
219        mHasBiasUncertaintyInNs = true;
220        mBiasUncertaintyInNs = biasUncertaintyInNs;
221    }
222
223    /**
224     * Resets the clock's Bias Uncertainty (1-Sigma) in nanoseconds.
225     */
226    public void resetBiasUncertaintyInNs() {
227        mHasBiasUncertaintyInNs = false;
228        mBiasUncertaintyInNs = Double.NaN;
229    }
230
231    /**
232     * Returns true if {@link #getDriftInNsPerSec()} is available, false otherwise.
233     */
234    public boolean hasDriftInNsPerSec() {
235        return mHasDriftInNsPerSec;
236    }
237
238    /**
239     * Gets the clock's Drift in nanoseconds per second.
240     * A positive value indicates that the frequency is higher than the nominal frequency.
241     * The reported drift includes {@link #getDriftUncertaintyInNsPerSec()}.
242     *
243     * The value is only available if {@link #hasDriftInNsPerSec()} is true.
244     */
245    public double getDriftInNsPerSec() {
246        return mDriftInNsPerSec;
247    }
248
249    /**
250     * Sets the clock's Drift in nanoseconds per second.
251     */
252    public void setDriftInNsPerSec(double driftInNsPerSec) {
253        mHasDriftInNsPerSec = true;
254        mDriftInNsPerSec = driftInNsPerSec;
255    }
256
257    /**
258     * Resets the clock's Drift in nanoseconds per second.
259     */
260    public void resetDriftInNsPerSec() {
261        mHasDriftInNsPerSec = false;
262        mDriftInNsPerSec = Double.NaN;
263    }
264
265    /**
266     * Returns true if {@link #getDriftUncertaintyInNsPerSec()} is available, false otherwise.
267     */
268    public boolean hasDriftUncertaintyInNsPerSec() {
269        return mHasDriftUncertaintyInNsPerSec;
270    }
271
272    /**
273     * Gets the clock's Drift Uncertainty (1-Sigma) in nanoseconds per second.
274     *
275     * The value is only available if {@link #hasDriftUncertaintyInNsPerSec()} is true.
276     */
277    public double getDriftUncertaintyInNsPerSec() {
278        return mDriftUncertaintyInNsPerSec;
279    }
280
281    /**
282     * Sets the clock's Drift Uncertainty (1-Sigma) in nanoseconds per second.
283     */
284    public void setDriftUncertaintyInNsPerSec(double driftUncertaintyInNsPerSec) {
285        mHasDriftUncertaintyInNsPerSec = true;
286        mDriftUncertaintyInNsPerSec = driftUncertaintyInNsPerSec;
287    }
288
289    /**
290     * Resets the clock's Drift Uncertainty (1-Sigma) in nanoseconds per second.
291     */
292    public void resetDriftUncertaintyInNsPerSec() {
293        mHasDriftUncertaintyInNsPerSec = false;
294        mDriftUncertaintyInNsPerSec = Double.NaN;
295    }
296
297    public static final Creator<GpsClock> CREATOR = new Creator<GpsClock>() {
298        @Override
299        public GpsClock createFromParcel(Parcel parcel) {
300            GpsClock gpsClock = new GpsClock();
301            gpsClock.mTimeInNs = parcel.readLong();
302
303            gpsClock.mHasLeapSecond = parcel.readInt() != 0;
304            gpsClock.mLeapSecond = (short) parcel.readInt();
305            gpsClock.mHasTimeUncertaintyInNs = parcel.readInt() != 0;
306            gpsClock.mTimeUncertaintyInNs = parcel.readDouble();
307            gpsClock.mHasBiasInNs = parcel.readInt() != 0;
308            gpsClock.mBiasInNs = parcel.readDouble();
309            gpsClock.mHasBiasUncertaintyInNs = parcel.readInt() != 0;
310            gpsClock.mBiasUncertaintyInNs = parcel.readDouble();
311            gpsClock.mHasDriftInNsPerSec = parcel.readInt() != 0;
312            gpsClock.mDriftInNsPerSec = parcel.readDouble();
313            gpsClock.mHasDriftUncertaintyInNsPerSec = parcel.readInt() != 0;
314            gpsClock.mDriftUncertaintyInNsPerSec = parcel.readDouble();
315
316            return gpsClock;
317        }
318
319        @Override
320        public GpsClock[] newArray(int size) {
321            return new GpsClock[size];
322        }
323    };
324
325    public void writeToParcel(Parcel parcel, int flags) {
326        parcel.writeLong(mTimeInNs);
327
328        parcel.writeInt(mHasLeapSecond ? 1 : 0);
329        parcel.writeInt(mLeapSecond);
330        parcel.writeInt(mHasTimeUncertaintyInNs ? 1 : 0);
331        parcel.writeDouble(mTimeUncertaintyInNs);
332        parcel.writeInt(mHasBiasInNs ? 1 : 0);
333        parcel.writeDouble(mBiasInNs);
334        parcel.writeInt(mHasBiasUncertaintyInNs ? 1 : 0);
335        parcel.writeDouble(mBiasUncertaintyInNs);
336        parcel.writeInt(mHasDriftInNsPerSec ? 1 : 0);
337        parcel.writeDouble(mDriftInNsPerSec);
338        parcel.writeInt(mHasDriftUncertaintyInNsPerSec ? 1 : 0);
339        parcel.writeDouble(mDriftUncertaintyInNsPerSec);
340    }
341
342    @Override
343    public int describeContents() {
344        return 0;
345    }
346
347    @Override
348    public String toString() {
349        final String format = "   %-15s = %-25s   %-26s = %s\n";
350        StringBuilder builder = new StringBuilder("GpsClock:\n");
351
352        builder.append(String.format(
353                format,
354                "LeapSecond",
355                mHasLeapSecond ? mLeapSecond : null,
356                "",
357                ""));
358
359        builder.append(String.format(
360                format,
361                "TimeInNs",
362                mTimeInNs,
363                "TimeUncertaintyInNs",
364                mHasTimeUncertaintyInNs ? mTimeUncertaintyInNs : null));
365
366        builder.append(String.format(
367                format,
368                "BiasInNs",
369                mHasBiasInNs ? mBiasInNs : null,
370                "BiasUncertaintyInNs",
371                mHasBiasUncertaintyInNs ? mBiasUncertaintyInNs : null));
372
373        builder.append(String.format(
374                format,
375                "DriftInNsPerSec",
376                mHasDriftInNsPerSec ? mDriftInNsPerSec : null,
377                "DriftUncertaintyInNsPerSec",
378                mHasDriftUncertaintyInNsPerSec ? mDriftUncertaintyInNsPerSec : null));
379
380        return builder.toString();
381    }
382}
383