wear-osandroid-wear-data-api

DataAPI synchronization


I'm developing an app that listens to the gps positions on the wear side and write them on a file. As I want to keep this files synced on the handheld side, currently I'm adding the positions to a datamap as they are acquired:

private void addLocationEntry(double latitude, double longitude, float accuracy, long gpsTime, float speed, double altitude, long time){
    String PATH = "/GPSdata";

    if(!googleClient.isConnected()){
        return;
    }

    String dataString = gpsTime + "\t" + time + "\t" + latitude + "\t" + longitude + "\t" + speed +
            "\t" + distance + "\t" + accuracy + "\t" + altitude + "\n";
    String name = new SimpleDateFormat("HH'h'mm'm'ss's'_dd-MM-yyyy").format(initialTime);

    //make datamap and send it (GPSdata file)
    DataMap dataMap = new DataMap();
    dataMap.putLong(KEY_INITIALTIME, initialTime);
    dataMap.putString(GPSinfo, dataString);
    new SendToDataLayerThread(GPSdata + "/" + time, dataMap).start();
}

class SendToDataLayerThread extends Thread {
    String path;
    DataMap dataMap;

    // Constructor for sending data objects to the data layer
    SendToDataLayerThread(String p, DataMap data) {
        path = p;
        dataMap = data;
    }

    public void run() {
        // Construct a DataRequest and send over the data layer
        PutDataMapRequest putDMR = PutDataMapRequest.create(path);
        putDMR.getDataMap().putAll(dataMap);
        PutDataRequest request = putDMR.asPutDataRequest();
        DataApi.DataItemResult result = Wearable.DataApi.putDataItem(googleClient, request).await();

        if (!result.getStatus().isSuccess()){
            run();
        }
    }
}

The thing is that the user, at the end of the activity, can say that the data gathered until then is not to be stored in the files... So I want to delete all the datamaps sent in order to, on the handheld side, don't receive them..

In this moment I'm sending a datamap at a time every time it is created, that means that in the end, if the user dont want to store the data, all the datamaps have been already sent...

Is there a way to cancel the datamaps created or send them all at the end of the app (without over killing the memory)?


Solution

  • You cannot "schedule" a sync in DataApi, if that is what you are looking for. If keeping it in memory is too much, then your only option is to write that to a local data file and at the end read from the file and send the data and delete the file, or delete the file if user doesn't want to send the data. Note that even if you could schedule a sync, you had the same amount of memory used as well (system had to keep the data in memory before sending) or it had to write them to a file till the schedule comes up, so I am not sure it would be any different if you do it yourself.