Skip to content

Commit

Permalink
improve Wearable API check
Browse files Browse the repository at this point in the history
  • Loading branch information
c99koder committed Sep 10, 2024
1 parent c0df2bf commit 00077d0
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions app/src/main/java/org/c99/healthconnect_librelinkup/SyncWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.AvailabilityException;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.android.gms.wearable.DataClient;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.PutDataRequest;
import com.google.android.gms.wearable.Wearable;
Expand All @@ -39,6 +45,7 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.concurrent.ExecutionException;

import kotlin.coroutines.Continuation;
import kotlin.coroutines.CoroutineContext;
Expand Down Expand Up @@ -92,18 +99,23 @@ public void resumeWith(@NonNull Object o) {
}
});

try {
getApplicationContext().getPackageManager().getPackageInfo("com.google.android.wearable.app", PackageManager.GET_META_DATA);
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/glucose");
putDataMapReq.getDataMap().putFloat(GLUCOSE_KEY, result.data.get(0).glucoseMeasurement.Value);
putDataMapReq.getDataMap().putInt(COLOR_KEY, result.data.get(0).glucoseMeasurement.MeasurementColor);
putDataMapReq.getDataMap().putInt(TREND_ARROW_KEY, result.data.get(0).glucoseMeasurement.TrendArrow);
putDataMapReq.getDataMap().putInt(UNITS_KEY, result.data.get(0).glucoseMeasurement.GlucoseUnits);
putDataMapReq.getDataMap().putString(TIMESTAMP_KEY, result.data.get(0).glucoseMeasurement.FactoryTimestamp);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
Tasks.await(Wearable.getDataClient(getApplicationContext()).putDataItem(putDataReq));
} catch (PackageManager.NameNotFoundException e) {
//android wear app is not installed
if(GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(getApplicationContext()) == com.google.android.gms.common.ConnectionResult.SUCCESS) {
try {
DataClient dc = Wearable.getDataClient(getApplicationContext());
Task<Void> t = GoogleApiAvailability.getInstance().checkApiAvailability(dc);
Tasks.await(t);

PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/glucose");
putDataMapReq.getDataMap().putFloat(GLUCOSE_KEY, result.data.get(0).glucoseMeasurement.Value);
putDataMapReq.getDataMap().putInt(COLOR_KEY, result.data.get(0).glucoseMeasurement.MeasurementColor);
putDataMapReq.getDataMap().putInt(TREND_ARROW_KEY, result.data.get(0).glucoseMeasurement.TrendArrow);
putDataMapReq.getDataMap().putInt(UNITS_KEY, result.data.get(0).glucoseMeasurement.GlucoseUnits);
putDataMapReq.getDataMap().putString(TIMESTAMP_KEY, result.data.get(0).glucoseMeasurement.FactoryTimestamp);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
Tasks.await(dc.putDataItem(putDataReq));
} catch (Exception e) {
//Wearable API not available
}
}
} catch (Exception e) {
e.printStackTrace();
Expand Down

0 comments on commit 00077d0

Please sign in to comment.