1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-25 18:15:49 +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 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 GBDeviceApp apps[];
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 UUID uuid = null;
public GBDeviceEventAppManagement() {
eventClass = EventClass.APP_MANAGEMENT;
}
public enum EventType {
UNKNOWN,
INSTALL,

View File

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

View File

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

View File

@ -2,8 +2,4 @@ package nodomain.freeyourgadget.gadgetbridge.deviceevents;
public class GBDeviceEventDismissNotification extends GBDeviceEvent {
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 Event event = Event.UNKNOWN;
public GBDeviceEventMusicControl() {
eventClass = EventClass.MUSIC_CONTROL;
}
public enum Event {
UNKNOWN,
PLAY,

View File

@ -6,8 +6,4 @@ public class GBDeviceEventScreenshot extends GBDeviceEvent {
public byte bpp;
public byte[] clut;
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 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 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 GBDeviceEventSleepMonitorResult() {
eventClass = EventClass.SLEEP_MONITOR_RES;
}
}

View File

@ -6,8 +6,4 @@ import nodomain.freeyourgadget.gadgetbridge.R;
public class GBDeviceEventVersionInfo extends GBDeviceEvent {
public String fwVersion = 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.util.Date;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.AppManagerActivity;
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.GBDeviceEventAppInfo;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventDismissNotification;
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.GBDeviceEventVersionInfo;
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?
// 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
* initialized.
*
* @see GBDevice#isInitialized()
*/
protected boolean isInitialized() {
@ -90,34 +91,22 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
}
public void evaluateGBDeviceEvent(GBDeviceEvent deviceEvent) {
switch (deviceEvent.eventClass) {
case MUSIC_CONTROL:
handleGBDeviceEvent((GBDeviceEventMusicControl) deviceEvent);
break;
case CALL_CONTROL:
handleGBDeviceEvent((GBDeviceEventCallControl) deviceEvent);
break;
case VERSION_INFO:
handleGBDeviceEvent((GBDeviceEventVersionInfo) deviceEvent);
break;
case APP_INFO:
handleGBDeviceEvent((GBDeviceEventAppInfo) deviceEvent);
break;
case SLEEP_MONITOR_RES:
handleGBDeviceEvent((GBDeviceEventSleepMonitorResult) deviceEvent);
break;
case SCREENSHOT:
handleGBDeviceEvent((GBDeviceEventScreenshot) deviceEvent);
break;
case DISMISS_NOTIFICATION:
handleGBDeviceEvent((GBDeviceEventDismissNotification) deviceEvent);
break;
case BATTERY_INFO:
handleGBDeviceEvent((GBDeviceEventBatteryInfo) deviceEvent);
break;
default:
break;
if (deviceEvent instanceof GBDeviceEventMusicControl) {
handleGBDeviceEvent((GBDeviceEventMusicControl) deviceEvent);
} else if (deviceEvent instanceof GBDeviceEventCallControl) {
handleGBDeviceEvent((GBDeviceEventCallControl) deviceEvent);
} else if (deviceEvent instanceof GBDeviceEventVersionInfo) {
handleGBDeviceEvent((GBDeviceEventVersionInfo) deviceEvent);
} else if (deviceEvent instanceof GBDeviceEventAppInfo) {
handleGBDeviceEvent((GBDeviceEventAppInfo) deviceEvent);
} else if (deviceEvent instanceof GBDeviceEventSleepMonitorResult) {
handleGBDeviceEvent((GBDeviceEventSleepMonitorResult) deviceEvent);
} else if (deviceEvent instanceof GBDeviceEventScreenshot) {
handleGBDeviceEvent((GBDeviceEventScreenshot) deviceEvent);
} else if (deviceEvent instanceof GBDeviceEventDismissNotification) {
handleGBDeviceEvent((GBDeviceEventDismissNotification) deviceEvent);
} else if (deviceEvent instanceof GBDeviceEventBatteryInfo) {
handleGBDeviceEvent((GBDeviceEventBatteryInfo) deviceEvent);
}
}
@ -239,8 +228,8 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
GB.updateBatteryNotification(context.getString(R.string.notif_battery_low_percent, gbDevice.getName(), deviceEvent.level),
deviceEvent.extendedInfoAvailable() ?
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_number_of_charges, deviceEvent.numCharges)
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);
}

View File

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

View File

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