diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b891581..88dd0f3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -25,7 +25,9 @@ - + + + diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/EditAlarmDialog.kt b/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/EditAlarmDialog.kt index 0d58e55..a998147 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/EditAlarmDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/EditAlarmDialog.kt @@ -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 diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/SelectAlarmSoundDialog.kt b/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/SelectAlarmSoundDialog.kt index afa771d..5dc4c4f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/SelectAlarmSoundDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/SelectAlarmSoundDialog.kt @@ -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 } } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt index 2489a22..2e5efaf 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt @@ -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) { diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt index 52f69ca..e772f84 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt @@ -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 diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/receivers/TimerReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/clock/receivers/TimerReceiver.kt new file mode 100644 index 0000000..b066cbc --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/clock/receivers/TimerReceiver.kt @@ -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) + } +} diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml index 49d1407..eb85320 100644 --- a/app/src/main/res/values-pt/strings.xml +++ b/app/src/main/res/values-pt/strings.xml @@ -6,9 +6,12 @@ Label No days selected Alarm + Timer Lap Stopwatch has been stopped Timer has been stopped + Max reminder length + Time expired Relógio diff --git a/app/src/main/res/values-sk/strings.xml b/app/src/main/res/values-sk/strings.xml index 2379aa3..15a31c1 100644 --- a/app/src/main/res/values-sk/strings.xml +++ b/app/src/main/res/values-sk/strings.xml @@ -6,9 +6,12 @@ Štítok Neboli označené žiadne dni Budík + Časovač Okruh Stopky boli zastavené Časovač bol zastavený + Maximálna dĺžka upozorňovania + Čas vypršal Okno s časom diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index df40e31..42cf145 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -6,9 +6,12 @@ Label No days selected Alarm + Timer Lap Stopwatch has been stopped Timer has been stopped + Max reminder length + Time expired Clock tab