adding initial alarm reminder handling

This commit is contained in:
tibbi 2018-03-06 20:55:37 +01:00
parent 91a1f059d3
commit 258453d88c
8 changed files with 109 additions and 1 deletions

View File

@ -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'
}

View File

@ -4,6 +4,7 @@
package="com.simplemobiletools.clock">
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:name=".App"
@ -50,6 +51,12 @@
android:label="@string/frequently_asked_questions"
android:parentActivityName="com.simplemobiletools.commons.activities.AboutActivity"/>
<activity
android:name=".activities.SnoozeReminderActivity"
android:theme="@style/Theme.Transparent"/>
<service android:name=".services.SnoozeService"/>
<receiver android:name=".receivers.AlarmReceiver"/>
</application>
</manifest>

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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) {
}
}

View File

@ -5,6 +5,7 @@
<string name="vibrate">Vibrate</string>
<string name="label">Label</string>
<string name="no_days_selected">No days selected</string>
<string name="alarm">Alarm</string>
<!-- Settings -->
<string name="clock_tab">Relógio</string>

View File

@ -5,6 +5,7 @@
<string name="vibrate">Vibrovanie</string>
<string name="label">Štítok</string>
<string name="no_days_selected">Neboli označené žiadne dni</string>
<string name="alarm">Budík</string>
<!-- Settings -->
<string name="clock_tab">Okno s časom</string>

View File

@ -5,6 +5,7 @@
<string name="vibrate">Vibrate</string>
<string name="label">Label</string>
<string name="no_days_selected">No days selected</string>
<string name="alarm">Alarm</string>
<!-- Settings -->
<string name="clock_tab">Clock tab</string>