Search in sources :

Example 1 with FileEncryptedGetRequest

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

the class FossilHRWatchAdapter method onFetchActivityData.

@Override
public void onFetchActivityData() {
    if (connectionMode == CONNECTION_MODE.NOT_AUTHENTICATED) {
        GB.toast(getContext().getString(R.string.fossil_hr_unavailable_unauthed), Toast.LENGTH_LONG, GB.ERROR);
        return;
    }
    syncSettings();
    queueWrite(new FileLookupRequest(FileHandle.ACTIVITY_FILE, this) {

        @Override
        public void handleFileLookup(final short fileHandle) {
            queueWrite((FileEncryptedInterface) new FileEncryptedGetRequest(fileHandle, FossilHRWatchAdapter.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);
                        if (saveRawActivityFiles) {
                            writeFile(String.valueOf(System.currentTimeMillis()), fileData);
                        }
                        queueWrite(new FileDeleteRequest(fileHandle));
                        GB.toast(getContext().getString(R.string.fossil_hr_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 yet", Toast.LENGTH_LONG, GB.ERROR);
            } else {
                throw new RuntimeException("strange lookup stuff");
            }
            getDeviceSupport().getDevice().sendDeviceUpdateIntent(getContext());
        }
    });
}
Also used : FileEncryptedInterface(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.file.FileEncryptedInterface) FileEncryptedGetRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.file.FileEncryptedGetRequest) ActivityFileParser(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.parser.ActivityFileParser) ArrayList(java.util.ArrayList) FileLookupRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileLookupRequest) JSONException(org.json.JSONException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BufferOverflowException(java.nio.BufferOverflowException) DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) HybridHRActivitySampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.qhybrid.HybridHRActivitySampleProvider) FileDeleteRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileDeleteRequest)

Example 2 with FileEncryptedGetRequest

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

the class FossilHRWatchAdapter method downloadFile.

@Override
public void downloadFile(final FileHandle handle, boolean fileIsEncrypted) {
    if (fileIsEncrypted) {
        queueWrite((FileEncryptedInterface) new FileEncryptedGetRequest(handle, this) {

            @Override
            public void handleFileData(byte[] fileData) {
                LOG.debug("downloaded encrypted file");
                handleFileDownload(handle, fileData);
            }
        });
    } else {
        queueWrite(new FileGetRawRequest(handle, this) {

            @Override
            public void handleFileRawData(byte[] fileData) {
                LOG.debug("downloaded regular file");
                handleFileDownload(handle, fileData);
            }
        });
    }
}
Also used : FileEncryptedGetRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.file.FileEncryptedGetRequest) FileGetRawRequest(nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileGetRawRequest)

Aggregations

FileEncryptedGetRequest (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.file.FileEncryptedGetRequest)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 BufferOverflowException (java.nio.BufferOverflowException)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 FileGetRawRequest (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileGetRawRequest)1 FileLookupRequest (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FileLookupRequest)1 FileEncryptedInterface (nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.file.FileEncryptedInterface)1 JSONException (org.json.JSONException)1