diff --git a/build.gradle b/build.gradle index df39367a..e63f11b4 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.3' + classpath 'com.android.tools.build:gradle:3.1.4' classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' } } diff --git a/library/src/main/AndroidManifest.xml b/library/src/main/AndroidManifest.xml index 6d5aa9b3..1e8dc664 100644 --- a/library/src/main/AndroidManifest.xml +++ b/library/src/main/AndroidManifest.xml @@ -7,16 +7,8 @@ - - - - - - - - diff --git a/library/src/main/java/com/instacart/library/truetime/BootCompletedBroadcastReceiver.java b/library/src/main/java/com/instacart/library/truetime/BootCompletedBroadcastReceiver.java deleted file mode 100644 index dd645473..00000000 --- a/library/src/main/java/com/instacart/library/truetime/BootCompletedBroadcastReceiver.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.instacart.library.truetime; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; - -public class BootCompletedBroadcastReceiver - extends BroadcastReceiver { - - private static final String TAG = BootCompletedBroadcastReceiver.class.getSimpleName(); - - @Override - public void onReceive(Context context, Intent intent) { - TrueLog.i(TAG, "---- clearing TrueTime disk cache as we've detected a boot"); - if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { - TrueTime.clearCachedInfo(); - } - } -} diff --git a/library/src/main/java/com/instacart/library/truetime/CacheInterface.java b/library/src/main/java/com/instacart/library/truetime/CacheInterface.java index 950a66f2..26a15937 100644 --- a/library/src/main/java/com/instacart/library/truetime/CacheInterface.java +++ b/library/src/main/java/com/instacart/library/truetime/CacheInterface.java @@ -5,11 +5,16 @@ public interface CacheInterface { String KEY_CACHED_BOOT_TIME = "com.instacart.library.truetime.cached_boot_time"; String KEY_CACHED_DEVICE_UPTIME = "com.instacart.library.truetime.cached_device_uptime"; String KEY_CACHED_SNTP_TIME = "com.instacart.library.truetime.cached_sntp_time"; + String KEY_CACHED_BOOT_ID = "com.instacart.library.truetime.cached_boot_id"; void put(String key, long value); + void put(String key, String value); + long get(String key, long defaultValue); + String get(String key, String defaultValue); + void clear(); diff --git a/library/src/main/java/com/instacart/library/truetime/DiskCacheClient.java b/library/src/main/java/com/instacart/library/truetime/DiskCacheClient.java index 000a4304..c0c106b7 100644 --- a/library/src/main/java/com/instacart/library/truetime/DiskCacheClient.java +++ b/library/src/main/java/com/instacart/library/truetime/DiskCacheClient.java @@ -2,6 +2,7 @@ import android.os.SystemClock; +import static com.instacart.library.truetime.CacheInterface.KEY_CACHED_BOOT_ID; import static com.instacart.library.truetime.CacheInterface.KEY_CACHED_BOOT_TIME; import static com.instacart.library.truetime.CacheInterface.KEY_CACHED_DEVICE_UPTIME; import static com.instacart.library.truetime.CacheInterface.KEY_CACHED_SNTP_TIME; @@ -87,6 +88,21 @@ long getCachedSntpTime() { return _cacheInterface.get(KEY_CACHED_SNTP_TIME, 0L); } + String getCachedBootId() { + if (cacheUnavailable()) { + return null; + } + + return _cacheInterface.get(KEY_CACHED_BOOT_ID, null); + } + + void cacheBootId(String bootId) { + if (cacheUnavailable()) + return; + + _cacheInterface.put(KEY_CACHED_BOOT_ID, bootId); + } + // ----------------------------------------------------------------------------------- private boolean cacheUnavailable() { diff --git a/library/src/main/java/com/instacart/library/truetime/SharedPreferenceCacheImpl.java b/library/src/main/java/com/instacart/library/truetime/SharedPreferenceCacheImpl.java index 9a102297..ad3e2f64 100644 --- a/library/src/main/java/com/instacart/library/truetime/SharedPreferenceCacheImpl.java +++ b/library/src/main/java/com/instacart/library/truetime/SharedPreferenceCacheImpl.java @@ -20,11 +20,21 @@ public void put(String key, long value) { _sharedPreferences.edit().putLong(key, value).apply(); } + @Override + public void put(String key, String value) { + _sharedPreferences.edit().putString(key, value).apply(); + } + @Override public long get(String key, long defaultValue) { return _sharedPreferences.getLong(key, defaultValue); } + @Override + public String get(String key, String defaultValue) { + return _sharedPreferences.getString(key, defaultValue); + } + @Override public void clear() { remove(CacheInterface.KEY_CACHED_BOOT_TIME); diff --git a/library/src/main/java/com/instacart/library/truetime/TrueTime.java b/library/src/main/java/com/instacart/library/truetime/TrueTime.java index 18934c50..1632b7f8 100644 --- a/library/src/main/java/com/instacart/library/truetime/TrueTime.java +++ b/library/src/main/java/com/instacart/library/truetime/TrueTime.java @@ -1,10 +1,16 @@ package com.instacart.library.truetime; import android.content.Context; +import android.os.Build; import android.os.SystemClock; +import android.provider.Settings; + +import java.io.BufferedReader; +import java.io.FileReader; import java.io.IOException; import java.util.Date; import java.util.Locale; +import java.util.logging.Logger; public class TrueTime { @@ -55,6 +61,7 @@ public void initialize() throws IOException { */ public synchronized TrueTime withSharedPreferencesCache(Context context) { DISK_CACHE_CLIENT.enableCacheInterface(new SharedPreferenceCacheImpl(context)); + checkBootId(context); return INSTANCE; } @@ -173,4 +180,50 @@ private static long _getCachedSntpTime() { return cachedSntpTime; } + public void checkBootId(Context context) { + String cachedBootId = DISK_CACHE_CLIENT.getCachedBootId(); + + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) { + try { + String bootId = String.valueOf(Settings.Global.getInt(context.getContentResolver(), + Settings.Global.BOOT_COUNT)); + + if (cachedBootId == null || !cachedBootId.equals(bootId)) { + DISK_CACHE_CLIENT.cacheBootId(bootId); + clearCachedInfo(); + } + } catch (Settings.SettingNotFoundException e) { + clearCachedInfo(); + } + } else { + BufferedReader bufferedReader = null; + + try { + bufferedReader = new BufferedReader( + new FileReader("proc/sys/kernel/random/boot_id")); + + StringBuilder bootId = new StringBuilder(); + String line; + + while ((line = bufferedReader.readLine()) != null) { + bootId.append(line); + } + + if (cachedBootId == null || !cachedBootId.equals(bootId.toString())) { + DISK_CACHE_CLIENT.cacheBootId(bootId.toString()); + clearCachedInfo(); + } + } catch (IOException e) { + clearCachedInfo(); + } finally { + if (bufferedReader != null) { + try { + bufferedReader.close(); + } catch (IOException e) { + } + } + } + } + } + }