From c7034d1ed8c3612e949d162b5748d99fd51ead1e Mon Sep 17 00:00:00 2001 From: OwenMcGirr Date: Sun, 3 Nov 2024 19:27:23 +0000 Subject: [PATCH] Tap notifications instead of using intents closes #747 --- .../service/gestures/GestureManager.kt | 11 ++++-- .../notifications/NotificationManager.kt | 36 ++++++------------- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/enaboapps/switchify/service/gestures/GestureManager.kt b/app/src/main/java/com/enaboapps/switchify/service/gestures/GestureManager.kt index 05ec443a..2f7ddc8f 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/gestures/GestureManager.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/gestures/GestureManager.kt @@ -68,12 +68,19 @@ class GestureManager private constructor() { /** * Performs a tap gesture at the current point. + * + * @param x The x coordinate of the tap gesture. If null, the current point will be used. + * @param y The y coordinate of the tap gesture. If null, the current point will be used. */ - fun performTap() { + fun performTap(x: Int? = null, y: Int? = null) { try { accessibilityService?.let { val path = Path() - val point = getAssistedCurrentPoint() + var point = if (x != null && y != null) { + PointF(x.toFloat(), y.toFloat()) + } else { + getAssistedCurrentPoint() + } val gestureDrawing = GestureDrawing(it) gestureDrawing.drawCircleAndRemove( point.x.toInt(), diff --git a/app/src/main/java/com/enaboapps/switchify/service/notifications/NotificationManager.kt b/app/src/main/java/com/enaboapps/switchify/service/notifications/NotificationManager.kt index f1ea3d4a..405b4674 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/notifications/NotificationManager.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/notifications/NotificationManager.kt @@ -5,7 +5,8 @@ import android.app.Notification import android.os.Handler import android.os.Looper import android.view.accessibility.AccessibilityEvent -import android.view.accessibility.AccessibilityNodeInfo +import com.enaboapps.switchify.service.gestures.GestureManager +import com.enaboapps.switchify.service.utils.ScreenUtils import java.util.concurrent.atomic.AtomicBoolean class NotificationManager(private val accessibilityService: AccessibilityService) { @@ -82,34 +83,19 @@ class NotificationManager(private val accessibilityService: AccessibilityService return if (isNotificationActive.get()) currentNotification else null } - fun openNotification(notification: NotificationInfo) { - try { - // Try using PendingIntent first - notification.notification.contentIntent?.send() - cancelTimeout() // Cancel timeout after successful opening - } catch (e: Exception) { - println("Failed to open notification using PendingIntent: ${e.message}") - try { - // Fallback to accessibility action if PendingIntent fails - val rootNode = accessibilityService.rootInActiveWindow - rootNode?.findAccessibilityNodeInfosByText(notification.title)?.firstOrNull() - ?.let { node -> - val success = node.performAction(AccessibilityNodeInfo.ACTION_CLICK) - if (success) { - cancelTimeout() // Cancel timeout after successful opening - } else { - println("Failed to open notification") - } - } - } catch (e: Exception) { - println("Failed to open notification: ${e.message}") - } - } + fun openNotification() { + val screenWidth = ScreenUtils.getWidth(accessibilityService) + // Calculate the center point of the notification roughly + val centerX = screenWidth / 2 + val centerY = ScreenUtils.dpToPx(accessibilityService, 80) + GestureManager.getInstance().performTap(centerX, centerY) + + cancelTimeout() // Cancel timeout after successful opening } fun handleSwitch() { getCurrentNotification()?.let { notification -> - openNotification(notification) + openNotification() } } } \ No newline at end of file