mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2025-02-24 00:11:10 +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();
|
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() {
|
public synchronized GBDeviceProtocol getDeviceProtocol() {
|
||||||
if (gbDeviceProtocol == null) {
|
if (gbDeviceProtocol == null) {
|
||||||
gbDeviceProtocol = createDeviceProtocol();
|
gbDeviceProtocol = createDeviceProtocol();
|
||||||
@ -26,7 +40,7 @@ public abstract class AbstractBTDeviceSupport extends AbstractDeviceSupport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void sendToDevice(byte[] bytes) {
|
protected void sendToDevice(byte[] bytes) {
|
||||||
if (bytes != null) {
|
if (bytes != null && gbDeviceIOThread != null) {
|
||||||
gbDeviceIOThread.write(bytes);
|
gbDeviceIOThread.write(bytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,15 +103,8 @@ public class BluetoothCommunicationService extends Service {
|
|||||||
sharedPrefs.edit().putString("last_device_address", btDeviceAddress).commit();
|
sharedPrefs.edit().putString("last_device_address", btDeviceAddress).commit();
|
||||||
|
|
||||||
if (btDeviceAddress != null && !isConnected() && !isConnecting()) {
|
if (btDeviceAddress != null && !isConnected() && !isConnecting()) {
|
||||||
// currently only one thread allowed
|
if (mDeviceSupport != null) {
|
||||||
if (mGBDeviceIoThread != null) {
|
mDeviceSupport.dispose();
|
||||||
mGBDeviceIoThread.quit();
|
|
||||||
try {
|
|
||||||
mGBDeviceIoThread.join();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(btDeviceAddress);
|
BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(btDeviceAddress);
|
||||||
if (btDevice != null) {
|
if (btDevice != null) {
|
||||||
@ -201,13 +194,8 @@ public class BluetoothCommunicationService extends Service {
|
|||||||
|
|
||||||
GB.setReceiversEnableState(false, this); // disable BroadcastReceivers
|
GB.setReceiversEnableState(false, this); // disable BroadcastReceivers
|
||||||
|
|
||||||
if (mGBDeviceIoThread != null) {
|
if (mDeviceSupport != null) {
|
||||||
try {
|
mDeviceSupport.dispose();
|
||||||
mGBDeviceIoThread.quit();
|
|
||||||
mGBDeviceIoThread.join();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
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
|
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 BluetoothAdapter getBluetoothAdapter();
|
||||||
|
|
||||||
public Context getContext();
|
public Context getContext();
|
||||||
|
|
||||||
|
public void dispose();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user