├── .gitattributes ├── README.md ├── _config.yml ├── android ├── .idea │ ├── codeStyles │ │ └── Project.xml │ ├── gradle.xml │ ├── misc.xml │ ├── modules.xml │ ├── runConfigurations.xml │ └── workspace.xml ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── reactlibrary │ └── disablebatteryoptimizationsandroid │ ├── .idea │ ├── codeStyles │ │ └── Project.xml │ ├── modules.xml │ ├── reactlibrary.iml │ └── workspace.xml │ ├── RNDisableBatteryOptimizationsModule.java │ └── RNDisableBatteryOptimizationsPackage.java ├── index.js └── package.json /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-disable-battery-optimizations-android 2 | 3 | ## Getting started 4 | 5 | `$ npm install react-native-disable-battery-optimizations-android --save` 6 | 7 | ### Mostly automatic installation for react native >= v 0.60 8 | Manual linking not required for above versions 9 | 10 | For manual linking : 11 | `$ react-native link react-native-disable-battery-optimizations-android` 12 | 13 | ### Manual installation 14 | 15 | 16 | 17 | #### Android 18 | 19 | 1. Open up `android/app/src/main/java/[...]/MainActivity.java` 20 | - Add `import com.reactlibrary.RNDisableBatteryOptimizationsandroidPackage;` to the imports at the top of the file 21 | - Add `new RNDisableBatteryOptimizationsPackage()` to the list returned by the `getPackages()` method 22 | 2. Append the following lines to `android/settings.gradle`: 23 | ``` 24 | include ':react-native-disable-battery-optimizations-android' 25 | project(':react-native-disable-battery-optimizations-android').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-disable-battery-optimizations-android/android') 26 | ``` 27 | 3. Insert the following lines inside the dependencies block in `android/app/build.gradle`: 28 | ``` 29 | compile project(':react-native-disable-battery-optimizations-android') 30 | ``` 31 | 32 | ## Usage 33 | ```javascript 34 | import RNDisableBatteryOptimizationsAndroid from 'react-native-disable-battery-optimizations-android'; 35 | 36 | RNDisableBatteryOptimizationsAndroid.isBatteryOptimizationEnabled().then((isEnabled)=>{ 37 | if(isEnabled){ 38 | RNDisableBatteryOptimizationsAndroid.openBatteryModal(); 39 | } 40 | }); 41 | 42 | // for direct whitelisting app with Instant Dialogue 43 | //The user will be taken to a screen where they can indicate that they are willing to suspend portions of Doze mode effects on your app. 44 | Note: Play store will not allow using this method, and will allow only as an exception 45 | 46 | 47 | RNDisableBatteryOptimizationsAndroid.enableBackgroundServicesDialogue(); 48 | 49 | Also 50 | Add this permission in your AndroidManifest.xml 51 | 52 | 53 | 54 | 55 | ``` 56 | 57 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /android/.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | xmlns:android 14 | 15 | ^$ 16 | 17 | 18 | 19 |
20 |
21 | 22 | 23 | 24 | xmlns:.* 25 | 26 | ^$ 27 | 28 | 29 | BY_NAME 30 | 31 |
32 |
33 | 34 | 35 | 36 | .*:id 37 | 38 | http://schemas.android.com/apk/res/android 39 | 40 | 41 | 42 |
43 |
44 | 45 | 46 | 47 | .*:name 48 | 49 | http://schemas.android.com/apk/res/android 50 | 51 | 52 | 53 |
54 |
55 | 56 | 57 | 58 | name 59 | 60 | ^$ 61 | 62 | 63 | 64 |
65 |
66 | 67 | 68 | 69 | style 70 | 71 | ^$ 72 | 73 | 74 | 75 |
76 |
77 | 78 | 79 | 80 | .* 81 | 82 | ^$ 83 | 84 | 85 | BY_NAME 86 | 87 |
88 |
89 | 90 | 91 | 92 | .* 93 | 94 | http://schemas.android.com/apk/res/android 95 | 96 | 97 | ANDROID_ATTRIBUTE_ORDER 98 | 99 |
100 |
101 | 102 | 103 | 104 | .* 105 | 106 | .* 107 | 108 | 109 | BY_NAME 110 | 111 |
112 |
113 |
114 |
115 |
116 |
-------------------------------------------------------------------------------- /android/.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 12 | 13 | -------------------------------------------------------------------------------- /android/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 17 | -------------------------------------------------------------------------------- /android/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /android/.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /android/.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 45 | 46 | 47 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 58 | 59 | 60 | 61 | 62 | 63 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 102 | 109 | 110 | 112 | 113 | 118 | 119 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 1571041653772 132 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | Android API 26 Platform 176 | 177 | 182 | 183 | 184 | 185 | 186 | 187 | -------------------------------------------------------------------------------- /android/src/main/java/com/reactlibrary/disablebatteryoptimizationsandroid/RNDisableBatteryOptimizationsModule.java: -------------------------------------------------------------------------------- 1 | package com.reactlibrary.disablebatteryoptimizationsandroid; 2 | 3 | import com.facebook.react.bridge.ReactApplicationContext; 4 | import com.facebook.react.bridge.ReactContextBaseJavaModule; 5 | import com.facebook.react.bridge.ReactMethod; 6 | import com.facebook.react.bridge.Promise; 7 | 8 | import android.content.Intent; 9 | import android.provider.Settings; 10 | import android.os.PowerManager; 11 | import android.net.Uri; 12 | import android.os.Build; 13 | 14 | 15 | public class RNDisableBatteryOptimizationsModule extends ReactContextBaseJavaModule { 16 | 17 | private final ReactApplicationContext reactContext; 18 | 19 | public RNDisableBatteryOptimizationsModule(ReactApplicationContext reactContext) { 20 | super(reactContext); 21 | this.reactContext = reactContext; 22 | } 23 | 24 | @ReactMethod 25 | public void openBatteryModal() { 26 | if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 27 | String packageName = reactContext.getPackageName(); 28 | Intent intent = new Intent(); 29 | intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); 30 | intent.setData(Uri.parse("package:" + packageName)); 31 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 32 | reactContext.startActivity(intent); 33 | 34 | } 35 | 36 | } 37 | 38 | @ReactMethod 39 | public void isBatteryOptimizationEnabled(Promise promise) { 40 | if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 41 | String packageName = reactContext.getPackageName(); 42 | PowerManager pm = (PowerManager) reactContext.getSystemService(reactContext.POWER_SERVICE); 43 | if (!pm.isIgnoringBatteryOptimizations(packageName)) { 44 | promise.resolve(true); 45 | return ; 46 | } 47 | } 48 | promise.resolve(false); 49 | } 50 | 51 | 52 | 53 | 54 | @ReactMethod 55 | public void enableBackgroundServicesDialogue() { 56 | if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 57 | Intent myIntent = new Intent(); 58 | String packageName = reactContext.getPackageName(); 59 | PowerManager pm = (PowerManager) reactContext.getSystemService(reactContext.POWER_SERVICE); 60 | if (pm.isIgnoringBatteryOptimizations(packageName)) 61 | myIntent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); 62 | else { 63 | myIntent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); 64 | myIntent.setData(Uri.parse("package:" + packageName)); 65 | } 66 | myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 67 | reactContext.startActivity(myIntent); 68 | } 69 | } 70 | 71 | @Override 72 | public String getName() { 73 | return "RNDisableBatteryOptimizationsAndroid"; 74 | } 75 | } -------------------------------------------------------------------------------- /android/src/main/java/com/reactlibrary/disablebatteryoptimizationsandroid/RNDisableBatteryOptimizationsPackage.java: -------------------------------------------------------------------------------- 1 | package com.reactlibrary.disablebatteryoptimizationsandroid; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collections; 5 | import java.util.List; 6 | 7 | import com.facebook.react.ReactPackage; 8 | import com.facebook.react.bridge.NativeModule; 9 | import com.facebook.react.bridge.ReactApplicationContext; 10 | import com.facebook.react.uimanager.ViewManager; 11 | import com.facebook.react.bridge.JavaScriptModule; 12 | 13 | public class RNDisableBatteryOptimizationsPackage implements ReactPackage { 14 | @Override 15 | public List createNativeModules(ReactApplicationContext reactContext) { 16 | return Arrays.asList(new RNDisableBatteryOptimizationsModule(reactContext)); 17 | } 18 | 19 | // Deprecated from RN 0.47 20 | public List> createJSModules() { 21 | return Collections.emptyList(); 22 | } 23 | 24 | @Override 25 | public List createViewManagers(ReactApplicationContext reactContext) { 26 | return Collections.emptyList(); 27 | } 28 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | import { NativeModules } from 'react-native'; 3 | 4 | const { RNDisableBatteryOptimizationsAndroid } = NativeModules; 5 | 6 | export default RNDisableBatteryOptimizationsAndroid; 7 | 8 | 9 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-disable-battery-optimizations-android", 3 | "version": "1.0.6", 4 | "description": "A react native library for checking battery optimization and whitelisting in Android", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [ 10 | "react-native", 11 | "settings", 12 | "android", 13 | "open", 14 | "battery-optimizations", 15 | "background service" 16 | ], 17 | "repository": { 18 | "type": "git", 19 | "url": "git+https://github.com/rasheedk/react-native-disable-battery-optimizations-android.git" 20 | }, 21 | "author": "@rasheedk", 22 | "license": "", 23 | "peerDependencies": { 24 | "react-native": ">0.41.2" 25 | } 26 | } 27 | --------------------------------------------------------------------------------