permissions-dispatcher / PermissionsDispatcher

A declarative API to handle Android runtime permissions.
https://github.com/permissions-dispatcher/PermissionsDispatcher
Apache License 2.0
11.22k stars 1.44k forks source link

Mic detection of audio stops after navigation between activities #692

Open rckahale opened 3 years ago

rckahale commented 3 years ago

Firstly, very useful & wonderfully easy to implement library.

Overview

My issue is that, I land up in a state where the audio record permission is enabled but audio is not detected.

If I just remain on one activity the issue is not present. I navigate between 4 activities in which 1) I ask for audio record permission..navigate to another listing activity..do stuff 2) I ask for file record permission..navigate to new listing activity..do stuff 3) I ask for audio record permission....do stuff 4) I produce sounds using midi & android.media.MediaPlayer

The issue was more often when I had wrongly put the permission asking function inside onResume() Now I have corrected it & put it inside onStart and the corresponding activities work fine Issue does not have fixed steps to reproduce.

Expected

Actual

I feel there is an extra call made from onStartwhenever I come Back from listing activity or activities which are not using audio record permission. So just like it happened in case of onResume, it stops recording altogether, even if it has permissions.

All audio rec activities are affected and none work till there is killing of app. If I modify the permission from settings (from enable to disable), in stuck state, the permission enabler does its job and app doesn't need killing

Environment

There are no crashes in the log, but these lines

D/AudioRecordPermission: AudioRecordPermission
I/AudioRecordPermission: remindWithResult:false

I/HwAudioRecordImpl: HwAudioRecordImpl
    sendStateChangedIntent, state=3
D/ZrHung.AppEyeUiProbe: notify runnable to start.
I/Window: window add KeepScreenOnFlag for com.***
I/HwPhoneWindow: updateLayoutParamsColor true mSpecialSet=false, mForcedNavigationBarColor=true, navigationBarColor=ff1f2025, mNavBarShow=false, mIsFloating=false
D/OpenGLRenderer:   HWUI Binary is  enabled

I have read 10 years back there was need to call recordInstance.release() in onStop or onDestroy https://stackoverflow.com/questions/4342482/cannot-access-audiorecorder

  override fun onStart() {
        super.onStart()
        onAudioPermitWithPermissionCheck() 
    }

 @NeedsPermission(Manifest.permission.RECORD_AUDIO)
    fun onAudioPermit() {
    // stuff
    }

Additional info: There may be some flaw in my implementation as I do not get Allow/Deny popup after its shown once in respective activities after app install. Rest all times, the OnShowRationale is called. But, flow proceeds and works.

hotchemi commented 3 years ago

@rckahale sorry for the late(somehow missed notification). well I'm now trying to figure out the situation but could I ask the following:

rckahale commented 3 years ago

When I put it in onCreate, its working as expected and above issue does not occur

But, there is a presence of different issue in this scenario

If user has stopped the mic permission from settings, or if user has denied more than once the OnShowRationale associated function is called. This function opens settings (through alert dialog) in a new activity

      val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        val uri: Uri = Uri.fromParts("package", thisActivity.getPackageName(), null)
        intent.data = uri
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
        thisActivity.startActivityForResult(intent, 123)

When user allows the permission from this settings activity, the user lands back inside the activity where audio work is being done and the function onAudioPermit is not called. (It gets called inside onStart or onResume)

@NeedsPermission(Manifest.permission.RECORD_AUDIO)
    fun onAudioPermit() 

So the activity is in a state as good as audio permissions not granted, which is fixed only when user navigates out and back to this activity

hotchemi commented 3 years ago

thx, um..I'm just wondering it's kind of a bug originated from Android permission API itself(because our lib is just a wrapper around there and we don't intercept any special procedure)🤔 Perhaps I have not been able to 100% caught up but would you mind trying out without PermissionsDispatcher? 🙏

rckahale commented 3 years ago

Haha...PermissionDispatcher has landed me into the comfort zone, that now its difficult to write code without it. Instead I have added, the check of coming from ACTION_APPLICATION_DETAILS_SETTINGS

public override fun onActivityResult(comingFrom: Int, paramInt2: Int, paramIntent: Intent?) {
     if (comingFrom== NO_REC_PERMISSION_CODE) {
            // since there was a need to open permission settings, call the permit flow in case the permission is just now granted
            onAudioPermitWithPermissionCheck()
        }
}

NO_REC_PERMISSION_CODE = 123

This works for now. :thumbsup:

hotchemi commented 3 years ago

glad to hear that haha.

well let me walk through the issue again and JFYI we now have https://github.com/permissions-dispatcher/PermissionsDispatcher/tree/master/ktx which does not rely on kapt anymore and might be easier for debug as well.