|
| 1 | + |
| 2 | +package com.digitalnomad91.codebuilderadmin |
| 3 | + |
| 4 | +import android.content.Context |
| 5 | +import android.content.Intent |
| 6 | +import android.net.Uri |
| 7 | +import android.provider.Settings |
| 8 | +import android.util.Log |
| 9 | +import com.facebook.react.ReactPackage |
| 10 | +import com.facebook.react.bridge.NativeModule |
| 11 | +import com.facebook.react.bridge.ReactApplicationContext |
| 12 | +import com.facebook.react.bridge.ReactContextBaseJavaModule |
| 13 | +import com.facebook.react.bridge.ReactMethod |
| 14 | +import com.facebook.react.uimanager.ViewManager |
| 15 | + |
| 16 | +class BatteryOptimizationHelper(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { |
| 17 | + |
| 18 | + override fun getName(): String { |
| 19 | + return "BatteryOptimizationHelper" |
| 20 | + } |
| 21 | + |
| 22 | + @ReactMethod |
| 23 | + fun autoHighlightApp() { |
| 24 | + try { |
| 25 | + val context: Context = reactApplicationContext |
| 26 | + val packageName = context.packageName |
| 27 | + |
| 28 | + Log.d("BatteryOptimizationHelper", "Opening battery optimization settings for " + packageName) |
| 29 | + |
| 30 | + // First, try opening the specific battery optimization settings |
| 31 | + val intent = Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS) |
| 32 | + intent.data = Uri.parse("package:" + packageName) |
| 33 | + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 34 | + |
| 35 | + if (intent.resolveActivity(context.packageManager) != null) { |
| 36 | + context.startActivity(intent) |
| 37 | + Log.d("BatteryOptimizationHelper", "Battery optimization settings opened!") |
| 38 | + } else { |
| 39 | + // If direct setting fails, open general battery settings instead |
| 40 | + Log.e("BatteryOptimizationHelper", "Could not resolve activity for battery optimization settings! Trying general battery settings...") |
| 41 | + val generalIntent = Intent(Settings.ACTION_BATTERY_SAVER_SETTINGS) |
| 42 | + generalIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 43 | + |
| 44 | + if (generalIntent.resolveActivity(context.packageManager) != null) { |
| 45 | + context.startActivity(generalIntent) |
| 46 | + Log.d("BatteryOptimizationHelper", "General battery settings opened!") |
| 47 | + } else { |
| 48 | + Log.e("BatteryOptimizationHelper", "Could not resolve activity for general battery settings!") |
| 49 | + } |
| 50 | + } |
| 51 | + } catch (e: Exception) { |
| 52 | + Log.e("BatteryOptimizationHelper", "Error opening battery settings: " + e.message, e) |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +class BatteryOptimizationPackage : ReactPackage { |
| 58 | + override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> { |
| 59 | + return listOf(BatteryOptimizationHelper(reactContext)) |
| 60 | + } |
| 61 | + |
| 62 | + override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> { |
| 63 | + return emptyList() |
| 64 | + } |
| 65 | +} |
0 commit comments