1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-01-13 11:17:33 +01:00

remove eventClass field from GBDeviceEvent, use instanceof instead

This commit is contained in:
Andreas Shimokawa 2015-08-31 17:25:58 +02:00
parent 50960277dd
commit 95b65265b4
14 changed files with 102 additions and 173 deletions

View File

@ -2,20 +2,6 @@ package nodomain.freeyourgadget.gadgetbridge.deviceevents;
public abstract class GBDeviceEvent { public abstract class GBDeviceEvent {
public EventClass eventClass = EventClass.UNKNOWN;
public enum EventClass {
UNKNOWN,
MUSIC_CONTROL,
CALL_CONTROL,
APP_INFO,
VERSION_INFO,
APP_MANAGEMENT,
SEND_BYTES,
SLEEP_MONITOR_RES,
SCREENSHOT,
DISMISS_NOTIFICATION,
BATTERY_INFO
}
} }

View File

@ -5,8 +5,4 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp;
public class GBDeviceEventAppInfo extends GBDeviceEvent { public class GBDeviceEventAppInfo extends GBDeviceEvent {
public GBDeviceApp apps[]; public GBDeviceApp apps[];
public byte freeSlot = -1; public byte freeSlot = -1;
public GBDeviceEventAppInfo() {
eventClass = EventClass.APP_INFO;
}
} }

View File

