Search in sources :

Example 61 with ScaleUser

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

the class BluetoothYunmaiSE_Mini method parseBytes.

private void parseBytes(byte[] weightBytes) {
    final ScaleUser scaleUser = OpenScale.getInstance().getSelectedScaleUser();
    ScaleMeasurement scaleBtData = new ScaleMeasurement();
    long timestamp = Converters.fromUnsignedInt32Be(weightBytes, 5) * 1000;
    scaleBtData.setDateTime(new Date(timestamp));
    float weight = Converters.fromUnsignedInt16Be(weightBytes, 13) / 100.0f;
    scaleBtData.setWeight(weight);
    if (isMini) {
        int sex;
        if (scaleUser.getGender() == Converters.Gender.MALE) {
            sex = 1;
        } else {
            sex = 0;
        }
        YunmaiLib yunmaiLib = new YunmaiLib(sex, scaleUser.getBodyHeight(), scaleUser.getActivityLevel());
        float bodyFat;
        int resistance = Converters.fromUnsignedInt16Be(weightBytes, 15);
        if (weightBytes[1] >= (byte) 0x1E) {
            Timber.d("Extract the fat value from received bytes");
            bodyFat = Converters.fromUnsignedInt16Be(weightBytes, 17) / 100.0f;
        } else {
            Timber.d("Calculate the fat value using the Yunmai lib");
            bodyFat = yunmaiLib.getFat(scaleUser.getAge(), weight, resistance);
        }
        if (bodyFat != 0) {
            scaleBtData.setFat(bodyFat);
            scaleBtData.setMuscle(yunmaiLib.getMuscle(bodyFat));
            scaleBtData.setWater(yunmaiLib.getWater(bodyFat));
            scaleBtData.setBone(yunmaiLib.getBoneMass(scaleBtData.getMuscle(), weight));
            scaleBtData.setLbm(yunmaiLib.getLeanBodyMass(weight, bodyFat));
            scaleBtData.setVisceralFat(yunmaiLib.getVisceralFat(bodyFat, scaleUser.getAge()));
        } else {
            Timber.e("body fat is zero");
        }
        Timber.d("received bytes [%s]", byteInHex(weightBytes));
        Timber.d("received decrypted bytes [weight: %.2f, fat: %.2f, resistance: %d]", weight, bodyFat, resistance);
        Timber.d("user [%s]", scaleUser);
        Timber.d("scale measurement [%s]", scaleBtData);
    }
    addScaleMeasurement(scaleBtData);
}
Also used : ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) ScaleUser(com.health.openscale.core.datatypes.ScaleUser) YunmaiLib(com.health.openscale.core.bluetooth.lib.YunmaiLib) Date(java.util.Date)

Example 62 with ScaleUser

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

the class MainActivity method shareCsvFile.

private void shareCsvFile() {
    final ScaleUser selectedScaleUser = OpenScale.getInstance().getSelectedScaleUser();
    File shareFile = new File(getApplicationContext().getCacheDir(), getExportFilename(selectedScaleUser));
    if (!OpenScale.getInstance().exportData(Uri.fromFile(shareFile))) {
        return;
    }
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setType("text/csv");
    final Uri uri = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".fileprovider", shareFile);
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    intent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.label_share_subject, selectedScaleUser.getUserName()));
    startActivity(Intent.createChooser(intent, getResources().getString(R.string.label_share)));
}
Also used : ScaleUser(com.health.openscale.core.datatypes.ScaleUser) Intent(android.content.Intent) File(java.io.File) Uri(android.net.Uri)

Example 63 with ScaleUser

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

the class MainActivity method startActionCreateDocumentForExportIntent.

