2015-04-14 01:24:03 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge;
|
|
|
|
|
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-06-25 23:34:50 +02:00
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
|
|
|
|
2015-07-11 00:03:41 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractChartFragment;
|
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;
|
2015-07-21 01:25:22 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventDismissNotification;
|
2015-06-23 11:54:33 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl;
|
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-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.
|
|
|
|
* @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-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;
|
2015-06-24 23:55:51 +02:00
|
|
|
case SCREENSHOT:
|
|
|
|
handleGBDeviceEvent((GBDeviceEventScreenshot) deviceEvent);
|
|
|
|
break;
|
2015-07-21 01:25:22 +02:00
|
|
|
case DISMISS_NOTIFICATION:
|
|
|
|
handleGBDeviceEvent((GBDeviceEventDismissNotification) deviceEvent);
|
|
|
|
break;
|
2015-05-23 00:54:14 +02:00
|
|
|
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-07-11 00:03:41 +02:00
|
|
|
Intent sleepMontiorIntent = new Intent(AbstractChartFragment.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-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";
|
|
|
|
|
|
|
|
if (GB.writeScreenshot(screenshot, filename)) {
|
2015-06-27 00:21:30 +02:00
|
|
|
String fullpath = context.getExternalFilesDir(null).toString() + "/" + filename;
|
|
|
|
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-06-24 23:55:51 +02:00
|
|
|
}
|
2015-07-21 01:25:22 +02:00
|
|
|
|
|
|
|
private void handleGBDeviceEvent(GBDeviceEventDismissNotification deviceEvent) {
|
|
|
|
Context context = getContext();
|
|
|
|
LOG.info("Got DISMISS_NOTIFICATION device event");
|
|
|
|
Intent notificationListenerIntent = new Intent(NotificationListener.ACTION_DISMISS);
|
|
|
|
notificationListenerIntent.putExtra("id", deviceEvent.notificationID);
|
|
|
|
LocalBroadcastManager.getInstance(context).sendBroadcast(notificationListenerIntent);
|
|
|
|
}
|
2015-04-14 01:24:03 +02:00
|
|
|
}
|