Commit 6cc376f3 authored by Ricki Hirner's avatar Ricki Hirner

Minor code cleanup

parent 211657de
......@@ -18,7 +18,7 @@ import android.util.Log
object AppAccount {
val SYNC_INTERVAL_MANUALLY = -1L
const val SYNC_INTERVAL_MANUALLY = -1L
val account = Account("ICSdroid", "at.bitfire.icsdroid")
......
......@@ -10,15 +10,14 @@ package at.bitfire.icsdroid
import android.net.Uri
import android.os.Build
import net.fortuna.ical4j.util.CompatibilityHints
object Constants {
@JvmField val TAG = "icsdroid"
const val TAG = "icsdroid"
@JvmField val USER_AGENT = "ICSdroid/" + BuildConfig.VERSION_NAME + " (Android/" + Build.VERSION.RELEASE + ")"
@JvmField val MAX_REDIRECTS = 5
val USER_AGENT = "ICSdroid/" + BuildConfig.VERSION_NAME + " (Android/" + Build.VERSION.RELEASE + ")"
const val MAX_REDIRECTS = 5
@JvmField val donationUri = Uri.parse("https://icsdroid.bitfire.at/donate/?pk_campaign=icsdroid-app")!!
val donationUri = Uri.parse("https://icsdroid.bitfire.at/donate/?pk_campaign=icsdroid-app")!!
}
......@@ -16,14 +16,12 @@ import javax.net.ssl.SSLContext
object CustomCertificates {
@JvmStatic
fun certManager(context: Context, foreground: Boolean = false): CustomCertManager {
val manager = CustomCertManager(context, true)
manager.appInForeground = foreground
return manager
}
@JvmStatic
fun prepareURLConnection(manager: CustomCertManager, connection: HttpsURLConnection) {
try {
val sc = SSLContext.getInstance("TLS")
......
......@@ -9,18 +9,17 @@
package at.bitfire.icsdroid
import android.util.Log
import org.apache.commons.codec.Charsets
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import java.util.regex.Pattern
object MiscUtils {
private val regexContentTypeCharset = Pattern.compile("[; ]\\s*charset=\"?([^\"]+)\"?", Pattern.CASE_INSENSITIVE)
@JvmStatic
fun charsetFromContentType(contentType: String?): Charset {
// assume UTF-8 by default [RFC 5445 3.1.4]
var charset = Charsets.UTF_8
var charset = StandardCharsets.UTF_8
contentType?.let {
val m = regexContentTypeCharset.matcher(it)
......
......@@ -101,7 +101,6 @@ class SyncAdapter(
Log.i(Constants.TAG, "iCalendar file completely processed")
}
@Throws(CalendarStorageException::class)
private fun processEvents() {
var errorMessage: String? = null
......
......@@ -14,9 +14,9 @@ import android.content.Context
object CalendarCredentials {
private val PREF_CREDENTIALS = "basicAuth"
private const val PREF_CREDENTIALS = "basicAuth"
@SuppressLint("CommitPrefEdits")
@SuppressLint("ApplySharedPref")
fun getCredentials(context: Context, calendar: LocalCalendar): Pair<String?, String?> {
val prefs = context.getSharedPreferences(PREF_CREDENTIALS, 0)
val url = calendar.url!!
......
......@@ -12,15 +12,12 @@ import android.accounts.Account
import android.content.ContentProviderClient
import android.content.ContentUris
import android.content.ContentValues
import android.database.DatabaseUtils
import android.os.RemoteException
import android.provider.CalendarContract
import android.provider.CalendarContract.Calendars
import android.provider.CalendarContract.Events
import at.bitfire.ical4android.AndroidCalendar
import at.bitfire.ical4android.AndroidCalendarFactory
import at.bitfire.ical4android.CalendarStorageException
import java.io.FileNotFoundException
class LocalCalendar private constructor(
account: Account,
......
......@@ -37,13 +37,13 @@ class LocalEvent: AndroidEvent {
lastModified = event.lastModified?.dateTime?.time ?: 0
}
override fun populateEvent(values: ContentValues) {
super.populateEvent(values)
override fun populateEvent(row: ContentValues) {
super.populateEvent(row)
val event = requireNotNull(event)
event.uid = values.getAsString(CalendarContract.Events._SYNC_ID)
event.uid = row.getAsString(CalendarContract.Events._SYNC_ID)
values.getAsLong(COLUMN_LAST_MODIFIED).let {
row.getAsLong(COLUMN_LAST_MODIFIED).let {
lastModified = it
event.lastModified = LastModified(DateTime(it))
}
......
......@@ -20,8 +20,8 @@ import at.bitfire.icsdroid.R
class AddCalendarActivity: AppCompatActivity() {
companion object {
val EXTRA_TITLE = "title"
val EXTRA_COLOR = "color"
const val EXTRA_TITLE = "title"
const val EXTRA_COLOR = "color"
}
override fun onCreate(inState: Bundle?) {
......
......@@ -31,10 +31,10 @@ class AddCalendarDetailsFragment: Fragment(), TitleColorFragment.OnChangeListene
companion object {
val ARG_INFO = "info"
const val ARG_INFO = "info"
private val STATE_TITLE = "title"
private val STATE_COLOR = "color"
private const val STATE_TITLE = "title"
private const val STATE_COLOR = "color"
fun newInstance(info: ResourceInfo): AddCalendarDetailsFragment {
val frag = AddCalendarDetailsFragment()
......
......@@ -35,7 +35,7 @@ import javax.net.ssl.HttpsURLConnection
class AddCalendarValidationFragment: DialogFragment(), LoaderManager.LoaderCallbacks<ResourceInfo> {
companion object {
val ARG_INFO = "info"
const val ARG_INFO = "info"
fun newInstance(info: ResourceInfo): AddCalendarValidationFragment {
val frag = AddCalendarValidationFragment()
......@@ -142,7 +142,7 @@ class AddCalendarValidationFragment: DialogFragment(), LoaderManager.LoaderCallb
// handle redirects
val location = conn.getHeaderField("Location")
if (info.statusCode/100 == 3 && location != null) {
Log.i(Constants.TAG, "Following redirect to " + location)
Log.i(Constants.TAG, "Following redirect to $location")
url = URL(url, location)
followRedirect = true
if (info.statusCode == HttpURLConnection.HTTP_MOVED_PERM) {
......
......@@ -10,7 +10,6 @@ package at.bitfire.icsdroid.ui
import android.Manifest
import android.annotation.SuppressLint
import android.app.LoaderManager
import android.content.*
import android.content.pm.PackageManager
import android.database.ContentObserver
......@@ -24,7 +23,9 @@ import android.support.design.widget.Snackbar
import android.support.v4.app.ActivityCompat
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.LoaderManager
import android.support.v4.content.ContextCompat
import android.support.v4.content.Loader
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.app.AppCompatActivity
import android.util.Log
......@@ -75,7 +76,7 @@ class CalendarListActivity: AppCompatActivity(), LoaderManager.LoaderCallbacks<L
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED)
loaderManager.initLoader(0, null, this)
supportLoaderManager.initLoader(0, null, this)
else
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR), 0)
......@@ -89,7 +90,7 @@ class CalendarListActivity: AppCompatActivity(), LoaderManager.LoaderCallbacks<L
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
if (grantResults.all { it == PackageManager.PERMISSION_GRANTED })
loaderManager.initLoader(0, null, this)
supportLoaderManager.initLoader(0, null, this)
else
finish()
}
......
......@@ -8,12 +8,12 @@
package at.bitfire.icsdroid.ui
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.view.View;
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.drawable.shapes.OvalShape
import android.util.AttributeSet
import android.view.View
class ColorButton(
context: Context,
......
......@@ -23,9 +23,9 @@ import org.apache.commons.lang3.StringUtils
class CredentialsFragment: Fragment(), CompoundButton.OnCheckedChangeListener, TextWatcher {
companion object {
val ARG_AUTH_REQUIRED = "auth_required"
val ARG_USERNAME = "username"
val ARG_PASSWORD = "password"
const val ARG_AUTH_REQUIRED = "auth_required"
const val ARG_USERNAME = "username"
const val ARG_PASSWORD = "password"
fun newInstance(username: String? = null, password: String? = null): CredentialsFragment {
val frag = CredentialsFragment()
......
......@@ -18,7 +18,7 @@ import at.bitfire.icsdroid.R
class DonateDialogFragment: DialogFragment() {
companion object {
val PREF_NEXT_REMINDER = "nextDonationReminder"
const val PREF_NEXT_REMINDER = "nextDonationReminder"
}
override fun onCreateDialog(savedInstanceState: Bundle?) =
......
......@@ -10,17 +10,18 @@ package at.bitfire.icsdroid.ui
import android.Manifest
import android.annotation.SuppressLint
import android.app.AlertDialog
import android.app.DialogFragment
import android.app.LoaderManager
import android.content.*
import android.content.pm.PackageManager
import android.database.ContentObserver
import android.net.Uri
import android.os.Bundle
import android.provider.CalendarContract
import android.support.v4.app.DialogFragment
import android.support.v4.app.FragmentTransaction
import android.support.v4.app.LoaderManager
import android.support.v4.content.ContextCompat
import android.support.v4.content.Loader
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.Menu
......@@ -35,16 +36,16 @@ import at.bitfire.icsdroid.db.LocalCalendar
import kotlinx.android.synthetic.main.edit_calendar.*
import java.net.URI
class EditCalendarActivity: AppCompatActivity(), LoaderManager.LoaderCallbacks<LocalCalendar?> {
class EditCalendarActivity: AppCompatActivity(), LoaderManager.LoaderCallbacks<LocalCalendar> {
companion object {
private val STATE_TITLE = "title"
private val STATE_COLOR = "color"
private val STATE_SYNC_THIS = "sync_this"
private val STATE_REQUIRE_AUTH = "requires_auth"
private val STATE_USERNAME = "legacyUsername"
private val STATE_PASSWORD = "legacyPassword"
private val STATE_DIRTY = "dirty"
private const val STATE_TITLE = "title"
private const val STATE_COLOR = "color"
private const val STATE_SYNC_THIS = "sync_this"
private const val STATE_REQUIRE_AUTH = "requires_auth"
private const val STATE_USERNAME = "legacyUsername"
private const val STATE_PASSWORD = "legacyPassword"
private const val STATE_DIRTY = "dirty"
}
private var dirty = false // indicates whether title/color have been changed by the user
......@@ -68,7 +69,7 @@ class EditCalendarActivity: AppCompatActivity(), LoaderManager.LoaderCallbacks<L
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED)
// load calendar from provider
loaderManager.initLoader(0, null, this)
supportLoaderManager.initLoader(0, null, this)
else
finish()
}
......@@ -112,7 +113,7 @@ class EditCalendarActivity: AppCompatActivity(), LoaderManager.LoaderCallbacks<L
override fun onBackPressed() {
if (dirty)
fragmentManager.beginTransaction()
supportFragmentManager.beginTransaction()
.add(SaveDismissDialogFragment(), null)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
......@@ -147,7 +148,7 @@ class EditCalendarActivity: AppCompatActivity(), LoaderManager.LoaderCallbacks<L
}
fun onAskDelete(item: MenuItem) {
fragmentManager.beginTransaction()
supportFragmentManager.beginTransaction()
.add(DeleteDialogFragment(), null)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
......@@ -188,7 +189,7 @@ class EditCalendarActivity: AppCompatActivity(), LoaderManager.LoaderCallbacks<L
override fun onCreateLoader(id: Int, args: Bundle?) =
CalendarLoader(this, intent.data)
override fun onLoadFinished(loader: Loader<LocalCalendar?>, calendar: LocalCalendar?) {
override fun onLoadFinished(loader: Loader<LocalCalendar>, calendar: LocalCalendar?) {
if (calendar == null)
// calendar not available (anymore), close activity
finish()
......@@ -241,7 +242,7 @@ class EditCalendarActivity: AppCompatActivity(), LoaderManager.LoaderCallbacks<L
}
}
override fun onLoaderReset(loader: Loader<LocalCalendar?>) {
override fun onLoaderReset(loader: Loader<LocalCalendar>) {
calendar = null
}
......@@ -251,7 +252,7 @@ class EditCalendarActivity: AppCompatActivity(), LoaderManager.LoaderCallbacks<L
class CalendarLoader(
context: Context,
private val uri: Uri
): Loader<LocalCalendar?>(context) {
): Loader<LocalCalendar>(context) {
val TAG = "ICSdroid.Calendar"
private var loaded = false
......@@ -296,7 +297,7 @@ class EditCalendarActivity: AppCompatActivity(), LoaderManager.LoaderCallbacks<L
class SaveDismissDialogFragment: DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?) =
AlertDialog.Builder(activity)
AlertDialog.Builder(requireActivity())
.setTitle(R.string.edit_calendar_unsaved_changes)
.setPositiveButton(R.string.edit_calendar_save, { dialog, _ ->
dialog.dismiss()
......@@ -316,7 +317,7 @@ class EditCalendarActivity: AppCompatActivity(), LoaderManager.LoaderCallbacks<L
class DeleteDialogFragment: DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?) =
AlertDialog.Builder(activity)
AlertDialog.Builder(requireActivity())
.setMessage(R.string.edit_calendar_really_delete)
.setPositiveButton(R.string.edit_calendar_delete, { dialog, _ ->
dialog.dismiss()
......
......@@ -30,7 +30,6 @@ import at.bitfire.icsdroid.Constants
import at.bitfire.icsdroid.R
import kotlinx.android.synthetic.main.app_info_activity.*
import kotlinx.android.synthetic.main.app_info_component.view.*
import org.apache.commons.codec.Charsets
import org.apache.commons.io.IOUtils
import java.io.IOException
import java.nio.charset.StandardCharsets
......@@ -86,8 +85,8 @@ class InfoActivity: AppCompatActivity() {
companion object {
val KEY_POSITION = "position"
val KEY_LICENSE_FILE = "license_file"
const val KEY_POSITION = "position"
const val KEY_LICENSE_FILE = "license_file"
fun instantiate(position: Int): ComponentFragment {
val frag = ComponentFragment()
......
......@@ -16,7 +16,7 @@ import at.bitfire.icsdroid.R
object NotificationUtils {
val CHANNEL_SYNC = "sync"
const val CHANNEL_SYNC = "sync"
fun createChannels(context: Context): NotificationManager {
......
......@@ -23,9 +23,9 @@ import yuku.ambilwarna.AmbilWarnaDialog
class TitleColorFragment: Fragment(), TextWatcher {
companion object {
val ARG_URL = "url"
val ARG_TITLE = "title"
val ARG_COLOR = "color"
const val ARG_URL = "url"
const val ARG_TITLE = "title"
const val ARG_COLOR = "color"
}
private var url: String? = null
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment