Skip to content

Commit

Permalink
fix: 悬浮窗按钮无法点击
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Oct 1, 2023
1 parent 7f0d7f1 commit ba2271d
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 51 deletions.
5 changes: 5 additions & 0 deletions app/src/main/java/li/songe/gkd/composition/CanConfigBubble.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package li.songe.gkd.composition

interface CanConfigBubble {
fun configBubble(f: ConfigBubbleHook)
}
5 changes: 0 additions & 5 deletions app/src/main/java/li/songe/gkd/composition/CanSetupBubble.kt

This file was deleted.

31 changes: 17 additions & 14 deletions app/src/main/java/li/songe/gkd/composition/CompositionFbService.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package li.songe.gkd.composition

import com.torrydo.floatingbubbleview.FloatingBubble
import com.torrydo.floatingbubbleview.FloatingBubbleService
import com.torrydo.floatingbubbleview.service.expandable.BubbleBuilder
import com.torrydo.floatingbubbleview.service.expandable.ExpandableBubbleService

open class CompositionFbService(
private val block: CompositionFbService.() -> Unit,
) : FloatingBubbleService(), CanOnDestroy, CanSetupBubble {
) : ExpandableBubbleService(), CanOnDestroy, CanConfigBubble {


override fun configExpandedBubble() = null

override fun onCreate() {
block()
super.onCreate()
Expand All @@ -18,21 +22,20 @@ open class CompositionFbService(
destroyHooks.forEach { f -> f() }
}

override fun setupBubble(action: FloatingBubble.Action): FloatingBubble.Builder {
var result: FloatingBubble.Builder? = null

setupBubbleHooks.forEach { f ->
f(action) {
private val configBubbleHooks by lazy { linkedSetOf<ConfigBubbleHook>() }
override fun configBubble(f: ConfigBubbleHook) {
configBubbleHooks.add(f)
}

override fun configBubble(): BubbleBuilder? {
var result: BubbleBuilder? = null
configBubbleHooks.forEach { f ->
f {
result = it
}
}
return result ?: throw error("setupBubble result must be resolved")
}

private val setupBubbleHooks by lazy { linkedSetOf<SetupBubbleHook>() }
override fun setupBubble(f: SetupBubbleHook) {
setupBubbleHooks.add(f)
return result
}


}
4 changes: 2 additions & 2 deletions app/src/main/java/li/songe/gkd/composition/Typealias.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package li.songe.gkd.composition

import android.content.Context
import android.content.Intent
import com.torrydo.floatingbubbleview.FloatingBubble
import com.torrydo.floatingbubbleview.service.expandable.BubbleBuilder

typealias StartCommandHook = (intent: Intent?, flags: Int, startId: Int) -> Unit

typealias onReceiveType = (Context?, Intent?) -> Unit

typealias SetupBubbleHook = (FloatingBubble.Action, (FloatingBubble.Builder) -> Unit) -> Unit
typealias ConfigBubbleHook = ((BubbleBuilder) -> Unit) -> Unit
82 changes: 53 additions & 29 deletions app/src/main/java/li/songe/gkd/debug/FloatingService.kt
Original file line number Diff line number Diff line change
@@ -1,64 +1,88 @@
package li.songe.gkd.debug

import android.app.Notification
import android.content.Context
import android.content.Intent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import android.view.ViewConfiguration
import androidx.compose.foundation.layout.size
import androidx.compose.material.Icon
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.core.app.NotificationCompat
import com.blankj.utilcode.util.ServiceUtils
import com.blankj.utilcode.util.ToastUtils
import com.torrydo.floatingbubbleview.FloatingBubble
import com.torrydo.floatingbubbleview.FloatingBubbleListener
import com.torrydo.floatingbubbleview.service.expandable.BubbleBuilder
import kotlinx.coroutines.Dispatchers
import li.songe.gkd.app
import li.songe.gkd.appScope
import li.songe.gkd.composition.CompositionExt.useLifeCycleLog
import li.songe.gkd.composition.CompositionFbService
import li.songe.gkd.data.Tuple3
import li.songe.gkd.notif.createNotif
import li.songe.gkd.notif.floatingChannel
import li.songe.gkd.notif.floatingNotif
import li.songe.gkd.util.SafeR
import li.songe.gkd.util.launchTry
import kotlin.math.sqrt

class FloatingService : CompositionFbService({
useLifeCycleLog()
setupBubble { _, resolve ->
val builder = FloatingBubble.Builder(this).bubble {
Icon(painter = painterResource(SafeR.ic_capture),

configBubble { resolve ->
val builder = BubbleBuilder(this).bubbleCompose {
Icon(
painter = painterResource(SafeR.ic_capture),
contentDescription = "capture",
modifier = Modifier
.clickable(indication = null,
interactionSource = remember { MutableInteractionSource() }) {
appScope.launchTry(Dispatchers.IO) {
SnapshotExt.captureSnapshot()
ToastUtils.showShort("快照成功")
}
modifier = Modifier.size(40.dp),
tint = Color.Red
)
}.enableAnimateToEdge(false)

// https://github.com/gkd-kit/gkd/issues/62
// https://github.com/gkd-kit/gkd/issues/61
val defaultFingerData = Tuple3(0L, 0f, 0f)
var fingerDownData = defaultFingerData
val maxDistanceOffset = 50
builder.addFloatingBubbleListener(object : FloatingBubbleListener {
override fun onFingerDown(x: Float, y: Float) {
fingerDownData = Tuple3(System.currentTimeMillis(), x, y)
}

override fun onFingerMove(x: Float, y: Float) {
if (fingerDownData === defaultFingerData) {
return
}
val dx = fingerDownData.t1 - x
val dy = fingerDownData.t2 - y
val distance = sqrt(dx * dx + dy * dy)
if (distance > maxDistanceOffset) {
// reset
fingerDownData = defaultFingerData
}
}

override fun onFingerUp(x: Float, y: Float) {
if (System.currentTimeMillis() - fingerDownData.t0 < ViewConfiguration.getTapTimeout()) {
// is onClick
appScope.launchTry(Dispatchers.IO) {
SnapshotExt.captureSnapshot()
ToastUtils.showShort("快照成功")
}
.size(40.dp),
tint = Color.Red)
}.enableCloseBubble(false).enableAnimateToEdge(false)
}
}
})
resolve(builder)
}
}) {


override fun initialNotification(): Notification {
return NotificationCompat.Builder(this, floatingChannel.id).setOngoing(true)
.setSmallIcon(SafeR.ic_launcher).setContentTitle(floatingNotif.title)
.setContentText(floatingNotif.text).setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setCategory(Notification.CATEGORY_SERVICE).build()
override fun onCreate() {
super.onCreate()
minimize()
}

override fun notificationId() = floatingNotif.id

override fun createNotificationChannel(channelId: String, channelName: String) {
// by app init
override fun startNotificationForeground() {
createNotif(this, floatingChannel.id, floatingNotif)
}

companion object {
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ dependencyResolutionManagement {
// https://github.com/journeyapps/zxing-android-embedded
library("others.zxing.android.embedded", "com.journeyapps:zxing-android-embedded:4.3.0")
// https://github.com/TorryDo/Floating-Bubble-View
library("others.floating.bubble.view", "io.github.torrydo:floating-bubble-view:0.5.6")
library("others.floating.bubble.view", "io.github.torrydo:floating-bubble-view:0.6.1")

library("androidx.appcompat", "androidx.appcompat:appcompat:1.6.1")
library("androidx.core.ktx", "androidx.core:core-ktx:1.12.0")
Expand Down

0 comments on commit ba2271d

Please sign in to comment.