convert receivers to kotlin

This commit is contained in:
tibbi 2016-11-17 18:29:25 +01:00
parent ab6fd0b9cc
commit e82c056289
5 changed files with 79 additions and 88 deletions

View File

@ -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);
}

View File

@ -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<Event> events = new DBHelper(context).getEventsAtReboot();
for (Event event : events) {
Utils.scheduleNextEvent(context, event);
}
}
}

View File

@ -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();
}
}

View File

@ -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)
}
}
}

View File

@ -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()
}
}