Skip to content

Commit 3924b33

Browse files
authored
Merge pull request #207 from esensar/feature/105-sleep-timer
Add sleep timer
2 parents 10c3913 + e73737f commit 3924b33

File tree

54 files changed

+520
-51
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+520
-51
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ android {
7676
}
7777

7878
dependencies {
79-
implementation 'com.github.SimpleMobileTools:Simple-Commons:7c1e5b5777'
79+
implementation 'com.github.SimpleMobileTools:Simple-Commons:73d78e5cd3'
8080
implementation 'org.greenrobot:eventbus:3.3.1'
8181
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
8282
}

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<uses-permission android:name="android.permission.FLASHLIGHT" />
1212

13+
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
1314
<uses-permission
1415
android:name="android.permission.USE_FINGERPRINT"
1516
tools:node="remove" />
@@ -132,6 +133,9 @@
132133
android:resource="@xml/widget_bright_display" />
133134
</receiver>
134135

136+
<receiver android:name=".helpers.ShutDownReceiver"
137+
android:exported="false" />
138+
135139
<service
136140
android:name=".helpers.FlashlightTileService"
137141
android:exported="true"

app/src/main/kotlin/com/simplemobiletools/flashlight/activities/BrightDisplayActivity.kt

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ import android.content.pm.ActivityInfo
44
import android.graphics.drawable.ColorDrawable
55
import android.os.Bundle
66
import android.view.WindowManager
7+
import android.widget.RelativeLayout
78
import com.simplemobiletools.commons.dialogs.ColorPickerDialog
8-
import com.simplemobiletools.commons.extensions.applyColorFilter
9-
import com.simplemobiletools.commons.extensions.getContrastColor
10-
import com.simplemobiletools.commons.extensions.viewBinding
9+
import com.simplemobiletools.commons.extensions.*
1110
import com.simplemobiletools.flashlight.databinding.ActivityBrightDisplayBinding
1211
import com.simplemobiletools.flashlight.extensions.config
12+
import com.simplemobiletools.flashlight.helpers.stopSleepTimerCountDown
13+
import com.simplemobiletools.flashlight.models.Events
14+
import org.greenrobot.eventbus.EventBus
15+
import org.greenrobot.eventbus.Subscribe
16+
import org.greenrobot.eventbus.ThreadMode
17+
import kotlin.system.exitProcess
1318

1419
class BrightDisplayActivity : SimpleActivity() {
1520
private val binding by viewBinding(ActivityBrightDisplayBinding::inflate)
@@ -45,13 +50,17 @@ class BrightDisplayActivity : SimpleActivity() {
4550
}
4651
}
4752
}
53+
54+
binding.sleepTimerStop.setOnClickListener { stopSleepTimer() }
4855
}
4956

5057
override fun onResume() {
5158
super.onResume()
5259
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
5360
toggleBrightness(true)
5461
requestedOrientation = if (config.forcePortraitMode) ActivityInfo.SCREEN_ORIENTATION_PORTRAIT else ActivityInfo.SCREEN_ORIENTATION_SENSOR
62+
63+
(binding.sleepTimerHolder.layoutParams as RelativeLayout.LayoutParams).bottomMargin = navigationBarHeight
5564
}
5665

