diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Config.kt index d3e75cf3..76b047a7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Config.kt @@ -60,6 +60,10 @@ class Config(context: Context) : BaseConfig(context) { get() = prefs.getBoolean(SHOW_DIALPAD_LETTERS, true) set(showDialpadLetters) = prefs.edit().putBoolean(SHOW_DIALPAD_LETTERS, showDialpadLetters).apply() + var wasLocalAccountInitialized: Boolean + get() = prefs.getBoolean(WAS_LOCAL_ACCOUNT_INITIALIZED, false) + set(wasLocalAccountInitialized) = prefs.edit().putBoolean(WAS_LOCAL_ACCOUNT_INITIALIZED, wasLocalAccountInitialized).apply() + var lastExportPath: String get() = prefs.getString(LAST_EXPORT_PATH, "")!! set(lastExportPath) = prefs.edit().putString(LAST_EXPORT_PATH, lastExportPath).apply() diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Constants.kt index 0b0bd411..81da9bd4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Constants.kt @@ -23,6 +23,7 @@ const val SHOW_DIALPAD_BUTTON = "show_dialpad_button" const val SHOW_DIALPAD_LETTERS = "show_dialpad_letters" const val SPEED_DIAL = "speed_dial" const val LAST_EXPORT_PATH = "last_export_path" +const val WAS_LOCAL_ACCOUNT_INITIALIZED = "was_local_account_initialized" const val CONTACT_ID = "contact_id" const val SMT_PRIVATE = "smt_private" // used at the contact source of local contacts hidden from other apps diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt index 65d9c798..7706146f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt @@ -746,6 +746,11 @@ class ContactsHelper(val context: Context) { return sources } + if (!context.config.wasLocalAccountInitialized) { + initializeLocalPhoneAccount() + context.config.wasLocalAccountInitialized = true + } + val accounts = AccountManager.get(context).accounts accounts.forEach { if (ContentResolver.getIsSyncable(it, AUTHORITY) == 1) { @@ -766,6 +771,24 @@ class ContactsHelper(val context: Context) { return sources } + // make sure the local Phone contact source is initialized and available + // https://stackoverflow.com/a/6096508/1967672 + private fun initializeLocalPhoneAccount() { + try { + val operations = ArrayList() + ContentProviderOperation.newInsert(RawContacts.CONTENT_URI).apply { + withValue(RawContacts.ACCOUNT_NAME, null) + withValue(RawContacts.ACCOUNT_TYPE, null) + operations.add(build()) + } + + val results = context.contentResolver.applyBatch(AUTHORITY, operations) + val rawContactUri = results.firstOrNull()?.uri ?: return + context.contentResolver.delete(rawContactUri, null, null) + } catch (ignored: Exception) { + } + } + private fun getContactSourceType(accountName: String) = getDeviceContactSources().firstOrNull { it.name == accountName }?.type ?: "" private fun getContactProjection() = arrayOf(