How I resolved WindowManager.BadTokenException for Toast#handleShow()?

Niharika Arora
ProAndroidDev
Published in
4 min readJun 27, 2020

--

“Every problem is a gift — without problems, we would not grow.” ― Anthony Robbins

When we have to display information for a short span of time in Android, we use Toast.

Here are some key features:

  • It provides simple feedback about an operation in a small popup.
  • It is a view containing a quick little message for the user
  • It only fills the amount of space required for the message and the current activity remains visible and interactive.

Recently, I started getting a major number of crashes for Toast#handleShow() on Crashlytics. Here are the logs:

Before the analysis, it's important for you to know about WindowManager.BadTokenException.

Exception that is thrown when trying to add view whose LayoutParams LayoutParams#token is invalid.

Lets, Look at the WindowManagerService.java file.

WindowManagerService (WMS) is a system service that manages the windows on Android.

Window Tokens:

As the name suggest, a window token is a special type of Binder token used by window manager to uniquely identify a window in the system. Window tokens are important for security reasons because they make it impossible for malicious applications to draw on top of the windows of other applications. The window manager protects against this by requiring applications to pass their application’s window token as part of each request to add or remove a window. If the tokens don’t match, the window manager rejects the request and throws a BadTokenException. Without window tokens, this necessary identification step wouldn’t be possible and the window manager wouldn’t be able to protect itself from malicious applications.

A real-world scenario:

When an application starts up for the first time, the ActivityManagerService creates a special kind of window token called an application window token, which uniquely identifies the application’s top-level container window. The activity manager gives this token to both the (*application) and the window manager, and it sends the token to the window manager each time it wants to add a new window to the screen. This ensures secure interaction between the application and the window manager (by making it impossible to add windows on top of other applications), and also makes it easy for the activity manager to make direct requests to the window manager.

And the code that throws “BadTokenException”

Return WindowManagerGlobal.ADD_BAD_APP_TOKEN

If returned the WindowManagerGlobal.ADD_BAD_APP_TOKEN, the exception occurs when the WMS.addWindow () function is called to check whether the window to be added is not in violation of the policy according to the android window policy.

So now, after all, my research and analysis on BadTokenException, what I noticed about all the crashes there on Crashltyics:

All were on Android 7.1 (API level 25).

I compared the API level 25 Toast class and others for the difference and I found an interesting thing there:

From API 25, Android added a new param IBinder windowToken for Toast#handleShow(), and It brought an exception. 😪

Let me show you the code:

As you can see here, they try-catch the mWM.addView(mView, mParams)

Since the notification manager service cancels the token right after it notifies us to cancel the toast there is an inherent race and we may attempt to add a window after the token has been invalidated. Let us hedge against that.

However, API level 25 is still at risk. We had to capture it on our own else it would have continued to produce an exception for the users.

This exception occurs regardless of whether the Context you passed to Toast is an Activity or ApplicationContext or Service. And you can not try-catch it.

Solution :

So I created a library that possesses a ToastHandler class which is responsible for showing Toast smoothly on all API versions and replaced the base Context to a ToastContextWrapper, it will hook the WindowManagerWrapper.addView(view, params) method and fix the exception.

You just need to add this in your app/build.gradle:

Now, you are all good to use your Toast :

Also, If you forget to use it, Let the Android Lint help you :

With the latest update(1.2.0 & above), you will find a custom lint added which disallows the usage of Android’s Toast class in favour of ToastHandler.

This lint replaces Toast.makeText with ToastHandler.getToastInstance.

- Toast.makeText(this, "Hi, I am Toast", Toast.LENGTH_SHORT).show()
+ ToastHandler.getToastInstance(this, "Hi, I am Toast", Toast.LENGTH_SHORT).show()

Here’s a small GIF showing how this will look in the IDE.

Just relax and have a cup of coffee!! :-)

You can find the source code here:

That’s all from my side!!

Originally published here.

Do share your feedback and suggestions in the comments section below.

If you liked this blog, hit the 👏 . Stay tuned for the next one!

--

--