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