5766
override fun onPause() {
@@ -60,6 +69,16 @@ class BrightDisplayActivity : SimpleActivity() {
6069
toggleBrightness(false)
6170
}
6271

72+
override fun onStart() {
73+
super.onStart()
74+
EventBus.getDefault().register(this)
75+
}
76+
77+
override fun onStop() {
78+
super.onStop()
79+
EventBus.getDefault().unregister(this)
80+
}
81+
6382
private fun setBackgroundColor(color: Int) {
6483
binding.apply {
6584
brightDisplay.background = ColorDrawable(color)
@@ -77,4 +96,19 @@ class BrightDisplayActivity : SimpleActivity() {
7796
layout.screenBrightness = (if (increase) 1 else 0).toFloat()
7897
window.attributes = layout
7998
}
99+
100+
private fun stopSleepTimer() {
101+
binding.sleepTimerHolder.fadeOut()
102+
stopSleepTimerCountDown()
103+
}
104+
105+
@Subscribe(threadMode = ThreadMode.MAIN)
106+
fun sleepTimerChanged(event: Events.SleepTimerChanged) {
107+
binding.sleepTimerValue.text = event.seconds.getFormattedDuration()
108+
binding.sleepTimerHolder.beVisible()
109+
110+
if (event.seconds == 0) {
111+
exitProcess(0)
112+
}
113+
}
80114
}

app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt

Lines changed: 100 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
package com.simplemobiletools.flashlight.activities
22

33
import android.annotation.SuppressLint
4+
import android.app.AlarmManager
5+
import android.content.Context
46
import android.content.Intent
57
import android.content.pm.ActivityInfo
68
import android.content.pm.ShortcutInfo
9+
import android.graphics.drawable.ColorDrawable
710
import android.graphics.drawable.Icon
811
import android.graphics.drawable.LayerDrawable
912
import android.os.Bundle
1013
import android.view.WindowManager
1114
import android.widget.ImageView
15+
import com.simplemobiletools.commons.dialogs.PermissionRequiredDialog
16+
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
1217
import com.simplemobiletools.commons.extensions.*
13-
import com.simplemobiletools.commons.helpers.LICENSE_EVENT_BUS
14-
import com.simplemobiletools.commons.helpers.PERMISSION_CAMERA
15-
import com.simplemobiletools.commons.helpers.isNougatMR1Plus
16-
import com.simplemobiletools.commons.helpers.isNougatPlus
18+
import com.simplemobiletools.commons.helpers.*
1719
import com.simplemobiletools.commons.models.FAQItem
20+
import com.simplemobiletools.commons.models.RadioItem
1821
import com.simplemobiletools.flashlight.BuildConfig
1922
import com.simplemobiletools.flashlight.R
2023
import com.simplemobiletools.flashlight.databinding.ActivityMainBinding
24+
import com.simplemobiletools.flashlight.dialogs.SleepTimerCustomDialog
2125
import com.simplemobiletools.flashlight.extensions.config
22-
import com.simplemobiletools.flashlight.helpers.CameraTorchListener
23-
import com.simplemobiletools.flashlight.helpers.MIN_BRIGHTNESS_LEVEL
24-
import com.simplemobiletools.flashlight.helpers.MyCameraImpl
26+
import com.simplemobiletools.flashlight.helpers.*
2527
import com.simplemobiletools.flashlight.models.Events
2628
import org.greenrobot.eventbus.EventBus
2729
import org.greenrobot.eventbus.Subscribe
30+
import org.greenrobot.eventbus.ThreadMode
2831
import java.util.*
2932

3033
class MainActivity : SimpleActivity() {
@@ -74,6 +77,8 @@ class MainActivity : SimpleActivity() {
7477
stroboscopeBtn.setOnClickListener {
7578
toggleStroboscope(false)
7679
}
80+
81+
sleepTimerStop.setOnClickListener { stopSleepTimer() }
7782
}
7883

7984
setupStroboscope()
@@ -110,6 +115,9 @@ class MainActivity : SimpleActivity() {
110115
}
111116
}
112117

118+
binding.sleepTimerHolder.background = ColorDrawable(getProperBackgroundColor())
119+
binding.sleepTimerStop.applyColorFilter(getProperTextColor())
120+
113121
requestedOrientation = if (config.forcePortraitMode) ActivityInfo.SCREEN_ORIENTATION_PORTRAIT else ActivityInfo.SCREEN_ORIENTATION_SENSOR
114122
invalidateOptionsMenu()
115123

@@ -126,6 +134,11 @@ class MainActivity : SimpleActivity() {
126134
super.onStart()
127135
mBus!!.register(this)
128136

137+
if (config.sleepInTS == 0L) {
138+
binding.sleepTimerHolder.beGone()
139+
(getSystemService(Context.ALARM_SERVICE) as AlarmManager).cancel(getShutDownPendingIntent())
140+
}
141+
129142
if (mCameraImpl == null) {
130143
setupCameraImpl()
131144
}
@@ -146,6 +159,7 @@ class MainActivity : SimpleActivity() {
146159
when (menuItem.itemId) {
147160
R.id.more_apps_from_us -> launchMoreAppsFromUsIntent()
148161
R.id.settings -> launchSettings()
162+
R.id.sleep_timer -> showSleepTimer()
149163
R.id.about -> launchAbout()
150164
else -> return@setOnMenuItemClickListener false
151165
}
@@ -279,6 +293,85 @@ class MainActivity : SimpleActivity() {
279293
mCameraImpl = null
280294
}
281295

296+
private fun showSleepTimer(force: Boolean = false) {
297+
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
298+
if (isSPlus() && !alarmManager.canScheduleExactAlarms() && !force) {
299+
PermissionRequiredDialog(
300+
this,
301+
com.simplemobiletools.commons.R.string.allow_alarm_sleep_timer,
302+
positiveActionCallback = { openRequestExactAlarmSettings(baseConfig.appId) },
303+
negativeActionCallback = { showSleepTimer(true) }
304+
)
305+
return
306+
}
307+
308+
val items = ArrayList(listOf(10, 30, 60, 5 * 60, 10 * 60, 30 * 60).map {
309+
RadioItem(it, secondsToString(it))
310+
})
311+
312+
if (items.none { it.id == config.lastSleepTimerSeconds }) {
313+
items.add(RadioItem(config.lastSleepTimerSeconds, secondsToString(config.lastSleepTimerSeconds)))
314+
}
315+
316+
items.sortBy { it.id }
317+
items.add(RadioItem(-1, getString(R.string.custom)))
318+
319+
RadioGroupDialog(this, items, config.lastSleepTimerSeconds) {
320+
if (it as Int == -1) {
321+
SleepTimerCustomDialog(this) {
322+
if (it > 0) {
323+
pickedSleepTimer(it)
324+
}
325+
}
326+
} else if (it > 0) {
327+
pickedSleepTimer(it)
328+
}
329+
}
330+
}
331+
332+
private fun secondsToString(seconds: Int): String {
333+
val finalHours = seconds / 3600
334+
val finalMinutes = (seconds / 60) % 60
335+
val finalSeconds = seconds % 60
336+
val parts = mutableListOf<String>()
337+
if (finalHours != 0) {
338+
parts.add(resources.getQuantityString(R.plurals.hours, finalHours, finalHours))
339+
}
340+
if (finalMinutes != 0) {
341+
parts.add(resources.getQuantityString(R.plurals.minutes, finalMinutes, finalMinutes))
342+
}
343+
if (finalSeconds != 0) {
344+
parts.add(resources.getQuantityString(R.plurals.seconds, finalSeconds, finalSeconds))
345+
}
346+
return parts.joinToString(separator = " ")
347+
}
348+
349+
private fun pickedSleepTimer(seconds: Int) {
350+
config.lastSleepTimerSeconds = seconds
351+
config.sleepInTS = System.currentTimeMillis() + seconds * 1000
352+
startSleepTimer()
353+
}
354+
355+
private fun startSleepTimer() {
356+
binding.sleepTimerHolder.fadeIn()
357+
startSleepTimerCountDown()
358+
}
359+
360+
private fun stopSleepTimer() {
361+
binding.sleepTimerHolder.fadeOut()
362+
stopSleepTimerCountDown()
363+
}
364+
365+
@Subscribe(threadMode = ThreadMode.MAIN)
366+
fun sleepTimerChanged(event: Events.SleepTimerChanged) {
367+
binding.sleepTimerValue.text = event.seconds.getFormattedDuration()
368+
binding.sleepTimerHolder.beVisible()
369+
370+
if (event.seconds == 0) {
371+
finish()
372+
}
373+
}
374+
282375
@Subscribe
283376
fun stateChangedEvent(event: Events.StateChanged) {
284377
checkState(event.isEnabled)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.simplemobiletools.flashlight.dialogs
2+
3+
import android.app.Activity
4+
import android.view.inputmethod.EditorInfo
5+
import androidx.appcompat.app.AlertDialog
6+
import com.simplemobiletools.commons.extensions.*
7+
import com.simplemobiletools.flashlight.R
8+
import com.simplemobiletools.flashlight.databinding.DialogCustomSleepTimerPickerBinding
9+
10+
class SleepTimerCustomDialog(val activity: Activity, val callback: (seconds: Int) -> Unit) {
11+
private var dialog: AlertDialog? = null
12+
private val binding = DialogCustomSleepTimerPickerBinding.inflate(activity.layoutInflater)
13+
14+
init {
15+
binding.dialogRadioView.check(R.id.dialog_radio_minutes)
16+
binding.timerValue.setOnEditorActionListener { _, actionId, _ ->
17+
if (actionId == EditorInfo.IME_ACTION_DONE) {
18+
dialogConfirmed()
19+
return@setOnEditorActionListener true
20+
}
21+
return@setOnEditorActionListener false
22+
}
23+
24+
activity.getAlertDialogBuilder()
25+
.setPositiveButton(R.string.ok) { _, _ -> dialogConfirmed() }
26+
.setNegativeButton(R.string.cancel, null)
27+
.apply {
28+
activity.setupDialogStuff(binding.root, this, R.string.sleep_timer) { alertDialog ->
29+
dialog = alertDialog
30+
alertDialog.showKeyboard(binding.timerValue)
31+
}
32+
}
33+
}
34+
35+
private fun dialogConfirmed() {
36+
val value = binding.timerValue.value
37+
val minutes = Integer.valueOf(value.ifEmpty { "0" })
38+
val multiplier = getMultiplier(binding.dialogRadioView.checkedRadioButtonId)
39+
callback(minutes * multiplier)
40+
activity.hideKeyboard()
41+
dialog?.dismiss()
42+
}
43+
44+
private fun getMultiplier(id: Int) = when (id) {
45+
R.id.dialog_radio_seconds -> 1
46+
R.id.dialog_radio_minutes -> 60
47+
else -> 60
48+
}
49+
}

app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Config.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,12 @@ class Config(context: Context) : BaseConfig(context) {
4444
var brightnessLevel: Int
4545
get() = prefs.getInt(BRIGHTNESS_LEVEL, DEFAULT_BRIGHTNESS_LEVEL)
4646
set(brightnessLevel) = prefs.edit().putInt(BRIGHTNESS_LEVEL, brightnessLevel).apply()
47+
48+
var lastSleepTimerSeconds: Int
49+
get() = prefs.getInt(LAST_SLEEP_TIMER_SECONDS, 30 * 60)
50+
set(lastSleepTimerSeconds) = prefs.edit().putInt(LAST_SLEEP_TIMER_SECONDS, lastSleepTimerSeconds).apply()
51+
52+
var sleepInTS: Long
53+
get() = prefs.getLong(SLEEP_IN_TS, 0)
54+
set(sleepInTS) = prefs.edit().putLong(SLEEP_IN_TS, sleepInTS).apply()
4755
}

app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Constants.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ const val STROBOSCOPE_PROGRESS = "stroboscope_progress"
1212
const val FORCE_PORTRAIT_MODE = "force_portrait_mode"
1313
const val SOS = "sos"
1414
const val BRIGHTNESS_LEVEL = "brightness_level"
15+
const val LAST_SLEEP_TIMER_SECONDS = "last_sleep_timer_seconds"
16+
const val SLEEP_IN_TS = "sleep_in_ts"
1517
const val MIN_BRIGHTNESS_LEVEL = 1
1618
const val DEFAULT_BRIGHTNESS_LEVEL = -1
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.simplemobiletools.flashlight.helpers
2+
3+
import android.content.BroadcastReceiver
4+
import android.content.Context
5+
import android.content.Intent
6+
import kotlin.system.exitProcess
7+
8+
class ShutDownReceiver : BroadcastReceiver() {
9+
override fun onReceive(context: Context?, intent: Intent?) {
10+
exitProcess(0)
11+
}
12+
}

0 commit comments

Comments
 (0)