From c23810f54c2f7f877d006eed43fab546ce046a32 Mon Sep 17 00:00:00 2001 From: vanous Date: Thu, 23 Dec 2021 22:41:50 +0100 Subject: [PATCH] Provide Add test device directly from the Discovery screen --- .../activities/DebugActivity.java | 34 ++++---- .../activities/DiscoveryActivity.java | 78 +++++++++++++++++-- app/src/main/res/values/strings.xml | 3 +- 3 files changed, 94 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java index 476931254..a03b8e9e2 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java @@ -70,6 +70,7 @@ import java.util.Locale; import java.util.Map; import java.util.Objects; import java.util.Random; +import java.util.TreeMap; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; @@ -125,7 +126,7 @@ public class DebugActivity extends AbstractGBActivity { }; private Spinner sendTypeSpinner; private EditText editContent; - private static long SELECT_DEVICE = 999L; + public static final long SELECT_DEVICE = 999L; private long selectedTestDeviceKey = SELECT_DEVICE; private String selectedTestDeviceMAC; @@ -474,12 +475,12 @@ public class DebugActivity extends AbstractGBActivity { new AlertDialog.Builder(DebugActivity.this) .setCancelable(true) - .setTitle("Add test device") + .setTitle(R.string.add_test_device) .setView(linearLayout) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - createTestDevice(); + createTestDevice(DebugActivity.this, selectedTestDeviceKey, selectedTestDeviceMAC); } }) .setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() { @@ -646,25 +647,25 @@ public class DebugActivity extends AbstractGBActivity { spinner.setOnItemSelectedListener(new CustomOnDeviceSelectedListener()); } - private void createTestDevice() { - if (selectedTestDeviceKey == SELECT_DEVICE) { + protected static void createTestDevice(Context context, long deviceKey, String deviceMac) { + if (deviceKey == SELECT_DEVICE) { return; } - DeviceType deviceType = DeviceType.fromKey((int) selectedTestDeviceKey); + DeviceType deviceType = DeviceType.fromKey((int) deviceKey); try ( - DBHandler db = GBApplication.acquireDB()) { + DBHandler db = GBApplication.acquireDB()) { DaoSession daoSession = db.getDaoSession(); - GBDevice gbDevice = new GBDevice(selectedTestDeviceMAC, deviceType.name(), "", deviceType); - gbDevice.setFirmwareVersion("V1.0"); - gbDevice.setFirmwareVersion2("V1.0"); + GBDevice gbDevice = new GBDevice(deviceMac, deviceType.name(), "", deviceType); + gbDevice.setFirmwareVersion("N/A"); + gbDevice.setFirmwareVersion2("N/A"); //this causes the attributes (fw version) to be stored as well. Not much useful, but still... gbDevice.setState(GBDevice.State.INITIALIZED); - Device device = DBHelper.getDevice(gbDevice, daoSession); + Device device = DBHelper.getDevice(gbDevice, daoSession); //the addition happens here Intent refreshIntent = new Intent(DeviceManager.ACTION_REFRESH_DEVICELIST); - LocalBroadcastManager.getInstance(DebugActivity.this).sendBroadcast(refreshIntent); - GB.toast(DebugActivity.this, "Added test device: " + deviceType.name(), Toast.LENGTH_SHORT, GB.INFO); + LocalBroadcastManager.getInstance(context).sendBroadcast(refreshIntent); + GB.toast(context, "Added test device: " + deviceType.name(), Toast.LENGTH_SHORT, GB.INFO); } catch ( Exception e) { @@ -686,9 +687,8 @@ public class DebugActivity extends AbstractGBActivity { return TextUtils.join(separator, mac).toUpperCase(Locale.ROOT); } - private LinkedHashMap getAllSupportedDevices(Context appContext) { + public static LinkedHashMap getAllSupportedDevices(Context appContext) { LinkedHashMap> newMap = new LinkedHashMap<>(1); - newMap.put("Select device", new Pair(SELECT_DEVICE, R.drawable.ic_create)); GBApplication app = (GBApplication) appContext; for (DeviceCoordinator coordinator : DeviceHelper.getInstance().getAllCoordinators()) { DeviceType deviceType = coordinator.getDeviceType(); @@ -697,6 +697,10 @@ public class DebugActivity extends AbstractGBActivity { long deviceId = deviceType.getKey(); newMap.put(name, new Pair(deviceId, icon)); } + TreeMap > sortedMap = new TreeMap<>(newMap); + newMap = new LinkedHashMap<>(1); + newMap.put(app.getString(R.string.widget_settings_select_device_title), new Pair(SELECT_DEVICE, R.drawable.ic_device_unknown)); + newMap.putAll(sortedMap); return newMap; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java index 94a8a6658..2ba857dab 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java @@ -22,6 +22,7 @@ import static nodomain.freeyourgadget.gadgetbridge.util.GB.toast; import android.Manifest; import android.app.Activity; +import android.app.AlertDialog; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothManager; @@ -35,6 +36,7 @@ import android.content.BroadcastReceiver; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; @@ -48,11 +50,14 @@ import android.os.ParcelUuid; import android.os.Parcelable; import android.provider.Settings; import android.text.TextUtils; +import android.util.Pair; import android.view.View; import android.widget.AdapterView; import android.widget.Button; +import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ProgressBar; +import android.widget.Spinner; import android.widget.Toast; import androidx.annotation.NonNull; @@ -63,14 +68,18 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.Objects; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsActivity; import nodomain.freeyourgadget.gadgetbridge.adapter.DeviceCandidateAdapter; +import nodomain.freeyourgadget.gadgetbridge.adapter.SpinnerWithIconAdapter; +import nodomain.freeyourgadget.gadgetbridge.adapter.SpinnerWithIconItem; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; @@ -112,6 +121,7 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView private BluetoothAdapter adapter; private Button startButton; private Scanning isScanning = Scanning.SCANNING_OFF; + private long selectedUnsupportedDeviceKey = DebugActivity.SELECT_DEVICE; private final Runnable stopRunnable = new Runnable() { @Override public void run() { @@ -783,17 +793,57 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView @Override public boolean onItemLongClick(AdapterView adapterView, View view, int position, long id) { - GBDeviceCandidate deviceCandidate = deviceCandidates.get(position); + stopDiscovery(); + final GBDeviceCandidate deviceCandidate = deviceCandidates.get(position); if (deviceCandidate == null) { LOG.error("Device candidate clicked, but item not found"); return true; } if (!deviceCandidate.getDeviceType().isSupported()) { LOG.error("Unsupported device candidate"); - ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); - ClipData clip = ClipData.newPlainText(deviceCandidate.getName(), deviceCandidate.getMacAddress()); - clipboard.setPrimaryClip(clip); - toast(this, "Bluetooth address copied to clipboard", Toast.LENGTH_SHORT, GB.INFO); + LinkedHashMap> allDevices; + allDevices = DebugActivity.getAllSupportedDevices(getApplicationContext()); + + final LinearLayout linearLayout = new LinearLayout(DiscoveryActivity.this); + linearLayout.setOrientation(LinearLayout.VERTICAL); + + final LinearLayout macLayout = new LinearLayout(DiscoveryActivity.this); + macLayout.setOrientation(LinearLayout.HORIZONTAL); + macLayout.setPadding(20, 0, 20, 0); + + final Spinner deviceListSpinner = new Spinner(DiscoveryActivity.this); + ArrayList deviceListArray = new ArrayList<>(); + for (Map.Entry> item : allDevices.entrySet()) { + deviceListArray.add(new SpinnerWithIconItem(item.getKey(), item.getValue().first, item.getValue().second)); + } + final SpinnerWithIconAdapter deviceListAdapter = new SpinnerWithIconAdapter(DiscoveryActivity.this, + R.layout.spinner_with_image_layout, R.id.spinner_item_text, deviceListArray); + deviceListSpinner.setAdapter(deviceListAdapter); + addListenerOnSpinnerDeviceSelection(deviceListSpinner); + + linearLayout.addView(deviceListSpinner); + linearLayout.addView(macLayout); + + new AlertDialog.Builder(DiscoveryActivity.this) + .setCancelable(true) + .setTitle(R.string.add_test_device) + .setView(linearLayout) + .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + if (selectedUnsupportedDeviceKey != DebugActivity.SELECT_DEVICE) { + DebugActivity.createTestDevice(DiscoveryActivity.this, selectedUnsupportedDeviceKey, deviceCandidate.getMacAddress()); + finish(); + } + } + }) + .setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + } + }) + .show(); + return true; } @@ -810,6 +860,24 @@ public class DiscoveryActivity extends AbstractGBActivity implements AdapterView return true; } + private void addListenerOnSpinnerDeviceSelection(Spinner spinner) { + spinner.setOnItemSelectedListener(new CustomOnDeviceSelectedListener()); + } + + public class CustomOnDeviceSelectedListener implements AdapterView.OnItemSelectedListener { + + public void onItemSelected(AdapterView parent, View view, int pos, long id) { + SpinnerWithIconItem selectedItem = (SpinnerWithIconItem) parent.getItemAtPosition(pos); + selectedUnsupportedDeviceKey = selectedItem.getId(); + } + + @Override + public void onNothingSelected(AdapterView arg0) { + // TODO Auto-generated method stub + } + + } + public void onBondingComplete(boolean success) { finish(); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a2b39ad8a..2326836ad 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -864,6 +864,7 @@ Watchface Unknown Device Test Device + Add test device Pebble Mi Band Mi Band 2 @@ -1102,7 +1103,7 @@ Ignore bonded devices Enabling this option will ignore devices that have been bonded/paired already when scanning Discover unsupported devices - Enabling this option will display all discovered bluetooth devices when scanning. They cannot be used in Gadgetbridge. + Enabling this option will display all discovered bluetooth devices when scanning. Short tap will copy device name and mac address to clipboard. Long press will launch `Add test device` dialog. Location must be turned on to scan for devices Sony SWR12 Settings Low vibration enabled