mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-18 20:08:53 +01:00
Avoid duplicate contacts, update recipient contact on sync
This commit is contained in:
@@ -26,6 +26,8 @@ import common.util.extensions.asFlowable
|
||||
import common.util.extensions.insertOrUpdate
|
||||
import common.util.extensions.map
|
||||
import common.util.extensions.mapWhile
|
||||
import common.util.filter.ContactFilter
|
||||
import data.mapper.CursorToContact
|
||||
import data.mapper.CursorToConversation
|
||||
import data.mapper.CursorToMessage
|
||||
import data.mapper.CursorToRecipient
|
||||
@@ -42,7 +44,9 @@ class SyncManager @Inject constructor(
|
||||
private val messageRepo: MessageRepository,
|
||||
private val cursorToConversation: CursorToConversation,
|
||||
private val cursorToMessage: CursorToMessage,
|
||||
private val cursorToRecipient: CursorToRecipient) {
|
||||
private val cursorToRecipient: CursorToRecipient,
|
||||
private val cursorToContact: CursorToContact,
|
||||
private val contactFilter: ContactFilter) {
|
||||
|
||||
sealed class Status {
|
||||
class Idle : Status()
|
||||
@@ -53,7 +57,7 @@ class SyncManager @Inject constructor(
|
||||
private var status: Status = Status.Idle()
|
||||
|
||||
// TODO: This needs to be substantially faster
|
||||
fun performSync(fullSync: Boolean = false) {
|
||||
fun syncMessages(fullSync: Boolean = false) {
|
||||
|
||||
// If the sync is already running, don't try to do another one
|
||||
if (status is Status.Running) return
|
||||
@@ -100,10 +104,11 @@ class SyncManager @Inject constructor(
|
||||
realm.insertOrUpdate(recipients)
|
||||
recipientCursor.close()
|
||||
|
||||
|
||||
realm.commitTransaction()
|
||||
realm.close()
|
||||
|
||||
syncContacts()
|
||||
|
||||
status = Status.Idle()
|
||||
}
|
||||
|
||||
@@ -146,4 +151,35 @@ class SyncManager @Inject constructor(
|
||||
.doOnNext { message -> message.insertOrUpdate() }
|
||||
}
|
||||
|
||||
fun syncContacts() {
|
||||
// Load all the contacts
|
||||
var contacts = context.contentResolver.query(CursorToContact.URI, CursorToContact.PROJECTION, null, null, null)
|
||||
.map { cursor -> cursorToContact.map(cursor) }
|
||||
.groupBy { contact -> contact.lookupKey }
|
||||
.map { contacts ->
|
||||
val allNumbers = contacts.value.map { it.numbers }.flatten()
|
||||
contacts.value.first().apply {
|
||||
numbers.clear()
|
||||
numbers.addAll(allNumbers)
|
||||
}
|
||||
}
|
||||
|
||||
val realm = Realm.getDefaultInstance()
|
||||
val recipients = realm.where(Recipient::class.java).findAll()
|
||||
|
||||
realm.executeTransaction {
|
||||
realm.delete(Contact::class.java)
|
||||
|
||||
contacts = realm.copyToRealm(contacts)
|
||||
|
||||
// Update all the recipients with the new contacts
|
||||
val updatedRecipients = recipients.map { recipient ->
|
||||
recipient.apply { contact = contacts.firstOrNull { contactFilter.filter(it, recipient.address) } }
|
||||
}
|
||||
|
||||
realm.insertOrUpdate(updatedRecipients)
|
||||
}
|
||||
realm.close()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,8 +24,10 @@ import javax.inject.Inject
|
||||
class PhoneNumberFilter @Inject constructor() : Filter<String>() {
|
||||
|
||||
override fun filter(item: String, query: CharSequence): Boolean {
|
||||
return query.all { PhoneNumberUtils.isReallyDialable(it) } &&
|
||||
PhoneNumberUtils.stripSeparators(item).contains(PhoneNumberUtils.stripSeparators(query.toString()))
|
||||
val allCharactersDialable = query.all { PhoneNumberUtils.isReallyDialable(it) }
|
||||
|
||||
return allCharactersDialable && (PhoneNumberUtils.compare(item, query.toString()) ||
|
||||
PhoneNumberUtils.stripSeparators(item).contains(PhoneNumberUtils.stripSeparators(query.toString())))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,11 +20,11 @@ package data.mapper
|
||||
|
||||
import android.database.Cursor
|
||||
import android.net.Uri
|
||||
import android.telephony.PhoneNumberUtils
|
||||
import data.model.Recipient
|
||||
import data.repository.ContactRepository
|
||||
import javax.inject.Inject
|
||||
|
||||
class CursorToRecipient @Inject constructor(private val contactRepository: ContactRepository) : Mapper<Cursor, Recipient> {
|
||||
class CursorToRecipient @Inject constructor() : Mapper<Cursor, Recipient> {
|
||||
|
||||
companion object {
|
||||
val URI = Uri.parse("content://mms-sms/canonical-addresses")
|
||||
@@ -35,8 +35,7 @@ class CursorToRecipient @Inject constructor(private val contactRepository: Conta
|
||||
|
||||
override fun map(from: Cursor) = Recipient().apply {
|
||||
id = from.getLong(COLUMN_ID)
|
||||
address = from.getString(COLUMN_ADDRESS)
|
||||
contact = contactRepository.getContactBlocking(address) // TODO do this lazily for faster syncs
|
||||
address = PhoneNumberUtils.stripSeparators(from.getString(COLUMN_ADDRESS))
|
||||
lastUpdate = System.currentTimeMillis()
|
||||
}
|
||||
|
||||
|
||||
@@ -20,17 +20,14 @@ package data.repository
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.provider.BaseColumns
|
||||
import android.provider.ContactsContract
|
||||
import common.util.extensions.asFlowable
|
||||
import data.model.Contact
|
||||
import data.model.PhoneNumber
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.Single
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import io.realm.Realm
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -59,38 +56,4 @@ class ContactRepository @Inject constructor(val context: Context) {
|
||||
.observeOn(Schedulers.io())
|
||||
}
|
||||
|
||||
fun getContactBlocking(address: String): Contact? {
|
||||
if (address.isEmpty()) return null
|
||||
|
||||
val realm = Realm.getDefaultInstance()
|
||||
var contact = realm.where(Contact::class.java)
|
||||
.equalTo("numbers.address", address)
|
||||
.findFirst()
|
||||
|
||||
if (contact == null) {
|
||||
val contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address))
|
||||
val projection = arrayOf(BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.LOOKUP_KEY)
|
||||
try {
|
||||
context.contentResolver.query(contactUri, projection, null, null, null).use { contactCursor ->
|
||||
if (contactCursor.moveToFirst()) {
|
||||
contact = Contact().apply {
|
||||
numbers.add(PhoneNumber().apply { this.address = address })
|
||||
name = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)).orEmpty()
|
||||
lookupKey = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.PhoneLookup.LOOKUP_KEY)).orEmpty()
|
||||
lastUpdate = System.currentTimeMillis()
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Timber.w(e)
|
||||
}
|
||||
} else {
|
||||
contact = realm.copyFromRealm(contact)
|
||||
}
|
||||
|
||||
realm.close()
|
||||
|
||||
return contact
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,45 +18,20 @@
|
||||
*/
|
||||
package interactor
|
||||
|
||||
import android.content.Context
|
||||
import common.util.extensions.asFlowable
|
||||
import common.util.extensions.insertOrUpdate
|
||||
import data.mapper.CursorToContact
|
||||
import data.model.Contact
|
||||
import common.util.SyncManager
|
||||
import io.reactivex.Flowable
|
||||
import timber.log.Timber
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Inject
|
||||
|
||||
open class ContactSync @Inject constructor(
|
||||
private val context: Context,
|
||||
private val cursorToContact: CursorToContact)
|
||||
: Interactor<Unit, List<Contact>>() {
|
||||
open class ContactSync @Inject constructor(private val syncManager: SyncManager) : Interactor<Unit, Long>() {
|
||||
|
||||
override fun buildObservable(params: Unit): Flowable<List<Contact>> {
|
||||
val contentResolver = context.contentResolver
|
||||
|
||||
Timber.v("Starting contact sync")
|
||||
val startTime = System.currentTimeMillis()
|
||||
|
||||
return contentResolver.query(CursorToContact.URI, CursorToContact.PROJECTION, null, null, null).asFlowable()
|
||||
.map { cursor -> cursorToContact.map(cursor) }
|
||||
.groupBy { contact -> contact.lookupKey }
|
||||
.flatMap { group -> group.toList().toFlowable() }
|
||||
.map { contacts ->
|
||||
val allNumbers = contacts.map { it.numbers }.flatten()
|
||||
contacts.first().apply {
|
||||
numbers.clear()
|
||||
numbers.addAll(allNumbers)
|
||||
}
|
||||
}
|
||||
.toList().toFlowable()
|
||||
.doOnNext { contacts ->
|
||||
contacts.insertOrUpdate()
|
||||
|
||||
val duration = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime)
|
||||
Timber.v("Synced contacts in $duration seconds")
|
||||
}
|
||||
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") }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -33,7 +33,7 @@ class FullSync @Inject constructor(
|
||||
override fun buildObservable(params: Unit): Flowable<Long> {
|
||||
return Flowable.just(System.currentTimeMillis())
|
||||
.doOnNext { keys.reset() }
|
||||
.doOnNext { syncManager.performSync(true) }
|
||||
.doOnNext { syncManager.syncMessages(true) }
|
||||
.map { startTime -> System.currentTimeMillis() - startTime }
|
||||
.map { elapsed -> TimeUnit.MILLISECONDS.toSeconds(elapsed) }
|
||||
.doOnNext { seconds -> Timber.v("Completed sync in $seconds seconds") }
|
||||
|
||||
@@ -33,7 +33,7 @@ open class PartialSync @Inject constructor(
|
||||
override fun buildObservable(params: Unit): Flowable<Long> {
|
||||
return Flowable.just(System.currentTimeMillis())
|
||||
.skipWhile { !permissions.hasSmsAndContacts() }
|
||||
.doOnNext { syncManager.performSync() }
|
||||
.doOnNext { syncManager.syncMessages() }
|
||||
.map { startTime -> System.currentTimeMillis() - startTime }
|
||||
.map { elapsed -> TimeUnit.MILLISECONDS.toSeconds(elapsed) }
|
||||
.doOnNext { seconds -> Timber.v("Completed sync in $seconds seconds") }
|
||||
|
||||
@@ -81,15 +81,15 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
||||
|
||||
newState { it.copy(page = Inbox(data = conversations)) }
|
||||
|
||||
val isDefaultSms = Telephony.Sms.getDefaultSmsPackage(context) != context.packageName
|
||||
val isNotDefaultSms = Telephony.Sms.getDefaultSmsPackage(context) != context.packageName
|
||||
val hasSmsPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED
|
||||
val hasContactPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED
|
||||
|
||||
if (isDefaultSms) {
|
||||
if (isNotDefaultSms) {
|
||||
partialSync.execute(Unit)
|
||||
}
|
||||
|
||||
if (isDefaultSms || !hasSmsPermission || !hasContactPermission) {
|
||||
if (isNotDefaultSms || !hasSmsPermission || !hasContactPermission) {
|
||||
navigator.showSetupActivity()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user