mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-28 12:56:49 +01:00
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.
This commit is contained in:
parent
36aae1e81c
commit
f99895da0c
@ -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);
|
||||
|
@ -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) {
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<GBDevice> 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<GBDevice> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user