Ziggeo / android-sdk-demo

Ziggeo's Android Client SDK 2.0
Apache License 2.0
8 stars 5 forks source link

Prevent OS from sleeping while the recorder is on #28

Closed sagrawal31 closed 4 years ago

sagrawal31 commented 4 years ago

I'm using v0.82.2 of the Android SDK on an Android 10. When the recording is running and we do not interact with the screen, the screen goes to sleep (as set in the OS settings - defaulted to 30 seconds). And as soon as the screen goes to sleep, we can not restore the recording and the app freezes with an exception:

03-09 00:38:52.280 11335 11335 E AndroidRuntime: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at androidx.fragment.app.FragmentManagerImpl.checkStateLoss(FragmentManagerImpl.java:1536)
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at androidx.fragment.app.FragmentManagerImpl.enqueueAction(FragmentManagerImpl.java:1558)
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:317)
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:282)
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at androidx.fragment.app.DialogFragment.show(DialogFragment.java:155)
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at com.ziggeo.androidsdk.ui.fragments.BaseRecorderFragment.startRecording(BaseRecorderFragment.java:107)
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at com.ziggeo.androidsdk.ui.fragments.BaseRecorderFragment.readyToRecord(BaseRecorderFragment.java:87)
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at com.ziggeo.androidsdk.ui.fragments.CameraRecorderFragment$1.cameraOpened(CameraRecorderFragment.java:109)
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at com.ziggeo.androidsdk.widgets.cameraview.CameraView$CallbackBridge.onCameraClosed(CameraView.java:838)
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at com.ziggeo.androidsdk.widgets.cameraview.Camera2$2.onClosed(Camera2.java:198)
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at android.hardware.camera2.impl.CameraDeviceImpl$5.run(CameraDeviceImpl.java:212)
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at android.os.Handler.handleCallback(Handler.java:873)
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:214)
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:7099)
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
03-09 00:38:52.280 11335 11335 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

How can we prevent OS from sleeping while the recording is on? I was thinking that this would be handled by OS itself.

By the way, I tried to call getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); but that doesn't help. https://developer.android.com/training/scheduling/wakelock

3akat commented 4 years ago

Hi @sagrawal31, checking it

3akat commented 4 years ago

Please take a look at v 0.82.3. FYI: we have removed ability to configure audio encoder. Since it was not the issue we don't plan to allow it for now.

sagrawal31 commented 4 years ago

Thank you @3akat for releasing a new version. I'll try it out and will let you know that.

sagrawal31 commented 4 years ago

Hi @3akat this isn't working. It's throwing some exception:

7100 17100 E AndroidRuntime:    at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:45)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1957)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:106)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:214)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:7099)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
03-09 15:53:16.590 17100 17100 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.PowerManager$WakeLock.release()' on a null object reference
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at com.ziggeo.androidsdk.widgets.cameraview.CameraView.removeKeepScreenOn(CameraView.java:669)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at com.ziggeo.androidsdk.widgets.cameraview.CameraView.stop(CameraView.java:298)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at com.ziggeo.androidsdk.ui.fragments.CameraRecorderFragment.onPause(CameraRecorderFragment.java:280)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at androidx.fragment.app.Fragment.performPause(Fragment.java:2645)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1512)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3269)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at androidx.fragment.app.FragmentManagerImpl.dispatchPause(FragmentManager.java:3245)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at androidx.fragment.app.FragmentController.dispatchPause(FragmentController.java:234)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at androidx.fragment.app.FragmentActivity.onPause(FragmentActivity.java:476)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at android.app.Activity.performPause(Activity.java:7646)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1469)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:4281)
03-09 15:53:16.590 17100 17100 E AndroidRuntime:    ... 12 more
3akat commented 4 years ago

you're getting this when try to stop the recording, right?

sagrawal31 commented 4 years ago

No. This is crashing as soon as I'm launching the recorder.

3akat commented 4 years ago

Weird. I was able to catch this two times but the issue disappeared after a clean build (build folder delete). Can you please try it?

sagrawal31 commented 4 years ago

@3akat I have already tried cleaning the app multiple times and also tried to delete the build folder (after your comment) but still getting that exception.

sagrawal31 commented 4 years ago

Any update here @3akat

3akat commented 4 years ago

Can't reproduce the issue. Will do some crash-prevent fixes and will share a new version to try

sagrawal31 commented 4 years ago

Okay. I'll be waiting.

3akat commented 4 years ago

Please take a look at 0.82.4

sagrawal31 commented 4 years ago

Oh! This also worked just fine. And the build issue is also gone. Great job Ziggeo team 👏

sagrawal31 commented 4 years ago

By the way, looks like the wake lock is not being cleared when the recording is done. I killed the app and the screen was on until I manually locked the screen (and it got reset afterwards).

This is fine for me but might not be okay for others. So it's up to you if you want to resolve this now or later in future.

3akat commented 4 years ago

Hm... I actually revoke the wakelock when the recording is stopped. I believe you've killed the app without stopping it, right? If so I'll add the fix for that case

sagrawal31 commented 4 years ago

No, I killed the app after the recording was done and it was showing "Uploading" in the notification.

3akat commented 4 years ago

Roger that, will check, thanks for sharing