private void startActionCreateDocumentForExportIntent() {
    OpenScale openScale = OpenScale.getInstance();
    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 64 with ScaleUser

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

the class MainActivity method showAssistedWeighingDialog.

private void showAssistedWeighingDialog(boolean manuelEntry) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    linearLayout.setPadding(50, 50, 0, 0);
    TextView title = new TextView(this);
    title.setText(R.string.label_assisted_weighing);
    title.setTextSize(24);
    title.setTypeface(null, Typeface.BOLD);
    TextView description = new TextView(this);
    description.setPadding(0, 20, 0, 0);
    description.setText(R.string.info_assisted_weighing_choose_reference_user);
    linearLayout.addView(title);
    linearLayout.addView(description);
    builder.setCustomTitle(linearLayout);
    List<ScaleUser> scaleUserList = OpenScale.getInstance().getScaleUserList();
    ArrayList<String> infoTexts = new ArrayList<>();
    ArrayList<Integer> userIds = new ArrayList<>();
    int assistedWeighingRefUserId = prefs.getInt("assistedWeighingRefUserId", -1);
    int checkedItem = 0;
    for (ScaleUser scaleUser : scaleUserList) {
        String singleInfoText = scaleUser.getUserName();
        if (!scaleUser.isAssistedWeighing()) {
            ScaleMeasurement lastRefScaleMeasurement = OpenScale.getInstance().getLastScaleMeasurement(scaleUser.getId());
            if (lastRefScaleMeasurement != null) {
                singleInfoText += " [" + Converters.fromKilogram(lastRefScaleMeasurement.getWeight(), scaleUser.getScaleUnit()) + scaleUser.getScaleUnit().toString() + "]";
            } else {
                singleInfoText += " [" + getString(R.string.label_empty) + "]";
            }
            infoTexts.add(singleInfoText);
            userIds.add(scaleUser.getId());
        }
        if (scaleUser.getId() == assistedWeighingRefUserId) {
            checkedItem = infoTexts.indexOf(singleInfoText);
        }
    }
    if (!infoTexts.isEmpty()) {
        builder.setSingleChoiceItems(infoTexts.toArray(new CharSequence[infoTexts.size()]), checkedItem, null);
    } else {
        builder.setMessage(getString(R.string.info_assisted_weighing_no_reference_user));
    }
    builder.setNegativeButton(R.string.label_cancel, null);
    builder.setPositiveButton(R.string.label_ok, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            int selectedPosition = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
            prefs.edit().putInt("assistedWeighingRefUserId", userIds.get(selectedPosition)).commit();
            ScaleMeasurement lastRefScaleMeasurement = OpenScale.getInstance().getLastScaleMeasurement(userIds.get(selectedPosition));
            if (lastRefScaleMeasurement != null) {
                Calendar calMinusOneDay = Calendar.getInstance();
                calMinusOneDay.add(Calendar.DAY_OF_YEAR, -1);
                if (calMinusOneDay.getTime().after(lastRefScaleMeasurement.getDateTime())) {
                    Toast.makeText(getApplicationContext(), getString(R.string.info_assisted_weighing_old_reference_measurement), Toast.LENGTH_LONG).show();
                }
            } else {
                Toast.makeText(getApplicationContext(), getString(R.string.info_assisted_weighing_no_reference_measurements), Toast.LENGTH_LONG).show();
                return;
            }
            if (manuelEntry) {
                MobileNavigationDirections.ActionNavMobileNavigationToNavDataentry action = MobileNavigationDirections.actionNavMobileNavigationToNavDataentry();
                action.setMode(MeasurementEntryFragment.DATA_ENTRY_MODE.ADD);
                action.setTitle(getString(R.string.label_add_measurement));
                navController.navigate(action);
            } else {
                invokeConnectToBluetoothDevice();
            }
        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) ScaleUser(com.health.openscale.core.datatypes.ScaleUser) TextView(android.widget.TextView) LinearLayout(android.widget.LinearLayout)

Example 65 with ScaleUser

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

the class MainActivity method exportCsvFile.

private void exportCsvFile() {
    OpenScale openScale = OpenScale.getInstance();
    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)

Aggregations

ScaleUser (com.health.openscale.core.datatypes.ScaleUser)69 ScaleMeasurement (com.health.openscale.core.datatypes.ScaleMeasurement)23 Date (java.util.Date)21 Calendar (java.util.Calendar)9 OpenScale (com.health.openscale.core.OpenScale)8 ParseException (java.text.ParseException)6 Converters (com.health.openscale.core.utils.Converters)5 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)4 Intent (android.content.Intent)3 SharedPreferences (android.content.SharedPreferences)3 SimpleDateFormat (java.text.SimpleDateFormat)3 AlertDialog (android.app.AlertDialog)2 Context (android.content.Context)2 DialogInterface (android.content.DialogInterface)2 Uri (android.net.Uri)2 ArrayAdapter (android.widget.ArrayAdapter)2 Spinner (android.widget.Spinner)2 TableRow (android.widget.TableRow)2 TextView (android.widget.TextView)2