Skip to content

Commit

Permalink
Workaround ongoing notifications that can be closed by user on androi…
Browse files Browse the repository at this point in the history
…d 14 (#485)
  • Loading branch information
frimtec authored Dec 15, 2023
1 parent 84ccb2a commit 324c701
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.github.frimtec.android.pikettassist.service;

import static com.github.frimtec.android.pikettassist.service.system.NotificationService.ACTION_CLOSE_ALARM;
import static com.github.frimtec.android.pikettassist.service.system.NotificationService.ACTION_LOW_BATTERY_NOTIFICATION_CLOSED_BY_USER;
import static com.github.frimtec.android.pikettassist.service.system.NotificationService.ACTION_ON_CALL_NOTIFICATION_CLOSED_BY_USER;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.github.frimtec.android.pikettassist.action.Action;

import static com.github.frimtec.android.pikettassist.service.system.NotificationService.ACTION_CLOSE_ALARM;
import com.github.frimtec.android.pikettassist.domain.BatteryStatus;
import com.github.frimtec.android.pikettassist.service.system.BatteryService;
import com.github.frimtec.android.pikettassist.service.system.NotificationService;
import com.github.frimtec.android.pikettassist.state.ApplicationPreferences;

public class NotificationActionListener extends BroadcastReceiver {

Expand All @@ -17,11 +23,19 @@ public class NotificationActionListener extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null) {
if (ACTION_CLOSE_ALARM.equals(action)) {
new AlertService(context).closeAlert();
context.sendBroadcast(new Intent(Action.REFRESH.getId()));
} else {
Log.e(TAG, "Unknown action: " + action);
switch (action) {
case ACTION_CLOSE_ALARM -> {
new AlertService(context).closeAlert();
context.sendBroadcast(new Intent(Action.REFRESH.getId()));
}
case ACTION_ON_CALL_NOTIFICATION_CLOSED_BY_USER -> PikettWorker.enqueueWork(context);
case ACTION_LOW_BATTERY_NOTIFICATION_CLOSED_BY_USER -> {
BatteryStatus batteryStatus = new BatteryService(context).batteryStatus();
if (batteryStatus.level() <= ApplicationPreferences.instance().getBatteryWarnLevel(context)) {
new NotificationService(context).notifyBatteryLow(batteryStatus);
}
}
default -> Log.e(TAG, "Unknown action: " + action);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.github.frimtec.android.pikettassist.R;
import com.github.frimtec.android.pikettassist.domain.BatteryStatus;
import com.github.frimtec.android.pikettassist.domain.TestAlarmContext;
import com.github.frimtec.android.pikettassist.service.NotificationActionListener;
import com.github.frimtec.android.pikettassist.service.system.SignalStrengthService.SignalLevel;
import com.github.frimtec.android.pikettassist.ui.MainActivity;

Expand All @@ -47,6 +48,8 @@ public class NotificationService {
public static final int BATTERY_NOTIFICATION_ID = 5;

public static final String ACTION_CLOSE_ALARM = "com.github.frimtec.android.pikettassist.CLOSE_ALARM";
public static final String ACTION_ON_CALL_NOTIFICATION_CLOSED_BY_USER = "com.github.frimtec.android.pikettassist.ON_CALL_NOTIFICATION_CLOSED_BY_USER";
public static final String ACTION_LOW_BATTERY_NOTIFICATION_CLOSED_BY_USER = "com.github.frimtec.android.pikettassist.LOW_BATTERY_NOTIFICATION_CLOSED_BY_USER";

private static final String CHANNEL_ID_ALARM = "com.github.frimtec.android.pikettassist.alarm";
private static final String CHANNEL_ID_NOTIFICATION = "com.github.frimtec.android.pikettassist.notification";
Expand Down Expand Up @@ -156,14 +159,21 @@ public void notifyShiftOn(Progress progress) {
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.notification_large_icon))
.setCategory(CATEGORY_EVENT)
.setOnlyAlertOnce(true)
.setContentIntent(notifyPendingIntent)
.setOngoing(true)
.setContentIntent(notifyPendingIntent);
.setDeleteIntent(getDeleteIntent(ACTION_ON_CALL_NOTIFICATION_CLOSED_BY_USER));
if (progress != null) {
notificationBuilder.setProgress(progress.getMax(), progress.getProgress(), false);
}
notifyIfAllowed(context, SHIFT_NOTIFICATION_ID, notificationBuilder.build());
}

protected PendingIntent getDeleteIntent(String action) {
Intent intent = new Intent(context, NotificationActionListener.class);
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
}

public void notifySignalLow(SignalLevel level) {
PendingIntent notifyPendingIntent = PendingIntent.getActivity(
context, 0, new Intent(context, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
Expand Down Expand Up @@ -193,8 +203,9 @@ public void notifyBatteryLow(BatteryStatus batteryStatus) {
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.notification_large_icon))
.setCategory(CATEGORY_EVENT)
.setOnlyAlertOnce(true)
.setOngoing(true)
.setContentIntent(notifyPendingIntent)
.setOngoing(true)
.setDeleteIntent(getDeleteIntent(ACTION_LOW_BATTERY_NOTIFICATION_CLOSED_BY_USER))
.build();
notifyIfAllowed(context, BATTERY_NOTIFICATION_ID, notification);
}
Expand Down

0 comments on commit 324c701

Please sign in to comment.