mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-02-17 04:10:45 +01:00
adding Contact anniversaries importing
This commit is contained in:
parent
6a6cfbcb51
commit
72a146b11d
@ -308,7 +308,14 @@ class MainActivity : SimpleActivity(), NavigationListener {
|
||||
handlePermission(PERMISSION_READ_CONTACTS) {
|
||||
if (it) {
|
||||
Thread({
|
||||
addBirthdays()
|
||||
addContactEvents(true) {
|
||||
if (it > 0) {
|
||||
toast(R.string.birthdays_added)
|
||||
updateViewPager()
|
||||
} else {
|
||||
toast(R.string.no_birthdays)
|
||||
}
|
||||
}
|
||||
}).start()
|
||||
} else {
|
||||
toast(R.string.no_contacts_permission)
|
||||
@ -320,7 +327,14 @@ class MainActivity : SimpleActivity(), NavigationListener {
|
||||
handlePermission(PERMISSION_READ_CONTACTS) {
|
||||
if (it) {
|
||||
Thread({
|
||||
addAnniversaries()
|
||||
addContactEvents(false) {
|
||||
if (it > 0) {
|
||||
toast(R.string.anniversaries_added)
|
||||
updateViewPager()
|
||||
} else {
|
||||
toast(R.string.no_anniversaries)
|
||||
}
|
||||
}
|
||||
}).start()
|
||||
} else {
|
||||
toast(R.string.no_contacts_permission)
|
||||
@ -336,7 +350,7 @@ class MainActivity : SimpleActivity(), NavigationListener {
|
||||
}, Toast.LENGTH_LONG)
|
||||
}
|
||||
|
||||
private fun addBirthdays() {
|
||||
private fun addContactEvents(birthdays: Boolean, callback: (Int) -> Unit) {
|
||||
var eventsAdded = 0
|
||||
val uri = ContactsContract.Data.CONTENT_URI
|
||||
val projection = arrayOf(ContactsContract.Contacts.DISPLAY_NAME,
|
||||
@ -345,14 +359,16 @@ class MainActivity : SimpleActivity(), NavigationListener {
|
||||
ContactsContract.CommonDataKinds.Event.START_DATE)
|
||||
|
||||
val selection = "${ContactsContract.Data.MIMETYPE} = ? AND ${ContactsContract.CommonDataKinds.Event.TYPE} = ?"
|
||||
val selectionArgs = arrayOf(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY.toString())
|
||||
val type = if (birthdays) ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY else ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY
|
||||
val selectionArgs = arrayOf(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, type.toString())
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
|
||||
if (cursor?.moveToFirst() == true) {
|
||||
val dateFormats = getDateFormats()
|
||||
val importIDs = dbHelper.getBirthdays().map { it.importId }
|
||||
val eventTypeId = getBirthdaysEventTypeId()
|
||||
val existingEvents = if (birthdays) dbHelper.getBirthdays() else dbHelper.getAnniversaries()
|
||||
val importIDs = existingEvents.map { it.importId }
|
||||
val eventTypeId = if (birthdays) getBirthdaysEventTypeId() else getAnniversariesEventTypeId()
|
||||
|
||||
do {
|
||||
val contactId = cursor.getIntValue(ContactsContract.CommonDataKinds.Event.CONTACT_ID).toString()
|
||||
@ -367,9 +383,10 @@ class MainActivity : SimpleActivity(), NavigationListener {
|
||||
date.year = 70
|
||||
|
||||
val timestamp = (date.time / 1000).toInt()
|
||||
val source = if (birthdays) SOURCE_CONTACT_BIRTHDAY else SOURCE_CONTACT_ANNIVERSARY
|
||||
val lastUpdated = cursor.getLongValue(ContactsContract.CommonDataKinds.Event.CONTACT_LAST_UPDATED_TIMESTAMP)
|
||||
val event = Event(0, timestamp, timestamp, name, importId = contactId, flags = FLAG_ALL_DAY, repeatInterval = YEAR,
|
||||
eventType = eventTypeId, source = SOURCE_CONTACT_BIRTHDAY, lastUpdated = lastUpdated)
|
||||
eventType = eventTypeId, source = source, lastUpdated = lastUpdated)
|
||||
|
||||
if (!importIDs.contains(contactId)) {
|
||||
dbHelper.insert(event, false) {
|
||||
@ -389,12 +406,7 @@ class MainActivity : SimpleActivity(), NavigationListener {
|
||||
}
|
||||
|
||||
runOnUiThread {
|
||||
if (eventsAdded > 0) {
|
||||
toast(R.string.birthdays_added)
|
||||
updateViewPager()
|
||||
} else {
|
||||
toast(R.string.no_birthdays)
|
||||
}
|
||||
callback(eventsAdded)
|
||||
}
|
||||
}
|
||||
|
||||
@ -408,8 +420,14 @@ class MainActivity : SimpleActivity(), NavigationListener {
|
||||
return eventTypeId
|
||||
}
|
||||
|
||||
private fun addAnniversaries() {
|
||||
|
||||
private fun getAnniversariesEventTypeId(): Int {
|
||||
val anniversaries = getString(R.string.anniversaries)
|
||||
var eventTypeId = dbHelper.getEventTypeIdWithTitle(anniversaries)
|
||||
if (eventTypeId == -1) {
|
||||
val eventType = EventType(0, anniversaries, resources.getColor(R.color.default_anniversaries_color))
|
||||
eventTypeId = dbHelper.insertEventType(eventType)
|
||||
}
|
||||
return eventTypeId
|
||||
}
|
||||
|
||||
private fun getDateFormats() = arrayListOf(
|
||||
|
Loading…
x
Reference in New Issue
Block a user