1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-01 14:32:54 +01:00

Provide Add test device directly from the Discovery screen

This commit is contained in:
vanous 2021-12-23 22:41:50 +01:00
parent 682a26e56d
commit c23810f54c
3 changed files with 94 additions and 21 deletions

View File

@ -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()) {
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<String, Pair<Long, Integer>> 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 <String, Pair<Long, Integer>> 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;
}

View File

@ -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<String, Pair<Long, Integer>> 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<SpinnerWithIconItem> deviceListArray = new ArrayList<>();
for (Map.Entry<String, Pair<Long, Integer>> 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();
}

View File

@ -864,6 +864,7 @@
<string name="kind_watchface">Watchface</string>
<string name="devicetype_unknown">Unknown Device</string>
<string name="devicetype_test">Test Device</string>
<string name="add_test_device">Add test device</string>
<string name="devicetype_pebble">Pebble</string>
<string name="devicetype_miband">Mi Band</string>
<string name="devicetype_miband2">Mi Band 2</string>
@ -1102,7 +1103,7 @@
<string name="ignore_bonded_devices">Ignore bonded devices</string>
<string name="ignore_bonded_devices_description">Enabling this option will ignore devices that have been bonded/paired already when scanning</string>
<string name="discover_unsupported_devices">Discover unsupported devices</string>
<string name="discover_unsupported_devices_description">Enabling this option will display all discovered bluetooth devices when scanning. They cannot be used in Gadgetbridge.</string>
<string name="discover_unsupported_devices_description">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.</string>
<string name="error_location_enabled_mandatory">Location must be turned on to scan for devices</string>
<string name="sonyswr12_settings_title">Sony SWR12 Settings</string>
<string name="sonyswr12_settings_low_vibration">Low vibration enabled</string>