Doze Mode battery optimization, device specific issue - android

I implemented ignore battery optimization functionality programmatically.
I found lots of questions and answers but i want to know that the IGNORE_BATTERY_OPTIMIZATIONS is works only for pure android or is there any other cases?
I have couple of devices as below listed :
Moto g4 (7.0)
Moto g play (6.0)
Samsung galaxy note 4(6.0)
Mi note 4 (6.0)
Lenovo Tab(6.0)
Honor (6.0)
It works proper in Moto device.
In Moto we able to get pop up and if we accept permission then it works well.
It work 50% correct in Lenovo tab & Honor devices.
In Lenovo tab when the Permission pop up appears and if we accept the permission then the application added but when we change settings manually it's not working.
Well while in Honor devices when we got permission popup and accept the permission after that we open setting and check the app status its listed in don't allow battery optimization app but the state of the application is "Allow battery optimization". After that when we allow it manually it will work.
In MI and Samsung devices it's not working anymore.
well The Mi Devices are exceptional cases as they have their customized architecture, so we are not concerning MI devices.
for Samsung note 4 there is a special case as below :
After installing app we got the permission pop up and accept the permission. After that we check it in setting, but the app is not listed in "don't allow battery optimization" app list. (Note: When device will reboot the app added in don't allow battery optimization)
Here is Code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
msPackageName = getPackageName();
moPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
Log.i(TAG, "Permission " + moPowerManager.isIgnoringBatteryOptimizations(msPackageName));
if (!moPowerManager.isIgnoringBatteryOptimizations(msPackageName)) {
Intent loIntent = new Intent();
loIntent.setAction(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
loIntent.setData(Uri.parse("package:" + msPackageName));
startActivity(loIntent);
}
}
Please advice for this different behavior. and if there is a solution please let me know that. it's becoming frustrating.

Related

How to enable "Display over other apps" on Redmi A1+

How to enable Display over other apps on Redmi A1+
I have seen a code that could help circumvent the permission for specific apps, but I don't know how to add the code into the manifest.

Companion device pairing: Is location services needed to be enabled?

I have implemented Companion device pairing and it works great for most devices without requiring any location permission or location services enabled. However, we found for example Xiaomi Redmi Note 10 Pro (Android 11) where the BLE scan timeouts when Location services are disabled.
Do I still need to implement requiring Location services enabled before the scan or this is undesirable behavior? I hoped it is not needed anymore with this system-level BLE scan.
If so, is there a way how to distinguish which device needs it? I don't want to force all people when it is not needed (for example my Pixel 5)
In my opinion the Companion Device feature was implemented and designed in a rush. You could expect bugs like the "Location services" must be turned on and the Companion Device pairing dialog doesn't warn when it is not enabled. Until Xiaomi or Google fixes this bug, you will need to have workarounds in your app, for example telling the user to first enable Location services if you think that will be needed.
I am also facing issues with applying a scan filter. It is completely a nightmare.
I have started with this:
val filter = ScanFilter.Builder().apply {
setManufacturerData(SOME_INDEX, byteArrayOf(1))
setDeviceName(SOME_NAME)
setServiceUuid(ParcelUuid.fromString(SOME_UUID))
}.build()
Finally, I have only this:
ScanFilter.Builder().apply {
setManufacturerData(SOME_INDEX, byteArrayOf(1))
}.build()
Because setServiceUuid() is not working for example on all tested Huawei phones and Sony Xperia X.
And setDeviceName() is not working for example on Samsung S10e. Finally I have found solution for name filtering by applying directly:
BluetoothLeDeviceFilter.Builder()
.setNamePattern(Pattern.compile(SOME_NAME))
.setScanFilter(filter)

Bluetooth service optimized, Android

I have an app that uses BLE to exchange data with a BLE-capable device.
I used to develop and test it using Asus Zenfone Max 3 (Android 8.1) and I had no problems.
Then, I got an Asus Zenfone Max Pro M1 (Android 8.1). The app connects to the device but could not exchange any data.
After long investigation, it turns out that I had to go to Battery optimization in the smartphone's settings and change Bluetooth and Bluetooth MIDI Service to Not optimized and then my app worked fine.
I don't know whether this default setting is related to the OS (Android One) or to the phone model. But it is really a crappy thing because I am not supposed to tell every customer to do this (in his phone's settings) in order for the app to work !
So, my question is, is their a way to know, from the code, whether these services are optimized or not, and whether I can change these settings by code or any other ideas that could better fix this issue.
Add this permission to your manifest file:
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
and test it like this :
Intent intent = new Intent();
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm.isIgnoringBatteryOptimizations(packageName))
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
}
context.startActivity(intent);
reference

Ignore Battery Optimisation not working on Oneplus 5

Currently using powerManager.isIgnoringBatteryOptimizations(getPackageName()) for devices running api >= 23.
And requesting user to allow my app to be not optimised by using
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + getPackageName()));
This works on most of the devices, but on Oneplus devices even after user allowing it to be not optimised, I see no effect, my application is still optimised and I miss SMS_RECEIVED broadcast.
Oneplus device with Oxygen OS 5.0.1 and Android OS 8.0.0

Is it me, or is DevicePolicyManager.getStorageEncryptionStatus() broken?

I am trying to determine the storage encryption status of my Android device from within my application. Following the recommendations of the relevant Android Developer page, here is my code:
DevicePolicyManager mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
int encryptionStatus = mDPM.getStorageEncryptionStatus();
if (DEBUG) Log.v(TAG, "checkSecuritySettingsSufficient: encryptionStatus=" + encryptionStatus);
Here's the trouble: when I run this code on a device (I've tried it on a Motorola Droid Maxx running Android 4.4.4 and a Nexus 7 running Android 5.0.2) which I have previously encrypted, DevicePolicyManager.getStorageEncryptionStatus() will always return a value of 1, i.e. ENCRYPTION_STATUS_INACTIVE.
Android is therefore reporting that the device is not encrypted, despite the fact that the file system is definitely encrypted (I checked its status from the Settings > Security page).
Is this function broken? There doesn't seem to be any mention of that on SO or on other web sources. This leads me to believe that I am not doing something correctly with respect to DevicePolicyManager.
UPDATE After running through the encryption steps again with the Motorola device, DevicePolicyManager.getStorageEncryptionStatus() is returning the correct value, but it's still failing on the Nexus 7.
I just ran into this same issue and found out it was happening because the device had disk encryption enabled, but did not require the passcode be entered at startup. Changing the passcode, and forcing the require PIN at startup option to be true made DevicePolicyManager.getStorageEncryptionStatus() correctly return ENCRYPTION_STATUS_ACTIVE.

Categories

Resources