fetch SimpleContact birthdays and anniversaries too, for the calendar

This commit is contained in:
tibbi 2020-10-27 19:45:26 +01:00
parent 082ab6d2c3
commit f9de4d69d5
6 changed files with 88 additions and 7 deletions

View file

@ -6,7 +6,7 @@ buildscript {
propMinSdkVersion = 21
propTargetSdkVersion = propCompileSdkVersion
propVersionCode = 1
propVersionName = '5.31.10'
propVersionName = '5.31.11'
kotlin_version = '1.4.10'
}

View file

@ -33,6 +33,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'androidx.documentfile:documentfile:1.0.1'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation 'joda-time:joda-time:2.10.1'
api 'androidx.appcompat:appcompat:1.2.0'
api 'com.github.ajalt.reprint:core:3.3.0@aar'

View file

@ -11,10 +11,15 @@ import android.text.Spannable
import android.text.SpannableString
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import android.widget.TextView
import com.bumptech.glide.signature.ObjectKey
import com.simplemobiletools.commons.helpers.*
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import java.io.File
import java.text.DateFormat
import java.text.Normalizer
import java.text.SimpleDateFormat
import java.util.*
import java.util.regex.Pattern
@ -226,6 +231,31 @@ fun String.highlightTextFromNumbers(textToHighlight: String, adjustedPrimaryColo
return spannableString
}
fun String.getDateTimeFromDateString(viewToUpdate: TextView? = null): DateTime {
val dateFormats = getDateFormats()
var date = DateTime()
for (format in dateFormats) {
try {
date = DateTime.parse(this, DateTimeFormat.forPattern(format))
val formatter = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault())
var localPattern = (formatter as SimpleDateFormat).toLocalizedPattern()
val hasYear = format.contains("y")
if (!hasYear) {
localPattern = localPattern.replace("y", "").replace(",", "").trim()
date = date.withYear(DateTime().year)
}
val formattedString = date.toString(localPattern)
viewToUpdate?.text = formattedString
break
} catch (ignored: Exception) {
}
}
return date
}
fun String.getMimeType(): String {
val typesMap = HashMap<String, String>().apply {
put("323", "text/h323")

View file

@ -9,7 +9,7 @@ import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.getStringValue
import com.simplemobiletools.commons.models.SimpleContact
// used for sharing privately stored contacts in Simple Contacts with Simple Dialer and Simple SMS Messenger
// used for sharing privately stored contacts in Simple Contacts with Simple Dialer, Simple SMS Messenger and Simple Calendar Pro
class MyContactsContentProvider {
companion object {
private const val AUTHORITY = "com.simplemobiletools.commons.contactsprovider"
@ -21,6 +21,8 @@ class MyContactsContentProvider {
const val COL_NAME = "name"
const val COL_PHOTO_URI = "photo_uri"
const val COL_PHONE_NUMBERS = "phone_numbers"
const val COL_BIRTHDAYS = "birthdays"
const val COL_ANNIVERSARIES = "anniversaries"
fun getSimpleContacts(context: Context, cursor: Cursor?): ArrayList<SimpleContact> {
val contacts = ArrayList<SimpleContact>()
@ -38,10 +40,15 @@ class MyContactsContentProvider {
val name = cursor.getStringValue(COL_NAME)
val photoUri = cursor.getStringValue(COL_PHOTO_URI)
val phoneNumbersJson = cursor.getStringValue(COL_PHONE_NUMBERS)
val birthdaysJson = cursor.getStringValue(COL_BIRTHDAYS)
val anniversariesJson = cursor.getStringValue(COL_ANNIVERSARIES)
val token = object : TypeToken<ArrayList<String>>() {}.type
val phoneNumbers = Gson().fromJson<ArrayList<String>>(phoneNumbersJson, token) ?: ArrayList()
val contact = SimpleContact(rawId, contactId, name, photoUri, phoneNumbers)
val birthdays = Gson().fromJson<ArrayList<String>>(birthdaysJson, token) ?: ArrayList()
val anniversaries = Gson().fromJson<ArrayList<String>>(anniversariesJson, token) ?: ArrayList()
val contact = SimpleContact(rawId, contactId, name, photoUri, phoneNumbers, birthdays, anniversaries)
contacts.add(contact)
} while (cursor.moveToNext())
}

View file

@ -12,6 +12,7 @@ import android.provider.ContactsContract.*
import android.provider.ContactsContract.CommonDataKinds.Organization
import android.provider.ContactsContract.CommonDataKinds.StructuredName
import android.text.TextUtils
import android.util.SparseArray
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
@ -47,6 +48,20 @@ class SimpleContactsHelper(val context: Context) {
it.phoneNumbers.first().substring(startIndex)
}.distinctBy { it.rawId }.toMutableList() as ArrayList<SimpleContact>
val birthdays = getContactEvents(true)
var size = birthdays.size()
for (i in 0 until size) {
val key = birthdays.keyAt(i)
allContacts.firstOrNull { it.rawId == key }?.birthdays = birthdays.valueAt(i)
}
val anniversaries = getContactEvents(false)
size = anniversaries.size()
for (i in 0 until size) {
val key = anniversaries.keyAt(i)
allContacts.firstOrNull { it.rawId == key }?.anniversaries = anniversaries.valueAt(i)
}
allContacts.sort()
callback(allContacts)
}
@ -101,7 +116,7 @@ class SimpleContactsHelper(val context: Context) {
}
val fullName = TextUtils.join(" ", names)
val contact = SimpleContact(rawId, contactId, fullName, photoUri, ArrayList())
val contact = SimpleContact(rawId, contactId, fullName, photoUri, ArrayList(), ArrayList(), ArrayList())
contacts.add(contact)
}
}
@ -112,7 +127,7 @@ class SimpleContactsHelper(val context: Context) {
val jobTitle = cursor.getStringValue(Organization.TITLE) ?: ""
if (company.isNotEmpty() || jobTitle.isNotEmpty()) {
val fullName = "$company $jobTitle".trim()
val contact = SimpleContact(rawId, contactId, fullName, photoUri, ArrayList())
val contact = SimpleContact(rawId, contactId, fullName, photoUri, ArrayList(), ArrayList(), ArrayList())
contacts.add(contact)
}
}
@ -139,13 +154,40 @@ class SimpleContactsHelper(val context: Context) {
val rawId = cursor.getIntValue(Data.RAW_CONTACT_ID)
val contactId = cursor.getIntValue(Data.CONTACT_ID)
if (contacts.firstOrNull { it.rawId == rawId } == null) {
contacts.add(SimpleContact(rawId, contactId, "", "", ArrayList()))
val contact = SimpleContact(rawId, contactId, "", "", ArrayList(), ArrayList(), ArrayList())
contacts.add(contact)
}
contacts.firstOrNull { it.rawId == rawId }?.phoneNumbers?.add(phoneNumber)
}
return contacts
}
fun getContactEvents(getBirthdays: Boolean): SparseArray<ArrayList<String>> {
val eventDates = SparseArray<ArrayList<String>>()
val uri = Data.CONTENT_URI
val projection = arrayOf(
Data.RAW_CONTACT_ID,
CommonDataKinds.Event.START_DATE
)
val selection = "${CommonDataKinds.Event.MIMETYPE} = ? AND ${CommonDataKinds.Event.TYPE} = ?"
val requiredType = if (getBirthdays) CommonDataKinds.Event.TYPE_BIRTHDAY.toString() else CommonDataKinds.Event.TYPE_ANNIVERSARY.toString()
val selectionArgs = arrayOf(CommonDataKinds.Event.CONTENT_ITEM_TYPE, requiredType)
context.queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
val id = cursor.getIntValue(Data.RAW_CONTACT_ID)
val startDate = cursor.getStringValue(CommonDataKinds.Event.START_DATE) ?: return@queryCursor
if (eventDates[id] == null) {
eventDates.put(id, ArrayList())
}
eventDates[id]!!.add(startDate)
}
return eventDates
}
fun getNameFromPhoneNumber(number: String): String {
if (!context.hasPermission(PERMISSION_READ_CONTACTS)) {
return number

View file

@ -4,7 +4,8 @@ import android.telephony.PhoneNumberUtils
import com.simplemobiletools.commons.extensions.normalizePhoneNumber
import com.simplemobiletools.commons.extensions.normalizeString
data class SimpleContact(val rawId: Int, val contactId: Int, var name: String, var photoUri: String, var phoneNumbers: ArrayList<String>) : Comparable<SimpleContact> {
data class SimpleContact(val rawId: Int, val contactId: Int, var name: String, var photoUri: String, var phoneNumbers: ArrayList<String>,
var birthdays: ArrayList<String>, var anniversaries: ArrayList<String>) : Comparable<SimpleContact> {
override fun compareTo(other: SimpleContact): Int {
val firstString = name.normalizeString()
val secondString = other.name.normalizeString()