mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2025-02-23 16:01:13 +01:00
Also move the resource management into DeviceSupport classes
This commit is contained in:
parent
1f31c1d79c
commit
d0ff14bc0e
@ -11,6 +11,20 @@ public abstract class AbstractBTDeviceSupport extends AbstractDeviceSupport {
|
||||
|
||||
protected abstract GBDeviceIoThread createDeviceIOThread();
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
// currently only one thread allowed
|
||||
if (gbDeviceIOThread != null) {
|
||||
gbDeviceIOThread.quit();
|
||||
try {
|
||||
gbDeviceIOThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
gbDeviceIOThread = null;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized GBDeviceProtocol getDeviceProtocol() {
|
||||
if (gbDeviceProtocol == null) {
|
||||
gbDeviceProtocol = createDeviceProtocol();
|
||||
@ -26,7 +40,7 @@ public abstract class AbstractBTDeviceSupport extends AbstractDeviceSupport {
|
||||
}
|
||||
|
||||
protected void sendToDevice(byte[] bytes) {
|
||||
if (bytes != null) {
|
||||
if (bytes != null && gbDeviceIOThread != null) {
|
||||
gbDeviceIOThread.write(bytes);
|
||||
}
|
||||
}
|
||||
|
@ -103,15 +103,8 @@ public class BluetoothCommunicationService extends Service {
|
||||
sharedPrefs.edit().putString("last_device_address", btDeviceAddress).commit();
|
||||
|
||||
if (btDeviceAddress != null && !isConnected() && !isConnecting()) {
|
||||
// currently only one thread allowed
|
||||
if (mGBDeviceIoThread != null) {
|
||||
mGBDeviceIoThread.quit();
|
||||
try {
|
||||
mGBDeviceIoThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (mDeviceSupport != null) {
|
||||
mDeviceSupport.dispose();
|
||||
}
|
||||
BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(btDeviceAddress);
|
||||
if (btDevice != null) {
|
||||
@ -201,13 +194,8 @@ public class BluetoothCommunicationService extends Service {
|
||||
|
||||
GB.setReceiversEnableState(false, this); // disable BroadcastReceivers
|
||||
|
||||
if (mGBDeviceIoThread != null) {
|
||||
try {
|
||||
mGBDeviceIoThread.quit();
|
||||
mGBDeviceIoThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (mDeviceSupport != null) {
|
||||
mDeviceSupport.dispose();
|
||||
}
|
||||
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
nm.cancel(GB.NOTIFICATION_ID); // need to do this because the updated notification wont be cancelled when service stops
|
||||
|
@ -13,4 +13,6 @@ public interface DeviceSupport extends EventHandler {
|
||||
public BluetoothAdapter getBluetoothAdapter();
|
||||
|
||||
public Context getContext();
|
||||
|
||||
public void dispose();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user