shortening some more code, no real change

This commit is contained in:
tibbi 2020-04-17 16:49:12 +02:00
parent f3ed1ccec4
commit 5ee1890c2a
3 changed files with 87 additions and 117 deletions

View File

@ -4,7 +4,6 @@ import android.app.Activity
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Intent
import android.database.Cursor
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.LayerDrawable
@ -1458,30 +1457,21 @@ class EventActivity : SimpleActivity() {
val selection = "${ContactsContract.Data.MIMETYPE} = ?"
val selectionArgs = arrayOf(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
var cursor: Cursor? = null
try {
cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
val prefix = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX) ?: ""
val firstName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME) ?: ""
val middleName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME) ?: ""
val surname = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME) ?: ""
val suffix = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX) ?: ""
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI) ?: ""
queryCursor(uri, projection, selection, selectionArgs) { cursor ->
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
val prefix = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX) ?: ""
val firstName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME) ?: ""
val middleName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME) ?: ""
val surname = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME) ?: ""
val suffix = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX) ?: ""
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI) ?: ""
val names = arrayListOf(prefix, firstName, middleName, surname, suffix).filter { it.trim().isNotEmpty() }
val fullName = TextUtils.join(" ", names).trim()
if (fullName.isNotEmpty() || photoUri.isNotEmpty()) {
val contact = Attendee(id, fullName, "", CalendarContract.Attendees.ATTENDEE_STATUS_NONE, photoUri, false, CalendarContract.Attendees.RELATIONSHIP_NONE)
contacts.add(contact)
}
} while (cursor.moveToNext())
val names = arrayListOf(prefix, firstName, middleName, surname, suffix).filter { it.trim().isNotEmpty() }
val fullName = TextUtils.join(" ", names).trim()
if (fullName.isNotEmpty() || photoUri.isNotEmpty()) {
val contact = Attendee(id, fullName, "", CalendarContract.Attendees.ATTENDEE_STATUS_NONE, photoUri, false, CalendarContract.Attendees.RELATIONSHIP_NONE)
contacts.add(contact)
}
} catch (ignored: Exception) {
} finally {
cursor?.close()
}
return contacts
}
@ -1494,21 +1484,13 @@ class EventActivity : SimpleActivity() {
ContactsContract.CommonDataKinds.Email.DATA
)
var cursor: Cursor? = null
try {
cursor = contentResolver.query(uri, projection, null, null, null)
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
val email = cursor.getStringValue(ContactsContract.CommonDataKinds.Email.DATA) ?: continue
val contact = Attendee(id, "", email, CalendarContract.Attendees.ATTENDEE_STATUS_NONE, "", false, CalendarContract.Attendees.RELATIONSHIP_NONE)
contacts.add(contact)
} while (cursor.moveToNext())
}
} catch (ignored: Exception) {
} finally {
cursor?.close()
queryCursor(uri, projection) { cursor ->
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
val email = cursor.getStringValue(ContactsContract.CommonDataKinds.Email.DATA) ?: return@queryCursor
val contact = Attendee(id, "", email, CalendarContract.Attendees.ATTENDEE_STATUS_NONE, "", false, CalendarContract.Attendees.RELATIONSHIP_NONE)
contacts.add(contact)
}
return contacts
}

View File

