mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-28 21:06:50 +01:00
Add "Reconnect only to connected devices" setting
* Add general_reconnectonlytoconnected setting. * Replace last_device_address shared prefs string with last_device_addresses shared prefs string set. Bluetooth address of a device is added to last_device_addresses when connecting to the device. Bluetooth address of a device is removed from last_device_addresses only when deleting the device or explicitly disconnecting from the device (e.g. by selecting "Disconnect" in the device tile menu). * Adjust ExternalPebbleJSActivity to better support multiple connected devices.
This commit is contained in:
parent
904a7807a8
commit
9e10da062e
@ -35,8 +35,10 @@ import androidx.core.app.NavUtils;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||||
@ -51,6 +53,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.webview.GBWeb
|
|||||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.webview.JSInterface;
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.webview.JSInterface;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.WebViewSingleton;
|
import nodomain.freeyourgadget.gadgetbridge.util.WebViewSingleton;
|
||||||
|
|
||||||
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_CONNECT;
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_CONNECT;
|
||||||
@ -91,28 +94,22 @@ public class ExternalPebbleJSActivity extends AbstractGBActivity {
|
|||||||
DeviceManager deviceManager = ((GBApplication) getApplication()).getDeviceManager();
|
DeviceManager deviceManager = ((GBApplication) getApplication()).getDeviceManager();
|
||||||
List<GBDevice> deviceList = deviceManager.getDevices();
|
List<GBDevice> deviceList = deviceManager.getDevices();
|
||||||
for (GBDevice device : deviceList) {
|
for (GBDevice device : deviceList) {
|
||||||
if (device.getState() == GBDevice.State.INITIALIZED) {
|
if (device.getState() == GBDevice.State.INITIALIZED && device.getType().equals(DeviceType.PEBBLE)) {
|
||||||
if (device.getType().equals(DeviceType.PEBBLE)) {
|
currentDevice = device;
|
||||||
currentDevice = device;
|
break;
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
LOG.error("attempting to load pebble configuration but a different device type is connected!!!");
|
|
||||||
finish();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (currentDevice == null) {
|
if (currentDevice == null) {
|
||||||
//then try to reconnect to last connected device
|
//then try to reconnect to one of last connected Pebble devices
|
||||||
String btDeviceAddress = GBApplication.getPrefs().getPreferences().getString("last_device_address", null);
|
Set<String> lastDeviceAddresses = GBApplication.getPrefs().getStringSet(GBPrefs.LAST_DEVICE_ADDRESSES, Collections.emptySet());
|
||||||
if (btDeviceAddress != null) {
|
for (GBDevice device : deviceList) {
|
||||||
GBDevice candidate = DeviceHelper.getInstance().findAvailableDevice(btDeviceAddress, this);
|
if (!device.isConnected() && device.getType() == DeviceType.PEBBLE && lastDeviceAddresses.contains(device.getAddress())) {
|
||||||
if(!candidate.isConnected() && candidate.getType() == DeviceType.PEBBLE){
|
|
||||||
Intent intent = new Intent(this, DeviceCommunicationService.class)
|
Intent intent = new Intent(this, DeviceCommunicationService.class)
|
||||||
.setAction(ACTION_CONNECT)
|
.setAction(ACTION_CONNECT)
|
||||||
.putExtra(GBDevice.EXTRA_DEVICE, currentDevice);
|
.putExtra(GBDevice.EXTRA_DEVICE, device);
|
||||||
this.startService(intent);
|
this.startService(intent);
|
||||||
currentDevice = candidate;
|
currentDevice = device;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,12 +83,15 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||||
@ -121,6 +124,7 @@ import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
|||||||
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.FormatUtils;
|
import nodomain.freeyourgadget.gadgetbridge.util.FormatUtils;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
|
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -855,6 +859,7 @@ public class GBDeviceAdapterv2 extends ListAdapter<GBDevice, GBDeviceAdapterv2.V
|
|||||||
showTransientSnackbar(R.string.controlcenter_snackbar_disconnecting);
|
showTransientSnackbar(R.string.controlcenter_snackbar_disconnecting);
|
||||||
GBApplication.deviceService(device).disconnect();
|
GBApplication.deviceService(device).disconnect();
|
||||||
}
|
}
|
||||||
|
removeFromLastDeviceAddressesPref(device);
|
||||||
return true;
|
return true;
|
||||||
case R.id.controlcenter_device_submenu_set_alias:
|
case R.id.controlcenter_device_submenu_set_alias:
|
||||||
showSetAliasDialog(device);
|
showSetAliasDialog(device);
|
||||||
@ -1061,6 +1066,15 @@ public class GBDeviceAdapterv2 extends ListAdapter<GBDevice, GBDeviceAdapterv2.V
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void removeFromLastDeviceAddressesPref(GBDevice device) {
|
||||||
|
Set<String> lastDeviceAddresses = GBApplication.getPrefs().getStringSet(GBPrefs.LAST_DEVICE_ADDRESSES, Collections.emptySet());
|
||||||
|
if (lastDeviceAddresses.contains(device.getAddress())) {
|
||||||
|
lastDeviceAddresses = new HashSet<String>(lastDeviceAddresses);
|
||||||
|
lastDeviceAddresses.remove(device.getAddress());
|
||||||
|
GBApplication.getPrefs().getPreferences().edit().putStringSet(GBPrefs.LAST_DEVICE_ADDRESSES, lastDeviceAddresses).apply();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void setAppPreferences(GBDevice device) {
|
private void setAppPreferences(GBDevice device) {
|
||||||
Intent startIntent;
|
Intent startIntent;
|
||||||
startIntent = new Intent(context, DeviceSettingsActivity.class);
|
startIntent = new Intent(context, DeviceSettingsActivity.class);
|
||||||
|
@ -41,7 +41,9 @@ import java.util.Arrays;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import de.greenrobot.dao.query.QueryBuilder;
|
import de.greenrobot.dao.query.QueryBuilder;
|
||||||
@ -74,6 +76,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.Spo2Sample;
|
|||||||
import nodomain.freeyourgadget.gadgetbridge.model.StressSample;
|
import nodomain.freeyourgadget.gadgetbridge.model.StressSample;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.TemperatureSample;
|
import nodomain.freeyourgadget.gadgetbridge.model.TemperatureSample;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.service.ServiceDeviceSupport;
|
import nodomain.freeyourgadget.gadgetbridge.service.ServiceDeviceSupport;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
||||||
|
|
||||||
public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
|
public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
|
||||||
@ -134,10 +137,12 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
|
|||||||
}
|
}
|
||||||
Prefs prefs = getPrefs();
|
Prefs prefs = getPrefs();
|
||||||
|
|
||||||
String lastDevice = prefs.getPreferences().getString("last_device_address", "");
|
Set<String> lastDeviceAddresses = prefs.getStringSet(GBPrefs.LAST_DEVICE_ADDRESSES, Collections.emptySet());
|
||||||
if (gbDevice.getAddress().equals(lastDevice)) {
|
if (lastDeviceAddresses.contains(gbDevice.getAddress())) {
|
||||||
LOG.debug("#1605 removing last device");
|
LOG.debug("#1605 removing last device (one of last devices)");
|
||||||
prefs.getPreferences().edit().remove("last_device_address").apply();
|
lastDeviceAddresses = new HashSet<String>(lastDeviceAddresses);
|
||||||
|
lastDeviceAddresses.remove(gbDevice.getAddress());
|
||||||
|
prefs.getPreferences().edit().putStringSet(GBPrefs.LAST_DEVICE_ADDRESSES, lastDeviceAddresses).apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
String macAddress = prefs.getPreferences().getString(MiBandConst.PREF_MIBAND_ADDRESS, "");
|
String macAddress = prefs.getPreferences().getString(MiBandConst.PREF_MIBAND_ADDRESS, "");
|
||||||
|
@ -48,8 +48,11 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||||
@ -484,6 +487,17 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||||||
gbDevs = new ArrayList<>();
|
gbDevs = new ArrayList<>();
|
||||||
gbDevs.add(extraDevice);
|
gbDevs.add(extraDevice);
|
||||||
fromExtra = true;
|
fromExtra = true;
|
||||||
|
} else if (prefs.getBoolean(GBPrefs.RECONNECT_ONLY_TO_CONNECTED, true)) {
|
||||||
|
List<GBDevice> gbAllDevs = GBApplication.app().getDeviceManager().getDevices();
|
||||||
|
Set<String> lastDeviceAddresses = prefs.getStringSet(GBPrefs.LAST_DEVICE_ADDRESSES, Collections.emptySet());
|
||||||
|
if (gbAllDevs != null && !gbAllDevs.isEmpty() && !lastDeviceAddresses.isEmpty()) {
|
||||||
|
gbDevs = new ArrayList<>();
|
||||||
|
for(GBDevice gbDev : gbAllDevs) {
|
||||||
|
if (lastDeviceAddresses.contains(gbDev.getAddress())) {
|
||||||
|
gbDevs.add(gbDev);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
gbDevs = GBApplication.app().getDeviceManager().getDevices();
|
gbDevs = GBApplication.app().getDeviceManager().getDevices();
|
||||||
}
|
}
|
||||||
@ -501,7 +515,12 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||||||
if(!fromExtra && !autoReconnect) {
|
if(!fromExtra && !autoReconnect) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
prefs.getPreferences().edit().putString("last_device_address", btDeviceAddress).apply();
|
Set<String> lastDeviceAddresses = prefs.getStringSet(GBPrefs.LAST_DEVICE_ADDRESSES, Collections.emptySet());
|
||||||
|
if (!lastDeviceAddresses.contains(btDeviceAddress)) {
|
||||||
|
lastDeviceAddresses = new HashSet<String>(lastDeviceAddresses);
|
||||||
|
lastDeviceAddresses.add(btDeviceAddress);
|
||||||
|
prefs.getPreferences().edit().putStringSet(GBPrefs.LAST_DEVICE_ADDRESSES, lastDeviceAddresses).apply();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!fromExtra && !autoReconnect) {
|
if(!fromExtra && !autoReconnect) {
|
||||||
|
@ -49,6 +49,7 @@ import java.io.FileOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||||
@ -61,6 +62,7 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|||||||
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.DeviceService;
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceService;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
|
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
||||||
|
|
||||||
import static nodomain.freeyourgadget.gadgetbridge.GBApplication.isRunningOreoOrLater;
|
import static nodomain.freeyourgadget.gadgetbridge.GBApplication.isRunningOreoOrLater;
|
||||||
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_RECORDED_DATA_TYPES;
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_RECORDED_DATA_TYPES;
|
||||||
@ -277,7 +279,9 @@ public class GB {
|
|||||||
builder.setColor(context.getResources().getColor(R.color.accent));
|
builder.setColor(context.getResources().getColor(R.color.accent));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (GBApplication.getPrefs().getString("last_device_address", null) != null) {
|
// A small bug: When "Reconnect only to connected devices" is disabled, the intent will be added even when there are no devices in GB
|
||||||
|
// Not sure whether it is worth the complexity to fix this
|
||||||
|
if (!GBApplication.getPrefs().getBoolean(GBPrefs.RECONNECT_ONLY_TO_CONNECTED, true) || !GBApplication.getPrefs().getStringSet(GBPrefs.LAST_DEVICE_ADDRESSES, Collections.emptySet()).isEmpty()) {
|
||||||
Intent deviceCommunicationServiceIntent = new Intent(context, DeviceCommunicationService.class);
|
Intent deviceCommunicationServiceIntent = new Intent(context, DeviceCommunicationService.class);
|
||||||
deviceCommunicationServiceIntent.setAction(DeviceService.ACTION_CONNECT);
|
deviceCommunicationServiceIntent.setAction(DeviceService.ACTION_CONNECT);
|
||||||
PendingIntent reconnectPendingIntent = PendingIntentUtils.getService(context, 2, deviceCommunicationServiceIntent, PendingIntent.FLAG_ONE_SHOT, false);
|
PendingIntent reconnectPendingIntent = PendingIntentUtils.getService(context, 2, deviceCommunicationServiceIntent, PendingIntent.FLAG_ONE_SHOT, false);
|
||||||
|
@ -65,6 +65,9 @@ public class GBPrefs {
|
|||||||
public static final String CHART_MAX_HEART_RATE = "chart_max_heart_rate";
|
public static final String CHART_MAX_HEART_RATE = "chart_max_heart_rate";
|
||||||
public static final String CHART_MIN_HEART_RATE = "chart_min_heart_rate";
|
public static final String CHART_MIN_HEART_RATE = "chart_min_heart_rate";
|
||||||
|
|
||||||
|
public static final String LAST_DEVICE_ADDRESSES = "last_device_addresses";
|
||||||
|
public static final String RECONNECT_ONLY_TO_CONNECTED = "general_reconnectonlytoconnected";
|
||||||
|
|
||||||
private final Prefs mPrefs;
|
private final Prefs mPrefs;
|
||||||
|
|
||||||
public GBPrefs(Prefs prefs) {
|
public GBPrefs(Prefs prefs) {
|
||||||
|
@ -204,6 +204,8 @@
|
|||||||
<string name="pref_header_workout">Workout</string>
|
<string name="pref_header_workout">Workout</string>
|
||||||
<string name="pref_header_equalizer">Equalizer</string>
|
<string name="pref_header_equalizer">Equalizer</string>
|
||||||
<string name="pref_title_general_autoconnectonbluetooth">Connect to Gadgetbridge device(s) when Bluetooth is turned on</string>
|
<string name="pref_title_general_autoconnectonbluetooth">Connect to Gadgetbridge device(s) when Bluetooth is turned on</string>
|
||||||
|
<string name="pref_title_general_reconnectonlytoconnected">Reconnect only to connected devices</string>
|
||||||
|
<string name="pref_summary_general_reconnectonlytoconnected">Reconnect only to connected devices, instead of reconnecting to all devices</string>
|
||||||
<string name="pref_title_general_autostartonboot">Start automatically</string>
|
<string name="pref_title_general_autostartonboot">Start automatically</string>
|
||||||
<string name="pref_title_general_autoreconnect">Reconnect automatically</string>
|
<string name="pref_title_general_autoreconnect">Reconnect automatically</string>
|
||||||
<string name="pref_title_mb_intents">Broadcast Media Button Intents Directly</string>
|
<string name="pref_title_mb_intents">Broadcast Media Button Intents Directly</string>
|
||||||
|
@ -18,6 +18,13 @@
|
|||||||
android:layout="@layout/preference_checkbox"
|
android:layout="@layout/preference_checkbox"
|
||||||
android:title="@string/pref_title_general_autoconnectonbluetooth"
|
android:title="@string/pref_title_general_autoconnectonbluetooth"
|
||||||
app:iconSpaceReserved="false" />
|
app:iconSpaceReserved="false" />
|
||||||
|
<SwitchPreferenceCompat
|
||||||
|
android:defaultValue="true"
|
||||||
|
android:key="general_reconnectonlytoconnected"
|
||||||
|
android:layout="@layout/preference_checkbox"
|
||||||
|
android:title="@string/pref_title_general_reconnectonlytoconnected"
|
||||||
|
android:summary="@string/pref_summary_general_reconnectonlytoconnected"
|
||||||
|
app:iconSpaceReserved="false" />
|
||||||
<SwitchPreferenceCompat
|
<SwitchPreferenceCompat
|
||||||
android:defaultValue="false"
|
android:defaultValue="false"
|
||||||
android:enabled="false"
|
android:enabled="false"
|
||||||
|
Loading…
Reference in New Issue
Block a user