Skip to content

Commit

Permalink
add an option to automatically assign the weight data to the associat…
Browse files Browse the repository at this point in the history
…ed 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
  • Loading branch information
oliexdev committed Dec 27, 2016
1 parent 8ac4f98 commit a3b9cfa
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 42 deletions.
4 changes: 2 additions & 2 deletions android_app/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -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"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
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;
Expand Down Expand Up @@ -154,15 +156,29 @@ public ScaleData getScaleData(long id)
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);
Expand All @@ -180,8 +196,34 @@ public void addScaleData(int user_id, String date_time, float weight, float fat,
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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public 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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ private void updateOnView()
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);
}
Expand Down Expand Up @@ -258,7 +258,7 @@ private boolean validateInput()
}
}

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;
Expand All @@ -268,7 +268,7 @@ private boolean validateInput()
}
}

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -143,9 +145,6 @@ public boolean onCreateOptionsMenu(Menu menu) {

@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) {
Expand All @@ -155,24 +154,23 @@ public boolean onOptionsItemSelected(MenuItem item) {
}

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;
}

return super.onOptionsItemSelected(item);
}

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;
}

Expand All @@ -190,6 +188,7 @@ private void invokeSearchBluetoothDevice() {
}
}

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();
Expand All @@ -205,12 +204,9 @@ public void handleMessage(Message msg) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ private void updateStatistics(ArrayList<ScaleData> scaleDataList) {
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++;
Expand All @@ -491,13 +491,13 @@ private void updateStatistics(ArrayList<ScaleData> scaleDataList) {
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++;
Expand Down Expand Up @@ -603,11 +603,11 @@ private void updateLastLineChart(ArrayList<ScaleData> scaleDataList) {
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);
}

Expand Down
3 changes: 2 additions & 1 deletion android_app/app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
1 change: 1 addition & 0 deletions android_app/app/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
3 changes: 2 additions & 1 deletion android_app/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion android_app/app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
<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">
<CheckBoxPreference android:title="@string/label_enable_labels" android:summaryOn="@string/info_is_visible" android:summaryOff="@string/info_is_not_visible" android:key="labelsEnable" android:defaultValue="true"/>
</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"/>
Expand Down

0 comments on commit a3b9cfa

Please sign in to comment.