@@ -17,6 +17,9 @@ import java.io.FileOutputStream
1717import androidx.core.content.FileProvider
1818import android.net.Uri
1919import android.content.Intent
20+ import android.content.ActivityNotFoundException
21+ import android.os.Handler
22+ import android.os.Looper
2023
2124val grantedPermissions = mapOf (
2225 " canAskAgain" to true , " granted" to true , " expires" to " never" , " status" to " granted"
@@ -64,7 +67,7 @@ class DownloadsModule : Module() {
6467 )
6568 }
6669
67- AsyncFunction (" openFile" ) { options: OpenFileOptions ->
70+ AsyncFunction (" openFile" ) { options: OpenFileOptions , promise : Promise ->
6871 val parsedUri = try {
6972 Uri .parse(options.uri)
7073 } catch (e: Exception ) {
@@ -73,7 +76,7 @@ class DownloadsModule : Module() {
7376 if (parsedUri.scheme == null || (parsedUri.scheme != " content" && parsedUri.scheme != " file" )) {
7477 throw InvalidArgumentException (" uri is invalid" )
7578 }
76- openFile(parsedUri, options.type)
79+ openFile(parsedUri, options.type, promise )
7780 }
7881 }
7982
@@ -137,16 +140,22 @@ class DownloadsModule : Module() {
137140 }
138141 }
139142
140- private fun openFile (uri : Uri , type : String ) {
143+ private fun openFile (uri : Uri , type : String , promise : Promise ) {
141144 val intent = Intent (Intent .ACTION_VIEW ).apply {
142145 setDataAndType(uri, type)
143146 addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
144147 addFlags(Intent .FLAG_GRANT_READ_URI_PERMISSION )
145148 }
146- try {
147- mContext.startActivity(intent)
148- } catch (e: Exception ) {
149- throw FileOpenException ()
149+ Handler (Looper .getMainLooper()).post {
150+ try {
151+ mContext.startActivity(intent)
152+ promise.resolve(null )
153+ } catch (e: ActivityNotFoundException ) {
154+ // 開けるアプリが無い場合。フリーズさせず明示的に拒否する
155+ promise.reject(FileOpenException ())
156+ } catch (e: Exception ) {
157+ promise.reject(FileOpenException ())
158+ }
150159 }
151160 }
152161}
0 commit comments