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;
|
||||
Habit habit = Habit.get(ContentUris.parseId(data));
|
||||
MainActivity.createReminderAlarm(context, habit,
|
||||
ReminderHelper.createReminderAlarm(context, habit,
|
||||
new Date().getTime() + delayMinutes * 60 * 1000);
|
||||
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.Fragment;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Point;
|
||||
import android.graphics.Typeface;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Vibrator;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.Log;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.ContextMenu.ContextMenuInfo;
|
||||
import android.view.Display;
|
||||
@@ -40,30 +37,28 @@ import com.mobeta.android.dslv.DragSortListView.DropListener;
|
||||
import org.isoron.helpers.Command;
|
||||
import org.isoron.helpers.DateHelper;
|
||||
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.ShowHabitActivity;
|
||||
import org.isoron.uhabits.ReminderHelper;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.Locale;
|
||||
import java.util.Random;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class ListHabitsFragment extends Fragment
|
||||
implements OnSavedListener, OnItemClickListener, OnLongClickListener, DropListener,
|
||||
OnClickListener
|
||||
{
|
||||
|
||||
public interface OnHabitClickListener
|
||||
{
|
||||
public void onHabitClicked(Habit habit);
|
||||
void onHabitClicked(Habit habit);
|
||||
}
|
||||
|
||||
ListHabitsAdapter adapter;
|
||||
DragSortListView listView;
|
||||
MainActivity mainActivity;
|
||||
ReplayableActivity activity;
|
||||
TextView tvNameHeader;
|
||||
long lastLongClick = 0;
|
||||
private int tvNameWidth;
|
||||
@@ -110,8 +105,6 @@ public class ListHabitsFragment extends Fragment
|
||||
|
||||
updateEmptyMessage();
|
||||
|
||||
mainActivity = (MainActivity) getActivity();
|
||||
|
||||
setHasOptionsMenu(true);
|
||||
return view;
|
||||
}
|
||||
@@ -121,6 +114,7 @@ public class ListHabitsFragment extends Fragment
|
||||
{
|
||||
super.onAttach(activity);
|
||||
habitClickListener = (OnHabitClickListener) activity;
|
||||
this.activity = (ReplayableActivity) activity;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -132,7 +126,7 @@ public class ListHabitsFragment extends Fragment
|
||||
|
||||
private void updateHeader()
|
||||
{
|
||||
LayoutInflater inflater = mainActivity.getLayoutInflater();
|
||||
LayoutInflater inflater = activity.getLayoutInflater();
|
||||
View view = getView();
|
||||
|
||||
if (view == null) return;
|
||||
@@ -223,7 +217,7 @@ public class ListHabitsFragment extends Fragment
|
||||
public void onSaved(Command command)
|
||||
{
|
||||
executeCommand(command);
|
||||
MainActivity.createReminderAlarms(mainActivity);
|
||||
ReminderHelper.createReminderAlarms(activity);
|
||||
}
|
||||
|
||||
public void notifyDataSetChanged()
|
||||
@@ -263,7 +257,7 @@ public class ListHabitsFragment extends Fragment
|
||||
|
||||
private void executeCommand(Command c)
|
||||
{
|
||||
mainActivity.executeCommand(c);
|
||||
activity.executeCommand(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user