Skip to content

Commit

Permalink
Added notificationAction to LiveNotification
Browse files Browse the repository at this point in the history
  • Loading branch information
choiman1559 committed Nov 10, 2024
1 parent 55455dc commit 3a0cf8e
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 37 deletions.
8 changes: 4 additions & 4 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ android {
}

dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.2'
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.3'
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public interface OnNotificationRemoveRequest {
}

public static OnNotificationRemoveRequest removeListener;
public static OnNotificationRemoveRequest removeListenerAll;
public static OnNotificationRemoveRequest removeListenerById;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class NotiListenerService extends NotificationListenerService {
private static final Object pastNotificationLock = new Object();
private volatile StatusBarNotification pastNotification = null;
private final FirebaseMessageService.OnNotificationRemoveRequest onRemoveRequestListener = this::cancelNotification;
private final FirebaseMessageService.OnNotificationRemoveRequest onRemoveRequestAllListener = (unused) -> cancelAllNotifications();
private final FirebaseMessageService.OnNotificationRemoveRequest onRemoveRequestListenerById = key -> {
StatusBarNotification[] notifications = this.getActiveNotifications();
for (StatusBarNotification statusBarNotification : notifications) {
Expand Down Expand Up @@ -114,6 +115,7 @@ public void onCreate() {
super.onCreate();
if (instance == null) initService(this);
FirebaseMessageService.removeListener = onRemoveRequestListener;
FirebaseMessageService.removeListenerAll = onRemoveRequestAllListener;
FirebaseMessageService.removeListenerById = onRemoveRequestListenerById;
}

Expand Down
21 changes: 12 additions & 9 deletions app/src/main/java/com/noti/main/service/backend/PacketBonding.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,7 @@ public static void runBondingSchedule(Context context, JSONObject notification,
for(JSONObject o : onWriteArrayList) {
array.put(o);
}

JSONObject finalNotification = new JSONObject();
finalNotification.put("type", PacketConst.SERVICE_TYPE_PACKET_BONDING);
finalNotification.put(PacketConst.KEY_PACKET_BONDING_ARRAY, array.toString());
finalNotification.put(PacketConst.KEY_DEVICE_ID, NotiListenerService.getUniqueID());
finalNotification.put(PacketConst.KEY_DEVICE_NAME, NotiListenerService.getDeviceName());

if(BuildConfig.DEBUG) Log.d("PacketBonding", String.format("Sending %d bonded packets together...", array.length()));
NotiListenerService.proxyToBackend(finalNotification, "PacketBonding", context, false);
sendBondingArrayNow(context, array);
}
} catch (JSONException | NoSuchAlgorithmException e) {
e.printStackTrace();
Expand All @@ -78,6 +70,17 @@ public static void runBondingSchedule(Context context, JSONObject notification,
}, selectedDelay, TimeUnit.MILLISECONDS);
}

public static void sendBondingArrayNow(Context context, JSONArray array) throws JSONException, NoSuchAlgorithmException {
JSONObject finalNotification = new JSONObject();
finalNotification.put("type", PacketConst.SERVICE_TYPE_PACKET_BONDING);
finalNotification.put(PacketConst.KEY_PACKET_BONDING_ARRAY, array.toString());
finalNotification.put(PacketConst.KEY_DEVICE_ID, NotiListenerService.getUniqueID());
finalNotification.put(PacketConst.KEY_DEVICE_NAME, NotiListenerService.getDeviceName());

if(BuildConfig.DEBUG) Log.d("PacketBonding", String.format("Sending %d bonded packets together...", array.length()));
NotiListenerService.proxyToBackend(finalNotification, "PacketBonding", context, false);
}

@SuppressWarnings("unchecked")
public static void processPacketBonding(Map<String, String> map, OnPacketBondingCallback callback) {
String rawData = map.get(PacketConst.KEY_PACKET_BONDING_ARRAY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.noti.main.BuildConfig;
import com.noti.main.service.FirebaseMessageService;
import com.noti.main.service.backend.PacketConst;
import com.noti.main.service.mirnoti.NotificationActionProcess;
import com.noti.main.service.mirnoti.NotificationsData;

import java.util.ArrayList;
Expand All @@ -24,6 +25,7 @@ public class LiveNotiProcess {
public static final String REQUEST_LIVE_NOTIFICATION = "request_live_notification";
public static final String RESPONSE_LIVE_NOTIFICATION = "response_live_notification";
public static final String REQUEST_NOTIFICATION_ACTION = "response_notification_action";
public static final String REQUEST_NOTIFICATION_DISMISS_ALL = "request_notification_dismiss_all";

public static onLiveNotificationUploadCompleteListener mOnLiveNotificationDownloadCompleteListener;
public static onLiveNotificationUploadCompleteListener mOnLiveNotificationUploadCompleteListener;
Expand All @@ -49,6 +51,11 @@ public static void onProcessReceive(Map<String, String> map, Context context) {
case REQUEST_LIVE_NOTIFICATION -> LiveNotiRequests.postLiveNotificationData(context, map);
case RESPONSE_LIVE_NOTIFICATION -> LiveNotiRequests.getLiveNotificationData(context, map);
case REQUEST_NOTIFICATION_ACTION -> FirebaseMessageService.startNewRemoteActivity(context, map);
case REQUEST_NOTIFICATION_DISMISS_ALL -> {
if(FirebaseMessageService.removeListenerAll != null) {
FirebaseMessageService.removeListenerAll.onRequested(null);
}
}
default -> {
if(BuildConfig.DEBUG) {
Log.e("LiveNotification", "onProcessReceive failed: Action type is not supported: " + map.get(PacketConst.KEY_ACTION_TYPE));
Expand All @@ -72,6 +79,10 @@ public static String toStringLiveNotificationList(Context context) throws JsonPr
continue;
}

if(Objects.requireNonNullElse(statusBarNotification.getNotification().actions, new Object[0]).length > 0) {
NotificationActionProcess.registerAction(statusBarNotification);
}

try {
finalDataList.add(new NotificationsData(context, statusBarNotification).toString());
keyList.add(statusBarNotification.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.noti.main.service.FirebaseMessageService;

import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class NotificationActionProcess {
Expand Down Expand Up @@ -121,7 +122,7 @@ public void onReceive(Context context, Intent intent) {
return;
}

String key = intent.getStringExtra(NotificationRequest.KEY_NOTIFICATION_KEY);
String key = Objects.requireNonNullElse(intent.getStringExtra(NotificationRequest.KEY_NOTIFICATION_KEY), "");
int index = intent.getIntExtra(NotificationRequest.KEY_NOTIFICATION_ACTION_INDEX, 0);

if(intent.getBooleanExtra(NotificationRequest.KEY_NOTIFICATION_HAS_INPUT, false)) {
Expand All @@ -134,7 +135,7 @@ public void onReceive(Context context, Intent intent) {
intent.getStringExtra("device_name"), intent.getStringExtra("device_id"));
}

final int uniqueCode = intent.getIntExtra(NotificationRequest.KEY_NOTIFICATION_HASHCODE, -1);
final int uniqueCode = intent.getIntExtra(NotificationRequest.KEY_NOTIFICATION_HASHCODE, key.hashCode());
if(uniqueCode != -1) {
if(FirebaseMessageService.removeListenerById == null) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,21 @@ public static void receptionNotification(Context context, @Nullable String Packa
if(BuildConfig.DEBUG) Log.d("data-receive", notificationBody.toString());
NotiListenerService.sendNotification(notificationBody, Package, context);
}

public static void requestDismissAllNotifications(Context context, String DEVICE_NAME, String DEVICE_ID) {
JSONObject notificationBody = new JSONObject();
try {
notificationBody.put(PacketConst.KEY_ACTION_TYPE, LiveNotiProcess.REQUEST_NOTIFICATION_DISMISS_ALL);
notificationBody.put("type","pair|live_notification");
notificationBody.put("device_name", NotiListenerService.getDeviceName());
notificationBody.put("device_id", NotiListenerService.getUniqueID());
notificationBody.put("send_device_name", DEVICE_NAME);
notificationBody.put("send_device_id", DEVICE_ID);
} catch (JSONException e) {
Log.e("Noti", "onCreate: " + e.getMessage() );
}

if(BuildConfig.DEBUG) Log.d("data-receive", notificationBody.toString());
NotiListenerService.sendNotification(notificationBody, "NotificationRequest", context);
}
}
Loading

0 comments on commit 3a0cf8e

Please sign in to comment.