1 /*
2  * Copyright (C) 2019 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 
17 package android.location;
18 
19 import android.annotation.NonNull;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import java.time.Duration;
24 import java.time.Instant;
25 
26 /**
27  * Data class for passing GNSS-derived time.
28  * @hide
29  */
30 public final class LocationTime implements Parcelable {
31 
32     private final long mUnixEpochTimeMillis;
33     private final long mElapsedRealtimeNanos;
34 
LocationTime(long unixEpochTimeMillis, long elapsedRealtimeNanos)35     public LocationTime(long unixEpochTimeMillis, long elapsedRealtimeNanos) {
36         mUnixEpochTimeMillis = unixEpochTimeMillis;
37         mElapsedRealtimeNanos = elapsedRealtimeNanos;
38     }
39 
40     /**
41      * The Unix epoch time in millis, according to the Gnss location provider.
42      */
getUnixEpochTimeMillis()43     public long getUnixEpochTimeMillis() {
44         return mUnixEpochTimeMillis;
45     }
46 
47     /**
48      * The elapsed nanos since boot when {@link #getUnixEpochTimeMillis} was the current time.
49      */
getElapsedRealtimeNanos()50     public long getElapsedRealtimeNanos() {
51         return mElapsedRealtimeNanos;
52     }
53 
54     @Override
writeToParcel(Parcel out, int flags)55     public void writeToParcel(Parcel out, int flags) {
56         out.writeLong(mUnixEpochTimeMillis);
57         out.writeLong(mElapsedRealtimeNanos);
58     }
59 
60     @Override
describeContents()61     public int describeContents() {
62         return 0;
63     }
64 
65     @Override
toString()66     public String toString() {
67         return "LocationTime{"
68                 + "mUnixEpochTimeMillis=" + Instant.ofEpochMilli(mUnixEpochTimeMillis)
69                 + "(" + mUnixEpochTimeMillis + ")"
70                 + ", mElapsedRealtimeNanos=" + Duration.ofNanos(mElapsedRealtimeNanos)
71                 + "(" + mElapsedRealtimeNanos + ")"
72                 + '}';
73     }
74 
75     public static final @NonNull Parcelable.Creator<LocationTime> CREATOR =
76             new Parcelable.Creator<>() {
77                 public LocationTime createFromParcel(Parcel in) {
78                     long time = in.readLong();
79                     long elapsedRealtimeNanos = in.readLong();
80                     return new LocationTime(time, elapsedRealtimeNanos);
81                 }
82 
83                 public LocationTime[] newArray(int size) {
84                     return new LocationTime[size];
85                 }
86             };
87 }
88