diff --git a/android_app/app/src/main/java/com/health/openscale/gui/activities/BluetoothSettingsActivity.java b/android_app/app/src/main/java/com/health/openscale/gui/activities/BluetoothSettingsActivity.java index d69cbd9c..9c804f26 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/activities/BluetoothSettingsActivity.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/activities/BluetoothSettingsActivity.java @@ -17,7 +17,6 @@ package com.health.openscale.gui.activities; import android.app.Activity; import android.app.AlertDialog; -import android.app.DatePickerDialog; import android.bluetooth.BluetoothDevice; import android.bluetooth.le.ScanResult; import android.content.Context; @@ -27,54 +26,38 @@ import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.graphics.Color; import android.graphics.PorterDuff; -import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; -import android.preference.Preference; import android.preference.PreferenceManager; -import android.preference.PreferenceScreen; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.style.ForegroundColorSpan; import android.text.style.RelativeSizeSpan; -import android.view.Menu; -import android.view.MenuInflater; +import android.view.Gravity; import android.view.MenuItem; import android.view.View; -import android.widget.DatePicker; -import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; -import android.widget.RadioGroup; -import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.widget.Toolbar; -import androidx.core.graphics.drawable.DrawableCompat; import com.health.openscale.R; import com.health.openscale.core.OpenScale; import com.health.openscale.core.bluetooth.BluetoothCommunication; import com.health.openscale.core.bluetooth.BluetoothFactory; -import com.health.openscale.core.datatypes.ScaleUser; -import com.health.openscale.core.utils.Converters; -import com.health.openscale.gui.preferences.BluetoothPreferences; import com.health.openscale.gui.utils.ColorUtil; import com.health.openscale.gui.utils.PermissionHelper; import com.welie.blessed.BluetoothCentral; import com.welie.blessed.BluetoothCentralCallback; import com.welie.blessed.BluetoothPeripheral; -import java.text.DateFormat; -import java.util.Calendar; -import java.util.Date; import java.util.HashMap; -import java.util.List; import java.util.Map; import timber.log.Timber; @@ -215,7 +198,6 @@ public class BluetoothSettingsActivity extends BaseAppCompatActivity { if (btDevice != null) { Timber.d("Found supported device %s (driver: %s)", formatDeviceName(device), btDevice.driverName()); - deviceView.setOnClickListener(new onClickListenerDeviceSelect()); deviceView.setDeviceAddress(device.getAddress()); deviceView.setIcon(R.drawable.ic_bluetooth_device_supported); deviceView.setSummaryText(btDevice.driverName()); @@ -242,24 +224,6 @@ public class BluetoothSettingsActivity extends BaseAppCompatActivity { deviceListView.addView(deviceView); } - private class onClickListenerDeviceSelect implements View.OnClickListener { - @Override - public void onClick(View view) { - BluetoothDeviceView deviceView = (BluetoothDeviceView)view; - - BluetoothDevice device = foundDevices.get(deviceView.getDeviceAddress()); - - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - - prefs.edit() - .putString(PREFERENCE_KEY_BLUETOOTH_HW_ADDRESS, device.getAddress()) - .putString(PREFERENCE_KEY_BLUETOOTH_DEVICE_NAME, device.getName()) - .apply(); - - finishActivity(GET_SCALE_REQUEST); - } - } - private void getDebugInfo(final BluetoothDevice device) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Fetching info") @@ -293,7 +257,7 @@ public class BluetoothSettingsActivity extends BaseAppCompatActivity { OpenScale.getInstance().connectToBluetoothDeviceDebugMode(macAddress, btHandler); } - private class BluetoothDeviceView extends LinearLayout { + private class BluetoothDeviceView extends LinearLayout implements View.OnClickListener { private TextView deviceName; private ImageView deviceIcon; @@ -302,9 +266,26 @@ public class BluetoothSettingsActivity extends BaseAppCompatActivity { public BluetoothDeviceView(Context context) { super(context); + LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( + LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); + + layoutParams.setMargins(0, 20, 0, 20); + setLayoutParams(layoutParams); + deviceName = new TextView(context); deviceName.setLines(2); - deviceIcon = new ImageView(context); + deviceIcon = new ImageView(context);; + + LinearLayout.LayoutParams centerLayoutParams = new LinearLayout.LayoutParams( + LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT); + layoutParams.gravity= Gravity.CENTER; + + deviceIcon.setLayoutParams(centerLayoutParams); + deviceName.setLayoutParams(centerLayoutParams); + + deviceName.setOnClickListener(this); + deviceIcon.setOnClickListener(this); + setOnClickListener(this); addView(deviceIcon); addView(deviceName); @@ -359,6 +340,24 @@ public class BluetoothSettingsActivity extends BaseAppCompatActivity { deviceName.setEnabled(status); deviceIcon.setEnabled(status); } + + @Override + public void onClick(View view) { + BluetoothDevice device = foundDevices.get(getDeviceAddress()); + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + + prefs.edit() + .putString(PREFERENCE_KEY_BLUETOOTH_HW_ADDRESS, device.getAddress()) + .putString(PREFERENCE_KEY_BLUETOOTH_DEVICE_NAME, device.getName()) + .apply(); + + Timber.d("Saved Bluetooth device " + device.getName() + " with address " + device.getAddress()); + + stopBluetoothDiscovery(); + setResult(GET_SCALE_REQUEST); + finish(); + } } @Override diff --git a/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothPreferences.java b/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothPreferences.java index bfc8bb1e..d41e1048 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothPreferences.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothPreferences.java @@ -15,51 +15,21 @@ */ package com.health.openscale.gui.preferences; -import android.app.Activity; -import android.app.AlertDialog; -import android.app.Fragment; -import android.bluetooth.BluetoothDevice; -import android.bluetooth.le.ScanResult; -import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; -import android.content.pm.PackageManager; -import android.graphics.PorterDuff; -import android.net.Uri; import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; -import android.os.Message; import android.preference.Preference; import android.preference.PreferenceFragment; -import android.preference.PreferenceScreen; import android.widget.BaseAdapter; -import android.widget.Toast; import com.health.openscale.R; -import com.health.openscale.core.OpenScale; -import com.health.openscale.core.bluetooth.BluetoothCommunication; -import com.health.openscale.core.bluetooth.BluetoothFactory; import com.health.openscale.gui.activities.BluetoothSettingsActivity; -import com.health.openscale.gui.activities.UserSettingsActivity; -import com.health.openscale.gui.utils.ColorUtil; -import com.health.openscale.gui.utils.PermissionHelper; -import com.welie.blessed.BluetoothCentral; -import com.welie.blessed.BluetoothCentralCallback; -import com.welie.blessed.BluetoothPeripheral; - -import java.util.HashMap; -import java.util.Map; - -import timber.log.Timber; - -import static android.app.Activity.RESULT_OK; public class BluetoothPreferences extends PreferenceFragment { private static final String PREFERENCE_KEY_BLUETOOTH_SCANNER = "btScanner"; - private PreferenceScreen btScanner; + private Preference btScanner; private static final String formatDeviceName(String name, String address) { if (name.isEmpty() || address.isEmpty()) { @@ -87,7 +57,7 @@ public class BluetoothPreferences extends PreferenceFragment { addPreferencesFromResource(R.xml.bluetooth_preferences); - btScanner = (PreferenceScreen) findPreference(PREFERENCE_KEY_BLUETOOTH_SCANNER); + btScanner = (Preference) findPreference(PREFERENCE_KEY_BLUETOOTH_SCANNER); btScanner.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override @@ -99,16 +69,12 @@ public class BluetoothPreferences extends PreferenceFragment { }); updateBtScannerSummary(); - - // Dummy preference to make screen open - btScanner.addPreference(new Preference(getActivity())); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { - if (resultCode != RESULT_OK) { - return; - } + super.onActivityResult(requestCode, resultCode, data); + if (requestCode == BluetoothSettingsActivity.GET_SCALE_REQUEST) { updateBtScannerSummary(); } diff --git a/android_app/app/src/main/java/com/health/openscale/gui/slides/BluetoothIntroSlide.java b/android_app/app/src/main/java/com/health/openscale/gui/slides/BluetoothIntroSlide.java index fd730826..e09ec3a2 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/slides/BluetoothIntroSlide.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/slides/BluetoothIntroSlide.java @@ -31,8 +31,6 @@ import androidx.fragment.app.Fragment; import com.health.openscale.R; import com.health.openscale.gui.activities.BluetoothSettingsActivity; -import static android.app.Activity.RESULT_OK; - public class BluetoothIntroSlide extends Fragment { private static final String ARG_LAYOUT_RES_ID = "layoutResId"; private int layoutResId; @@ -82,9 +80,6 @@ public class BluetoothIntroSlide extends Fragment { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { - if (resultCode != RESULT_OK) { - return; - } if (requestCode == BluetoothSettingsActivity.GET_SCALE_REQUEST) { txtFoundDevice.setText(getCurrentDeviceName()); } diff --git a/android_app/app/src/main/res/layout/activity_bluetoothsettings.xml b/android_app/app/src/main/res/layout/activity_bluetoothsettings.xml index 71ed84bb..17299886 100644 --- a/android_app/app/src/main/res/layout/activity_bluetoothsettings.xml +++ b/android_app/app/src/main/res/layout/activity_bluetoothsettings.xml @@ -30,7 +30,8 @@ style="?android:attr/progressBarStyle" android:layout_width="match_parent" android:layout_height="wrap_content" - android:visibility="gone"/> + android:layout_margin="20px" + android:visibility="gone" /> - + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:background="@android:color/white"> - - - - - - - + android:gravity="center_horizontal" + android:orientation="vertical"> - - + android:layout_gravity="center" + android:layout_marginTop="30dp" + android:layout_marginBottom="30dp" + android:orientation="horizontal"> + android:id="@+id/imgLogo" + android:layout_width="40dp" + android:layout_height="40dp" + app:srcCompat="@drawable/ic_launcher_openscale" /> - - - - - -