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? {
@@ -0,0 +1,43 @@
/*
* 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.interactor
import com.moez.QKSMS.repository.ScheduledMessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class AddScheduledMessage @Inject constructor(
private val scheduledMessageRepo: ScheduledMessageRepository,
private val updateScheduledMessageAlarms: UpdateScheduledMessageAlarms
) : Interactor<AddScheduledMessage.Params>() {
data class Params(val date: Long,
val subId: Int,
val recipients: List<String>,
val sendAsGroup: Boolean,
val body: String,
val attachments: List<String>)
override fun buildObservable(params: Params): Flowable<*> {
return Flowable.just(params)
.map { scheduledMessageRepo.saveScheduledMessage(it.date, it.subId, it.recipients, it.sendAsGroup, it.body, it.attachments) }
.flatMap { updateScheduledMessageAlarms.buildObservable(Unit) }
}
}
@@ -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.interactor
import com.moez.QKSMS.manager.AlarmManager
import com.moez.QKSMS.repository.ScheduledMessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class UpdateScheduledMessageAlarms @Inject constructor(
private val alarmManager: AlarmManager,
private val scheduledMessageRepo: ScheduledMessageRepository
) : Interactor<Unit>() {
override fun buildObservable(params: Unit): Flowable<*> {
return Flowable.just(params)
.map { scheduledMessageRepo.getScheduledMessages() }
.flatMap { messages -> Flowable.fromIterable(messages) }
.map { message -> Pair(message.id, message.date) }
.doOnNext { (id, date) -> alarmManager.setAlarm(date, alarmManager.getScheduledMessageIntent(id)) }
}
}
@@ -0,0 +1,29 @@
/*
* 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
interface AlarmManager {
fun getScheduledMessageIntent(id: Long): PendingIntent
fun setAlarm(date: Long, intent: PendingIntent)
}
@@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.RECEIVE_MMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
@@ -75,6 +76,11 @@
<activity android:name=".feature.settings.about.AboutActivity" />
<activity android:name=".feature.scheduled.ScheduledActivity" />
<receiver android:name=".receiver.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name=".receiver.DefaultSmsChangedReceiver">
<intent-filter>
<action android:name="android.provider.action.DEFAULT_SMS_PACKAGE_CHANGED" />
@@ -38,6 +38,7 @@ import com.moez.QKSMS.extensions.isImage
import com.moez.QKSMS.extensions.mapNotNull
import com.moez.QKSMS.extensions.removeAccents
import com.moez.QKSMS.filter.ContactFilter
import com.moez.QKSMS.interactor.AddScheduledMessage
import com.moez.QKSMS.interactor.CancelDelayedMessage
import com.moez.QKSMS.interactor.ContactSync
import com.moez.QKSMS.interactor.DeleteMessages
@@ -80,6 +81,7 @@ class ComposeViewModel @Inject constructor(
@Named("text") private val sharedText: String,
@Named("attachments") private val sharedAttachments: List<Attachment>,
private val context: Context,
private val addScheduledMessage: AddScheduledMessage,
private val billingManager: BillingManager,
private val cancelMessage: CancelDelayedMessage,
private val contactFilter: ContactFilter,
@@ -93,7 +95,6 @@ class ComposeViewModel @Inject constructor(
private val permissionManager: PermissionManager,
private val prefs: Preferences,
private val retrySending: RetrySending,
private val scheduledMessageRepo: ScheduledMessageRepository,
private val sendMessage: SendMessage,
private val subscriptionManager: SubscriptionManagerCompat,
private val syncContacts: ContactSync
@@ -583,7 +584,8 @@ class ComposeViewModel @Inject constructor(
state.scheduled != 0L -> {
newState { copy(scheduled = 0) }
val uris = attachments.map { it.getUri() }.map { it.toString() }
scheduledMessageRepo.saveScheduledMessage(state.scheduled, subId, addresses, state.sendAsGroup, body, uris)
val params = AddScheduledMessage.Params(state.scheduled, subId, addresses, state.sendAsGroup, body, uris)
addScheduledMessage.execute(params)
context.toast(R.string.compose_scheduled_toast)
}
@@ -26,6 +26,8 @@ import androidx.lifecycle.ViewModelProvider
import com.f2prateek.rx.preferences2.RxSharedPreferences
import com.moez.QKSMS.common.ViewModelFactory
import com.moez.QKSMS.common.util.NotificationManagerImpl
import com.moez.QKSMS.manager.AlarmManager
import com.moez.QKSMS.manager.AlarmManagerImpl
import com.moez.QKSMS.manager.AnalyticsManager
import com.moez.QKSMS.manager.AnalyticsManagerImpl
import com.moez.QKSMS.manager.ExternalBlockingManager
@@ -87,6 +89,9 @@ class AppModule(private var application: Application) {
// Manager
@Provides
fun provideAlarmManager(manager: AlarmManagerImpl): AlarmManager = manager
@Provides
fun provideAnalyticsManager(manager: AnalyticsManagerImpl): AnalyticsManager = manager
@@ -19,6 +19,7 @@
package com.moez.QKSMS.injection
import com.moez.QKSMS.feature.widget.WidgetProvider
import com.moez.QKSMS.receiver.BootReceiver
import com.moez.QKSMS.receiver.DefaultSmsChangedReceiver
import com.moez.QKSMS.receiver.MarkReadReceiver
import com.moez.QKSMS.receiver.MarkSeenReceiver
@@ -40,6 +41,10 @@ import dagger.android.ContributesAndroidInjector
@Module
abstract class BroadcastReceiverBuilderModule {
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindBootReceiver(): BootReceiver
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindDefaultSmsChangedReceiver(): DefaultSmsChangedReceiver