Skip to content

Latest commit

 

History

History
82 lines (62 loc) · 3.09 KB

README.md

File metadata and controls

82 lines (62 loc) · 3.09 KB

BatteryPermissionHelper

License: MIT Android Build Unit Tests API

An Android Library to detect battery optimizations settings and navigate the user to the relevant screens so that they can whitelist the app. This will prevent the app from getting killed in the background due to Battery Optimization Managers. The library supports multiple OEMs.

Installation

Gradle

Add this to your module's build.gradle file:

dependencies {
    // ... other dependencies
    implementation 'com.waseemsabir:betterypermissionhelper:1.0.3'    
}

Maven

<dependency>
    <groupId>com.waseemsabir</groupId>
    <artifactId>betterypermissionhelper</artifactId>
    <version>1.0.3</version>
</dependency>

Other instructions for installation can be viewed here.

Usage

To keep the library small and extendable, UI elements (dialog/popups) implementation is left up to the library user. The library will not show any screens.

Kotlin

private val batteryPermissionHelper = BatteryPermissionHelper.getInstance()

// check whether or not Battery Permission is Available for Device
val isBatteryPermissionAvailable = batteryPermissionHelper.isBatterySaverPermissionAvailable(context = context, onlyIfSupported = true)

// Show a dialog based on availability (Implementation left to Dev) and OnClick open permission manager
buttonInDialog.setOnClickListener {
    batteryPermissionHelper.getPermission(this, open = true, newTask = true)
}

Java

private final BatteryPermissionHelper batteryPermissionHelper = BatteryPermissionHelper.Companion.getInstance();

// Check whether or not Battery Permission is Available for Device
boolean isBatteryPermissionAvailable = batteryPermissionHelper.isBatterySaverPermissionAvailable(context, /* onlyIfSupported */ true);

// Show a dialog based on availability (Implementation left to Dev) and OnClick open permission manager
buttonInDialog.setOnClickListener((view) -> {
    batteryPermissionHelper.getPermission(context, /* open */ true, /* newTask */ true);
});

Supported Devices

Currently, the library supports following devices.

  1. Xiaomi
  2. Redmi
  3. Poco
  4. Letv [ Untested ]
  5. HTC [ Untested ]
  6. Huawei
  7. Oppo
  8. Samsung
  9. ZTE [ Untested ]
  10. Meizu [ Untested ]

I am open to any PR's and contributions for any devices. I'll also try to add more devices, whenever possible.

Relevant Work