package com.github.pocmo.sensordashboard;

import android.content.Context;
import android.util.Log;

import com.github.pocmo.sensordashboard.shared.DataMapKeys;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.PutDataRequest;
import com.google.android.gms.wearable.Wearable;

import java.util.concurrent.TimeUnit;

public class DeviceClient {
    private static final String TAG = "SensorDashboard/DeviceClient";
    private static final int CLIENT_CONNECTION_TIMEOUT = 15000;

    public static DeviceClient instance;

    public static DeviceClient getInstance(Context context) {
        if (instance == null) {
            instance = new DeviceClient(context.getApplicationContext());
        }

        return instance;
    }

    private Context context;
    private GoogleApiClient googleApiClient;

    private DeviceClient(Context context) {
        this.context = context;

        this.googleApiClient = new GoogleApiClient.Builder(context)
                .addApi(Wearable.API)
                .build();
    }

    private boolean validateConnection() {
        if (googleApiClient.isConnected()) {
            return true;
        }

        ConnectionResult result = googleApiClient.blockingConnect(CLIENT_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);

        return result.isSuccess();
    }

    public void sendSensorData(int sensorType, int accuracy, long timestamp, float[] values) {
        PutDataMapRequest dataMap = PutDataMapRequest.create("/sensors/" + sensorType);

        dataMap.getDataMap().putInt(DataMapKeys.ACCURACY, accuracy);
        dataMap.getDataMap().putLong(DataMapKeys.TIMESTAMP, timestamp);
        dataMap.getDataMap().putFloatArray(DataMapKeys.VALUES, values);

        PutDataRequest putDataRequest = dataMap.asPutDataRequest();
        send(putDataRequest);
    }

    private void send(PutDataRequest putDataRequest) {
        if (validateConnection()) {
            Wearable.DataApi.putDataItem(googleApiClient, putDataRequest).setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
                @Override
                public void onResult(DataApi.DataItemResult dataItemResult) {
                    Log.d(TAG, "Sending sensor data: " + dataItemResult.getStatus().isSuccess());
                }
            });
        }
    }
}

@@ -535,16 +535,100 @@
 rable;%0A%0A
+import java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0A
 import j
@@ -657,16 +657,16 @@
 meUnit;%0A
-
 %0Apublic 
@@ -1147,16 +1147,61 @@
 iClient;
+%0A    private ExecutorService executorService;
 %0A%0A    pr
@@ -1279,21 +1279,16 @@
         
-this.
 googleAp
@@ -1333,33 +1333,16 @@
 context)
-%0A                
 .addApi(
@@ -1358,31 +1358,74 @@
 API)
-%0A                .build
+.build();%0A%0A        executorService = Executors.newCachedThreadPool
 ();%0A
@@ -1711,24 +1711,24 @@
 s();%0A    %7D%0A%0A
-
     public v
@@ -1746,16 +1746,368 @@
 sorData(
+final int sensorType, final int accuracy, final long timestamp, final float%5B%5D values) %7B%0A        executorService.submit(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                sendSensorDataInBackground(sensorType, accuracy, timestamp, values);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private void sendSensorDataInBackground(
 int sens

Fix communication.