2015-03-21 18:18:07 +01:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge;
|
|
|
|
|
2015-04-13 01:01:52 +02:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2015-04-19 22:20:47 +02:00
|
|
|
import android.util.Log;
|
2015-04-13 01:01:52 +02:00
|
|
|
|
2015-03-21 18:18:07 +01:00
|
|
|
public class GBDevice {
|
2015-04-19 14:34:18 +02:00
|
|
|
public static final String ACTION_DEVICE_CHANGED
|
|
|
|
= "nodomain.freeyourgadget.gadgetbride.gbdevice.action.device_changed";
|
2015-04-19 22:20:47 +02:00
|
|
|
private static final String TAG = GBDevice.class.getSimpleName();
|
2015-04-19 14:34:18 +02:00
|
|
|
|
2015-03-21 18:18:07 +01:00
|
|
|
private final String name;
|
|
|
|
private final String address;
|
2015-03-31 23:34:19 +02:00
|
|
|
private final Type type;
|
2015-03-22 23:38:51 +01:00
|
|
|
private String firmwareVersion = null;
|
|
|
|
private State state = State.NOT_CONNECTED;
|
|
|
|
|
2015-04-19 22:20:47 +02:00
|
|
|
private short mBatteryLevel = 50; // unknown
|
|
|
|
|
|
|
|
private String mBatteryState;
|
|
|
|
|
|
|
|
|
2015-03-31 23:34:19 +02:00
|
|
|
public GBDevice(String address, String name, Type type) {
|
2015-03-21 18:18:07 +01:00
|
|
|
this.address = address;
|
|
|
|
this.name = name;
|
2015-03-31 23:34:19 +02:00
|
|
|
this.type = type;
|
2015-03-22 00:34:54 +01:00
|
|
|
}
|
|
|
|
|
2015-03-21 18:18:07 +01:00
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getAddress() {
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
2015-03-22 23:38:51 +01:00
|
|
|
public String getFirmwareVersion() {
|
|
|
|
return firmwareVersion;
|
|
|
|
}
|
|
|
|
|
2015-03-31 23:34:19 +02:00
|
|
|
public void setFirmwareVersion(String firmwareVersion) {
|
|
|
|
this.firmwareVersion = firmwareVersion;
|
|
|
|
}
|
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
public boolean isConnected() {
|
|
|
|
return state.ordinal() >= State.CONNECTED.ordinal();
|
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
public boolean isInitialized() {
|
|
|
|
return state.ordinal() >= State.INITIALIZED.ordinal();
|
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-20 10:50:30 +02:00
|
|
|
public boolean isConnecting() {
|
|
|
|
return state == State.CONNECTING;
|
|
|
|
}
|
|
|
|
|
2015-03-22 23:38:51 +01:00
|
|
|
public State getState() {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2015-03-31 23:34:19 +02:00
|
|
|
public void setState(State state) {
|
|
|
|
this.state = state;
|
|
|
|
}
|
|
|
|
|
2015-03-22 23:38:51 +01:00
|
|
|
String getStateString() {
|
|
|
|
switch (state) {
|
|
|
|
case NOT_CONNECTED:
|
|
|
|
return "not connected"; // TODO: do not hardcode
|
|
|
|
case CONNECTING:
|
|
|
|
return "connecting";
|
|
|
|
case CONNECTED:
|
|
|
|
return "connected";
|
2015-04-19 15:21:15 +02:00
|
|
|
case INITIALIZED:
|
|
|
|
return "initialized";
|
2015-03-22 23:38:51 +01:00
|
|
|
}
|
|
|
|
return "unknown state";
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getInfoString() {
|
2015-03-22 00:34:54 +01:00
|
|
|
if (firmwareVersion != null) {
|
2015-03-22 23:38:51 +01:00
|
|
|
return getStateString() + " (FW: " + firmwareVersion + ")";
|
2015-03-22 00:34:54 +01:00
|
|
|
} else {
|
2015-03-22 23:38:51 +01:00
|
|
|
return getStateString();
|
2015-03-22 00:34:54 +01:00
|
|
|
}
|
2015-03-21 18:18:07 +01:00
|
|
|
}
|
2015-03-31 23:34:19 +02:00
|
|
|
|
|
|
|
public Type getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
// TODO: this doesn't really belong here
|
2015-04-13 01:01:52 +02:00
|
|
|
public void sendDeviceUpdateIntent(Context context) {
|
2015-04-19 14:34:18 +02:00
|
|
|
Intent deviceUpdateIntent = new Intent(ACTION_DEVICE_CHANGED);
|
2015-04-13 01:01:52 +02:00
|
|
|
deviceUpdateIntent.putExtra("device_address", getAddress());
|
|
|
|
deviceUpdateIntent.putExtra("device_state", getState().ordinal());
|
|
|
|
deviceUpdateIntent.putExtra("firmware_version", getFirmwareVersion());
|
|
|
|
|
|
|
|
LocalBroadcastManager.getInstance(context).sendBroadcast(deviceUpdateIntent);
|
|
|
|
}
|
2015-04-13 11:22:03 +02:00
|
|
|
|
2015-03-31 23:34:19 +02:00
|
|
|
public enum State {
|
2015-04-19 02:37:29 +02:00
|
|
|
// Note: the order is important!
|
2015-03-31 23:34:19 +02:00
|
|
|
NOT_CONNECTED,
|
|
|
|
CONNECTING,
|
2015-04-19 02:37:29 +02:00
|
|
|
CONNECTED,
|
|
|
|
INITIALIZED
|
2015-03-31 23:34:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public enum Type {
|
|
|
|
UNKNOWN,
|
|
|
|
PEBBLE,
|
|
|
|
MIBAND
|
|
|
|
}
|
2015-04-19 22:20:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ranges from 0-100 (percent)
|
|
|
|
* @return the battery level in range 0-100
|
|
|
|
*/
|
|
|
|
public short getBatteryLevel() {
|
|
|
|
return mBatteryLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBatteryLevel(short batteryLevel) {
|
|
|
|
if (mBatteryLevel >= 0 && mBatteryLevel <= 100) {
|
|
|
|
mBatteryLevel = batteryLevel;
|
|
|
|
} else {
|
|
|
|
Log.e(TAG, "Battery level musts be within range 0-100: " + batteryLevel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a string representation of the battery state.
|
|
|
|
*/
|
|
|
|
public String getBatteryState() {
|
|
|
|
return mBatteryState != null ? mBatteryState : "(unknown)";
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBatteryState(String batteryState) {
|
|
|
|
mBatteryState = batteryState;
|
|
|
|
}
|
2015-03-21 18:18:07 +01:00
|
|
|
}
|