Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception: Only the original thread that created a view hierarchy can touch its views #6

Closed
tranb3r opened this issue Jun 27, 2023 · 5 comments · Fixed by #7
Closed
Assignees
Labels
bug Something isn't working

Comments

@tranb3r
Copy link

tranb3r commented Jun 27, 2023

Here is an exception I get sometimes on android:

EnableScreenSecurityProtection failed with Exception message: Only the original thread that created a view hierarchy can touch its views.
Exception Stacktrace:    at Java.Interop.JniEnvironment.InstanceMethods.CallVoidMethod(JniObjectReference , JniMethodInfo , JniArgumentValue* )
   at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(String , IJavaPeerable , JniArgumentValue* )
   at Android.Views.Window.SetFlags(WindowManagerFlags , WindowManagerFlags )
   at Plugin.Maui.ScreenSecurity.ScreenSecurityImplementation.EnableScreenSecurityProtection()
  --- End of managed Java.Lang.RuntimeException stack trace ---
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
	at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:8384)
	at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1476)
	at android.view.View.requestLayout(View.java:24528)
	at android.view.View.setLayoutParams(View.java:17308)
	at android.view.WindowManagerGlobal.updateViewLayout(WindowManagerGlobal.java:452)
	at android.view.WindowManagerImpl.updateViewLayout(WindowManagerImpl.java:103)
	at android.app.Activity.onWindowAttributesChanged(Activity.java:3869)
	at androidx.appcompat.view.WindowCallbackWrapper.onWindowAttributesChanged(WindowCallbackWrapper.java:115)
	at android.view.Window.dispatchWindowAttributesChanged(Window.java:1172)
	at com.android.internal.policy.PhoneWindow.dispatchWindowAttributesChanged(PhoneWindow.java:3027)
	at android.view.Window.setFlags(Window.java:1148)

Could you please take a look?
Thanks

@tranb3r
Copy link
Author

tranb3r commented Jun 27, 2023

Also, I've seen the error because I was looking at the logcat while investigating another issue.
Maybe it's not a good idea to catch exceptions in this plugin. Or at least, I think there should be a way for the client to know that something has failed.
What do you think?

@FabriBertani
Copy link
Owner

Hi @tranb3r.

Can you share more details about how you got this exception?

@tranb3r
Copy link
Author

tranb3r commented Jun 27, 2023

The error appears in the logs, but I don't know the exact scenario, and I'm not sure I can build a simple repro for it.
I've managed to workaround it by forcing MainThread when calling your plugin.
But I guess if it's mandatory to call SetFlags from the UI thread, then it's better to do it in the code of the plugin instead of on the client side.

@FabriBertani
Copy link
Owner

Ok, seems you're right, will force the method to run on the UI thread directly from the plugin.

Will add this on the next release.

Thank you!

@FabriBertani FabriBertani added the bug Something isn't working label Jun 28, 2023
@FabriBertani FabriBertani self-assigned this Jun 28, 2023
FabriBertani added a commit that referenced this issue Jul 3, 2023
@tranb3r
Copy link
Author

tranb3r commented Jul 10, 2023

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants