From e82c05628966ab8b99766f2dbb1783ea25d404bd Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 17 Nov 2016 18:29:25 +0100 Subject: [PATCH] convert receivers to kotlin --- .../com/simplemobiletools/calendar/Utils.java | 2 +- .../receivers/BootCompletedReceiver.java | 22 ------- .../receivers/NotificationReceiver.java | 65 ------------------- .../receivers/BootCompletedReceiver.kt | 17 +++++ .../receivers/NotificationReceiver.kt | 61 +++++++++++++++++ 5 files changed, 79 insertions(+), 88 deletions(-) delete mode 100644 app/src/main/java/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.java delete mode 100644 app/src/main/java/com/simplemobiletools/calendar/receivers/NotificationReceiver.java create mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/receivers/NotificationReceiver.kt diff --git a/app/src/main/java/com/simplemobiletools/calendar/Utils.java b/app/src/main/java/com/simplemobiletools/calendar/Utils.java index 1ac6b22a9..71e6e0d6c 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/Utils.java +++ b/app/src/main/java/com/simplemobiletools/calendar/Utils.java @@ -73,7 +73,7 @@ public class Utils { private static PendingIntent getNotificationIntent(Context context, int eventId) { final Intent intent = new Intent(context, NotificationReceiver.class); - intent.putExtra(NotificationReceiver.EVENT_ID, eventId); + intent.putExtra(NotificationReceiver.Companion.getEVENT_ID(), eventId); return PendingIntent.getBroadcast(context, eventId, intent, PendingIntent.FLAG_UPDATE_CURRENT); } diff --git a/app/src/main/java/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.java b/app/src/main/java/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.java deleted file mode 100644 index 046af31d7..000000000 --- a/app/src/main/java/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.simplemobiletools.calendar.receivers; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; - -import com.simplemobiletools.calendar.DBHelper; -import com.simplemobiletools.calendar.Utils; -import com.simplemobiletools.calendar.models.Event; - -import java.util.List; - -public class BootCompletedReceiver extends BroadcastReceiver { - - @Override - public void onReceive(Context context, Intent arg1) { - final List events = new DBHelper(context).getEventsAtReboot(); - for (Event event : events) { - Utils.scheduleNextEvent(context, event); - } - } -} diff --git a/app/src/main/java/com/simplemobiletools/calendar/receivers/NotificationReceiver.java b/app/src/main/java/com/simplemobiletools/calendar/receivers/NotificationReceiver.java deleted file mode 100644 index a6a12889d..000000000 --- a/app/src/main/java/com/simplemobiletools/calendar/receivers/NotificationReceiver.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.simplemobiletools.calendar.receivers; - -import android.app.Notification; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.media.RingtoneManager; -import android.net.Uri; - -import com.simplemobiletools.calendar.Constants; -import com.simplemobiletools.calendar.DBHelper; -import com.simplemobiletools.calendar.Formatter; -import com.simplemobiletools.calendar.R; -import com.simplemobiletools.calendar.Utils; -import com.simplemobiletools.calendar.activities.EventActivity; -import com.simplemobiletools.calendar.models.Event; - -public class NotificationReceiver extends BroadcastReceiver { - public static String EVENT_ID = "event_id"; - - public void onReceive(Context context, Intent intent) { - final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - int id = intent.getIntExtra(EVENT_ID, -1); - if (id == -1) - return; - - final Event event = new DBHelper(context).getEvent(id); - if (event == null || event.getReminderMinutes() == -1) - return; - - final PendingIntent pendingIntent = getPendingIntent(context, event); - final String startTime = Formatter.INSTANCE.getTime(event.getStartTS()); - final String endTime = Formatter.INSTANCE.getTime(event.getEndTS()); - final String title = event.getTitle(); - final Notification notification = getNotification(context, pendingIntent, getEventTime(startTime, endTime) + " " + title); - notificationManager.notify(id, notification); - - if (event.getRepeatInterval() != 0) - Utils.scheduleNextEvent(context, event); - } - - private String getEventTime(String startTime, String endTime) { - return startTime.equals(endTime) ? startTime : (startTime + " - " + endTime); - } - - private PendingIntent getPendingIntent(Context context, Event event) { - final Intent intent = new Intent(context, EventActivity.class); - intent.putExtra(Constants.EVENT_ID, event); - return PendingIntent.getActivity(context, event.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT); - } - - private Notification getNotification(Context context, PendingIntent pendingIntent, String content) { - final Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); - final Notification.Builder builder = new Notification.Builder(context); - builder.setContentTitle(context.getResources().getString(R.string.app_name)); - builder.setContentText(content); - builder.setSmallIcon(R.mipmap.calendar); - builder.setContentIntent(pendingIntent); - builder.setAutoCancel(true); - builder.setSound(soundUri); - return builder.build(); - } -} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.kt new file mode 100644 index 000000000..6250e1163 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.kt @@ -0,0 +1,17 @@ +package com.simplemobiletools.calendar.receivers + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import com.simplemobiletools.calendar.DBHelper +import com.simplemobiletools.calendar.Utils + +class BootCompletedReceiver : BroadcastReceiver() { + + override fun onReceive(context: Context, arg1: Intent) { + val events = DBHelper(context).getEventsAtReboot() + for (event in events) { + Utils.scheduleNextEvent(context, event) + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/NotificationReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/NotificationReceiver.kt new file mode 100644 index 000000000..33da1dbc1 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/NotificationReceiver.kt @@ -0,0 +1,61 @@ +package com.simplemobiletools.calendar.receivers + +import android.app.Notification +import android.app.NotificationManager +import android.app.PendingIntent +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.media.RingtoneManager +import com.simplemobiletools.calendar.* +import com.simplemobiletools.calendar.activities.EventActivity +import com.simplemobiletools.calendar.models.Event + +class NotificationReceiver : BroadcastReceiver() { + companion object { + var EVENT_ID = "event_id" + } + + override fun onReceive(context: Context, intent: Intent) { + val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + val id = intent.getIntExtra(EVENT_ID, -1) + if (id == -1) + return + + val event = DBHelper(context).getEvent(id) + if (event == null || event.reminderMinutes == -1) + return + + val pendingIntent = getPendingIntent(context, event) + val startTime = Formatter.getTime(event.startTS) + val endTime = Formatter.getTime(event.endTS) + val title = event.title + val notification = getNotification(context, pendingIntent, getEventTime(startTime, endTime) + " " + title) + notificationManager.notify(id, notification) + + if (event.repeatInterval != 0) + Utils.scheduleNextEvent(context, event) + } + + private fun getEventTime(startTime: String, endTime: String): String { + return if (startTime == endTime) startTime else startTime + " - " + endTime + } + + private fun getPendingIntent(context: Context, event: Event): PendingIntent { + val intent = Intent(context, EventActivity::class.java) + intent.putExtra(Constants.EVENT_ID, event) + return PendingIntent.getActivity(context, event.id, intent, PendingIntent.FLAG_UPDATE_CURRENT) + } + + private fun getNotification(context: Context, pendingIntent: PendingIntent, content: String): Notification { + val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) + return Notification.Builder(context) + .setContentTitle(context.resources.getString(R.string.app_name)) + .setContentText(content) + .setSmallIcon(R.mipmap.calendar) + .setContentIntent(pendingIntent) + .setAutoCancel(true) + .setSound(soundUri) + .build() + } +}