Skip to content

Commit

Permalink
Merge pull request #38 from benceszasz/dev
Browse files Browse the repository at this point in the history
reCAPTCHA login using WebView
  • Loading branch information
benceszasz committed Oct 1, 2023
2 parents 6966afa + 46e0d3d commit bf98076
Show file tree
Hide file tree
Showing 11 changed files with 949 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import com.eveningoutpost.dexdrip.Models.UserError;
import com.eveningoutpost.dexdrip.UtilityModels.CollectionServiceStarter;
import com.eveningoutpost.dexdrip.UtilityModels.Inevitable;
import com.eveningoutpost.dexdrip.cgm.carelinkfollow.auth.CareLinkAuthenticator;
import com.eveningoutpost.dexdrip.cgm.carelinkfollow.auth.CareLinkCredentialStore;
import com.eveningoutpost.dexdrip.cgm.carelinkfollow.client.*;
import com.eveningoutpost.dexdrip.cgm.carelinkfollow.message.RecentData;

import static com.eveningoutpost.dexdrip.Models.JoH.dateText;
import static com.eveningoutpost.dexdrip.Models.JoH.emptyString;

public class CareLinkFollowDownloader {
Expand All @@ -31,11 +34,12 @@ public class CareLinkFollowDownloader {

private int lastResponseCode = 0;

public String getStatus(){
public String getStatus() {
return status;
}
public int getLastResponseCode(){
return lastResponseCode;

public int getLastResponseCode() {
return lastResponseCode;
}

CareLinkFollowDownloader(String carelinkUsername, String carelinkPassword, String carelinkCountry, String carelinkPatient) {
Expand All @@ -44,33 +48,57 @@ public int getLastResponseCode(){
this.carelinkCountry = carelinkCountry;
this.carelinkPatient = carelinkPatient;
loginDataLooksOkay = !emptyString(carelinkUsername) && !emptyString(carelinkPassword) && carelinkCountry != null && !emptyString(carelinkCountry);
loginDataLooksOkay = true;
}

public static void resetInstance() {
UserError.Log.d(TAG, "Instance reset");
CollectionServiceStarter.restartCollectionServiceBackground();
}

public boolean doEverything( ) {
public boolean doEverything() {
msg("Start download");

if (D) UserError.Log.e(TAG, "doEverything called");
if (loginDataLooksOkay) {
//if (loginDataLooksOkay) {
if (CareLinkCredentialStore.getInstance().getAuthStatus() == CareLinkCredentialStore.AUTHENTICATED) {
if (CareLinkCredentialStore.getInstance().getExpiresIn() < 6 * 60_000) {
UserError.Log.e(TAG, "Token is about to expire, trying to renew it.");
try {
if (getCareLinkClient() != null) {
extendWakeLock(30_000);
backgroundProcessConnectData();
} else {
UserError.Log.d(TAG, "Cannot get data as CareLinkClient is null");
if (!(new CareLinkAuthenticator(CareLinkCredentialStore.getInstance().getCredential().country, CareLinkCredentialStore.getInstance()).refreshToken())) {
UserError.Log.e(TAG, "Error renewing token!");
return false;
} else {
UserError.Log.e(TAG, "Token renewed!");
}
return true;
} catch (Exception e) {
UserError.Log.e(TAG, "Got exception in getData() " + e);
releaseWakeLock();
UserError.Log.e(TAG, "Exception in renewing token! " + e.getMessage());
return false;
}
}
try {
if (getCareLinkClient() != null) {
extendWakeLock(30_000);
backgroundProcessConnectData();
} else {
UserError.Log.d(TAG, "Cannot get data as CareLinkClient is null");
return false;
}
} else {
return true;
} catch (Exception e) {
UserError.Log.e(TAG, "Got exception in getData() " + e);
releaseWakeLock();
return false;
}
} else {
if (CareLinkCredentialStore.getInstance().getAuthStatus() == CareLinkCredentialStore.NOT_AUTHENTICATED) {
UserError.Log.e(TAG, "Not authenticated! Please login!");
msg("Not authenticated!");
}
if (CareLinkCredentialStore.getInstance().getAuthStatus() == CareLinkCredentialStore.TOKEN_EXPIRED) {
UserError.Log.e(TAG, "Token expired!");
msg("Token expired!");
}
/*
final String invalid = "Invalid CareLink login data!";
msg(invalid);
UserError.Log.e(TAG, invalid);
Expand All @@ -85,6 +113,7 @@ public boolean doEverything( ) {
}else if(!CountryUtils.isSupportedCountry(carelinkCountry)){
UserError.Log.e(TAG, "CareLink Country not supported!");
}
*/
return false;
}

Expand Down Expand Up @@ -116,11 +145,11 @@ private void processCareLinkData() {
if (carelinkClient != null) {

//Try twice in case of 401 error
for(int i = 0; i < 2; i++) {
for (int i = 0; i < 2; i++) {

//Get data
try {
if(JoH.emptyString(this.carelinkPatient))
if (JoH.emptyString(this.carelinkPatient))
recentData = getCareLinkClient().getRecentData();
else
recentData = getCareLinkClient().getRecentData(this.carelinkPatient);
Expand All @@ -134,18 +163,18 @@ private void processCareLinkData() {
UserError.Log.d(TAG, "Success get data!");
//Process data
try {
if (D) UserError.Log.d(TAG, "Start process data");
//Process CareLink data (conversion and update xDrip data)
CareLinkDataProcessor.processRecentData(recentData, true);
if (D) UserError.Log.d(TAG, "ProcessData finished!");
//Update Service status
CareLinkFollowService.updateBgReceiveDelay();
msg(null);
if (D) UserError.Log.d(TAG, "Start process data");
//Process CareLink data (conversion and update xDrip data)
CareLinkDataProcessor.processRecentData(recentData, true);
if (D) UserError.Log.d(TAG, "ProcessData finished!");
//Update Service status
CareLinkFollowService.updateBgReceiveDelay();
msg(null);
} catch (Exception e) {
UserError.Log.e(TAG, "Exception in data processing: " + e);
msg("Data processing error!");
}
//Data receive error
//Data receive error
} else {
//first 401 error => TRY AGAIN, only debug log
if (carelinkClient.getLastResponseCode() == 401 && i == 0) {
Expand All @@ -155,7 +184,7 @@ private void processCareLinkData() {
UserError.Log.e(TAG, "CareLink login error! Response code: " + carelinkClient.getLastResponseCode());
msg("Login error!");
//login error
} else if (!getCareLinkClient().getLastLoginSuccess()){
} else if (!getCareLinkClient().getLastLoginSuccess()) {
UserError.Log.e(TAG, "CareLink login error! Response code: " + carelinkClient.getLastResponseCode());
UserError.Log.e(TAG, "Error message: " + getCareLinkClient().getLastErrorMessage());
msg("Login error!");
Expand All @@ -168,7 +197,7 @@ private void processCareLinkData() {
}

//Next try only for 401 error and first attempt
if(!(carelinkClient.getLastResponseCode() == 401 && i == 0))
if (!(carelinkClient.getLastResponseCode() == 401 && i == 0))
break;

}
Expand All @@ -182,7 +211,9 @@ private CareLinkClient getCareLinkClient() {
if (carelinkClient == null) {
try {
UserError.Log.d(TAG, "Creating CareLinkClient");
carelinkClient = new CareLinkClient(carelinkUsername, carelinkPassword, carelinkCountry);
if (CareLinkCredentialStore.getInstance().getAuthStatus() == CareLinkCredentialStore.AUTHENTICATED)
carelinkClient = new CareLinkClient(CareLinkCredentialStore.getInstance());
//carelinkClient = new CareLinkClient(carelinkUsername, carelinkPassword, carelinkCountry);
} catch (Exception e) {
UserError.Log.e(TAG, "Error creating CareLinkClient");
}
Expand All @@ -193,12 +224,12 @@ private CareLinkClient getCareLinkClient() {

private static synchronized void extendWakeLock(final long ms) {
if (wl == null) {
if (D) UserError.Log.d(TAG,"Creating wakelock");
if (D) UserError.Log.d(TAG, "Creating wakelock");
wl = JoH.getWakeLock("CareLinkFollow-download", (int) ms);
} else {
JoH.releaseWakeLock(wl); // lets not get too messy
wl.acquire(ms);
if (D) UserError.Log.d(TAG,"Extending wakelock");
if (D) UserError.Log.d(TAG, "Extending wakelock");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.eveningoutpost.dexdrip.UtilityModels.Inevitable;
import com.eveningoutpost.dexdrip.UtilityModels.Pref;
import com.eveningoutpost.dexdrip.UtilityModels.StatusItem;
import com.eveningoutpost.dexdrip.cgm.carelinkfollow.auth.CareLinkCredentialStore;
import com.eveningoutpost.dexdrip.utils.DexCollectionType;
import com.eveningoutpost.dexdrip.utils.framework.BuggySamsung;
import com.eveningoutpost.dexdrip.utils.framework.ForegroundService;
Expand Down Expand Up @@ -96,11 +97,10 @@ private static long getGraceMillis() {

private static long getIntervalMillis() {
//return Constants.SECOND_IN_MS * WAKE_UP_GRACE_SECOND;
if(pollInterval == 0) {
if(DEBUG_TIMING)UserError.Log.d(TAG, "POLL INTERVAL IS 0 !!!");
if (pollInterval == 0) {
if (DEBUG_TIMING) UserError.Log.d(TAG, "POLL INTERVAL IS 0 !!!");
return SAMPLE_PERIOD;
}
else
} else
return Constants.MINUTE_IN_MS * pollInterval;
}

Expand All @@ -121,34 +121,35 @@ public static long anticipateNextWakeUp(long now, final long last, final long pe
long next;
long expectedLast;

if(DEBUG_TIMING)UserError.Log.d(TAG, "Now: " + JoH.dateTimeText(now));
if(DEBUG_TIMING)UserError.Log.d(TAG, "Last: " + JoH.dateTimeText(last));
if(DEBUG_TIMING)UserError.Log.d(TAG, "Period: " + String.valueOf(period));
if(DEBUG_TIMING)UserError.Log.d(TAG, "Interval: " + String.valueOf(interval));
if(DEBUG_TIMING)UserError.Log.d(TAG, "Grace: " + String.valueOf(grace));
if (DEBUG_TIMING) UserError.Log.d(TAG, "Now: " + JoH.dateTimeText(now));
if (DEBUG_TIMING) UserError.Log.d(TAG, "Last: " + JoH.dateTimeText(last));
if (DEBUG_TIMING) UserError.Log.d(TAG, "Period: " + String.valueOf(period));
if (DEBUG_TIMING) UserError.Log.d(TAG, "Interval: " + String.valueOf(interval));
if (DEBUG_TIMING) UserError.Log.d(TAG, "Grace: " + String.valueOf(grace));

//recent reading (less then data period) => last + period + grace
if((now - last) < period) {
if ((now - last) < period) {
next = last + period + grace;
if(DEBUG_TIMING)UserError.Log.d(TAG, "Recent reading case, next wakeup: " + JoH.dateTimeText(next));
if (DEBUG_TIMING)
UserError.Log.d(TAG, "Recent reading case, next wakeup: " + JoH.dateTimeText(next));
}
//old reading => anticipated next + grace
else{
else {
//last expected
if(DEBUG_TIMING)UserError.Log.d(TAG, "Old reading.");
if (DEBUG_TIMING) UserError.Log.d(TAG, "Old reading.");
next = now + ((last - now) % period);
if(DEBUG_TIMING)UserError.Log.d(TAG, "Last expected: " + JoH.dateTimeText(next));
if (DEBUG_TIMING) UserError.Log.d(TAG, "Last expected: " + JoH.dateTimeText(next));
//add poll interval until next time is reached
while(next < now){
while (next < now) {
next += interval;
}
if(DEBUG_TIMING)UserError.Log.d(TAG, "Next poll: " + JoH.dateTimeText(next));
if (DEBUG_TIMING) UserError.Log.d(TAG, "Next poll: " + JoH.dateTimeText(next));
//add grace
next += grace;
if(DEBUG_TIMING)UserError.Log.d(TAG, "Next poll + grace: " + JoH.dateTimeText(next));
if (DEBUG_TIMING) UserError.Log.d(TAG, "Next poll + grace: " + JoH.dateTimeText(next));
}

return next;
return next;

}

Expand All @@ -173,9 +174,9 @@ public int onStartCommand(Intent intent, int flags, int startId) {
last_wakeup = JoH.tsl();

// Check current
if(gracePeriod == 0)
if (gracePeriod == 0)
gracePeriod = Pref.getStringToInt("clfollow_grace_period", 30);
if(pollInterval == 0)
if (pollInterval == 0)
pollInterval = Pref.getStringToInt("clfollow_poll_interval", 5);
lastBg = BgReading.lastNoSenssor();
if (lastBg != null) {
Expand All @@ -195,7 +196,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
if (JoH.ratelimit("last-carelink-follow-poll", 5)) {
Inevitable.task("CareLink-Follow-Work", 200, () -> {
try {
downloader.doEverything( );
downloader.doEverything();
} catch (NullPointerException e) {
UserError.Log.e(TAG, "Caught concurrency exception when trying to run doeverything");
}
Expand Down Expand Up @@ -247,8 +248,36 @@ public static List<StatusItem> megaStatus() {
// Last response code
int lastResponseCode = downloader != null ? downloader.getLastResponseCode() : 0;

List<StatusItem> megaStatus = new ArrayList<>();
StatusItem.Highlight authHighlight = StatusItem.Highlight.NORMAL;

if (lastBg != null) {
long age = JoH.msSince(lastBg.timestamp);
ageLastBg = JoH.niceTimeScalar(age);
if (age > SAMPLE_PERIOD + hightlightGrace) {
bgAgeHighlight = StatusItem.Highlight.BAD;
}
}
String authStatus = null;
switch (CareLinkCredentialStore.getInstance().getAuthStatus()) {
case CareLinkCredentialStore.NOT_AUTHENTICATED:
authStatus = "NOT AUTHENTICATED";
authHighlight = StatusItem.Highlight.CRITICAL;
break;
case CareLinkCredentialStore.AUTHENTICATED:
authHighlight = StatusItem.Highlight.GOOD;
authStatus = "AUTHENTICATED";
break;
case CareLinkCredentialStore.TOKEN_EXPIRED:
authHighlight = StatusItem.Highlight.BAD;
authStatus = "TOKEN EXPIRED";
break;
}

//Create status screeen
List<StatusItem> megaStatus = new ArrayList<>();
megaStatus.add(new StatusItem("Authentication status", authStatus, authHighlight));
megaStatus.add(new StatusItem("Token expires in", JoH.niceTimeScalar(CareLinkCredentialStore.getInstance().getExpiresIn())));
megaStatus.add(new StatusItem());
megaStatus.add(new StatusItem("Latest BG", ageLastBg + (lastBg != null ? " ago" : ""), bgAgeHighlight));
megaStatus.add(new StatusItem("BG receive delay", ageOfBgLastPoll, ageOfLastBgPollHighlight));
megaStatus.add(new StatusItem("Data period:", JoH.niceTimeScalar(SAMPLE_PERIOD)));
Expand All @@ -261,7 +290,7 @@ public static List<StatusItem> megaStatus() {
if (lastBg != null) {
megaStatus.add(new StatusItem("Last BG time", JoH.dateTimeText(lastBg.timestamp)));
}
megaStatus.add(new StatusItem("Last poll time", lastPoll > 0 ? JoH.dateTimeText(lastPoll) : "n/a"));
megaStatus.add(new StatusItem("Last poll time", lastPoll > 0 ? JoH.dateTimeText(lastPoll) : "n/a"));
megaStatus.add(new StatusItem("Next poll time", JoH.dateTimeText(wakeup_time)));
megaStatus.add(new StatusItem());
megaStatus.add(new StatusItem("Last response code", lastResponseCode));
Expand Down
Loading

0 comments on commit bf98076

Please sign in to comment.