mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 17:18:52 -06:00
Dismiss notification automatically
This commit is contained in:
@@ -25,6 +25,7 @@ import android.support.annotation.*;
|
||||
|
||||
import com.activeandroid.*;
|
||||
|
||||
import org.isoron.uhabits.notifications.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
import org.isoron.uhabits.widgets.*;
|
||||
|
||||
@@ -43,6 +44,8 @@ public class HabitsApplication extends Application
|
||||
|
||||
private ReminderScheduler reminderScheduler;
|
||||
|
||||
private NotificationTray notificationTray;
|
||||
|
||||
public AppComponent getComponent()
|
||||
{
|
||||
return component;
|
||||
@@ -54,6 +57,7 @@ public class HabitsApplication extends Application
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Deprecated
|
||||
public static Context getContext()
|
||||
{
|
||||
if (context == null) throw new RuntimeException("context is null");
|
||||
@@ -100,6 +104,9 @@ public class HabitsApplication extends Application
|
||||
reminderScheduler = component.getReminderScheduler();
|
||||
reminderScheduler.startListening();
|
||||
|
||||
notificationTray = component.getNotificationTray();
|
||||
notificationTray.startListening();
|
||||
|
||||
DatabaseUtils.initializeActiveAndroid();
|
||||
}
|
||||
|
||||
@@ -111,6 +118,7 @@ public class HabitsApplication extends Application
|
||||
|
||||
reminderScheduler.stopListening();
|
||||
widgetUpdater.stopListening();
|
||||
notificationTray.stopListening();
|
||||
super.onTerminate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,4 +46,9 @@ public class ToggleRepetitionCommand extends Command
|
||||
{
|
||||
execute();
|
||||
}
|
||||
|
||||
public Habit getHabit()
|
||||
{
|
||||
return habit;
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import android.support.v4.app.*;
|
||||
import android.support.v4.app.NotificationCompat.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.commands.*;
|
||||
import org.isoron.uhabits.intents.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.tasks.*;
|
||||
@@ -37,7 +38,7 @@ import static android.graphics.BitmapFactory.*;
|
||||
import static org.isoron.uhabits.utils.RingtoneUtils.*;
|
||||
|
||||
@AppScope
|
||||
public class NotificationTray
|
||||
public class NotificationTray implements CommandRunner.Listener
|
||||
{
|
||||
@NonNull
|
||||
private final Context context;
|
||||
@@ -48,14 +49,18 @@ public class NotificationTray
|
||||
@NonNull
|
||||
private final PendingIntentFactory pendingIntents;
|
||||
|
||||
private CommandRunner commandRunner;
|
||||
|
||||
@Inject
|
||||
public NotificationTray(@AppContext @NonNull Context context,
|
||||
@NonNull TaskRunner taskRunner,
|
||||
@NonNull PendingIntentFactory pendingIntents)
|
||||
@NonNull PendingIntentFactory pendingIntents,
|
||||
@NonNull CommandRunner commandRunner)
|
||||
{
|
||||
this.context = context;
|
||||
this.taskRunner = taskRunner;
|
||||
this.pendingIntents = pendingIntents;
|
||||
this.commandRunner = commandRunner;
|
||||
}
|
||||
|
||||
public void cancel(@NonNull Habit habit)
|
||||
@@ -64,12 +69,37 @@ public class NotificationTray
|
||||
NotificationManagerCompat.from(context).cancel(notificationId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommandExecuted(@NonNull Command command,
|
||||
@Nullable Long refreshKey)
|
||||
{
|
||||
if (!(command instanceof ToggleRepetitionCommand))
|
||||
return;
|
||||
|
||||
ToggleRepetitionCommand toggleCommand =
|
||||
(ToggleRepetitionCommand) command;
|
||||
|
||||
Habit habit = toggleCommand.getHabit();
|
||||
if(habit.getCheckmarks().getTodayValue() != Checkmark.UNCHECKED)
|
||||
cancel(habit);
|
||||
}
|
||||
|
||||
public void show(@NonNull Habit habit, long timestamp, long reminderTime)
|
||||
{
|
||||
taskRunner.execute(
|
||||
new ShowNotificationTask(habit, timestamp, reminderTime));
|
||||
}
|
||||
|
||||
public void startListening()
|
||||
{
|
||||
commandRunner.addListener(this);
|
||||
}
|
||||
|
||||
public void stopListening()
|
||||
{
|
||||
commandRunner.removeListener(this);
|
||||
}
|
||||
|
||||
private int getNotificationId(Habit habit)
|
||||
{
|
||||
Long id = habit.getId();
|
||||
|
||||
Reference in New Issue
Block a user