Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Nov 28, 2023
2 parents 04e6984 + 3dcca01 commit a8b51a0
Show file tree
Hide file tree
Showing 13 changed files with 866 additions and 125 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ android {
exclude 'META-INF/ASL2.0'
exclude 'META-INF/androidx.interpolator_interpolator.version'
exclude 'META-INF/androidx.core_core.version'
pickFirst 'org/bouncycastle/x509/CertPathReviewerMessages.properties'
pickFirst 'org/bouncycastle/x509/CertPathReviewerMessages_de.properties'
}

compileOptions {
Expand Down Expand Up @@ -341,6 +343,7 @@ dependencies {
//implementation 'com.google.dagger:dagger-android-support:2.x' // if you use the support libraries
annotationProcessor 'com.google.dagger:dagger-compiler:2.25.4'
implementation 'net.danlew:android.joda:2.10.6.1'
implementation 'org.bouncycastle:bcpkix-jdk15to18:1.68'
testImplementation 'joda-time:joda-time:2.10.7'

testImplementation 'junit:junit:4.13.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public NavDrawerBuilder(final Context context) {
}
} else { //If there haven't been two initial calibrations
if (BgReading.isDataSuitableForDoubleCalibration() || Ob1G5CollectionService.isG5WantingInitialCalibration()) {
if (FirmwareCapability.isTransmitterRawIncapable(getTransmitterID()) && last_two_bgReadings.size() > 1) { //A Firefly G6 after third reading
if ((FirmwareCapability.isTransmitterRawIncapable(getTransmitterID()) && last_two_bgReadings.size() > 1) || FirmwareCapability.isDeviceG7(getTransmitterID()) ) { //A Firefly G6 after third reading or a G7
this.nav_drawer_options.add(context.getString(R.string.add_calibration));
this.nav_drawer_intents.add(new Intent(context, AddCalibration.class));
} else { //G5 or non-Firefly G6 or Firefly G6 in no-code mode, after warm-up before initial calibration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.eveningoutpost.dexdrip.utilitymodels.CollectionServiceStarter;
import com.eveningoutpost.dexdrip.utilitymodels.Inevitable;
import com.eveningoutpost.dexdrip.cgm.carelinkfollow.client.CareLinkClient;
import com.eveningoutpost.dexdrip.cgm.carelinkfollow.client.CountryUtils;
import com.eveningoutpost.dexdrip.cgm.carelinkfollow.message.RecentData;

import static com.eveningoutpost.dexdrip.models.JoH.emptyString;
Expand Down Expand Up @@ -66,7 +65,7 @@ public void doEverything(boolean refreshToken, boolean downloadData) {

private void downloadData() {
msg("Start download");
if (checkCredentials()) {
if (checkCredentials(true, true, true)) {
try {
if (getCareLinkClient() != null) {
extendWakeLock(30_000);
Expand All @@ -85,35 +84,37 @@ private void downloadData() {

private void refreshToken() {
msg("Start refreshing token");
if (checkCredentials()) {
if (checkCredentials(true, false, true)) {
try {
if (new CareLinkAuthenticator(CareLinkCredentialStore.getInstance().getCredential().country, CareLinkCredentialStore.getInstance()).refreshToken()) {
UserError.Log.d(TAG, "Login token renewed!");
UserError.Log.d(TAG, "Access token renewed!");
msg(null);
} else {
UserError.Log.e(TAG, "Error renewing login token!");
msg("Login refresh failed! Will try again!");
UserError.Log.e(TAG, "Error renewing access token!");
msg("Access refresh failed! Will try again!");
}
} catch (Exception e) {
UserError.Log.e(TAG, "Error renewing login token: " + e.getMessage());
msg("Login refresh failed! Will try again!");
UserError.Log.e(TAG, "Error renewing access token: " + e.getMessage());
msg("Access refresh failed! Will try again!");
}
}
}

private boolean checkCredentials() {
private boolean checkCredentials(boolean checkAuthenticated, boolean checkAccessExpired, boolean checkRefreshExpired) {
// Not authenticated
if (CareLinkCredentialStore.getInstance().getAuthStatus() != CareLinkCredentialStore.AUTHENTICATED) {
if (checkAuthenticated && CareLinkCredentialStore.getInstance().getAuthStatus() != CareLinkCredentialStore.AUTHENTICATED) {
msg("Not logged in! Please log in!");
return false;
// Token expired
} else if (CareLinkCredentialStore.getInstance().getExpiresIn() <= 0) {
msg("Login refresh expired! Please log in!");
}
if (checkAccessExpired && CareLinkCredentialStore.getInstance().getAccessExpiresIn() <= 0) {
msg("Access expired!");
return false;
}
if (checkRefreshExpired && CareLinkCredentialStore.getInstance().getRefreshExpiresIn() <= 0) {
msg("Login expired! Please log in!");
return false;
// Credentials are all ok!
} else {
return true;
}
return true;
}

private void msg(final String msg) {
Expand Down Expand Up @@ -214,4 +215,4 @@ protected static synchronized void releaseWakeLock() {
JoH.releaseWakeLock(wl);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,20 @@ static void scheduleWakeUp() {
final BgReading lastBg = BgReading.lastNoSenssor();
final long last = lastBg != null ? lastBg.timestamp : 0;

final long nextTokenRefresh = anticipateNextTokenRefresh(JoH.tsl(), CareLinkCredentialStore.getInstance().getExpiresOn(), getRenewBeforeMillis(), getRenewIntervalMillis());
final long nextTokenRefresh = anticipateNextTokenRefresh(JoH.tsl(), CareLinkCredentialStore.getInstance().getAccessExpiresOn(), getRenewBeforeMillis(), getRenewIntervalMillis());
final long nextDataPoll = anticipateNextDataPoll(JoH.tsl(), last, SAMPLE_PERIOD, getGraceMillis(), getMissedIntervalMillis());

// Token needs to refreshed sooner
if(nextTokenRefresh <= nextDataPoll){
if (nextTokenRefresh <= nextDataPoll) {
next = nextTokenRefresh;
scheduleReason = " as login expires: ";
scheduleReason = " as access expires: ";
// Data is required sooner
} else {
next = nextDataPoll;
scheduleReason = " as last BG timestamp: ";
}

if(JoH.msTill(next) < (RATE_LIMIT_SECONDS * Constants.SECOND_IN_MS))
if (JoH.msTill(next) < (RATE_LIMIT_SECONDS * Constants.SECOND_IN_MS))
next = JoH.tsl() + (RATE_LIMIT_SECONDS * Constants.SECOND_IN_MS);

wakeup_time = next;
Expand All @@ -149,7 +149,7 @@ static void scheduleWakeUp() {
JoH.wakeUpIntent(xdrip.getAppContext(), JoH.msTill(next), WakeLockTrampoline.getPendingIntent(CareLinkFollowService.class, Constants.CARELINK_SERVICE_FAILOVER_ID));
}

private static long anticipateNextTokenRefresh(long now, final long expiry, final long before, final long interval){
private static long anticipateNextTokenRefresh(long now, final long expiry, final long before, final long interval) {

long next;

Expand Down Expand Up @@ -188,7 +188,7 @@ public static long anticipateNextDataPoll(long now, final long last, final long

}

private static CareLinkFollowDownloader getDownloader(){
private static CareLinkFollowDownloader getDownloader() {
if (downloader == null) {
downloader = new CareLinkFollowDownloader(
Pref.getString("clfollow_user", ""),
Expand Down Expand Up @@ -223,16 +223,16 @@ public int onStartCommand(Intent intent, int flags, int startId) {
gracePeriod = Pref.getStringToInt("clfollow_grace_period", 30);
if (missedPollInterval == 0)
missedPollInterval = Pref.getStringToInt("clfollow_missed_poll_interval", 5);
if(renewBefore == 0)
if (renewBefore == 0)
renewBefore = 10;
if(renewInterval == 0)
if (renewInterval == 0)
renewInterval = 1;
lastBg = BgReading.lastNoSenssor();
if (lastBg != null) {
lastBgTime = lastBg.timestamp;
}
// Check if downloader needs to be started (last BG old or token needs to be renewed)
final boolean refreshToken = (JoH.msTill(CareLinkCredentialStore.getInstance().getExpiresOn()) < getRenewBeforeMillis()) ? true : false;
final boolean refreshToken = (JoH.msTill(CareLinkCredentialStore.getInstance().getAccessExpiresOn()) < getRenewBeforeMillis()) ? true : false;
final boolean downloadData = (lastBg == null || msSince(lastBg.timestamp) > SAMPLE_PERIOD + getGraceMillis()) ? true : false;
if (refreshToken || downloadData) {
//Only start if rate limit is not exceeded
Expand Down Expand Up @@ -262,7 +262,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
public static List<StatusItem> megaStatus() {
final BgReading lastBg = BgReading.lastNoSenssor();

long hightlightGrace = Constants.SECOND_IN_MS * getGraceMillis() + Constants.SECOND_IN_MS * 10; //garce + 20 seconds for processing
long hightlightGrace = Constants.SECOND_IN_MS * getGraceMillis() + Constants.SECOND_IN_MS * 10; //garce + 20 seconds for processing

// Status for BG receive delay (time from bg was recorded till received in xdrip)
String ageOfBgLastPoll = "n/a";
Expand Down Expand Up @@ -300,18 +300,36 @@ public static List<StatusItem> megaStatus() {
authHighlight = StatusItem.Highlight.GOOD;
authStatus = "AUTHENTICATED";
break;
case CareLinkCredentialStore.TOKEN_EXPIRED:
case CareLinkCredentialStore.ACCESS_EXPIRED:
authHighlight = StatusItem.Highlight.NOTICE;
authStatus = "ACCESS EXPIRED";
break;
case CareLinkCredentialStore.REFRESH_EXPIRED:
authHighlight = StatusItem.Highlight.BAD;
authStatus = "TOKEN EXPIRED";
authStatus = "REFRESH EXPIRED";
break;
}

//Client type
String clientType = "Unkown";
try {
switch (CareLinkCredentialStore.getInstance().getCredential().authType) {
case Browser:
clientType = "Browser";
break;
case MobileApp:
clientType = "CarePartner app";
break;
}
} catch (Exception ex) {
}

//Build status screeen
List<StatusItem> megaStatus = new ArrayList<>();

megaStatus.add(new StatusItem("Client type", clientType));
megaStatus.add(new StatusItem("Authentication status", authStatus, authHighlight));
megaStatus.add(new StatusItem("Login expires in", JoH.niceTimeScalar(CareLinkCredentialStore.getInstance().getExpiresIn())));
megaStatus.add(new StatusItem("Access expires in", JoH.niceTimeScalar(CareLinkCredentialStore.getInstance().getAccessExpiresIn())));
megaStatus.add(new StatusItem("Login expires in", JoH.niceTimeScalar(CareLinkCredentialStore.getInstance().getRefreshExpiresIn())));
megaStatus.add(new StatusItem());
megaStatus.add(new StatusItem("Latest BG", ageLastBg + (lastBg != null ? " ago" : ""), bgAgeHighlight));
megaStatus.add(new StatusItem("BG receive delay", ageOfBgLastPoll, ageOfLastBgPollHighlight));
Expand Down Expand Up @@ -374,4 +392,4 @@ public static SpannableString nanoStatus() {
return emptyString(current_state) ? null : new SpannableString(current_state);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.eveningoutpost.dexdrip.cgm.carelinkfollow.auth;

public enum CareLinkAuthType {
Browser,
MobileApp
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.eveningoutpost.dexdrip.cgm.carelinkfollow.auth;

import okhttp3.Headers;

public class CareLinkAuthentication {

public CareLinkAuthType authType;
private Headers.Builder builder;

public CareLinkAuthentication(Headers headers, CareLinkAuthType authType) {
this.builder = new Headers.Builder();
this.builder.addAll(headers);
this.authType = authType;
}

public Headers getHeaders() {
return builder.build();
}

}
Loading

0 comments on commit a8b51a0

Please sign in to comment.