mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-05-18 11:49:01 +00:00
also reschedule daily reminder
This commit is contained in:
parent
05a4c88921
commit
022d655be3
4 changed files with 39 additions and 35 deletions
|
|
@ -1,5 +1,6 @@
|
|||
package com.habitrpg.android.habitica.helpers;
|
||||
|
||||
import com.habitrpg.android.habitica.NotificationPublisher;
|
||||
import com.habitrpg.android.habitica.events.ReminderDeleteEvent;
|
||||
import com.habitrpg.android.habitica.events.TaskDeleteEvent;
|
||||
import com.habitrpg.android.habitica.events.TaskSaveEvent;
|
||||
|
|
@ -16,6 +17,7 @@ import android.app.AlarmManager;
|
|||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.support.v7.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
|
@ -109,6 +111,8 @@ public class TaskAlarmManager {
|
|||
for (Task task : tasks) {
|
||||
this.setAlarmsForTask(task);
|
||||
}
|
||||
|
||||
scheduleDailyReminder(context);
|
||||
}
|
||||
|
||||
private RemindersItem setTimeForDailyReminder(RemindersItem remindersItem, Task task) {
|
||||
|
|
@ -161,4 +165,33 @@ public class TaskAlarmManager {
|
|||
am.cancel(sender);
|
||||
|
||||
}
|
||||
|
||||
public static void scheduleDailyReminder(Context context) {
|
||||
String timeval = PreferenceManager.getDefaultSharedPreferences(context).getString("reminder_time", "19:00");
|
||||
String[] pieces = timeval.split(":");
|
||||
int hour = Integer.parseInt(pieces[0]);
|
||||
int minute = Integer.parseInt(pieces[1]);
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.set(Calendar.HOUR_OF_DAY, hour);
|
||||
cal.set(Calendar.MINUTE, minute);
|
||||
long trigger_time = cal.getTimeInMillis();
|
||||
|
||||
Intent notificationIntent = new Intent(context, NotificationPublisher.class);
|
||||
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
|
||||
notificationIntent.putExtra(NotificationPublisher.CHECK_DAILIES, false);
|
||||
|
||||
if (PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_NO_CREATE) == null) {
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
|
||||
|
||||
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, trigger_time, AlarmManager.INTERVAL_DAY, pendingIntent);
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeDailyReminder(Context context) {
|
||||
Intent notificationIntent = new Intent(context, NotificationPublisher.class);
|
||||
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
PendingIntent displayIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0);
|
||||
alarmManager.cancel(displayIntent);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,6 @@ public class TaskReceiver extends BroadcastReceiver {
|
|||
|
||||
Bundle extras = arg1.getExtras();
|
||||
|
||||
Log.e("EXTRAS", extras.toString());
|
||||
if (extras != null) {
|
||||
taskTitle = extras.getString(TaskAlarmManager.TASK_NAME_INTENT_KEY);
|
||||
taskId = extras.getString(TaskAlarmManager.TASK_ID_INTENT_KEY);
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ import com.habitrpg.android.habitica.NotificationPublisher;
|
|||
import com.habitrpg.android.habitica.R;
|
||||
import com.habitrpg.android.habitica.callbacks.MergeUserCallback;
|
||||
import com.habitrpg.android.habitica.helpers.LanguageHelper;
|
||||
import com.habitrpg.android.habitica.helpers.TaskAlarmManager;
|
||||
import com.habitrpg.android.habitica.helpers.notifications.PushNotificationManager;
|
||||
import com.habitrpg.android.habitica.prefs.TimePreference;
|
||||
import com.habitrpg.android.habitica.ui.activities.ClassSelectionActivity;
|
||||
|
|
@ -153,36 +154,7 @@ public class PreferencesFragment extends BasePreferencesFragment implements
|
|||
return super.onPreferenceTreeClick(preference);
|
||||
}
|
||||
|
||||
private void scheduleNotifications() {
|
||||
|
||||
String timeval = getPreferenceManager().getSharedPreferences().getString("reminder_time", "19:00");
|
||||
|
||||
String[] pieces = timeval.split(":");
|
||||
int hour = Integer.parseInt(pieces[0]);
|
||||
int minute = Integer.parseInt(pieces[1]);
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.set(Calendar.HOUR_OF_DAY, hour);
|
||||
cal.set(Calendar.MINUTE, minute);
|
||||
long trigger_time = cal.getTimeInMillis();
|
||||
|
||||
Intent notificationIntent = new Intent(context, NotificationPublisher.class);
|
||||
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
|
||||
notificationIntent.putExtra(NotificationPublisher.CHECK_DAILIES, false);
|
||||
|
||||
if (PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_NO_CREATE) == null) {
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
|
||||
|
||||
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, trigger_time, AlarmManager.INTERVAL_DAY, pendingIntent);
|
||||
}
|
||||
}
|
||||
|
||||
private void removeNotifications() {
|
||||
Intent notificationIntent = new Intent(context, NotificationPublisher.class);
|
||||
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
PendingIntent displayIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0);
|
||||
alarmManager.cancel(displayIntent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
|
|
@ -190,13 +162,13 @@ public class PreferencesFragment extends BasePreferencesFragment implements
|
|||
boolean use_reminder = sharedPreferences.getBoolean(key, false);
|
||||
timePreference.setEnabled(use_reminder);
|
||||
if (use_reminder) {
|
||||
scheduleNotifications();
|
||||
TaskAlarmManager.scheduleDailyReminder(context);
|
||||
} else {
|
||||
removeNotifications();
|
||||
TaskAlarmManager.removeDailyReminder(context);
|
||||
}
|
||||
} else if (key.equals("reminder_time")) {
|
||||
removeNotifications();
|
||||
scheduleNotifications();
|
||||
TaskAlarmManager.removeDailyReminder(context);
|
||||
TaskAlarmManager.scheduleDailyReminder(context);
|
||||
} else if (key.equals("usePushNotifications")) {
|
||||
boolean userPushNotifications = sharedPreferences.getBoolean(key, false);
|
||||
pushNotificationsPreference.setEnabled(userPushNotifications);
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ buildscript {
|
|||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:2.2.1'
|
||||
classpath 'com.android.tools.build:gradle:2.2.2'
|
||||
classpath 'com.android.databinding:dataBinder:1.0-rc4'
|
||||
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
|
||||
classpath 'com.google.gms:google-services:3.0.0'
|
||||
|
|
|
|||
Loading…
Reference in a new issue