MainActivity.kt
2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.doublefeel.app
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContracts
import com.doublefeel.app.native.AlipayHostApiImpl
import com.doublefeel.app.native.HealthKitHostApiImpl
import com.doublefeel.app.native.PlatformHostApiImpl
import com.doublefeel.app.native.WearEngineHostApiImpl
import com.doublefeel.app.pigeon.HealthKitHostApi
import com.doublefeel.app.pigeon.alipay.AlipayHostApi
import com.doublefeel.app.pigeon.platform.PlatformHostApi
import com.doublefeel.app.pigeon.wear.WearEngineHostApi
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
class MainActivity : FlutterFragmentActivity() {
private var healthAuthLatch: CountDownLatch? = null
private val healthAuthGranted = AtomicBoolean(false)
private val healthClientAuthLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult(),
) { result ->
healthAuthGranted.set(result.resultCode == RESULT_OK)
healthAuthLatch?.countDown()
}
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MainActivityHolder.activity = this
val messenger = flutterEngine.dartExecutor.binaryMessenger
HealthKitHostApi.setUp(messenger, HealthKitHostApiImpl(this))
WearEngineHostApi.setUp(messenger, WearEngineHostApiImpl(applicationContext))
AlipayHostApi.setUp(messenger, AlipayHostApiImpl(this))
PlatformHostApi.setUp(messenger, PlatformHostApiImpl(application))
}
override fun onDestroy() {
if (MainActivityHolder.activity === this) {
MainActivityHolder.activity = null
}
super.onDestroy()
}
fun launchHealthClientAuth(intent: Intent): Boolean {
healthAuthGranted.set(false)
healthAuthLatch = CountDownLatch(1)
healthClientAuthLauncher.launch(intent)
return healthAuthLatch?.await(120, TimeUnit.SECONDS) == true && healthAuthGranted.get()
}
}
object MainActivityHolder {
var activity: MainActivity? = null
}