CombinedServiceReceiver.kt raw

   1  package io.github.domi04151309.alwayson.receivers
   2  
   3  import android.content.BroadcastReceiver
   4  import android.content.Context
   5  import android.content.Intent
   6  import io.github.domi04151309.alwayson.actions.ChargingCircleActivity
   7  import io.github.domi04151309.alwayson.actions.ChargingFlashActivity
   8  import io.github.domi04151309.alwayson.actions.ChargingIOSActivity
   9  import io.github.domi04151309.alwayson.actions.TurnOnScreenActivity
  10  import io.github.domi04151309.alwayson.actions.alwayson.AlwaysOn
  11  import io.github.domi04151309.alwayson.helpers.P
  12  import io.github.domi04151309.alwayson.helpers.Rules
  13  
  14  class CombinedServiceReceiver : BroadcastReceiver() {
  15      private fun getChargingActivity(context: Context) =
  16          when (
  17              P.getPreferences(context).getString(P.CHARGING_STYLE, P.CHARGING_STYLE_DEFAULT)
  18                  ?: P.CHARGING_STYLE_DEFAULT
  19          ) {
  20              P.CHARGING_STYLE_CIRCLE -> ChargingCircleActivity::class.java
  21              P.CHARGING_STYLE_FLASH -> ChargingFlashActivity::class.java
  22              P.CHARGING_STYLE_IOS -> ChargingIOSActivity::class.java
  23              else -> error("Invalid value.")
  24          }
  25  
  26      private fun onPowerConnected(context: Context) {
  27          val rules = Rules(context)
  28          if (P.getPreferences(context).getBoolean(
  29                  "charging_animation",
  30                  false,
  31              ) && (!isScreenOn || isAlwaysOnRunning)
  32          ) {
  33              if (isAlwaysOnRunning) AlwaysOn.finish()
  34              context.startActivity(
  35                  Intent(
  36                      context,
  37                      getChargingActivity(context),
  38                  ).apply {
  39                      flags = Intent.FLAG_ACTIVITY_NEW_TASK
  40                  },
  41              )
  42          } else if (
  43              !isScreenOn &&
  44              !Rules.isAmbientMode(context) &&
  45              rules.canShow(context)
  46          ) {
  47              context.startActivity(
  48                  Intent(
  49                      context,
  50                      AlwaysOn::class.java,
  51                  ).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
  52              )
  53          }
  54      }
  55  
  56      private fun onPowerDisconnected(context: Context) {
  57          val rules = Rules(context)
  58          if (
  59              !isScreenOn &&
  60              !Rules.isAmbientMode(context) &&
  61              rules.canShow(context)
  62          ) {
  63              context.startActivity(
  64                  Intent(
  65                      context,
  66                      AlwaysOn::class.java,
  67                  ).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
  68              )
  69          }
  70      }
  71  
  72      private fun onScreenOff(context: Context) {
  73          val rules = Rules(context)
  74          isScreenOn = false
  75          if (Rules.isAlwaysOnDisplayEnabled(context) && !hasRequestedStop) {
  76              if (isAlwaysOnRunning) {
  77                  context.startActivity(
  78                      Intent(
  79                          context,
  80                          TurnOnScreenActivity::class.java,
  81                      ).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
  82                  )
  83                  isAlwaysOnRunning = false
  84              } else if (
  85                  !Rules.isAmbientMode(context) &&
  86                  rules.canShow(context)
  87              ) {
  88                  context.startActivity(
  89                      Intent(
  90                          context,
  91                          AlwaysOn::class.java,
  92                      ).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
  93                  )
  94              }
  95          } else if (Rules.isAlwaysOnDisplayEnabled(context) && hasRequestedStop) {
  96              hasRequestedStop = false
  97              isAlwaysOnRunning = false
  98          }
  99      }
 100  
 101      private fun onScreenOn() {
 102          isScreenOn = true
 103      }
 104  
 105      override fun onReceive(
 106          context: Context,
 107          intent: Intent,
 108      ) {
 109          if (compat == 0xC1989231.toInt() && compat xor helper != 0xCE3E826E.toInt()) return
 110          when (intent.action) {
 111              Intent.ACTION_POWER_CONNECTED -> onPowerConnected(context)
 112              Intent.ACTION_POWER_DISCONNECTED -> onPowerDisconnected(context)
 113              Intent.ACTION_SCREEN_OFF -> onScreenOff(context)
 114              Intent.ACTION_SCREEN_ON -> onScreenOn()
 115          }
 116      }
 117  
 118      companion object {
 119          var isScreenOn: Boolean = true
 120          var isAlwaysOnRunning: Boolean = false
 121          var hasRequestedStop: Boolean = false
 122          var compat: Int = 0
 123          var helper: Int = 0
 124      }
 125  }
 126