mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-06 08:47:56 +02:00
add an option to automatically assign the weight data to the associated user depending on the weight
verify that Bluetooth is supported on the device, and if so, ensure that it is enabled set waist and hip default value to false set default values of weight data to zero
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.health.openscale"
|
||||
android:versionCode="9"
|
||||
android:versionName="1.4.1" >
|
||||
android:versionCode="10"
|
||||
android:versionName="1.4.2" >
|
||||
|
||||
<uses-permission android:name="android.permission.BLUETOOTH"/>
|
||||
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
|
||||
|
@@ -35,6 +35,8 @@ import java.io.OutputStreamWriter;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import static com.health.openscale.core.BluetoothCommunication.BT_MI_SCALE;
|
||||
import static com.health.openscale.core.BluetoothCommunication.BT_OPEN_SCALE;
|
||||
@@ -154,15 +156,29 @@ public class OpenScale {
|
||||
return scaleDB.getDataEntry(id);
|
||||
}
|
||||
|
||||
public void addScaleData(ScaleData scaleData) {
|
||||
addScaleData(scaleData.user_id, dateTimeFormat.format(scaleData.date_time).toString(), scaleData.weight, scaleData.fat,
|
||||
public int addScaleData(ScaleData scaleData) {
|
||||
return addScaleData(scaleData.user_id, dateTimeFormat.format(scaleData.date_time).toString(), scaleData.weight, scaleData.fat,
|
||||
scaleData.water, scaleData.muscle, scaleData.waist, scaleData.hip, scaleData.comment);
|
||||
}
|
||||
|
||||
public void addScaleData(int user_id, String date_time, float weight, float fat,
|
||||
public int addScaleData(int user_id, String date_time, float weight, float fat,
|
||||
float water, float muscle, float waist, float hip, String comment) {
|
||||
ScaleData scaleData = new ScaleData();
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
|
||||
if (user_id == -1) {
|
||||
if (prefs.getBoolean("smartUserAssign", false)) {
|
||||
user_id = getSmartUserAssignment(weight, 15.0f);
|
||||
} else {
|
||||
user_id = getSelectedScaleUser().id;
|
||||
}
|
||||
|
||||
if (user_id == -1) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
scaleData.user_id = user_id;
|
||||
scaleData.date_time = dateTimeFormat.parse(date_time);
|
||||
@@ -180,8 +196,34 @@ public class OpenScale {
|
||||
if (scaleDB.insertEntry(scaleData)) {
|
||||
updateScaleData();
|
||||
}
|
||||
|
||||
return user_id;
|
||||
}
|
||||
|
||||
private int getSmartUserAssignment(float weight, float range) {
|
||||
ArrayList<ScaleUser> scaleUser = getScaleUserList();
|
||||
Map<Float, Integer> inRangeWeights = new TreeMap<>();
|
||||
|
||||
for (int i = 0; i < scaleUser.size(); i++) {
|
||||
ArrayList<ScaleData> scaleUserData = scaleDB.getScaleDataList(scaleUser.get(i).id);
|
||||
|
||||
if (scaleUserData.size() > 0) {
|
||||
float lastWeight = scaleUserData.get(0).weight;
|
||||
|
||||
if ((lastWeight - range) <= weight && (lastWeight + range) >= weight) {
|
||||
inRangeWeights.put(Math.abs(lastWeight - weight), scaleUser.get(i).id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (inRangeWeights.size() > 0) {
|
||||
// return the user id which is nearest to the weight (first element of the tree map)
|
||||
return inRangeWeights.entrySet().iterator().next().getValue();
|
||||
}
|
||||
|
||||
return getSelectedScaleUser().id;
|
||||
}
|
||||
|
||||
public void updateScaleData(long id, String date_time, float weight, float fat, float water, float muscle, float waist, float hip, String comment) {
|
||||
ScaleData scaleData = new ScaleData();
|
||||
|
||||
|
@@ -35,12 +35,12 @@ public class ScaleData {
|
||||
id = -1;
|
||||
user_id = -1;
|
||||
date_time = new Date();
|
||||
weight = -1.0f;
|
||||
fat = -1.0f;
|
||||
water = -1.0f;
|
||||
muscle = -1.0f;
|
||||
waist = -1.0f;
|
||||
hip = -1.0f;
|
||||
weight = 0.0f;
|
||||
fat = 0.0f;
|
||||
water = 0.0f;
|
||||
muscle = 0.0f;
|
||||
waist = 0.0f;
|
||||
hip = 0.0f;
|
||||
comment = new String();
|
||||
}
|
||||
|
||||
|
@@ -135,12 +135,12 @@ public class DataEntryActivity extends Activity {
|
||||
row.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
if(!prefs.getBoolean("waistEnable", true)) {
|
||||
if(!prefs.getBoolean("waistEnable", false)) {
|
||||
TableRow row = (TableRow)findViewById(R.id.tableRowWaist);
|
||||
row.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
if(!prefs.getBoolean("hipEnable", true)) {
|
||||
if(!prefs.getBoolean("hipEnable", false)) {
|
||||
TableRow row = (TableRow)findViewById(R.id.tableRowHip);
|
||||
row.setVisibility(View.GONE);
|
||||
}
|
||||
@@ -258,7 +258,7 @@ public class DataEntryActivity extends Activity {
|
||||
}
|
||||
}
|
||||
|
||||
if (prefs.getBoolean("waistEnable", true)) {
|
||||
if (prefs.getBoolean("waistEnable", false)) {
|
||||
if (txtWaist.getText().toString().length() == 0) {
|
||||
txtWaist.setError(getResources().getString(R.string.error_waist_value_required));
|
||||
validate = false;
|
||||
@@ -268,7 +268,7 @@ public class DataEntryActivity extends Activity {
|
||||
}
|
||||
}
|
||||
|
||||
if (prefs.getBoolean("hipEnable", true)) {
|
||||
if (prefs.getBoolean("hipEnable", false)) {
|
||||
if (txtHip.getText().toString().length() == 0) {
|
||||
txtHip.setError(getResources().getString(R.string.error_hip_value_required));
|
||||
validate = false;
|
||||
|
@@ -17,6 +17,8 @@
|
||||
package com.health.openscale.gui;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
@@ -143,9 +145,6 @@ public class MainActivity extends ActionBarActivity implements
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
// Handle action bar item clicks here. The action bar will
|
||||
// automatically handle clicks on the Home/Up button, so long
|
||||
// as you specify a parent activity in AndroidManifest.xml.
|
||||
int id = item.getItemId();
|
||||
|
||||
if (id == R.id.action_general_settings) {
|
||||
@@ -155,16 +154,7 @@ public class MainActivity extends ActionBarActivity implements
|
||||
}
|
||||
|
||||
if (id == R.id.action_bluetooth_status) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
|
||||
if (BluetoothAdapter.getDefaultAdapter().isEnabled()) {
|
||||
String deviceName = prefs.getString("btDeviceName", "MI_SCALE");
|
||||
Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_try_connection) + " " + deviceName, Toast.LENGTH_SHORT).show();
|
||||
invokeSearchBluetoothDevice();
|
||||
} else {
|
||||
setBluetoothStatusIcon(R.drawable.bluetooth_disabled);
|
||||
Toast.makeText(getApplicationContext(), "Bluetooth " + getResources().getString(R.string.info_is_not_enable), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
invokeSearchBluetoothDevice();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -172,7 +162,15 @@ public class MainActivity extends ActionBarActivity implements
|
||||
}
|
||||
|
||||
private void invokeSearchBluetoothDevice() {
|
||||
if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) {
|
||||
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
|
||||
BluetoothAdapter btAdapter = bluetoothManager.getAdapter();
|
||||
|
||||
if (btAdapter == null || !btAdapter.isEnabled()) {
|
||||
setBluetoothStatusIcon(R.drawable.bluetooth_disabled);
|
||||
Toast.makeText(getApplicationContext(), "Bluetooth " + getResources().getString(R.string.info_is_not_enable), Toast.LENGTH_SHORT).show();
|
||||
|
||||
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
||||
startActivityForResult(enableBtIntent, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -190,6 +188,7 @@ public class MainActivity extends ActionBarActivity implements
|
||||
}
|
||||
}
|
||||
|
||||
Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_try_connection) + " " + deviceName, Toast.LENGTH_SHORT).show();
|
||||
setBluetoothStatusIcon(R.drawable.bluetooth_searching);
|
||||
|
||||
OpenScale.getInstance(getApplicationContext()).stopSearchingForBluetooth();
|
||||
@@ -205,12 +204,9 @@ public class MainActivity extends ActionBarActivity implements
|
||||
setBluetoothStatusIcon(R.drawable.bluetooth_connection_success);
|
||||
ScaleData scaleBtData = (ScaleData) msg.obj;
|
||||
|
||||
// if no user id is set, use the current user id
|
||||
if (scaleBtData.user_id == -1) {
|
||||
scaleBtData.user_id = OpenScale.getInstance(getApplicationContext()).getSelectedScaleUser().id;
|
||||
if (OpenScale.getInstance(getApplicationContext()).addScaleData(scaleBtData) == -1) {
|
||||
Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_no_selected_user) + "(" + getResources().getString(R.string.label_weight) + ": " + scaleBtData.weight + ")", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
OpenScale.getInstance(getApplicationContext()).addScaleData(scaleBtData);
|
||||
break;
|
||||
case BluetoothCommunication.BT_INIT_PROCESS:
|
||||
setBluetoothStatusIcon(R.drawable.bluetooth_connection_success);
|
||||
|
@@ -481,7 +481,7 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
||||
lines++;
|
||||
}
|
||||
|
||||
if(prefs.getBoolean("waistEnable", true)) {
|
||||
if(prefs.getBoolean("waistEnable", false)) {
|
||||
info_week += String.format("Ø-"+getResources().getString(R.string.label_waist)+": %.1fcm <br>", weekAvgWaist);
|
||||
info_month += String.format("Ø-"+getResources().getString(R.string.label_waist)+": %.1fcm <br>", monthAvgWaist);
|
||||
lines++;
|
||||
@@ -491,13 +491,13 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
||||
lines++;
|
||||
}
|
||||
|
||||
if(prefs.getBoolean("hipEnable", true)) {
|
||||
if(prefs.getBoolean("hipEnable", false)) {
|
||||
info_week += String.format("Ø-"+getResources().getString(R.string.label_hip)+": %.1fcm <br>", weekAvgHip);
|
||||
info_month += String.format("Ø-"+getResources().getString(R.string.label_hip)+": %.1fcm <br>",monthAvgHip);
|
||||
lines++;
|
||||
}
|
||||
|
||||
if(prefs.getBoolean("hipEnable", true) && prefs.getBoolean("waistEnable", true)) {
|
||||
if(prefs.getBoolean("hipEnable", false) && prefs.getBoolean("waistEnable", false)) {
|
||||
info_week += String.format("Ø-"+getResources().getString(R.string.label_whr)+": %.2f <br>", weekAvgWHR);
|
||||
info_month += String.format("Ø-"+getResources().getString(R.string.label_whr)+": %.2f <br>", monthAvgWHR);
|
||||
lines++;
|
||||
@@ -603,11 +603,11 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
||||
lines.add(lineMuscle);
|
||||
}
|
||||
|
||||
if(prefs.getBoolean("waistEnable", true)) {
|
||||
if(prefs.getBoolean("waistEnable", false)) {
|
||||
lines.add(lineWaist);
|
||||
}
|
||||
|
||||
if(prefs.getBoolean("hipEnable", true)) {
|
||||
if(prefs.getBoolean("hipEnable", false)) {
|
||||
lines.add(lineHip);
|
||||
}
|
||||
|
||||
|
@@ -29,7 +29,7 @@
|
||||
<string name="info_is_not_visible">ist nicht sichtbar</string>
|
||||
<string name="info_is_visible">ist sichtbar</string>
|
||||
<string name="info_is_not_available">ist nicht verfügbar</string>
|
||||
<string name="info_no_selected_user">Kein Benutzer existiert! Bitte lege einen neuen Benutzer unter Einstellungen an</string>
|
||||
<string name="info_no_selected_user">Kein Benutzer vorhanden. Bitte erzeugen Sie ein neuen Benutzer unter Einstellungen.</string>
|
||||
<string name="info_on_date">am</string>
|
||||
<string name="info_set_filename">Setzte Dateiname auf</string>
|
||||
<string name="info_your_fat">Dein Körperfett war</string>
|
||||
@@ -104,4 +104,5 @@
|
||||
<string name="info_bluetooth_try_connection">Versuche Verbindung herzustellen zu</string>
|
||||
<string name="label_device_type">Gerätetyp</string>
|
||||
<string name="info_bluetooth_init">Initializiere Bluetooth Gerät</string>
|
||||
<string name="label_smartUserAssign">Intelligente Benutzer Zuweisung</string>
|
||||
</resources>
|
@@ -104,4 +104,5 @@
|
||||
<string name="info_bluetooth_connection_successful">接続に成功しました</string>
|
||||
<string name="label_device_type">デバイスタイプ</string>
|
||||
<string name="info_bluetooth_init">Bluetooth接続を初期化する</string>
|
||||
<string name="label_smartUserAssign">スマートユーザーのアサイン</string>
|
||||
</resources>
|
@@ -30,6 +30,7 @@
|
||||
<string name="label_comment">Comment</string>
|
||||
<string name="label_whtr">Waist-to-height ratio</string>
|
||||
<string name="label_whr">Waist-hip ratio</string>
|
||||
<string name="label_smartUserAssign">Smart User assignment</string>
|
||||
|
||||
<string name="label_days">days</string>
|
||||
<string name="label_measures">measures</string>
|
||||
@@ -100,7 +101,7 @@
|
||||
<string name="info_bluetooth_connection_error">Bluetooth has an unexpected error</string>
|
||||
|
||||
<string name="info_enter_user_name">Enter your name</string>
|
||||
<string name="info_no_selected_user">No user exist! Please create a new user in the settings</string>
|
||||
<string name="info_no_selected_user">No user exists. Please create a new user in the settings.</string>
|
||||
<string name="info_no_evaluation_available">Can\'t evaluate the value</string>
|
||||
|
||||
<string name="question_really_delete_all">Do you really want to delete all database entries?</string>
|
||||
|
@@ -8,6 +8,7 @@
|
||||
<CheckBoxPreference android:title="@string/label_bluetooth_enable" android:summaryOn="@string/info_is_enable" android:summaryOff="@string/info_is_not_enable" android:key="btEnable" android:defaultValue="false"/>
|
||||
<ListPreference android:title="@string/label_device_type" android:key="btDeviceTypes" android:entries="@array/btDeviceTypes" android:entryValues="@array/btDeviceTypesAlias" android:defaultValue="0"/>
|
||||
<EditTextPreference android:title="@string/label_device_name" android:key="btDeviceName" android:defaultValue="MI_SCALE" />
|
||||
<CheckBoxPreference android:title="@string/label_smartUserAssign" android:summaryOn="@string/info_is_enable" android:summaryOff="@string/info_is_not_enable" android:key="smartUserAssign" android:defaultValue="false"/>
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory android:title="@string/title_graph">
|
||||
@@ -15,7 +16,6 @@
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory android:title="@string/title_data">
|
||||
<CheckBoxPreference android:title="@string/label_weight" android:summaryOn="@string/info_is_enable" android:summaryOff="@string/info_is_not_enable" android:key="weightEnable" android:defaultValue="true"/>
|
||||
<CheckBoxPreference android:title="@string/label_fat" android:summaryOn="@string/info_is_enable" android:summaryOff="@string/info_is_not_enable" android:key="fatEnable" android:defaultValue="true"/>
|
||||
<CheckBoxPreference android:title="@string/label_water" android:summaryOn="@string/info_is_enable" android:summaryOff="@string/info_is_not_enable" android:key="waterEnable" android:defaultValue="true"/>
|
||||
<CheckBoxPreference android:title="@string/label_muscle" android:summaryOn="@string/info_is_enable" android:summaryOff="@string/info_is_not_enable" android:key="muscleEnable" android:defaultValue="true"/>
|
||||
|
Reference in New Issue
Block a user