create new background threads only when necessary

This commit is contained in:
tibbi 2019-08-14 20:45:47 +02:00
parent b52872cf1a
commit 007e264f69
13 changed files with 58 additions and 50 deletions

View File

@ -21,6 +21,7 @@ import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.dialogs.CustomLabelDialog
@ -148,7 +149,7 @@ class EditContactActivity : ContactActivity() {
}
if (contactId != 0) {
Thread {
ensureBackgroundThread {
contact = ContactsHelper(this).getContactWithId(contactId, intent.getBooleanExtra(IS_PRIVATE, false))
if (contact == null) {
toast(R.string.unknown_error_occurred)
@ -158,7 +159,7 @@ class EditContactActivity : ContactActivity() {
gotContact()
}
}
}.start()
}
} else {
gotContact()
}
@ -875,7 +876,7 @@ class EditContactActivity : ContactActivity() {
val jobPosition = contact_organization_job_position.value
organization = Organization(company, jobPosition)
Thread {
ensureBackgroundThread {
config.lastUsedContactSource = source
when {
id == 0 -> insertNewContact(false)
@ -885,7 +886,7 @@ class EditContactActivity : ContactActivity() {
updateContact(photoUpdateStatus)
}
}
}.start()
}
}
}

View File

@ -7,6 +7,7 @@ import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import com.simplemobiletools.commons.extensions.underlineText
import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.adapters.ContactsAdapter
import com.simplemobiletools.contacts.pro.dialogs.SelectContactsDialog
@ -70,11 +71,11 @@ class GroupContactsActivity : SimpleActivity(), RemoveFromGroupListener, Refresh
private fun fabClicked() {
SelectContactsDialog(this, allContacts, groupContacts) { addedContacts, removedContacts ->
Thread {
ensureBackgroundThread {
addContactsToGroup(addedContacts, group.id!!)
removeContactsFromGroup(removedContacts, group.id!!)
refreshContacts()
}.start()
}
}
}
@ -124,11 +125,11 @@ class GroupContactsActivity : SimpleActivity(), RemoveFromGroupListener, Refresh
}
override fun removeFromGroup(contacts: ArrayList<Contact>) {
Thread {
ensureBackgroundThread {
removeContactsFromGroup(contacts, group.id!!)
if (groupContacts.size == contacts.size) {
refreshContacts()
}
}.start()
}
}
}

View File

