diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt index ea1713296..1fbf40c2e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt @@ -172,6 +172,7 @@ class MainActivity : SimpleActivity(), NavigationListener { R.id.refresh_caldav_calendars -> refreshCalDAVCalendars() R.id.add_holidays -> addHolidays() R.id.add_birthdays -> tryAddBirthdays() + R.id.add_anniversaries -> tryAddAnniversaries() R.id.import_events -> tryImportEvents() R.id.export_events -> tryExportEvents() R.id.settings -> launchSettings() @@ -315,6 +316,18 @@ class MainActivity : SimpleActivity(), NavigationListener { } } + private fun tryAddAnniversaries() { + handlePermission(PERMISSION_READ_CONTACTS) { + if (it) { + Thread({ + addAnniversaries() + }).start() + } else { + toast(R.string.no_contacts_permission) + } + } + } + private fun handleParseResult(result: IcsImporter.ImportResult) { toast(when (result) { IcsImporter.ImportResult.IMPORT_OK -> R.string.holidays_imported_successfully @@ -324,16 +337,7 @@ class MainActivity : SimpleActivity(), NavigationListener { } private fun addBirthdays() { - val birthdays = getString(R.string.birthdays) - var eventTypeId = dbHelper.getEventTypeIdWithTitle(birthdays) - if (eventTypeId == -1) { - val eventType = EventType(0, birthdays, resources.getColor(R.color.default_birthdays_color)) - eventTypeId = dbHelper.insertEventType(eventType) - } - - val birthdayImportIDs = dbHelper.getBirthdays().map { it.importId } - var birthdaysAdded = 0 - val dateFormats = getBirthdayFormats() + var eventsAdded = 0 val uri = ContactsContract.Data.CONTENT_URI val projection = arrayOf(ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Event.CONTACT_ID, @@ -346,27 +350,30 @@ class MainActivity : SimpleActivity(), NavigationListener { 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() + do { val contactId = cursor.getIntValue(ContactsContract.CommonDataKinds.Event.CONTACT_ID).toString() val name = cursor.getStringValue(ContactsContract.Contacts.DISPLAY_NAME) - val birthDay = cursor.getStringValue(ContactsContract.CommonDataKinds.Event.START_DATE) + val startDate = cursor.getStringValue(ContactsContract.CommonDataKinds.Event.START_DATE) for (format in dateFormats) { try { val formatter = SimpleDateFormat(format, Locale.getDefault()) - val date = formatter.parse(birthDay) - if (date.year < 1970) + val date = formatter.parse(startDate) + if (date.year < 70) date.year = 70 val timestamp = (date.time / 1000).toInt() - 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) - if (!birthdayImportIDs.contains(contactId)) { + if (!importIDs.contains(contactId)) { dbHelper.insert(event, false) { - birthdaysAdded++ + eventsAdded++ } } break @@ -382,7 +389,7 @@ class MainActivity : SimpleActivity(), NavigationListener { } runOnUiThread { - if (birthdaysAdded > 0) { + if (eventsAdded > 0) { toast(R.string.birthdays_added) updateViewPager() } else { @@ -391,7 +398,21 @@ class MainActivity : SimpleActivity(), NavigationListener { } } - private fun getBirthdayFormats() = arrayListOf( + private fun getBirthdaysEventTypeId(): Int { + val birthdays = getString(R.string.birthdays) + var eventTypeId = dbHelper.getEventTypeIdWithTitle(birthdays) + if (eventTypeId == -1) { + val eventType = EventType(0, birthdays, resources.getColor(R.color.default_birthdays_color)) + eventTypeId = dbHelper.insertEventType(eventType) + } + return eventTypeId + } + + private fun addAnniversaries() { + + } + + private fun getDateFormats() = arrayListOf( "yyyy-MM-dd", "yyyyMMdd", "yyyy.MM.dd", diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt index 9ac30a2c0..1ece3f212 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt @@ -127,6 +127,7 @@ val FONT_SIZE_LARGE = 2 val SOURCE_SIMPLE_CALENDAR = "simple-calendar" val SOURCE_IMPORTED_ICS = "imported-ics" val SOURCE_CONTACT_BIRTHDAY = "contact-birthday" +val SOURCE_CONTACT_ANNIVERSARY = "contact-anniversary" // deprecated val SOURCE_GOOGLE_CALENDAR = 1 diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt index 411ea0bf4..08345ca38 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt @@ -401,6 +401,13 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont return fillEvents(cursor) } + fun getAnniversaries(): List { + val selection = "$MAIN_TABLE_NAME.$COL_EVENT_SOURCE = ?" + val selectionArgs = arrayOf(SOURCE_CONTACT_ANNIVERSARY) + val cursor = getEventsCursor(selection, selectionArgs) + return fillEvents(cursor) + } + fun deleteEvents(ids: Array, deleteFromCalDAV: Boolean) { val args = TextUtils.join(", ", ids) val selection = "$MAIN_TABLE_NAME.$COL_ID IN ($args)" diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml index 4a11004f5..93f25c68e 100644 --- a/app/src/main/res/menu/menu_main.xml +++ b/app/src/main/res/menu/menu_main.xml @@ -29,6 +29,10 @@ android:id="@+id/add_birthdays" android:title="@string/add_birthdays" app:showAsAction="never"/> + #FFF62F2F #FF303DF6 #FF64DD17 + #FFF63030