2015-04-14 01:24:03 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge;
|
|
|
|
|
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
|
import android.content.Context;
|
2015-05-23 00:54:14 +02:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-06-17 08:18:24 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.SleepChartActivity;
|
2015-06-23 11:54:33 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSleepMonitorResult;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo;
|
2015-04-14 01:24:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
// 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.
|
|
|
|
// maybe need to check for "unread notifications" on device for that.
|
2015-04-14 01:24:03 +02:00
|
|
|
public abstract class AbstractDeviceSupport implements DeviceSupport {
|
2015-05-23 00:54:14 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(AbstractDeviceSupport.class);
|
|
|
|
|
2015-05-22 20:20:33 +02:00
|
|
|
protected GBDevice gbDevice;
|
2015-04-14 01:24:03 +02:00
|
|
|
private BluetoothAdapter btAdapter;
|
|
|
|
private Context context;
|
|
|
|
|
|
|
|
public void initialize(GBDevice gbDevice, BluetoothAdapter btAdapter, Context context) {
|
|
|
|
this.gbDevice = gbDevice;
|
|
|
|
this.btAdapter = btAdapter;
|
|
|
|
this.context = context;
|
|
|
|
}
|
2015-04-14 10:29:09 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
@Override
|
|
|
|
public boolean isConnected() {
|
|
|
|
return gbDevice.isConnected();
|
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
protected boolean isInitialized() {
|
|
|
|
return gbDevice.isInitialized();
|
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-14 01:24:03 +02:00
|
|
|
@Override
|
|
|
|
public GBDevice getDevice() {
|
|
|
|
return gbDevice;
|
|
|
|
}
|
2015-04-14 10:29:09 +02:00
|
|
|
|
2015-04-14 01:24:03 +02:00
|
|
|
@Override
|
|
|
|
public BluetoothAdapter getBluetoothAdapter() {
|
|
|
|
return btAdapter;
|
|
|
|
}
|
2015-04-14 10:29:09 +02:00
|
|
|
|
2015-04-14 01:24:03 +02:00
|
|
|
@Override
|
|
|
|
public Context getContext() {
|
|
|
|
return context;
|
|
|
|
}
|
2015-05-23 00:54:14 +02:00
|
|
|
|
2015-06-23 11:54:33 +02:00
|
|
|
public void evaluateGBDeviceEvent(GBDeviceEvent deviceEvent) {
|
2015-05-23 00:54:14 +02:00
|
|
|
|
2015-06-23 11:54:33 +02:00
|
|
|
switch (deviceEvent.eventClass) {
|
2015-05-23 00:54:14 +02:00
|
|
|
case MUSIC_CONTROL:
|
2015-06-23 11:54:33 +02:00
|
|
|
handleGBDeviceEvent((GBDeviceEventMusicControl) deviceEvent);
|
2015-05-23 00:54:14 +02:00
|
|
|
break;
|
|
|
|
case CALL_CONTROL:
|
2015-06-23 11:54:33 +02:00
|
|
|
handleGBDeviceEvent((GBDeviceEventCallControl) deviceEvent);
|
2015-05-23 00:54:14 +02:00
|
|
|
break;
|
|
|
|
case VERSION_INFO:
|
2015-06-23 11:54:33 +02:00
|
|
|
handleGBDeviceEvent((GBDeviceEventVersionInfo) deviceEvent);
|
2015-05-23 00:54:14 +02:00
|
|
|
break;
|
|
|
|
case APP_INFO:
|
2015-06-23 11:54:33 +02:00
|
|
|
handleGBDeviceEvent((GBDeviceEventAppInfo) deviceEvent);
|
2015-05-23 00:54:14 +02:00
|
|
|
break;
|
|
|
|
case SLEEP_MONITOR_RES:
|
2015-06-23 11:54:33 +02:00
|
|
|
handleGBDeviceEvent((GBDeviceEventSleepMonitorResult) deviceEvent);
|
2015-05-23 00:54:14 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:54:33 +02:00
|
|
|
public void handleGBDeviceEvent(GBDeviceEventMusicControl musicEvent) {
|
2015-05-23 00:54:14 +02:00
|
|
|
Context context = getContext();
|
2015-06-23 11:54:33 +02:00
|
|
|
LOG.info("Got event for MUSIC_CONTROL");
|
2015-05-23 00:54:14 +02:00
|
|
|
Intent musicIntent = new Intent(GBMusicControlReceiver.ACTION_MUSICCONTROL);
|
2015-06-23 11:54:33 +02:00
|
|
|
musicIntent.putExtra("event", musicEvent.event.ordinal());
|
2015-05-23 00:54:14 +02:00
|
|
|
musicIntent.setPackage(context.getPackageName());
|
|
|
|
context.sendBroadcast(musicIntent);
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:54:33 +02:00
|
|
|
public void handleGBDeviceEvent(GBDeviceEventCallControl callEvent) {
|
2015-05-23 00:54:14 +02:00
|
|
|
Context context = getContext();
|
2015-06-23 11:54:33 +02:00
|
|
|
LOG.info("Got event for CALL_CONTROL");
|
2015-05-23 00:54:14 +02:00
|
|
|
Intent callIntent = new Intent(GBCallControlReceiver.ACTION_CALLCONTROL);
|
2015-06-23 11:54:33 +02:00
|
|
|
callIntent.putExtra("event", callEvent.event.ordinal());
|
2015-05-23 00:54:14 +02:00
|
|
|
callIntent.setPackage(context.getPackageName());
|
|
|
|
context.sendBroadcast(callIntent);
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:54:33 +02:00
|
|
|
public void handleGBDeviceEvent(GBDeviceEventVersionInfo infoEvent) {
|
2015-05-23 00:54:14 +02:00
|
|
|
Context context = getContext();
|
2015-06-23 11:54:33 +02:00
|
|
|
LOG.info("Got event for VERSION_INFO");
|
2015-05-23 00:54:14 +02:00
|
|
|
if (gbDevice == null) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-23 11:54:33 +02:00
|
|
|
gbDevice.setFirmwareVersion(infoEvent.fwVersion);
|
|
|
|
gbDevice.setHardwareVersion(infoEvent.hwVersion);
|
2015-05-23 00:54:14 +02:00
|
|
|
gbDevice.sendDeviceUpdateIntent(context);
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:54:33 +02:00
|
|
|
public void handleGBDeviceEvent(GBDeviceEventAppInfo appInfoEvent) {
|
2015-05-23 00:54:14 +02:00
|
|
|
Context context = getContext();
|
2015-06-23 11:54:33 +02:00
|
|
|
LOG.info("Got event for APP_INFO");
|
2015-05-23 00:54:14 +02:00
|
|
|
|
|
|
|
Intent appInfoIntent = new Intent(AppManagerActivity.ACTION_REFRESH_APPLIST);
|
2015-06-23 11:54:33 +02:00
|
|
|
int appCount = appInfoEvent.apps.length;
|
2015-05-23 00:54:14 +02:00
|
|
|
appInfoIntent.putExtra("app_count", appCount);
|
|
|
|
for (Integer i = 0; i < appCount; i++) {
|
2015-06-23 11:54:33 +02:00
|
|
|
appInfoIntent.putExtra("app_name" + i.toString(), appInfoEvent.apps[i].getName());
|
|
|
|
appInfoIntent.putExtra("app_creator" + i.toString(), appInfoEvent.apps[i].getCreator());
|
|
|
|
appInfoIntent.putExtra("app_uuid" + i.toString(), appInfoEvent.apps[i].getUUID().toString());
|
|
|
|
appInfoIntent.putExtra("app_type" + i.toString(), appInfoEvent.apps[i].getType().ordinal());
|
2015-05-23 00:54:14 +02:00
|
|
|
}
|
|
|
|
LocalBroadcastManager.getInstance(context).sendBroadcast(appInfoIntent);
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:54:33 +02:00
|
|
|
public void handleGBDeviceEvent(GBDeviceEventSleepMonitorResult sleepMonitorResult) {
|
2015-05-23 00:54:14 +02:00
|
|
|
Context context = getContext();
|
2015-06-23 11:54:33 +02:00
|
|
|
LOG.info("Got event for SLEEP_MONIOR_RES");
|
2015-06-17 08:18:24 +02:00
|
|
|
Intent sleepMontiorIntent = new Intent(SleepChartActivity.ACTION_REFRESH);
|
2015-05-23 00:54:14 +02:00
|
|
|
sleepMontiorIntent.putExtra("smartalarm_from", sleepMonitorResult.smartalarm_from);
|
|
|
|
sleepMontiorIntent.putExtra("smartalarm_to", sleepMonitorResult.smartalarm_to);
|
|
|
|
sleepMontiorIntent.putExtra("recording_base_timestamp", sleepMonitorResult.recording_base_timestamp);
|
|
|
|
sleepMontiorIntent.putExtra("alarm_gone_off", sleepMonitorResult.alarm_gone_off);
|
|
|
|
|
|
|
|
LocalBroadcastManager.getInstance(context).sendBroadcast(sleepMontiorIntent);
|
|
|
|
}
|
2015-04-14 01:24:03 +02:00
|
|
|
}
|