Move packages to com.moez.QKSMS

This commit is contained in:
Moez Bhatti
2018-06-20 14:29:07 -04:00
parent d8141737b3
commit a5f518042e
268 changed files with 1101 additions and 1116 deletions
@@ -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.extensions
import io.reactivex.Flowable
import io.reactivex.Observable
data class Optional<out T>(val value: T?) {
fun notNull() = value != null
}
fun <T, R> Flowable<T>.mapNotNull(mapper: (T) -> R?): Flowable<R>
= map { input -> Optional(mapper(input)) }
.filter { optional -> optional.notNull() }
.map { optional -> optional.value }
fun <T, R> Observable<T>.mapNotNull(mapper: (T) -> R?): Observable<R>
= map { input -> Optional(mapper(input)) }
.filter { optional -> optional.notNull() }
.map { optional -> optional.value }
fun <T> Observable<T>.toFlowable(): Flowable<T> = this.toFlowable(io.reactivex.BackpressureStrategy.BUFFER)
@@ -0,0 +1,33 @@
/*
* 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.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class CancelDelayedMessage @Inject constructor(private val messageRepo: MessageRepository) : Interactor<Long>() {
override fun buildObservable(params: Long): Flowable<*> {
return Flowable.just(params)
.doOnNext { id -> messageRepo.cancelDelayedSms(id) }
.doOnNext { id -> messageRepo.deleteMessages(id) }
}
}
@@ -0,0 +1,37 @@
/*
* 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.SyncRepository
import io.reactivex.Flowable
import timber.log.Timber
import java.util.concurrent.TimeUnit
import javax.inject.Inject
open class ContactSync @Inject constructor(private val syncManager: SyncRepository) : Interactor<Unit>() {
override fun buildObservable(params: Unit): Flowable<Long> {
return Flowable.just(System.currentTimeMillis())
.doOnNext { syncManager.syncContacts() }
.map { startTime -> System.currentTimeMillis() - startTime }
.map { elapsed -> TimeUnit.MILLISECONDS.toSeconds(elapsed) }
.doOnNext { seconds -> Timber.v("Completed sync in $seconds seconds") }
}
}
@@ -0,0 +1,36 @@
/*
* 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.ConversationRepository
import io.reactivex.Flowable
import javax.inject.Inject
class DeleteConversations @Inject constructor(
private val conversationRepo: ConversationRepository,
private val updateBadge: UpdateBadge
) : Interactor<List<Long>>() {
override fun buildObservable(params: List<Long>): Flowable<*> {
return Flowable.just(params.toLongArray())
.doOnNext { threadIds -> conversationRepo.deleteConversations(*threadIds) }
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
}
@@ -0,0 +1,41 @@
/*
* 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.ConversationRepository
import com.moez.QKSMS.repository.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class DeleteMessages @Inject constructor(
private val conversationRepo: ConversationRepository,
private val messageRepo: MessageRepository,
private val updateBadge: UpdateBadge
) : Interactor<DeleteMessages.Params>() {
data class Params(val messageIds: List<Long>, val threadId: Long? = null)
override fun buildObservable(params: Params): Flowable<*> {
return Flowable.just(params.messageIds.toLongArray())
.doOnNext { messageIds -> messageRepo.deleteMessages(*messageIds) } // Delete the messages
.doOnNext { params.threadId?.let { conversationRepo.updateConversations(it) } } // Update the conversation
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
}
@@ -0,0 +1,51 @@
/*
* 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 io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
abstract class Interactor<in Params> : Disposable {
private val disposables: CompositeDisposable = CompositeDisposable()
abstract fun buildObservable(params: Params): Flowable<*>
fun execute(params: Params, onComplete: () -> Unit = {}) {
disposables += buildObservable(params)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnComplete(onComplete)
.subscribe({}, Timber::w)
}
override fun dispose() {
return disposables.dispose()
}
override fun isDisposed(): Boolean {
return disposables.isDisposed
}
}
@@ -0,0 +1,32 @@
/*
* 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.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class MarkAllSeen @Inject constructor(private val messageRepo: MessageRepository) : Interactor<Unit>() {
override fun buildObservable(params: Unit): Flowable<Unit> {
return Flowable.just(Unit)
.doOnNext { messageRepo.markAllSeen() }
}
}
@@ -0,0 +1,32 @@
/*
* 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.ConversationRepository
import io.reactivex.Flowable
import javax.inject.Inject
class MarkArchived @Inject constructor(private val conversationRepo: ConversationRepository) : Interactor<List<Long>>() {
override fun buildObservable(params: List<Long>): Flowable<*> {
return Flowable.just(params.toLongArray())
.doOnNext { threadIds -> conversationRepo.markArchived(*threadIds) }
}
}
@@ -0,0 +1,32 @@
/*
* 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.ConversationRepository
import io.reactivex.Flowable
import javax.inject.Inject
class MarkBlocked @Inject constructor(private val conversationRepo: ConversationRepository) : Interactor<List<Long>>() {
override fun buildObservable(params: List<Long>): Flowable<*> {
return Flowable.just(params.toLongArray())
.doOnNext { threadIds -> conversationRepo.markBlocked(*threadIds) }
}
}
@@ -0,0 +1,32 @@
/*
* 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.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class MarkDelivered @Inject constructor(private val messageRepo: MessageRepository) : Interactor<Long>() {
override fun buildObservable(params: Long): Flowable<Unit> {
return Flowable.just(Unit)
.doOnNext { messageRepo.markDelivered(params) }
}
}
@@ -0,0 +1,36 @@
/*
* 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.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class MarkDeliveryFailed @Inject constructor(
private val messageRepo: MessageRepository
) : Interactor<MarkDeliveryFailed.Params>() {
data class Params(val id: Long, val resultCode: Int)
override fun buildObservable(params: Params): Flowable<Unit> {
return Flowable.just(Unit)
.doOnNext { messageRepo.markDeliveryFailed(params.id, params.resultCode) }
}
}
@@ -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.NotificationManager
import com.moez.QKSMS.repository.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class MarkFailed @Inject constructor(
private val messageRepo: MessageRepository,
private val notificationManager: NotificationManager
) : Interactor<MarkFailed.Params>() {
data class Params(val id: Long, val resultCode: Int)
override fun buildObservable(params: Params): Flowable<Unit> {
return Flowable.just(Unit)
.doOnNext { messageRepo.markFailed(params.id, params.resultCode) }
.doOnNext { notificationManager.notifyFailed(params.id) }
}
}
@@ -0,0 +1,42 @@
/*
* 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.NotificationManager
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.repository.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class MarkRead @Inject constructor(
private val conversationRepo: ConversationRepository,
private val messageRepo: MessageRepository,
private val notificationManager: NotificationManager,
private val updateBadge: UpdateBadge
) : Interactor<List<Long>>() {
override fun buildObservable(params: List<Long>): Flowable<*> {
return Flowable.just(params.toLongArray())
.doOnNext { threadIds -> messageRepo.markRead(*threadIds) }
.doOnNext { threadIds -> conversationRepo.updateConversations(*threadIds) } // Update the conversation
.doOnNext { threadIds -> threadIds.forEach(notificationManager::update) }
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
}
@@ -0,0 +1,32 @@
/*
* 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.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class MarkSeen @Inject constructor(private val messageRepo: MessageRepository) : Interactor<Long>() {
override fun buildObservable(params: Long): Flowable<Unit> {
return Flowable.just(Unit)
.doOnNext { messageRepo.markSeen(params) }
}
}
@@ -0,0 +1,32 @@
/*
* 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.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class MarkSent @Inject constructor(private val messageRepo: MessageRepository) : Interactor<Long>() {
override fun buildObservable(params: Long): Flowable<Unit> {
return Flowable.just(Unit)
.doOnNext { messageRepo.markSent(params) }
}
}
@@ -0,0 +1,32 @@
/*
* 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.ConversationRepository
import io.reactivex.Flowable
import javax.inject.Inject
class MarkUnarchived @Inject constructor(private val conversationRepo: ConversationRepository) : Interactor<List<Long>>() {
override fun buildObservable(params: List<Long>): Flowable<*> {
return Flowable.just(params.toLongArray())
.doOnNext { threadIds -> conversationRepo.markUnarchived(*threadIds) }
}
}
@@ -0,0 +1,32 @@
/*
* 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.ConversationRepository
import io.reactivex.Flowable
import javax.inject.Inject
class MarkUnblocked @Inject constructor(private val conversationRepo: ConversationRepository) : Interactor<List<Long>>() {
override fun buildObservable(params: List<Long>): Flowable<*> {
return Flowable.just(params.toLongArray())
.doOnNext { threadIds -> conversationRepo.markUnblocked(*threadIds) }
}
}
@@ -0,0 +1,36 @@
/*
* 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.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class MarkUnread @Inject constructor(
private val messageRepo: MessageRepository,
private val updateBadge: UpdateBadge
) : Interactor<List<Long>>() {
override fun buildObservable(params: List<Long>): Flowable<*> {
return Flowable.just(params.toLongArray())
.doOnNext { threadId -> messageRepo.markUnread(*threadId) }
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
}
@@ -0,0 +1,63 @@
/*
* 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 android.net.Uri
import com.moez.QKSMS.extensions.mapNotNull
import com.moez.QKSMS.manager.ExternalBlockingManager
import com.moez.QKSMS.manager.NotificationManager
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.repository.MessageRepository
import com.moez.QKSMS.repository.SyncRepository
import io.reactivex.Flowable
import java.util.concurrent.TimeUnit
import javax.inject.Inject
class ReceiveMms @Inject constructor(
private val conversationRepo: ConversationRepository,
private val externalBlockingManager: ExternalBlockingManager,
private val syncManager: SyncRepository,
private val messageRepo: MessageRepository,
private val notificationManager: NotificationManager,
private val updateBadge: UpdateBadge
) : Interactor<Uri>() {
override fun buildObservable(params: Uri): Flowable<*> {
return Flowable.just(params)
.mapNotNull { uri -> syncManager.syncMessage(uri) } // Sync the message
.filter { message ->
// Because we use the smsmms library for receiving and storing MMS, we'll need
// to check if it should be blocked after we've pulled it into realm. If it
// turns out that it should be blocked, then delete it
// TODO Don't store blocked messages in the first place
!externalBlockingManager.shouldBlock(message.address).blockingGet().also { blocked ->
if (blocked) messageRepo.deleteMessages(message.id)
}
}
.doOnNext { message -> conversationRepo.updateConversations(message.threadId) } // Update the conversation
.mapNotNull { message -> conversationRepo.getOrCreateConversation(message.threadId) } // Map message to conversation
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
.doOnNext { conversation -> if (conversation.archived) conversationRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
.map { conversation -> conversation.id } // Map to the id because [delay] will put us on the wrong thread
.delay(1, TimeUnit.SECONDS) // Wait one second before trying to notify, in case the foreground app marks it as read first
.doOnNext { threadId -> notificationManager.update(threadId) } // Update the notification
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
}
@@ -0,0 +1,69 @@
/*
* 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 android.telephony.SmsMessage
import com.moez.QKSMS.extensions.mapNotNull
import com.moez.QKSMS.manager.ExternalBlockingManager
import com.moez.QKSMS.manager.NotificationManager
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.repository.MessageRepository
import io.reactivex.Flowable
import java.util.concurrent.TimeUnit
import javax.inject.Inject
class ReceiveSms @Inject constructor(
private val conversationRepo: ConversationRepository,
private val externalBlockingManager: ExternalBlockingManager,
private val messageRepo: MessageRepository,
private val notificationManager: NotificationManager,
private val updateBadge: UpdateBadge
) : Interactor<ReceiveSms.Params>() {
class Params(val subId: Int, val messages: Array<SmsMessage>)
override fun buildObservable(params: Params): Flowable<*> {
return Flowable.just(params)
.filter { it.messages.isNotEmpty() }
.filter {
// Don't continue if the sender is blocked
val address = it.messages[0].displayOriginatingAddress
!externalBlockingManager.shouldBlock(address).blockingGet()
}
.map {
val messages = it.messages
val address = messages[0].displayOriginatingAddress
val time = messages[0].timestampMillis
val body: String = messages
.map { message -> message.displayMessageBody }
.reduce { body, new -> body + new }
messageRepo.insertReceivedSms(it.subId, address, body, time) // Add the message to the db
}
.doOnNext { message -> conversationRepo.updateConversations(message.threadId) } // Update the conversation
.mapNotNull { message -> conversationRepo.getOrCreateConversation(message.threadId) } // Map message to conversation
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
.doOnNext { conversation -> if (conversation.archived) conversationRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
.map { conversation -> conversation.id } // Map to the id because [delay] will put us on the wrong thread
.delay(1, TimeUnit.SECONDS) // Wait one second before trying to notify, in case the foreground app marks it as read first
.doOnNext { threadId -> notificationManager.update(threadId) } // Update the notification
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
}
@@ -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.interactor
import com.moez.QKSMS.model.Message
import com.moez.QKSMS.repository.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class RetrySending @Inject constructor(private val messageRepo: MessageRepository) : Interactor<Message>() {
override fun buildObservable(params: Message): Flowable<Message> {
// We don't want to touch the supplied message on another thread in case it's a live realm
// object, so copy the required fields into a new object that is safe to pass around threads
val message = Message().apply {
id = params.id
type = params.type
address = params.address
body = params.body
subId = params.subId
}
return Flowable.just(message)
.filter { message.isSms() } // TODO support resending failed MMS
.doOnNext { messageRepo.markSending(message.id) }
.doOnNext { messageRepo.sendSms(message) }
}
}
@@ -0,0 +1,38 @@
/*
* 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.ImageRepository
import com.moez.QKSMS.repository.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class SaveImage @Inject constructor(
private val imageRepository: ImageRepository,
private val messageRepo: MessageRepository
) : Interactor<Long>() {
override fun buildObservable(params: Long): Flowable<*> {
return Flowable.just(params)
.map { partId -> messageRepo.getPart(partId) }
.map { part -> part.getUri() }
.doOnNext { uri -> imageRepository.saveImage(uri) }
}
}
@@ -0,0 +1,50 @@
/*
* 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.model.Attachment
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.repository.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class SendMessage @Inject constructor(
private val conversationRepo: ConversationRepository,
private val messageRepo: MessageRepository
) : Interactor<SendMessage.Params>() {
data class Params(val subId: Int, 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.subId, params.threadId, params.addresses.first(), params.body)
} else {
messageRepo.sendMms(params.subId, params.threadId, params.addresses, params.body, params.attachments)
}
}
// If this was the first message sent in the conversation, the conversation might not exist yet
.doOnNext { conversationRepo.getOrCreateConversation(params.threadId) }
.doOnNext { conversationRepo.updateConversations(params.threadId) }
.doOnNext { conversationRepo.markUnarchived(params.threadId) }
}
}
@@ -0,0 +1,41 @@
/*
* 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 android.net.Uri
import com.moez.QKSMS.extensions.mapNotNull
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.repository.SyncRepository
import io.reactivex.Flowable
import javax.inject.Inject
class SyncMessage @Inject constructor(
private val conversationRepo: ConversationRepository,
private val syncManager: SyncRepository,
private val updateBadge: UpdateBadge
) : Interactor<Uri>() {
override fun buildObservable(params: Uri): Flowable<*> {
return Flowable.just(params)
.mapNotNull { uri -> syncManager.syncMessage(uri) }
.doOnNext { message -> conversationRepo.updateConversations(message.threadId) }
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
}
@@ -0,0 +1,41 @@
/*
* 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.SyncRepository
import io.reactivex.Flowable
import timber.log.Timber
import java.util.concurrent.TimeUnit
import javax.inject.Inject
class SyncMessages @Inject constructor(
private val syncManager: SyncRepository,
private val updateBadge: UpdateBadge
) : Interactor<Unit>() {
override fun buildObservable(params: Unit): Flowable<*> {
return Flowable.just(System.currentTimeMillis())
.doOnNext { syncManager.syncMessages() }
.map { startTime -> System.currentTimeMillis() - startTime }
.map { elapsed -> TimeUnit.MILLISECONDS.toSeconds(elapsed) }
.doOnNext { seconds -> Timber.v("Completed sync in $seconds seconds") }
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
}
@@ -0,0 +1,38 @@
/*
* 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.WidgetManager
import com.moez.QKSMS.repository.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class UpdateBadge @Inject constructor(
private val messageRepo: MessageRepository,
private val widgetManager: WidgetManager
) : Interactor<Unit>() {
override fun buildObservable(params: Unit): Flowable<*> {
return Flowable.just(params)
.map { messageRepo.getUnreadCount() }
.map { count -> count.toInt() }
.doOnNext { count -> widgetManager.updateUnreadCount(count) }
}
}
@@ -0,0 +1,27 @@
/*
* 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
interface AnalyticsManager {
fun track(event: String, vararg properties: Pair<String, String>)
fun setUserProperty(key: String, value: Any)
}
@@ -0,0 +1,30 @@
/*
* 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 io.reactivex.Single
interface ExternalBlockingManager {
/**
* Return a Single<Boolean> which emits whether or not the given [address] should be blocked
*/
fun shouldBlock(address: String): Single<Boolean>
}
@@ -0,0 +1,36 @@
/*
* 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
/**
* Helper class for generating incrementing ids for messages
*/
interface KeyManager {
/**
* Should be called when a new sync is being started
*/
fun reset()
/**
* Returns a valid ID that can be used to store a new message
*/
fun newId(): Long
}
@@ -0,0 +1,31 @@
/*
* 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
interface NotificationManager {
fun update(threadId: Long)
fun notifyFailed(threadId: Long)
fun createNotificationChannel(threadId: Long)
fun buildNotificationChannelId(threadId: Long): String
}
@@ -0,0 +1,35 @@
/*
* 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
interface PermissionManager {
fun isDefaultSms(): Boolean
fun hasSmsAndContacts(): Boolean
fun hasSms(): Boolean
fun hasContacts(): Boolean
fun hasPhone(): Boolean
fun hasStorage(): Boolean
}
@@ -0,0 +1,36 @@
/*
* 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 io.reactivex.Observable
interface RatingManager {
val shouldShowRating: Observable<Boolean>
/**
* Whether or not we should show the rating UI should depend on the number of sessions
*/
fun addSession()
fun rate()
fun dismiss()
}
@@ -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
interface WidgetManager {
companion object {
const val ACTION_NOTIFY_DATASET_CHANGED = "com.moez.QKSMS.intent.action.ACTION_NOTIFY_DATASET_CHANGED"
}
fun updateUnreadCount(count: Int)
}
@@ -0,0 +1,28 @@
/*
* 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.mapper
import android.database.Cursor
import com.moez.QKSMS.model.Contact
interface CursorToContact : Mapper<Cursor, Contact> {
fun getContactsCursor(): Cursor?
}
@@ -0,0 +1,30 @@
/*
* 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.mapper
import android.database.Cursor
import com.moez.QKSMS.model.Conversation
interface CursorToConversation : Mapper<Cursor, Conversation> {
fun getConversationsCursor(lastSync: Long = 0): Cursor?
fun getConversationCursor(threadId: Long): Cursor?
}
@@ -0,0 +1,68 @@
/*
* 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.mapper
import android.database.Cursor
import android.provider.Telephony.*
import com.moez.QKSMS.model.Message
import timber.log.Timber
import java.util.*
interface CursorToMessage : Mapper<Pair<Cursor, CursorToMessage.MessageColumns>, Message> {
fun getMessagesCursor(): Cursor?
fun getMessageCursor(id: Long): Cursor?
class MessageColumns(private val cursor: Cursor) {
val msgType by lazy { getColumnIndex(MmsSms.TYPE_DISCRIMINATOR_COLUMN) }
val msgId by lazy { getColumnIndex(MmsSms._ID) }
val date by lazy { getColumnIndex(Mms.DATE) }
val dateSent by lazy { getColumnIndex(Mms.DATE_SENT) }
val read by lazy { getColumnIndex(Mms.READ) }
val threadId by lazy { getColumnIndex(Mms.THREAD_ID) }
val locked by lazy { getColumnIndex(Mms.LOCKED) }
val subId by lazy { getColumnIndex(Mms.SUBSCRIPTION_ID) }
val smsAddress by lazy { getColumnIndex(Sms.ADDRESS) }
val smsBody by lazy { getColumnIndex(Sms.BODY) }
val smsSeen by lazy { getColumnIndex(Sms.SEEN) }
val smsType by lazy { getColumnIndex(Sms.TYPE) }
val smsStatus by lazy { getColumnIndex(Sms.STATUS) }
val smsErrorCode by lazy { getColumnIndex(Sms.ERROR_CODE) }
val mmsSubject by lazy { getColumnIndex(Mms.SUBJECT) }
val mmsSubjectCharset by lazy { getColumnIndex(Mms.SUBJECT_CHARSET) }
val mmsSeen by lazy { getColumnIndex(Mms.SEEN) }
val mmsMessageType by lazy { getColumnIndex(Mms.MESSAGE_TYPE) }
val mmsMessageBox by lazy { getColumnIndex(Mms.MESSAGE_BOX) }
val mmsDeliveryReport by lazy { getColumnIndex(Mms.DELIVERY_REPORT) }
val mmsReadReport by lazy { getColumnIndex(Mms.READ_REPORT) }
val mmsErrorType by lazy { getColumnIndex(MmsSms.PendingMessages.ERROR_TYPE) }
val mmsStatus by lazy { getColumnIndex(Mms.STATUS) }
private fun getColumnIndex(columnsName: String) = try {
cursor.getColumnIndexOrThrow(columnsName)
} catch (e: Exception) {
Timber.e("Couldn't find column \'$columnsName\' in ${Arrays.toString(cursor.columnNames)}")
-1
}
}
}
@@ -0,0 +1,28 @@
/*
* 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.mapper
import android.database.Cursor
import com.moez.QKSMS.model.MmsPart
interface CursorToPart : Mapper<Cursor, MmsPart> {
fun getPartsCursor(messageId: Long): Cursor?
}
@@ -0,0 +1,30 @@
/*
* 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.mapper
import android.database.Cursor
import com.moez.QKSMS.model.Recipient
interface CursorToRecipient : Mapper<Cursor, Recipient> {
fun getRecipientCursor(): Cursor?
fun getRecipientCursor(id: Long): Cursor?
}
@@ -0,0 +1,25 @@
/*
* 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.mapper
interface Mapper<in From, out To> {
fun map(from: From): To
}
@@ -0,0 +1,44 @@
/*
* 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.model
import android.content.Context
import android.net.Uri
import android.os.Build
import android.support.v13.view.inputmethod.InputContentInfoCompat
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("image/gif")
} else {
context.contentResolver.getType(uri) == "image/gif"
}
}
}
@@ -0,0 +1,30 @@
/*
* 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.model
import io.realm.RealmList
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
open class Contact(
@PrimaryKey var lookupKey: String = "",
var numbers: RealmList<PhoneNumber> = RealmList(),
var name: String = "",
var lastUpdate: Long = 0
) : RealmObject()
@@ -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.model
import io.realm.RealmList
import io.realm.RealmObject
import io.realm.annotations.Index
import io.realm.annotations.PrimaryKey
open class Conversation(
@PrimaryKey var id: Long = 0,
@Index var archived: Boolean = false,
@Index var blocked: Boolean = false,
var recipients: RealmList<Recipient> = RealmList(),
var count: Int = 0,
var date: Long = 0,
var snippet: String = "",
var read: Boolean = true,
var me: Boolean = false,
var draft: String = "",
// For group chats, the user is allowed to set a custom title for the conversation
var name: String = ""
) : RealmObject() {
fun getTitle(): String {
return name.takeIf { it.isNotBlank() } ?: recipients.joinToString { recipient -> recipient.getDisplayName() }
}
}
@@ -0,0 +1,162 @@
/*
* 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.model
import android.content.ContentUris
import android.net.Uri
import android.provider.Telephony.*
import io.realm.RealmList
import io.realm.RealmObject
import io.realm.annotations.Index
import io.realm.annotations.PrimaryKey
open class Message : RealmObject() {
enum class AttachmentType { TEXT, IMAGE, VIDEO, AUDIO, SLIDESHOW, NOT_LOADED }
@PrimaryKey var id: Long = 0
@Index var threadId: Long = 0
// The MMS-SMS content provider returns messages where duplicate ids can exist. This is because
// SMS and MMS are stored in separate tables. We can't use these ids as our realm message id
// since it's our primary key for the single message object, so we'll store the original id in
// case we need to access the original message item in the content provider
var contentId: Long = 0
var address: String = ""
var boxId: Int = 0
var type: String = ""
var date: Long = 0
var dateSent: Long = 0
var seen: Boolean = false
var read: Boolean = false
var locked: Boolean = false
var subId: Int = -1
// SMS only
var body: String = ""
var errorCode: Int = 0
var deliveryStatus: Int = Sms.STATUS_NONE
// MMS only
var attachmentTypeString: String = AttachmentType.NOT_LOADED.toString()
var attachmentType: AttachmentType
get() = AttachmentType.valueOf(attachmentTypeString)
set(value) {
attachmentTypeString = value.toString()
}
var mmsDeliveryStatusString: String = ""
var readReportString: String = ""
var errorType: Int = 0
var messageSize: Int = 0
var messageType: Int = 0
var mmsStatus: Int = 0
var subject: String = ""
var textContentType: String = ""
var parts: RealmList<MmsPart> = RealmList()
fun getUri(): Uri {
val baseUri = if (isMms()) Mms.CONTENT_URI else Sms.CONTENT_URI
return ContentUris.withAppendedId(baseUri, contentId)
}
fun isMms(): Boolean = type == "mms"
fun isSms(): Boolean = type == "sms"
fun isMe(): Boolean {
val isIncomingMms = isMms() && (boxId == Mms.MESSAGE_BOX_INBOX || boxId == Mms.MESSAGE_BOX_ALL)
val isIncomingSms = isSms() && (boxId == Sms.MESSAGE_TYPE_INBOX || boxId == Sms.MESSAGE_TYPE_ALL)
return !(isIncomingMms || isIncomingSms)
}
fun isOutgoingMessage(): Boolean {
val isOutgoingMms = isMms() && boxId == Mms.MESSAGE_BOX_OUTBOX
val isOutgoingSms = isSms() && (boxId == Sms.MESSAGE_TYPE_FAILED
|| boxId == Sms.MESSAGE_TYPE_OUTBOX
|| boxId == Sms.MESSAGE_TYPE_QUEUED)
return isOutgoingMms || isOutgoingSms
}
/**
* Returns the text that should be copied to the clipboard
*/
fun getText(): String {
return when {
isSms() -> body
else -> parts
.mapNotNull { it.text }
.joinToString("\n") { text -> text }
}
}
/**
* Returns the text that should be displayed when a preview of the message
* needs to be displayed, such as in the conversation view or in a notification
*
* TODO: Don't return a hardcoded string
*/
fun getSummary(): String {
return when {
isSms() -> body
else -> parts
.mapNotNull { it.text }
.joinToString("\n") { text -> text }
.takeIf { it.isNotEmpty() }
?: getCleansedSubject().takeIf { it.isNotEmpty() }
?: "An MMS message"
}
}
/**
* Cleanses the subject in case it's useless, so that the UI doesn't have to show it
*/
fun getCleansedSubject(): String {
val uselessSubjects = listOf("no subject", "NoSubject", "<not present>")
return if (uselessSubjects.contains(subject)) "" else subject
}
fun isSending(): Boolean {
return !isFailedMessage() && isOutgoingMessage()
}
fun isDelivered(): Boolean {
val isDeliveredMms = false // TODO
val isDeliveredSms = deliveryStatus == Sms.STATUS_COMPLETE
return isDeliveredMms || isDeliveredSms
}
fun isFailedMessage(): Boolean {
val isFailedMms = isMms() && (errorType >= MmsSms.ERR_TYPE_GENERIC_PERMANENT || boxId == Mms.MESSAGE_BOX_FAILED)
val isFailedSms = isSms() && boxId == Sms.MESSAGE_TYPE_FAILED
return isFailedMms || isFailedSms
}
fun compareSender(other: Message): Boolean = when {
isMe() && other.isMe() -> subId == other.subId
!isMe() && !other.isMe() -> subId == other.subId && address == other.address
else -> false
}
}
@@ -0,0 +1,38 @@
/*
* 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.model
import androidx.net.toUri
import io.realm.RealmObject
import io.realm.RealmResults
import io.realm.annotations.LinkingObjects
import io.realm.annotations.PrimaryKey
open class MmsPart : RealmObject() {
@PrimaryKey var id: Long = 0
var type: String = ""
var text: String? = null
@LinkingObjects("parts")
val messages: RealmResults<Message>? = null
fun getUri() = "content://mms/part/$id".toUri()
}
@@ -0,0 +1,26 @@
/*
* 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.model
import io.realm.RealmObject
open class PhoneNumber(
var address: String = "",
var type: String = ""
) : RealmObject()
@@ -0,0 +1,40 @@
/*
* 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.model
import android.telephony.PhoneNumberUtils
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import java.util.*
open class Recipient(
@PrimaryKey var id: Long = 0,
var address: String = "",
var contact: Contact? = null,
var lastUpdate: Long = 0
) : RealmObject() {
/**
* Return a string that can be displayed to represent the name of this contact
*/
fun getDisplayName(): String = contact?.name?.takeIf { it.isNotBlank() }
?: PhoneNumberUtils.formatNumber(address, Locale.getDefault().country)
?: address
}
@@ -0,0 +1,3 @@
package com.moez.QKSMS.model
data class SearchResult(val query: String, val conversation: Conversation, val messages: Int)
@@ -0,0 +1,27 @@
/*
* 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.model
import io.realm.RealmObject
open class SyncLog : RealmObject() {
var date: Long = System.currentTimeMillis()
}
@@ -0,0 +1,32 @@
/*
* 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.repository
import android.net.Uri
import com.moez.QKSMS.model.Contact
import io.reactivex.Flowable
import io.reactivex.Single
interface ContactRepository {
fun findContactUri(address: String): Single<Uri>
fun getUnmanagedContacts(): Flowable<List<Contact>>
}
@@ -0,0 +1,50 @@
package com.moez.QKSMS.repository
import com.moez.QKSMS.model.Conversation
import com.moez.QKSMS.model.SearchResult
import io.realm.RealmResults
interface ConversationRepository {
fun getConversations(archived: Boolean = false): RealmResults<Conversation>
fun getConversationsSnapshot(): List<Conversation>
fun setConversationName(id: Long, name: String)
fun searchConversations(query: String): List<SearchResult>
fun getBlockedConversations(): RealmResults<Conversation>
fun getConversationAsync(threadId: Long): Conversation
fun getConversation(threadId: Long): Conversation?
fun getThreadId(recipient: String): Long?
fun getThreadId(recipients: Collection<String>): Long?
fun getOrCreateConversation(threadId: Long): Conversation?
fun getOrCreateConversation(address: String): Conversation?
fun getOrCreateConversation(addresses: List<String>): Conversation?
fun saveDraft(threadId: Long, draft: String)
/**
* Updates message-related fields in the conversation, like the date and snippet
*/
fun updateConversations(vararg threadIds: Long)
fun markArchived(vararg threadIds: Long)
fun markUnarchived(vararg threadIds: Long)
fun markBlocked(vararg threadIds: Long)
fun markUnblocked(vararg threadIds: Long)
fun deleteConversations(vararg threadIds: Long)
}
@@ -0,0 +1,12 @@
package com.moez.QKSMS.repository
import android.graphics.Bitmap
import android.net.Uri
interface ImageRepository {
fun loadImage(uri: Uri): Bitmap?
fun saveImage(uri: Uri)
}
@@ -0,0 +1,96 @@
/*
* 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.repository
import com.moez.QKSMS.model.Attachment
import com.moez.QKSMS.model.Message
import com.moez.QKSMS.model.MmsPart
import io.realm.RealmResults
interface MessageRepository {
fun getMessages(threadId: Long, query: String = ""): RealmResults<Message>
fun getMessage(id: Long): Message?
fun getMessageForPart(id: Long): Message?
fun getUnreadCount(): Long
fun getPart(id: Long): MmsPart?
fun getPartsForConversation(threadId: Long): RealmResults<MmsPart>
/**
* Retrieves the list of messages which should be shown in the notification
* for a given conversation
*/
fun getUnreadUnseenMessages(threadId: Long): RealmResults<Message>
/**
* Retrieves the list of messages which should be shown in the quickreply popup
* for a given conversation
*/
fun getUnreadMessages(threadId: Long): RealmResults<Message>
fun markAllSeen()
fun markSeen(threadId: Long)
fun markRead(vararg threadIds: Long)
fun markUnread(vararg threadIds: Long)
/**
* Persists the SMS and attempts to send it
*/
fun sendSmsAndPersist(subId: Int, threadId: Long, address: String, body: String)
/**
* Attempts to send the SMS message. This can be called if the message has already been persisted
*/
fun sendSms(message: Message)
fun sendMms(subId: Int, threadId: Long, addresses: List<String>, body: String, attachments: List<Attachment>)
/**
* Attempts to cancel sending the message with the given id
*/
fun cancelDelayedSms(id: Long)
fun insertSentSms(subId: Int, threadId: Long, address: String, body: String, date: Long): Message
fun insertReceivedSms(subId: Int, address: String, body: String, sentTime: Long): Message
/**
* Marks the message as sending, in case we need to retry sending it
*/
fun markSending(id: Long)
fun markSent(id: Long)
fun markFailed(id: Long, resultCode: Int)
fun markDelivered(id: Long)
fun markDeliveryFailed(id: Long, resultCode: Int)
fun deleteMessages(vararg messageIds: Long)
}
@@ -0,0 +1,40 @@
/*
* 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.repository
import android.net.Uri
import com.moez.QKSMS.model.Message
import io.reactivex.Observable
interface SyncRepository {
sealed class SyncProgress {
class Idle : SyncProgress()
class Running(progress: Float) : SyncProgress()
}
val syncProgress: Observable<SyncProgress>
fun syncMessages()
fun syncMessage(uri: Uri): Message?
fun syncContacts()
}