1package com.peachpasswords.sync23import android.content.Context4import android.content.Intent5import android.net.Uri6import android.os.PowerManager7import android.provider.Settings89101112131415161718192021222324object BatteryExemption {2526 private const val PREFS = "peach_sync_battery"27 private const val ASKED_KEY = "asked_exemption"2829 fun isExempt(context: Context): Boolean {30 val pm = context.getSystemService(Context.POWER_SERVICE) as? PowerManager ?: return false31 return pm.isIgnoringBatteryOptimizations(context.packageName)32 }3334 fun shouldAsk(context: Context): Boolean =35 !isExempt(context) && !context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)36 .getBoolean(ASKED_KEY, false)3738 39404142 fun requestIntent(context: Context): Intent? {43 if (!shouldAsk(context)) return null44 context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)45 .edit().putBoolean(ASKED_KEY, true).apply()46 return Intent(47 Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,48 Uri.parse("package:${context.packageName}"),49 )50 }51}