Skip to content

Commit

Permalink
Merge pull request #749 from enaboapps/iss747
Browse files Browse the repository at this point in the history
Tap notifications instead of using intents
  • Loading branch information
OwenMcGirr authored Nov 3, 2024
2 parents 5e15ade + c7034d1 commit 90a25ca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
}
}
}

0 comments on commit 90a25ca

Please sign in to comment.