Update all scheduled message alarms when device is rebooted

This commit is contained in:
Moez Bhatti
2018-06-29 23:19:54 -04:00
parent 0046f0fed8
commit ffe9935e44
10 changed files with 218 additions and 24 deletions
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.manager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import com.klinker.android.send_message.BroadcastUtils
import javax.inject.Inject
class AlarmManagerImpl @Inject constructor(private val context: Context) : AlarmManager {
override fun getScheduledMessageIntent(id: Long): PendingIntent {
val action = "com.moez.QKSMS.SEND_SCHEDULED_MESSAGE"
val intent = Intent(action).putExtra("id", id)
BroadcastUtils.addClassName(context, intent, action)
return PendingIntent.getBroadcast(context, id.toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
override fun setAlarm(date: Long, intent: PendingIntent) {
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as android.app.AlarmManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(android.app.AlarmManager.RTC_WAKEUP, date, intent)
} else {
alarmManager.setExact(android.app.AlarmManager.RTC_WAKEUP, date, intent)
}
}
}
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.moez.QKSMS.interactor.UpdateScheduledMessageAlarms
import dagger.android.AndroidInjection
import javax.inject.Inject
class BootReceiver : BroadcastReceiver() {
@Inject lateinit var updateScheduledMessageAlarms: UpdateScheduledMessageAlarms
override fun onReceive(context: Context, intent: Intent?) {
AndroidInjection.inject(this, context)
val result = goAsync()
updateScheduledMessageAlarms.execute(Unit) { result.finish() }
}
}
@@ -1,20 +1,12 @@
package com.moez.QKSMS.repository
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import com.klinker.android.send_message.BroadcastUtils
import com.moez.QKSMS.model.ScheduledMessage
import io.realm.Realm
import io.realm.RealmList
import io.realm.RealmResults
import javax.inject.Inject
class ScheduledMessageRepositoryImpl @Inject constructor(
private val context: Context
) : ScheduledMessageRepository {
class ScheduledMessageRepositoryImpl @Inject constructor() : ScheduledMessageRepository {
override fun saveScheduledMessage(date: Long, subId: Int, recipients: List<String>, sendAsGroup: Boolean,
body: String, attachments: List<String>) {
@@ -26,18 +18,6 @@ class ScheduledMessageRepositoryImpl @Inject constructor(
val message = ScheduledMessage(id, date, subId, recipientsRealmList, sendAsGroup, body, attachmentsRealmList)
val action = "com.moez.QKSMS.SEND_SCHEDULED_MESSAGE"
val intent = Intent(action).putExtra("id", id)
BroadcastUtils.addClassName(context, intent, action)
val pendingIntent = PendingIntent.getBroadcast(context, id.toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT)
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, date, pendingIntent)
} else {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, date, pendingIntent)
}
realm.executeTransaction { realm.insertOrUpdate(message) }
}
}
@@ -46,7 +26,7 @@ class ScheduledMessageRepositoryImpl @Inject constructor(
return Realm.getDefaultInstance()
.where(ScheduledMessage::class.java)
.sort("date")
.findAllAsync()
.findAll()
}
override fun getScheduledMessage(id: Long): ScheduledMessage? {