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