Refactoring

pull/367/merge
Alinson S. Xavier 8 years ago
parent 819a8d341f
commit a201273781

@ -18,6 +18,11 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {

@ -117,7 +117,7 @@ public class TimePickerDialog extends AppCompatDialogFragment implements OnValue
*/
void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute);
void onTimeCleared(RadialPickerLayout view);
default void onTimeCleared(RadialPickerLayout view) {}
}
public TimePickerDialog() {

@ -91,7 +91,7 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.habits.list.ListHabitsActivity"/>
</activity>
<activity android:name=".notifications.SnoozeDelayActivity"
<activity android:name=".notifications.SnoozeDelayPickerActivity"
android:excludeFromRecents="true"
android:launchMode="singleInstance"
android:theme="@android:style/Theme.Translucent.NoTitleBar">

@ -33,6 +33,7 @@ import org.isoron.uhabits.core.ui.*;
import org.isoron.uhabits.core.ui.screens.habits.list.*;
import org.isoron.uhabits.core.utils.*;
import org.isoron.uhabits.intents.*;
import org.isoron.uhabits.receivers.*;
import org.isoron.uhabits.sync.*;
import org.isoron.uhabits.tasks.*;
import org.isoron.uhabits.widgets.*;
@ -80,6 +81,8 @@ public interface HabitsApplicationComponent
ReminderScheduler getReminderScheduler();
ReminderController getReminderController();
SyncManager getSyncManager();
TaskRunner getTaskRunner();

@ -1,128 +0,0 @@
package org.isoron.uhabits.notifications;
import android.app.*;
import android.content.*;
import android.os.*;
import android.support.v4.app.*;
import android.support.v7.view.*;
import android.text.format.*;
import android.util.*;
import com.android.datetimepicker.time.*;
import com.android.datetimepicker.time.TimePickerDialog;
import org.isoron.uhabits.*;
import org.isoron.uhabits.core.utils.DateUtils;
import org.isoron.uhabits.receivers.*;
import java.util.*;
import static org.isoron.uhabits.core.ui.ThemeSwitcher.THEME_DARK;
import static org.isoron.uhabits.core.utils.DateUtils.applyTimezone;
public class SnoozeDelayActivity extends FragmentActivity implements
TimePickerDialog.OnTimeSetListener, DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
public static final String ACTION_ASK_SNOOZE = "org.isoron.uhabits.ACTION_ASK_SNOOZE";
private static final String TAG = "SnoozeDelayActivity";
private AlertDialog activeDelayDialog;
@Override
protected void onCreate(Bundle bundle)
{
super.onCreate(bundle);
Intent intent = getIntent();
try
{
switch (intent.getAction())
{
case ACTION_ASK_SNOOZE:
AskSnooze();
break;
}
}
catch (RuntimeException e)
{
Log.e(TAG, "could not process intent", e);
}
}
private void AskSnooze()
{
HabitsApplicationComponent component = ((HabitsApplication) getApplicationContext()).getComponent();
int theme = component.getPreferences().getTheme() == THEME_DARK
? R.style.Theme_AppCompat_Dialog_Alert : R.style.Theme_AppCompat_Light_Dialog_Alert;
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, theme));
builder.setTitle(R.string.select_snooze_delay)
.setItems(R.array.snooze_interval_names_reminder, this);
AlertDialog dialog = builder.create();
dialog.setOnDismissListener(this);
activeDelayDialog = dialog;
dialog.show();
}
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
int[] snoozeDelay = getResources().getIntArray(R.array.snooze_interval_values_reminder);
assert (i >= 0 && i <= snoozeDelay.length);
if( snoozeDelay[ i ] < 0 )
{
activeDelayDialog.setOnDismissListener(null);
AskCustomSnooze();
return;
}
Intent intent = new Intent( ReminderReceiver.ACTION_SNOOZE_REMINDER_DELAY, getIntent().getData(),
this, ReminderReceiver.class );
intent.putExtra("snoozeDelay", snoozeDelay[ i ]);
sendBroadcast(intent);
}
private void AskCustomSnooze()
{
final Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
TimePickerDialog dialog;
dialog = TimePickerDialog.newInstance(this,
hour, minute, DateFormat.is24HourFormat(this));
HabitsApplicationComponent component = ((HabitsApplication) getApplicationContext()).getComponent();
dialog.setThemeDark(component.getPreferences().getTheme() == THEME_DARK);
dialog.setDismissListener(this);
dialog.show(getSupportFragmentManager(),"timePicker");
}
@Override
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute)
{
Calendar calendar = DateUtils.getStartOfTodayCalendar();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
Long time = calendar.getTimeInMillis();
if (DateUtils.getLocalTime() > time)
time += DateUtils.DAY_LENGTH;
time = applyTimezone(time);
Intent intent = new Intent( ReminderReceiver.ACTION_SNOOZE_REMINDER_SET, getIntent().getData(),
this, ReminderReceiver.class );
intent.putExtra("reminderTime", time);
sendBroadcast(intent);
}
@Override
public void onTimeCleared(RadialPickerLayout view)
{
Intent intent = new Intent( ReminderReceiver.ACTION_DISMISS_REMINDER, getIntent().getData(),
this, ReminderReceiver.class );
sendBroadcast(intent);
}
@Override
public void onDismiss(DialogInterface dialogInterface)
{
finish();
}
}

