From 258453d88c61a9b98dcf80c200e950ceaef301f8 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 6 Mar 2018 20:55:37 +0100 Subject: [PATCH] adding initial alarm reminder handling --- app/build.gradle | 2 +- app/src/main/AndroidManifest.xml | 7 ++ .../activities/SnoozeReminderActivity.kt | 10 +++ .../clock/receivers/AlarmReceiver.kt | 78 +++++++++++++++++++ .../clock/services/SnoozeService.kt | 10 +++ app/src/main/res/values-pt/strings.xml | 1 + app/src/main/res/values-sk/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 8 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/clock/activities/SnoozeReminderActivity.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/clock/services/SnoozeService.kt diff --git a/app/build.gradle b/app/build.gradle index cfa373f..2d26bc2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -41,7 +41,7 @@ android { } dependencies { - implementation 'com.simplemobiletools:commons:3.15.3' + implementation 'com.simplemobiletools:commons:3.15.4' implementation 'com.facebook.stetho:stetho:1.5.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c495716..be19042 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,7 @@ package="com.simplemobiletools.clock"> + + + + + diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/activities/SnoozeReminderActivity.kt b/app/src/main/kotlin/com/simplemobiletools/clock/activities/SnoozeReminderActivity.kt new file mode 100644 index 0000000..ca003eb --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/clock/activities/SnoozeReminderActivity.kt @@ -0,0 +1,10 @@ +package com.simplemobiletools.clock.activities + +import android.os.Bundle +import android.support.v7.app.AppCompatActivity + +class SnoozeReminderActivity : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/receivers/AlarmReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/clock/receivers/AlarmReceiver.kt index 9cd270e..20a84d3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/receivers/AlarmReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/receivers/AlarmReceiver.kt @@ -1,14 +1,92 @@ package com.simplemobiletools.clock.receivers +import android.annotation.SuppressLint +import android.app.Notification +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import android.media.AudioManager +import android.net.Uri +import android.support.v4.app.NotificationCompat +import com.simplemobiletools.clock.R +import com.simplemobiletools.clock.activities.MainActivity +import com.simplemobiletools.clock.activities.SnoozeReminderActivity +import com.simplemobiletools.clock.extensions.config import com.simplemobiletools.clock.extensions.dbHelper +import com.simplemobiletools.clock.extensions.formatAlarmTime import com.simplemobiletools.clock.helpers.ALARM_ID +import com.simplemobiletools.clock.models.Alarm +import com.simplemobiletools.clock.services.SnoozeService +import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor +import com.simplemobiletools.commons.helpers.isLollipopPlus +import com.simplemobiletools.commons.helpers.isOreoPlus class AlarmReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val id = intent.getIntExtra(ALARM_ID, -1) val alarm = context.dbHelper.getAlarmWithId(id) ?: return + + val pendingIntent = getPendingIntent(context, alarm) + val notification = getNotification(context, pendingIntent, alarm) + val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + notificationManager.notify(alarm.id, notification) + } + + @SuppressLint("NewApi") + private fun getNotification(context: Context, pendingIntent: PendingIntent, alarm: Alarm): Notification { + val channelId = "alarm_channel" + if (isOreoPlus()) { + val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + val name = context.resources.getString(R.string.alarm) + val importance = NotificationManager.IMPORTANCE_HIGH + NotificationChannel(channelId, name, importance).apply { + enableLights(true) + lightColor = context.getAdjustedPrimaryColor() + enableVibration(alarm.vibrate) + notificationManager.createNotificationChannel(this) + } + } + + val builder = NotificationCompat.Builder(context) + .setContentTitle(context.getString(R.string.alarm)) + .setContentText(alarm.timeInMinutes.formatAlarmTime()) + .setSmallIcon(R.drawable.ic_alarm) + .setContentIntent(pendingIntent) + .setPriority(Notification.PRIORITY_HIGH) + .setDefaults(Notification.DEFAULT_LIGHTS) + .setAutoCancel(true) + .setSound(Uri.parse(alarm.soundUri), AudioManager.STREAM_ALARM) + .setChannelId(channelId) + .addAction(R.drawable.ic_snooze, context.getString(R.string.snooze), getSnoozePendingIntent(context, alarm)) + + if (isLollipopPlus()) { + builder.setVisibility(Notification.VISIBILITY_PUBLIC) + } + + if (alarm.vibrate) { + val vibrateArray = LongArray(100) { 500 } + builder.setVibrate(vibrateArray) + } + + return builder.build() + } + + private fun getSnoozePendingIntent(context: Context, alarm: Alarm): PendingIntent { + val snoozeClass = if (context.config.useSameSnooze) SnoozeService::class.java else SnoozeReminderActivity::class.java + val intent = Intent(context, snoozeClass).setAction("Snooze") + intent.putExtra(ALARM_ID, alarm.id) + return if (context.config.useSameSnooze) { + PendingIntent.getService(context, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT) + } else { + PendingIntent.getActivity(context, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT) + } + } + + private fun getPendingIntent(context: Context, alarm: Alarm): PendingIntent { + val intent = Intent(context, MainActivity::class.java) + return PendingIntent.getActivity(context, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/services/SnoozeService.kt b/app/src/main/kotlin/com/simplemobiletools/clock/services/SnoozeService.kt new file mode 100644 index 0000000..5eb4209 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/clock/services/SnoozeService.kt @@ -0,0 +1,10 @@ +package com.simplemobiletools.clock.services + +import android.app.IntentService +import android.content.Intent + +class SnoozeService : IntentService("Snooze") { + override fun onHandleIntent(intent: Intent) { + + } +} diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml index 41af030..50b4afe 100644 --- a/app/src/main/res/values-pt/strings.xml +++ b/app/src/main/res/values-pt/strings.xml @@ -5,6 +5,7 @@ Vibrate Label No days selected + Alarm Relógio diff --git a/app/src/main/res/values-sk/strings.xml b/app/src/main/res/values-sk/strings.xml index 47705f4..5304d46 100644 --- a/app/src/main/res/values-sk/strings.xml +++ b/app/src/main/res/values-sk/strings.xml @@ -5,6 +5,7 @@ Vibrovanie Štítok Neboli označené žiadne dni + Budík Okno s časom diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b0f8c5c..ddafeda 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -5,6 +5,7 @@ Vibrate Label No days selected + Alarm Clock tab