replace "this" with "applicationContext" in some cases to avoid leaking activities

This commit is contained in:
tibbi
2017-11-06 23:58:01 +01:00
parent fe3d636996
commit 69fe41829e

View File

@@ -36,10 +36,10 @@ import java.text.SimpleDateFormat
import java.util.* import java.util.*
fun Context.updateWidgets() { fun Context.updateWidgets() {
val widgetsCnt = AppWidgetManager.getInstance(this).getAppWidgetIds(ComponentName(this, MyWidgetMonthlyProvider::class.java)) val widgetsCnt = AppWidgetManager.getInstance(applicationContext).getAppWidgetIds(ComponentName(this, MyWidgetMonthlyProvider::class.java))
if (widgetsCnt.isNotEmpty()) { if (widgetsCnt.isNotEmpty()) {
val ids = intArrayOf(R.xml.widget_monthly_info) val ids = intArrayOf(R.xml.widget_monthly_info)
Intent(this, MyWidgetMonthlyProvider::class.java).apply { Intent(applicationContext, MyWidgetMonthlyProvider::class.java).apply {
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids) putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
sendBroadcast(this) sendBroadcast(this)
@@ -50,10 +50,10 @@ fun Context.updateWidgets() {
} }
fun Context.updateListWidget() { fun Context.updateListWidget() {
val widgetsCnt = AppWidgetManager.getInstance(this).getAppWidgetIds(ComponentName(this, MyWidgetListProvider::class.java)) val widgetsCnt = AppWidgetManager.getInstance(applicationContext).getAppWidgetIds(ComponentName(this, MyWidgetListProvider::class.java))
if (widgetsCnt.isNotEmpty()) { if (widgetsCnt.isNotEmpty()) {
val ids = intArrayOf(R.xml.widget_list_info) val ids = intArrayOf(R.xml.widget_list_info)
Intent(this, MyWidgetListProvider::class.java).apply { Intent(applicationContext, MyWidgetListProvider::class.java).apply {
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids) putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
sendBroadcast(this) sendBroadcast(this)
@@ -93,7 +93,7 @@ fun Context.scheduleEventIn(notifTS: Long, event: Event) {
if (notifTS < System.currentTimeMillis()) if (notifTS < System.currentTimeMillis())
return return
val pendingIntent = getNotificationIntent(this, event) val pendingIntent = getNotificationIntent(applicationContext, event)
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
if (isKitkatPlus()) if (isKitkatPlus())
@@ -103,8 +103,8 @@ fun Context.scheduleEventIn(notifTS: Long, event: Event) {
} }
fun Context.cancelNotification(id: Int) { fun Context.cancelNotification(id: Int) {
val intent = Intent(this, NotificationReceiver::class.java) val intent = Intent(applicationContext, NotificationReceiver::class.java)
PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT).cancel() PendingIntent.getBroadcast(applicationContext, id, intent, PendingIntent.FLAG_UPDATE_CURRENT).cancel()
} }
private fun getNotificationIntent(context: Context, event: Event): PendingIntent { private fun getNotificationIntent(context: Context, event: Event): PendingIntent {
@@ -165,12 +165,12 @@ fun Context.notifyRunningEvents() {
} }
fun Context.notifyEvent(event: Event) { fun Context.notifyEvent(event: Event) {
val pendingIntent = getPendingIntent(this, event) val pendingIntent = getPendingIntent(applicationContext, event)
val startTime = Formatter.getTimeFromTS(this, event.startTS) val startTime = Formatter.getTimeFromTS(applicationContext, event.startTS)
val endTime = Formatter.getTimeFromTS(this, event.endTS) val endTime = Formatter.getTimeFromTS(applicationContext, event.endTS)
val timeRange = if (event.getIsAllDay()) getString(R.string.all_day) else getFormattedEventTime(startTime, endTime) val timeRange = if (event.getIsAllDay()) getString(R.string.all_day) else getFormattedEventTime(startTime, endTime)
val descriptionOrLocation = if (config.replaceDescription) event.location else event.description val descriptionOrLocation = if (config.replaceDescription) event.location else event.description
val notification = getNotification(this, pendingIntent, event, "$timeRange $descriptionOrLocation") val notification = getNotification(applicationContext, pendingIntent, event, "$timeRange $descriptionOrLocation")
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(event.id, notification) notificationManager.notify(event.id, notification)
} }
@@ -231,20 +231,20 @@ fun Context.getNewEventTimestampFromCode(dayCode: String): Int {
fun Context.getCurrentOffset() = SimpleDateFormat("Z", Locale.getDefault()).format(Date()) fun Context.getCurrentOffset() = SimpleDateFormat("Z", Locale.getDefault()).format(Date())
fun Context.getSyncedCalDAVCalendars() = CalDAVHandler(this).getCalDAVCalendars(null, config.caldavSyncedCalendarIDs) fun Context.getSyncedCalDAVCalendars() = CalDAVHandler(applicationContext).getCalDAVCalendars(null, config.caldavSyncedCalendarIDs)
fun Context.recheckCalDAVCalendars(callback: () -> Unit) { fun Context.recheckCalDAVCalendars(callback: () -> Unit) {
if (config.caldavSync) { if (config.caldavSync) {
Thread({ Thread({
CalDAVHandler(this).refreshCalendars(null, callback) CalDAVHandler(applicationContext).refreshCalendars(null, callback)
updateWidgets() updateWidgets()
}).start() }).start()
} }
} }
fun Context.scheduleCalDAVSync(activate: Boolean) { fun Context.scheduleCalDAVSync(activate: Boolean) {
val syncIntent = Intent(this, CalDAVSyncReceiver::class.java) val syncIntent = Intent(applicationContext, CalDAVSyncReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(this, 0, syncIntent, PendingIntent.FLAG_UPDATE_CURRENT) val pendingIntent = PendingIntent.getBroadcast(applicationContext, 0, syncIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val alarm = getSystemService(Context.ALARM_SERVICE) as AlarmManager val alarm = getSystemService(Context.ALARM_SERVICE) as AlarmManager
if (activate) { if (activate) {
@@ -264,7 +264,7 @@ fun Context.addDayNumber(rawTextColor: Int, day: DayMonthly, linearLayout: Linea
if (!day.isThisMonth) if (!day.isThisMonth)
textColor = textColor.adjustAlpha(LOW_ALPHA) textColor = textColor.adjustAlpha(LOW_ALPHA)
(View.inflate(this, R.layout.day_monthly_number_view, null) as TextView).apply { (View.inflate(applicationContext, R.layout.day_monthly_number_view, null) as TextView).apply {
setTextColor(textColor) setTextColor(textColor)
text = day.value.toString() text = day.value.toString()
gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
@@ -306,7 +306,7 @@ fun Context.addDayEvents(day: DayMonthly, linearLayout: LinearLayout, res: Resou
textColor = textColor.adjustAlpha(0.25f) textColor = textColor.adjustAlpha(0.25f)
} }
(View.inflate(this, R.layout.day_monthly_event_view, null) as TextView).apply { (View.inflate(applicationContext, R.layout.day_monthly_event_view, null) as TextView).apply {
setTextColor(textColor) setTextColor(textColor)
text = it.title.replace(" ", "\u00A0") // allow word break by char text = it.title.replace(" ", "\u00A0") // allow word break by char
background = backgroundDrawable background = backgroundDrawable