Search in sources :

Example 6 with OpenScale

use of com.health.openscale.core.OpenScale in project openScale by oliexdev.

the class MainActivity method startActionCreateDocumentForExportIntent.

private void startActionCreateDocumentForExportIntent() {
    OpenScale openScale = OpenScale.getInstance(getApplicationContext());
    ScaleUser selectedScaleUser = openScale.getSelectedScaleUser();
    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("text/csv");
    intent.putExtra(Intent.EXTRA_TITLE, getExportFilename(selectedScaleUser));
    startActivityForResult(intent, EXPORT_DATA_REQUEST);
}
Also used : ScaleUser(com.health.openscale.core.datatypes.ScaleUser) Intent(android.content.Intent) OpenScale(com.health.openscale.core.OpenScale)

Example 7 with OpenScale

use of com.health.openscale.core.OpenScale in project openScale by oliexdev.

the class MainActivity method doExportData.

private boolean doExportData(Uri uri) {
    OpenScale openScale = OpenScale.getInstance(getApplicationContext());
    if (openScale.exportData(uri)) {
        String filename = openScale.getFilenameFromUri(uri);
        Toast.makeText(this, getResources().getString(R.string.info_data_exported) + " " + filename, Toast.LENGTH_SHORT).show();
        return true;
    }
    return false;
}
Also used : OpenScale(com.health.openscale.core.OpenScale)

Example 8 with OpenScale

use of com.health.openscale.core.OpenScale in project openScale by oliexdev.

the class MainActivity method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != RESULT_OK || data == null) {
        return;
    }
    OpenScale openScale = OpenScale.getInstance(getApplicationContext());
    switch(requestCode) {
        case IMPORT_DATA_REQUEST:
            openScale.importData(data.getData());
            break;
        case EXPORT_DATA_REQUEST:
            if (doExportData(data.getData())) {
                SharedPreferences.Editor editor = prefs.edit();
                String key = getExportPreferenceKey(openScale.getSelectedScaleUser());
                // Remove any old persistable permission and export uri
                try {
                    getContentResolver().releasePersistableUriPermission(Uri.parse(prefs.getString(key, "")), Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                    editor.remove(key);
                } catch (Exception ex) {
                // Ignore
                }
                // Take persistable permission and save export uri
                try {
                    getContentResolver().takePersistableUriPermission(data.getData(), Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                    editor.putString(key, data.getData().toString());
                } catch (Exception ex) {
                // Ignore
                }
                editor.apply();
            }
            break;
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) OpenScale(com.health.openscale.core.OpenScale) ActivityNotFoundException(android.content.ActivityNotFoundException)

Example 9 with OpenScale

use of com.health.openscale.core.OpenScale in project openScale by oliexdev.

the class MainActivity method exportCsvFile.

private void exportCsvFile() {
    OpenScale openScale = OpenScale.getInstance(getApplicationContext());
    final ScaleUser selectedScaleUser = openScale.getSelectedScaleUser();
    Uri uri;
    try {
        String exportUri = prefs.getString(getExportPreferenceKey(selectedScaleUser), "");
        uri = Uri.parse(exportUri);
        // Verify that the file still exists and that we have write permission
        getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        openScale.getFilenameFromUriMayThrow(uri);
    } catch (Exception ex) {
        uri = null;
    }
    if (uri == null) {
        startActionCreateDocumentForExportIntent();
        return;
    }
    AlertDialog.Builder exportDialog = new AlertDialog.Builder(this);
    exportDialog.setTitle(R.string.label_export);
    exportDialog.setMessage(getResources().getString(R.string.label_export_overwrite, openScale.getFilenameFromUri(uri)));
    final Uri exportUri = uri;
    exportDialog.setPositiveButton(R.string.label_yes, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (!doExportData(exportUri)) {
                prefs.edit().remove(getExportPreferenceKey(selectedScaleUser)).apply();
            }
        }
    });
    exportDialog.setNegativeButton(R.string.label_no, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            startActionCreateDocumentForExportIntent();
        }
    });
    exportDialog.setNeutralButton(R.string.label_cancel, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    exportDialog.show();
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) ScaleUser(com.health.openscale.core.datatypes.ScaleUser) OpenScale(com.health.openscale.core.OpenScale) Uri(android.net.Uri) ActivityNotFoundException(android.content.ActivityNotFoundException) SuppressLint(android.annotation.SuppressLint)

