store the local account type in shared prefs

This commit is contained in:
tibbi 2018-02-01 23:54:36 +01:00
parent 93e7d73d91
commit 6f82aadcb9
4 changed files with 53 additions and 0 deletions

View File

@ -51,6 +51,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
setContentView(R.layout.activity_main)
appLaunched()
setupTabColors()
storeLocalAccountType()
handlePermission(PERMISSION_READ_CONTACTS) {
if (it) {
@ -214,6 +215,30 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
}
}
private fun storeLocalAccountType() {
if (config.localAccountType == "-1") {
// some manufacturer contact account types from https://stackoverflow.com/a/44802016/1967672
val localAccountTypes = arrayListOf("vnd.sec.contact.phone",
"com.htc.android.pcsc",
"com.sonyericsson.localcontacts",
"com.lge.sync",
"com.lge.phone",
"vnd.tmobileus.contact.phone",
"com.android.huawei.phone",
"Local Phone Account")
ContactsHelper(this).getContactTypes {
var localAccountType = ""
it.forEach {
if (localAccountTypes.contains(it)) {
localAccountType = it
}
}
config.localAccountType = localAccountType
}
}
}
private fun getOtherViewPagerItem(used: Int) = if (used == 1) 0 else 1
private fun initFragments() {

View File

@ -43,4 +43,8 @@ class Config(context: Context) : BaseConfig(context) {
var lastUsedViewPagerPage: Int
get() = prefs.getInt(LAST_USED_VIEW_PAGER_PAGE, 0)
set(lastUsedViewPagerPage) = prefs.edit().putInt(LAST_USED_VIEW_PAGER_PAGE, lastUsedViewPagerPage).apply()
var localAccountType: String
get() = prefs.getString(LOCAL_ACCOUNT_TYPE, "-1")
set(localAccountType) = prefs.edit().putString(LOCAL_ACCOUNT_TYPE, localAccountType).apply()
}

View File

@ -10,6 +10,7 @@ const val DISPLAY_CONTACT_SOURCES = "display_contact_sources"
const val START_NAME_WITH_SURNAME = "start_name_with_surname"
const val LAST_USED_CONTACT_SOURCE = "last_used_contact_source"
const val LAST_USED_VIEW_PAGER_PAGE = "last_used_view_pager_page"
const val LOCAL_ACCOUNT_TYPE = "local_account_type"
const val CONTACT_ID = "contact_id"

View File

@ -252,6 +252,29 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
}.start()
}
fun getContactTypes(callback: (ArrayList<String>) -> Unit) {
val accounts = HashSet<String>()
Thread {
val uri = ContactsContract.RawContacts.CONTENT_URI
val projection = arrayOf(ContactsContract.RawContacts.ACCOUNT_TYPE)
var cursor: Cursor? = null
try {
cursor = activity.contentResolver.query(uri, projection, null, null, null)
if (cursor?.moveToFirst() == true) {
do {
val type = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_TYPE) ?: continue
accounts.add(type)
} while (cursor.moveToNext())
}
} finally {
cursor?.close()
}
callback(ArrayList(accounts))
}.start()
}
private fun getContactSourceType(accountName: String): String {
if (accountName.isEmpty()) {
return ""