reformat code with androidstudio

live-sensor-data
Andreas Shimokawa 2015-04-13 11:22:03 +02:00
parent 62a41ac5be
commit bb44cb1e19
4 changed files with 412 additions and 409 deletions

View File

@ -1,10 +1,5 @@
package nodomain.freeyourgadget.gadgetbridge; package nodomain.freeyourgadget.gadgetbridge;
import nodomain.freeyourgadget.gadgetbridge.GBDevice.State;
import nodomain.freeyourgadget.gadgetbridge.pebble.PebbleIoThread;
import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceProtocol;
import nodomain.freeyourgadget.gadgetbridge.protocol.MibandProtocol;
import nodomain.freeyourgadget.gadgetbridge.protocol.PebbleProtocol;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.Service; import android.app.Service;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
@ -21,6 +16,12 @@ import android.provider.ContactsContract;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import nodomain.freeyourgadget.gadgetbridge.GBDevice.State;
import nodomain.freeyourgadget.gadgetbridge.pebble.PebbleIoThread;
import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceProtocol;
import nodomain.freeyourgadget.gadgetbridge.protocol.MibandProtocol;
import nodomain.freeyourgadget.gadgetbridge.protocol.PebbleProtocol;
public class BluetoothCommunicationService extends Service { public class BluetoothCommunicationService extends Service {
public static final String ACTION_START public static final String ACTION_START
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.start"; = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.start";
@ -197,10 +198,10 @@ public class BluetoothCommunicationService extends Service {
} }
private boolean isConnected() { private boolean isConnected() {
return mGBDevice != null && mGBDevice.getState() == State.CONNECTED; return mGBDevice != null && mGBDevice.getState() == State.CONNECTED;
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();

View File

@ -73,7 +73,7 @@ public class GBDevice {
LocalBroadcastManager.getInstance(context).sendBroadcast(deviceUpdateIntent); LocalBroadcastManager.getInstance(context).sendBroadcast(deviceUpdateIntent);
} }
public enum State { public enum State {
NOT_CONNECTED, NOT_CONNECTED,
CONNECTING, CONNECTING,

View File

@ -4,21 +4,21 @@ import android.content.Context;
public abstract class GBDeviceIoThread extends Thread { public abstract class GBDeviceIoThread extends Thread {
protected final GBDevice gbDevice; protected final GBDevice gbDevice;
private final Context context; private final Context context;
public GBDeviceIoThread(GBDevice gbDevice, Context context) { public GBDeviceIoThread(GBDevice gbDevice, Context context) {
this.gbDevice = gbDevice; this.gbDevice = gbDevice;
this.context = context; this.context = context;
} }
public Context getContext() { public Context getContext() {
return context; return context;
} }
public GBDevice getDevice() { public GBDevice getDevice() {
return gbDevice; return gbDevice;
} }
protected boolean connect(String btDeviceAddress) { protected boolean connect(String btDeviceAddress) {
return false; return false;
} }

View File

@ -1,5 +1,17 @@
package nodomain.freeyourgadget.gadgetbridge.pebble; package nodomain.freeyourgadget.gadgetbridge.pebble;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.ParcelUuid;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -21,379 +33,369 @@ import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandMusicControl
import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandVersionInfo; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandVersionInfo;
import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceProtocol; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceProtocol;
import nodomain.freeyourgadget.gadgetbridge.protocol.PebbleProtocol; import nodomain.freeyourgadget.gadgetbridge.protocol.PebbleProtocol;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.ParcelUuid;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
public class PebbleIoThread extends GBDeviceIoThread { public class PebbleIoThread extends GBDeviceIoThread {
private static final String TAG = PebbleIoThread.class.getSimpleName(); private static final String TAG = PebbleIoThread.class.getSimpleName();
private enum PebbleAppInstallState { private enum PebbleAppInstallState {
UNKNOWN, UNKNOWN,
APP_WAIT_SLOT, APP_WAIT_SLOT,
APP_START_INSTALL, APP_START_INSTALL,
APP_WAIT_TOKEN, APP_WAIT_TOKEN,
APP_UPLOAD_CHUNK, APP_UPLOAD_CHUNK,
APP_UPLOAD_COMMIT, APP_UPLOAD_COMMIT,
APP_WAIT_COMMMIT, APP_WAIT_COMMMIT,
APP_UPLOAD_COMPLETE, APP_UPLOAD_COMPLETE,
APP_REFRESH, APP_REFRESH,
} }
private final PebbleProtocol mmPebbleProtocol; private final PebbleProtocol mmPebbleProtocol;
private InputStream mmInStream = null;
private OutputStream mmOutStream = null;
private boolean mmQuit = false;
private boolean mmIsConnected = false;
private boolean mmIsInstalling = false;
private int mmConnectionAttempts = 0;
/* app installation */ private BluetoothAdapter mBtAdapter = null;
private Uri mmInstallURI = null; private BluetoothSocket mBtSocket = null;
private PBWReader mmPBWReader = null; private InputStream mmInStream = null;
private int mmAppInstallToken = -1; private OutputStream mmOutStream = null;
private ZipInputStream mmZis = null; private boolean mmQuit = false;
private STM32CRC mmSTM32CRC = new STM32CRC(); private boolean mmIsConnected = false;
private PebbleAppInstallState mmInstallState = PebbleAppInstallState.UNKNOWN; private boolean mmIsInstalling = false;
private String[] mmFilesToInstall = null; private int mmConnectionAttempts = 0;
private int mmCurrentFileIndex = -1;
private int mmInstallSlot = -1;
private BluetoothAdapter mBtAdapter = null;
private BluetoothSocket mBtSocket = null;
public PebbleIoThread(GBDevice gbDevice, GBDeviceProtocol gbDeviceProtocol, BluetoothAdapter btAdapter, Context context) { /* app installation */
super(gbDevice, context); private Uri mmInstallURI = null;
mmPebbleProtocol = (PebbleProtocol) gbDeviceProtocol; private PBWReader mmPBWReader = null;
mBtAdapter = btAdapter; private int mmAppInstallToken = -1;
} private ZipInputStream mmZis = null;
private STM32CRC mmSTM32CRC = new STM32CRC();
private PebbleAppInstallState mmInstallState = PebbleAppInstallState.UNKNOWN;
private String[] mmFilesToInstall = null;
private int mmCurrentFileIndex = -1;
private int mmInstallSlot = -1;
protected boolean connect(String btDeviceAddress) { public PebbleIoThread(GBDevice gbDevice, GBDeviceProtocol gbDeviceProtocol, BluetoothAdapter btAdapter, Context context) {
BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(btDeviceAddress); super(gbDevice, context);
ParcelUuid uuids[] = btDevice.getUuids(); mmPebbleProtocol = (PebbleProtocol) gbDeviceProtocol;
try { mBtAdapter = btAdapter;
mBtSocket = btDevice.createRfcommSocketToServiceRecord(uuids[0].getUuid()); }
mBtSocket.connect();
mmInStream = mBtSocket.getInputStream();
mmOutStream = mBtSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
gbDevice.setState(GBDevice.State.NOT_CONNECTED);
mmInStream = null;
mmOutStream = null;
mBtSocket = null;
return false;
}
gbDevice.setState(GBDevice.State.CONNECTED);
gbDevice.sendDeviceUpdateIntent(getContext());
GB.updateNotification("connected to " + btDevice.getName(), getContext());
return true; protected boolean connect(String btDeviceAddress) {
} BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(btDeviceAddress);
ParcelUuid uuids[] = btDevice.getUuids();
try {
mBtSocket = btDevice.createRfcommSocketToServiceRecord(uuids[0].getUuid());
mBtSocket.connect();
mmInStream = mBtSocket.getInputStream();
mmOutStream = mBtSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
gbDevice.setState(GBDevice.State.NOT_CONNECTED);
mmInStream = null;
mmOutStream = null;
mBtSocket = null;
return false;
}
gbDevice.setState(GBDevice.State.CONNECTED);
gbDevice.sendDeviceUpdateIntent(getContext());
GB.updateNotification("connected to " + btDevice.getName(), getContext());
public void run() { return true;
mmIsConnected = connect(gbDevice.getAddress()); }
GB.setReceiversEnableState(mmIsConnected, getContext()); // enable/disable BroadcastReceivers
mmQuit = !mmIsConnected; // quit if not connected
byte[] buffer = new byte[8192]; public void run() {
int bytes; mmIsConnected = connect(gbDevice.getAddress());
GB.setReceiversEnableState(mmIsConnected, getContext()); // enable/disable BroadcastReceivers
mmQuit = !mmIsConnected; // quit if not connected
while (!mmQuit) { byte[] buffer = new byte[8192];
try { int bytes;
if (mmIsInstalling) {
switch (mmInstallState) {
case APP_WAIT_SLOT:
if (mmInstallSlot != -1) {
GB.updateNotification("starting installation", getContext());
mmInstallState = PebbleAppInstallState.APP_START_INSTALL;
continue;
}
break;
case APP_START_INSTALL:
Log.i(TAG, "start installing app binary");
mmSTM32CRC.reset();
if (mmPBWReader == null) {
mmPBWReader = new PBWReader(mmInstallURI, getContext());
mmFilesToInstall = mmPBWReader.getFilesToInstall();
mmCurrentFileIndex = 0;
}
String fileName = mmFilesToInstall[mmCurrentFileIndex];
mmZis = mmPBWReader.getInputStreamFile(fileName);
int binarySize = mmPBWReader.getFileSize(fileName);
// FIXME: do not assume type from filename, parse json correctly in PBWReader
byte type = -1;
if (fileName.equals("pebble-app.bin")) {
type = PebbleProtocol.PUTBYTES_TYPE_BINARY;
} else if (fileName.equals("pebble-worker.bin")) {
type = PebbleProtocol.PUTBYTES_TYPE_WORKER;
} else if (fileName.equals("app_resources.pbpack")) {
type = PebbleProtocol.PUTBYTES_TYPE_RESOURCES;
} else {
finishInstall(true);
break;
}
writeInstallApp(mmPebbleProtocol.encodeUploadStart(type, (byte) mmInstallSlot, binarySize)); while (!mmQuit) {
mmInstallState = PebbleAppInstallState.APP_WAIT_TOKEN; try {
break; if (mmIsInstalling) {
case APP_WAIT_TOKEN: switch (mmInstallState) {
if (mmAppInstallToken != -1) { case APP_WAIT_SLOT:
Log.i(TAG, "got token " + mmAppInstallToken); if (mmInstallSlot != -1) {
mmInstallState = PebbleAppInstallState.APP_UPLOAD_CHUNK; GB.updateNotification("starting installation", getContext());
continue; mmInstallState = PebbleAppInstallState.APP_START_INSTALL;
} continue;
break; }
case APP_UPLOAD_CHUNK: break;
bytes = mmZis.read(buffer); case APP_START_INSTALL:
Log.i(TAG, "start installing app binary");
mmSTM32CRC.reset();
if (mmPBWReader == null) {
mmPBWReader = new PBWReader(mmInstallURI, getContext());
mmFilesToInstall = mmPBWReader.getFilesToInstall();
mmCurrentFileIndex = 0;
}
String fileName = mmFilesToInstall[mmCurrentFileIndex];
mmZis = mmPBWReader.getInputStreamFile(fileName);
int binarySize = mmPBWReader.getFileSize(fileName);
// FIXME: do not assume type from filename, parse json correctly in PBWReader
byte type = -1;
if (fileName.equals("pebble-app.bin")) {
type = PebbleProtocol.PUTBYTES_TYPE_BINARY;
} else if (fileName.equals("pebble-worker.bin")) {
type = PebbleProtocol.PUTBYTES_TYPE_WORKER;
} else if (fileName.equals("app_resources.pbpack")) {
type = PebbleProtocol.PUTBYTES_TYPE_RESOURCES;
} else {
finishInstall(true);
break;
}
if (bytes != -1) { writeInstallApp(mmPebbleProtocol.encodeUploadStart(type, (byte) mmInstallSlot, binarySize));
mmSTM32CRC.addData(buffer, bytes); mmInstallState = PebbleAppInstallState.APP_WAIT_TOKEN;
writeInstallApp(mmPebbleProtocol.encodeUploadChunk(mmAppInstallToken, buffer, bytes)); break;
mmAppInstallToken = -1; case APP_WAIT_TOKEN:
mmInstallState = PebbleAppInstallState.APP_WAIT_TOKEN; if (mmAppInstallToken != -1) {
} else { Log.i(TAG, "got token " + mmAppInstallToken);
mmInstallState = PebbleAppInstallState.APP_UPLOAD_COMMIT; mmInstallState = PebbleAppInstallState.APP_UPLOAD_CHUNK;
continue; continue;
} }
break; break;
case APP_UPLOAD_COMMIT: case APP_UPLOAD_CHUNK:
writeInstallApp(mmPebbleProtocol.encodeUploadCommit(mmAppInstallToken, mmSTM32CRC.getResult())); bytes = mmZis.read(buffer);
mmAppInstallToken = -1;
mmInstallState = PebbleAppInstallState.APP_WAIT_COMMMIT;
break;
case APP_WAIT_COMMMIT:
if (mmAppInstallToken != -1) {
Log.i(TAG, "got token " + mmAppInstallToken);
mmInstallState = PebbleAppInstallState.APP_UPLOAD_COMPLETE;
continue;
}
break;
case APP_UPLOAD_COMPLETE:
writeInstallApp(mmPebbleProtocol.encodeUploadComplete(mmAppInstallToken));
if (++mmCurrentFileIndex < mmFilesToInstall.length) {
mmInstallState = PebbleAppInstallState.APP_START_INSTALL;
} else {
mmInstallState = PebbleAppInstallState.APP_REFRESH;
}
break;
case APP_REFRESH:
writeInstallApp(mmPebbleProtocol.encodeAppRefresh(mmInstallSlot));
break;
default:
break;
}
}
bytes = mmInStream.read(buffer, 0, 4);
if (bytes < 4)
continue;
ByteBuffer buf = ByteBuffer.wrap(buffer); if (bytes != -1) {
buf.order(ByteOrder.BIG_ENDIAN); mmSTM32CRC.addData(buffer, bytes);
short length = buf.getShort(); writeInstallApp(mmPebbleProtocol.encodeUploadChunk(mmAppInstallToken, buffer, bytes));
short endpoint = buf.getShort(); mmAppInstallToken = -1;
if (length < 0 || length > 8192) { mmInstallState = PebbleAppInstallState.APP_WAIT_TOKEN;
Log.i(TAG, "invalid length " + length); } else {
while (mmInStream.available() > 0) { mmInstallState = PebbleAppInstallState.APP_UPLOAD_COMMIT;
mmInStream.read(buffer); // read all continue;
} }
continue; break;
} case APP_UPLOAD_COMMIT:
writeInstallApp(mmPebbleProtocol.encodeUploadCommit(mmAppInstallToken, mmSTM32CRC.getResult()));
mmAppInstallToken = -1;
mmInstallState = PebbleAppInstallState.APP_WAIT_COMMMIT;
break;
case APP_WAIT_COMMMIT:
if (mmAppInstallToken != -1) {
Log.i(TAG, "got token " + mmAppInstallToken);
mmInstallState = PebbleAppInstallState.APP_UPLOAD_COMPLETE;
continue;
}
break;
case APP_UPLOAD_COMPLETE:
writeInstallApp(mmPebbleProtocol.encodeUploadComplete(mmAppInstallToken));
if (++mmCurrentFileIndex < mmFilesToInstall.length) {
mmInstallState = PebbleAppInstallState.APP_START_INSTALL;
} else {
mmInstallState = PebbleAppInstallState.APP_REFRESH;
}
break;
case APP_REFRESH:
writeInstallApp(mmPebbleProtocol.encodeAppRefresh(mmInstallSlot));
break;
default:
break;
}
}
bytes = mmInStream.read(buffer, 0, 4);
if (bytes < 4)
continue;
bytes = mmInStream.read(buffer, 4, length); ByteBuffer buf = ByteBuffer.wrap(buffer);
if (bytes < length) { buf.order(ByteOrder.BIG_ENDIAN);
try { short length = buf.getShort();
Thread.sleep(100); short endpoint = buf.getShort();
} catch (InterruptedException e) { if (length < 0 || length > 8192) {
e.printStackTrace(); Log.i(TAG, "invalid length " + length);
} while (mmInStream.available() > 0) {
Log.i(TAG, "Read " + bytes + ", expected " + length + " reading remaining " + (length - bytes)); mmInStream.read(buffer); // read all
int bytes_rest = mmInStream.read(buffer, 4 + bytes, length - bytes); }
bytes += bytes_rest; continue;
} }
if (length == 1 && endpoint == PebbleProtocol.ENDPOINT_PHONEVERSION) { bytes = mmInStream.read(buffer, 4, length);
Log.i(TAG, "Pebble asked for Phone/App Version - repLYING!"); if (bytes < length) {
write(mmPebbleProtocol.encodePhoneVersion(PebbleProtocol.PHONEVERSION_REMOTE_OS_ANDROID)); try {
write(mmPebbleProtocol.encodeFirmwareVersionReq()); Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i(TAG, "Read " + bytes + ", expected " + length + " reading remaining " + (length - bytes));
int bytes_rest = mmInStream.read(buffer, 4 + bytes, length - bytes);
bytes += bytes_rest;
}
// this does not really belong here, but since the pebble only asks for our version once it should do the job if (length == 1 && endpoint == PebbleProtocol.ENDPOINT_PHONEVERSION) {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); Log.i(TAG, "Pebble asked for Phone/App Version - repLYING!");
if (sharedPrefs.getBoolean("datetime_synconconnect", true)) { write(mmPebbleProtocol.encodePhoneVersion(PebbleProtocol.PHONEVERSION_REMOTE_OS_ANDROID));
Log.i(TAG, "syncing time"); write(mmPebbleProtocol.encodeFirmwareVersionReq());
write(mmPebbleProtocol.encodeSetTime(-1));
}
} else if (endpoint != PebbleProtocol.ENDPOINT_DATALOG) {
GBDeviceCommand deviceCmd = mmPebbleProtocol.decodeResponse(buffer);
if (deviceCmd == null) {
Log.i(TAG, "unhandled message to endpoint " + endpoint + " (" + bytes + " bytes)");
} else {
evaluateGBCommandBundle(deviceCmd);
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
if (e.getMessage().contains("socket closed")) { //FIXME: this does not feel right
Log.i(TAG, e.getMessage());
gbDevice.setState(GBDevice.State.CONNECTING);
gbDevice.sendDeviceUpdateIntent(getContext());
GB.updateNotification("connection lost, trying to reconnect", getContext());
while (mmConnectionAttempts++ < 10) { // this does not really belong here, but since the pebble only asks for our version once it should do the job
Log.i(TAG, "Trying to reconnect (attempt " + mmConnectionAttempts + ")"); SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
mmIsConnected = connect(gbDevice.getAddress()); if (sharedPrefs.getBoolean("datetime_synconconnect", true)) {
if (mmIsConnected) Log.i(TAG, "syncing time");
break; write(mmPebbleProtocol.encodeSetTime(-1));
} }
mmConnectionAttempts = 0; } else if (endpoint != PebbleProtocol.ENDPOINT_DATALOG) {
if (!mmIsConnected) { GBDeviceCommand deviceCmd = mmPebbleProtocol.decodeResponse(buffer);
mBtSocket = null; if (deviceCmd == null) {
GB.setReceiversEnableState(false, getContext()); Log.i(TAG, "unhandled message to endpoint " + endpoint + " (" + bytes + " bytes)");
Log.i(TAG, "Bluetooth socket closed, will quit IO Thread"); } else {
mmQuit = true; evaluateGBCommandBundle(deviceCmd);
} }
} }
} try {
} Thread.sleep(100);
mmIsConnected = false; } catch (InterruptedException e) {
if (mBtSocket != null) { e.printStackTrace();
try { }
mBtSocket.close(); } catch (IOException e) {
} catch (IOException e) { if (e.getMessage().contains("socket closed")) { //FIXME: this does not feel right
e.printStackTrace(); Log.i(TAG, e.getMessage());
} gbDevice.setState(GBDevice.State.CONNECTING);
} gbDevice.sendDeviceUpdateIntent(getContext());
mBtSocket = null; GB.updateNotification("connection lost, trying to reconnect", getContext());
GB.updateNotification("not connected", getContext());
gbDevice.setState(GBDevice.State.NOT_CONNECTED);
gbDevice.sendDeviceUpdateIntent(getContext());
}
synchronized public void write(byte[] bytes) { while (mmConnectionAttempts++ < 10) {
// block writes if app installation in in progress Log.i(TAG, "Trying to reconnect (attempt " + mmConnectionAttempts + ")");
if (mmIsConnected && !mmIsInstalling) { mmIsConnected = connect(gbDevice.getAddress());
try { if (mmIsConnected)
mmOutStream.write(bytes); break;
mmOutStream.flush(); }
} catch (IOException e) { mmConnectionAttempts = 0;
} if (!mmIsConnected) {
} mBtSocket = null;
} GB.setReceiversEnableState(false, getContext());
Log.i(TAG, "Bluetooth socket closed, will quit IO Thread");
mmQuit = true;
}
}
}
}
mmIsConnected = false;
if (mBtSocket != null) {
try {
mBtSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
mBtSocket = null;
GB.updateNotification("not connected", getContext());
gbDevice.setState(GBDevice.State.NOT_CONNECTED);
gbDevice.sendDeviceUpdateIntent(getContext());
}
private void evaluateGBCommandBundle(GBDeviceCommand deviceCmd) { synchronized public void write(byte[] bytes) {
Context context = getContext(); // block writes if app installation in in progress
if (mmIsConnected && !mmIsInstalling) {
try {
mmOutStream.write(bytes);
mmOutStream.flush();
} catch (IOException e) {
}
}
}
switch (deviceCmd.commandClass) { private void evaluateGBCommandBundle(GBDeviceCommand deviceCmd) {
case MUSIC_CONTROL: Context context = getContext();
Log.i(TAG, "Got command for MUSIC_CONTROL");
GBDeviceCommandMusicControl musicCmd = (GBDeviceCommandMusicControl) deviceCmd;
Intent musicIntent = new Intent(GBMusicControlReceiver.ACTION_MUSICCONTROL);
musicIntent.putExtra("command", musicCmd.command.ordinal());
musicIntent.setPackage(context.getPackageName());
context.sendBroadcast(musicIntent);
break;
case CALL_CONTROL:
Log.i(TAG, "Got command for CALL_CONTROL");
GBDeviceCommandCallControl callCmd = (GBDeviceCommandCallControl) deviceCmd;
Intent callIntent = new Intent(GBCallControlReceiver.ACTION_CALLCONTROL);
callIntent.putExtra("command", callCmd.command.ordinal());
callIntent.setPackage(context.getPackageName());
context.sendBroadcast(callIntent);
break;
case VERSION_INFO:
Log.i(TAG, "Got command for VERSION_INFO");
if (gbDevice == null) {
return;
}
GBDeviceCommandVersionInfo infoCmd = (GBDeviceCommandVersionInfo) deviceCmd;
gbDevice.setFirmwareVersion(infoCmd.fwVersion);
gbDevice.sendDeviceUpdateIntent(context);
break;
case APP_INFO:
Log.i(TAG, "Got command for APP_INFO");
GBDeviceCommandAppInfo appInfoCmd = (GBDeviceCommandAppInfo) deviceCmd;
setInstallSlot(appInfoCmd.freeSlot);
Intent appInfoIntent = new Intent(AppManagerActivity.ACTION_REFRESH_APPLIST); switch (deviceCmd.commandClass) {
int appCount = appInfoCmd.apps.length; case MUSIC_CONTROL:
appInfoIntent.putExtra("app_count", appCount); Log.i(TAG, "Got command for MUSIC_CONTROL");
for (Integer i = 0; i < appCount; i++) { GBDeviceCommandMusicControl musicCmd = (GBDeviceCommandMusicControl) deviceCmd;
appInfoIntent.putExtra("app_name" + i.toString(), appInfoCmd.apps[i].getName()); Intent musicIntent = new Intent(GBMusicControlReceiver.ACTION_MUSICCONTROL);
appInfoIntent.putExtra("app_creator" + i.toString(), appInfoCmd.apps[i].getCreator()); musicIntent.putExtra("command", musicCmd.command.ordinal());
appInfoIntent.putExtra("app_id" + i.toString(), appInfoCmd.apps[i].getId()); musicIntent.setPackage(context.getPackageName());
appInfoIntent.putExtra("app_index" + i.toString(), appInfoCmd.apps[i].getIndex()); context.sendBroadcast(musicIntent);
appInfoIntent.putExtra("app_type" + i.toString(), appInfoCmd.apps[i].getType().ordinal()); break;
} case CALL_CONTROL:
LocalBroadcastManager.getInstance(context).sendBroadcast(appInfoIntent); Log.i(TAG, "Got command for CALL_CONTROL");
break; GBDeviceCommandCallControl callCmd = (GBDeviceCommandCallControl) deviceCmd;
case APP_MANAGEMENT_RES: Intent callIntent = new Intent(GBCallControlReceiver.ACTION_CALLCONTROL);
GBDeviceCommandAppManagementResult appMgmtRes = (GBDeviceCommandAppManagementResult) deviceCmd; callIntent.putExtra("command", callCmd.command.ordinal());
switch (appMgmtRes.type) { callIntent.setPackage(context.getPackageName());
case DELETE: context.sendBroadcast(callIntent);
// right now on the Pebble we also receive this on a failed/successful installation ;/ break;
switch (appMgmtRes.result) { case VERSION_INFO:
case FAILURE: Log.i(TAG, "Got command for VERSION_INFO");
Log.i(TAG, "failure removing app"); // TODO: report to AppManager if (gbDevice == null) {
finishInstall(true); return;
break; }
case SUCCESS: GBDeviceCommandVersionInfo infoCmd = (GBDeviceCommandVersionInfo) deviceCmd;
finishInstall(false); gbDevice.setFirmwareVersion(infoCmd.fwVersion);
// refresh app list gbDevice.sendDeviceUpdateIntent(context);
write(mmPebbleProtocol.encodeAppInfoReq()); break;
break; case APP_INFO:
default: Log.i(TAG, "Got command for APP_INFO");
break; GBDeviceCommandAppInfo appInfoCmd = (GBDeviceCommandAppInfo) deviceCmd;
} setInstallSlot(appInfoCmd.freeSlot);
break;
case INSTALL:
switch (appMgmtRes.result) {
case FAILURE:
Log.i(TAG, "failure installing app"); // TODO: report to Installer
finishInstall(true);
break;
case SUCCESS:
setToken(appMgmtRes.token);
break;
default:
break;
}
break;
default:
break;
}
default:
break;
}
}
public void setToken(int token) { Intent appInfoIntent = new Intent(AppManagerActivity.ACTION_REFRESH_APPLIST);
mmAppInstallToken = token; int appCount = appInfoCmd.apps.length;
} appInfoIntent.putExtra("app_count", appCount);
for (Integer i = 0; i < appCount; i++) {
appInfoIntent.putExtra("app_name" + i.toString(), appInfoCmd.apps[i].getName());
appInfoIntent.putExtra("app_creator" + i.toString(), appInfoCmd.apps[i].getCreator());
appInfoIntent.putExtra("app_id" + i.toString(), appInfoCmd.apps[i].getId());
appInfoIntent.putExtra("app_index" + i.toString(), appInfoCmd.apps[i].getIndex());
appInfoIntent.putExtra("app_type" + i.toString(), appInfoCmd.apps[i].getType().ordinal());
}
LocalBroadcastManager.getInstance(context).sendBroadcast(appInfoIntent);
break;
case APP_MANAGEMENT_RES:
GBDeviceCommandAppManagementResult appMgmtRes = (GBDeviceCommandAppManagementResult) deviceCmd;
switch (appMgmtRes.type) {
case DELETE:
// right now on the Pebble we also receive this on a failed/successful installation ;/
switch (appMgmtRes.result) {
case FAILURE:
Log.i(TAG, "failure removing app"); // TODO: report to AppManager
finishInstall(true);
break;
case SUCCESS:
finishInstall(false);
// refresh app list
write(mmPebbleProtocol.encodeAppInfoReq());
break;
default:
break;
}
break;
case INSTALL:
switch (appMgmtRes.result) {
case FAILURE:
Log.i(TAG, "failure installing app"); // TODO: report to Installer
finishInstall(true);
break;
case SUCCESS:
setToken(appMgmtRes.token);
break;
default:
break;
}
break;
default:
break;
}
default:
break;
}
}
public void setInstallSlot(int slot) { public void setToken(int token) {
if (mmIsInstalling) { mmAppInstallToken = token;
mmInstallSlot = slot; }
}
}
private void writeInstallApp(byte[] bytes) { public void setInstallSlot(int slot) {
if (!mmIsInstalling) { if (mmIsInstalling) {
return; mmInstallSlot = slot;
} }
int length = bytes.length; }
Log.i(TAG, "got bytes for writeInstallApp()" + length);
/* private void writeInstallApp(byte[] bytes) {
if (!mmIsInstalling) {
return;
}
int length = bytes.length;
Log.i(TAG, "got bytes for writeInstallApp()" + length);
/*
final char[] hexArray = "0123456789ABCDEF".toCharArray(); final char[] hexArray = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[length * 2]; char[] hexChars = new char[length * 2];
for (int j = 0; j < length; j++) { for (int j = 0; j < length; j++) {
@ -403,53 +405,53 @@ public class PebbleIoThread extends GBDeviceIoThread {
} }
Log.i(TAG, new String(hexChars)); Log.i(TAG, new String(hexChars));
*/ */
try { try {
mmOutStream.write(bytes); mmOutStream.write(bytes);
mmOutStream.flush(); mmOutStream.flush();
} catch (IOException e) { } catch (IOException e) {
} }
} }
public void installApp(Uri uri) { public void installApp(Uri uri) {
if (mmIsInstalling) { if (mmIsInstalling) {
return; return;
} }
write(mmPebbleProtocol.encodeAppInfoReq()); // do this here to get run() out of its blocking read write(mmPebbleProtocol.encodeAppInfoReq()); // do this here to get run() out of its blocking read
mmInstallState = PebbleAppInstallState.APP_WAIT_SLOT; mmInstallState = PebbleAppInstallState.APP_WAIT_SLOT;
mmInstallURI = uri; mmInstallURI = uri;
mmIsInstalling = true; mmIsInstalling = true;
} }
public void finishInstall(boolean hadError) { public void finishInstall(boolean hadError) {
if (!mmIsInstalling) { if (!mmIsInstalling) {
return; return;
} }
if (hadError) { if (hadError) {
GB.updateNotification("installation failed!", getContext()); GB.updateNotification("installation failed!", getContext());
} else { } else {
GB.updateNotification("installation successful", getContext()); GB.updateNotification("installation successful", getContext());
} }
mmInstallState = PebbleAppInstallState.UNKNOWN; mmInstallState = PebbleAppInstallState.UNKNOWN;
if (hadError == true && mmAppInstallToken != -1) { if (hadError == true && mmAppInstallToken != -1) {
writeInstallApp(mmPebbleProtocol.encodeUploadCancel(mmAppInstallToken)); writeInstallApp(mmPebbleProtocol.encodeUploadCancel(mmAppInstallToken));
} }
mmPBWReader = null; mmPBWReader = null;
mmIsInstalling = false; mmIsInstalling = false;
mmZis = null; mmZis = null;
mmAppInstallToken = -1; mmAppInstallToken = -1;
mmInstallSlot = -1; mmInstallSlot = -1;
} }
public void quit() { public void quit() {
mmQuit = true; mmQuit = true;
if (mBtSocket != null) { if (mBtSocket != null) {
try { try {
mBtSocket.close(); mBtSocket.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
} }