mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Handle multiple reminders at same time
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
android:value="uhabits.db" />
|
||||
<meta-data
|
||||
android:name="AA_DB_VERSION"
|
||||
android:value="5" />
|
||||
android:value="6" />
|
||||
|
||||
<activity
|
||||
android:name="org.isoron.uhabits.MainActivity"
|
||||
@@ -35,6 +35,10 @@
|
||||
|
||||
<receiver android:name="ReminderAlarmReceiver">
|
||||
</receiver>
|
||||
|
||||
<receiver android:name="ReminderAlarmDismissReceiver">
|
||||
</receiver>
|
||||
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
||||
1
assets/migrations/6.sql
Normal file
1
assets/migrations/6.sql
Normal file
@@ -0,0 +1 @@
|
||||
alter table habits add column highlight integer not null default 0;
|
||||
@@ -71,7 +71,8 @@ public class MainActivity extends Activity
|
||||
Intent alarmIntent = new Intent(MainActivity.this, ReminderAlarmReceiver.class);
|
||||
alarmIntent.setData(uri);
|
||||
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 100,
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,
|
||||
((int)(habit.getId() % Integer.MAX_VALUE)) + 1,
|
||||
alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
||||
|
||||
30
src/org/isoron/uhabits/ReminderAlarmDismissReceiver.java
Normal file
30
src/org/isoron/uhabits/ReminderAlarmDismissReceiver.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package org.isoron.uhabits;
|
||||
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
|
||||
public class ReminderAlarmDismissReceiver extends BroadcastReceiver
|
||||
{
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent)
|
||||
{
|
||||
createNotification(context, intent.getData(), intent.getDataString());
|
||||
}
|
||||
|
||||
|
||||
private void createNotification(Context context, Uri data, String text)
|
||||
{
|
||||
for(Habit h : Habit.getHighlightedHabits())
|
||||
{
|
||||
Log.d("Alarm", String.format("Removing highlight from: %s", h.name));
|
||||
h.highlight = 0;
|
||||
h.save();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.isoron.uhabits;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.isoron.helpers.DateHelper;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
|
||||
@@ -28,38 +30,63 @@ public class ReminderAlarmReceiver extends BroadcastReceiver
|
||||
private void createNotification(Context context, Uri data, String text)
|
||||
{
|
||||
Log.d("Alarm", "Alarm received!");
|
||||
|
||||
Habit habit = Habit.get(ContentUris.parseId(data));
|
||||
|
||||
// Check if user already did the habit repetition
|
||||
if(habit.hasRep(DateHelper.getStartOfDay(DateHelper.getLocalTime())))
|
||||
return;
|
||||
|
||||
Log.d("Alarm", String.format("Applying highlight: %s", habit.name));
|
||||
habit.highlight = 1;
|
||||
habit.save();
|
||||
|
||||
// Check if reminder has been turned off after alarm was scheduled
|
||||
if(habit.reminder_hour == null)
|
||||
return;
|
||||
|
||||
Intent resultIntent = new Intent(context, MainActivity.class);
|
||||
resultIntent.setData(data);
|
||||
Intent contentIntent = new Intent(context, MainActivity.class);
|
||||
contentIntent.setData(data);
|
||||
PendingIntent contentPendingIntent = PendingIntent.getActivity(context, 0, contentIntent, 0);
|
||||
|
||||
PendingIntent notificationIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
|
||||
Intent deleteIntent = new Intent(context, ReminderAlarmDismissReceiver.class);
|
||||
PendingIntent deletePendingIntent = PendingIntent.getBroadcast(context, 0, deleteIntent, 0);
|
||||
|
||||
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
||||
|
||||
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
|
||||
inboxStyle.setBigContentTitle("Habit Reminder:");
|
||||
List<Habit> pendingHabits = Habit.getHighlightedHabits();
|
||||
for(Habit h : pendingHabits)
|
||||
{
|
||||
if(h.hasRep(DateHelper.getStartOfDay(DateHelper.getLocalTime())))
|
||||
continue;
|
||||
inboxStyle.addLine(h.name);
|
||||
Log.d("Alarm", String.format("Found highlighted: %s", h.name));
|
||||
}
|
||||
|
||||
String contentText = habit.name;
|
||||
if(pendingHabits.size() > 1) {
|
||||
contentText = String.format("%d pending habits.", pendingHabits.size());
|
||||
}
|
||||
|
||||
Notification notification =
|
||||
new NotificationCompat.Builder(context)
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setContentTitle("Habit Reminder")
|
||||
.setContentText(habit.name)
|
||||
.setContentIntent(notificationIntent)
|
||||
.setContentText(contentText)
|
||||
.setContentIntent(contentPendingIntent)
|
||||
.setDeleteIntent(deletePendingIntent)
|
||||
.setSound(soundUri)
|
||||
.setStyle(inboxStyle)
|
||||
.build();
|
||||
|
||||
notification.flags = Notification.FLAG_AUTO_CANCEL;
|
||||
notification.flags |= Notification.FLAG_AUTO_CANCEL;
|
||||
|
||||
NotificationManager notificationManager = (NotificationManager) context
|
||||
.getSystemService(Activity.NOTIFICATION_SERVICE);
|
||||
|
||||
notificationManager.notify(0, notification);
|
||||
notificationManager.notify(1, notification);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
package org.isoron.uhabits.dialogs;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.isoron.helpers.Command;
|
||||
import org.isoron.helpers.DialogHelper.OnSavedListener;
|
||||
import org.isoron.uhabits.R;
|
||||
@@ -37,7 +33,7 @@ public class EditHabitFragment extends DialogFragment implements OnClickListener
|
||||
|
||||
private OnSavedListener onSavedListener;
|
||||
|
||||
private Habit originalHabit, modifiedHabit;
|
||||
private Habit originalHabit, modified_habit;
|
||||
private TextView tvName, tvDescription, tvFreqNum, tvFreqDen, tvInputReminder;
|
||||
|
||||
static class SolidColorMatrix extends ColorMatrix
|
||||
@@ -104,23 +100,23 @@ public class EditHabitFragment extends DialogFragment implements OnClickListener
|
||||
if(mode == CREATE_MODE)
|
||||
{
|
||||
getDialog().setTitle("Create habit");
|
||||
modifiedHabit = new Habit();
|
||||
modified_habit = new Habit();
|
||||
}
|
||||
else if(mode == EDIT_MODE)
|
||||
{
|
||||
originalHabit = Habit.get((Long) args.get("habitId"));
|
||||
modifiedHabit = new Habit(originalHabit);
|
||||
modified_habit = new Habit(originalHabit);
|
||||
|
||||
getDialog().setTitle("Edit habit");
|
||||
tvName.append(modifiedHabit.name);
|
||||
tvDescription.append(modifiedHabit.description);
|
||||
tvName.append(modified_habit.name);
|
||||
tvDescription.append(modified_habit.description);
|
||||
tvFreqNum.setText(null);
|
||||
tvFreqDen.setText(null);
|
||||
tvFreqNum.append(modifiedHabit.freq_num.toString());
|
||||
tvFreqDen.append(modifiedHabit.freq_den.toString());
|
||||
tvFreqNum.append(modified_habit.freq_num.toString());
|
||||
tvFreqDen.append(modified_habit.freq_den.toString());
|
||||
}
|
||||
|
||||
changeColor(modifiedHabit.color);
|
||||
changeColor(modified_habit.color);
|
||||
updateReminder();
|
||||
|
||||
buttonPickColor.setOnClickListener(new OnClickListener()
|
||||
@@ -129,13 +125,13 @@ public class EditHabitFragment extends DialogFragment implements OnClickListener
|
||||
{
|
||||
ColorPickerDialog picker = ColorPickerDialog.newInstance(
|
||||
R.string.color_picker_default_title,
|
||||
Habit.colors, modifiedHabit.color, 4, ColorPickerDialog.SIZE_SMALL);
|
||||
Habit.colors, modified_habit.color, 4, ColorPickerDialog.SIZE_SMALL);
|
||||
|
||||
picker.setOnColorSelectedListener(new ColorPickerSwatch.OnColorSelectedListener()
|
||||
{
|
||||
public void onColorSelected(int color)
|
||||
{
|
||||
modifiedHabit.color = color;
|
||||
modified_habit.color = color;
|
||||
changeColor(color);
|
||||
}
|
||||
});
|
||||
@@ -159,11 +155,11 @@ public class EditHabitFragment extends DialogFragment implements OnClickListener
|
||||
|
||||
private void updateReminder()
|
||||
{
|
||||
if(modifiedHabit.reminder_hour != null)
|
||||
if(modified_habit.reminder_hour != null)
|
||||
{
|
||||
tvInputReminder.setTextColor(Color.BLACK);
|
||||
tvInputReminder.setText(String.format("%02d:%02d", modifiedHabit.reminder_hour,
|
||||
modifiedHabit.reminder_min));
|
||||
tvInputReminder.setText(String.format("%02d:%02d", modified_habit.reminder_hour,
|
||||
modified_habit.reminder_min));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -189,25 +185,33 @@ public class EditHabitFragment extends DialogFragment implements OnClickListener
|
||||
/* Due date spinner */
|
||||
if(id == R.id.input_reminder_time)
|
||||
{
|
||||
int default_hour = 8;
|
||||
int default_min = 0;
|
||||
|
||||
if(modified_habit.reminder_hour != null) {
|
||||
default_hour = modified_habit.reminder_hour;
|
||||
default_min = modified_habit.reminder_min;
|
||||
}
|
||||
|
||||
TimePickerDialog timePicker = TimePickerDialog.newInstance(new OnTimeSetListener()
|
||||
{
|
||||
|
||||
@Override
|
||||
public void onTimeSet(RadialPickerLayout view, int hour, int minute)
|
||||
{
|
||||
modifiedHabit.reminder_hour = hour;
|
||||
modifiedHabit.reminder_min = minute;
|
||||
modified_habit.reminder_hour = hour;
|
||||
modified_habit.reminder_min = minute;
|
||||
updateReminder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTimeCleared(RadialPickerLayout view)
|
||||
{
|
||||
modifiedHabit.reminder_hour = null;
|
||||
modifiedHabit.reminder_min = null;
|
||||
modified_habit.reminder_hour = null;
|
||||
modified_habit.reminder_min = null;
|
||||
updateReminder();
|
||||
}
|
||||
}, 8, 0, true);
|
||||
}, default_hour, default_min, true);
|
||||
timePicker.show(getFragmentManager(), "timePicker");
|
||||
}
|
||||
|
||||
@@ -216,20 +220,20 @@ public class EditHabitFragment extends DialogFragment implements OnClickListener
|
||||
{
|
||||
Command command = null;
|
||||
|
||||
modifiedHabit.name = tvName.getText().toString().trim();
|
||||
modifiedHabit.description = tvDescription.getText().toString().trim();
|
||||
modifiedHabit.freq_num = Integer.parseInt(tvFreqNum.getText().toString());
|
||||
modifiedHabit.freq_den = Integer.parseInt(tvFreqDen.getText().toString());
|
||||
modified_habit.name = tvName.getText().toString().trim();
|
||||
modified_habit.description = tvDescription.getText().toString().trim();
|
||||
modified_habit.freq_num = Integer.parseInt(tvFreqNum.getText().toString());
|
||||
modified_habit.freq_den = Integer.parseInt(tvFreqDen.getText().toString());
|
||||
|
||||
Boolean valid = true;
|
||||
|
||||
if(modifiedHabit.name.length() == 0)
|
||||
if(modified_habit.name.length() == 0)
|
||||
{
|
||||
tvName.setError("Name cannot be blank.");
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if(modifiedHabit.freq_num <= 0)
|
||||
if(modified_habit.freq_num <= 0)
|
||||
{
|
||||
tvFreqNum.setError("Frequency has to be positive.");
|
||||
valid = false;
|
||||
@@ -239,10 +243,10 @@ public class EditHabitFragment extends DialogFragment implements OnClickListener
|
||||
return;
|
||||
|
||||
if(mode == EDIT_MODE)
|
||||
command = originalHabit.new EditCommand(modifiedHabit);
|
||||
command = originalHabit.new EditCommand(modified_habit);
|
||||
|
||||
if(mode == CREATE_MODE)
|
||||
command = new Habit.CreateCommand(modifiedHabit);
|
||||
command = new Habit.CreateCommand(modified_habit);
|
||||
|
||||
if(onSavedListener != null)
|
||||
onSavedListener.onSaved(command);
|
||||
|
||||
@@ -57,6 +57,9 @@ public class Habit extends Model
|
||||
|
||||
@Column(name = "reminder_min")
|
||||
public Integer reminder_min;
|
||||
|
||||
@Column(name = "highlight")
|
||||
public Integer highlight;
|
||||
|
||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* Commands *
|
||||
@@ -240,6 +243,12 @@ public class Habit extends Model
|
||||
return select().execute();
|
||||
}
|
||||
|
||||
public static java.util.List<Habit> getHighlightedHabits()
|
||||
{
|
||||
return select().where("highlight = 1").orderBy("reminder_hour desc, reminder_min desc")
|
||||
.execute();
|
||||
}
|
||||
|
||||
public static java.util.List<Habit> getHabitsWithReminder()
|
||||
{
|
||||
return select().where("reminder_hour is not null").execute();
|
||||
|
||||
Reference in New Issue
Block a user