mirror of
https://github.com/SimpleMobileTools/Simple-Flashlight.git
synced 2025-06-05 21:59:19 +02:00
@ -1,6 +1,8 @@
|
||||
package com.simplemobiletools.flashlight.activities
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.AlarmManager
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.ActivityInfo
|
||||
import android.content.pm.ShortcutInfo
|
||||
@ -9,22 +11,22 @@ import android.graphics.drawable.LayerDrawable
|
||||
import android.os.Bundle
|
||||
import android.view.WindowManager
|
||||
import android.widget.ImageView
|
||||
import com.simplemobiletools.commons.dialogs.PermissionRequiredDialog
|
||||
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.commons.helpers.LICENSE_EVENT_BUS
|
||||
import com.simplemobiletools.commons.helpers.PERMISSION_CAMERA
|
||||
import com.simplemobiletools.commons.helpers.isNougatMR1Plus
|
||||
import com.simplemobiletools.commons.helpers.isNougatPlus
|
||||
import com.simplemobiletools.commons.helpers.*
|
||||
import com.simplemobiletools.commons.models.FAQItem
|
||||
import com.simplemobiletools.commons.models.RadioItem
|
||||
import com.simplemobiletools.flashlight.BuildConfig
|
||||
import com.simplemobiletools.flashlight.R
|
||||
import com.simplemobiletools.flashlight.databinding.ActivityMainBinding
|
||||
import com.simplemobiletools.flashlight.dialogs.SleepTimerCustomDialog
|
||||
import com.simplemobiletools.flashlight.extensions.config
|
||||
import com.simplemobiletools.flashlight.helpers.CameraTorchListener
|
||||
import com.simplemobiletools.flashlight.helpers.MIN_BRIGHTNESS_LEVEL
|
||||
import com.simplemobiletools.flashlight.helpers.MyCameraImpl
|
||||
import com.simplemobiletools.flashlight.helpers.*
|
||||
import com.simplemobiletools.flashlight.models.Events
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import org.greenrobot.eventbus.Subscribe
|
||||
import org.greenrobot.eventbus.ThreadMode
|
||||
import java.util.*
|
||||
|
||||
class MainActivity : SimpleActivity() {
|
||||
@ -74,6 +76,8 @@ class MainActivity : SimpleActivity() {
|
||||
stroboscopeBtn.setOnClickListener {
|
||||
toggleStroboscope(false)
|
||||
}
|
||||
|
||||
sleepTimerStop.setOnClickListener { stopSleepTimer() }
|
||||
}
|
||||
|
||||
setupStroboscope()
|
||||
@ -126,6 +130,11 @@ class MainActivity : SimpleActivity() {
|
||||
super.onStart()
|
||||
mBus!!.register(this)
|
||||
|
||||
if (config.sleepInTS == 0L) {
|
||||
binding.sleepTimerHolder.beGone()
|
||||
(getSystemService(Context.ALARM_SERVICE) as AlarmManager).cancel(getShutDownPendingIntent())
|
||||
}
|
||||
|
||||
if (mCameraImpl == null) {
|
||||
setupCameraImpl()
|
||||
}
|
||||
@ -146,6 +155,7 @@ class MainActivity : SimpleActivity() {
|
||||
when (menuItem.itemId) {
|
||||
R.id.more_apps_from_us -> launchMoreAppsFromUsIntent()
|
||||
R.id.settings -> launchSettings()
|
||||
R.id.sleep_timer -> showSleepTimer()
|
||||
R.id.about -> launchAbout()
|
||||
else -> return@setOnMenuItemClickListener false
|
||||
}
|
||||
@ -279,6 +289,76 @@ class MainActivity : SimpleActivity() {
|
||||
mCameraImpl = null
|
||||
}
|
||||
|
||||
private fun showSleepTimer(force: Boolean = false) {
|
||||
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
if (isSPlus() && !alarmManager.canScheduleExactAlarms() && !force) {
|
||||
PermissionRequiredDialog(
|
||||
this,
|
||||
com.simplemobiletools.commons.R.string.allow_alarm_sleep_timer,
|
||||
positiveActionCallback = { openRequestExactAlarmSettings(baseConfig.appId) },
|
||||
negativeActionCallback = { showSleepTimer(true) }
|
||||
)
|
||||
return
|
||||
}
|
||||
val minutes = getString(R.string.minutes_raw)
|
||||
val hour = resources.getQuantityString(R.plurals.hours, 1, 1)
|
||||
|
||||
val items = arrayListOf(
|
||||
RadioItem(5 * 60, "5 $minutes"),
|
||||
RadioItem(10 * 60, "10 $minutes"),
|
||||
RadioItem(20 * 60, "20 $minutes"),
|
||||
RadioItem(30 * 60, "30 $minutes"),
|
||||
RadioItem(60 * 60, hour)
|
||||
)
|
||||
|
||||
if (items.none { it.id == config.lastSleepTimerSeconds }) {
|
||||
val lastSleepTimerMinutes = config.lastSleepTimerSeconds / 60
|
||||
val text = resources.getQuantityString(R.plurals.minutes, lastSleepTimerMinutes, lastSleepTimerMinutes)
|
||||
items.add(RadioItem(config.lastSleepTimerSeconds, text))
|
||||
}
|
||||
|
||||
items.sortBy { it.id }
|
||||
items.add(RadioItem(-1, getString(R.string.custom)))
|
||||
|
||||
RadioGroupDialog(this, items, config.lastSleepTimerSeconds) {
|
||||
if (it as Int == -1) {
|
||||
SleepTimerCustomDialog(this) {
|
||||
if (it > 0) {
|
||||
pickedSleepTimer(it)
|
||||
}
|
||||
}
|
||||
} else if (it > 0) {
|
||||
pickedSleepTimer(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun pickedSleepTimer(seconds: Int) {
|
||||
config.lastSleepTimerSeconds = seconds
|
||||
config.sleepInTS = System.currentTimeMillis() + seconds * 1000
|
||||
startSleepTimer()
|
||||
}
|
||||
|
||||
private fun startSleepTimer() {
|
||||
binding.sleepTimerHolder.fadeIn()
|
||||
startSleepTimerCountDown()
|
||||
}
|
||||
|
||||
private fun stopSleepTimer() {
|
||||
binding.sleepTimerHolder.fadeOut()
|
||||
stopSleepTimerCountDown()
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
fun sleepTimerChanged(event: Events.SleepTimerChanged) {
|
||||
binding.sleepTimerValue.text = event.seconds.getFormattedDuration()
|
||||
binding.sleepTimerHolder.beVisible()
|
||||
|
||||
if (event.seconds == 0) {
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
fun stateChangedEvent(event: Events.StateChanged) {
|
||||
checkState(event.isEnabled)
|
||||
|
Reference in New Issue
Block a user