2017-03-10 14:53:19 +01:00
|
|
|
/* Copyright (C) 2016-2017 Andreas Shimokawa, Carsten Pfeiffer
|
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2016-06-18 23:35:34 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.devices;
|
|
|
|
|
|
|
|
import android.bluetooth.BluetoothDevice;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
2016-07-05 21:48:10 +02:00
|
|
|
import android.support.annotation.Nullable;
|
2016-06-18 23:35:34 +02:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2016-09-29 22:56:16 +02:00
|
|
|
import java.text.Collator;
|
2016-06-18 23:35:34 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
2016-09-29 22:56:16 +02:00
|
|
|
import java.util.Comparator;
|
2016-06-18 23:35:34 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2017-04-07 21:17:37 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
2016-06-18 23:35:34 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides access to the list of devices managed by Gadgetbridge.
|
|
|
|
* Changes to the devices (e.g. connection state) or the list of devices
|
|
|
|
* are broadcasted via #ACTION_DEVICE_CHANGED
|
|
|
|
*/
|
|
|
|
public class DeviceManager {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(DeviceManager.class);
|
|
|
|
|
2016-07-11 00:28:15 +02:00
|
|
|
public static final String BLUETOOTH_DEVICE_ACTION_ALIAS_CHANGED = "android.bluetooth.device.action.ALIAS_CHANGED";
|
2016-06-18 23:35:34 +02:00
|
|
|
/**
|
|
|
|
* Intent action to notify that the list of devices has changed.
|
|
|
|
*/
|
|
|
|
public static final String ACTION_DEVICES_CHANGED
|
|
|
|
= "nodomain.freeyourgadget.gadgetbridge.devices.devicemanager.action.devices_changed";
|
|
|
|
/**
|
|
|
|
* Intent action to notify this class that the list of devices shall be refreshed.
|
|
|
|
*/
|
|
|
|
public static final String ACTION_REFRESH_DEVICELIST
|
|
|
|
= "nodomain.freeyourgadget.gadgetbridge.devices.devicemanager.action.set_version";
|
|
|
|
private final Context context;
|
|
|
|
/**
|
|
|
|
* This list is final, it will never be recreated. Only its contents change.
|
|
|
|
* This allows direct access to the list from ListAdapters.
|
|
|
|
*/
|
|
|
|
private final List<GBDevice> deviceList = new ArrayList<>();
|
|
|
|
private GBDevice selectedDevice = null;
|
|
|
|
|
|
|
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
switch (action) {
|
2016-07-11 00:28:15 +02:00
|
|
|
case ACTION_REFRESH_DEVICELIST: // fall through
|
2016-06-18 23:35:34 +02:00
|
|
|
case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
|
|
|
|
refreshPairedDevices();
|
|
|
|
break;
|
2016-07-11 00:28:15 +02:00
|
|
|
case BluetoothDevice.ACTION_NAME_CHANGED:
|
|
|
|
case BLUETOOTH_DEVICE_ACTION_ALIAS_CHANGED:
|
|
|
|
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
|
|
|
String newName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
|
|
|
|
updateDeviceName(device, newName);
|
|
|
|
break;
|
2016-06-18 23:35:34 +02:00
|
|
|
case GBDevice.ACTION_DEVICE_CHANGED:
|
|
|
|
GBDevice dev = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
|
|
|
if (dev.getAddress() != null) {
|
|
|
|
int index = deviceList.indexOf(dev); // search by address
|
|
|
|
if (index >= 0) {
|
|
|
|
deviceList.set(index, dev);
|
|
|
|
} else {
|
|
|
|
deviceList.add(dev);
|
|
|
|
}
|
2017-04-07 21:17:37 +02:00
|
|
|
if (dev.isInitialized()) {
|
|
|
|
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
|
|
|
DBHelper.getDevice(dev, dbHandler.getDaoSession()); // implicitly creates the device in database if not present, and updates device attributes
|
|
|
|
} catch (Exception ignore) {
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 23:35:34 +02:00
|
|
|
}
|
|
|
|
updateSelectedDevice(dev);
|
|
|
|
refreshPairedDevices();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public DeviceManager(Context context) {
|
|
|
|
this.context = context;
|
|
|
|
IntentFilter filterLocal = new IntentFilter();
|
|
|
|
filterLocal.addAction(DeviceManager.ACTION_REFRESH_DEVICELIST);
|
|
|
|
filterLocal.addAction(GBDevice.ACTION_DEVICE_CHANGED);
|
|
|
|
filterLocal.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
|
|
|
|
LocalBroadcastManager.getInstance(context).registerReceiver(mReceiver, filterLocal);
|
2016-07-11 00:28:15 +02:00
|
|
|
|
|
|
|
IntentFilter filterGlobal = new IntentFilter();
|
|
|
|
filterGlobal.addAction(BluetoothDevice.ACTION_NAME_CHANGED);
|
|
|
|
filterGlobal.addAction(BLUETOOTH_DEVICE_ACTION_ALIAS_CHANGED);
|
|
|
|
filterGlobal.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
|
|
|
|
context.registerReceiver(mReceiver, filterGlobal);
|
2016-06-18 23:35:34 +02:00
|
|
|
|
|
|
|
refreshPairedDevices();
|
|
|
|
}
|
|
|
|
|
2016-07-11 00:28:15 +02:00
|
|
|
private void updateDeviceName(BluetoothDevice device, String newName) {
|
|
|
|
for (GBDevice dev : deviceList) {
|
|
|
|
if (device.getAddress().equals(dev.getAddress())) {
|
|
|
|
if (!dev.getName().equals(newName)) {
|
|
|
|
dev.setName(newName);
|
|
|
|
notifyDevicesChanged();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-18 23:35:34 +02:00
|
|
|
private void updateSelectedDevice(GBDevice dev) {
|
|
|
|
if (selectedDevice == null) {
|
|
|
|
selectedDevice = dev;
|
|
|
|
} else {
|
2016-07-05 21:48:10 +02:00
|
|
|
if (selectedDevice.equals(dev)) {
|
|
|
|
selectedDevice = dev; // equality vs identity!
|
|
|
|
} else {
|
2016-06-18 23:35:34 +02:00
|
|
|
if (selectedDevice.isConnected() && dev.isConnected()) {
|
|
|
|
LOG.warn("multiple connected devices -- this is currently not really supported");
|
|
|
|
selectedDevice = dev; // use the last one that changed
|
2016-07-05 21:48:10 +02:00
|
|
|
} else if (!selectedDevice.isConnected()) {
|
2016-06-18 23:35:34 +02:00
|
|
|
selectedDevice = dev; // use the last one that changed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void refreshPairedDevices() {
|
|
|
|
Set<GBDevice> availableDevices = DeviceHelper.getInstance().getAvailableDevices(context);
|
|
|
|
deviceList.retainAll(availableDevices);
|
|
|
|
for (GBDevice availableDevice : availableDevices) {
|
|
|
|
if (!deviceList.contains(availableDevice)) {
|
|
|
|
deviceList.add(availableDevice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 22:56:16 +02:00
|
|
|
Collections.sort(deviceList, new Comparator<GBDevice>() {
|
|
|
|
@Override
|
|
|
|
public int compare(GBDevice lhs, GBDevice rhs) {
|
|
|
|
return Collator.getInstance().compare(lhs.getName(), rhs.getName());
|
|
|
|
}
|
|
|
|
});
|
2016-06-18 23:35:34 +02:00
|
|
|
notifyDevicesChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The returned list is final, it will never be recreated. Only its contents change.
|
|
|
|
* This allows direct access to the list from ListAdapters.
|
|
|
|
*/
|
|
|
|
public List<GBDevice> getDevices() {
|
|
|
|
return Collections.unmodifiableList(deviceList);
|
|
|
|
}
|
|
|
|
|
2016-07-05 21:48:10 +02:00
|
|
|
@Nullable
|
2016-06-18 23:35:34 +02:00
|
|
|
public GBDevice getSelectedDevice() {
|
|
|
|
return selectedDevice;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void notifyDevicesChanged() {
|
|
|
|
LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(ACTION_DEVICES_CHANGED));
|
|
|
|
}
|
|
|
|
}
|