mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-02-10 15:57:00 +01:00
multi-device: refactoring
This commit is contained in:
parent
7ad08d12a5
commit
73e9ff785b
@ -401,20 +401,24 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||||||
autoReconnect = getGBPrefs().getAutoReconnect();
|
autoReconnect = getGBPrefs().getAutoReconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
DeviceStruct struct = null;
|
DeviceStruct registeredStruct = getDeviceStructOrNull(gbDevice);
|
||||||
try{
|
if(registeredStruct != null){
|
||||||
struct = getDeviceStruct(gbDevice);
|
boolean deviceAlreadyConnected = isDeviceConnecting(registeredStruct.getDevice()) || isDeviceConnected(registeredStruct.getDevice());
|
||||||
}catch (DeviceNotFoundException e){
|
if(deviceAlreadyConnected){
|
||||||
// e.printStackTrace();
|
break;
|
||||||
}
|
}
|
||||||
if(struct != null){
|
try {
|
||||||
if(!isDeviceConnecting(struct.getDevice()) && !isDeviceConnected(struct.getDevice())){
|
removeDeviceSupport(gbDevice);
|
||||||
setDeviceSupport(gbDevice, null);
|
} catch (DeviceNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
struct = new DeviceStruct();
|
registeredStruct = new DeviceStruct();
|
||||||
struct.setDevice(gbDevice);
|
registeredStruct.setDevice(gbDevice);
|
||||||
|
registeredStruct.setCoordinator(DeviceHelper.getInstance().getCoordinator(gbDevice));
|
||||||
|
deviceStructs.add(registeredStruct);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
DeviceSupport deviceSupport = mFactory.createDeviceSupport(gbDevice);
|
DeviceSupport deviceSupport = mFactory.createDeviceSupport(gbDevice);
|
||||||
if (deviceSupport != null) {
|
if (deviceSupport != null) {
|
||||||
@ -430,7 +434,6 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
GB.toast(this, getString(R.string.cannot_connect, e.getMessage()), Toast.LENGTH_SHORT, GB.ERROR, e);
|
GB.toast(this, getString(R.string.cannot_connect, e.getMessage()), Toast.LENGTH_SHORT, GB.ERROR, e);
|
||||||
setDeviceSupport(gbDevice, null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for(DeviceStruct struct2 : deviceStructs){
|
for(DeviceStruct struct2 : deviceStructs){
|
||||||
@ -738,26 +741,31 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||||||
*
|
*
|
||||||
* @param deviceSupport deviceSupport to reokace/add
|
* @param deviceSupport deviceSupport to reokace/add
|
||||||
*/
|
*/
|
||||||
private void setDeviceSupport(GBDevice device, @Nullable DeviceSupport deviceSupport) {
|
private void setDeviceSupport(GBDevice device, DeviceSupport deviceSupport) throws DeviceNotFoundException {
|
||||||
try{
|
|
||||||
DeviceStruct deviceStruct = getDeviceStruct(device);
|
DeviceStruct deviceStruct = getDeviceStruct(device);
|
||||||
DeviceSupport mDeviceSupport = deviceStruct.getDeviceSupport();
|
DeviceSupport cachedDeviceSupport = deviceStruct.getDeviceSupport();
|
||||||
if (deviceSupport != mDeviceSupport && mDeviceSupport != null) {
|
if (deviceSupport != cachedDeviceSupport && cachedDeviceSupport != null) {
|
||||||
mDeviceSupport.dispose();
|
cachedDeviceSupport.dispose();
|
||||||
}
|
}
|
||||||
deviceStruct.setDeviceSupport(deviceSupport);
|
deviceStruct.setDeviceSupport(deviceSupport);
|
||||||
deviceStruct.setCoordinator(DeviceHelper.getInstance().getCoordinator(device));
|
|
||||||
return;
|
|
||||||
}catch (DeviceNotFoundException e){
|
|
||||||
// device was not connected yet
|
|
||||||
}
|
}
|
||||||
// TODO: something to do if device was not connected yet, but deviceSupport is null
|
|
||||||
DeviceStruct struct = new DeviceStruct();
|
|
||||||
struct.setDevice(device);
|
|
||||||
struct.setDeviceSupport(deviceSupport);
|
|
||||||
struct.setCoordinator(DeviceHelper.getInstance().getCoordinator(device));
|
|
||||||
|
|
||||||
deviceStructs.add(struct);
|
private void removeDeviceSupport(GBDevice device) throws DeviceNotFoundException {
|
||||||
|
DeviceStruct struct = getDeviceStruct(device);
|
||||||
|
if(struct.getDeviceSupport() != null){
|
||||||
|
struct.getDeviceSupport().dispose();
|
||||||
|
}
|
||||||
|
struct.setDeviceSupport(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private DeviceStruct getDeviceStructOrNull(GBDevice device){
|
||||||
|
DeviceStruct deviceStruct = null;
|
||||||
|
try {
|
||||||
|
deviceStruct = getDeviceStruct(device);
|
||||||
|
} catch (DeviceNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return deviceStruct;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DeviceStruct getDeviceStruct(GBDevice device) throws DeviceNotFoundException {
|
public DeviceStruct getDeviceStruct(GBDevice device) throws DeviceNotFoundException {
|
||||||
@ -1039,7 +1047,11 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||||||
setReceiversEnableState(false, false, null); // disable BroadcastReceivers
|
setReceiversEnableState(false, false, null); // disable BroadcastReceivers
|
||||||
|
|
||||||
for(GBDevice device : getGBDevices()){
|
for(GBDevice device : getGBDevices()){
|
||||||
setDeviceSupport(device, null);
|
try {
|
||||||
|
removeDeviceSupport(device);
|
||||||
|
} catch (DeviceNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
GB.removeNotification(GB.NOTIFICATION_ID, this); // need to do this because the updated notification won't be cancelled when service stops
|
GB.removeNotification(GB.NOTIFICATION_ID, this); // need to do this because the updated notification won't be cancelled when service stops
|
||||||
}
|
}
|
||||||
|
@ -143,7 +143,6 @@ public class GB {
|
|||||||
public static Notification createNotification(List<GBDevice> devices, Context context) {
|
public static Notification createNotification(List<GBDevice> devices, Context context) {
|
||||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
|
||||||
if(devices.size() == 0){
|
if(devices.size() == 0){
|
||||||
// TODO: extract
|
|
||||||
builder.setContentTitle(context.getString(R.string.info_no_devices_connected))
|
builder.setContentTitle(context.getString(R.string.info_no_devices_connected))
|
||||||
.setSmallIcon(R.drawable.ic_notification_disconnected)
|
.setSmallIcon(R.drawable.ic_notification_disconnected)
|
||||||
.setContentIntent(getContentIntent(context))
|
.setContentIntent(getContentIntent(context))
|
||||||
|
Loading…
Reference in New Issue
Block a user