@ -8,6 +8,7 @@ import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import com.simplemobiletools.commons.extensions.underlineText
import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.adapters.ManageBlockedNumbersAdapter
@ -63,7 +64,7 @@ class ManageBlockedNumbersActivity : SimpleActivity(), RefreshRecyclerViewListen
}
private fun updateBlockedNumbers() {
Thread {
ensureBackgroundThread {
val blockedNumbers = getBlockedNumbers()
runOnUiThread {
ManageBlockedNumbersAdapter(this, blockedNumbers, this, manage_blocked_numbers_list) {
@ -75,7 +76,7 @@ class ManageBlockedNumbersActivity : SimpleActivity(), RefreshRecyclerViewListen
manage_blocked_numbers_placeholder.beVisibleIf(blockedNumbers.isEmpty())
manage_blocked_numbers_placeholder_2.beVisibleIf(blockedNumbers.isEmpty())
}
}.start()
}
}
private fun addOrEditBlockedNumber(currentNumber: BlockedNumber? = null) {

View File

@ -11,6 +11,7 @@ import android.widget.RelativeLayout
import com.bumptech.glide.Glide
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.dialogs.CallConfirmationDialog
import com.simplemobiletools.contacts.pro.extensions.*
@ -46,18 +47,18 @@ class ViewContactActivity : ContactActivity() {
if (isViewIntent) {
handlePermission(PERMISSION_READ_CONTACTS) {
if (it) {
Thread {
ensureBackgroundThread {
initContact()
}.start()
}
} else {
toast(R.string.no_contacts_permission)
finish()
}
}
} else {
Thread {
ensureBackgroundThread {
initContact()
}.start()
}
}
}

View File

@ -20,6 +20,7 @@ import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
import com.simplemobiletools.commons.extensions.highlightTextPart
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.commons.views.FastScroller
import com.simplemobiletools.commons.views.MyRecyclerView
@ -164,9 +165,9 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: ArrayList<Cont
val positions = getSelectedItemPositions()
contactItems.removeAll(contactsToRemove)
Thread {
ensureBackgroundThread {
ContactsHelper(activity).deleteContacts(contactsToRemove)
}.start()
}
if (contactItems.isEmpty()) {
refreshListener?.refreshContacts(ALL_TABS_MASK)
@ -219,17 +220,17 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: ArrayList<Cont
RadioGroupDialog(activity, radioItems, 0) {
if (it as Int == NEW_GROUP_ID) {
CreateNewGroupDialog(activity) {
Thread {
ensureBackgroundThread {
activity.addContactsToGroup(selectedContacts, it.id!!.toLong())
refreshListener?.refreshContacts(GROUPS_TAB_MASK)
}.start()
}
finishActMode()
}
} else {
Thread {
ensureBackgroundThread {
activity.addContactsToGroup(selectedContacts, it.toLong())
refreshListener?.refreshContacts(GROUPS_TAB_MASK)
}.start()
}
finishActMode()
}
}

View File

@ -9,6 +9,7 @@ import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import com.simplemobiletools.commons.extensions.highlightTextPart
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.views.FastScroller
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.contacts.pro.R
@ -102,9 +103,9 @@ class GroupsAdapter(activity: SimpleActivity, var groups: ArrayList<Group>, val
private fun askConfirmDelete() {
ConfirmationDialog(activity) {
Thread {
ensureBackgroundThread {
deleteGroups()
}.start()
}
}
}

View File

@ -8,6 +8,7 @@ import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.showKeyboard
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.value
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.extensions.getPrivateContactSource
@ -60,7 +61,7 @@ class CreateNewGroupDialog(val activity: BaseSimpleActivity, val callback: (newG
}
private fun createGroupUnder(name: String, contactSource: ContactSource, dialog: AlertDialog) {
Thread {
ensureBackgroundThread {
val newGroup = ContactsHelper(activity).createNewGroup(name, contactSource.name, contactSource.type)
activity.runOnUiThread {
if (newGroup != null) {
@ -68,6 +69,6 @@ class CreateNewGroupDialog(val activity: BaseSimpleActivity, val callback: (newG
}
dialog.dismiss()
}
}.start()
}
}
}

View File

@ -3,6 +3,7 @@ package com.simplemobiletools.contacts.pro.dialogs
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.activities.SimpleActivity
import com.simplemobiletools.contacts.pro.adapters.FilterContactSourcesAdapter
@ -49,12 +50,12 @@ class ExportContactsDialog(val activity: SimpleActivity, val path: String, priva
return@setOnClickListener
}
Thread {
ensureBackgroundThread {
val selectedSources = (view.export_contacts_list.adapter as FilterContactSourcesAdapter).getSelectedContactSources()
val ignoredSources = contactSources.filter { !selectedSources.contains(it) }.map { it.getFullIdentifier() }.toHashSet()
callback(file, ignoredSources)
dismiss()
}.start()
}
}
else -> activity.toast(R.string.invalid_name)
}

View File

@ -4,6 +4,7 @@ import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.activities.SimpleActivity
import com.simplemobiletools.contacts.pro.extensions.config
@ -41,11 +42,11 @@ class ImportContactsDialog(val activity: SimpleActivity, val path: String, priva
activity.setupDialogStuff(view, this, R.string.import_contacts) {
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
activity.toast(R.string.importing)
Thread {
ensureBackgroundThread {
val result = VcfImporter(activity).importContacts(path, targetContactSource)
handleParseResult(result)
dismiss()
}.start()
}
}
}
}

View File

@ -3,6 +3,7 @@ package com.simplemobiletools.contacts.pro.dialogs
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.extensions.groupsDB
import com.simplemobiletools.contacts.pro.helpers.ContactsHelper
@ -35,7 +36,7 @@ class RenameGroupDialog(val activity: BaseSimpleActivity, val group: Group, val
}
group.title = newTitle
Thread {
ensureBackgroundThread {
if (group.isPrivateSecretGroup()) {
activity.groupsDB.insertOrUpdate(group)
} else {
@ -45,7 +46,7 @@ class RenameGroupDialog(val activity: BaseSimpleActivity, val group: Group, val
callback()
dismiss()
}
}.start()
}
}
}
}

View File

@ -3,6 +3,7 @@ package com.simplemobiletools.contacts.pro.dialogs
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.extensions.baseConfig
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.activities.SimpleActivity
import com.simplemobiletools.contacts.pro.adapters.SelectContactsAdapter
@ -45,13 +46,13 @@ class SelectContactsDialog(val activity: SimpleActivity, initialContacts: ArrayL
}
private fun dialogConfirmed() {
Thread {
ensureBackgroundThread {
val adapter = view?.select_contact_list?.adapter as? SelectContactsAdapter
val selectedContacts = adapter?.getSelectedItemsSet()?.toList() ?: ArrayList()
val newlySelectedContacts = selectedContacts.filter { !initiallySelectedContacts.contains(it) } as ArrayList
val unselectedContacts = initiallySelectedContacts.filter { !selectedContacts.contains(it) } as ArrayList
callback(newlySelectedContacts, unselectedContacts)
}.start()
}
}
}