@ -8,13 +8,12 @@ import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.database.Cursor
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Icon
import android.graphics.drawable.LayerDrawable
import android.net.Uri
import android.os.Bundle
import android.provider.ContactsContract
import android.provider.ContactsContract.*
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
@ -32,6 +31,8 @@ import com.simplemobiletools.calendar.pro.extensions.*
import com.simplemobiletools.calendar.pro.fragments.*
import com.simplemobiletools.calendar.pro.helpers.*
import com.simplemobiletools.calendar.pro.helpers.Formatter
import com.simplemobiletools.calendar.pro.helpers.IcsExporter.ExportResult
import com.simplemobiletools.calendar.pro.helpers.IcsImporter.ImportResult
import com.simplemobiletools.calendar.pro.jobs.CalDAVUpdateListener
import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.calendar.pro.models.EventType
@ -488,7 +489,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
val result = IcsImporter(this).importEvents(it as String, eventTypeId, 0, false)
handleParseResult(result)
if (result != IcsImporter.ImportResult.IMPORT_FAIL) {
if (result != ImportResult.IMPORT_FAIL) {
runOnUiThread {
updateViewPager()
}
@ -545,11 +546,11 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
}
}
private fun handleParseResult(result: IcsImporter.ImportResult) {
private fun handleParseResult(result: ImportResult) {
toast(when (result) {
IcsImporter.ImportResult.IMPORT_NOTHING_NEW -> R.string.no_new_items
IcsImporter.ImportResult.IMPORT_OK -> R.string.holidays_imported_successfully
IcsImporter.ImportResult.IMPORT_PARTIAL -> R.string.importing_some_holidays_failed
ImportResult.IMPORT_NOTHING_NEW -> R.string.no_new_items
ImportResult.IMPORT_OK -> R.string.holidays_imported_successfully
ImportResult.IMPORT_PARTIAL -> R.string.importing_some_holidays_failed
else -> R.string.importing_holidays_failed
}, Toast.LENGTH_LONG)
}
@ -557,78 +558,69 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
private fun addContactEvents(birthdays: Boolean, reminders: ArrayList<Int>, callback: (Int) -> Unit) {
var eventsAdded = 0
var eventsFound = 0
val uri = ContactsContract.Data.CONTENT_URI
val projection = arrayOf(ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.CONTACT_LAST_UPDATED_TIMESTAMP,
ContactsContract.CommonDataKinds.Event.START_DATE)
val uri = Data.CONTENT_URI
val projection = arrayOf(Contacts.DISPLAY_NAME,
CommonDataKinds.Event.CONTACT_ID,
CommonDataKinds.Event.CONTACT_LAST_UPDATED_TIMESTAMP,
CommonDataKinds.Event.START_DATE)
val selection = "${ContactsContract.Data.MIMETYPE} = ? AND ${ContactsContract.CommonDataKinds.Event.TYPE} = ?"
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 existingEvents = if (birthdays) eventsDB.getBirthdays() else eventsDB.getAnniversaries()
val importIDs = HashMap<String, Long>()
existingEvents.forEach {
importIDs[it.importId] = it.startTS
}
val selection = "${Data.MIMETYPE} = ? AND ${CommonDataKinds.Event.TYPE} = ?"
val type = if (birthdays) CommonDataKinds.Event.TYPE_BIRTHDAY else CommonDataKinds.Event.TYPE_ANNIVERSARY
val selectionArgs = arrayOf(CommonDataKinds.Event.CONTENT_ITEM_TYPE, type.toString())
val eventTypeId = if (birthdays) getBirthdaysEventTypeId() else getAnniversariesEventTypeId()
val dateFormats = getDateFormats()
val existingEvents = if (birthdays) eventsDB.getBirthdays() else eventsDB.getAnniversaries()
val importIDs = HashMap<String, Long>()
existingEvents.forEach {
importIDs[it.importId] = it.startTS
}
do {
val contactId = cursor.getIntValue(ContactsContract.CommonDataKinds.Event.CONTACT_ID).toString()
val name = cursor.getStringValue(ContactsContract.Contacts.DISPLAY_NAME)
val startDate = cursor.getStringValue(ContactsContract.CommonDataKinds.Event.START_DATE)
val eventTypeId = if (birthdays) getBirthdaysEventTypeId() else getAnniversariesEventTypeId()
for (format in dateFormats) {
try {
val formatter = SimpleDateFormat(format, Locale.getDefault())
val date = formatter.parse(startDate)
if (date.year < 70) {
date.year = 70
queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
val contactId = cursor.getIntValue(CommonDataKinds.Event.CONTACT_ID).toString()
val name = cursor.getStringValue(Contacts.DISPLAY_NAME)
val startDate = cursor.getStringValue(CommonDataKinds.Event.START_DATE)
for (format in dateFormats) {
try {
val formatter = SimpleDateFormat(format, Locale.getDefault())
val date = formatter.parse(startDate)
if (date.year < 70) {
date.year = 70
}
val timestamp = date.time / 1000L
val source = if (birthdays) SOURCE_CONTACT_BIRTHDAY else SOURCE_CONTACT_ANNIVERSARY
val lastUpdated = cursor.getLongValue(CommonDataKinds.Event.CONTACT_LAST_UPDATED_TIMESTAMP)
val event = Event(null, timestamp, timestamp, name, reminder1Minutes = reminders[0], reminder2Minutes = reminders[1],
reminder3Minutes = reminders[2], importId = contactId, timeZone = DateTimeZone.getDefault().id, flags = FLAG_ALL_DAY,
repeatInterval = YEAR, repeatRule = REPEAT_SAME_DAY, eventType = eventTypeId, source = source, lastUpdated = lastUpdated)
val importIDsToDelete = ArrayList<String>()
for ((key, value) in importIDs) {
if (key == contactId && value != timestamp) {
val deleted = eventsDB.deleteBirthdayAnniversary(source, key)
if (deleted == 1) {
importIDsToDelete.add(key)
}
val timestamp = date.time / 1000L
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(null, timestamp, timestamp, name, reminder1Minutes = reminders[0], reminder2Minutes = reminders[1],
reminder3Minutes = reminders[2], importId = contactId, timeZone = DateTimeZone.getDefault().id, flags = FLAG_ALL_DAY,
repeatInterval = YEAR, repeatRule = REPEAT_SAME_DAY, eventType = eventTypeId, source = source, lastUpdated = lastUpdated)
val importIDsToDelete = ArrayList<String>()
for ((key, value) in importIDs) {
if (key == contactId && value != timestamp) {
val deleted = eventsDB.deleteBirthdayAnniversary(source, key)
if (deleted == 1) {
importIDsToDelete.add(key)
}
}
}
importIDsToDelete.forEach {
importIDs.remove(it)
}
eventsFound++
if (!importIDs.containsKey(contactId)) {
eventsHelper.insertEvent(event, false, false) {
eventsAdded++
}
}
break
} catch (e: Exception) {
}
}
} while (cursor.moveToNext())
importIDsToDelete.forEach {
importIDs.remove(it)
}
eventsFound++
if (!importIDs.containsKey(contactId)) {
eventsHelper.insertEvent(event, false, false) {
eventsAdded++
}
}
break
} catch (e: Exception) {
}
}
} catch (e: Exception) {
showErrorToast(e)
} finally {
cursor?.close()
}
runOnUiThread {
@ -839,8 +831,8 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
} else {
IcsExporter().exportEvents(this, outputStream, events, true) {
toast(when (it) {
IcsExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful
IcsExporter.ExportResult.EXPORT_PARTIAL -> R.string.exporting_some_entries_failed
ExportResult.EXPORT_OK -> R.string.exporting_successful
ExportResult.EXPORT_PARTIAL -> R.string.exporting_some_entries_failed
else -> R.string.exporting_failed
})
}

View File

@ -4,7 +4,6 @@ import android.annotation.SuppressLint
import android.content.ContentUris
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.provider.CalendarContract.*
import android.util.SparseIntArray
import com.google.gson.Gson
@ -109,14 +108,11 @@ class CalDAVHelper(val context: Context) {
val selection = "${Colors.COLOR_TYPE} = ? AND ${Colors.COLOR} = ? AND ${Colors.ACCOUNT_NAME} = ?"
val selectionArgs = arrayOf(Colors.TYPE_CALENDAR.toString(), eventType.color.toString(), eventType.caldavEmail)
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
val cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use {
if (cursor.moveToFirst()) {
return cursor.getStringValue(Colors.COLOR_KEY).toInt()
}
} finally {
cursor?.close()
}
return -1