diff --git a/android/src/main/kotlin/com/simform/audio_waveforms/AudioRecorder.kt b/android/src/main/kotlin/com/simform/audio_waveforms/AudioRecorder.kt index 85dfb381..b061ac97 100644 --- a/android/src/main/kotlin/com/simform/audio_waveforms/AudioRecorder.kt +++ b/android/src/main/kotlin/com/simform/audio_waveforms/AudioRecorder.kt @@ -22,6 +22,7 @@ private const val RECORD_AUDIO_REQUEST_CODE = 1001 class AudioRecorder : PluginRegistry.RequestPermissionsResultListener { private var permissions = arrayOf(Manifest.permission.RECORD_AUDIO) private var useLegacyNormalization = false + private var successCallback: RequestPermissionsSuccessCallback? = null fun getDecibel(result: MethodChannel.Result, recorder: MediaRecorder?) { if (useLegacyNormalization) { @@ -130,6 +131,7 @@ class AudioRecorder : PluginRegistry.RequestPermissionsResultListener { grantResults: IntArray ): Boolean { return if (requestCode == RECORD_AUDIO_REQUEST_CODE) { + successCallback?.onSuccess(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED } else { false @@ -142,7 +144,8 @@ class AudioRecorder : PluginRegistry.RequestPermissionsResultListener { return result == PackageManager.PERMISSION_GRANTED } - fun checkPermission(result: MethodChannel.Result, activity: Activity?) { + fun checkPermission(result: MethodChannel.Result, activity: Activity?, successCallback: RequestPermissionsSuccessCallback) { + this.successCallback = successCallback if (!isPermissionGranted(activity)) { activity?.let { ActivityCompat.requestPermissions( diff --git a/android/src/main/kotlin/com/simform/audio_waveforms/AudioWaveformsPlugin.kt b/android/src/main/kotlin/com/simform/audio_waveforms/AudioWaveformsPlugin.kt index 40c8a25a..a1ebc2c9 100644 --- a/android/src/main/kotlin/com/simform/audio_waveforms/AudioWaveformsPlugin.kt +++ b/android/src/main/kotlin/com/simform/audio_waveforms/AudioWaveformsPlugin.kt @@ -15,6 +15,7 @@ import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.plugin.common.MethodChannel.Result +import io.flutter.plugin.common.PluginRegistry.Registrar import java.io.File import java.io.IOException import java.text.SimpleDateFormat @@ -35,6 +36,7 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { private lateinit var applicationContext: Context private var audioPlayers = mutableMapOf() private var extractors = mutableMapOf() + private var pluginBinding: ActivityPluginBinding? = null override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { channel = MethodChannel(flutterPluginBinding.binaryMessenger, Constants.methodChannelName) @@ -66,7 +68,7 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { Constants.pauseRecording -> audioRecorder.pauseRecording(result, recorder) Constants.resumeRecording -> audioRecorder.resumeRecording(result, recorder) Constants.getDecibel -> audioRecorder.getDecibel(result, recorder) - Constants.checkPermission -> audioRecorder.checkPermission(result, activity) + Constants.checkPermission -> audioRecorder.checkPermission(result, activity, result :: success) Constants.preparePlayer -> { val audioPath = call.argument(Constants.path) as String? val volume = call.argument(Constants.volume) as Double? @@ -276,6 +278,9 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { override fun onAttachedToActivity(binding: ActivityPluginBinding) { activity = binding.activity + pluginBinding = binding + pluginBinding!!.addRequestPermissionsResultListener(this.audioRecorder) + } override fun onDetachedFromActivityForConfigChanges() { @@ -292,5 +297,8 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { audioPlayers.clear() extractors.clear() activity = null + if (pluginBinding != null) { + pluginBinding!!.removeRequestPermissionsResultListener(this.audioRecorder) + } } } diff --git a/android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt b/android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt index 99b6339a..87f7557f 100644 --- a/android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt +++ b/android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt @@ -75,4 +75,8 @@ enum class UpdateFrequency(val value:Long) { High(50), Medium(100), Low(200), +} + +fun interface RequestPermissionsSuccessCallback { + fun onSuccess(results: Boolean?) } \ No newline at end of file