Skip to content

Commit

Permalink
update...
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyen1994 committed Dec 10, 2018
1 parent 466833f commit 58e7959
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 46 additions & 45 deletions googlefitkit/src/main/java/com/example/googlefitkit/FitUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import com.example.googlefitkit.fitEntities.GoogleFitData1;
import com.example.googlefitkit.fitEntities.GoogleFitDataFloatValue;
import com.example.googlefitkit.fitEntities.GoogleFitDataBloodPressure;
import com.example.googlefitkit.fitEntities.GoogleFitDataHeartRate;
import com.example.googlefitkit.fitEntities.GoogleFitDataSleep;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.fitness.Fitness;
import com.google.android.gms.fitness.FitnessOptions;
import com.google.android.gms.fitness.data.*;
import com.google.android.gms.fitness.request.DataReadRequest;
import com.google.android.gms.fitness.result.DataReadResponse;
import com.google.android.gms.tasks.Task;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -48,17 +50,41 @@ public static void requestPermissionsGoogleFit(@NonNull Activity activity,
GoogleSignIn.getLastSignedInAccount(activity),
fitnessOptions);
} else {
///kokokokoooooooo
onAccessListener.onAccessGoogleFit();
}
}

public static DataReadRequest queryFitnessData(long endTime, long startTime) {
public static Task<DataReadResponse> readHistoryClient(Activity activity, DataReadRequest readRequest) {
return Fitness.getHistoryClient(activity, GoogleSignIn.getLastSignedInAccount(activity))
.readData(readRequest);
}

public static DataReadRequest queryFitnessWeightData(long endTime, long startTime) {
return new DataReadRequest.Builder()
.read(DataType.TYPE_WEIGHT)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
}

public static DataReadRequest queryFitnessBodyFatData(long endTime, long startTime) {
return new DataReadRequest.Builder()
.read(DataType.TYPE_BODY_FAT_PERCENTAGE)
.read(DataType.TYPE_ACTIVITY_SEGMENT)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
}

public static DataReadRequest queryFitnessBloodPressureData(long endTime, long startTime) {
return new DataReadRequest.Builder()
.read(HealthDataTypes.TYPE_BLOOD_PRESSURE)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
}

public static DataReadRequest queryFitnessHeartRateData(long endTime, long startTime) {
return new DataReadRequest.Builder()
.read(DataType.TYPE_HEART_RATE_BPM)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
Expand Down Expand Up @@ -107,33 +133,11 @@ public static <T> ArrayList<T> parseDataSet(ArrayList<DataSet> dataSets) {
List<DataPoint> dataPoints = dataSet.getDataPoints();
if (dataSet.getDataType().equals(DataType.TYPE_ACTIVITY_SEGMENT)) {
for (DataPoint dp : dataPoints) {
datas.add((T) parseDataPointSleep(dp, datetimeFormat));
}
} else if(dataSet.getDataType().equals(DataType.TYPE_WEIGHT)) {
if (dataPoints != null && dataPoints.size() > 0) {
DataPoint dp = dataPoints.get(dataPoints.size() - 1);
datas.add((T) parseDataPoint(dp, dateFormat));
}
} else if(dataSet.getDataType().equals(DataType.TYPE_BODY_FAT_PERCENTAGE)) {
if (dataPoints != null && dataPoints.size() > 0) {
DataPoint dp = dataPoints.get(dataPoints.size() - 1);
datas.add((T) parseDataPoint(dp, dateFormat));
}
} else if(dataSet.getDataType().equals(DataType.TYPE_STEP_COUNT_DELTA)) {
if (dataPoints != null && dataPoints.size() > 0) {
DataPoint dp = dataPoints.get(dataPoints.size() - 1);
datas.add((T) parseDataPoint(dp, dateFormat));
}
} else if(dataSet.getDataType().equals(DataType.TYPE_HEART_RATE_BPM)) {
if (dataPoints != null && dataPoints.size() > 0) {
DataPoint dp = dataPoints.get(dataPoints.size() - 1);
datas.add((T) parseDataPoint(dp, dateFormat));
}
} else if(dataSet.getDataType().equals(HealthDataTypes.TYPE_BLOOD_PRESSURE)) {
if (dataPoints != null && dataPoints.size() > 0) {
DataPoint dp = dataPoints.get(dataPoints.size() - 1);
datas.add((T) parseDataPoint(dp, dateFormat));
datas.add((T) parseDataPoint(dp, datetimeFormat));
}
} else if (dataPoints != null && dataPoints.size() > 0) {
DataPoint dp = dataPoints.get(dataPoints.size() - 1);
datas.add((T) parseDataPoint(dp, dateFormat));
}
}
datas.removeAll(Collections.singleton(null));
Expand All @@ -142,14 +146,15 @@ public static <T> ArrayList<T> parseDataSet(ArrayList<DataSet> dataSets) {

public static <T> T parseDataPoint(DataPoint dp, SimpleDateFormat dateFormat) {
String startTime = dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS));
String endTime = dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS));
if (dp.getDataType().equals(DataType.TYPE_WEIGHT)) {
float value = 0;
for (Field field : dp.getDataType().getFields()) {
if (field.equals(Field.FIELD_WEIGHT)) {
value = dp.getValue(field).asFloat();
}
}
return (T) new GoogleFitData1(startTime, value);
return (T) new GoogleFitDataFloatValue(startTime, value);
}
if (dp.getDataType().equals(DataType.TYPE_BODY_FAT_PERCENTAGE)) {
float value = 0;
Expand All @@ -158,7 +163,7 @@ public static <T> T parseDataPoint(DataPoint dp, SimpleDateFormat dateFormat) {
value = dp.getValue(field).asFloat();
}
}
return (T) new GoogleFitData1(startTime, value);
return (T) new GoogleFitDataFloatValue(startTime, value);
}
if (dp.getDataType().equals(DataType.AGGREGATE_STEP_COUNT_DELTA)) {
int value = 0;
Expand All @@ -167,7 +172,7 @@ public static <T> T parseDataPoint(DataPoint dp, SimpleDateFormat dateFormat) {
value = dp.getValue(field).asInt();
}
}
return (T) new GoogleFitData1(startTime, value);
return (T) new GoogleFitDataFloatValue(startTime, value);
}
if (dp.getDataType().equals(HealthDataTypes.TYPE_BLOOD_PRESSURE)) {
float systolic = 0, diasolic = 0;
Expand Down Expand Up @@ -196,21 +201,17 @@ public static <T> T parseDataPoint(DataPoint dp, SimpleDateFormat dateFormat) {
}
return (T) new GoogleFitDataHeartRate(startTime, value, max, min);
}
return null;
}

