Catch StaleDataException in ContactLoader

This commit is contained in:
Sylvain Berfini 2022-04-12 15:31:02 +02:00
parent ae04a06e42
commit 4d6f614df8

View file

@ -21,6 +21,7 @@ package org.linphone.contact
import android.content.ContentUris import android.content.ContentUris
import android.database.Cursor import android.database.Cursor
import android.database.StaleDataException
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.ContactsContract import android.provider.ContactsContract
@ -31,6 +32,7 @@ import androidx.loader.content.CursorLoader
import androidx.loader.content.Loader import androidx.loader.content.Loader
import java.lang.Exception import java.lang.Exception
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.LinphoneApplication.Companion.coreContext
@ -79,6 +81,7 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
val friends = HashMap<String, Friend>() val friends = HashMap<String, Friend>()
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
try {
while (!cursor.isClosed && cursor.moveToNext()) { while (!cursor.isClosed && cursor.moveToNext()) {
try { try {
val id: String = val id: String =
@ -87,10 +90,14 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Data.DISPLAY_NAME_PRIMARY)) cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Data.DISPLAY_NAME_PRIMARY))
val mime: String? = val mime: String? =
cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Data.MIMETYPE)) cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Data.MIMETYPE))
val data1: String? = cursor.getString(cursor.getColumnIndexOrThrow("data1")) val data1: String? =
val data2: String? = cursor.getString(cursor.getColumnIndexOrThrow("data2")) cursor.getString(cursor.getColumnIndexOrThrow("data1"))
val data3: String? = cursor.getString(cursor.getColumnIndexOrThrow("data3")) val data2: String? =
val data4: String? = cursor.getString(cursor.getColumnIndexOrThrow("data4")) cursor.getString(cursor.getColumnIndexOrThrow("data2"))
val data3: String? =
cursor.getString(cursor.getColumnIndexOrThrow("data3"))
val data4: String? =
cursor.getString(cursor.getColumnIndexOrThrow("data4"))
val starred = val starred =
cursor.getInt(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.STARRED)) == 1 cursor.getInt(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.STARRED)) == 1
val lookupKey = val lookupKey =
@ -114,7 +121,8 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
when (mime) { when (mime) {
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE -> { ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE -> {
val typeLabel = ContactsContract.CommonDataKinds.Phone.getTypeLabel( val typeLabel =
ContactsContract.CommonDataKinds.Phone.getTypeLabel(
loader.context.resources, loader.context.resources,
data2?.toInt() ?: 0, data2?.toInt() ?: 0,
data3 data3
@ -201,6 +209,10 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
Log.i("[Contacts Loader] Friends added & subscription updated") Log.i("[Contacts Loader] Friends added & subscription updated")
coreContext.contactsManager.fetchFinished() coreContext.contactsManager.fetchFinished()
} }
} catch (sde: StaleDataException) {
Log.e("[Contacts Loader] State Data Exception: $sde")
cancel()
}
} }
} }
} }