@ -0,0 +1,85 @@
package org.isoron.uhabits.notifications;
import android.os.*;
import android.support.annotation.*;
import android.support.v4.app.*;
import android.support.v7.app.*;
import android.text.format.*;
import android.view.*;
import android.widget.*;
import com.android.datetimepicker.time.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.R;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.receivers.*;
import java.util.*;
import butterknife.*;
import static android.content.ContentUris.*;
public class SnoozeDelayPickerActivity extends AppCompatActivity implements AdapterView
.OnItemClickListener
{
public static final String ACTION_ASK_SNOOZE = "org.isoron.uhabits.ACTION_ASK_SNOOZE";
private Habit habit;
private ReminderController reminderController;
@BindArray(R.array.snooze_picker_names)
protected String[] snoozeNames;
@BindArray(R.array.snooze_picker_values)
protected int[] snoozeValues;
@Override
protected void onCreate(@Nullable Bundle bundle)
{
super.onCreate(bundle);
if (getIntent() == null) finish();
if (getIntent().getData() == null) finish();
HabitsApplication app = (HabitsApplication) getApplicationContext();
HabitsApplicationComponent appComponent = app.getComponent();
reminderController = appComponent.getReminderController();
habit = appComponent.getHabitList().getById(parseId(getIntent().getData()));
if (habit == null) finish();
setTheme(R.style.Theme_AppCompat_Light_Dialog_Alert);
ButterKnife.bind(this);
ListView listView = new ListView(this);
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
snoozeNames));
listView.setOnItemClickListener(this);
setContentView(listView);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if (snoozeValues[position] >= 0)
{
reminderController.onSnoozeDelayPicked(habit, snoozeValues[position]);
finish();
}
else showTimePicker();
}
private void showTimePicker()
{
final Calendar calendar = Calendar.getInstance();
int defaultHour = calendar.get(Calendar.HOUR_OF_DAY);
int defaultMinute = calendar.get(Calendar.MINUTE);
TimePickerDialog dialog = TimePickerDialog.newInstance(
(view, hour, minute) ->
reminderController.onSnoozeTimePicked(habit, hour, minute),
defaultHour, defaultMinute, DateFormat.is24HourFormat(this));
dialog.show(getSupportFragmentManager(), "timePicker");
}
}

