mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-25 03:16:51 +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.LoggerFactory;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
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.util.DeviceHelper;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.WebViewSingleton;
|
||||
|
||||
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_CONNECT;
|
||||
@ -91,28 +94,22 @@ public class ExternalPebbleJSActivity extends AbstractGBActivity {
|
||||
DeviceManager deviceManager = ((GBApplication) getApplication()).getDeviceManager();
|
||||
List<GBDevice> deviceList = deviceManager.getDevices();
|
||||
for (GBDevice device : deviceList) {
|
||||
if (device.getState() == GBDevice.State.INITIALIZED) {
|
||||
if (device.getType().equals(DeviceType.PEBBLE)) {
|
||||
currentDevice = device;
|
||||
break;
|
||||
} else {
|
||||
LOG.error("attempting to load pebble configuration but a different device type is connected!!!");
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
if (device.getState() == GBDevice.State.INITIALIZED && device.getType().equals(DeviceType.PEBBLE)) {
|
||||
currentDevice = device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (currentDevice == null) {
|
||||
//then try to reconnect to last connected device
|
||||
String btDeviceAddress = GBApplication.getPrefs().getPreferences().getString("last_device_address", null);
|
||||
if (btDeviceAddress != null) {
|
||||
GBDevice candidate = DeviceHelper.getInstance().findAvailableDevice(btDeviceAddress, this);
|
||||
if(!candidate.isConnected() && candidate.getType() == DeviceType.PEBBLE){
|
||||
//then try to reconnect to one of last connected Pebble devices
|
||||
Set<String> lastDeviceAddresses = GBApplication.getPrefs().getStringSet(GBPrefs.LAST_DEVICE_ADDRESSES, Collections.emptySet());
|
||||
for (GBDevice device : deviceList) {
|
||||
if (!device.isConnected() && device.getType() == DeviceType.PEBBLE && lastDeviceAddresses.contains(device.getAddress())) {
|
||||
Intent intent = new Intent(this, DeviceCommunicationService.class)
|
||||
.setAction(ACTION_CONNECT)
|
||||
.putExtra(GBDevice.EXTRA_DEVICE, currentDevice);
|
||||
.putExtra(GBDevice.EXTRA_DEVICE, device);
|
||||
this.startService(intent);
|
||||
currentDevice = candidate;
|
||||
currentDevice = device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -83,12 +83,15 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Hashtable;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
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.FormatUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
|
||||
|
||||
/**
|
||||
@ -855,6 +859,7 @@ public class GBDeviceAdapterv2 extends ListAdapter<GBDevice, GBDeviceAdapterv2.V
|
||||
showTransientSnackbar(R.string.controlcenter_snackbar_disconnecting);
|
||||
GBApplication.deviceService(device).disconnect();
|
||||
}
|
||||
removeFromLastDeviceAddressesPref(device);
|
||||
return true;
|
||||
case R.id.controlcenter_device_submenu_set_alias:
|
||||
showSetAliasDialog(device);
|
||||
@ -1061,6 +1066,15 @@ public class GBDeviceAdapterv2 extends ListAdapter<GBDevice, GBDeviceAdapterv2.V
|
||||
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) {
|
||||
Intent startIntent;
|
||||
startIntent = new Intent(context, DeviceSettingsActivity.class);
|
||||
|
@ -41,7 +41,9 @@ import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
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.TemperatureSample;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.ServiceDeviceSupport;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
||||
|
||||
public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
|
||||
@ -134,10 +137,12 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
|
||||
}
|
||||
Prefs prefs = getPrefs();
|
||||
|
||||
String lastDevice = prefs.getPreferences().getString("last_device_address", "");
|
||||
if (gbDevice.getAddress().equals(lastDevice)) {
|
||||
LOG.debug("#1605 removing last device");
|
||||
prefs.getPreferences().edit().remove("last_device_address").apply();
|
||||
Set<String> lastDeviceAddresses = prefs.getStringSet(GBPrefs.LAST_DEVICE_ADDRESSES, Collections.emptySet());
|
||||
if (lastDeviceAddresses.contains(gbDevice.getAddress())) {
|
||||
LOG.debug("#1605 removing last device (one of last devices)");
|
||||
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, "");
|
||||
|
@ -48,8 +48,11 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
@ -484,6 +487,17 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
||||
gbDevs = new ArrayList<>();
|
||||
gbDevs.add(extraDevice);
|
||||
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 {
|
||||
gbDevs = GBApplication.app().getDeviceManager().getDevices();
|
||||
}
|
||||
@ -501,7 +515,12 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
||||
if(!fromExtra && !autoReconnect) {
|
||||
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) {
|
||||
|
@ -49,6 +49,7 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
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.DeviceService;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
||||
|
||||
import static nodomain.freeyourgadget.gadgetbridge.GBApplication.isRunningOreoOrLater;
|
||||
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));
|
||||
}
|
||||
|
||||
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);
|
||||
deviceCommunicationServiceIntent.setAction(DeviceService.ACTION_CONNECT);
|
||||
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_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;
|
||||
|
||||
public GBPrefs(Prefs prefs) {
|
||||
|
@ -204,6 +204,8 @@
|
||||
<string name="pref_header_workout">Workout</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_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_autoreconnect">Reconnect automatically</string>
|
||||
<string name="pref_title_mb_intents">Broadcast Media Button Intents Directly</string>
|
||||
|
@ -18,6 +18,13 @@
|
||||
android:layout="@layout/preference_checkbox"
|
||||
android:title="@string/pref_title_general_autoconnectonbluetooth"
|
||||
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
|
||||
android:defaultValue="false"
|
||||
android:enabled="false"
|
||||
|
Loading…
Reference in New Issue
Block a user