I am trying to check if Android 12 is optimizing my app battery usage, so I used isIgnoringBatteryOptimizations method ( https://developer.android.com/reference/android/os/PowerManager#isIgnoringBatteryOptimizations(java.lang.String)). But isIgnoringBatteryOptimizations returns a boolean as yes, it's optimized, while Android 12 has 3 levels of optimizations ( check screenshot ) :
- Unrestricted
- Optimized
- Restricted
My app works fine in case of 1 & 2, I want to show a warning if it's in Restricted mode.

After some research, many other posts try to solve this question with the implementation of the following code:
Add to Manifest :
Use this function to request optimization:
As you mentioned in the post, isIgnoringBatteryOptimizations will be true only in case 1. Appart from that, it looks like Google won't be very happy with this approach and your app may not be published.
I found out the following approach to work more conveniently:
This code works if you want to find case 3. Please take into consideration that case 3 is only relevant in Android API 28 and onwards, apps shouldn't be killed in the background when API < 28, there's no case 3 when API < 28. Hope it helps.