Migrate services and receivers to data package

This commit is contained in:
Moez Bhatti
2018-05-29 11:11:38 -04:00
parent 554791db49
commit c0968e4b42
35 changed files with 323 additions and 200 deletions
@@ -20,6 +20,8 @@ package common
*/
import android.app.Activity
import android.app.Application
import android.app.Service
import android.content.BroadcastReceiver
import android.support.text.emoji.EmojiCompat
import android.support.text.emoji.FontRequestEmojiCompatConfig
import android.support.v4.provider.FontRequest
@@ -27,10 +29,12 @@ import com.bugsnag.android.Bugsnag
import com.bugsnag.android.Configuration
import com.moez.QKSMS.BuildConfig
import com.moez.QKSMS.R
import common.util.NightModeManager
import util.NightModeManager
import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.HasActivityInjector
import dagger.android.HasBroadcastReceiverInjector
import dagger.android.HasServiceInjector
import injection.AppComponentManager
import injection.appComponent
import io.realm.Realm
@@ -40,7 +44,7 @@ import migration.QkRealmMigration
import timber.log.Timber
import javax.inject.Inject
class QKApplication : Application(), HasActivityInjector {
class QKApplication : Application(), HasActivityInjector, HasBroadcastReceiverInjector, HasServiceInjector {
/**
* Inject this so that it is forced to initialize
@@ -48,7 +52,9 @@ class QKApplication : Application(), HasActivityInjector {
@Suppress("unused")
@Inject lateinit var analyticsManager: AnalyticsManager
@Inject lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
@Inject lateinit var dispatchingActivityInjector: DispatchingAndroidInjector<Activity>
@Inject lateinit var dispatchingBroadcastReceiverInjector: DispatchingAndroidInjector<BroadcastReceiver>
@Inject lateinit var dispatchingServiceInjector: DispatchingAndroidInjector<Service>
@Inject lateinit var nightModeManager: NightModeManager
private val packages = arrayOf("common", "feature", "injection", "filter", "interactor", "manager", "mapper",
@@ -88,7 +94,15 @@ class QKApplication : Application(), HasActivityInjector {
}
override fun activityInjector(): AndroidInjector<Activity> {
return dispatchingAndroidInjector
return dispatchingActivityInjector
}
override fun broadcastReceiverInjector(): AndroidInjector<BroadcastReceiver> {
return dispatchingBroadcastReceiverInjector
}
override fun serviceInjector(): AndroidInjector<Service> {
return dispatchingServiceInjector
}
}
@@ -1,132 +0,0 @@
/*
* 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 common.util
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import receiver.NightModeReceiver
import util.Preferences
import util.tryOrNull
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class NightModeManager @Inject constructor(
private val context: Context,
private val prefs: Preferences) {
fun updateCurrentTheme() {
// If night mode is not on auto, then there's nothing to do here
if (prefs.nightMode.get() != Preferences.NIGHT_MODE_AUTO) {
return
}
val nightStartTime = getPreviousInstanceOfTime(prefs.nightStart.get())
val nightEndTime = getPreviousInstanceOfTime(prefs.nightEnd.get())
// If the last nightStart was more recent than the last nightEnd, then it's night time
prefs.night.set(nightStartTime > nightEndTime)
}
fun updateNightMode(mode: Int) {
prefs.nightMode.set(mode)
// If it's not on auto mode, set the appropriate night mode
if (mode != Preferences.NIGHT_MODE_AUTO) {
prefs.night.set(mode == Preferences.NIGHT_MODE_ON)
}
updateAlarms()
}
fun setNightStart(hour: Int, minute: Int) {
prefs.nightStart.set("$hour:$minute")
updateAlarms()
}
fun setNightEnd(hour: Int, minute: Int) {
prefs.nightEnd.set("$hour:$minute")
updateAlarms()
}
private fun updateAlarms() {
val dayCalendar = createCalendar(prefs.nightEnd.get())
val day = Intent(context, NightModeReceiver::class.java)
val dayIntent = PendingIntent.getBroadcast(context, 0, day, 0)
val nightCalendar = createCalendar(prefs.nightStart.get())
val night = Intent(context, NightModeReceiver::class.java)
val nightIntent = PendingIntent.getBroadcast(context, 1, night, 0)
context.sendBroadcast(day)
context.sendBroadcast(night)
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
if (prefs.nightMode.get() == Preferences.NIGHT_MODE_AUTO) {
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, dayCalendar.timeInMillis, AlarmManager.INTERVAL_DAY, dayIntent)
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, nightCalendar.timeInMillis, AlarmManager.INTERVAL_DAY, nightIntent)
} else {
alarmManager.cancel(dayIntent)
alarmManager.cancel(nightIntent)
}
}
private fun createCalendar(time: String): Calendar {
val calendar = parseTime(time)
return Calendar.getInstance().apply {
set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY))
set(Calendar.MINUTE, calendar.get(Calendar.MINUTE))
}
}
/**
* Parses the hour and minute out of the [time], which should be formatted h:mm
*/
fun parseTime(time: String): Calendar {
return tryOrNull {
val parsedTime = SimpleDateFormat("H:mm", Locale.US).parse(time)
Calendar.getInstance().apply { this.time = parsedTime }
} ?: tryOrNull {
// Parse the legacy timestamp format (<=3.1.3)
val parsedTime = SimpleDateFormat("h:mm a", Locale.US).parse(time)
Calendar.getInstance().apply { this.time = parsedTime }
} ?: Calendar.getInstance().apply { set(Calendar.HOUR_OF_DAY, 18) }
}
/**
* Returns a Calendar set to the most recent occurrence of this time
*/
private fun getPreviousInstanceOfTime(time: String): Calendar {
val currentTime = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(10)
val calendar = createCalendar(time)
while (calendar.timeInMillis > currentTime) {
calendar.add(Calendar.DAY_OF_YEAR, -1)
}
return calendar
}
}
@@ -1,45 +0,0 @@
/*
* 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 feature.compose
import android.content.Context
import android.net.Uri
import android.os.Build
import android.support.v13.view.inputmethod.InputContentInfoCompat
import com.google.android.mms.ContentType
data class Attachment(private val uri: Uri? = null, private val inputContent: InputContentInfoCompat? = null) {
fun getUri(): Uri? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
inputContent?.contentUri ?: uri
} else {
uri
}
}
fun isGif(context: Context): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && inputContent != null) {
inputContent.description.hasMimeType(ContentType.IMAGE_GIF)
} else {
context.contentResolver.getType(uri) == ContentType.IMAGE_GIF
}
}
}
@@ -31,6 +31,7 @@ import io.reactivex.disposables.CompositeDisposable
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.attachment_list_item.view.*
import model.Attachment
import javax.inject.Inject
class AttachmentAdapter @Inject constructor(
@@ -50,6 +50,7 @@ import io.reactivex.Observable
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.compose_activity.*
import model.Attachment
import model.Contact
import model.Message
import java.text.SimpleDateFormat
@@ -19,6 +19,7 @@
package feature.compose
import io.realm.RealmResults
import model.Attachment
import model.Contact
import model.Conversation
import model.Message
@@ -23,6 +23,7 @@ import android.support.v13.view.inputmethod.InputContentInfoCompat
import common.base.QkView
import io.reactivex.Observable
import io.reactivex.subjects.Subject
import model.Attachment
import model.Contact
import model.Message
@@ -49,6 +49,7 @@ import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import io.realm.RealmList
import manager.PermissionManager
import model.Attachment
import model.Contact
import model.Conversation
import model.Message
@@ -27,7 +27,7 @@ import common.base.QkViewModel
import common.util.BillingManager
import common.util.Colors
import common.util.DateFormatter
import common.util.NightModeManager
import util.NightModeManager
import interactor.SyncMessages
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.withLatestFrom
@@ -31,9 +31,9 @@ import android.widget.RemoteViews
import com.moez.QKSMS.R
import common.util.Colors
import common.util.extensions.getColorCompat
import dagger.android.AndroidInjection
import feature.compose.ComposeActivity
import feature.main.MainActivity
import injection.appComponent
import manager.WidgetManager
import timber.log.Timber
import util.Preferences
@@ -44,11 +44,9 @@ class WidgetProvider : AppWidgetProvider() {
@Inject lateinit var colors: Colors
@Inject lateinit var prefs: Preferences
init {
appComponent.inject(this)
}
override fun onReceive(context: Context, intent: Intent) {
AndroidInjection.inject(this, context)
when (intent.action) {
WidgetManager.ACTION_NOTIFY_DATASET_CHANGED -> updateData(context)
else -> super.onReceive(context, intent)
@@ -26,7 +26,7 @@ import feature.themepicker.ThemePickerActivity
import feature.themepicker.ThemePickerActivityModule
@Module
abstract class BuildersModule {
abstract class ActivityBuilderModule {
@ActivityScope
@ContributesAndroidInjector(modules = [MainActivityModule::class])
@@ -31,25 +31,15 @@ import dagger.Component
import dagger.android.support.AndroidSupportInjectionModule
import feature.compose.DetailedChipView
import feature.widget.WidgetAdapter
import feature.widget.WidgetProvider
import receiver.DefaultSmsChangedReceiver
import receiver.MarkReadReceiver
import receiver.MarkSeenReceiver
import receiver.MmsReceivedReceiver
import receiver.MmsSentReceiver
import receiver.MmsUpdatedReceiver
import receiver.NightModeReceiver
import receiver.RemoteMessagingReceiver
import receiver.SendSmsReceiver
import receiver.SmsDeliveredReceiver
import receiver.SmsProviderChangedReceiver
import receiver.SmsReceiver
import receiver.SmsSentReceiver
import service.HeadlessSmsSendService
import javax.inject.Singleton
@Singleton
@Component(modules = [AndroidSupportInjectionModule::class, AppModule::class, BuildersModule::class])
@Component(modules = [
AndroidSupportInjectionModule::class,
AppModule::class,
ActivityBuilderModule::class,
BroadcastReceiverBuilderModule::class,
ServiceBuilderModule::class])
interface AppComponent {
fun inject(application: QKApplication)
@@ -58,22 +48,6 @@ interface AppComponent {
fun inject(fetcher: ContactImageLoader.ContactImageFetcher)
fun inject(receiver: DefaultSmsChangedReceiver)
fun inject(receiver: SmsDeliveredReceiver)
fun inject(receiver: SmsSentReceiver)
fun inject(receiver: MarkSeenReceiver)
fun inject(receiver: MarkReadReceiver)
fun inject(receiver: MmsReceivedReceiver)
fun inject(receiver: MmsSentReceiver)
fun inject(receiver: MmsUpdatedReceiver)
fun inject(receiver: NightModeReceiver)
fun inject(receiver: RemoteMessagingReceiver)
fun inject(receiver: SendSmsReceiver)
fun inject(receiver: SmsProviderChangedReceiver)
fun inject(receiver: SmsReceiver)
fun inject(receiver: WidgetProvider)
fun inject(service: HeadlessSmsSendService)
fun inject(service: WidgetAdapter)
fun inject(view: AvatarView)
@@ -0,0 +1,102 @@
/*
* 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 injection
import dagger.Module
import dagger.android.ContributesAndroidInjector
import feature.widget.WidgetProvider
import receiver.DefaultSmsChangedReceiver
import receiver.MarkReadReceiver
import receiver.MarkSeenReceiver
import receiver.MmsReceivedReceiver
import receiver.MmsReceiver
import receiver.MmsSentReceiver
import receiver.MmsUpdatedReceiver
import receiver.NightModeReceiver
import receiver.RemoteMessagingReceiver
import receiver.SendSmsReceiver
import receiver.SmsDeliveredReceiver
import receiver.SmsProviderChangedReceiver
import receiver.SmsReceiver
import receiver.SmsSentReceiver
@Module
abstract class BroadcastReceiverBuilderModule {
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindDefaultSmsChangedReceiver(): DefaultSmsChangedReceiver
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindMarkReadReceiver(): MarkReadReceiver
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindMarkSeenReceiver(): MarkSeenReceiver
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindMmsReceivedReceiver(): MmsReceivedReceiver
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindMmsReceiver(): MmsReceiver
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindMmsSentReceiver(): MmsSentReceiver
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindMmsUpdatedReceiver(): MmsUpdatedReceiver
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindNightModeReceiver(): NightModeReceiver
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindRemoteMessagingReceiver(): RemoteMessagingReceiver
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindSendSmsReceiver(): SendSmsReceiver
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindSmsDeliveredReceiver(): SmsDeliveredReceiver
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindSmsProviderChangedReceiver(): SmsProviderChangedReceiver
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindSmsReceiver(): SmsReceiver
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindSmsSentReceiver(): SmsSentReceiver
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindWidgetProvider(): WidgetProvider
}
@@ -16,8 +16,17 @@
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package receiver
package injection
import com.android.mms.transaction.PushReceiver
import dagger.Module
import dagger.android.ContributesAndroidInjector
import service.HeadlessSmsSendService
class MmsReceiver : PushReceiver()
@Module
abstract class ServiceBuilderModule {
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindHeadlessSmsSendService(): HeadlessSmsSendService
}
@@ -19,8 +19,8 @@
package interactor
import com.f2prateek.rx.preferences2.RxSharedPreferences
import common.util.NightModeManager
import io.reactivex.Flowable
import util.NightModeManager
import util.Preferences
import javax.inject.Inject
@@ -1,112 +0,0 @@
/*
* 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 interactor
import android.content.Context
import android.graphics.Bitmap
import com.google.android.mms.ContentType
import com.klinker.android.send_message.Message
import com.klinker.android.send_message.Settings
import com.klinker.android.send_message.Transaction
import feature.compose.Attachment
import io.reactivex.Flowable
import repository.ImageRepository
import repository.MessageRepository
import util.Preferences
import java.io.ByteArrayOutputStream
import javax.inject.Inject
class SendMessage @Inject constructor(
private val context: Context,
private val imageRepository: ImageRepository,
private val prefs: Preferences,
private val messageRepo: MessageRepository
) : Interactor<SendMessage.Params>() {
data class Params(val threadId: Long, val addresses: List<String>, val body: String, val attachments: List<Attachment> = listOf())
override fun buildObservable(params: Params): Flowable<Unit> {
return Flowable.just(Unit)
.filter { params.addresses.isNotEmpty() }
.doOnNext {
if (params.addresses.size == 1 && params.attachments.isEmpty()) {
messageRepo.sendSmsAndPersist(params.threadId, params.addresses.first(), params.body)
} else {
sendMms(params.threadId, params.addresses, params.body, params.attachments)
}
}
.doOnNext { messageRepo.updateConversations(params.threadId) }
.doOnNext { messageRepo.markUnarchived(params.threadId) }
}
private fun sendMms(threadId: Long, addresses: List<String>, body: String, attachments: List<Attachment>) {
val settings = Settings()
val message = Message(body, addresses.toTypedArray())
// Add the GIFs as attachments. The app currently can't compress them, which may result
// in a lot of these messages failing to send
// TODO Add support for GIF compression
attachments
.filter { attachment -> attachment.isGif(context) }
.map { attachment -> attachment.getUri() }
.map { uri -> context.contentResolver.openInputStream(uri) }
.map { inputStream -> inputStream.readBytes() }
.forEach { bitmap -> message.addMedia(bitmap, ContentType.IMAGE_GIF) }
// Compress the images and add them as attachments
var totalImageBytes = 0
attachments
.filter { attachment -> !attachment.isGif(context) }
.mapNotNull { attachment -> attachment.getUri() }
.mapNotNull { uri -> imageRepository.loadImage(uri) }
.also { totalImageBytes = it.sumBy { it.allocationByteCount } }
.map { bitmap ->
val byteRatio = bitmap.allocationByteCount / totalImageBytes.toFloat()
shrink(bitmap, (prefs.mmsSize.get() * 1024 * byteRatio).toInt())
}
.forEach { bitmap -> message.addMedia(bitmap, ContentType.IMAGE_JPEG) }
val transaction = Transaction(context, settings)
transaction.sendNewMessage(message, threadId)
}
private fun shrink(src: Bitmap, maxBytes: Int): ByteArray {
var step = 0.0
val factor = 0.5
val quality = 60
val height = src.height
val width = src.width
val stream = ByteArrayOutputStream()
src.compress(Bitmap.CompressFormat.JPEG, 100, stream)
while (maxBytes > 0 && stream.size() > maxBytes) {
step++
val scale = Math.pow(factor, step)
stream.reset()
Bitmap.createScaledBitmap(src, (width * scale).toInt(), (height * scale).toInt(), false)
.compress(Bitmap.CompressFormat.JPEG, quality, stream)
}
return stream.toByteArray()
}
}
@@ -1,47 +0,0 @@
/*
* 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 receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.provider.Telephony
import android.support.annotation.RequiresApi
import injection.appComponent
import interactor.SyncMessages
import util.Preferences
import javax.inject.Inject
class DefaultSmsChangedReceiver : BroadcastReceiver() {
@Inject lateinit var prefs: Preferences
@Inject lateinit var syncMessages: SyncMessages
@RequiresApi(Build.VERSION_CODES.N)
override fun onReceive(context: Context, intent: Intent) {
appComponent.inject(this)
if (intent.getBooleanExtra(Telephony.Sms.Intents.EXTRA_IS_DEFAULT_SMS_APP, false)) {
val pendingResult = goAsync()
syncMessages.execute(Unit, { pendingResult.finish() })
}
}
}
@@ -1,39 +0,0 @@
/*
* 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 receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import injection.appComponent
import interactor.MarkRead
import javax.inject.Inject
class MarkReadReceiver : BroadcastReceiver() {
@Inject lateinit var markRead: MarkRead
override fun onReceive(context: Context, intent: Intent) {
appComponent.inject(this)
val threadId = intent.getLongExtra("threadId", 0)
markRead.execute(threadId)
}
}
@@ -1,39 +0,0 @@
/*
* 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 receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import injection.appComponent
import interactor.MarkSeen
import javax.inject.Inject
class MarkSeenReceiver : BroadcastReceiver() {
@Inject lateinit var markSeen: MarkSeen
override fun onReceive(context: Context, intent: Intent) {
appComponent.inject(this)
val threadId = intent.getLongExtra("threadId", 0)
markSeen.execute(threadId)
}
}
@@ -1,40 +0,0 @@
/*
* 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 receiver
import android.net.Uri
import com.klinker.android.send_message.MmsReceivedReceiver
import injection.appComponent
import interactor.ReceiveMms
import javax.inject.Inject
class MmsReceivedReceiver : MmsReceivedReceiver() {
@Inject lateinit var receiveMms: ReceiveMms
override fun onMessageReceived(messageUri: Uri?) {
appComponent.inject(this)
messageUri?.let { uri ->
val pendingResult = goAsync()
receiveMms.execute(uri) { pendingResult.finish() }
}
}
}
@@ -1,91 +0,0 @@
/*
* 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 receiver
import android.app.Activity
import android.content.BroadcastReceiver
import android.content.ContentUris
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.Telephony
import com.google.android.mms.MmsException
import com.google.android.mms.util_alt.SqliteWrapper
import com.klinker.android.send_message.Transaction
import injection.appComponent
import interactor.SyncMessage
import timber.log.Timber
import java.io.File
import javax.inject.Inject
class MmsSentReceiver : BroadcastReceiver() {
@Inject lateinit var syncMessage: SyncMessage
override fun onReceive(context: Context, intent: Intent) {
appComponent.inject(this)
Timber.v("MMS sending result: $resultCode")
val uri = Uri.parse(intent.getStringExtra(Transaction.EXTRA_CONTENT_URI))
Timber.v(uri.toString())
when (resultCode) {
Activity.RESULT_OK -> {
Timber.v("MMS has finished sending, marking it as so in the database")
val values = ContentValues(1)
values.put(Telephony.Mms.MESSAGE_BOX, Telephony.Mms.MESSAGE_BOX_SENT)
SqliteWrapper.update(context, context.contentResolver, uri, values, null, null)
}
else -> {
Timber.v("MMS has failed to send, marking it as so in the database")
try {
val messageId = ContentUris.parseId(uri)
val values = ContentValues(1)
values.put(Telephony.Mms.MESSAGE_BOX, Telephony.Mms.MESSAGE_BOX_FAILED)
SqliteWrapper.update(context, context.contentResolver, Telephony.Mms.CONTENT_URI, values,
"${Telephony.Mms._ID} = ?", arrayOf(messageId.toString()))
// TODO this query isn't able to find any results
// Need to figure out why the message isn't appearing in the PendingMessages Uri,
// so that we can properly assign the error type
val errorTypeValues = ContentValues(1)
errorTypeValues.put(Telephony.MmsSms.PendingMessages.ERROR_TYPE, Telephony.MmsSms.ERR_TYPE_GENERIC_PERMANENT)
SqliteWrapper.update(context, context.contentResolver, Telephony.MmsSms.PendingMessages.CONTENT_URI,
errorTypeValues, "${Telephony.MmsSms.PendingMessages.MSG_ID} = ?", arrayOf(messageId.toString()))
} catch (e: MmsException) {
e.printStackTrace()
}
}
}
val filePath = intent.getStringExtra(Transaction.EXTRA_FILE_PATH)
Timber.v(filePath)
File(filePath).delete()
Uri.parse(intent.getStringExtra("content_uri"))?.let { uri ->
val pendingResult = goAsync()
syncMessage.execute(uri) { pendingResult.finish() }
}
}
}
@@ -1,45 +0,0 @@
/*
* 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 receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.Uri
import injection.appComponent
import interactor.SyncMessage
import javax.inject.Inject
class MmsUpdatedReceiver : BroadcastReceiver() {
companion object {
const val URI = "uri"
}
@Inject lateinit var syncMessage: SyncMessage
override fun onReceive(context: Context, intent: Intent) {
appComponent.inject(this)
intent.getStringExtra(URI)?.let { uriString ->
val pendingResult = goAsync()
syncMessage.execute(Uri.parse(uriString)) { pendingResult.finish() }
}
}
}
@@ -1,39 +0,0 @@
/*
* 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 receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import injection.appComponent
import common.util.NightModeManager
import javax.inject.Inject
class NightModeReceiver : BroadcastReceiver() {
@Inject lateinit var nightModeManager: NightModeManager
init {
appComponent.inject(this)
}
override fun onReceive(context: Context, intent: Intent) {
nightModeManager.updateCurrentTheme()
}
}
@@ -1,48 +0,0 @@
/*
* 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 receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.support.v4.app.RemoteInput
import injection.appComponent
import interactor.MarkRead
import interactor.SendMessage
import javax.inject.Inject
class RemoteMessagingReceiver : BroadcastReceiver() {
@Inject lateinit var sendMessage: SendMessage
@Inject lateinit var markRead: MarkRead
override fun onReceive(context: Context, intent: Intent) {
appComponent.inject(this)
val remoteInput = RemoteInput.getResultsFromIntent(intent)
val bundle = intent.extras
if (remoteInput != null && bundle != null) {
val address = bundle.getString("address")
val threadId = bundle.getLong("threadId")
val body = remoteInput.getCharSequence("body").toString()
markRead.execute(threadId)
sendMessage.execute(SendMessage.Params(threadId, listOf(address), body))
}
}
}
@@ -1,44 +0,0 @@
/*
* 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 receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import injection.appComponent
import interactor.RetrySending
import repository.MessageRepository
import javax.inject.Inject
class SendSmsReceiver : BroadcastReceiver() {
@Inject lateinit var messageRepo: MessageRepository
@Inject lateinit var retrySending: RetrySending
override fun onReceive(context: Context, intent: Intent) {
appComponent.inject(this)
val id = intent.getLongExtra("id", 0L)
messageRepo.getMessage(id)?.let { message ->
val result = goAsync()
retrySending.execute(message) { result.finish() }
}
}
}
@@ -1,57 +0,0 @@
/*
* 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 receiver
import android.app.Activity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import injection.appComponent
import interactor.MarkDelivered
import interactor.MarkDeliveryFailed
import javax.inject.Inject
class SmsDeliveredReceiver : BroadcastReceiver() {
@Inject lateinit var markDelivered: MarkDelivered
@Inject lateinit var markDeliveryFailed: MarkDeliveryFailed
init {
appComponent.inject(this)
}
override fun onReceive(context: Context, intent: Intent) {
val id = intent.getLongExtra("id", 0L)
when (resultCode) {
// TODO notify about delivery
Activity.RESULT_OK -> {
val pendingResult = goAsync()
markDelivered.execute(id) { pendingResult.finish() }
}
// TODO notify about delivery failure
Activity.RESULT_CANCELED -> {
val pendingResult = goAsync()
markDeliveryFailed.execute(MarkDeliveryFailed.Params(id, resultCode)) { pendingResult.finish() }
}
}
}
}
@@ -1,55 +0,0 @@
/*
* 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 receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import injection.appComponent
import interactor.SyncMessage
import javax.inject.Inject
/**
* When the SMS contentprovider is changed by a process other than us, we need to sync the Uri that
* was changed.
*
* This can happen if a message is sent through something like Pushbullet or Google Assistant.
*
* This only works on API 24+, so to fully solve this problem we'll need a smarter way of running
* partial syncs on older devices.
*
* https://developer.android.com/reference/android/provider/Telephony.Sms.Intents.html#ACTION_EXTERNAL_PROVIDER_CHANGE
*/
class SmsProviderChangedReceiver : BroadcastReceiver() {
@Inject lateinit var syncMessage: SyncMessage
override fun onReceive(context: Context, intent: Intent) {
appComponent.inject(this)
// Obtain the uri for the changed data
// If the value is null, don't continue
val uri = intent.data ?: return
// Sync the message to our realm
val pendingResult = goAsync()
syncMessage.execute(uri) { pendingResult.finish() }
}
}
@@ -1,42 +0,0 @@
/*
* 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 receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.provider.Telephony.Sms
import injection.appComponent
import interactor.ReceiveSms
import javax.inject.Inject
class SmsReceiver : BroadcastReceiver() {
@Inject lateinit var receiveMessage: ReceiveSms
override fun onReceive(context: Context, intent: Intent) {
appComponent.inject(this)
Sms.Intents.getMessagesFromIntent(intent)?.let { messages ->
val pendingResult = goAsync()
receiveMessage.execute(messages, { pendingResult.finish() })
}
}
}
@@ -1,56 +0,0 @@
/*
* 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 receiver
import android.app.Activity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.telephony.SmsManager
import injection.appComponent
import interactor.MarkFailed
import interactor.MarkSent
import javax.inject.Inject
class SmsSentReceiver : BroadcastReceiver() {
@Inject lateinit var markSent: MarkSent
@Inject lateinit var markFailed: MarkFailed
override fun onReceive(context: Context, intent: Intent) {
appComponent.inject(this)
val id = intent.getLongExtra("id", 0L)
when (resultCode) {
Activity.RESULT_OK -> {
val pendingResult = goAsync()
markSent.execute(id) { pendingResult.finish() }
}
SmsManager.RESULT_ERROR_GENERIC_FAILURE,
SmsManager.RESULT_ERROR_NO_SERVICE,
SmsManager.RESULT_ERROR_NULL_PDU,
SmsManager.RESULT_ERROR_RADIO_OFF -> {
val pendingResult = goAsync()
markFailed.execute(MarkFailed.Params(id, resultCode)) { pendingResult.finish() }
}
}
}
}
@@ -1,53 +0,0 @@
/*
* 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 service
import android.app.IntentService
import android.content.Intent
import android.net.Uri
import android.telephony.TelephonyManager
import injection.appComponent
import interactor.SendMessage
import repository.MessageRepository
import javax.inject.Inject
class HeadlessSmsSendService : IntentService("HeadlessSmsSendService") {
@Inject lateinit var messageRepo: MessageRepository
@Inject lateinit var sendMessage: SendMessage
override fun onHandleIntent(intent: Intent?) {
if (intent?.action != TelephonyManager.ACTION_RESPOND_VIA_MESSAGE) return
appComponent.inject(this)
intent.extras?.getString(Intent.EXTRA_TEXT)?.takeIf { it.isNotBlank() }?.let { body ->
val intentUri = intent.data
val recipients = getRecipients(intentUri).split(";")
val threadId = messageRepo.getOrCreateConversation(recipients).blockingGet()?.id ?: 0L
sendMessage.execute(SendMessage.Params(threadId, recipients, body))
}
}
private fun getRecipients(uri: Uri): String {
val base = uri.schemeSpecificPart
val position = base.indexOf('?')
return if (position == -1) base else base.substring(0, position)
}
}