MainActivity.kt raw

   1  package io.github.domi04151309.alwayson.activities
   2  
   3  import android.Manifest
   4  import android.annotation.SuppressLint
   5  import android.content.ComponentName
   6  import android.content.DialogInterface
   7  import android.content.Intent
   8  import android.os.Bundle
   9  import android.provider.Settings
  10  import android.service.quicksettings.TileService
  11  import android.widget.ImageView
  12  import android.widget.TextView
  13  import androidx.activity.addCallback
  14  import androidx.core.app.ActivityCompat
  15  import androidx.core.content.ContextCompat
  16  import androidx.preference.Preference
  17  import com.google.android.material.dialog.MaterialAlertDialogBuilder
  18  import com.google.android.material.elevation.SurfaceColors
  19  import io.github.domi04151309.alwayson.BuildConfig
  20  import io.github.domi04151309.alwayson.R
  21  import io.github.domi04151309.alwayson.custom.BasePreferenceFragment
  22  import io.github.domi04151309.alwayson.helpers.Global
  23  import io.github.domi04151309.alwayson.helpers.P
  24  import io.github.domi04151309.alwayson.helpers.Permissions
  25  import io.github.domi04151309.alwayson.helpers.PreferenceScreenHelper
  26  import io.github.domi04151309.alwayson.receivers.AlwaysOnAppWidgetProvider
  27  import io.github.domi04151309.alwayson.services.AlwaysOnTileService
  28  import io.github.domi04151309.alwayson.services.ForegroundService
  29  
  30  class MainActivity : BaseActivity() {
  31      enum class Dialog {
  32          DEVICE_ADMIN,
  33          NOTIFICATION_ACCESS,
  34          DISPLAY_OVER_OTHER_APPS,
  35          PHONE_STATE,
  36          CALENDAR,
  37      }
  38  
  39      override fun onCreate(savedInstanceState: Bundle?) {
  40          super.onCreate(savedInstanceState)
  41          setContentView(R.layout.activity_main)
  42  
  43          window.statusBarColor = SurfaceColors.SURFACE_0.getColor(this)
  44  
  45          supportFragmentManager
  46              .beginTransaction()
  47              .replace(R.id.settings, GeneralPreferenceFragment())
  48              .commit()
  49  
  50          ContextCompat.startForegroundService(this, Intent(this, ForegroundService::class.java))
  51  
  52          onBackPressedDispatcher.addCallback {
  53              startActivity(
  54                  Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME)
  55                      .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
  56              )
  57          }
  58      }
  59  
  60      override fun onStart() {
  61          super.onStart()
  62  
  63          if (Permissions.needsNotificationPermissions(this)) buildDialog(Dialog.NOTIFICATION_ACCESS)
  64          if (Permissions.needsDeviceAdminOrRoot(this)) buildDialog(Dialog.DEVICE_ADMIN)
  65          if (Permissions.needsCalendarPermission(this)) buildDialog(Dialog.CALENDAR)
  66  
  67          if (!Permissions.hasPhoneStatePermission(this)) buildDialog(Dialog.PHONE_STATE)
  68          if (!Settings.canDrawOverlays(this)) buildDialog(Dialog.DISPLAY_OVER_OTHER_APPS)
  69      }
  70  
  71      @SuppressLint("InflateParams")
  72      private fun showDialog(
  73          icon: Int,
  74          title: Int,
  75          message: Int,
  76          onOk: DialogInterface.OnClickListener,
  77      ) {
  78          val builder = MaterialAlertDialogBuilder(this)
  79          val view = layoutInflater.inflate(R.layout.dialog_permission, null, false)
  80          view.findViewById<ImageView>(R.id.icon).setImageResource(icon)
  81          view.findViewById<TextView>(R.id.title).setText(title)
  82          view.findViewById<TextView>(R.id.message).setText(message)
  83          builder.setPositiveButton(resources.getString(android.R.string.ok), onOk)
  84          builder.setView(view)
  85          builder.setNegativeButton(resources.getString(android.R.string.cancel)) { dialog, _ ->
  86              dialog.cancel()
  87          }
  88          builder.show()
  89      }
  90  
  91      @SuppressLint("InflateParams")
  92      private fun buildDialog(dialogType: Dialog) {
  93          when (dialogType) {
  94              Dialog.DEVICE_ADMIN ->
  95                  showDialog(
  96                      R.drawable.ic_color_mode,
  97                      R.string.device_admin,
  98                      R.string.device_admin_summary,
  99                  ) { _, _ ->
 100                      startActivity(Intent(this, PermissionsActivity::class.java))
 101                  }
 102  
 103              Dialog.DISPLAY_OVER_OTHER_APPS ->
 104                  showDialog(
 105                      R.drawable.ic_color_draw_over_other_apps,
 106                      R.string.setup_draw_over_other_apps,
 107                      R.string.setup_draw_over_other_apps_summary,
 108                  ) { _, _ ->
 109                      startActivity(Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION))
 110                  }
 111  
 112              Dialog.NOTIFICATION_ACCESS ->
 113                  showDialog(
 114                      R.drawable.ic_color_notification,
 115                      R.string.notification_listener_service,
 116                      R.string.notification_listener_service_summary,
 117                  ) { _, _ ->
 118                      startActivity(Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"))
 119                  }
 120  
 121              Dialog.PHONE_STATE ->
 122                  showDialog(
 123                      R.drawable.ic_color_phone_state,
 124                      R.string.setup_phone_state,
 125                      R.string.setup_phone_state_summary,
 126                  ) { _, _ ->
 127                      ActivityCompat.requestPermissions(
 128                          this,
 129                          arrayOf(Manifest.permission.READ_PHONE_STATE),
 130                          0,
 131                      )
 132                  }
 133  
 134              Dialog.CALENDAR ->
 135                  showDialog(
 136                      R.drawable.ic_color_calendar,
 137                      R.string.setup_calendar,
 138                      R.string.setup_calendar_summary,
 139                  ) { _, _ ->
 140                      ActivityCompat.requestPermissions(
 141                          this,
 142                          arrayOf(Manifest.permission.READ_CALENDAR),
 143                          0,
 144                      )
 145                  }
 146          }
 147      }
 148  
 149      class GeneralPreferenceFragment : BasePreferenceFragment() {
 150          private var debugClicker = 0
 151  
 152          @Suppress("SameReturnValue")
 153          private fun onAlwaysOnClicked(): Boolean {
 154              TileService.requestListeningState(
 155                  context,
 156                  ComponentName(requireContext(), AlwaysOnTileService::class.java),
 157              )
 158              requireContext().sendBroadcast(
 159                  Intent(context, AlwaysOnAppWidgetProvider::class.java)
 160                      .setAction(Global.ALWAYS_ON_STATE_CHANGED),
 161              )
 162              if (BuildConfig.DEBUG) {
 163                  debugClicker++
 164                  if (debugClicker == CLICKS_TIL_TEST) {
 165                      debugClicker = 0
 166                      startActivity(Intent(requireContext(), AODTestActivity::class.java))
 167                  }
 168              }
 169              return true
 170          }
 171  
 172          override fun onCreatePreferences(
 173              savedInstanceState: Bundle?,
 174              rootKey: String?,
 175          ) {
 176              addPreferencesFromResource(R.xml.pref_main)
 177              checkPermissions()
 178  
 179              findPreference<Preference>(P.ALWAYS_ON)?.setOnPreferenceClickListener {
 180                  onAlwaysOnClicked()
 181              }
 182              PreferenceScreenHelper.linkPreferenceToActivity(
 183                  this,
 184                  "pref_watch_face",
 185                  Intent(requireContext(), LAFWatchFaceActivity::class.java),
 186              )
 187              PreferenceScreenHelper.linkPreferenceToActivity(
 188                  this,
 189                  "pref_background",
 190                  Intent(requireContext(), LAFBackgroundActivity::class.java),
 191              )
 192              PreferenceScreenHelper.linkPreferenceToActivity(
 193                  this,
 194                  "rules",
 195                  Intent(requireContext(), LAFRulesActivity::class.java),
 196              )
 197              PreferenceScreenHelper.linkPreferenceToActivity(
 198                  this,
 199                  "pref_behavior",
 200                  Intent(requireContext(), LAFBehaviorActivity::class.java),
 201              )
 202              PreferenceScreenHelper.linkPreferenceToActivity(
 203                  this,
 204                  P.CHARGING_STYLE,
 205                  Intent(requireContext(), LAFChargingLookActivity::class.java),
 206              )
 207              findPreference<Preference>("dark_mode")?.setOnPreferenceClickListener {
 208                  activity?.recreate()
 209                  true
 210              }
 211              PreferenceScreenHelper.linkPreferenceToActivity(
 212                  this,
 213                  "pref_permissions",
 214                  Intent(requireContext(), PermissionsActivity::class.java),
 215              )
 216              PreferenceScreenHelper.linkPreferenceToActivity(
 217                  this,
 218                  "pref_help",
 219                  Intent(requireContext(), HelpActivity::class.java),
 220              )
 221              PreferenceScreenHelper.linkPreferenceToActivity(
 222                  this,
 223                  "pref_about",
 224                  Intent(requireContext(), AboutActivity::class.java),
 225              )
 226          }
 227  
 228          companion object {
 229              private const val CLICKS_TIL_TEST = 4
 230          }
 231      }
 232  }
 233