├── .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 |
6 |
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 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/android/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/android/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/android/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
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 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
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 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
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 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 | 1571041930096
204 |
205 |
206 | 1571041930096
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 | Android API 26 Platform
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
--------------------------------------------------------------------------------
/android/build.gradle:
--------------------------------------------------------------------------------
1 |
2 | buildscript {
3 | repositories {
4 | jcenter()
5 | }
6 |
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:1.3.1'
9 | }
10 | }
11 |
12 | apply plugin: 'com.android.library'
13 |
14 | android {
15 | compileSdkVersion 23
16 | buildToolsVersion "23.0.1"
17 |
18 | defaultConfig {
19 | minSdkVersion 16
20 | targetSdkVersion 22
21 | versionCode 1
22 | versionName "1.0"
23 | }
24 | lintOptions {
25 | abortOnError false
26 | }
27 | }
28 |
29 | repositories {
30 | mavenCentral()
31 | }
32 |
33 | dependencies {
34 | implementation fileTree(include: ['*.jar'], dir: 'libs')
35 | implementation "com.facebook.react:react-native:+"
36 | }
37 |
38 |
--------------------------------------------------------------------------------
/android/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/android/src/main/java/com/reactlibrary/disablebatteryoptimizationsandroid/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
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/src/main/java/com/reactlibrary/disablebatteryoptimizationsandroid/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/android/src/main/java/com/reactlibrary/disablebatteryoptimizationsandroid/.idea/reactlibrary.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/android/src/main/java/com/reactlibrary/disablebatteryoptimizationsandroid/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
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 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
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 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 | 1571041653772
132 |
133 |
134 | 1571041653772
135 |
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 |
178 |
179 |
180 |
181 |
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 |
--------------------------------------------------------------------------------