From f9e38540996c4da9cf15a3372ed2b562425b2172 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 1 May 2018 10:39:35 +0200 Subject: [PATCH] do not store the visible contact sources, store the ignored ones instead --- .../activities/SelectContactActivity.kt | 3 ++- .../adapters/FilterContactSourcesAdapter.kt | 2 +- .../contacts/dialogs/ExportContactsDialog.kt | 4 ++-- .../dialogs/FilterContactSourcesDialog.kt | 24 ++++++++++++------- .../contacts/dialogs/SelectContactsDialog.kt | 3 ++- .../contacts/extensions/Activity.kt | 10 ++++++++ .../contacts/fragments/MyViewPagerFragment.kt | 7 ++---- .../contacts/helpers/Config.kt | 6 ++--- .../contacts/helpers/Constants.kt | 2 +- .../contacts/helpers/ContactsHelper.kt | 12 ++++++---- .../contacts/helpers/DBHelper.kt | 6 ++--- 11 files changed, 49 insertions(+), 30 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/SelectContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/SelectContactActivity.kt index bab426b8..6c1544c9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/SelectContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/SelectContactActivity.kt @@ -18,6 +18,7 @@ import com.simplemobiletools.contacts.dialogs.ChangeSortingDialog import com.simplemobiletools.contacts.dialogs.FilterContactSourcesDialog import com.simplemobiletools.contacts.extensions.config import com.simplemobiletools.contacts.extensions.getContactPublicUri +import com.simplemobiletools.contacts.extensions.getVisibleContactSources import com.simplemobiletools.contacts.helpers.ContactsHelper import com.simplemobiletools.contacts.helpers.SMT_PRIVATE import com.simplemobiletools.contacts.models.Contact @@ -98,7 +99,7 @@ class SelectContactActivity : SimpleActivity() { } } as ArrayList - val contactSources = config.displayContactSources + val contactSources = getVisibleContactSources() contacts = contacts.filter { contactSources.contains(it.source) } as ArrayList Contact.sorting = config.sorting diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/FilterContactSourcesAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/FilterContactSourcesAdapter.kt index b1c6ab53..a316d0be 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/FilterContactSourcesAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/FilterContactSourcesAdapter.kt @@ -14,7 +14,7 @@ import com.simplemobiletools.contacts.models.ContactSource import kotlinx.android.synthetic.main.item_filter_contact_source.view.* import java.util.* -class FilterContactSourcesAdapter(val activity: SimpleActivity, private val contactSources: List, private val displayContactSources: Set) : +class FilterContactSourcesAdapter(val activity: SimpleActivity, private val contactSources: List, private val displayContactSources: ArrayList) : RecyclerView.Adapter() { private val itemViews = SparseArray() private val selectedPositions = HashSet() diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/ExportContactsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/ExportContactsDialog.kt index 7aedd1af..a2b5e8ea 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/ExportContactsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/ExportContactsDialog.kt @@ -6,7 +6,7 @@ import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.adapters.FilterContactSourcesAdapter -import com.simplemobiletools.contacts.extensions.config +import com.simplemobiletools.contacts.extensions.getVisibleContactSources import com.simplemobiletools.contacts.helpers.ContactsHelper import com.simplemobiletools.contacts.helpers.SMT_PRIVATE import com.simplemobiletools.contacts.models.ContactSource @@ -25,7 +25,7 @@ class ExportContactsDialog(val activity: SimpleActivity, val path: String, priva ContactsHelper(activity).getContactSources { it.mapTo(contactSources, { it.copy() }) activity.runOnUiThread { - export_contacts_list.adapter = FilterContactSourcesAdapter(activity, it, activity.config.displayContactSources) + export_contacts_list.adapter = FilterContactSourcesAdapter(activity, it, activity.getVisibleContactSources()) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/FilterContactSourcesDialog.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/FilterContactSourcesDialog.kt index cc1a9d41..5be53220 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/FilterContactSourcesDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/FilterContactSourcesDialog.kt @@ -6,6 +6,7 @@ import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.adapters.FilterContactSourcesAdapter import com.simplemobiletools.contacts.extensions.config +import com.simplemobiletools.contacts.extensions.getVisibleContactSources import com.simplemobiletools.contacts.helpers.ContactsHelper import com.simplemobiletools.contacts.helpers.SMT_PRIVATE import com.simplemobiletools.contacts.models.ContactSource @@ -24,7 +25,7 @@ class FilterContactSourcesDialog(val activity: SimpleActivity, private val callb } it.mapTo(contactSources, { it.copy() }) - val selectedSources = activity.config.displayContactSources + val selectedSources = activity.getVisibleContactSources() activity.runOnUiThread { view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, it, selectedSources) @@ -32,21 +33,28 @@ class FilterContactSourcesDialog(val activity: SimpleActivity, private val callb .setPositiveButton(R.string.ok, { dialogInterface, i -> confirmEventTypes() }) .setNegativeButton(R.string.cancel, null) .create().apply { - activity.setupDialogStuff(view, this) - } + activity.setupDialogStuff(view, this) + } } } } private fun confirmEventTypes() { val selectedIndexes = (view.filter_contact_sources_list.adapter as FilterContactSourcesAdapter).getSelectedItemsSet() - val selectedContactSources = HashSet() - selectedIndexes.forEach { - selectedContactSources.add(if (contactSources[it].type == SMT_PRIVATE) SMT_PRIVATE else contactSources[it].name) + val ignoredIndexes = ArrayList() + for (i in 0 until contactSources.size) { + if (!selectedIndexes.contains(i)) { + ignoredIndexes.add(i) + } } - if (activity.config.displayContactSources != selectedContactSources) { - activity.config.displayContactSources = selectedContactSources + val ignoredContactSources = HashSet() + ignoredIndexes.forEach { + ignoredContactSources.add(if (contactSources[it].type == SMT_PRIVATE) SMT_PRIVATE else contactSources[it].name) + } + + if (activity.getVisibleContactSources() != ignoredContactSources) { + activity.config.ignoredContactSources = ignoredContactSources callback() } dialog?.dismiss() diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/SelectContactsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/SelectContactsDialog.kt index eed613d0..76660338 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/SelectContactsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/SelectContactsDialog.kt @@ -7,6 +7,7 @@ import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.adapters.SelectContactsAdapter import com.simplemobiletools.contacts.extensions.config +import com.simplemobiletools.contacts.extensions.getVisibleContactSources import com.simplemobiletools.contacts.models.Contact import kotlinx.android.synthetic.main.layout_select_contact.view.* @@ -18,7 +19,7 @@ class SelectContactsDialog(val activity: SimpleActivity, initialContacts: ArrayL init { var allContacts = initialContacts if (selectContacts == null) { - val contactSources = activity.config.displayContactSources + val contactSources = activity.getVisibleContactSources() allContacts = allContacts.filter { contactSources.contains(it.source) } as ArrayList initiallySelectedContacts = allContacts.filter { it.starred == 1 } as ArrayList diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Activity.kt index e7b4eabb..7044edc6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Activity.kt @@ -1,5 +1,6 @@ package com.simplemobiletools.contacts.extensions +import android.app.Activity import android.content.Intent import android.net.Uri import android.provider.ContactsContract @@ -17,6 +18,7 @@ import com.simplemobiletools.contacts.helpers.ContactsHelper import com.simplemobiletools.contacts.helpers.SMT_PRIVATE import com.simplemobiletools.contacts.helpers.VcfExporter import com.simplemobiletools.contacts.models.Contact +import com.simplemobiletools.contacts.models.ContactSource import java.io.File fun SimpleActivity.startCallIntent(recipient: String) { @@ -181,3 +183,11 @@ fun BaseSimpleActivity.getContactPublicUri(contact: Contact): Uri { val lookupKey = ContactsHelper(this).getContactLookupKey(contact.id.toString()) return Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey) } + +fun Activity.getVisibleContactSources(): ArrayList { + val sources = ContactsHelper(this).getDeviceContactSources() + sources.add(ContactSource(getString(R.string.phone_storage_hidden), SMT_PRIVATE)) + val sourceNames = ArrayList(sources).map { if (it.type == SMT_PRIVATE) SMT_PRIVATE else it.name }.toMutableList() as ArrayList + sourceNames.removeAll(config.ignoredContactSources) + return sourceNames +} diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/MyViewPagerFragment.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/MyViewPagerFragment.kt index ac4fbfcb..853b1292 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/MyViewPagerFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/MyViewPagerFragment.kt @@ -15,10 +15,7 @@ import com.simplemobiletools.contacts.activities.MainActivity import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.adapters.ContactsAdapter import com.simplemobiletools.contacts.adapters.GroupsAdapter -import com.simplemobiletools.contacts.extensions.config -import com.simplemobiletools.contacts.extensions.editContact -import com.simplemobiletools.contacts.extensions.tryStartCall -import com.simplemobiletools.contacts.extensions.viewContact +import com.simplemobiletools.contacts.extensions.* import com.simplemobiletools.contacts.helpers.* import com.simplemobiletools.contacts.models.Contact import com.simplemobiletools.contacts.models.Group @@ -103,7 +100,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) this is GroupsFragment -> contacts this is FavoritesFragment -> contacts.filter { it.starred == 1 } as ArrayList else -> { - val contactSources = config.displayContactSources + val contactSources = activity!!.getVisibleContactSources() contacts.filter { contactSources.contains(it.source) } as ArrayList } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Config.kt index ad8384ba..38ac340d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Config.kt @@ -8,9 +8,9 @@ class Config(context: Context) : BaseConfig(context) { fun newInstance(context: Context) = Config(context) } - var displayContactSources: HashSet - get() = prefs.getStringSet(DISPLAY_CONTACT_SOURCES, hashSetOf("-1")) as HashSet - set(displayContactSources) = prefs.edit().remove(DISPLAY_CONTACT_SOURCES).putStringSet(DISPLAY_CONTACT_SOURCES, displayContactSources).apply() + var ignoredContactSources: HashSet + get() = prefs.getStringSet(IGNORED_CONTACT_SOURCES, hashSetOf("")) as HashSet + set(ignoreContactSources) = prefs.edit().remove(IGNORED_CONTACT_SOURCES).putStringSet(IGNORED_CONTACT_SOURCES, ignoreContactSources).apply() var showContactThumbnails: Boolean get() = prefs.getBoolean(SHOW_CONTACT_THUMBNAILS, true) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt index d38e8294..5409d2dd 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt @@ -5,7 +5,7 @@ import android.provider.ContactsContract.CommonDataKinds // shared prefs const val SHOW_CONTACT_THUMBNAILS = "show_contact_thumbnails" const val SHOW_PHONE_NUMBERS = "show_phone_numbers" -const val DISPLAY_CONTACT_SOURCES = "display_contact_sources" +const val IGNORED_CONTACT_SOURCES = "ignored_contact_sources" const val START_NAME_WITH_SURNAME = "start_name_with_surname" const val LAST_USED_CONTACT_SOURCE = "last_used_contact_source" const val LOCAL_ACCOUNT_NAME = "local_account_name" diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt index 6b52f1bf..ced0aea9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -1,6 +1,7 @@ package com.simplemobiletools.contacts.helpers import android.accounts.AccountManager +import android.app.Activity import android.content.* import android.database.Cursor import android.graphics.Bitmap @@ -11,7 +12,6 @@ import android.provider.ContactsContract.CommonDataKinds.Note import android.provider.MediaStore import android.text.TextUtils import android.util.SparseArray -import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME import com.simplemobiletools.commons.helpers.SORT_BY_MIDDLE_NAME @@ -22,14 +22,14 @@ import com.simplemobiletools.contacts.extensions.* import com.simplemobiletools.contacts.models.* import com.simplemobiletools.contacts.overloads.times -class ContactsHelper(val activity: BaseSimpleActivity) { +class ContactsHelper(val activity: Activity) { private val BATCH_SIZE = 100 - private var displayContactSources = HashSet() + private var displayContactSources = ArrayList() fun getContacts(callback: (ArrayList) -> Unit) { Thread { val contacts = SparseArray() - displayContactSources = activity.config.displayContactSources + displayContactSources = activity.getVisibleContactSources() getDeviceContacts(contacts) if (displayContactSources.contains(SMT_PRIVATE)) { @@ -72,6 +72,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) { val selectionArgs = getSourcesSelectionArgs(CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) val sortOrder = getSortString() + val names = HashSet() var cursor: Cursor? = null try { cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, sortOrder) @@ -89,6 +90,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) { val addresses = ArrayList
() val events = ArrayList() val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) ?: "" + names.add(accountName) val starred = cursor.getIntValue(CommonDataKinds.StructuredName.STARRED) val contactId = cursor.getIntValue(ContactsContract.Data.CONTACT_ID) val thumbnailUri = cursor.getStringValue(CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI) ?: "" @@ -647,7 +649,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) { return ArrayList(sources) } - private fun getDeviceContactSources(): LinkedHashSet { + fun getDeviceContactSources(): LinkedHashSet { val sources = LinkedHashSet() if (!activity.hasContactPermissions()) { return sources diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt index e3729c19..63690935 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt @@ -1,5 +1,6 @@ package com.simplemobiletools.contacts.helpers +import android.app.Activity import android.content.ContentValues import android.content.Context import android.database.sqlite.SQLiteDatabase @@ -11,7 +12,6 @@ import android.provider.MediaStore import android.text.TextUtils import com.google.gson.Gson import com.google.gson.reflect.TypeToken -import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.extensions.getBlobValue import com.simplemobiletools.commons.extensions.getIntValue import com.simplemobiletools.commons.extensions.getLongValue @@ -249,7 +249,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont } } - fun getContacts(activity: BaseSimpleActivity, selection: String? = null, selectionArgs: Array? = null): ArrayList { + fun getContacts(activity: Activity, selection: String? = null, selectionArgs: Array? = null): ArrayList { val storedGroups = ContactsHelper(activity).getStoredGroups() val contacts = ArrayList() val projection = arrayOf(COL_ID, COL_PREFIX, COL_FIRST_NAME, COL_MIDDLE_NAME, COL_SURNAME, COL_SUFFIX, COL_PHONE_NUMBERS, COL_EMAILS, @@ -319,7 +319,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont return contacts } - fun getContactWithId(activity: BaseSimpleActivity, id: Int): Contact? { + fun getContactWithId(activity: Activity, id: Int): Contact? { val selection = "$COL_ID = ?" val selectionArgs = arrayOf(id.toString()) return getContacts(activity, selection, selectionArgs).firstOrNull()