@@ -8,31 +8,20 @@ const androidManifestPath = path.join(
8
8
__dirname ,
9
9
"../android/app/src/main/AndroidManifest.xml"
10
10
) ;
11
- const javaMainApplicationPath = path . join (
12
- __dirname ,
13
- "../android/app/src/main/java/com/digitalnomad91/codebuilderadmin/MainApplication.java"
14
- ) ;
15
11
const kotlinMainApplicationPath = path . join (
16
12
__dirname ,
17
13
"../android/app/src/main/java/com/digitalnomad91/codebuilderadmin/MainApplication.kt"
18
14
) ;
19
- const javaHelperPath = path . join (
20
- __dirname ,
21
- "../android/app/src/main/java/com/digitalnomad91/codebuilderadmin/BatteryOptimizationHelper.java"
22
- ) ;
23
15
const kotlinHelperPath = path . join (
24
16
__dirname ,
25
17
"../android/app/src/main/java/com/digitalnomad91/codebuilderadmin/BatteryOptimizationHelper.kt"
26
18
) ;
27
19
28
- // Determine whether the project uses Java or Kotlin
29
- const isKotlin = fs . existsSync ( kotlinMainApplicationPath ) ;
30
- const mainApplicationPath = isKotlin
31
- ? kotlinMainApplicationPath
32
- : javaMainApplicationPath ;
33
- const helperPath = isKotlin ? kotlinHelperPath : javaHelperPath ;
20
+ // Check if the app uses Kotlin
21
+ const mainApplicationPath = kotlinMainApplicationPath ;
22
+ const helperPath = kotlinHelperPath ;
34
23
35
- console . log ( ` 🔍 Detected ${ isKotlin ? " Kotlin" : "Java" } project` ) ;
24
+ console . log ( " 🔍 Detected Kotlin project" ) ;
36
25
37
26
// 🟢 1. Ensure `AndroidManifest.xml` has the required permission
38
27
const addPermissionToManifest = async ( ) => {
@@ -41,7 +30,6 @@ const addPermissionToManifest = async () => {
41
30
const permission =
42
31
'<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />' ;
43
32
44
- // Ensure the permission is inside <manifest> but before <application>
45
33
if ( ! manifest . includes ( permission ) ) {
46
34
manifest = manifest . replace (
47
35
/ < m a n i f e s t [ ^ > ] * > / ,
@@ -62,57 +50,24 @@ const addPermissionToManifest = async () => {
62
50
}
63
51
} ;
64
52
65
- // 🟢 2. Create the `BatteryOptimizationHelper` module in either Java or Kotlin
66
- const javaHelperCode = `
67
- package com.digitalnomad91.codebuilderadmin;
68
-
69
- import android.content.Context;
70
- import android.content.Intent;
71
- import android.net.Uri;
72
- import android.provider.Settings;
73
- import com.facebook.react.bridge.ReactApplicationContext;
74
- import com.facebook.react.bridge.ReactContextBaseJavaModule;
75
- import com.facebook.react.bridge.ReactMethod;
76
-
77
- public class BatteryOptimizationHelper extends ReactContextBaseJavaModule {
78
-
79
- public BatteryOptimizationHelper(ReactApplicationContext reactContext) {
80
- super(reactContext);
81
- }
82
-
83
- @Override
84
- public String getName() {
85
- return "BatteryOptimizationHelper";
86
- }
87
-
88
- @ReactMethod
89
- public void autoHighlightApp() {
90
- try {
91
- Context context = getReactApplicationContext();
92
- Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
93
- intent.setData(Uri.parse("package:${ packageName } "));
94
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
95
- context.startActivity(intent);
96
- } catch (Exception e) {
97
- e.printStackTrace();
98
- }
99
- }
100
- }
101
- ` ;
102
-
53
+ // 🟢 2. Create `BatteryOptimizationHelper.kt` with improved logic
103
54
const kotlinHelperCode = `
104
- package com.digitalnomad91.codebuilderadmin
55
+ package ${ packageName }
105
56
106
57
import android.content.Context
107
58
import android.content.Intent
108
59
import android.net.Uri
109
60
import android.provider.Settings
61
+ import android.util.Log
62
+ import com.facebook.react.ReactPackage
63
+ import com.facebook.react.bridge.NativeModule
110
64
import com.facebook.react.bridge.ReactApplicationContext
111
65
import com.facebook.react.bridge.ReactContextBaseJavaModule
112
66
import com.facebook.react.bridge.ReactMethod
67
+ import com.facebook.react.uimanager.ViewManager
113
68
114
69
class BatteryOptimizationHelper(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
115
-
70
+
116
71
override fun getName(): String {
117
72
return "BatteryOptimizationHelper"
118
73
}
@@ -121,42 +76,63 @@ class BatteryOptimizationHelper(reactContext: ReactApplicationContext) : ReactCo
121
76
fun autoHighlightApp() {
122
77
try {
123
78
val context: Context = reactApplicationContext
124
- val packageName = context.packageName // ✅ FIX: Dynamically retrieve package name
79
+ val packageName = context.packageName
80
+
81
+ Log.d("BatteryOptimizationHelper", "Opening battery optimization settings for " + packageName)
125
82
83
+ // First, try opening the specific battery optimization settings
126
84
val intent = Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
127
- intent.data = Uri.parse("package:$packageName") // ✅ FIX: Use correct package name
85
+ intent.data = Uri.parse("package:" + packageName)
128
86
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
129
- context.startActivity(intent)
87
+
88
+ if (intent.resolveActivity(context.packageManager) != null) {
89
+ context.startActivity(intent)
90
+ Log.d("BatteryOptimizationHelper", "Battery optimization settings opened!")
91
+ } else {
92
+ // If direct setting fails, open general battery settings instead
93
+ Log.e("BatteryOptimizationHelper", "Could not resolve activity for battery optimization settings! Trying general battery settings...")
94
+ val generalIntent = Intent(Settings.ACTION_BATTERY_SAVER_SETTINGS)
95
+ generalIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
96
+
97
+ if (generalIntent.resolveActivity(context.packageManager) != null) {
98
+ context.startActivity(generalIntent)
99
+ Log.d("BatteryOptimizationHelper", "General battery settings opened!")
100
+ } else {
101
+ Log.e("BatteryOptimizationHelper", "Could not resolve activity for general battery settings!")
102
+ }
103
+ }
130
104
} catch (e: Exception) {
131
- e.printStackTrace( )
105
+ Log.e("BatteryOptimizationHelper", "Error opening battery settings: " + e.message, e )
132
106
}
133
107
}
134
108
}
109
+
110
+ class BatteryOptimizationPackage : ReactPackage {
111
+ override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
112
+ return listOf(BatteryOptimizationHelper(reactContext))
113
+ }
114
+
115
+ override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
116
+ return emptyList()
117
+ }
118
+ }
135
119
` ;
136
120
137
121
const createBatteryOptimizationHelper = async ( ) => {
138
122
try {
139
- const helperCode = isKotlin ? kotlinHelperCode : javaHelperCode ;
140
123
await fs . ensureFile ( helperPath ) ;
141
- await fs . writeFile ( helperPath , helperCode , "utf8" ) ;
142
- console . log (
143
- `✅ BatteryOptimizationHelper.${ isKotlin ? "kt" : "java" } created`
144
- ) ;
124
+ await fs . writeFile ( helperPath , kotlinHelperCode , "utf8" ) ;
125
+ console . log ( "✅ BatteryOptimizationHelper.kt created" ) ;
145
126
} catch ( error ) {
146
- console . error (
147
- `❌ Error creating BatteryOptimizationHelper.${
148
- isKotlin ? "kt" : "java"
149
- } :`,
150
- error
151
- ) ;
127
+ console . error ( "❌ Error creating BatteryOptimizationHelper.kt:" , error ) ;
152
128
}
153
129
} ;
154
130
155
- // 🟢 3. Modify `MainApplication` to register the new module
131
+ // 🟢 3. Modify `MainApplication.kt ` to register the module correctly
156
132
const modifyMainApplication = async ( ) => {
157
133
try {
158
134
let mainApplication = await fs . readFile ( mainApplicationPath , "utf8" ) ;
159
- const importStatement = `import com.digitalnomad91.codebuilderadmin.BatteryOptimizationHelper ` ;
135
+ const importStatement = `import ${ packageName } .BatteryOptimizationPackage ` ;
160
136
161
137
if ( ! mainApplication . includes ( importStatement ) ) {
162
138
mainApplication = mainApplication . replace (
@@ -165,36 +141,30 @@ const modifyMainApplication = async () => {
165
141
) ;
166
142
}
167
143
168
- if ( isKotlin ) {
169
- const registerModule =
170
- "BatteryOptimizationHelper(reactNativeHost.reactInstanceManager.currentReactContext!!)," ;
171
- if ( ! mainApplication . includes ( registerModule ) ) {
172
- mainApplication = mainApplication . replace (
173
- "packages = mutableListOf(" ,
174
- `packages = mutableListOf(\n ${ registerModule } `
175
- ) ;
176
- }
177
- } else {
178
- const registerModule = "new BatteryOptimizationHelper()," ;
144
+ // ✅ Detect Expo’s New Architecture
145
+ const isNewArch = mainApplication . includes ( "DefaultReactNativeHost" ) ;
146
+
147
+ if ( isNewArch ) {
148
+ console . log (
149
+ "🔍 Detected Expo New Architecture. Adjusting `MainApplication.kt`..."
150
+ ) ;
151
+
152
+ const registerModule = "BatteryOptimizationPackage()" ;
153
+
179
154
if ( ! mainApplication . includes ( registerModule ) ) {
180
155
mainApplication = mainApplication . replace (
181
- "new MainReactPackage()," ,
182
- `new MainReactPackage(), \n ${ registerModule } `
156
+ / v a l p a c k a g e s = P a c k a g e L i s t \( t h i s \) \. p a c k a g e s / ,
157
+ `val packages = PackageList(this).packages.toMutableList() \n packages.add( ${ registerModule } ) `
183
158
) ;
184
159
}
185
160
}
186
161
187
162
await fs . writeFile ( mainApplicationPath , mainApplication , "utf8" ) ;
188
163
console . log (
189
- `✅ BatteryOptimizationHelper registered in MainApplication.${
190
- isKotlin ? "kt" : "java"
191
- } `
164
+ "✅ BatteryOptimizationPackage registered in MainApplication.kt"
192
165
) ;
193
166
} catch ( error ) {
194
- console . error (
195
- `❌ Error updating MainApplication.${ isKotlin ? "kt" : "java" } :` ,
196
- error
197
- ) ;
167
+ console . error ( "❌ Error updating MainApplication.kt:" , error ) ;
198
168
}
199
169
} ;
200
170
0 commit comments