mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Move reminder methods to separate class
This commit is contained in:
@@ -45,7 +45,7 @@ public class ReminderAlarmReceiver extends BroadcastReceiver
|
|||||||
{
|
{
|
||||||
int delayMinutes = 60;
|
int delayMinutes = 60;
|
||||||
Habit habit = Habit.get(ContentUris.parseId(data));
|
Habit habit = Habit.get(ContentUris.parseId(data));
|
||||||
MainActivity.createReminderAlarm(context, habit,
|
ReminderHelper.createReminderAlarm(context, habit,
|
||||||
new Date().getTime() + delayMinutes * 60 * 1000);
|
new Date().getTime() + delayMinutes * 60 * 1000);
|
||||||
dismissNotification(context, habit);
|
dismissNotification(context, habit);
|
||||||
}
|
}
|
||||||
|
|||||||
66
app/src/main/java/org/isoron/uhabits/ReminderHelper.java
Normal file
66
app/src/main/java/org/isoron/uhabits/ReminderHelper.java
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package org.isoron.uhabits;
|
||||||
|
|
||||||
|
import android.app.AlarmManager;
|
||||||
|
import android.app.PendingIntent;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class ReminderHelper
|
||||||
|
{
|
||||||
|
public static void createReminderAlarms(Context context)
|
||||||
|
{
|
||||||
|
for (Habit habit : Habit.getHabitsWithReminder())
|
||||||
|
createReminderAlarm(context, habit, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void createReminderAlarm(Context context, Habit habit, Long reminderTime)
|
||||||
|
{
|
||||||
|
Uri uri = Uri.parse("content://org.isoron.uhabits/habit/" + habit.getId());
|
||||||
|
|
||||||
|
if (reminderTime == null)
|
||||||
|
{
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.setTimeInMillis(System.currentTimeMillis());
|
||||||
|
calendar.set(Calendar.HOUR_OF_DAY, habit.reminder_hour);
|
||||||
|
calendar.set(Calendar.MINUTE, habit.reminder_min);
|
||||||
|
calendar.set(Calendar.SECOND, 0);
|
||||||
|
|
||||||
|
reminderTime = calendar.getTimeInMillis();
|
||||||
|
|
||||||
|
if (System.currentTimeMillis() > reminderTime)
|
||||||
|
{
|
||||||
|
reminderTime += AlarmManager.INTERVAL_DAY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Intent alarmIntent = new Intent(context, ReminderAlarmReceiver.class);
|
||||||
|
alarmIntent.setAction(ReminderAlarmReceiver.ACTION_REMIND);
|
||||||
|
alarmIntent.setData(uri);
|
||||||
|
|
||||||
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
|
||||||
|
((int) (habit.getId() % Integer.MAX_VALUE)) + 1, alarmIntent,
|
||||||
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
|
||||||
|
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||||
|
if (Build.VERSION.SDK_INT >= 19)
|
||||||
|
{
|
||||||
|
manager.setExact(AlarmManager.RTC_WAKEUP, reminderTime, pendingIntent);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
manager.set(AlarmManager.RTC_WAKEUP, reminderTime, pendingIntent);
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d("Alarm", String.format("Setting alarm (%s): %s",
|
||||||
|
DateFormat.getDateTimeInstance().format(new Date(reminderTime)), habit.name));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,15 +3,12 @@ package org.isoron.uhabits.dialogs;
|
|||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.Fragment;
|
import android.app.Fragment;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.graphics.Point;
|
import android.graphics.Point;
|
||||||
import android.graphics.Typeface;
|
import android.graphics.Typeface;
|
||||||
import android.net.Uri;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Vibrator;
|
import android.os.Vibrator;
|
||||||
import android.util.DisplayMetrics;
|
import android.util.DisplayMetrics;
|
||||||
import android.util.Log;
|
|
||||||
import android.view.ContextMenu;
|
import android.view.ContextMenu;
|
||||||
import android.view.ContextMenu.ContextMenuInfo;
|
import android.view.ContextMenu.ContextMenuInfo;
|
||||||
import android.view.Display;
|
import android.view.Display;
|
||||||
@@ -40,30 +37,28 @@ import com.mobeta.android.dslv.DragSortListView.DropListener;
|
|||||||
import org.isoron.helpers.Command;
|
import org.isoron.helpers.Command;
|
||||||
import org.isoron.helpers.DateHelper;
|
import org.isoron.helpers.DateHelper;
|
||||||
import org.isoron.helpers.DialogHelper.OnSavedListener;
|
import org.isoron.helpers.DialogHelper.OnSavedListener;
|
||||||
import org.isoron.uhabits.MainActivity;
|
import org.isoron.helpers.ReplayableActivity;
|
||||||
import org.isoron.uhabits.R;
|
import org.isoron.uhabits.R;
|
||||||
import org.isoron.uhabits.ShowHabitActivity;
|
import org.isoron.uhabits.ReminderHelper;
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Random;
|
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
public class ListHabitsFragment extends Fragment
|
public class ListHabitsFragment extends Fragment
|
||||||
implements OnSavedListener, OnItemClickListener, OnLongClickListener, DropListener,
|
implements OnSavedListener, OnItemClickListener, OnLongClickListener, DropListener,
|
||||||
OnClickListener
|
OnClickListener
|
||||||
{
|
{
|
||||||
|
|
||||||
public interface OnHabitClickListener
|
public interface OnHabitClickListener
|
||||||
{
|
{
|
||||||
public void onHabitClicked(Habit habit);
|
void onHabitClicked(Habit habit);
|
||||||
}
|
}
|
||||||
|
|
||||||
ListHabitsAdapter adapter;
|
ListHabitsAdapter adapter;
|
||||||
DragSortListView listView;
|
DragSortListView listView;
|
||||||
MainActivity mainActivity;
|
ReplayableActivity activity;
|
||||||
TextView tvNameHeader;
|
TextView tvNameHeader;
|
||||||
long lastLongClick = 0;
|
long lastLongClick = 0;
|
||||||
private int tvNameWidth;
|
private int tvNameWidth;
|
||||||
@@ -110,8 +105,6 @@ public class ListHabitsFragment extends Fragment
|
|||||||
|
|
||||||
updateEmptyMessage();
|
updateEmptyMessage();
|
||||||
|
|
||||||
mainActivity = (MainActivity) getActivity();
|
|
||||||
|
|
||||||
setHasOptionsMenu(true);
|
setHasOptionsMenu(true);
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
@@ -121,6 +114,7 @@ public class ListHabitsFragment extends Fragment
|
|||||||
{
|
{
|
||||||
super.onAttach(activity);
|
super.onAttach(activity);
|
||||||
habitClickListener = (OnHabitClickListener) activity;
|
habitClickListener = (OnHabitClickListener) activity;
|
||||||
|
this.activity = (ReplayableActivity) activity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -132,7 +126,7 @@ public class ListHabitsFragment extends Fragment
|
|||||||
|
|
||||||
private void updateHeader()
|
private void updateHeader()
|
||||||
{
|
{
|
||||||
LayoutInflater inflater = mainActivity.getLayoutInflater();
|
LayoutInflater inflater = activity.getLayoutInflater();
|
||||||
View view = getView();
|
View view = getView();
|
||||||
|
|
||||||
if (view == null) return;
|
if (view == null) return;
|
||||||
@@ -223,7 +217,7 @@ public class ListHabitsFragment extends Fragment
|
|||||||
public void onSaved(Command command)
|
public void onSaved(Command command)
|
||||||
{
|
{
|
||||||
executeCommand(command);
|
executeCommand(command);
|
||||||
MainActivity.createReminderAlarms(mainActivity);
|
ReminderHelper.createReminderAlarms(activity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void notifyDataSetChanged()
|
public void notifyDataSetChanged()
|
||||||
@@ -263,7 +257,7 @@ public class ListHabitsFragment extends Fragment
|
|||||||
|
|
||||||
private void executeCommand(Command c)
|
private void executeCommand(Command c)
|
||||||
{
|
{
|
||||||
mainActivity.executeCommand(c);
|
activity.executeCommand(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user