do not store the visible contact sources, store the ignored ones instead

This commit is contained in:
tibbi 2018-05-01 10:39:35 +02:00
parent 22a4cca1fe
commit f9e3854099
11 changed files with 49 additions and 30 deletions

View File

@ -18,6 +18,7 @@ import com.simplemobiletools.contacts.dialogs.ChangeSortingDialog
import com.simplemobiletools.contacts.dialogs.FilterContactSourcesDialog import com.simplemobiletools.contacts.dialogs.FilterContactSourcesDialog
import com.simplemobiletools.contacts.extensions.config import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.extensions.getContactPublicUri import com.simplemobiletools.contacts.extensions.getContactPublicUri
import com.simplemobiletools.contacts.extensions.getVisibleContactSources
import com.simplemobiletools.contacts.helpers.ContactsHelper import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.helpers.SMT_PRIVATE import com.simplemobiletools.contacts.helpers.SMT_PRIVATE
import com.simplemobiletools.contacts.models.Contact import com.simplemobiletools.contacts.models.Contact
@ -98,7 +99,7 @@ class SelectContactActivity : SimpleActivity() {
} }
} as ArrayList<Contact> } as ArrayList<Contact>
val contactSources = config.displayContactSources val contactSources = getVisibleContactSources()
contacts = contacts.filter { contactSources.contains(it.source) } as ArrayList<Contact> contacts = contacts.filter { contactSources.contains(it.source) } as ArrayList<Contact>
Contact.sorting = config.sorting Contact.sorting = config.sorting

View File

