mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-04-14 19:56:32 +00:00
Generalized notifications for tasks. Added inital daily notifications
This commit is contained in:
parent
d00bb82f95
commit
50e04194c6
8 changed files with 98 additions and 55 deletions
|
|
@ -121,7 +121,7 @@
|
|||
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
|
||||
<receiver android:process=":remote" android:name=".NotificationPublisher" />
|
||||
|
||||
<receiver android:process=":remote" android:name=".receivers.TodoReceiver"></receiver>
|
||||
<receiver android:process=":remote" android:name=".receivers.TaskReceiver"></receiver>
|
||||
<receiver android:name=".receivers.TaskAlarmBootReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action._BOOT_COMPLETED"/>
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
ALTER TABLE RemindersItem ADD COLUMN 'alarmId' integer;
|
||||
|
|
@ -7,5 +7,5 @@ public class HabitDatabase {
|
|||
|
||||
public static final String NAME = "Habitica";
|
||||
|
||||
public static final int VERSION = 22;
|
||||
public static final int VERSION = 21;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import android.content.BroadcastReceiver;
|
|||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
import com.habitrpg.android.habitica.R;
|
||||
import com.habitrpg.android.habitica.ui.activities.MainActivity;
|
||||
|
|
@ -15,7 +16,7 @@ import com.habitrpg.android.habitica.ui.helpers.TaskAlarmManager;
|
|||
/**
|
||||
* Created by keithholliday on 5/29/16.
|
||||
*/
|
||||
public class TodoReceiver extends BroadcastReceiver {
|
||||
public class TaskReceiver extends BroadcastReceiver {
|
||||
|
||||
private Context context;
|
||||
|
||||
|
|
@ -29,12 +30,14 @@ public class TodoReceiver extends BroadcastReceiver {
|
|||
taskAlarmManager = TaskAlarmManager.getInstance(context);
|
||||
|
||||
Bundle extras = arg1.getExtras();
|
||||
|
||||
if (extras != null) {
|
||||
taskTitle = extras.getString(TaskAlarmManager.TASK_NAME_INTENT_KEY);
|
||||
taskId = extras.getString(TaskAlarmManager.TASK_ID_INTENT_KEY);
|
||||
//This will set up the next reminders for dailies
|
||||
taskAlarmManager.addAlarmForTaskId(taskId);
|
||||
createNotification();
|
||||
}
|
||||
|
||||
createNotification();
|
||||
}
|
||||
|
||||
public void createNotification() {
|
||||
|
|
@ -6,7 +6,6 @@ import com.habitrpg.android.habitica.R;
|
|||
import com.habitrpg.android.habitica.components.AppComponent;
|
||||
import com.habitrpg.android.habitica.events.TaskSaveEvent;
|
||||
import com.habitrpg.android.habitica.events.commands.DeleteTaskCommand;
|
||||
import com.habitrpg.android.habitica.receivers.TodoReceiver;
|
||||
import com.habitrpg.android.habitica.ui.WrapContentRecyclerViewLayoutManager;
|
||||
import com.habitrpg.android.habitica.ui.adapter.tasks.CheckListAdapter;
|
||||
import com.habitrpg.android.habitica.ui.adapter.tasks.RemindersAdapter;
|
||||
|
|
@ -597,6 +596,7 @@ public class TaskFormActivity extends BaseActivity implements AdapterView.OnItem
|
|||
String[] reminderTimeSplit = reminderTimeString.split(":");
|
||||
time.setHours(Integer.parseInt(reminderTimeSplit[0]));
|
||||
time.setMinutes(Integer.parseInt(reminderTimeSplit[1]));
|
||||
time.setSeconds(0);
|
||||
|
||||
RemindersItem item = new RemindersItem();
|
||||
UUID randomUUID = UUID.randomUUID();
|
||||
|
|
|
|||
|
|
@ -4,14 +4,10 @@ import android.app.AlarmManager;
|
|||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.habitrpg.android.habitica.events.TaskCreatedEvent;
|
||||
import com.habitrpg.android.habitica.events.TaskSaveEvent;
|
||||
import com.habitrpg.android.habitica.events.TaskUpdatedEvent;
|
||||
import com.habitrpg.android.habitica.receivers.TodoReceiver;
|
||||
import com.habitrpg.android.habitica.receivers.TaskReceiver;
|
||||
import com.magicmicky.habitrpgwrapper.lib.models.tasks.RemindersItem;
|
||||
import com.magicmicky.habitrpgwrapper.lib.models.tasks.Task;
|
||||
import com.raizlabs.android.dbflow.sql.builder.Condition;
|
||||
|
|
@ -20,15 +16,11 @@ import com.raizlabs.android.dbflow.sql.language.Select;
|
|||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by keithholliday on 5/29/16.
|
||||
|
|
@ -59,36 +51,36 @@ public class TaskAlarmManager {
|
|||
Task task = (Task) evnt.task;
|
||||
List<RemindersItem> reminders = task.getReminders();
|
||||
for (RemindersItem reminder : reminders) {
|
||||
this.addAlarmForTaskReminder(task, reminder);
|
||||
if (task.getType().equals(Task.TYPE_DAILY)) {
|
||||
//Ensure that we set to the next available time
|
||||
reminder = this.setTimeForDailyReminder(reminder, task);
|
||||
}
|
||||
this.setAlarmForRemindersItem(reminder);
|
||||
reminder.async().save();
|
||||
}
|
||||
}
|
||||
|
||||
public void addAlarmForTaskReminder(Task task, RemindersItem reminder) {
|
||||
if (task.getType().equals("todo")) {
|
||||
this.addAlarmForTodo(task, reminder);
|
||||
}
|
||||
}
|
||||
//This function is used from the TaskReceiver since we do not have access to the task
|
||||
//We currently only use this function to schedule the next reminder for dailies
|
||||
//We may be able to use repeating alarms instead of this in the future
|
||||
public void addAlarmForTaskId(String taskId) {
|
||||
List<Task> tasks = new Select()
|
||||
.from(Task.class)
|
||||
.where(Condition.column("id").eq(taskId))
|
||||
.queryList();
|
||||
|
||||
private void addAlarmForTodo(Task task, RemindersItem reminder) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(reminder.getStartDate());
|
||||
|
||||
Intent intent = new Intent(context, TodoReceiver.class);
|
||||
intent.putExtra(TASK_NAME_INTENT_KEY, task.getText());
|
||||
intent.putExtra(TASK_ID_INTENT_KEY, task.getId());
|
||||
|
||||
Integer alarmId = reminder.getAlarmId();
|
||||
if (alarmId == null) {
|
||||
alarmId = (int) System.currentTimeMillis();
|
||||
reminder.setAlarmId(alarmId);
|
||||
Task task = tasks.get(0);
|
||||
if (!task.getType().equals(Task.TYPE_DAILY)) {
|
||||
return;
|
||||
}
|
||||
|
||||
PendingIntent sender = PendingIntent.getBroadcast(context, alarmId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
|
||||
|
||||
reminder.async().save();
|
||||
List<RemindersItem> reminders = task.getReminders();
|
||||
for (RemindersItem remindersItem : reminders) {
|
||||
//Ensure that we set to the next available time
|
||||
remindersItem = this.setTimeForDailyReminder(remindersItem, task);
|
||||
this.setAlarmForRemindersItem(remindersItem);
|
||||
remindersItem.async().save();
|
||||
}
|
||||
}
|
||||
|
||||
public void scheduleAllSavedAlarms() {
|
||||
|
|
@ -99,21 +91,45 @@ public class TaskAlarmManager {
|
|||
.queryList();
|
||||
|
||||
for (RemindersItem remindersItem : reminders) {
|
||||
Task reminderItemTask = remindersItem.getTask();
|
||||
Integer alarmId = remindersItem.getAlarmId();
|
||||
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(remindersItem.getStartDate());
|
||||
|
||||
Intent intent = new Intent(context, TodoReceiver.class);
|
||||
intent.putExtra(TASK_NAME_INTENT_KEY, reminderItemTask.getText());
|
||||
intent.putExtra(TASK_ID_INTENT_KEY, reminderItemTask.getId());
|
||||
|
||||
PendingIntent sender = PendingIntent.getBroadcast(context, alarmId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
|
||||
this.setAlarmForRemindersItem(remindersItem);
|
||||
}
|
||||
}
|
||||
|
||||
private RemindersItem setTimeForDailyReminder (RemindersItem remindersItem, Task task) {
|
||||
Date oldTime = remindersItem.getTime();
|
||||
Date newTime = task.getNextActiveDateAfter(oldTime);
|
||||
newTime.setHours(oldTime.getHours());
|
||||
newTime.setMinutes(oldTime.getMinutes());
|
||||
newTime.setSeconds(0);
|
||||
remindersItem.setTime(newTime);
|
||||
return remindersItem;
|
||||
}
|
||||
|
||||
private void setAlarmForRemindersItem(RemindersItem remindersItem) {
|
||||
Task reminderItemTask = remindersItem.getTask();
|
||||
Integer alarmId = remindersItem.getAlarmId();
|
||||
|
||||
if (alarmId == null) {
|
||||
alarmId = (int) System.currentTimeMillis();
|
||||
remindersItem.setAlarmId(alarmId);
|
||||
}
|
||||
|
||||
Date now = new Date();
|
||||
|
||||
if (remindersItem.getTime().before(now)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(remindersItem.getTime());
|
||||
|
||||
Intent intent = new Intent(context, TaskReceiver.class);
|
||||
intent.putExtra(TASK_NAME_INTENT_KEY, reminderItemTask.getText());
|
||||
intent.putExtra(TASK_ID_INTENT_KEY, reminderItemTask.getId());
|
||||
|
||||
PendingIntent sender = PendingIntent.getBroadcast(context, alarmId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -577,4 +577,28 @@ public class Task extends BaseModel {
|
|||
public Boolean isChecklistDisplayActive(int offset) {
|
||||
return this.isDisplayedActive(offset) && (this.checklist.size() != this.getCompletedChecklistCount());
|
||||
}
|
||||
|
||||
public Date getNextActiveDateAfter(Date oldTime) {
|
||||
Calendar today = new GregorianCalendar();
|
||||
today.set(Calendar.MILLISECOND, 0);
|
||||
today.set(Calendar.SECOND, 0);
|
||||
|
||||
Calendar newTime = new GregorianCalendar();
|
||||
newTime.setTime(oldTime);
|
||||
|
||||
if (this.getFrequency().equals(FREQUENCY_DAILY)) {
|
||||
TimeUnit timeUnit = TimeUnit.DAYS;
|
||||
long diffInMillies = newTime.getTimeInMillis() - today.getTimeInMillis();
|
||||
long daySinceStart = timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS);
|
||||
Log.v("Test", String.valueOf(daySinceStart % this.getEveryX()));
|
||||
} else {
|
||||
int nextActiveDayOfTheWeek = today.get(Calendar.DAY_OF_WEEK);
|
||||
while (!this.getRepeat().getForDay(nextActiveDayOfTheWeek) || newTime.before(today)) {
|
||||
if (nextActiveDayOfTheWeek == 6) nextActiveDayOfTheWeek = 0;
|
||||
nextActiveDayOfTheWeek += 1;
|
||||
newTime.add(Calendar.DATE, 1);
|
||||
}
|
||||
}
|
||||
return newTime.getTime();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
1
gradle/wrapper/gradle-wrapper.properties
vendored
1
gradle/wrapper/gradle-wrapper.properties
vendored
|
|
@ -4,3 +4,4 @@ distributionPath=wrapper/dists
|
|||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip
|
||||
org.gradle.jvmargs=-Xmx3072M
|
||||
|
|
|
|||
Loading…
Reference in a new issue