|
|
@ -29,16 +29,20 @@ import org.isoron.uhabits.*;
|
|
|
|
import org.isoron.uhabits.commands.*;
|
|
|
|
import org.isoron.uhabits.commands.*;
|
|
|
|
import org.isoron.uhabits.intents.*;
|
|
|
|
import org.isoron.uhabits.intents.*;
|
|
|
|
import org.isoron.uhabits.models.*;
|
|
|
|
import org.isoron.uhabits.models.*;
|
|
|
|
|
|
|
|
import org.isoron.uhabits.preferences.*;
|
|
|
|
import org.isoron.uhabits.tasks.*;
|
|
|
|
import org.isoron.uhabits.tasks.*;
|
|
|
|
import org.isoron.uhabits.utils.*;
|
|
|
|
import org.isoron.uhabits.utils.*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.inject.*;
|
|
|
|
import javax.inject.*;
|
|
|
|
|
|
|
|
|
|
|
|
import static android.graphics.BitmapFactory.*;
|
|
|
|
import static android.graphics.BitmapFactory.*;
|
|
|
|
import static org.isoron.uhabits.utils.RingtoneUtils.*;
|
|
|
|
import static org.isoron.uhabits.utils.RingtoneUtils.*;
|
|
|
|
|
|
|
|
|
|
|
|
@AppScope
|
|
|
|
@AppScope
|
|
|
|
public class NotificationTray implements CommandRunner.Listener
|
|
|
|
public class NotificationTray
|
|
|
|
|
|
|
|
implements CommandRunner.Listener, Preferences.Listener
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@NonNull
|
|
|
|
@NonNull
|
|
|
|
private final Context context;
|
|
|
|
private final Context context;
|
|
|
@ -49,32 +53,42 @@ public class NotificationTray implements CommandRunner.Listener
|
|
|
|
@NonNull
|
|
|
|
@NonNull
|
|
|
|
private final PendingIntentFactory pendingIntents;
|
|
|
|
private final PendingIntentFactory pendingIntents;
|
|
|
|
|
|
|
|
|
|
|
|
private CommandRunner commandRunner;
|
|
|
|
@NonNull
|
|
|
|
|
|
|
|
private final CommandRunner commandRunner;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
|
|
|
|
private final Preferences preferences;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
|
|
|
|
private final HashMap<Habit, NotificationData> active;
|
|
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
@Inject
|
|
|
|
public NotificationTray(@AppContext @NonNull Context context,
|
|
|
|
public NotificationTray(@AppContext @NonNull Context context,
|
|
|
|
@NonNull TaskRunner taskRunner,
|
|
|
|
@NonNull TaskRunner taskRunner,
|
|
|
|
@NonNull PendingIntentFactory pendingIntents,
|
|
|
|
@NonNull PendingIntentFactory pendingIntents,
|
|
|
|
@NonNull CommandRunner commandRunner)
|
|
|
|
@NonNull CommandRunner commandRunner,
|
|
|
|
|
|
|
|
@NonNull Preferences preferences)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
this.context = context;
|
|
|
|
this.context = context;
|
|
|
|
this.taskRunner = taskRunner;
|
|
|
|
this.taskRunner = taskRunner;
|
|
|
|
this.pendingIntents = pendingIntents;
|
|
|
|
this.pendingIntents = pendingIntents;
|
|
|
|
this.commandRunner = commandRunner;
|
|
|
|
this.commandRunner = commandRunner;
|
|
|
|
|
|
|
|
this.preferences = preferences;
|
|
|
|
|
|
|
|
this.active = new HashMap<>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void cancel(@NonNull Habit habit)
|
|
|
|
public void cancel(@NonNull Habit habit)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int notificationId = getNotificationId(habit);
|
|
|
|
int notificationId = getNotificationId(habit);
|
|
|
|
NotificationManagerCompat.from(context).cancel(notificationId);
|
|
|
|
NotificationManagerCompat.from(context).cancel(notificationId);
|
|
|
|
|
|
|
|
active.remove(habit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onCommandExecuted(@NonNull Command command,
|
|
|
|
public void onCommandExecuted(@NonNull Command command,
|
|
|
|
@Nullable Long refreshKey)
|
|
|
|
@Nullable Long refreshKey)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!(command instanceof ToggleRepetitionCommand))
|
|
|
|
if (!(command instanceof ToggleRepetitionCommand)) return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ToggleRepetitionCommand toggleCommand =
|
|
|
|
ToggleRepetitionCommand toggleCommand =
|
|
|
|
(ToggleRepetitionCommand) command;
|
|
|
|
(ToggleRepetitionCommand) command;
|
|
|
@ -84,20 +98,29 @@ public class NotificationTray implements CommandRunner.Listener
|
|
|
|
cancel(habit);
|
|
|
|
cancel(habit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void onNotificationsChanged()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
reshowAll();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void show(@NonNull Habit habit, long timestamp, long reminderTime)
|
|
|
|
public void show(@NonNull Habit habit, long timestamp, long reminderTime)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
taskRunner.execute(
|
|
|
|
NotificationData data = new NotificationData(timestamp, reminderTime);
|
|
|
|
new ShowNotificationTask(habit, timestamp, reminderTime));
|
|
|
|
active.put(habit, data);
|
|
|
|
|
|
|
|
taskRunner.execute(new ShowNotificationTask(habit, data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void startListening()
|
|
|
|
public void startListening()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
commandRunner.addListener(this);
|
|
|
|
commandRunner.addListener(this);
|
|
|
|
|
|
|
|
preferences.addListener(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void stopListening()
|
|
|
|
public void stopListening()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
commandRunner.removeListener(this);
|
|
|
|
commandRunner.removeListener(this);
|
|
|
|
|
|
|
|
preferences.removeListener(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private int getNotificationId(Habit habit)
|
|
|
|
private int getNotificationId(Habit habit)
|
|
|
@ -107,6 +130,28 @@ public class NotificationTray implements CommandRunner.Listener
|
|
|
|
return (int) (id % Integer.MAX_VALUE);
|
|
|
|
return (int) (id % Integer.MAX_VALUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void reshowAll()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
for (Habit habit : active.keySet())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
NotificationData data = active.get(habit);
|
|
|
|
|
|
|
|
taskRunner.execute(new ShowNotificationTask(habit, data));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NotificationData
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public final long timestamp;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public final long reminderTime;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public NotificationData(long timestamp, long reminderTime)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
this.timestamp = timestamp;
|
|
|
|
|
|
|
|
this.reminderTime = reminderTime;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private class ShowNotificationTask implements Task
|
|
|
|
private class ShowNotificationTask implements Task
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int todayValue;
|
|
|
|
int todayValue;
|
|
|
@ -117,13 +162,11 @@ public class NotificationTray implements CommandRunner.Listener
|
|
|
|
|
|
|
|
|
|
|
|
private final long reminderTime;
|
|
|
|
private final long reminderTime;
|
|
|
|
|
|
|
|
|
|
|
|
public ShowNotificationTask(Habit habit,
|
|
|
|
public ShowNotificationTask(Habit habit, NotificationData data)
|
|
|
|
long timestamp,
|
|
|
|
|
|
|
|
long reminderTime)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
this.habit = habit;
|
|
|
|
this.habit = habit;
|
|
|
|
this.timestamp = timestamp;
|
|
|
|
this.timestamp = data.timestamp;
|
|
|
|
this.reminderTime = reminderTime;
|
|
|
|
this.reminderTime = data.reminderTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -148,7 +191,7 @@ public class NotificationTray implements CommandRunner.Listener
|
|
|
|
.setContentTitle(habit.getName())
|
|
|
|
.setContentTitle(habit.getName())
|
|
|
|
.setContentText(habit.getDescription())
|
|
|
|
.setContentText(habit.getDescription())
|
|
|
|
.setContentIntent(pendingIntents.showHabit(habit))
|
|
|
|
.setContentIntent(pendingIntents.showHabit(habit))
|
|
|
|
.setDeleteIntent(pendingIntents.dismissNotification())
|
|
|
|
.setDeleteIntent(pendingIntents.dismissNotification(habit))
|
|
|
|
.addAction(R.drawable.ic_action_check,
|
|
|
|
.addAction(R.drawable.ic_action_check,
|
|
|
|
context.getString(R.string.check),
|
|
|
|
context.getString(R.string.check),
|
|
|
|
pendingIntents.addCheckmark(habit, timestamp))
|
|
|
|
pendingIntents.addCheckmark(habit, timestamp))
|
|
|
@ -159,6 +202,7 @@ public class NotificationTray implements CommandRunner.Listener
|
|
|
|
.extend(wearableExtender)
|
|
|
|
.extend(wearableExtender)
|
|
|
|
.setWhen(reminderTime)
|
|
|
|
.setWhen(reminderTime)
|
|
|
|
.setShowWhen(true)
|
|
|
|
.setShowWhen(true)
|
|
|
|
|
|
|
|
.setOngoing(preferences.shouldMakeNotificationsSticky())
|
|
|
|
.build();
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
|
|
NotificationManager notificationManager =
|
|
|
|
NotificationManager notificationManager =
|
|
|
@ -179,6 +223,5 @@ public class NotificationTray implements CommandRunner.Listener
|
|
|
|
|
|
|
|
|
|
|
|
return reminderDays[weekday];
|
|
|
|
return reminderDays[weekday];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|