Search in sources :

Example 6 with DataReadRequest

use of com.google.android.gms.fitness.request.DataReadRequest in project Zweet-Fit-App by rachit0904.

the class YearlyFragment method getDataRequestForWeeks.

// create request to retrieve step history for specific weeks
public static DataReadRequest getDataRequestForWeeks(String startDate, String endDate) {
    long start = LocalDateTime.of(Integer.parseInt(startDate.split("-")[0]), Integer.parseInt(startDate.split("-")[1]), Integer.parseInt(startDate.split("-")[2]), 0, 0).toEpochSecond(ZoneOffset.UTC);
    long end = LocalDateTime.of(Integer.parseInt(endDate.split("-")[0]), Integer.parseInt(endDate.split("-")[1]), Integer.parseInt(endDate.split("-")[2]), 0, 0).toEpochSecond(ZoneOffset.UTC);
    java.text.DateFormat dateFormat = DateFormat.getDateInstance();
    Log.e(TAG, "Range Start: " + (startDate));
    Log.e(TAG, "Range End: " + (endDate));
    final DataSource ds = new DataSource.Builder().setAppPackageName("com.google.android.gms").setDataType(DataType.TYPE_STEP_COUNT_DELTA).setType(DataSource.TYPE_DERIVED).setStreamName("estimated_steps").build();
    DataReadRequest readRequest = new DataReadRequest.Builder().aggregate(ds, DataType.TYPE_STEP_COUNT_DELTA).bucketByTime(1, TimeUnit.DAYS).setTimeRange(start, end, TimeUnit.SECONDS).build();
    return readRequest;
}
Also used : DataReadRequest(com.google.android.gms.fitness.request.DataReadRequest) DateFormat(java.text.DateFormat) DataSource(com.google.android.gms.fitness.data.DataSource)

Example 7 with DataReadRequest

use of com.google.android.gms.fitness.request.DataReadRequest in project Zweet-Fit-App by rachit0904.

the class MonthlyFragment method getDataRequestForWeeks.

// create request to retrieve step history for specific weeks
public static DataReadRequest getDataRequestForWeeks(String startDate, String endDate) {
    long start = LocalDateTime.of(Integer.parseInt(startDate.split("-")[0]), Integer.parseInt(startDate.split("-")[1]), Integer.parseInt(startDate.split("-")[2]), 0, 0).toEpochSecond(ZoneOffset.UTC);
    long end = LocalDateTime.of(Integer.parseInt(endDate.split("-")[0]), Integer.parseInt(endDate.split("-")[1]), Integer.parseInt(endDate.split("-")[2]), 0, 0).toEpochSecond(ZoneOffset.UTC);
    java.text.DateFormat dateFormat = DateFormat.getDateInstance();
    Log.e(TAG, "Range Start: " + (startDate));
    Log.e(TAG, "Range End: " + (endDate));
    final DataSource ds = new DataSource.Builder().setAppPackageName("com.google.android.gms").setDataType(DataType.TYPE_STEP_COUNT_DELTA).setType(DataSource.TYPE_DERIVED).setStreamName("estimated_steps").build();
    DataReadRequest readRequest = new DataReadRequest.Builder().aggregate(ds, DataType.TYPE_STEP_COUNT_DELTA).bucketByTime(1, TimeUnit.DAYS).setTimeRange(start, end, TimeUnit.SECONDS).build();
    return readRequest;
}
Also used : DataReadRequest(com.google.android.gms.fitness.request.DataReadRequest) Context(android.content.Context) DateFormat(java.text.DateFormat) DataSource(com.google.android.gms.fitness.data.DataSource)

Example 8 with DataReadRequest

use of com.google.android.gms.fitness.request.DataReadRequest in project capacitor-google-fit by perfood.

the class GoogleFitPlugin method getHistory.

@PluginMethod
public Task<DataReadResponse> getHistory(final PluginCall call) throws ParseException {
    GoogleSignInAccount account = getAccount();
    if (account == null) {
        call.reject("No access");
        return null;
    }
    long startTime = dateToTimestamp(call.getString("startTime"));
    long endTime = dateToTimestamp(call.getString("endTime"));
    if (startTime == -1 || endTime == -1) {
        call.reject("Must provide a start time and end time");
        return null;
    }
    DataReadRequest readRequest = new DataReadRequest.Builder().aggregate(DataType.TYPE_DISTANCE_DELTA).aggregate(DataType.AGGREGATE_DISTANCE_DELTA).aggregate(DataType.TYPE_SPEED).aggregate(DataType.TYPE_CALORIES_EXPENDED).aggregate(DataType.AGGREGATE_CALORIES_EXPENDED).setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS).bucketByTime(1, TimeUnit.DAYS).enableServerQueries().build();
    return Fitness.getHistoryClient(getActivity(), account).readData(readRequest).addOnSuccessListener(new OnSuccessListener<DataReadResponse>() {

        @Override
        public void onSuccess(DataReadResponse dataReadResponse) {
            List<Bucket> buckets = dataReadResponse.getBuckets();
            JSONArray days = new JSONArray();
            for (Bucket bucket : buckets) {
                JSONObject summary = new JSONObject();
                try {
                    summary.put("start", timestampToDate(bucket.getStartTime(TimeUnit.MILLISECONDS)));
                    summary.put("end", timestampToDate(bucket.getEndTime(TimeUnit.MILLISECONDS)));
                    List<DataSet> dataSets = bucket.getDataSets();
                    for (DataSet dataSet : dataSets) {
                        if (dataSet.getDataPoints().size() > 0) {
                            switch(dataSet.getDataType().getName()) {
                                case "com.google.distance.delta":
                                    summary.put("distance", dataSet.getDataPoints().get(0).getValue(Field.FIELD_DISTANCE));
                                    break;
                                case "com.google.speed.summary":
                                    summary.put("speed", dataSet.getDataPoints().get(0).getValue(Field.FIELD_AVERAGE));
                                    break;
                                case "com.google.calories.expended":
                                    summary.put("calories", dataSet.getDataPoints().get(0).getValue(Field.FIELD_CALORIES));
                                    break;
                                default:
                                    Log.i(TAG, "need to handle " + dataSet.getDataType().getName());
                            }
                        }
                    }
                } catch (JSONException e) {
                    call.reject(e.getMessage());
                    return;
                }
                days.put(summary);
            }
            JSObject result = new JSObject();
            result.put("days", days);
            call.resolve(result);
        }
    }).addOnFailureListener(new OnFailureListener() {

        @Override
        public void onFailure(@NonNull Exception e) {
            call.reject(e.getMessage());
        }
    });
}
Also used : DataSet(com.google.android.gms.fitness.data.DataSet) DataReadResponse(com.google.android.gms.fitness.result.DataReadResponse) JSONArray(org.json.JSONArray) DataReadRequest(com.google.android.gms.fitness.request.DataReadRequest) JSONException(org.json.JSONException) JSObject(com.getcapacitor.JSObject) JSONException(org.json.JSONException) ParseException(java.text.ParseException) GoogleSignInAccount(com.google.android.gms.auth.api.signin.GoogleSignInAccount) JSONObject(org.json.JSONObject) Bucket(com.google.android.gms.fitness.data.Bucket) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) PluginMethod(com.getcapacitor.PluginMethod)

Aggregations

DataReadRequest (com.google.android.gms.fitness.request.DataReadRequest)8 DataSource (com.google.android.gms.fitness.data.DataSource)6 DateFormat (java.text.DateFormat)6 Context (android.content.Context)4 JSObject (com.getcapacitor.JSObject)2 PluginMethod (com.getcapacitor.PluginMethod)2 GoogleSignInAccount (com.google.android.gms.auth.api.signin.GoogleSignInAccount)2 Bucket (com.google.android.gms.fitness.data.Bucket)2 DataSet (com.google.android.gms.fitness.data.DataSet)2 DataReadResponse (com.google.android.gms.fitness.result.DataReadResponse)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)1 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)1 ParseException (java.text.ParseException)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 List (java.util.List)1