// Copyright (c) 2014 Eugene Zhuk.
// Use of this source code is governed by the MIT license that can be found
// in the LICENSE file.

package com.ezhuk.wear;

import android.os.AsyncTask;
import android.util.Log;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.wearable.Asset;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.MessageApi;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.NodeApi;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.PutDataRequest;
import com.google.android.gms.wearable.Wearable;


public class MessageUtils {
    private static class SendTextTask extends AsyncTask<Object, Void, Void> {
        private static final String TAG = "MessageUtils.SendTextTask";
        protected Void doInBackground(Object... params) {
            GoogleApiClient client = (GoogleApiClient)params[0];
            String path = (String)params[1];
            String text = (String)params[2];

            NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi
                    .getConnectedNodes(client).await();
            for (Node node : nodes.getNodes()) {
                MessageApi.SendMessageResult result = Wearable.MessageApi
                        .sendMessage(client, node.getId(), path, text.getBytes())
                        .await();
                if (!result.getStatus().isSuccess()) {
                    Log.e(TAG, "[ERROR] could not send message (" + result.getStatus() + ")");
                }
            }

            return null;
        }
    }

    public static void sendMessage(GoogleApiClient client, String path, String text) {
        new SendTextTask().execute(client, path, text);
    }

    public static void sendData(GoogleApiClient client, String path, Asset asset) {
        PutDataMapRequest dataMap = PutDataMapRequest.create(path);
        dataMap.getDataMap().putAsset("data", asset);
        PutDataRequest request = dataMap.asPutDataRequest();
        PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi
                .putDataItem(client, request);
    }
}

@@ -778,28 +778,31 @@
 c class Send
-Text
+Message
 Task extends
@@ -890,28 +890,31 @@
 geUtils.Send
-Text
+Message
 Task%22;%0A     
@@ -1703,32 +1703,905 @@
        %7D%0A    %7D%0A%0A
+    private static class SendDataTask extends AsyncTask%3CObject, Void, Void%3E %7B%0A        private static final String TAG = %22MessageUtils.SendDataTask%22;%0A        protected Void doInBackground(Object... params) %7B%0A            GoogleApiClient client = (GoogleApiClient)params%5B0%5D;%0A            String path = (String)params%5B1%5D;%0A            Asset asset = (Asset)params%5B2%5D;%0A%0A            PutDataMapRequest dataMap = PutDataMapRequest.create(path);%0A            dataMap.getDataMap().putAsset(%22data%22, asset);%0A            PutDataRequest request = dataMap.asPutDataRequest();%0A            DataApi.DataItemResult result = Wearable.DataApi%0A                    .putDataItem(client, request).await();%0A            if (!result.getStatus().isSuccess()) %7B%0A                Log.e(TAG, %22%5BERROR%5D could not send data (%22 + result.getStatus() + %22)%22);%0A            %7D%0A%0A            return null;%0A        %7D%0A    %7D%0A%0A
     public stati
@@ -2691,12 +2691,15 @@
 Send
-Text
+Message
 Task
@@ -2833,305 +2833,53 @@
     
-PutDataMapRequest dataMap = PutDataMapRequest.create(path);%0A        dataMap.getDataMap().putAsset(%22data%22, asset);%0A        PutDataRequest request = dataMap.asPutDataRequest();%0A        PendingResult%3CDataApi.DataItemResult%3E pendingResult = Wearable.DataApi%0A                .putDataItem(client, reques
+new SendDataTask().execute(client, path, asse
 t);%0A

Use async task to send data asset.