1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-13 13:39:26 +01:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractBTDeviceSupport.java

114 lines
3.2 KiB
Java

package nodomain.freeyourgadget.gadgetbridge;
import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceProtocol;
public abstract class AbstractBTDeviceSupport extends AbstractDeviceSupport {
private GBDeviceProtocol gbDeviceProtocol;
private GBDeviceIoThread gbDeviceIOThread;
protected abstract GBDeviceProtocol createDeviceProtocol();
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;
}
}
@Override
public void pair() {
// Default implementation does no manual pairing, use the Android
// pairing dialog instead.
}
public synchronized GBDeviceProtocol getDeviceProtocol() {
if (gbDeviceProtocol == null) {
gbDeviceProtocol = createDeviceProtocol();
}
return gbDeviceProtocol;
}
public synchronized GBDeviceIoThread getDeviceIOThread() {
if (gbDeviceIOThread == null) {
gbDeviceIOThread = createDeviceIOThread();
}
return gbDeviceIOThread;
}
protected void sendToDevice(byte[] bytes) {
if (bytes != null && gbDeviceIOThread != null) {
gbDeviceIOThread.write(bytes);
}
}
@Override
public void onSMS(String from, String body) {
byte[] bytes = gbDeviceProtocol.encodeSMS(from, body);
sendToDevice(bytes);
}
@Override
public void onEmail(String from, String subject, String body) {
byte[] bytes = gbDeviceProtocol.encodeEmail(from, subject, body);
sendToDevice(bytes);
}
@Override
public void onSetTime(long ts) {
byte[] bytes = gbDeviceProtocol.encodeSetTime(ts);
sendToDevice(bytes);
}
@Override
public void onSetCallState(String number, String name, GBCommand command) {
byte[] bytes = gbDeviceProtocol.encodeSetCallState(number, name, command);
sendToDevice(bytes);
}
@Override
public void onSetMusicInfo(String artist, String album, String track) {
byte[] bytes = gbDeviceProtocol.encodeSetMusicInfo(artist, album, track);
sendToDevice(bytes);
}
@Override
public void onFirmwareVersionReq() {
byte[] bytes = gbDeviceProtocol.encodeFirmwareVersionReq();
sendToDevice(bytes);
}
@Override
public void onBatteryInfoReq() {
byte[] bytes = gbDeviceProtocol.encodeBatteryInfoReq();
sendToDevice(bytes);
}
@Override
public void onAppInfoReq() {
byte[] bytes = gbDeviceProtocol.encodeAppInfoReq();
sendToDevice(bytes);
}
@Override
public void onAppDelete(int id, int index) {
byte[] bytes = gbDeviceProtocol.encodeAppDelete(id, index);
sendToDevice(bytes);
}
@Override
public void onPhoneVersion(byte os) {
byte[] bytes = gbDeviceProtocol.encodePhoneVersion(os);
sendToDevice(bytes);
}
}