1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-02 11:26:09 +02:00

add GBDeviceIoThread class for further device abstraction

Also add an empty implementation for the Miband.
Now Gadgedbridges pebbleisms should be removed.
This commit is contained in:
Andreas Shimokawa 2015-04-01 19:11:18 +02:00
parent ed2e177fcb
commit 71c201beaf

View File

@ -68,7 +68,7 @@ public class BluetoothCommunicationService extends Service {
private static final int NOTIFICATION_ID = 1; private static final int NOTIFICATION_ID = 1;
private BluetoothAdapter mBtAdapter = null; private BluetoothAdapter mBtAdapter = null;
private BluetoothSocket mBtSocket = null; private BluetoothSocket mBtSocket = null;
private BtSocketIoThread mBtSocketIoThread = null; private GBDeviceIoThread mGBDeviceIoThread = null;
private boolean mStarted = false; private boolean mStarted = false;
@ -183,7 +183,7 @@ public class BluetoothCommunicationService extends Service {
break; break;
case SUCCESS: case SUCCESS:
// refresh app list // refresh app list
mBtSocketIoThread.write(mGBDeviceProtocol.encodeAppInfoReq()); mGBDeviceIoThread.write(mGBDeviceProtocol.encodeAppInfoReq());
break; break;
default: default:
break; break;
@ -244,10 +244,10 @@ public class BluetoothCommunicationService extends Service {
if (btDeviceAddress != null && (mBtSocket == null || !mBtSocket.isConnected())) { if (btDeviceAddress != null && (mBtSocket == null || !mBtSocket.isConnected())) {
// currently only one thread allowed // currently only one thread allowed
if (mBtSocketIoThread != null) { if (mGBDeviceIoThread != null) {
mBtSocketIoThread.quit(); mGBDeviceIoThread.quit();
try { try {
mBtSocketIoThread.join(); mGBDeviceIoThread.join();
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -259,17 +259,18 @@ public class BluetoothCommunicationService extends Service {
if (btDevice.getName().indexOf("Pebble") == 0) { if (btDevice.getName().indexOf("Pebble") == 0) {
deviceType = GBDevice.Type.PEBBLE; deviceType = GBDevice.Type.PEBBLE;
mGBDeviceProtocol = new PebbleProtocol(); mGBDeviceProtocol = new PebbleProtocol();
mGBDeviceIoThread = new PebbleIoThread(btDeviceAddress);
} else if (btDevice.getName().equals("MI")) { } else if (btDevice.getName().equals("MI")) {
deviceType = GBDevice.Type.MIBAND; deviceType = GBDevice.Type.MIBAND;
mGBDeviceProtocol = new MibandProtocol(); mGBDeviceProtocol = new MibandProtocol();
mGBDeviceIoThread = new MibandIoThread(btDeviceAddress);
} }
if (mGBDeviceProtocol != null ) { if (mGBDeviceProtocol != null) {
mGBDevice = new GBDevice(btDeviceAddress, btDevice.getName(), deviceType); mGBDevice = new GBDevice(btDeviceAddress, btDevice.getName(), deviceType);
mGBDevice.setState(GBDevice.State.CONNECTING); mGBDevice.setState(GBDevice.State.CONNECTING);
sendDeviceUpdateIntent(); sendDeviceUpdateIntent();
mBtSocketIoThread = new BtSocketIoThread(btDeviceAddress); mGBDeviceIoThread.start();
mBtSocketIoThread.start();
} }
} }
} }
@ -278,19 +279,19 @@ public class BluetoothCommunicationService extends Service {
String title = intent.getStringExtra("notification_title"); String title = intent.getStringExtra("notification_title");
String body = intent.getStringExtra("notification_body"); String body = intent.getStringExtra("notification_body");
byte[] msg = mGBDeviceProtocol.encodeSMS(title, body); byte[] msg = mGBDeviceProtocol.encodeSMS(title, body);
mBtSocketIoThread.write(msg); mGBDeviceIoThread.write(msg);
} else if (action.equals(ACTION_NOTIFICATION_SMS)) { } else if (action.equals(ACTION_NOTIFICATION_SMS)) {
String sender = intent.getStringExtra("notification_sender"); String sender = intent.getStringExtra("notification_sender");
String body = intent.getStringExtra("notification_body"); String body = intent.getStringExtra("notification_body");
String senderName = getContactDisplayNameByNumber(sender); String senderName = getContactDisplayNameByNumber(sender);
byte[] msg = mGBDeviceProtocol.encodeSMS(senderName, body); byte[] msg = mGBDeviceProtocol.encodeSMS(senderName, body);
mBtSocketIoThread.write(msg); mGBDeviceIoThread.write(msg);
} else if (action.equals(ACTION_NOTIFICATION_EMAIL)) { } else if (action.equals(ACTION_NOTIFICATION_EMAIL)) {
String sender = intent.getStringExtra("notification_sender"); String sender = intent.getStringExtra("notification_sender");
String subject = intent.getStringExtra("notification_subject"); String subject = intent.getStringExtra("notification_subject");
String body = intent.getStringExtra("notification_body"); String body = intent.getStringExtra("notification_body");
byte[] msg = mGBDeviceProtocol.encodeEmail(sender, subject, body); byte[] msg = mGBDeviceProtocol.encodeEmail(sender, subject, body);
mBtSocketIoThread.write(msg); mGBDeviceIoThread.write(msg);
} else if (action.equals(ACTION_CALLSTATE)) { } else if (action.equals(ACTION_CALLSTATE)) {
GBCommand command = GBCommand.values()[intent.getIntExtra("call_command", 0)]; // UGLY GBCommand command = GBCommand.values()[intent.getIntExtra("call_command", 0)]; // UGLY
String phoneNumber = intent.getStringExtra("call_phonenumber"); String phoneNumber = intent.getStringExtra("call_phonenumber");
@ -299,29 +300,29 @@ public class BluetoothCommunicationService extends Service {
callerName = getContactDisplayNameByNumber(phoneNumber); callerName = getContactDisplayNameByNumber(phoneNumber);
} }
byte[] msg = mGBDeviceProtocol.encodeSetCallState(phoneNumber, callerName, command); byte[] msg = mGBDeviceProtocol.encodeSetCallState(phoneNumber, callerName, command);
mBtSocketIoThread.write(msg); mGBDeviceIoThread.write(msg);
} else if (action.equals(ACTION_SETTIME)) { } else if (action.equals(ACTION_SETTIME)) {
byte[] msg = mGBDeviceProtocol.encodeSetTime(-1); byte[] msg = mGBDeviceProtocol.encodeSetTime(-1);
mBtSocketIoThread.write(msg); mGBDeviceIoThread.write(msg);
} else if (action.equals(ACTION_SETMUSICINFO)) { } else if (action.equals(ACTION_SETMUSICINFO)) {
String artist = intent.getStringExtra("music_artist"); String artist = intent.getStringExtra("music_artist");
String album = intent.getStringExtra("music_album"); String album = intent.getStringExtra("music_album");
String track = intent.getStringExtra("music_track"); String track = intent.getStringExtra("music_track");
byte[] msg = mGBDeviceProtocol.encodeSetMusicInfo(artist, album, track); byte[] msg = mGBDeviceProtocol.encodeSetMusicInfo(artist, album, track);
mBtSocketIoThread.write(msg); mGBDeviceIoThread.write(msg);
} else if (action.equals(ACTION_REQUEST_VERSIONINFO)) { } else if (action.equals(ACTION_REQUEST_VERSIONINFO)) {
if (mGBDevice != null && mGBDevice.getFirmwareVersion() == null) { if (mGBDevice != null && mGBDevice.getFirmwareVersion() == null) {
byte[] msg = mGBDeviceProtocol.encodeFirmwareVersionReq(); byte[] msg = mGBDeviceProtocol.encodeFirmwareVersionReq();
mBtSocketIoThread.write(msg); mGBDeviceIoThread.write(msg);
} else { } else {
sendDeviceUpdateIntent(); sendDeviceUpdateIntent();
} }
} else if (action.equals(ACTION_REQUEST_APPINFO)) { } else if (action.equals(ACTION_REQUEST_APPINFO)) {
mBtSocketIoThread.write(mGBDeviceProtocol.encodeAppInfoReq()); mGBDeviceIoThread.write(mGBDeviceProtocol.encodeAppInfoReq());
} else if (action.equals(ACTION_DELETEAPP)) { } else if (action.equals(ACTION_DELETEAPP)) {
int id = intent.getIntExtra("app_id", -1); int id = intent.getIntExtra("app_id", -1);
int index = intent.getIntExtra("app_index", -1); int index = intent.getIntExtra("app_index", -1);
mBtSocketIoThread.write(mGBDeviceProtocol.encodeAppDelete(id, index)); mGBDeviceIoThread.write(mGBDeviceProtocol.encodeAppDelete(id, index));
} else if (action.equals(ACTION_START)) { } else if (action.equals(ACTION_START)) {
startForeground(NOTIFICATION_ID, createNotification("Gadgetbridge running")); startForeground(NOTIFICATION_ID, createNotification("Gadgetbridge running"));
mStarted = true; mStarted = true;
@ -336,10 +337,10 @@ public class BluetoothCommunicationService extends Service {
setReceiversEnableState(false); // disable BroadcastReceivers setReceiversEnableState(false); // disable BroadcastReceivers
if (mBtSocketIoThread != null) { if (mGBDeviceIoThread != null) {
try { try {
mBtSocketIoThread.quit(); mGBDeviceIoThread.quit();
mBtSocketIoThread.join(); mGBDeviceIoThread.join();
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -379,17 +380,44 @@ public class BluetoothCommunicationService extends Service {
return name; return name;
} }
private abstract class GBDeviceIoThread extends Thread {
protected final String mmBtDeviceAddress;
private class BtSocketIoThread extends Thread { public GBDeviceIoThread(String btDeviceAddress) {
private final String mmBtDeviceAddress; mmBtDeviceAddress = btDeviceAddress;
}
private boolean connect(String btDeviceAddress) {
return false;
}
public void run() {
}
synchronized public void write(byte[] bytes) {
}
public void quit() {
}
}
private class MibandIoThread extends GBDeviceIoThread {
public MibandIoThread(String btDeviceAddress) {
super(btDeviceAddress);
}
// implement connect() run() write() and quit() here
}
private class PebbleIoThread extends GBDeviceIoThread {
private InputStream mmInStream = null; private InputStream mmInStream = null;
private OutputStream mmOutStream = null; private OutputStream mmOutStream = null;
private boolean mQuit = false; private boolean mQuit = false;
private boolean mmIsConnected = false; private boolean mmIsConnected = false;
private int mmConnectionAttempts = 0; private int mmConnectionAttempts = 0;
public BtSocketIoThread(String btDeviceAddress) { public PebbleIoThread(String btDeviceAddress) {
mmBtDeviceAddress = btDeviceAddress; super(btDeviceAddress);
} }
private boolean connect(String btDeviceAddress) { private boolean connect(String btDeviceAddress) {