Search in sources :

Example 1 with FileLookupAndGetRequest

use of nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileLookupAndGetRequest in project Gadgetbridge by Freeyourgadget.

the class FossilWatchAdapter method onFetchActivityData.

@Override
public void onFetchActivityData() {
    queueWrite(new FileLookupAndGetRequest(FileHandle.ACTIVITY_FILE, this) {

        @Override
        public void handleFileData(byte[] fileData) {
            try (DBHandler dbHandler = GBApplication.acquireDB()) {
                ActivityFileParser parser = new ActivityFileParser();
                ArrayList<ActivityEntry> entries = parser.parseFile(fileData);
                HybridHRActivitySampleProvider provider = new HybridHRActivitySampleProvider(getDeviceSupport().getDevice(), dbHandler.getDaoSession());
                HybridHRActivitySample[] samples = new HybridHRActivitySample[entries.size()];
                Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId();
                Long deviceId = DBHelper.getDevice(getDeviceSupport().getDevice(), dbHandler.getDaoSession()).getId();
                for (int i = 0; i < entries.size(); i++) {
                    samples[i] = entries.get(i).toDAOActivitySample(userId, deviceId);
                }
                provider.addGBActivitySamples(samples);
                queueWrite(new FileDeleteRequest(getHandle()));
                GB.toast("synced activity data", Toast.LENGTH_SHORT, GB.INFO);
            } catch (Exception ex) {
                GB.toast(getContext(), "Error saving steps data: " + ex.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
                GB.updateTransferNotification(null, "Data transfer failed", false, 0, getContext());
            }
            getDeviceSupport().getDevice().sendDeviceUpdateIntent(getContext());
        }

        @Override
        public void handleFileLookupError(FILE_LOOKUP_ERROR error) {
            if (error == FILE_LOOKUP_ERROR.FILE_EMPTY) {
                GB.toast("activity file empty", Toast.LENGTH_SHORT, GB.INFO);
            }
        }
    });
}
Also used : DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) FileLookupAndGetRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileLookupAndGetRequest) ActivityFileParser(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.parser.ActivityFileParser) ArrayList(java.util.ArrayList) HybridHRActivitySampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.qhybrid.HybridHRActivitySampleProvider) FileDeleteRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileDeleteRequest) JSONException(org.json.JSONException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 DBHandler (nodomain.freeyourgadget.gadgetbridge.database.DBHandler)1 HybridHRActivitySampleProvider (nodomain.freeyourgadget.gadgetbridge.devices.qhybrid.HybridHRActivitySampleProvider)1 ActivityFileParser (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.parser.ActivityFileParser)1 FileDeleteRequest (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileDeleteRequest)1 FileLookupAndGetRequest (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileLookupAndGetRequest)1 JSONException (org.json.JSONException)1