From f99895da0cfbfaa874fca76752e69a8148ab7550 Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Tue, 19 Jun 2018 22:03:49 +0200 Subject: [PATCH] Remove activity transfer toast, add title to the transfer notification Also create a separate channel that allows "muting" transfer notifications on android oreo and above. --- .../operations/AbstractFetchOperation.java | 7 +++-- .../operations/FetchActivityOperation.java | 11 ++++--- .../service/devices/no1f1/No1F1Support.java | 8 ++--- .../receivers/GBAutoFetchReceiver.java | 31 ++++++++++++------- .../freeyourgadget/gadgetbridge/util/GB.java | 27 +++++++++++++--- 5 files changed, 55 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/miband2/operations/AbstractFetchOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/miband2/operations/AbstractFetchOperation.java index f31648aa4..2295f07b2 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/miband2/operations/AbstractFetchOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/miband2/operations/AbstractFetchOperation.java @@ -21,7 +21,6 @@ import android.bluetooth.BluetoothGattCharacteristic; import android.content.SharedPreferences; import android.support.annotation.CallSuper; import android.support.annotation.NonNull; -import android.widget.Toast; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -118,6 +117,7 @@ public abstract class AbstractFetchOperation extends AbstractMiBand2Operation { @CallSuper protected void handleActivityFetchFinish(boolean success) { + GB.updateTransferNotification(null,"",false,100,getContext()); operationFinished(); unsetBusy(); } @@ -148,8 +148,9 @@ public abstract class AbstractFetchOperation extends AbstractMiBand2Operation { Calendar startTimestamp = getSupport().fromTimeBytes(Arrays.copyOfRange(value, 7, value.length)); setStartTimestamp(startTimestamp); - GB.toast(getContext().getString(R.string.FetchActivityOperation_about_to_transfer_since, - DateFormat.getDateTimeInstance().format(startTimestamp.getTime())), Toast.LENGTH_LONG, GB.INFO); + GB.updateTransferNotification(getContext().getString(R.string.busy_task_fetch_activity_data), + getContext().getString(R.string.FetchActivityOperation_about_to_transfer_since, + DateFormat.getDateTimeInstance().format(startTimestamp.getTime())), true, 0, getContext());; } else { LOG.warn("Unexpected activity metadata: " + Logging.formatBytes(value)); handleActivityFetchFinish(false); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java index a09601cdb..52117d8d7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java @@ -236,12 +236,12 @@ public class FetchActivityOperation extends AbstractMiBand1Operation { // avoid too many notifications overloading the system if (progress - activityStruct.lastNotifiedProgress >= 8) { activityStruct.lastNotifiedProgress = progress; - GB.updateTransferNotification(getContext().getString(R.string.busy_task_fetch_activity_data), true, progress, getContext()); + GB.updateTransferNotification(null, getContext().getString(R.string.busy_task_fetch_activity_data), true, progress, getContext()); } if (activityStruct.isBlockFinished()) { sendAckDataTransfer(activityStruct.activityDataTimestampToAck, activityStruct.activityDataUntilNextHeader); - GB.updateTransferNotification("", false, 100, getContext()); + GB.updateTransferNotification(null, "", false, 100, getContext()); } } @@ -271,9 +271,10 @@ public class FetchActivityOperation extends AbstractMiBand1Operation { // as we just did if (activityStruct.isFirstChunk() && dataUntilNextHeader != 0) { - GB.toast(getContext().getString(R.string.user_feedback_miband_activity_data_transfer, + + GB.updateTransferNotification(getContext().getString(R.string.busy_task_fetch_activity_data), getContext().getString(R.string.user_feedback_miband_activity_data_transfer, DateTimeUtils.formatDurationHoursMinutes((totalDataToRead / getBytesPerMinuteOfActivityData()), TimeUnit.MINUTES), - DateFormat.getDateTimeInstance().format(timestamp.getTime())), Toast.LENGTH_LONG, GB.INFO); + DateFormat.getDateTimeInstance().format(timestamp.getTime())), true, 0, getContext()); } LOG.info("total data to read: " + totalDataToRead + " len: " + (totalDataToRead / getBytesPerMinuteOfActivityData()) + " minute(s)"); LOG.info("data to read until next header: " + dataUntilNextHeader + " len: " + (dataUntilNextHeader / getBytesPerMinuteOfActivityData()) + " minute(s)"); @@ -326,7 +327,7 @@ public class FetchActivityOperation extends AbstractMiBand1Operation { TransactionBuilder builder = performInitialized("send stop sync data"); builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT), new byte[]{MiBandService.COMMAND_STOP_SYNC_DATA}); builder.queue(getQueue()); - GB.updateTransferNotification("Data transfer failed", false, 0, getContext()); + GB.updateTransferNotification(null,"Data transfer failed", false, 0, getContext()); handleActivityFetchFinish(); } catch (IOException e) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/no1f1/No1F1Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/no1f1/No1F1Support.java index 393ff82ff..d09a2a3cd 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/no1f1/No1F1Support.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/no1f1/No1F1Support.java @@ -582,7 +582,7 @@ public class No1F1Support extends AbstractBTLEDeviceSupport { LOG.info("CRC received: " + (data[2] & 0xff) + ", calculated: " + (crc & 0xff)); if (data[2] != crc) { GB.toast(getContext(), "Incorrect CRC. Try fetching data again.", Toast.LENGTH_LONG, GB.ERROR); - GB.updateTransferNotification("Data transfer failed", false, 0, getContext()); + GB.updateTransferNotification(null,"Data transfer failed", false, 0, getContext()); if (getDevice().isBusy()) { getDevice().unsetBusyTask(); getDevice().sendDeviceUpdateIntent(getContext()); @@ -612,7 +612,7 @@ public class No1F1Support extends AbstractBTLEDeviceSupport { } else if (data[0] == No1F1Constants.CMD_FETCH_SLEEP) { sendFetchCommand(No1F1Constants.CMD_FETCH_HEARTRATE); } else { - GB.updateTransferNotification("", false, 100, getContext()); + GB.updateTransferNotification(null,"", false, 100, getContext()); if (getDevice().isBusy()) { getDevice().unsetBusyTask(); getDevice().sendDeviceUpdateIntent(getContext()); @@ -620,7 +620,7 @@ public class No1F1Support extends AbstractBTLEDeviceSupport { } } catch (Exception ex) { GB.toast(getContext(), "Error saving activity data: " + ex.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); - GB.updateTransferNotification("Data transfer failed", false, 0, getContext()); + GB.updateTransferNotification(null,"Data transfer failed", false, 0, getContext()); } } } else { @@ -657,7 +657,7 @@ public class No1F1Support extends AbstractBTLEDeviceSupport { firstTimestamp = sample.getTimestamp(); int progress = startProgress + 33 * (sample.getTimestamp() - firstTimestamp) / ((int) (Calendar.getInstance().getTimeInMillis() / 1000L) - firstTimestamp); - GB.updateTransferNotification(getContext().getString(R.string.busy_task_fetch_activity_data), true, progress, getContext()); + GB.updateTransferNotification(null, getContext().getString(R.string.busy_task_fetch_activity_data), true, progress, getContext()); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/GBAutoFetchReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/GBAutoFetchReceiver.java index 7d9a5b807..8201b8565 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/GBAutoFetchReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/GBAutoFetchReceiver.java @@ -3,6 +3,10 @@ package nodomain.freeyourgadget.gadgetbridge.service.receivers; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.List; import nodomain.freeyourgadget.gadgetbridge.GBApplication; @@ -13,20 +17,23 @@ import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; public class GBAutoFetchReceiver extends BroadcastReceiver { - @Override - public void onReceive(Context context, Intent intent) { - GBApplication application = (GBApplication) context; - List devices = application.getDeviceManager().getDevices(); - for (int i = 0; i < devices.size(); i++) { - GBDevice device = devices.get(i); - // Will show that the device is not connected even when the device is connected - if (device.isConnected() && device.isInitialized()) { - DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device); - if (coordinator.supportsActivityDataFetching() && !device.isBusy()) { - application.deviceService().onFetchRecordedData(RecordedDataTypes.TYPE_ACTIVITY); - } + private static final Logger LOG = LoggerFactory.getLogger(GBAutoFetchReceiver.class); + + @Override + public void onReceive(Context context, Intent intent) { + //LOG.info("User is present!"); + GBApplication application = (GBApplication) context; + List devices = application.getDeviceManager().getDevices(); + for (int i = 0; i < devices.size(); i++) { + GBDevice device = devices.get(i); + // Will show that the device is not connected even when the device is connected + if (device.isInitialized()) { + DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device); + if (coordinator.supportsActivityDataFetching() && !device.isBusy()) { + application.deviceService().onFetchRecordedData(RecordedDataTypes.TYPE_ACTIVITY); } } } } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java index 3643549a7..1af2c4e89 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java @@ -19,6 +19,8 @@ package nodomain.freeyourgadget.gadgetbridge.util; import android.app.Activity; import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.app.PendingIntent; import android.bluetooth.BluetoothAdapter; import android.content.Context; @@ -50,9 +52,12 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.model.DeviceService; import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService; +import static nodomain.freeyourgadget.gadgetbridge.GBApplication.isRunningOreoOrLater; + public class GB { public static final String NOTIFICATION_CHANNEL_ID = "gadgetbridge"; + public static final String NOTIFICATION_CHANNEL_ID_TRANSFER = "gadgetbridge transfer"; public static final int NOTIFICATION_ID = 1; public static final int NOTIFICATION_ID_INSTALL = 2; @@ -323,17 +328,29 @@ public class GB { } } - private static Notification createTransferNotification(String text, boolean ongoing, + private static Notification createTransferNotification(String title, String text, boolean ongoing, int percentage, Context context) { Intent notificationIntent = new Intent(context, ControlCenterv2.class); + NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + if(isRunningOreoOrLater()) { + NotificationChannel channel = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID_TRANSFER); + if(channel == null) { + channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID_TRANSFER, + context.getString(R.string.notification_channel_name), + NotificationManager.IMPORTANCE_LOW); + notificationManager.createNotificationChannel(channel); + } + } notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); - NotificationCompat.Builder nb = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID) + NotificationCompat.Builder nb = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID_TRANSFER) + .setTicker((title == null) ? context.getString(R.string.app_name) : title) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) - .setContentTitle(context.getString(R.string.app_name)) + .setContentTitle((title == null) ? context.getString(R.string.app_name) : title) + .setStyle(new NotificationCompat.BigTextStyle().bigText(text)) .setContentText(text) .setContentIntent(pendingIntent) .setOngoing(ongoing); @@ -355,11 +372,11 @@ public class GB { removeNotification(NOTIFICATION_ID_LOW_BATTERY, context); } - public static void updateTransferNotification(String text, boolean ongoing, int percentage, Context context) { + public static void updateTransferNotification(String title, String text, boolean ongoing, int percentage, Context context) { if (percentage == 100) { removeNotification(NOTIFICATION_ID_TRANSFER, context); } else { - Notification notification = createTransferNotification(text, ongoing, percentage, context); + Notification notification = createTransferNotification(title, text, ongoing, percentage, context); updateNotification(notification, NOTIFICATION_ID_TRANSFER, context); } }