11package com.ven.assists.web
22
3+ import android.app.Activity
34import android.app.Application
5+ import android.content.Intent
6+ import android.os.Bundle
7+ import androidx.activity.ComponentActivity
8+ import androidx.activity.result.ActivityResult
9+ import androidx.activity.result.ActivityResultLauncher
10+ import androidx.activity.result.contract.ActivityResultContracts
411import androidx.core.content.FileProvider
12+ import com.blankj.utilcode.util.ActivityUtils
13+ import com.journeyapps.barcodescanner.ScanContract
14+ import com.journeyapps.barcodescanner.ScanIntentResult
15+ import com.journeyapps.barcodescanner.ScanOptions
16+ import com.ven.assists.mp.MPManager
517import com.ven.assists.utils.CoroutineWrapper
18+ import kotlinx.coroutines.CompletableDeferred
619import kotlinx.coroutines.delay
720
821class CustomFileProvider : FileProvider () {
22+
23+
924 override fun onCreate (): Boolean {
1025 val applicationContext = context?.applicationContext
1126 if (applicationContext is Application ) {
@@ -29,7 +44,80 @@ class CustomFileProvider : FileProvider() {
2944 delay(1000 )
3045 }
3146 }
47+
48+ applicationContext.unregisterActivityLifecycleCallbacks(activityLifecycleCallbacks)
49+ applicationContext.registerActivityLifecycleCallbacks(activityLifecycleCallbacks)
3250 }
3351 return super .onCreate()
3452 }
53+
54+ private val activityLifecycleCallbacks = object : Application .ActivityLifecycleCallbacks {
55+ override fun onActivityCreated (activity : Activity , savedInstanceState : Bundle ? ) {
56+ if (activity is ComponentActivity && requestLaunchers[activity] == null ) {
57+ requestLaunchers[activity] = activity.registerForActivityResult(ActivityResultContracts .StartActivityForResult ()) { result ->
58+ currentCompletableDeferred?.complete(result)
59+ }
60+ }
61+ if (activity is ComponentActivity && requestLaunchersScan[activity] == null ) {
62+ requestLaunchersScan[activity] = activity.registerForActivityResult(ScanContract ()) { result ->
63+ currentCompletableDeferredScan?.complete(result)
64+ }
65+ }
66+ }
67+
68+ override fun onActivityStarted (activity : Activity ) {
69+ }
70+
71+ override fun onActivityResumed (activity : Activity ) {
72+ }
73+
74+ override fun onActivityPaused (activity : Activity ) {
75+ }
76+
77+ override fun onActivityStopped (activity : Activity ) {
78+ }
79+
80+ override fun onActivitySaveInstanceState (activity : Activity , outState : Bundle ) {
81+ }
82+
83+ override fun onActivityDestroyed (activity : Activity ) {
84+ requestLaunchers.remove(activity)
85+ requestLaunchersScan.remove(activity)
86+ }
87+ }
88+
89+ companion object {
90+ private val requestLaunchers = hashMapOf<Activity , ActivityResultLauncher <Intent >>()
91+ private var currentCompletableDeferred: CompletableDeferred <ActivityResult >? = null
92+ private val requestLaunchersScan = hashMapOf<Activity , ActivityResultLauncher <ScanOptions >>()
93+ private var currentCompletableDeferredScan: CompletableDeferred <ScanIntentResult >? = null
94+
95+ suspend fun requestLaunchers (intent : Intent ): ActivityResult ? {
96+ val result = runCatching {
97+ currentCompletableDeferred?.completeExceptionally(RuntimeException (" reset" ))
98+ currentCompletableDeferred = null
99+ currentCompletableDeferred = CompletableDeferred <ActivityResult >()
100+ ActivityUtils .getTopActivity()?.let {
101+ requestLaunchers[it]?.launch(intent)
102+ } ? : return @runCatching null
103+ return @runCatching currentCompletableDeferred?.await()
104+ }
105+ return result.getOrNull()
106+
107+ }
108+ suspend fun requestLaunchersScan (scanOptions : ScanOptions ): ScanIntentResult ? {
109+ val result = runCatching {
110+ currentCompletableDeferredScan?.completeExceptionally(RuntimeException (" reset" ))
111+ currentCompletableDeferredScan = null
112+ currentCompletableDeferredScan = CompletableDeferred <ScanIntentResult >()
113+ ActivityUtils .getTopActivity()?.let {
114+ requestLaunchersScan[it]?.launch(scanOptions)
115+ } ? : return @runCatching null
116+ return @runCatching currentCompletableDeferredScan?.await()
117+ }
118+ return result.getOrNull()
119+
120+ }
121+ }
122+
35123}
0 commit comments