fix #370 and fix #491, make sure the local Phone account is initialized

This commit is contained in:
tibbi 2020-04-16 12:27:42 +02:00
parent ab1fc2c7a9
commit 0ad5874c5d
3 changed files with 28 additions and 0 deletions

View File

@ -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()

View File

@ -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

View File

@ -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>()
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(