add a menu button for importing contact birthdays

This commit is contained in:
tibbi 2017-10-21 15:19:17 +02:00
parent c4972e77a7
commit d55c0eac62
22 changed files with 188 additions and 9 deletions

View File

@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.READ_CALENDAR"/>

View File

@ -6,10 +6,12 @@ import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
import android.database.ContentObserver
import android.database.Cursor
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.provider.CalendarContract
import android.provider.ContactsContract
import android.support.v4.app.ActivityCompat
import android.support.v4.view.ViewPager
import android.util.SparseIntArray
@ -45,6 +47,7 @@ import com.simplemobiletools.commons.models.Release
import kotlinx.android.synthetic.main.activity_main.*
import org.joda.time.DateTime
import java.io.FileOutputStream
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
@ -53,8 +56,9 @@ class MainActivity : SimpleActivity(), NavigationListener {
private val PREFILLED_MONTHS = 97
private val PREFILLED_YEARS = 31
private val PREFILLED_WEEKS = 61
private val STORAGE_PERMISSION_IMPORT = 1
private val STORAGE_PERMISSION_EXPORT = 2
private val PERMISSION_STORAGE_IMPORT = 1
private val PERMISSION_STORAGE_EXPORT = 2
private val PERMISSION_CONTACTS = 3
private var mIsMonthSelected = false
private var mStoredTextColor = 0
@ -173,6 +177,7 @@ class MainActivity : SimpleActivity(), NavigationListener {
R.id.filter -> showFilterDialog()
R.id.refresh_caldav_calendars -> refreshCalDAVCalendars()
R.id.add_holidays -> addHolidays()
R.id.add_birthdays -> tryAddBirthdays()
R.id.import_events -> tryImportEvents()
R.id.export_events -> tryExportEvents()
R.id.settings -> launchSettings()
@ -284,6 +289,49 @@ class MainActivity : SimpleActivity(), NavigationListener {
}
}
private fun tryAddBirthdays() {
if (hasContactsPermission()) {
Thread({
addBirthdays()
}).start()
} else {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_CONTACTS), PERMISSION_CONTACTS)
}
}
private fun addBirthdays() {
var birthdaysAdded = 0
val uri = ContactsContract.Data.CONTENT_URI
val projection = arrayOf(ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
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())
var cursor: Cursor? = null
try {
cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
do {
val contactId = cursor.getIntValue(ContactsContract.CommonDataKinds.Event.CONTACT_ID)
val name = cursor.getStringValue(ContactsContract.Contacts.DISPLAY_NAME)
val birthDay = cursor.getStringValue(ContactsContract.CommonDataKinds.Event.START_DATE)
val formatter = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
val timestamp = formatter.parse(birthDay).time / 1000
birthdaysAdded++
} while (cursor.moveToNext())
}
} catch (e: Exception) {
showErrorToast(e)
} finally {
cursor?.close()
}
runOnUiThread {
toast(if (birthdaysAdded > 0) R.string.birthdays_added else R.string.no_birthdays)
}
}
private fun handleParseResult(result: IcsImporter.ImportResult) {
toast(when (result) {
IcsImporter.ImportResult.IMPORT_OK -> R.string.holidays_imported_successfully
@ -329,7 +377,7 @@ class MainActivity : SimpleActivity(), NavigationListener {
if (hasReadStoragePermission()) {
importEvents()
} else {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), STORAGE_PERMISSION_IMPORT)
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), PERMISSION_STORAGE_IMPORT)
}
}
@ -372,7 +420,7 @@ class MainActivity : SimpleActivity(), NavigationListener {
if (hasReadStoragePermission()) {
exportEvents()
} else {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), STORAGE_PERMISSION_EXPORT)
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), PERMISSION_STORAGE_EXPORT)
}
}
@ -604,10 +652,10 @@ class MainActivity : SimpleActivity(), NavigationListener {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (requestCode == STORAGE_PERMISSION_IMPORT) {
importEvents()
} else if (requestCode == STORAGE_PERMISSION_EXPORT) {
exportEvents()
when (requestCode) {
PERMISSION_STORAGE_IMPORT -> importEvents()
PERMISSION_STORAGE_EXPORT -> exportEvents()
PERMISSION_CONTACTS -> Thread({ addBirthdays() }).start()
}
}
}

View File

