|
|
@ -1,11 +1,12 @@
|
|
|
|
package org.isoron.uhabits;
|
|
|
|
package org.isoron.uhabits;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
import org.isoron.helpers.DateHelper;
|
|
|
|
|
|
|
|
import org.isoron.uhabits.models.Habit;
|
|
|
|
import org.isoron.uhabits.models.Habit;
|
|
|
|
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.Activity;
|
|
|
|
|
|
|
|
import android.app.AlarmManager;
|
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.app.PendingIntent;
|
|
|
@ -20,22 +21,77 @@ import android.util.Log;
|
|
|
|
|
|
|
|
|
|
|
|
public class ReminderAlarmReceiver extends BroadcastReceiver
|
|
|
|
public class ReminderAlarmReceiver extends BroadcastReceiver
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String ACTION_CHECK = "org.isoron.uhabits.ACTION_CHECK";
|
|
|
|
|
|
|
|
public static String ACTION_DISMISS = "org.isoron.uhabits.ACTION_DISMISS";
|
|
|
|
|
|
|
|
public static String ACTION_REMIND = "org.isoron.uhabits.ACTION_REMIND";
|
|
|
|
|
|
|
|
public static String ACTION_REMOVE_REMINDER = "org.isoron.uhabits.ACTION_REMOVE_REMINDER";
|
|
|
|
|
|
|
|
public static String ACTION_SNOOZE = "org.isoron.uhabits.ACTION_SNOOZE";
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent)
|
|
|
|
public void onReceive(Context context, Intent intent)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
createNotification(context, intent.getData(), intent.getDataString());
|
|
|
|
String action = intent.getAction();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(action.equals(ACTION_REMIND))
|
|
|
|
|
|
|
|
createNotification(context, intent.getData());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else if(action.equals(ACTION_DISMISS))
|
|
|
|
|
|
|
|
dismissAllHabits();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else if(action.equals(ACTION_CHECK))
|
|
|
|
|
|
|
|
checkHabit(context, intent.getData());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else if(action.equals(ACTION_SNOOZE))
|
|
|
|
|
|
|
|
snoozeHabit(context, intent.getData());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void snoozeHabit(Context context, Uri data)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int delayMinutes = 15;
|
|
|
|
|
|
|
|
Habit habit = Habit.get(ContentUris.parseId(data));
|
|
|
|
|
|
|
|
MainActivity.createReminderAlarm(context, habit, new Date().getTime() + delayMinutes * 1000);
|
|
|
|
|
|
|
|
dismissNotification(context);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void checkHabit(Context context, Uri data)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Habit habit = Habit.get(ContentUris.parseId(data));
|
|
|
|
|
|
|
|
habit.toggleRepetitionToday();
|
|
|
|
|
|
|
|
habit.save();
|
|
|
|
|
|
|
|
dismissNotification(context);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void dismissAllHabits()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
for(Habit h : Habit.getHighlightedHabits())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.d("Alarm", String.format("Removing highlight from: %s", h.name));
|
|
|
|
|
|
|
|
h.highlight = 0;
|
|
|
|
|
|
|
|
h.save();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void dismissNotification(Context context)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
NotificationManager notificationManager = (NotificationManager) context
|
|
|
|
|
|
|
|
.getSystemService(Activity.NOTIFICATION_SERVICE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
notificationManager.cancel(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void createNotification(Context context, Uri data, String text)
|
|
|
|
private void createNotification(Context context, Uri data)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Log.d("Alarm", "Alarm received!");
|
|
|
|
Log.d("Alarm", "Alarm received!");
|
|
|
|
|
|
|
|
|
|
|
|
Habit habit = Habit.get(ContentUris.parseId(data));
|
|
|
|
Habit habit = Habit.get(ContentUris.parseId(data));
|
|
|
|
|
|
|
|
|
|
|
|
// Check if user already did the habit repetition
|
|
|
|
if(habit.hasImplicitRepToday())
|
|
|
|
if(habit.hasRep(DateHelper.getStartOfDay(DateHelper.getLocalTime())))
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.d("Alarm", String.format("(%s) has implicit rep today", habit.name));
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Log.d("Alarm", String.format("Applying highlight: %s", habit.name));
|
|
|
|
Log.d("Alarm", String.format("Applying highlight: %s", habit.name));
|
|
|
|
habit.highlight = 1;
|
|
|
|
habit.highlight = 1;
|
|
|
@ -49,27 +105,38 @@ public class ReminderAlarmReceiver extends BroadcastReceiver
|
|
|
|
contentIntent.setData(data);
|
|
|
|
contentIntent.setData(data);
|
|
|
|
PendingIntent contentPendingIntent = PendingIntent.getActivity(context, 0, contentIntent, 0);
|
|
|
|
PendingIntent contentPendingIntent = PendingIntent.getActivity(context, 0, contentIntent, 0);
|
|
|
|
|
|
|
|
|
|
|
|
Intent deleteIntent = new Intent(context, ReminderAlarmDismissReceiver.class);
|
|
|
|
Intent deleteIntent = new Intent(context, ReminderAlarmReceiver.class);
|
|
|
|
|
|
|
|
deleteIntent.setAction(ACTION_DISMISS);
|
|
|
|
PendingIntent deletePendingIntent = PendingIntent.getBroadcast(context, 0, deleteIntent, 0);
|
|
|
|
PendingIntent deletePendingIntent = PendingIntent.getBroadcast(context, 0, deleteIntent, 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Intent checkIntent = new Intent(context, ReminderAlarmReceiver.class);
|
|
|
|
|
|
|
|
checkIntent.setData(data);
|
|
|
|
|
|
|
|
checkIntent.setAction(ACTION_CHECK);
|
|
|
|
|
|
|
|
PendingIntent checkIntentPending = PendingIntent.getBroadcast(context, 0, checkIntent, 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Intent snoozeIntent = new Intent(context, ReminderAlarmReceiver.class);
|
|
|
|
|
|
|
|
snoozeIntent.setData(data);
|
|
|
|
|
|
|
|
snoozeIntent.setAction(ACTION_SNOOZE);
|
|
|
|
|
|
|
|
PendingIntent snoozeIntentPending = PendingIntent.getBroadcast(context, 0, snoozeIntent, 0);
|
|
|
|
|
|
|
|
|
|
|
|
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
|
|
|
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
|
|
|
|
|
|
|
|
|
|
|
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
|
|
|
|
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
|
|
|
|
inboxStyle.setBigContentTitle("Habit Reminder:");
|
|
|
|
inboxStyle.setBigContentTitle("Habit Reminder:");
|
|
|
|
List<Habit> pendingHabits = Habit.getHighlightedHabits();
|
|
|
|
List<Habit> pendingHabits = Habit.getHighlightedHabits();
|
|
|
|
|
|
|
|
StringBuffer contentText = new StringBuffer();
|
|
|
|
for(Habit h : pendingHabits)
|
|
|
|
for(Habit h : pendingHabits)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(h.hasRep(DateHelper.getStartOfDay(DateHelper.getLocalTime())))
|
|
|
|
if(h.hasImplicitRepToday())
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
inboxStyle.addLine(h.name);
|
|
|
|
inboxStyle.addLine(h.name);
|
|
|
|
|
|
|
|
if(contentText.length() > 0)
|
|
|
|
|
|
|
|
contentText.append(", ");
|
|
|
|
|
|
|
|
contentText.append(h.name);
|
|
|
|
Log.d("Alarm", String.format("Found highlighted: %s", h.name));
|
|
|
|
Log.d("Alarm", String.format("Found highlighted: %s", h.name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String contentText = habit.name;
|
|
|
|
|
|
|
|
if(pendingHabits.size() > 1) {
|
|
|
|
|
|
|
|
contentText = String.format("%d pending habits.", pendingHabits.size());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Notification notification =
|
|
|
|
Notification notification =
|
|
|
|
new NotificationCompat.Builder(context)
|
|
|
|
new NotificationCompat.Builder(context)
|
|
|
|
.setSmallIcon(R.drawable.ic_notification)
|
|
|
|
.setSmallIcon(R.drawable.ic_notification)
|
|
|
@ -77,6 +144,8 @@ public class ReminderAlarmReceiver extends BroadcastReceiver
|
|
|
|
.setContentText(contentText)
|
|
|
|
.setContentText(contentText)
|
|
|
|
.setContentIntent(contentPendingIntent)
|
|
|
|
.setContentIntent(contentPendingIntent)
|
|
|
|
.setDeleteIntent(deletePendingIntent)
|
|
|
|
.setDeleteIntent(deletePendingIntent)
|
|
|
|
|
|
|
|
.addAction(R.drawable.ic_action_check, "Check", checkIntentPending)
|
|
|
|
|
|
|
|
.addAction(R.drawable.ic_action_snooze, "Later", snoozeIntentPending)
|
|
|
|
.setSound(soundUri)
|
|
|
|
.setSound(soundUri)
|
|
|
|
.setStyle(inboxStyle)
|
|
|
|
.setStyle(inboxStyle)
|
|
|
|
.build();
|
|
|
|
.build();
|
|
|
|