View File

@ -15,10 +15,7 @@ import android.provider.ContactsContract
import android.telecom.TelecomManager
import androidx.core.content.FileProvider
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS
import com.simplemobiletools.commons.helpers.isMarshmallowPlus
import com.simplemobiletools.commons.helpers.isNougatPlus
import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.contacts.pro.BuildConfig
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.activities.EditContactActivity
@ -199,7 +196,7 @@ fun Context.getPublicContactSource(source: String, callback: (String) -> Unit) {
when (source) {
SMT_PRIVATE -> callback(getString(R.string.phone_storage_hidden))
else -> {
Thread {
ensureBackgroundThread {
ContactsHelper(this).getContactSources {
var newSource = source
for (contactSource in it) {
@ -212,7 +209,7 @@ fun Context.getPublicContactSource(source: String, callback: (String) -> Unit) {
callback(newSource)
}
}
}.start()
}
}
}
}

View File

@ -29,7 +29,7 @@ class ContactsHelper(val context: Context) {
private var displayContactSources = ArrayList<String>()
fun getContacts(ignoredContactSources: HashSet<String> = HashSet(), callback: (ArrayList<Contact>) -> Unit) {
Thread {
ensureBackgroundThread {
val contacts = SparseArray<Contact>()
displayContactSources = context.getVisibleContactSources()
if (ignoredContactSources.isNotEmpty()) {
@ -93,7 +93,7 @@ class ContactsHelper(val context: Context) {
Handler(Looper.getMainLooper()).post {
callback(resultContacts)
}
}.start()
}
}
private fun getContentResolverAccounts(): HashSet<ContactSource> {
@ -667,12 +667,12 @@ class ContactsHelper(val context: Context) {
}
fun getStoredGroups(callback: (ArrayList<Group>) -> Unit) {
Thread {
ensureBackgroundThread {
val groups = getStoredGroupsSync()
Handler(Looper.getMainLooper()).post {
callback(groups)
}
}.start()
}
}
fun getStoredGroupsSync(): ArrayList<Group> {
@ -838,9 +838,9 @@ class ContactsHelper(val context: Context) {
}
fun getContactSources(callback: (ArrayList<ContactSource>) -> Unit) {
Thread {
ensureBackgroundThread {
callback(getContactSourcesSync())
}.start()
}
}
private fun getContactSourcesSync(): ArrayList<ContactSource> {
@ -1476,21 +1476,21 @@ class ContactsHelper(val context: Context) {
}
fun addFavorites(contacts: ArrayList<Contact>) {
Thread {
ensureBackgroundThread {
toggleLocalFavorites(contacts, true)
if (context.hasContactPermissions()) {
toggleFavorites(contacts, true)
}
}.start()
}
}
fun removeFavorites(contacts: ArrayList<Contact>) {
Thread {
ensureBackgroundThread {
toggleLocalFavorites(contacts, false)
if (context.hasContactPermissions()) {
toggleFavorites(contacts, false)
}
}.start()
}
}
private fun toggleFavorites(contacts: ArrayList<Contact>, addToFavorites: Boolean) {
@ -1520,13 +1520,13 @@ class ContactsHelper(val context: Context) {
}
fun deleteContact(contact: Contact) {
Thread {
ensureBackgroundThread {
if (contact.isPrivate()) {
context.contactsDB.deleteContactId(contact.id)
} else {
deleteContacts(arrayListOf(contact))
}
}.start()
}
}
fun deleteContacts(contacts: ArrayList<Contact>) {