Example 10 with OpenScale

use of com.health.openscale.core.OpenScale in project openScale by oliexdev.

the class DataEntryActivity method updateOnView.

private void updateOnView() {
    int id = 0;
    if (getIntent().hasExtra(EXTRA_ID)) {
        id = getIntent().getExtras().getInt(EXTRA_ID);
    }
    if (scaleMeasurement == null || scaleMeasurement.getId() != id) {
        isDirty = false;
        scaleMeasurement = null;
        previousMeasurement = null;
        nextMeasurement = null;
    }
    OpenScale openScale = OpenScale.getInstance(context);
    if (id > 0) {
        // Show selected scale data
        if (scaleMeasurement == null) {
            ScaleMeasurement[] tupleScaleData = openScale.getTupleScaleData(id);
            previousMeasurement = tupleScaleData[0];
            scaleMeasurement = tupleScaleData[1].clone();
            nextMeasurement = tupleScaleData[2];
            btnLeft.setEnabled(previousMeasurement != null);
            btnRight.setEnabled(nextMeasurement != null);
        }
    } else {
        if (openScale.getScaleMeasurementList().isEmpty()) {
            // Show default values
            scaleMeasurement = new ScaleMeasurement();
            scaleMeasurement.setWeight(openScale.getSelectedScaleUser().getInitialWeight());
        } else {
            // Show the last scale data as default
            scaleMeasurement = openScale.getScaleMeasurementList().get(0).clone();
            scaleMeasurement.setId(0);
            scaleMeasurement.setDateTime(new Date());
            scaleMeasurement.setComment("");
        }
        isDirty = true;
        // clears these values.
        for (MeasurementView measurement : dataEntryMeasurements) {
            if (!measurement.isVisible()) {
                measurement.clearIn(scaleMeasurement);
            }
        }
    }
    for (MeasurementView measurement : dataEntryMeasurements) {
        measurement.loadFrom(scaleMeasurement, previousMeasurement);
    }
    txtDataNr.setMinWidth(txtDataNr.getWidth());
    txtDataNr.setText(DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT).format(scaleMeasurement.getDateTime()));
}
Also used : TimeMeasurementView(com.health.openscale.gui.views.TimeMeasurementView) WeightMeasurementView(com.health.openscale.gui.views.WeightMeasurementView) DateMeasurementView(com.health.openscale.gui.views.DateMeasurementView) MeasurementView(com.health.openscale.gui.views.MeasurementView) ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) OpenScale(com.health.openscale.core.OpenScale) Date(java.util.Date)

Aggregations

OpenScale (com.health.openscale.core.OpenScale)11 ScaleUser (com.health.openscale.core.datatypes.ScaleUser)6 AlertDialog (android.app.AlertDialog)2 ActivityNotFoundException (android.content.ActivityNotFoundException)2 DialogInterface (android.content.DialogInterface)2 Intent (android.content.Intent)2 SuppressLint (android.annotation.SuppressLint)1 SharedPreferences (android.content.SharedPreferences)1 Uri (android.net.Uri)1 Preference (android.preference.Preference)1 ScaleMeasurement (com.health.openscale.core.datatypes.ScaleMeasurement)1 Converters (com.health.openscale.core.utils.Converters)1 DateMeasurementView (com.health.openscale.gui.views.DateMeasurementView)1 MeasurementView (com.health.openscale.gui.views.MeasurementView)1 TimeMeasurementView (com.health.openscale.gui.views.TimeMeasurementView)1 WeightMeasurementView (com.health.openscale.gui.views.WeightMeasurementView)1 File (java.io.File)1 IOException (java.io.IOException)1 Date (java.util.Date)1 List (java.util.List)1