2015-08-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service;
|
2015-04-14 01:24:03 +02:00
|
|
|
|
2015-06-26 23:17:31 +02:00
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationManager;
|
2015-06-27 00:21:30 +02:00
|
|
|
import android.app.PendingIntent;
|
2015-04-14 01:24:03 +02:00
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
|
import android.content.Context;
|
2015-05-23 00:54:14 +02:00
|
|
|
import android.content.Intent;
|
2015-06-26 23:17:31 +02:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
2015-06-27 00:21:30 +02:00
|
|
|
import android.net.Uri;
|
2015-05-23 00:54:14 +02:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-06-27 00:21:30 +02:00
|
|
|
import java.io.File;
|
2015-10-28 23:54:08 +01:00
|
|
|
import java.io.IOException;
|
2015-08-18 17:37:51 +02:00
|
|
|
import java.text.DateFormat;
|
2015-06-25 23:34:50 +02:00
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
|
|
|
|
2015-08-31 17:25:58 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.AppManagerActivity;
|
2015-08-17 02:22:16 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsHost;
|
2015-06-23 11:54:33 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo;
|
2015-08-31 17:25:58 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo;
|
2015-06-23 11:54:33 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl;
|
2015-08-31 22:27:25 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventNotificationControl;
|
2015-06-24 23:55:51 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventScreenshot;
|
2015-06-23 11:54:33 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSleepMonitorResult;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo;
|
2015-07-21 01:25:22 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.NotificationListener;
|
2015-08-31 17:25:58 +02:00
|
|
|
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;
|
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-08-02 00:12:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract implementation of DeviceSupport with some implementations for
|
|
|
|
* common functionality. Still transport independent.
|
|
|
|
*/
|
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-06-26 23:17:31 +02:00
|
|
|
private static final int NOTIFICATION_ID_SCREENSHOT = 8000;
|
2015-05-23 00:54:14 +02:00
|
|
|
|
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;
|
|
|
|
|
2015-08-02 00:12:21 +02:00
|
|
|
public void setContext(GBDevice gbDevice, BluetoothAdapter btAdapter, Context context) {
|
2015-04-14 01:24:03 +02:00
|
|
|
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-08-02 00:12:21 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the device is not only connected, but also
|
|
|
|
* initialized.
|
2015-08-31 17:25:58 +02:00
|
|
|
*
|
2015-08-02 00:12:21 +02:00
|
|
|
* @see GBDevice#isInitialized()
|
|
|
|
*/
|
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-08-31 17:25:58 +02:00
|
|
|
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);
|
2015-08-31 22:27:25 +02:00
|
|
|
} else if (deviceEvent instanceof GBDeviceEventNotificationControl) {
|
|
|
|
handleGBDeviceEvent((GBDeviceEventNotificationControl) deviceEvent);
|
2015-08-31 17:25:58 +02:00
|
|
|
} else if (deviceEvent instanceof GBDeviceEventBatteryInfo) {
|
|
|
|
handleGBDeviceEvent((GBDeviceEventBatteryInfo) deviceEvent);
|
2015-05-23 00:54:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-17 02:22:16 +02:00
|
|
|
Intent sleepMontiorIntent = new Intent(ChartsHost.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-06-24 23:55:51 +02:00
|
|
|
|
|
|
|
private void handleGBDeviceEvent(GBDeviceEventScreenshot screenshot) {
|
2015-06-25 23:34:50 +02:00
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-hhmmss");
|
2015-06-26 23:17:31 +02:00
|
|
|
String filename = "screenshot_" + dateFormat.format(new Date()) + ".bmp";
|
|
|
|
|
2015-10-28 23:54:08 +01:00
|
|
|
try {
|
|
|
|
String fullpath = GB.writeScreenshot(screenshot, filename);
|
2015-06-27 00:21:30 +02:00
|
|
|
Bitmap bmp = BitmapFactory.decodeFile(fullpath);
|
|
|
|
Intent intent = new Intent();
|
|
|
|
intent.setAction(android.content.Intent.ACTION_VIEW);
|
|
|
|
intent.setDataAndType(Uri.fromFile(new File(fullpath)), "image/*");
|
|
|
|
|
|
|
|
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
2015-06-26 23:17:31 +02:00
|
|
|
|
2015-06-27 18:09:49 +02:00
|
|
|
Intent shareIntent = new Intent(Intent.ACTION_SEND);
|
|
|
|
shareIntent.setType("image/*");
|
|
|
|
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(fullpath)));
|
|
|
|
|
|
|
|
PendingIntent pendingShareIntent = PendingIntent.getActivity(context, 0, Intent.createChooser(shareIntent, "share screenshot"),
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
2015-06-26 23:17:31 +02:00
|
|
|
Notification notif = new Notification.Builder(context)
|
|
|
|
.setContentTitle("Screenshot taken")
|
|
|
|
.setTicker("Screenshot taken")
|
|
|
|
.setContentText(filename)
|
|
|
|
.setSmallIcon(R.drawable.ic_notification)
|
|
|
|
.setStyle(new Notification.BigPictureStyle()
|
|
|
|
.bigPicture(bmp))
|
2015-06-27 00:21:30 +02:00
|
|
|
.setContentIntent(pIntent)
|
2015-06-27 18:09:49 +02:00
|
|
|
.addAction(android.R.drawable.ic_menu_share, "share", pendingShareIntent)
|
2015-06-26 23:17:31 +02:00
|
|
|
.build();
|
2015-06-27 18:09:49 +02:00
|
|
|
|
|
|
|
|
2015-06-27 00:21:30 +02:00
|
|
|
notif.flags |= Notification.FLAG_AUTO_CANCEL;
|
|
|
|
|
2015-06-26 23:17:31 +02:00
|
|
|
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
nm.notify(NOTIFICATION_ID_SCREENSHOT, notif);
|
2015-10-28 23:54:08 +01:00
|
|
|
} catch (IOException ex) {
|
|
|
|
LOG.error("Error writing screenshot", ex);
|
2015-06-26 23:17:31 +02:00
|
|
|
}
|
2015-06-24 23:55:51 +02:00
|
|
|
}
|
2015-07-21 01:25:22 +02:00
|
|
|
|
2015-08-31 22:27:25 +02:00
|
|
|
private void handleGBDeviceEvent(GBDeviceEventNotificationControl deviceEvent) {
|
2015-07-21 01:25:22 +02:00
|
|
|
Context context = getContext();
|
2015-08-31 22:27:25 +02:00
|
|
|
LOG.info("Got NOTIFICATION CONTROL device event");
|
|
|
|
String action = null;
|
|
|
|
switch (deviceEvent.event) {
|
|
|
|
case DISMISS:
|
|
|
|
action = NotificationListener.ACTION_DISMISS;
|
|
|
|
break;
|
2015-09-01 21:58:36 +02:00
|
|
|
case DISMISS_ALL:
|
|
|
|
action = NotificationListener.ACTION_DISMISS_ALL;
|
|
|
|
break;
|
2015-08-31 22:27:25 +02:00
|
|
|
case OPEN:
|
|
|
|
action = NotificationListener.ACTION_OPEN;
|
|
|
|
break;
|
2015-09-25 00:53:40 +02:00
|
|
|
case MUTE:
|
|
|
|
action = NotificationListener.ACTION_MUTE;
|
|
|
|
break;
|
2015-12-13 22:43:53 +01:00
|
|
|
case REPLY:
|
|
|
|
GB.toast(context, "got notfication reply: " + deviceEvent.reply, 2, GB.INFO);
|
|
|
|
break;
|
2015-08-31 22:27:25 +02:00
|
|
|
}
|
|
|
|
if (action != null) {
|
|
|
|
Intent notificationListenerIntent = new Intent(action);
|
|
|
|
notificationListenerIntent.putExtra("handle", deviceEvent.handle);
|
|
|
|
LocalBroadcastManager.getInstance(context).sendBroadcast(notificationListenerIntent);
|
|
|
|
}
|
2015-07-21 01:25:22 +02:00
|
|
|
}
|
2015-08-18 17:37:51 +02:00
|
|
|
|
|
|
|
public void handleGBDeviceEvent(GBDeviceEventBatteryInfo deviceEvent) {
|
|
|
|
Context context = getContext();
|
|
|
|
LOG.info("Got BATTERY_INFO device event");
|
|
|
|
gbDevice.setBatteryLevel(deviceEvent.level);
|
2015-08-21 08:41:57 +02:00
|
|
|
gbDevice.setBatteryState(deviceEvent.state);
|
2015-08-18 17:37:51 +02:00
|
|
|
|
2015-08-21 08:41:57 +02:00
|
|
|
//show the notification if the battery level is below threshold and only if not connected to charger
|
|
|
|
if (deviceEvent.level <= gbDevice.getBatteryThresholdPercent() &&
|
2015-08-27 11:50:31 +02:00
|
|
|
(BatteryState.BATTERY_LOW.equals(deviceEvent.state) ||
|
|
|
|
BatteryState.BATTERY_NORMAL.equals(deviceEvent.state))
|
2015-08-21 08:41:57 +02:00
|
|
|
) {
|
2015-08-19 17:36:53 +02:00
|
|
|
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" +
|
2015-11-23 22:46:12 +01:00
|
|
|
context.getString(R.string.notif_battery_low_bigtext_last_charge_time, DateFormat.getDateTimeInstance().format(deviceEvent.lastChargeTime.getTime())) +
|
2015-08-31 17:25:58 +02:00
|
|
|
context.getString(R.string.notif_battery_low_bigtext_number_of_charges, deviceEvent.numCharges)
|
2015-08-19 17:36:53 +02:00
|
|
|
: ""
|
2015-08-18 17:37:51 +02:00
|
|
|
, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
gbDevice.sendDeviceUpdateIntent(context);
|
|
|
|
}
|
|
|
|
|
2015-04-14 01:24:03 +02:00
|
|
|
}
|