@ -14,7 +14,7 @@ import com.simplemobiletools.contacts.models.ContactSource
import kotlinx.android.synthetic.main.item_filter_contact_source.view.* import kotlinx.android.synthetic.main.item_filter_contact_source.view.*
import java.util.* import java.util.*
class FilterContactSourcesAdapter(val activity: SimpleActivity, private val contactSources: List<ContactSource>, private val displayContactSources: Set<String>) : class FilterContactSourcesAdapter(val activity: SimpleActivity, private val contactSources: List<ContactSource>, private val displayContactSources: ArrayList<String>) :
RecyclerView.Adapter<FilterContactSourcesAdapter.ViewHolder>() { RecyclerView.Adapter<FilterContactSourcesAdapter.ViewHolder>() {
private val itemViews = SparseArray<View>() private val itemViews = SparseArray<View>()
private val selectedPositions = HashSet<Int>() private val selectedPositions = HashSet<Int>()

View File

@ -6,7 +6,7 @@ import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.adapters.FilterContactSourcesAdapter 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.ContactsHelper
import com.simplemobiletools.contacts.helpers.SMT_PRIVATE import com.simplemobiletools.contacts.helpers.SMT_PRIVATE
import com.simplemobiletools.contacts.models.ContactSource import com.simplemobiletools.contacts.models.ContactSource
@ -25,7 +25,7 @@ class ExportContactsDialog(val activity: SimpleActivity, val path: String, priva
ContactsHelper(activity).getContactSources { ContactsHelper(activity).getContactSources {
it.mapTo(contactSources, { it.copy() }) it.mapTo(contactSources, { it.copy() })
activity.runOnUiThread { activity.runOnUiThread {
export_contacts_list.adapter = FilterContactSourcesAdapter(activity, it, activity.config.displayContactSources) export_contacts_list.adapter = FilterContactSourcesAdapter(activity, it, activity.getVisibleContactSources())
} }
} }
} }

View File

@ -6,6 +6,7 @@ import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.adapters.FilterContactSourcesAdapter import com.simplemobiletools.contacts.adapters.FilterContactSourcesAdapter
import com.simplemobiletools.contacts.extensions.config import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.extensions.getVisibleContactSources
import com.simplemobiletools.contacts.helpers.ContactsHelper import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.helpers.SMT_PRIVATE import com.simplemobiletools.contacts.helpers.SMT_PRIVATE
import com.simplemobiletools.contacts.models.ContactSource import com.simplemobiletools.contacts.models.ContactSource
@ -24,7 +25,7 @@ class FilterContactSourcesDialog(val activity: SimpleActivity, private val callb
} }
it.mapTo(contactSources, { it.copy() }) it.mapTo(contactSources, { it.copy() })
val selectedSources = activity.config.displayContactSources val selectedSources = activity.getVisibleContactSources()
activity.runOnUiThread { activity.runOnUiThread {
view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, it, selectedSources) view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, it, selectedSources)
@ -40,13 +41,20 @@ class FilterContactSourcesDialog(val activity: SimpleActivity, private val callb
private fun confirmEventTypes() { private fun confirmEventTypes() {
val selectedIndexes = (view.filter_contact_sources_list.adapter as FilterContactSourcesAdapter).getSelectedItemsSet() val selectedIndexes = (view.filter_contact_sources_list.adapter as FilterContactSourcesAdapter).getSelectedItemsSet()
val selectedContactSources = HashSet<String>() val ignoredIndexes = ArrayList<Int>()
selectedIndexes.forEach { for (i in 0 until contactSources.size) {
selectedContactSources.add(if (contactSources[it].type == SMT_PRIVATE) SMT_PRIVATE else contactSources[it].name) if (!selectedIndexes.contains(i)) {
ignoredIndexes.add(i)
}
} }
if (activity.config.displayContactSources != selectedContactSources) { val ignoredContactSources = HashSet<String>()
activity.config.displayContactSources = selectedContactSources 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() callback()
} }
dialog?.dismiss() dialog?.dismiss()

View File

@ -7,6 +7,7 @@ import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.adapters.SelectContactsAdapter import com.simplemobiletools.contacts.adapters.SelectContactsAdapter
import com.simplemobiletools.contacts.extensions.config import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.extensions.getVisibleContactSources
import com.simplemobiletools.contacts.models.Contact import com.simplemobiletools.contacts.models.Contact
import kotlinx.android.synthetic.main.layout_select_contact.view.* import kotlinx.android.synthetic.main.layout_select_contact.view.*
@ -18,7 +19,7 @@ class SelectContactsDialog(val activity: SimpleActivity, initialContacts: ArrayL
init { init {
var allContacts = initialContacts var allContacts = initialContacts
if (selectContacts == null) { if (selectContacts == null) {
val contactSources = activity.config.displayContactSources val contactSources = activity.getVisibleContactSources()
allContacts = allContacts.filter { contactSources.contains(it.source) } as ArrayList<Contact> allContacts = allContacts.filter { contactSources.contains(it.source) } as ArrayList<Contact>
initiallySelectedContacts = allContacts.filter { it.starred == 1 } as ArrayList<Contact> initiallySelectedContacts = allContacts.filter { it.starred == 1 } as ArrayList<Contact>

View File

@ -1,5 +1,6 @@
package com.simplemobiletools.contacts.extensions package com.simplemobiletools.contacts.extensions
import android.app.Activity
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.provider.ContactsContract 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.SMT_PRIVATE
import com.simplemobiletools.contacts.helpers.VcfExporter import com.simplemobiletools.contacts.helpers.VcfExporter
import com.simplemobiletools.contacts.models.Contact import com.simplemobiletools.contacts.models.Contact
import com.simplemobiletools.contacts.models.ContactSource
import java.io.File import java.io.File
fun SimpleActivity.startCallIntent(recipient: String) { fun SimpleActivity.startCallIntent(recipient: String) {
@ -181,3 +183,11 @@ fun BaseSimpleActivity.getContactPublicUri(contact: Contact): Uri {
val lookupKey = ContactsHelper(this).getContactLookupKey(contact.id.toString()) val lookupKey = ContactsHelper(this).getContactLookupKey(contact.id.toString())
return Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey) return Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey)
} }
fun Activity.getVisibleContactSources(): ArrayList<String> {
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<String>
sourceNames.removeAll(config.ignoredContactSources)
return sourceNames
}

View File

@ -15,10 +15,7 @@ import com.simplemobiletools.contacts.activities.MainActivity
import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.adapters.ContactsAdapter import com.simplemobiletools.contacts.adapters.ContactsAdapter
import com.simplemobiletools.contacts.adapters.GroupsAdapter import com.simplemobiletools.contacts.adapters.GroupsAdapter
import com.simplemobiletools.contacts.extensions.config import com.simplemobiletools.contacts.extensions.*
import com.simplemobiletools.contacts.extensions.editContact
import com.simplemobiletools.contacts.extensions.tryStartCall
import com.simplemobiletools.contacts.extensions.viewContact
import com.simplemobiletools.contacts.helpers.* import com.simplemobiletools.contacts.helpers.*
import com.simplemobiletools.contacts.models.Contact import com.simplemobiletools.contacts.models.Contact
import com.simplemobiletools.contacts.models.Group import com.simplemobiletools.contacts.models.Group
@ -103,7 +100,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
this is GroupsFragment -> contacts this is GroupsFragment -> contacts
this is FavoritesFragment -> contacts.filter { it.starred == 1 } as ArrayList<Contact> this is FavoritesFragment -> contacts.filter { it.starred == 1 } as ArrayList<Contact>
else -> { else -> {
val contactSources = config.displayContactSources val contactSources = activity!!.getVisibleContactSources()
contacts.filter { contactSources.contains(it.source) } as ArrayList<Contact> contacts.filter { contactSources.contains(it.source) } as ArrayList<Contact>
} }
} }

View File

@ -8,9 +8,9 @@ class Config(context: Context) : BaseConfig(context) {
fun newInstance(context: Context) = Config(context) fun newInstance(context: Context) = Config(context)
} }
var displayContactSources: HashSet<String> var ignoredContactSources: HashSet<String>
get() = prefs.getStringSet(DISPLAY_CONTACT_SOURCES, hashSetOf("-1")) as HashSet get() = prefs.getStringSet(IGNORED_CONTACT_SOURCES, hashSetOf("")) as HashSet
set(displayContactSources) = prefs.edit().remove(DISPLAY_CONTACT_SOURCES).putStringSet(DISPLAY_CONTACT_SOURCES, displayContactSources).apply() set(ignoreContactSources) = prefs.edit().remove(IGNORED_CONTACT_SOURCES).putStringSet(IGNORED_CONTACT_SOURCES, ignoreContactSources).apply()
var showContactThumbnails: Boolean var showContactThumbnails: Boolean
get() = prefs.getBoolean(SHOW_CONTACT_THUMBNAILS, true) get() = prefs.getBoolean(SHOW_CONTACT_THUMBNAILS, true)

View File

@ -5,7 +5,7 @@ import android.provider.ContactsContract.CommonDataKinds
// shared prefs // shared prefs
const val SHOW_CONTACT_THUMBNAILS = "show_contact_thumbnails" const val SHOW_CONTACT_THUMBNAILS = "show_contact_thumbnails"
const val SHOW_PHONE_NUMBERS = "show_phone_numbers" 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 START_NAME_WITH_SURNAME = "start_name_with_surname"
const val LAST_USED_CONTACT_SOURCE = "last_used_contact_source" const val LAST_USED_CONTACT_SOURCE = "last_used_contact_source"
const val LOCAL_ACCOUNT_NAME = "local_account_name" const val LOCAL_ACCOUNT_NAME = "local_account_name"

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.contacts.helpers package com.simplemobiletools.contacts.helpers
import android.accounts.AccountManager import android.accounts.AccountManager
import android.app.Activity
import android.content.* import android.content.*
import android.database.Cursor import android.database.Cursor
import android.graphics.Bitmap import android.graphics.Bitmap
@ -11,7 +12,6 @@ import android.provider.ContactsContract.CommonDataKinds.Note
import android.provider.MediaStore import android.provider.MediaStore
import android.text.TextUtils import android.text.TextUtils
import android.util.SparseArray import android.util.SparseArray
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME
import com.simplemobiletools.commons.helpers.SORT_BY_MIDDLE_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.models.*
import com.simplemobiletools.contacts.overloads.times import com.simplemobiletools.contacts.overloads.times
class ContactsHelper(val activity: BaseSimpleActivity) { class ContactsHelper(val activity: Activity) {
private val BATCH_SIZE = 100 private val BATCH_SIZE = 100
private var displayContactSources = HashSet<String>() private var displayContactSources = ArrayList<String>()
fun getContacts(callback: (ArrayList<Contact>) -> Unit) { fun getContacts(callback: (ArrayList<Contact>) -> Unit) {
Thread { Thread {
val contacts = SparseArray<Contact>() val contacts = SparseArray<Contact>()
displayContactSources = activity.config.displayContactSources displayContactSources = activity.getVisibleContactSources()
getDeviceContacts(contacts) getDeviceContacts(contacts)
if (displayContactSources.contains(SMT_PRIVATE)) { if (displayContactSources.contains(SMT_PRIVATE)) {
@ -72,6 +72,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
val selectionArgs = getSourcesSelectionArgs(CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) val selectionArgs = getSourcesSelectionArgs(CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
val sortOrder = getSortString() val sortOrder = getSortString()
val names = HashSet<String>()
var cursor: Cursor? = null var cursor: Cursor? = null
try { try {
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, sortOrder) cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, sortOrder)
@ -89,6 +90,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
val addresses = ArrayList<Address>() val addresses = ArrayList<Address>()
val events = ArrayList<Event>() val events = ArrayList<Event>()
val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) ?: "" val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) ?: ""
names.add(accountName)
val starred = cursor.getIntValue(CommonDataKinds.StructuredName.STARRED) val starred = cursor.getIntValue(CommonDataKinds.StructuredName.STARRED)
val contactId = cursor.getIntValue(ContactsContract.Data.CONTACT_ID) val contactId = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
val thumbnailUri = cursor.getStringValue(CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI) ?: "" val thumbnailUri = cursor.getStringValue(CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI) ?: ""
@ -647,7 +649,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
return ArrayList(sources) return ArrayList(sources)
} }
private fun getDeviceContactSources(): LinkedHashSet<ContactSource> { fun getDeviceContactSources(): LinkedHashSet<ContactSource> {
val sources = LinkedHashSet<ContactSource>() val sources = LinkedHashSet<ContactSource>()
if (!activity.hasContactPermissions()) { if (!activity.hasContactPermissions()) {
return sources return sources

View File

@ -1,5 +1,6 @@
package com.simplemobiletools.contacts.helpers package com.simplemobiletools.contacts.helpers
import android.app.Activity
import android.content.ContentValues import android.content.ContentValues
import android.content.Context import android.content.Context
import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteDatabase
@ -11,7 +12,6 @@ import android.provider.MediaStore
import android.text.TextUtils import android.text.TextUtils
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.getBlobValue import com.simplemobiletools.commons.extensions.getBlobValue
import com.simplemobiletools.commons.extensions.getIntValue import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.getLongValue 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<String>? = null): ArrayList<Contact> { fun getContacts(activity: Activity, selection: String? = null, selectionArgs: Array<String>? = null): ArrayList<Contact> {
val storedGroups = ContactsHelper(activity).getStoredGroups() val storedGroups = ContactsHelper(activity).getStoredGroups()
val contacts = ArrayList<Contact>() val contacts = ArrayList<Contact>()
val projection = arrayOf(COL_ID, COL_PREFIX, COL_FIRST_NAME, COL_MIDDLE_NAME, COL_SURNAME, COL_SUFFIX, COL_PHONE_NUMBERS, COL_EMAILS, 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 return contacts
} }
fun getContactWithId(activity: BaseSimpleActivity, id: Int): Contact? { fun getContactWithId(activity: Activity, id: Int): Contact? {
val selection = "$COL_ID = ?" val selection = "$COL_ID = ?"
val selectionArgs = arrayOf(id.toString()) val selectionArgs = arrayOf(id.toString())
return getContacts(activity, selection, selectionArgs).firstOrNull() return getContacts(activity, selection, selectionArgs).firstOrNull()