trigger some reminders when the Timer expires

This commit is contained in:
tibbi 2018-03-11 11:17:19 +01:00
parent a21ba3852c
commit 49937ede5c
9 changed files with 102 additions and 8 deletions

View File

@ -25,7 +25,9 @@
</intent-filter>
</activity>
<activity android:name=".activities.MainActivity"/>
<activity
android:name=".activities.MainActivity"
android:launchMode="singleTop"/>
<activity
android:name=".activities.SettingsActivity"
@ -60,6 +62,8 @@
<receiver android:name=".receivers.AlarmReceiver"/>
<receiver android:name=".receivers.TimerReceiver"/>
<receiver android:name=".receivers.BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.clock.dialogs
import android.app.TimePickerDialog
import android.graphics.drawable.Drawable
import android.media.AudioManager
import android.support.v7.app.AlertDialog
import android.widget.TextView
import com.simplemobiletools.clock.R
@ -29,7 +30,7 @@ class EditAlarmDialog(val activity: SimpleActivity, val alarm: Alarm, val callba
edit_alarm_sound.colorLeftDrawable(textColor)
edit_alarm_sound.text = alarm.soundTitle
edit_alarm_sound.setOnClickListener {
SelectAlarmSoundDialog(activity, alarm.soundUri) {
SelectAlarmSoundDialog(activity, alarm.soundUri, AudioManager.STREAM_ALARM) {
alarm.soundTitle = it.title
alarm.soundUri = it.uri
edit_alarm_sound.text = it.title

View File

@ -1,6 +1,5 @@
package com.simplemobiletools.clock.dialogs
import android.media.AudioManager
import android.media.MediaPlayer
import android.net.Uri
import android.support.v7.app.AlertDialog
@ -14,7 +13,7 @@ import com.simplemobiletools.clock.models.AlarmSound
import com.simplemobiletools.commons.extensions.setupDialogStuff
import kotlinx.android.synthetic.main.dialog_select_alarm_sound.view.*
class SelectAlarmSoundDialog(val activity: SimpleActivity, val currentUri: String, val callback: (alarmSound: AlarmSound) -> Unit) {
class SelectAlarmSoundDialog(val activity: SimpleActivity, val currentUri: String, val audioStream: Int, val callback: (alarmSound: AlarmSound) -> Unit) {
private val view = activity.layoutInflater.inflate(R.layout.dialog_select_alarm_sound, null)
private val alarms = activity.getAlarms()
private var mediaPlayer = MediaPlayer()
@ -29,7 +28,7 @@ class SelectAlarmSoundDialog(val activity: SimpleActivity, val currentUri: Strin
setOnClickListener {
mediaPlayer.stop()
mediaPlayer = MediaPlayer().apply {
setAudioStreamType(AudioManager.STREAM_ALARM)
setAudioStreamType(audioStream)
setDataSource(context, Uri.parse(alarmSound.uri))
prepare()
start()
@ -47,7 +46,7 @@ class SelectAlarmSoundDialog(val activity: SimpleActivity, val currentUri: Strin
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this)
window.volumeControlStream = AudioManager.STREAM_ALARM
window.volumeControlStream = audioStream
}
}

View File

@ -1,20 +1,35 @@
package com.simplemobiletools.clock.fragments
import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.media.AudioManager
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.os.SystemClock
import android.support.v4.app.Fragment
import android.support.v4.app.NotificationCompat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.activities.MainActivity
import com.simplemobiletools.clock.activities.SimpleActivity
import com.simplemobiletools.clock.dialogs.MyTimePickerDialogDialog
import com.simplemobiletools.clock.dialogs.SelectAlarmSoundDialog
import com.simplemobiletools.clock.extensions.colorLeftDrawable
import com.simplemobiletools.clock.extensions.config
import com.simplemobiletools.clock.helpers.TIMER_NOTIF_ID
import com.simplemobiletools.clock.receivers.TimerReceiver
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.isLollipopPlus
import com.simplemobiletools.commons.helpers.isOreoPlus
import kotlinx.android.synthetic.main.fragment_timer.view.*
class TimerFragment : Fragment() {
@ -60,7 +75,7 @@ class TimerFragment : Fragment() {
}
timer_sound.setOnClickListener {
SelectAlarmSoundDialog(activity as SimpleActivity, config.timerSoundUri) {
SelectAlarmSoundDialog(activity as SimpleActivity, config.timerSoundUri, AudioManager.STREAM_SYSTEM) {
config.timerSoundTitle = it.title
config.timerSoundUri = it.uri
timer_sound.text = it.title
@ -150,10 +165,62 @@ class TimerFragment : Fragment() {
view.timer_time.text = formattedDuration
if (diff == 0) {
val pendingIntent = getNotificationClickIntent(context!!)
val notification = getNotification(context!!, pendingIntent)
val notificationManager = context!!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(TIMER_NOTIF_ID, notification)
}
}
@SuppressLint("NewApi")
private fun getNotification(context: Context, pendingIntent: PendingIntent): Notification {
val channelId = "timer_channel"
if (isOreoPlus()) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val name = context.resources.getString(R.string.timer)
val importance = NotificationManager.IMPORTANCE_HIGH
NotificationChannel(channelId, name, importance).apply {
enableLights(true)
lightColor = context.getAdjustedPrimaryColor()
enableVibration(context.config.timerVibrate)
notificationManager.createNotificationChannel(this)
}
}
val builder = NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.timer))
.setContentText(context.getString(R.string.time_expired))
.setSmallIcon(R.drawable.ic_timer)
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_LIGHTS)
.setAutoCancel(true)
.setSound(Uri.parse(context.config.timerSoundUri), AudioManager.STREAM_SYSTEM)
.setChannelId(channelId)
.addAction(R.drawable.ic_cross, context.getString(R.string.dismiss), getTimerPendingIntent(context))
if (isLollipopPlus()) {
builder.setVisibility(Notification.VISIBILITY_PUBLIC)
}
if (context.config.timerVibrate) {
val vibrateArray = LongArray(120) { 500 }
builder.setVibrate(vibrateArray)
}
return builder.build()
}
private fun getTimerPendingIntent(context: Context): PendingIntent {
val intent = Intent(context, TimerReceiver::class.java)
return PendingIntent.getBroadcast(context, TIMER_NOTIF_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
private fun getNotificationClickIntent(context: Context): PendingIntent {
val intent = Intent(context, MainActivity::class.java)
return PendingIntent.getActivity(context, TIMER_NOTIF_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
private val updateRunnable = object : Runnable {
override fun run() {
if (isRunning) {

View File

@ -17,6 +17,7 @@ const val EDITED_TIME_ZONE_SEPARATOR = ":"
const val ALARM_ID = "alarm_id"
const val DEFAULT_ALARM_MINUTES = 480
const val DAY_MINUTES = 1440
const val TIMER_NOTIF_ID = 9999
const val SORT_BY_LAP = 1
const val SORT_BY_LAP_TIME = 2

View File

@ -0,0 +1,13 @@
package com.simplemobiletools.clock.receivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.simplemobiletools.clock.extensions.hideNotification
import com.simplemobiletools.clock.helpers.TIMER_NOTIF_ID
class TimerReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
context.hideNotification(TIMER_NOTIF_ID)
}
}

View File

@ -6,9 +6,12 @@
<string name="label">Label</string>
<string name="no_days_selected">No days selected</string>
<string name="alarm">Alarm</string>
<string name="timer">Timer</string>
<string name="lap">Lap</string>
<string name="stopwatch_stopped">Stopwatch has been stopped</string>
<string name="timer_stopped">Timer has been stopped</string>
<string name="max_reminder_length">Max reminder length</string>
<string name="time_expired">Time expired</string>
<!-- Settings -->
<string name="clock_tab">Relógio</string>

View File

@ -6,9 +6,12 @@
<string name="label">Štítok</string>
<string name="no_days_selected">Neboli označené žiadne dni</string>
<string name="alarm">Budík</string>
<string name="timer">Časovač</string>
<string name="lap">Okruh</string>
<string name="stopwatch_stopped">Stopky boli zastavené</string>
<string name="timer_stopped">Časovač bol zastavený</string>
<string name="max_reminder_length">Maximálna dĺžka upozorňovania</string>
<string name="time_expired">Čas vypršal</string>
<!-- Settings -->
<string name="clock_tab">Okno s časom</string>

View File

@ -6,9 +6,12 @@
<string name="label">Label</string>
<string name="no_days_selected">No days selected</string>
<string name="alarm">Alarm</string>
<string name="timer">Timer</string>
<string name="lap">Lap</string>
<string name="stopwatch_stopped">Stopwatch has been stopped</string>
<string name="timer_stopped">Timer has been stopped</string>
<string name="max_reminder_length">Max reminder length</string>
<string name="time_expired">Time expired</string>
<!-- Settings -->
<string name="clock_tab">Clock tab</string>