@ -12,7 +12,7 @@ import com.simplemobiletools.calendar.helpers.IcsExporter
import com.simplemobiletools.commons.extensions.toast
import java.io.File
fun Activity.hasGetAccountsPermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED
fun Activity.hasContactsPermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED
fun SimpleActivity.shareEvents(ids: List<Int>) {
val file = getTempFile()

View File

@ -25,6 +25,10 @@
android:id="@+id/add_holidays"
android:title="@string/add_holidays"
app:showAsAction="never"/>
<item
android:id="@+id/add_birthdays"
android:title="@string/add_birthdays"
app:showAsAction="never"/>
<item
android:id="@+id/import_events"
android:title="@string/import_events_from_ics"

View File

@ -101,6 +101,13 @@
<item quantity="other">%d roků</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Birthdays</string>
<string name="add_birthdays">Add contact birthdays</string>
<string name="no_contacts_permission">Cannot obtain birthdays without the Contacts permission</string>
<string name="no_birthdays">No birthdays have been found</string>
<string name="birthdays_added">Birthdays added successfully</string>
<!-- Event Reminders -->
<string name="reminder">Připomenutí</string>
<string name="no_reminder">Žádné připomenutí</string>

View File

@ -97,6 +97,13 @@
<item quantity="other">%d Jahre</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Birthdays</string>
<string name="add_birthdays">Add contact birthdays</string>
<string name="no_contacts_permission">Cannot obtain birthdays without the Contacts permission</string>
<string name="no_birthdays">No birthdays have been found</string>
<string name="birthdays_added">Birthdays added successfully</string>
<!-- Event Reminders -->
<string name="reminder">Erinnerung</string>
<string name="no_reminder">Keine Erinnerung</string>

View File

@ -97,6 +97,13 @@
<item quantity="other">%d años</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Birthdays</string>
<string name="add_birthdays">Add contact birthdays</string>
<string name="no_contacts_permission">Cannot obtain birthdays without the Contacts permission</string>
<string name="no_birthdays">No birthdays have been found</string>
<string name="birthdays_added">Birthdays added successfully</string>
<!-- Event Reminders -->
<string name="reminder">Recordatorio</string>
<string name="no_reminder">Sin recordatorio</string>

View File

@ -97,6 +97,13 @@
<item quantity="other">%d ans</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Birthdays</string>
<string name="add_birthdays">Add contact birthdays</string>
<string name="no_contacts_permission">Cannot obtain birthdays without the Contacts permission</string>
<string name="no_birthdays">No birthdays have been found</string>
<string name="birthdays_added">Birthdays added successfully</string>
<!-- Event Reminders -->
<string name="reminder">Rappel</string>
<string name="no_reminder">Pas de rappel</string>

View File

@ -97,6 +97,13 @@
<item quantity="other">%d years</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Birthdays</string>
<string name="add_birthdays">Add contact birthdays</string>
<string name="no_contacts_permission">Cannot obtain birthdays without the Contacts permission</string>
<string name="no_birthdays">No birthdays have been found</string>
<string name="birthdays_added">Birthdays added successfully</string>
<!-- Event Reminders -->
<string name="reminder">रिमाइंडर</string>
<string name="no_reminder">No reminder</string>

View File

@ -97,6 +97,13 @@
<item quantity="other">%d years</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Birthdays</string>
<string name="add_birthdays">Add contact birthdays</string>
<string name="no_contacts_permission">Cannot obtain birthdays without the Contacts permission</string>
<string name="no_birthdays">No birthdays have been found</string>
<string name="birthdays_added">Birthdays added successfully</string>
<!-- Event Reminders -->
<string name="reminder">Emlékeztető</string>
<string name="no_reminder">No reminder</string>

View File

@ -97,6 +97,13 @@
<item quantity="other">%d years</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Birthdays</string>
<string name="add_birthdays">Add contact birthdays</string>
<string name="no_contacts_permission">Cannot obtain birthdays without the Contacts permission</string>
<string name="no_birthdays">No birthdays have been found</string>
<string name="birthdays_added">Birthdays added successfully</string>
<!-- Event Reminders -->
<string name="reminder">Promemoria</string>
<string name="no_reminder">No reminder</string>

View File

@ -97,6 +97,13 @@
<item quantity="other">%d years</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Birthdays</string>
<string name="add_birthdays">Add contact birthdays</string>
<string name="no_contacts_permission">Cannot obtain birthdays without the Contacts permission</string>
<string name="no_birthdays">No birthdays have been found</string>
<string name="birthdays_added">Birthdays added successfully</string>
<!-- Event Reminders -->
<string name="reminder">תזכורת</string>
<string name="no_reminder">No reminder</string>

View File

@ -97,6 +97,13 @@
<item quantity="other">%d years</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Birthdays</string>
<string name="add_birthdays">Add contact birthdays</string>
<string name="no_contacts_permission">Cannot obtain birthdays without the Contacts permission</string>
<string name="no_birthdays">No birthdays have been found</string>
<string name="birthdays_added">Birthdays added successfully</string>
<!-- Event Reminders -->
<string name="reminder">リマインダー</string>
<string name="no_reminder">No reminder</string>

View File

@ -97,6 +97,13 @@
<item quantity="other">%d years</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Birthdays</string>
<string name="add_birthdays">Add contact birthdays</string>
<string name="no_contacts_permission">Cannot obtain birthdays without the Contacts permission</string>
<string name="no_birthdays">No birthdays have been found</string>
<string name="birthdays_added">Birthdays added successfully</string>
<!-- Event Reminders -->
<string name="reminder">알림</string>
<string name="no_reminder">알림 없음</string>

View File

@ -100,6 +100,13 @@
<item quantity="other">%d lat</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Birthdays</string>
<string name="add_birthdays">Add contact birthdays</string>
<string name="no_contacts_permission">Cannot obtain birthdays without the Contacts permission</string>
<string name="no_birthdays">No birthdays have been found</string>
<string name="birthdays_added">Birthdays added successfully</string>
<!-- Event Reminders -->
<string name="reminder">Przypomnienie</string>
<string name="no_reminder">Brak przypomnienia</string>

View File

@ -97,6 +97,13 @@
<item quantity="other">%d anos</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Birthdays</string>
<string name="add_birthdays">Add contact birthdays</string>
<string name="no_contacts_permission">Cannot obtain birthdays without the Contacts permission</string>
<string name="no_birthdays">No birthdays have been found</string>
<string name="birthdays_added">Birthdays added successfully</string>
<!-- Event Reminders -->
<string name="reminder">Lembrete</string>
<string name="no_reminder">Sem lembrete</string>

View File

@ -97,6 +97,13 @@
<item quantity="other">%d anos</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Birthdays</string>
<string name="add_birthdays">Add contact birthdays</string>
<string name="no_contacts_permission">Cannot obtain birthdays without the Contacts permission</string>
<string name="no_birthdays">No birthdays have been found</string>
<string name="birthdays_added">Birthdays added successfully</string>
<!-- Event Reminders -->
<string name="reminder">Lembrete</string>
<string name="no_reminder">Sem lembrete</string>

View File

@ -105,6 +105,13 @@
<item quantity="other">%d года(лет)</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Birthdays</string>
<string name="add_birthdays">Add contact birthdays</string>
<string name="no_contacts_permission">Cannot obtain birthdays without the Contacts permission</string>
<string name="no_birthdays">No birthdays have been found</string>
<string name="birthdays_added">Birthdays added successfully</string>
<!-- Event Reminders -->
<string name="reminder">Напоминание</string>
<string name="no_reminder">Без напоминания</string>

View File

@ -101,6 +101,13 @@
<item quantity="other">%d rokov</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Narodeniny</string>
<string name="add_birthdays">Pridať narodeniny kontaktov</string>
<string name="no_contacts_permission">Nepodarilo sa získať narodeniny bez povolenia</string>
<string name="no_birthdays">Nenašli sa žiadne narodeniny</string>
<string name="birthdays_added">Narodeniny boli úspešne pridané</string>
<!-- Event Reminders -->
<string name="reminder">Pripomenka</string>
<string name="no_reminder">Žiadna pripomienka</string>

View File

@ -97,6 +97,13 @@
<item quantity="other">%d år</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Birthdays</string>
<string name="add_birthdays">Add contact birthdays</string>
<string name="no_contacts_permission">Cannot obtain birthdays without the Contacts permission</string>
<string name="no_birthdays">No birthdays have been found</string>
<string name="birthdays_added">Birthdays added successfully</string>
<!-- Event Reminders -->
<string name="reminder">Påminnelse</string>
<string name="no_reminder">Ingen påminnelse</string>

View File

@ -97,6 +97,13 @@
<item quantity="other">yıllar %d</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Birthdays</string>
<string name="add_birthdays">Add contact birthdays</string>
<string name="no_contacts_permission">Cannot obtain birthdays without the Contacts permission</string>
<string name="no_birthdays">No birthdays have been found</string>
<string name="birthdays_added">Birthdays added successfully</string>
<!-- Event Reminders -->
<string name="reminder">Hatırlatma</string>
<string name="no_reminder">Hatırlatma yok</string>

View File

@ -97,6 +97,13 @@
<item quantity="other">%d years</item>
</plurals>
<!-- Birthdays -->
<string name="birthdays">Birthdays</string>
<string name="add_birthdays">Add contact birthdays</string>
<string name="no_contacts_permission">Cannot obtain birthdays without the Contacts permission</string>
<string name="no_birthdays">No birthdays have been found</string>
<string name="birthdays_added">Birthdays added successfully</string>
<!-- Event Reminders -->
<string name="reminder">Reminder</string>
<string name="no_reminder">No reminder</string>