Skip to content

Commit 8faee0b

Browse files
isUsingNetworkProvidedTime()
Useful in situations where you want to verify that the device has a correct time set, to avoid fraud, or if you want to prevent the user from messing with the time and abusing your "one-time" and "expiring" features.
1 parent 64555be commit 8faee0b

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

lib/utilcode/src/main/java/com/blankj/utilcode/util/TimeUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ protected Map<String, SimpleDateFormat> initialValue() {
3636
private static SimpleDateFormat getDefaultFormat() {
3737
return getSafeDateFormat("yyyy-MM-dd HH:mm:ss");
3838
}
39+
/**
40+
* Checks whether the device is using Network Provided Time or not.
41+
* Useful in situations where you want to verify that the device has a correct time set, to avoid fraud, or if you want to prevent the user from messing with the time and abusing your "one-time" and "expiring" features.
42+
* @return {@code true}: yes<br>{@code false}: no
43+
*/
44+
public static boolean isUsingNetworkProvidedTime() {
45+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
46+
return Settings.Global.getInt(Utils.getApp().getContentResolver(), Settings.Global.AUTO_TIME, 0) == 1;
47+
} else {
48+
return android.provider.Settings.System.getInt(Utils.getApp().getContentResolver(), android.provider.Settings.System.AUTO_TIME, 0) == 1;
49+
}
50+
}
51+
3952

4053
@SuppressLint("SimpleDateFormat")
4154
public static SimpleDateFormat getSafeDateFormat(String pattern) {

0 commit comments

Comments
 (0)