move the Timer reminder creation in context extensions

This commit is contained in:
tibbi 2018-03-14 14:16:43 +01:00
parent f2253f80c4
commit f28fe26a4d
4 changed files with 68 additions and 82 deletions

View File

@ -3,8 +3,6 @@ package com.simplemobiletools.clock.activities
import android.os.Bundle
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.extensions.showOverLockscreen
import com.simplemobiletools.clock.helpers.REMINDER_TEXT
import com.simplemobiletools.clock.helpers.REMINDER_TITLE
import com.simplemobiletools.commons.extensions.updateTextColors
import kotlinx.android.synthetic.main.activity_reminder.*
@ -15,7 +13,7 @@ class ReminderActivity : SimpleActivity() {
setContentView(R.layout.activity_reminder)
showOverLockscreen()
updateTextColors(reminder_holder)
reminder_title.text = intent.getStringExtra(REMINDER_TITLE)
reminder_text.text = intent.getStringExtra(REMINDER_TEXT)
reminder_title.text = getString(R.string.timer)
reminder_text.text = getString(R.string.time_expired)
}
}

View File

@ -1,31 +1,36 @@
package com.simplemobiletools.clock.extensions
import android.annotation.SuppressLint
import android.app.AlarmManager
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.*
import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.media.AudioManager
import android.media.RingtoneManager
import android.net.Uri
import android.os.PowerManager
import android.provider.Settings
import android.support.v4.app.NotificationCompat
import android.text.SpannableString
import android.text.style.RelativeSizeSpan
import android.widget.Toast
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.activities.MainActivity
import com.simplemobiletools.clock.activities.SplashActivity
import com.simplemobiletools.clock.helpers.*
import com.simplemobiletools.clock.models.Alarm
import com.simplemobiletools.clock.models.AlarmSound
import com.simplemobiletools.clock.models.MyTimeZone
import com.simplemobiletools.clock.receivers.AlarmReceiver
import com.simplemobiletools.clock.receivers.DateTimeWidgetUpdateReceiver
import com.simplemobiletools.clock.receivers.TimerReceiver
import com.simplemobiletools.commons.extensions.formatMinutesToTimeString
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.helpers.isKitkatPlus
import com.simplemobiletools.commons.helpers.isLollipopPlus
import com.simplemobiletools.commons.helpers.isOreoPlus
import java.util.*
import kotlin.math.pow
@ -130,12 +135,12 @@ fun Context.setupAlarmClock(alarm: Alarm, triggerInSeconds: Int) {
val targetMS = System.currentTimeMillis() + triggerInSeconds * 1000
if (isLollipopPlus()) {
val info = AlarmManager.AlarmClockInfo(targetMS, getOpenAppIntent())
val info = AlarmManager.AlarmClockInfo(targetMS, getOpenAlarmTabIntent())
alarmManager.setAlarmClock(info, getAlarmIntent(alarm))
}
}
fun Context.getOpenAppIntent(): PendingIntent {
fun Context.getOpenAlarmTabIntent(): PendingIntent {
val intent = Intent(this, MainActivity::class.java)
intent.putExtra(OPEN_TAB, TAB_ALARM)
return PendingIntent.getActivity(this, OPEN_ALARMS_TAB_INTENT_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
@ -220,3 +225,55 @@ fun Context.rescheduleEnabledAlarms() {
}
fun Context.isScreenOn() = (getSystemService(Context.POWER_SERVICE) as PowerManager).isScreenOn
@SuppressLint("NewApi")
fun Context.getTimerNotification(pendingIntent: PendingIntent): Notification {
val channelId = "timer_channel"
if (isOreoPlus()) {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val name = getString(R.string.timer)
val importance = NotificationManager.IMPORTANCE_HIGH
NotificationChannel(channelId, name, importance).apply {
enableLights(true)
lightColor = getAdjustedPrimaryColor()
enableVibration(config.timerVibrate)
notificationManager.createNotificationChannel(this)
}
}
val builder = NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.timer))
.setContentText(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(config.timerSoundUri), AudioManager.STREAM_SYSTEM)
.setChannelId(channelId)
.addAction(R.drawable.ic_cross, getString(R.string.dismiss), getTimerPendingIntent())
if (isLollipopPlus()) {
builder.setVisibility(Notification.VISIBILITY_PUBLIC)
}
if (config.timerVibrate) {
val vibrateArray = LongArray(2) { 500 }
builder.setVibrate(vibrateArray)
}
val notification = builder.build()
notification.flags = notification.flags or Notification.FLAG_INSISTENT
return notification
}
fun Context.getTimerPendingIntent(): PendingIntent {
val intent = Intent(this, TimerReceiver::class.java)
return PendingIntent.getBroadcast(this, TIMER_NOTIF_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
fun Context.getOpenTimerTabIntent(): PendingIntent {
val intent = Intent(this, SplashActivity::class.java)
intent.putExtra(OPEN_TAB, TAB_TIMER)
return PendingIntent.getActivity(this, TIMER_NOTIF_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}

View File

@ -1,38 +1,25 @@
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.ReminderActivity
import com.simplemobiletools.clock.activities.SimpleActivity
import com.simplemobiletools.clock.activities.SplashActivity
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.extensions.hideNotification
import com.simplemobiletools.clock.extensions.isScreenOn
import com.simplemobiletools.clock.helpers.*
import com.simplemobiletools.clock.receivers.TimerReceiver
import com.simplemobiletools.clock.extensions.*
import com.simplemobiletools.clock.helpers.TIMER_NOTIF_ID
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.*
@ -187,8 +174,8 @@ class TimerFragment : Fragment() {
view.timer_time.text = formattedDuration
if (diff == 0) {
if (context?.isScreenOn() == true) {
val pendingIntent = getOpenAppIntent(context!!)
val notification = getNotification(context!!, pendingIntent)
val pendingIntent = context!!.getOpenTimerTabIntent()
val notification = context!!.getTimerNotification(pendingIntent)
val notificationManager = context!!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(TIMER_NOTIF_ID, notification)
@ -197,8 +184,6 @@ class TimerFragment : Fragment() {
}, context?.config!!.timerMaxReminderSecs * 1000L)
} else {
Intent(context, ReminderActivity::class.java).apply {
putExtra(REMINDER_TITLE, context!!.getString(R.string.timer))
putExtra(REMINDER_TEXT, context!!.getString(R.string.time_expired))
activity?.startActivity(this)
}
}
@ -206,58 +191,6 @@ class TimerFragment : Fragment() {
return true
}
@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(2) { 500 }
builder.setVibrate(vibrateArray)
}
val notification = builder.build()
notification.flags = notification.flags or Notification.FLAG_INSISTENT
return notification
}
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 getOpenAppIntent(context: Context): PendingIntent {
val intent = Intent(context, SplashActivity::class.java)
intent.putExtra(OPEN_TAB, TAB_TIMER)
return PendingIntent.getActivity(context, TIMER_NOTIF_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
private fun hideTimerNotification() {
context?.hideNotification(TIMER_NOTIF_ID)
}

View File

@ -22,8 +22,6 @@ const val DEFAULT_ALARM_MINUTES = 480
const val DAY_MINUTES = 1440
const val DEFAULT_MAX_ALARM_REMINDER_SECS = 300
const val DEFAULT_MAX_TIMER_REMINDER_SECS = 60
const val REMINDER_TITLE = "reminder_title"
const val REMINDER_TEXT = "reminder_text"
const val OPEN_ALARMS_TAB_INTENT_ID = 9996
const val UPDATE_WIDGET_INTENT_ID = 9997