-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Current threshold is more than 5 days different than system clock
- Loading branch information
Showing
12 changed files
with
196 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
GPSTest/src/main/java/com/android/gpstest/util/DateTimeUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (C) 2020 Sean J. Barbeau | ||
* | ||
* 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 com.android.gpstest.util | ||
|
||
import android.os.Build | ||
import androidx.annotation.VisibleForTesting | ||
import java.time.Duration | ||
import java.time.Instant | ||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* Utilities for comparing two locations to measure error | ||
*/ | ||
class DateTimeUtils { | ||
|
||
companion object { | ||
val NUM_DAYS_TIME_VALID = 5 | ||
/** | ||
* Returns true if the provided UTC time of the fix, in milliseconds since January 1, 1970, | ||
* is valid, and false if it is not | ||
*/ | ||
fun isTimeValid(time: Long): Boolean { | ||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
// If the GPS time is less than five days different than system clock time, consider it valid | ||
Duration.between(Instant.ofEpochMilli(time), Instant.now()).toDays() < NUM_DAYS_TIME_VALID | ||
} else { | ||
isTimeValidLegacy(time) | ||
} | ||
} | ||
|
||
@VisibleForTesting | ||
internal fun isTimeValidLegacy(time: Long): Boolean { | ||
return TimeUnit.MILLISECONDS.toDays(Math.abs(System.currentTimeMillis() - time)) < NUM_DAYS_TIME_VALID | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<!-- Outside border --> | ||
<item android:id="@+id/error_background_border"> | ||
<shape> | ||
<corners android:radius="9dp" /> | ||
<padding | ||
android:top="3dp" | ||
android:left="3dp" | ||
android:bottom="3dp" | ||
android:right="3dp" /> | ||
<solid android:color="@color/red" /> | ||
</shape> | ||
</item> | ||
<!-- Inside fill --> | ||
<item android:id="@+id/error_background_fill"> | ||
<shape> | ||
<corners android:radius="6dp" /> | ||
<solid android:color="@color/red" /> | ||
</shape> | ||
</item> | ||
</layer-list> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | ||
style="?android:attr/textAppearance" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:paddingTop="14dp" | ||
android:paddingBottom="6dp" | ||
android:paddingLeft="24dp" | ||
android:paddingRight="10dp" | ||
android:textSize="16sp" | ||
android:autoLink="all" | ||
android:linksClickable="true"></TextView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
GPSTest/src/test/java/com/android/gpstest/util/DateTimeUtilsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (C) 2020 Sean J. Barbeau | ||
* | ||
* 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 com.android.gpstest.util | ||
|
||
import junit.framework.Assert.assertFalse | ||
import junit.framework.Assert.assertTrue | ||
import org.junit.Test | ||
import java.time.Instant | ||
import java.util.concurrent.TimeUnit | ||
|
||
class DateTimeUtilsTest { | ||
|
||
@Test | ||
fun isTimeValid() { | ||
// Valid times within 5 days of now | ||
assertTrue(DateTimeUtils.isTimeValid(Instant.now().toEpochMilli())) | ||
assertTrue(DateTimeUtils.isTimeValid(Instant.now().toEpochMilli() + TimeUnit.DAYS.toMillis(4))) | ||
assertTrue(DateTimeUtils.isTimeValid(Instant.now().toEpochMilli() - TimeUnit.DAYS.toMillis(4))) | ||
|
||
// Invalid times more than 5 days from now (past or future) | ||
assertFalse(DateTimeUtils.isTimeValid(Instant.now().toEpochMilli() + TimeUnit.DAYS.toMillis(6))) | ||
assertFalse(DateTimeUtils.isTimeValid(Instant.now().toEpochMilli() - TimeUnit.DAYS.toMillis(6))) | ||
} | ||
|
||
@Test | ||
fun isTimeValidLegacy() { | ||
// Valid times within 5 days of now | ||
assertTrue(DateTimeUtils.isTimeValidLegacy(Instant.now().toEpochMilli())) | ||
assertTrue(DateTimeUtils.isTimeValidLegacy(Instant.now().toEpochMilli() + TimeUnit.DAYS.toMillis(4))) | ||
assertTrue(DateTimeUtils.isTimeValidLegacy(Instant.now().toEpochMilli() - TimeUnit.DAYS.toMillis(4))) | ||
|
||
// Invalid times more than 5 days from now (past or future) | ||
assertFalse(DateTimeUtils.isTimeValidLegacy(Instant.now().toEpochMilli() + TimeUnit.DAYS.toMillis(6))) | ||
assertFalse(DateTimeUtils.isTimeValidLegacy(Instant.now().toEpochMilli() - TimeUnit.DAYS.toMillis(6))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
...java/com/android/gpstest/UIUtilsTest.java → ...com/android/gpstest/util/UIUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters