Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed boot receiver. Added reboot handling by boot id. #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
Expand Down
8 changes: 0 additions & 8 deletions library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<!--Detect boot to invalidate TrueTime-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application>
<!-- Start the Service if applicable on boot -->
<receiver android:name=".BootCompletedBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>

</manifest>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
53 changes: 53 additions & 0 deletions library/src/main/java/com/instacart/library/truetime/TrueTime.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
}
}
}
}
}

}