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);
}
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;
}
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;
}
}
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();
}
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()));
}
Aggregations