public static GoogleFitDataSleep parseDataPointSleep(DataPoint dp, SimpleDateFormat dateFormat) {
String startTime = dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS));
String endTime = dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS));
if (dp.getDataType().equals(DataType.TYPE_ACTIVITY_SEGMENT)) {
boolean isSleep = false;
for (Field field : dp.getDataType().getFields()) {
if (dp.getValue(field).toString().equals("72")) {
isSleep = true;
}

boolean isSleep = false;
for (Field field : dp.getDataType().getFields()) {
if(dp.getValue(field).toString().equals("72")){
isSleep = true;
}

return isSleep ? (T) new GoogleFitDataSleep(startTime, endTime) : null;
}
return isSleep ? new GoogleFitDataSleep(startTime, endTime) : null;
return null;
}

public interface OnAccessListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

import java.io.Serializable;

public class GoogleFitData1 implements Serializable {
public class GoogleFitDataFloatValue implements Serializable {
@SerializedName("date")
private String date;
@SerializedName("value")
private float value;

public GoogleFitData1() {
public GoogleFitDataFloatValue() {
}

public GoogleFitData1(String date, float value) {
public GoogleFitDataFloatValue(String date, float value) {
this.date = date;
this.value = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

import java.io.Serializable;

public class GoogleFitData2 implements Serializable {
public class GoogleFitDataIntValue implements Serializable {
@SerializedName("date")
private String date;
@SerializedName("value")
private int value;

public GoogleFitData2() {
public GoogleFitDataIntValue() {
}

public GoogleFitData2(String date, int value) {
public GoogleFitDataIntValue(String date, int value) {
this.date = date;
this.value = value;
}
Expand Down

0 comments on commit 58e7959

Please sign in to comment.