@ -8,10 +8,6 @@ public class GBDeviceEventAppManagement extends GBDeviceEvent {
public int token = -1; public int token = -1;
public UUID uuid = null; public UUID uuid = null;
public GBDeviceEventAppManagement() {
eventClass = EventClass.APP_MANAGEMENT;
}
public enum EventType { public enum EventType {
UNKNOWN, UNKNOWN,
INSTALL, INSTALL,

View File

@ -6,15 +6,11 @@ import java.util.GregorianCalendar;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryState; import nodomain.freeyourgadget.gadgetbridge.model.BatteryState;
public class GBDeviceEventBatteryInfo extends GBDeviceEvent { public class GBDeviceEventBatteryInfo extends GBDeviceEvent {
public GregorianCalendar lastChargeTime= null; public GregorianCalendar lastChargeTime = null;
public BatteryState state = BatteryState.UNKNOWN; public BatteryState state = BatteryState.UNKNOWN;
public short level = 50; public short level = 50;
public int numCharges = -1; public int numCharges = -1;
public GBDeviceEventBatteryInfo() {
eventClass = EventClass.BATTERY_INFO;
}
public boolean extendedInfoAvailable() { public boolean extendedInfoAvailable() {
if (numCharges != -1 && lastChargeTime != null) { if (numCharges != -1 && lastChargeTime != null) {
return true; return true;

View File

@ -4,10 +4,6 @@ package nodomain.freeyourgadget.gadgetbridge.deviceevents;
public class GBDeviceEventCallControl extends GBDeviceEvent { public class GBDeviceEventCallControl extends GBDeviceEvent {
public Event event = Event.UNKNOWN; public Event event = Event.UNKNOWN;
public GBDeviceEventCallControl() {
eventClass = EventClass.CALL_CONTROL;
}
public enum Event { public enum Event {
UNKNOWN, UNKNOWN,
ACCEPT, ACCEPT,

View File

@ -2,8 +2,4 @@ package nodomain.freeyourgadget.gadgetbridge.deviceevents;
public class GBDeviceEventDismissNotification extends GBDeviceEvent { public class GBDeviceEventDismissNotification extends GBDeviceEvent {
public int notificationID; public int notificationID;
public GBDeviceEventDismissNotification() {
eventClass = EventClass.DISMISS_NOTIFICATION;
}
} }

View File

@ -4,10 +4,6 @@ package nodomain.freeyourgadget.gadgetbridge.deviceevents;
public class GBDeviceEventMusicControl extends GBDeviceEvent { public class GBDeviceEventMusicControl extends GBDeviceEvent {
public Event event = Event.UNKNOWN; public Event event = Event.UNKNOWN;
public GBDeviceEventMusicControl() {
eventClass = EventClass.MUSIC_CONTROL;
}
public enum Event { public enum Event {
UNKNOWN, UNKNOWN,
PLAY, PLAY,

View File

@ -6,8 +6,4 @@ public class GBDeviceEventScreenshot extends GBDeviceEvent {
public byte bpp; public byte bpp;
public byte[] clut; public byte[] clut;
public byte[] data; public byte[] data;
public GBDeviceEventScreenshot() {
eventClass = EventClass.SCREENSHOT;
}
} }

View File

@ -2,8 +2,4 @@ package nodomain.freeyourgadget.gadgetbridge.deviceevents;
public class GBDeviceEventSendBytes extends GBDeviceEvent { public class GBDeviceEventSendBytes extends GBDeviceEvent {
public byte[] encodedBytes; public byte[] encodedBytes;
public GBDeviceEventSendBytes() {
eventClass = EventClass.SEND_BYTES;
}
} }

View File

@ -6,8 +6,4 @@ public class GBDeviceEventSleepMonitorResult extends GBDeviceEvent {
public int smartalarm_to = -1;// time in minutes relative from 0:00 for smart alarm (latest) public int smartalarm_to = -1;// time in minutes relative from 0:00 for smart alarm (latest)
public int recording_base_timestamp = -1; // timestamp for the first "point", all folowing are +10 minutes offset each public int recording_base_timestamp = -1; // timestamp for the first "point", all folowing are +10 minutes offset each
public int alarm_gone_off = -1; // time in minutes relative from 0:00 when alarm gone off public int alarm_gone_off = -1; // time in minutes relative from 0:00 when alarm gone off
public GBDeviceEventSleepMonitorResult() {
eventClass = EventClass.SLEEP_MONITOR_RES;
}
} }

View File

@ -6,8 +6,4 @@ import nodomain.freeyourgadget.gadgetbridge.R;
public class GBDeviceEventVersionInfo extends GBDeviceEvent { public class GBDeviceEventVersionInfo extends GBDeviceEvent {
public String fwVersion = GBApplication.getContext().getString(R.string.n_a); public String fwVersion = GBApplication.getContext().getString(R.string.n_a);
public String hwVersion = GBApplication.getContext().getString(R.string.n_a); public String hwVersion = GBApplication.getContext().getString(R.string.n_a);
public GBDeviceEventVersionInfo() {
eventClass = EventClass.VERSION_INFO;
}
} }

View File

@ -19,17 +19,12 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.AppManagerActivity; import nodomain.freeyourgadget.gadgetbridge.activities.AppManagerActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsHost; import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsHost;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryState;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.service.receivers.GBCallControlReceiver;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.service.receivers.GBMusicControlReceiver;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventDismissNotification; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventDismissNotification;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl;
@ -37,6 +32,11 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventScreenshot
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSleepMonitorResult; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSleepMonitorResult;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo;
import nodomain.freeyourgadget.gadgetbridge.externalevents.NotificationListener; import nodomain.freeyourgadget.gadgetbridge.externalevents.NotificationListener;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryState;
import nodomain.freeyourgadget.gadgetbridge.service.receivers.GBCallControlReceiver;
import nodomain.freeyourgadget.gadgetbridge.service.receivers.GBMusicControlReceiver;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
// TODO: support option for a single reminder notification when notifications could not be delivered? // TODO: support option for a single reminder notification when notifications could not be delivered?
// conditions: app was running and received notifications, but device was not connected. // conditions: app was running and received notifications, but device was not connected.
@ -68,6 +68,7 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
/** /**
* Returns true if the device is not only connected, but also * Returns true if the device is not only connected, but also
* initialized. * initialized.
*
* @see GBDevice#isInitialized() * @see GBDevice#isInitialized()
*/ */
protected boolean isInitialized() { protected boolean isInitialized() {
@ -90,34 +91,22 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
} }
public void evaluateGBDeviceEvent(GBDeviceEvent deviceEvent) { public void evaluateGBDeviceEvent(GBDeviceEvent deviceEvent) {
if (deviceEvent instanceof GBDeviceEventMusicControl) {
switch (deviceEvent.eventClass) { handleGBDeviceEvent((GBDeviceEventMusicControl) deviceEvent);
case MUSIC_CONTROL: } else if (deviceEvent instanceof GBDeviceEventCallControl) {
handleGBDeviceEvent((GBDeviceEventMusicControl) deviceEvent); handleGBDeviceEvent((GBDeviceEventCallControl) deviceEvent);
break; } else if (deviceEvent instanceof GBDeviceEventVersionInfo) {
case CALL_CONTROL: handleGBDeviceEvent((GBDeviceEventVersionInfo) deviceEvent);
handleGBDeviceEvent((GBDeviceEventCallControl) deviceEvent); } else if (deviceEvent instanceof GBDeviceEventAppInfo) {
break; handleGBDeviceEvent((GBDeviceEventAppInfo) deviceEvent);
case VERSION_INFO: } else if (deviceEvent instanceof GBDeviceEventSleepMonitorResult) {
handleGBDeviceEvent((GBDeviceEventVersionInfo) deviceEvent); handleGBDeviceEvent((GBDeviceEventSleepMonitorResult) deviceEvent);
break; } else if (deviceEvent instanceof GBDeviceEventScreenshot) {
case APP_INFO: handleGBDeviceEvent((GBDeviceEventScreenshot) deviceEvent);
handleGBDeviceEvent((GBDeviceEventAppInfo) deviceEvent); } else if (deviceEvent instanceof GBDeviceEventDismissNotification) {
break; handleGBDeviceEvent((GBDeviceEventDismissNotification) deviceEvent);
case SLEEP_MONITOR_RES: } else if (deviceEvent instanceof GBDeviceEventBatteryInfo) {
handleGBDeviceEvent((GBDeviceEventSleepMonitorResult) deviceEvent); handleGBDeviceEvent((GBDeviceEventBatteryInfo) deviceEvent);
break;
case SCREENSHOT:
handleGBDeviceEvent((GBDeviceEventScreenshot) deviceEvent);
break;
case DISMISS_NOTIFICATION:
handleGBDeviceEvent((GBDeviceEventDismissNotification) deviceEvent);
break;
case BATTERY_INFO:
handleGBDeviceEvent((GBDeviceEventBatteryInfo) deviceEvent);
break;
default:
break;
} }
} }
@ -239,8 +228,8 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
GB.updateBatteryNotification(context.getString(R.string.notif_battery_low_percent, gbDevice.getName(), deviceEvent.level), GB.updateBatteryNotification(context.getString(R.string.notif_battery_low_percent, gbDevice.getName(), deviceEvent.level),
deviceEvent.extendedInfoAvailable() ? deviceEvent.extendedInfoAvailable() ?
context.getString(R.string.notif_battery_low_percent, gbDevice.getName(), deviceEvent.level) + "\n" + context.getString(R.string.notif_battery_low_percent, gbDevice.getName(), deviceEvent.level) + "\n" +
context.getString(R.string.notif_battery_low_bigtext_last_charge_time, DateFormat.getDateTimeInstance().format(deviceEvent.lastChargeTime.getTime()).toString()) + context.getString(R.string.notif_battery_low_bigtext_last_charge_time, DateFormat.getDateTimeInstance().format(deviceEvent.lastChargeTime.getTime()).toString()) +
context.getString(R.string.notif_battery_low_bigtext_number_of_charges, deviceEvent.numCharges) context.getString(R.string.notif_battery_low_bigtext_number_of_charges, deviceEvent.numCharges)
: "" : ""
, context); , context);
} }

View File

@ -26,6 +26,7 @@ import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppManagement; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppManagement;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo;
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PBWReader; import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PBWReader;
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleInstallable; import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleInstallable;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
@ -320,85 +321,83 @@ public class PebbleIoThread extends GBDeviceIoThread {
// FIXME: parts are supporsed to be generic code // FIXME: parts are supporsed to be generic code
private boolean evaluateGBDeviceEventPebble(GBDeviceEvent deviceEvent) { private boolean evaluateGBDeviceEventPebble(GBDeviceEvent deviceEvent) {
switch (deviceEvent.eventClass) { if (deviceEvent instanceof GBDeviceEventVersionInfo) {
case VERSION_INFO: SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); if (sharedPrefs.getBoolean("datetime_synconconnect", true)) {
if (sharedPrefs.getBoolean("datetime_synconconnect", true)) { LOG.info("syncing time");
LOG.info("syncing time"); write(mPebbleProtocol.encodeSetTime());
write(mPebbleProtocol.encodeSetTime()); }
} gbDevice.setState(GBDevice.State.INITIALIZED);
gbDevice.setState(GBDevice.State.INITIALIZED); return false;
return false; } else if (deviceEvent instanceof GBDeviceEventAppManagement) {
case APP_MANAGEMENT: GBDeviceEventAppManagement appMgmt = (GBDeviceEventAppManagement) deviceEvent;
GBDeviceEventAppManagement appMgmt = (GBDeviceEventAppManagement) deviceEvent; switch (appMgmt.type) {
switch (appMgmt.type) { case DELETE:
case DELETE: // right now on the Pebble we also receive this on a failed/successful installation ;/
// right now on the Pebble we also receive this on a failed/successful installation ;/ switch (appMgmt.event) {
switch (appMgmt.event) { case FAILURE:
case FAILURE: if (mIsInstalling) {
if (mIsInstalling) { if (mInstallState == PebbleAppInstallState.WAIT_SLOT) {
if (mInstallState == PebbleAppInstallState.WAIT_SLOT) { // get the free slot
// get the free slot writeInstallApp(mPebbleProtocol.encodeAppInfoReq());
writeInstallApp(mPebbleProtocol.encodeAppInfoReq());
} else {
finishInstall(true);
}
} else { } else {
LOG.info("failure removing app"); finishInstall(true);
} }
break; } else {
case SUCCESS: LOG.info("failure removing app");
if (mIsInstalling) { }
if (mInstallState == PebbleAppInstallState.WAIT_SLOT) { break;
// get the free slot case SUCCESS:
writeInstallApp(mPebbleProtocol.encodeAppInfoReq()); if (mIsInstalling) {
} else { if (mInstallState == PebbleAppInstallState.WAIT_SLOT) {
finishInstall(false); // get the free slot
// refresh app list writeInstallApp(mPebbleProtocol.encodeAppInfoReq());
write(mPebbleProtocol.encodeAppInfoReq());
}
} else { } else {
LOG.info("successfully removed app"); finishInstall(false);
// refresh app list
write(mPebbleProtocol.encodeAppInfoReq()); write(mPebbleProtocol.encodeAppInfoReq());
} }
break; } else {
default: LOG.info("successfully removed app");
break; write(mPebbleProtocol.encodeAppInfoReq());
} }
break; break;
case INSTALL: default:
switch (appMgmt.event) { break;
case FAILURE: }
LOG.info("failure installing app"); // TODO: report to Installer break;
finishInstall(true); case INSTALL:
break; switch (appMgmt.event) {
case SUCCESS: case FAILURE:
setToken(appMgmt.token); LOG.info("failure installing app"); // TODO: report to Installer
break; finishInstall(true);
case REQUEST: break;
LOG.info("APPFETCH request: " + appMgmt.uuid + " / " + appMgmt.token); case SUCCESS:
try { setToken(appMgmt.token);
installApp(Uri.fromFile(new File(FileUtils.getExternalFilesDir() + "/pbw-cache/" + appMgmt.uuid.toString() + ".pbw")), appMgmt.token); break;
} catch (IOException e) { case REQUEST:
e.printStackTrace(); LOG.info("APPFETCH request: " + appMgmt.uuid + " / " + appMgmt.token);
} try {
break; installApp(Uri.fromFile(new File(FileUtils.getExternalFilesDir() + "/pbw-cache/" + appMgmt.uuid.toString() + ".pbw")), appMgmt.token);
default: } catch (IOException e) {
break; e.printStackTrace();
} }
break; break;
default: default:
break; break;
} }
return true; break;
case APP_INFO: default:
LOG.info("Got event for APP_INFO"); break;
GBDeviceEventAppInfo appInfoEvent = (GBDeviceEventAppInfo) deviceEvent; }
setInstallSlot(appInfoEvent.freeSlot); return true;
return false; } else if (deviceEvent instanceof GBDeviceEventAppInfo) {
default: LOG.info("Got event for APP_INFO");
return false; GBDeviceEventAppInfo appInfoEvent = (GBDeviceEventAppInfo) deviceEvent;
setInstallSlot(appInfoEvent.freeSlot);
return false;
} }
return false;
} }
public void setToken(int token) { public void setToken(int token) {

View File

@ -98,15 +98,10 @@ public abstract class AbstractSerialDeviceSupport extends AbstractDeviceSupport
@Override @Override
public void evaluateGBDeviceEvent(GBDeviceEvent deviceEvent) { public void evaluateGBDeviceEvent(GBDeviceEvent deviceEvent) {
if (deviceEvent instanceof GBDeviceEventSendBytes) {
switch (deviceEvent.eventClass) { handleGBDeviceEvent((GBDeviceEventSendBytes) deviceEvent);
case SEND_BYTES: return;
handleGBDeviceEvent((GBDeviceEventSendBytes) deviceEvent);
return;
default:
break;
} }
super.evaluateGBDeviceEvent(deviceEvent); super.evaluateGBDeviceEvent(deviceEvent);
} }