@ -23,18 +23,17 @@ import android.content.*;
import android.net.*;
import android.support.annotation.*;
import org.isoron.uhabits.core.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.preferences.*;
import org.isoron.uhabits.core.reminders.*;
import org.isoron.uhabits.core.ui.*;
import org.isoron.uhabits.core.utils.*;
import org.isoron.uhabits.notifications.*;
import javax.inject.*;
import static org.isoron.uhabits.core.utils.DateUtils.applyTimezone;
import static org.isoron.uhabits.core.utils.DateUtils.getLocalTime;
@ReceiverScope
@AppScope
public class ReminderController
{
@NonNull
@ -43,6 +42,7 @@ public class ReminderController
@NonNull
private final NotificationTray notificationTray;
@NonNull
private Preferences preferences;
@Inject
@ -68,38 +68,46 @@ public class ReminderController
reminderScheduler.scheduleAll();
}
public void onSnooze(@NonNull Habit habit, final Context context)
public void onSnoozePressed(@NonNull Habit habit, final Context context)
{
long snoozeInterval = preferences.getSnoozeInterval();
long delay = preferences.getSnoozeInterval();
if (snoozeInterval < 0)
{
context.sendBroadcast( new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
Intent intent = new Intent( SnoozeDelayActivity.ACTION_ASK_SNOOZE, Uri.parse( habit.getUriString()),
context, SnoozeDelayActivity.class );
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return;
if (delay < 0)
showSnoozeDelayPicker(habit, context);
else
scheduleReminderMinutesFromNow(habit, delay);
}
snoozeNotificationAddDelay(habit, snoozeInterval);
public void onSnoozeDelayPicked(Habit habit, int delay)
{
scheduleReminderMinutesFromNow(habit, delay);
}
public void snoozeNotificationAddDelay(@NonNull Habit habit, long snoozeInterval)
public void onSnoozeTimePicked(Habit habit, int hour, int minute)
{
long now = applyTimezone(getLocalTime());
long reminderTime = now + snoozeInterval * 60 * 1000;
snoozeNotificationSetReminderTime(habit, reminderTime);
Long time = DateUtils.getUpcomingTimeInMillis(hour, minute);
reminderScheduler.scheduleAtTime(habit, time);
notificationTray.cancel(habit);
}
public void snoozeNotificationSetReminderTime(@NonNull Habit habit, long reminderTime)
public void onDismiss(@NonNull Habit habit)
{
reminderScheduler.schedule(habit, reminderTime);
notificationTray.cancel(habit);
}
public void onDismiss(@NonNull Habit habit)
private void scheduleReminderMinutesFromNow(Habit habit, long minutes)
{
reminderScheduler.scheduleMinutesFromNow(habit, minutes);
notificationTray.cancel(habit);
}
private void showSnoozeDelayPicker(@NonNull Habit habit, Context context)
{
context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
Intent intent = new Intent(SnoozeDelayPickerActivity.ACTION_ASK_SNOOZE,
Uri.parse(habit.getUriString()),
context, SnoozeDelayPickerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}

@ -20,14 +20,13 @@
package org.isoron.uhabits.receivers;
import android.content.*;
import android.support.annotation.*;
import android.util.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.utils.*;
import dagger.*;
import static android.content.ContentUris.*;
/**
@ -46,28 +45,18 @@ public class ReminderReceiver extends BroadcastReceiver
public static final String ACTION_SNOOZE_REMINDER =
"org.isoron.uhabits.ACTION_SNOOZE_REMINDER";
public static final String ACTION_SNOOZE_REMINDER_SET =
"org.isoron.uhabits.ACTION_SNOOZE_REMINDER_SET";
public static final String ACTION_SNOOZE_REMINDER_DELAY =
"org.isoron.uhabits.ACTION_SNOOZE_REMINDER_DELAY";
private static final String TAG = "ReminderReceiver";
@Override
public void onReceive(final Context context, Intent intent)
public void onReceive(@Nullable final Context context, @Nullable Intent intent)
{
HabitsApplication app =
(HabitsApplication) context.getApplicationContext();
ReminderComponent component = DaggerReminderReceiver_ReminderComponent
.builder()
.habitsApplicationComponent(app.getComponent())
.build();
if (context == null || intent == null) return;
if (intent.getAction() == null) return;
HabitList habits = app.getComponent().getHabitList();
ReminderController reminderController =
component.getReminderController();
HabitsApplication app = (HabitsApplication) context.getApplicationContext();
HabitsApplicationComponent appComponent = app.getComponent();
HabitList habits = appComponent.getHabitList();
ReminderController reminderController = appComponent.getReminderController();
Log.i(TAG, String.format("Received intent: %s", intent.toString()));
@ -96,18 +85,7 @@ public class ReminderReceiver extends BroadcastReceiver
case ACTION_SNOOZE_REMINDER:
if (habit == null) return;
reminderController.onSnooze(habit,context);
break;
case ACTION_SNOOZE_REMINDER_SET:
if (habit == null) return;
reminderController.snoozeNotificationSetReminderTime(habit, reminderTime);
break;
case ACTION_SNOOZE_REMINDER_DELAY:
if (habit == null) return;
long snoozeDelay = intent.getIntExtra("snoozeDelay", 0);
reminderController.snoozeNotificationAddDelay(habit, snoozeDelay);
reminderController.onSnoozePressed(habit, context);
break;
case Intent.ACTION_BOOT_COMPLETED:
@ -120,11 +98,4 @@ public class ReminderReceiver extends BroadcastReceiver
Log.e(TAG, "could not process intent", e);
}
}
@ReceiverScope
@Component(dependencies = HabitsApplicationComponent.class)
interface ReminderComponent
{
ReminderController getReminderController();
}
}

@ -49,7 +49,7 @@
<item>-1</item>
</string-array>
<string-array name="snooze_interval_names_reminder">
<string-array name="snooze_picker_names">
<item>@string/interval_15_minutes</item>
<item>@string/interval_30_minutes</item>
<item>@string/interval_1_hour</item>
@ -60,7 +60,7 @@
<item>@string/interval_custom</item>
</string-array>
<integer-array name="snooze_interval_values_reminder" translatable="false">
<integer-array name="snooze_picker_values" translatable="false">
<item>15</item>
<item>30</item>
<item>60</item>

@ -70,9 +70,9 @@ public class ReminderControllerTest extends BaseAndroidJVMTest
DateUtils.setFixedLocalTime(now);
when(preferences.getSnoozeInterval()).thenReturn(15L);
controller.onSnooze(habit,null);
controller.onSnoozePressed(habit,null);
verify(reminderScheduler).schedule(habit, nowTz + 900000);
verify(reminderScheduler).scheduleAtTime(habit, nowTz + 900000);
verify(notificationTray).cancel(habit);
}

@ -22,8 +22,9 @@ package org.isoron.uhabits.core.models;
import android.support.annotation.*;
import org.apache.commons.lang3.builder.*;
import org.isoron.uhabits.core.utils.*;
import static org.isoron.uhabits.core.utils.StringUtils.defaultToStringStyle;
import static org.isoron.uhabits.core.utils.StringUtils.*;
public final class Reminder
{
@ -56,6 +57,11 @@ public final class Reminder
return minute;
}
public long getTimeInMillis()
{
return DateUtils.getUpcomingTimeInMillis(hour, minute);
}
@Override
public boolean equals(Object o)
{

@ -24,9 +24,6 @@ import android.support.annotation.*;
import org.isoron.uhabits.core.*;
import org.isoron.uhabits.core.commands.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.utils.*;
import java.util.*;
import javax.inject.*;
@ -60,14 +57,17 @@ public class ReminderScheduler implements CommandRunner.Listener
scheduleAll();
}
public void schedule(@NonNull Habit habit, @Nullable Long reminderTime)
public void schedule(@NonNull Habit habit)
{
Long reminderTime = habit.getReminder().getTimeInMillis();
scheduleAtTime(habit, reminderTime);
}
public void scheduleAtTime(@NonNull Habit habit, @NonNull Long reminderTime)
{
if (!habit.hasReminder()) return;
if (habit.isArchived()) return;
Reminder reminder = habit.getReminder();
if (reminderTime == null) reminderTime = getReminderTime(reminder);
long timestamp = getStartOfDay(removeTimezone(reminderTime));
sys.scheduleShowReminder(reminderTime, habit, timestamp);
}
@ -76,7 +76,7 @@ public class ReminderScheduler implements CommandRunner.Listener
HabitList reminderHabits =
habitList.getFiltered(HabitMatcher.WITH_ALARM);
for (Habit habit : reminderHabits)
schedule(habit, null);
schedule(habit);
}
public void startListening()
@ -89,19 +89,11 @@ public class ReminderScheduler implements CommandRunner.Listener
commandRunner.removeListener(this);
}
@NonNull
private Long getReminderTime(@NonNull Reminder reminder)
public void scheduleMinutesFromNow(Habit habit, long minutes)
{
Calendar calendar = DateUtils.getStartOfTodayCalendar();
calendar.set(Calendar.HOUR_OF_DAY, reminder.getHour());
calendar.set(Calendar.MINUTE, reminder.getMinute());
calendar.set(Calendar.SECOND, 0);
Long time = calendar.getTimeInMillis();
if (DateUtils.getLocalTime() > time)
time += DateUtils.DAY_LENGTH;
return applyTimezone(time);
long now = applyTimezone(getLocalTime());
long reminderTime = now + minutes * 60 * 1000;
scheduleAtTime(habit, reminderTime);
}
public interface SystemScheduler

@ -240,6 +240,20 @@ public abstract class DateUtils
}
}
public static long getUpcomingTimeInMillis(int hour, int minute)
{
Calendar calendar = DateUtils.getStartOfTodayCalendar();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
Long time = calendar.getTimeInMillis();
if (DateUtils.getLocalTime() > time)
time += DateUtils.DAY_LENGTH;
return applyTimezone(time);
}
public enum TruncateField
{
MONTH, WEEK_NUMBER, YEAR, QUARTER

Loading…
Cancel
Save