提交 55f97bca authored 作者: JarvanMo's avatar JarvanMo

Fix bug while requesting permission

上级 1d0b6a1c
...@@ -27,7 +27,9 @@ public class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { ...@@ -27,7 +27,9 @@ public class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
WXAPiHandler.setContext(registrar.activeContext()) WXAPiHandler.setContext(registrar.activeContext())
channel.setMethodCallHandler(FluwxPlugin().apply { channel.setMethodCallHandler(FluwxPlugin().apply {
this.authHandler = authHandler this.authHandler = authHandler
this.shareHandler = FluwxShareHandlerCompat(registrar) this.shareHandler = FluwxShareHandlerCompat(registrar).apply {
permissionHandler = PermissionHandler(registrar.activity())
}
}) })
} }
} }
...@@ -66,13 +68,16 @@ public class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { ...@@ -66,13 +68,16 @@ public class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
} }
override fun onDetachedFromActivity() { override fun onDetachedFromActivity() {
shareHandler?.permissionHandler = null
} }
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
shareHandler?.permissionHandler = PermissionHandler(binding.activity)
} }
override fun onAttachedToActivity(binding: ActivityPluginBinding) { override fun onAttachedToActivity(binding: ActivityPluginBinding) {
WXAPiHandler.setContext(binding.activity.applicationContext) WXAPiHandler.setContext(binding.activity.applicationContext)
shareHandler?.permissionHandler = PermissionHandler(binding.activity)
} }
override fun onDetachedFromActivityForConfigChanges() { override fun onDetachedFromActivityForConfigChanges() {
......
...@@ -7,7 +7,6 @@ import android.content.res.AssetFileDescriptor ...@@ -7,7 +7,6 @@ import android.content.res.AssetFileDescriptor
import android.net.Uri import android.net.Uri
import android.text.TextUtils import android.text.TextUtils
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.jarvan.fluwx.io.ImagesIO import com.jarvan.fluwx.io.ImagesIO
import com.jarvan.fluwx.io.ImagesIOIml import com.jarvan.fluwx.io.ImagesIOIml
import com.jarvan.fluwx.io.WeChatImage import com.jarvan.fluwx.io.WeChatImage
...@@ -42,6 +41,8 @@ internal class FluwxShareHandlerEmbedding(private val flutterAssets: FlutterPlug ...@@ -42,6 +41,8 @@ internal class FluwxShareHandlerEmbedding(private val flutterAssets: FlutterPlug
} }
override val job: Job = Job() override val job: Job = Job()
override var permissionHandler: PermissionHandler? = null
} }
internal class FluwxShareHandlerCompat(private val registrar: PluginRegistry.Registrar) : FluwxShareHandler { internal class FluwxShareHandlerCompat(private val registrar: PluginRegistry.Registrar) : FluwxShareHandler {
...@@ -58,6 +59,7 @@ internal class FluwxShareHandlerCompat(private val registrar: PluginRegistry.Reg ...@@ -58,6 +59,7 @@ internal class FluwxShareHandlerCompat(private val registrar: PluginRegistry.Reg
override val context: Context = registrar.context().applicationContext override val context: Context = registrar.context().applicationContext
override val job: Job = Job() override val job: Job = Job()
override var permissionHandler: PermissionHandler? = null
} }
internal interface FluwxShareHandler : CoroutineScope { internal interface FluwxShareHandler : CoroutineScope {
...@@ -89,7 +91,7 @@ internal interface FluwxShareHandler : CoroutineScope { ...@@ -89,7 +91,7 @@ internal interface FluwxShareHandler : CoroutineScope {
} }
private fun shareText(call: MethodCall, result: MethodChannel.Result) { private fun shareText(call: MethodCall, result: MethodChannel.Result) {
val textObj = WXTextObject(call.argument<String?>("source")) val textObj = WXTextObject(call.argument("source"))
val msg = WXMediaMessage() val msg = WXMediaMessage()
msg.mediaObject = textObj msg.mediaObject = textObj
val req = SendMessageToWX.Req() val req = SendMessageToWX.Req()
...@@ -136,7 +138,7 @@ internal interface FluwxShareHandler : CoroutineScope { ...@@ -136,7 +138,7 @@ internal interface FluwxShareHandler : CoroutineScope {
if (ContextCompat.checkSelfPermission(context, Manifest.permission_group.STORAGE) == PackageManager.PERMISSION_GRANTED) { if (ContextCompat.checkSelfPermission(context, Manifest.permission_group.STORAGE) == PackageManager.PERMISSION_GRANTED) {
setImagePath(sourceByteArray.toExternalCacheFile(context, sourceImage.suffix)?.absolutePath) setImagePath(sourceByteArray.toExternalCacheFile(context, sourceImage.suffix)?.absolutePath)
} else { } else {
Fragment().requestPermissions(arrayOf(Manifest.permission_group.STORAGE), 12121) permissionHandler?.requestStoragePermission()
} }
} }
} }
...@@ -283,8 +285,7 @@ internal interface FluwxShareHandler : CoroutineScope { ...@@ -283,8 +285,7 @@ internal interface FluwxShareHandler : CoroutineScope {
val job: Job val job: Job
fun onDestroy() { var permissionHandler: PermissionHandler?
job.cancel()
}
fun onDestroy() = job.cancel()
} }
\ No newline at end of file
package com.jarvan.fluwx.handlers
import android.Manifest
import android.app.Activity
import android.app.Fragment
import android.os.Build
/***
* Created by mo on 2020/3/27
* 冷风如刀,以大地为砧板,视众生为鱼肉。
* 万里飞雪,将穹苍作烘炉,熔万物为白银。
**/
class PermissionHandler(private val activity: Activity?) {
private val tag = "Fragment_TAG"
private val fragment: Fragment = Fragment()
fun requestStoragePermission() {
val currentFragment = oldFragment ?: fragment
activity?.run {
val ft = fragmentManager.beginTransaction()
ft.add(currentFragment, tag)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
ft.commitNow()
} else {
ft.commit()
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
currentFragment.requestPermissions(arrayOf(Manifest.permission_group.STORAGE), 12121)
}
}
}
private val oldFragment get() = activity?.fragmentManager?.findFragmentByTag(tag)
}
\ No newline at end of file
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
In most cases you can leave this as-is, but you if you want to provide In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. --> FlutterApplication and put your custom class here. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application <application
android:name="io.flutter.app.FlutterApplication" android:name="io.flutter.app.FlutterApplication"
android:label="fluwx_example" android:label="fluwx_example"
......
...@@ -80,7 +80,7 @@ packages: ...@@ -80,7 +80,7 @@ packages:
path: ".." path: ".."
relative: true relative: true
source: path source: path
version: "2.0.5" version: "2.0.5+1"
image: image:
dependency: transitive dependency: transitive
description: description:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论