mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Use separate broadcast actions for widgets and notifications
This commit is contained in:
@@ -54,6 +54,9 @@ public class HabitBroadcastReceiver extends BroadcastReceiver
|
||||
public static final String ACTION_SNOOZE =
|
||||
"org.isoron.uhabits.ACTION_SNOOZE";
|
||||
|
||||
public static final String ACTION_TOGGLE =
|
||||
"org.isoron.uhabits.ACTION_TOGGLE";
|
||||
|
||||
@Inject
|
||||
HabitList habits;
|
||||
|
||||
@@ -81,7 +84,11 @@ public class HabitBroadcastReceiver extends BroadcastReceiver
|
||||
break;
|
||||
|
||||
case ACTION_CHECK:
|
||||
checkHabit(context, intent);
|
||||
addRepetition(context, intent);
|
||||
break;
|
||||
|
||||
case ACTION_TOGGLE:
|
||||
toggleRepetition(context, intent);
|
||||
break;
|
||||
|
||||
case ACTION_SNOOZE:
|
||||
@@ -94,7 +101,9 @@ public class HabitBroadcastReceiver extends BroadcastReceiver
|
||||
}
|
||||
}
|
||||
|
||||
private void checkHabit(Context context, Intent intent)
|
||||
private void addOrRemoveRepetition(Context context,
|
||||
Intent intent,
|
||||
boolean abortIfExists)
|
||||
{
|
||||
Uri data = intent.getData();
|
||||
long today = DateUtils.getStartOfToday();
|
||||
@@ -107,11 +116,10 @@ public class HabitBroadcastReceiver extends BroadcastReceiver
|
||||
if (habit == null) return;
|
||||
|
||||
Repetition rep = habit.getRepetitions().getByTimestamp(timestamp);
|
||||
if (rep != null) return;
|
||||
if (abortIfExists && rep != null) return;
|
||||
|
||||
ToggleRepetitionCommand command =
|
||||
new ToggleRepetitionCommand(habit, timestamp);
|
||||
commandRunner.execute(command, habitId);
|
||||
commandRunner.execute(new ToggleRepetitionCommand(habit, timestamp),
|
||||
habitId);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -119,6 +127,11 @@ public class HabitBroadcastReceiver extends BroadcastReceiver
|
||||
}
|
||||
}
|
||||
|
||||
private void addRepetition(Context context, Intent intent)
|
||||
{
|
||||
addOrRemoveRepetition(context, intent, true);
|
||||
}
|
||||
|
||||
private boolean checkWeekday(Intent intent, Habit habit)
|
||||
{
|
||||
if (!habit.hasReminder()) return false;
|
||||
@@ -172,8 +185,8 @@ public class HabitBroadcastReceiver extends BroadcastReceiver
|
||||
dismissPendingIntent =
|
||||
HabitPendingIntents.dismissNotification(context);
|
||||
PendingIntent checkIntentPending =
|
||||
HabitPendingIntents.toggleCheckmark(context, habit,
|
||||
timestamp, 1);
|
||||
HabitPendingIntents.addCheckmark(context, habit,
|
||||
timestamp);
|
||||
PendingIntent snoozeIntentPending =
|
||||
HabitPendingIntents.snoozeNotification(context, habit);
|
||||
|
||||
@@ -247,4 +260,9 @@ public class HabitBroadcastReceiver extends BroadcastReceiver
|
||||
new Date().getTime() + delayMinutes * 60 * 1000);
|
||||
dismissNotification(context, habitId);
|
||||
}
|
||||
|
||||
private void toggleRepetition(Context context, Intent intent)
|
||||
{
|
||||
addOrRemoveRepetition(context, intent, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,19 @@ public abstract class HabitPendingIntents
|
||||
private static final String BASE_URL =
|
||||
"content://org.isoron.uhabits/habit/";
|
||||
|
||||
public static PendingIntent addCheckmark(Context context,
|
||||
Habit habit,
|
||||
Long timestamp)
|
||||
{
|
||||
Uri data = habit.getUri();
|
||||
Intent checkIntent = new Intent(context, HabitBroadcastReceiver.class);
|
||||
checkIntent.setData(data);
|
||||
checkIntent.setAction(HabitBroadcastReceiver.ACTION_CHECK);
|
||||
if (timestamp != null) checkIntent.putExtra("timestamp", timestamp);
|
||||
return PendingIntent.getBroadcast(context, 1, checkIntent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
}
|
||||
|
||||
public static PendingIntent dismissNotification(Context context)
|
||||
{
|
||||
Intent deleteIntent = new Intent(context, HabitBroadcastReceiver.class);
|
||||
@@ -52,15 +65,14 @@ public abstract class HabitPendingIntents
|
||||
|
||||
public static PendingIntent toggleCheckmark(Context context,
|
||||
Habit habit,
|
||||
Long timestamp,
|
||||
int requestCode)
|
||||
Long timestamp)
|
||||
{
|
||||
Uri data = habit.getUri();
|
||||
Intent checkIntent = new Intent(context, HabitBroadcastReceiver.class);
|
||||
checkIntent.setData(data);
|
||||
checkIntent.setAction(HabitBroadcastReceiver.ACTION_CHECK);
|
||||
checkIntent.setAction(HabitBroadcastReceiver.ACTION_TOGGLE);
|
||||
if (timestamp != null) checkIntent.putExtra("timestamp", timestamp);
|
||||
return PendingIntent.getBroadcast(context, requestCode, checkIntent,
|
||||
return PendingIntent.getBroadcast(context, 2, checkIntent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ public class CheckmarkWidget extends BaseWidget
|
||||
@Override
|
||||
public PendingIntent getOnClickPendingIntent(Context context)
|
||||
{
|
||||
return HabitPendingIntents.toggleCheckmark(context, habit, null, 2);
|
||||
return HabitPendingIntents.toggleCheckmark(context, habit, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user