mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Merge branch 'llunak/custom_snooze' into dev
This commit is contained in:
@@ -63,6 +63,7 @@ public class TimePickerDialog extends AppCompatDialogFragment implements OnValue
|
||||
private static final int PULSE_ANIMATOR_DELAY = 300;
|
||||
|
||||
private OnTimeSetListener mCallback;
|
||||
private DialogInterface.OnDismissListener dismissListener;
|
||||
|
||||
private HapticFeedbackController mHapticFeedbackController;
|
||||
|
||||
@@ -998,4 +999,15 @@ public class TimePickerDialog extends AppCompatDialogFragment implements OnValue
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void setDismissListener( DialogInterface.OnDismissListener listener ) {
|
||||
dismissListener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDismiss(DialogInterface dialog) {
|
||||
super.onDismiss(dialog);
|
||||
if( dismissListener != null )
|
||||
dismissListener.onDismiss(dialog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,6 +91,11 @@
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value=".activities.habits.list.ListHabitsActivity"/>
|
||||
</activity>
|
||||
<activity android:name=".notifications.SnoozeDelayActivity"
|
||||
android:excludeFromRecents="true"
|
||||
android:launchMode="singleInstance"
|
||||
android:theme="@android:style/Theme.Translucent.NoTitleBar">
|
||||
</activity>
|
||||
|
||||
<receiver
|
||||
android:name=".widgets.CheckmarkWidgetProvider"
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -19,12 +19,15 @@
|
||||
|
||||
package org.isoron.uhabits.receivers;
|
||||
|
||||
import android.content.*;
|
||||
import android.net.*;
|
||||
import android.support.annotation.*;
|
||||
|
||||
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.notifications.*;
|
||||
|
||||
import javax.inject.*;
|
||||
|
||||
@@ -65,13 +68,32 @@ public class ReminderController
|
||||
reminderScheduler.scheduleAll();
|
||||
}
|
||||
|
||||
public void onSnooze(@NonNull Habit habit)
|
||||
public void onSnooze(@NonNull Habit habit, final Context context)
|
||||
{
|
||||
long snoozeInterval = 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;
|
||||
}
|
||||
|
||||
snoozeNotificationAddDelay(habit, snoozeInterval);
|
||||
}
|
||||
|
||||
public void snoozeNotificationAddDelay(@NonNull Habit habit, long snoozeInterval)
|
||||
{
|
||||
long now = applyTimezone(getLocalTime());
|
||||
long reminderTime = now + snoozeInterval * 60 * 1000;
|
||||
snoozeNotificationSetReminderTime(habit, reminderTime);
|
||||
}
|
||||
|
||||
public void snoozeNotificationSetReminderTime(@NonNull Habit habit, long reminderTime)
|
||||
{
|
||||
reminderScheduler.schedule(habit, reminderTime);
|
||||
notificationTray.cancel(habit);
|
||||
}
|
||||
|
||||
@@ -46,6 +46,12 @@ 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
|
||||
@@ -90,7 +96,18 @@ public class ReminderReceiver extends BroadcastReceiver
|
||||
|
||||
case ACTION_SNOOZE_REMINDER:
|
||||
if (habit == null) return;
|
||||
reminderController.onSnooze(habit);
|
||||
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);
|
||||
break;
|
||||
|
||||
case Intent.ACTION_BOOT_COMPLETED:
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
<item>@string/interval_4_hour</item>
|
||||
<item>@string/interval_8_hour</item>
|
||||
<item>@string/interval_24_hour</item>
|
||||
<item>@string/interval_always_ask</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="snooze_interval_values" translatable="false">
|
||||
@@ -45,8 +46,31 @@
|
||||
<item>240</item>
|
||||
<item>480</item>
|
||||
<item>1440</item>
|
||||
<item>-1</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="snooze_interval_names_reminder">
|
||||
<item>@string/interval_15_minutes</item>
|
||||
<item>@string/interval_30_minutes</item>
|
||||
<item>@string/interval_1_hour</item>
|
||||
<item>@string/interval_2_hour</item>
|
||||
<item>@string/interval_4_hour</item>
|
||||
<item>@string/interval_8_hour</item>
|
||||
<item>@string/interval_24_hour</item>
|
||||
<item>@string/interval_custom</item>
|
||||
</string-array>
|
||||
|
||||
<integer-array name="snooze_interval_values_reminder" translatable="false">
|
||||
<item>15</item>
|
||||
<item>30</item>
|
||||
<item>60</item>
|
||||
<item>120</item>
|
||||
<item>240</item>
|
||||
<item>480</item>
|
||||
<item>1440</item>
|
||||
<item>-1</item>
|
||||
</integer-array>
|
||||
|
||||
<string-array name="frequencyQuickSelect" translatable="false">
|
||||
<item>@string/every_day</item>
|
||||
<item>@string/every_week</item>
|
||||
|
||||
@@ -83,6 +83,8 @@
|
||||
<string name="interval_4_hour">4 hours</string>
|
||||
<string name="interval_8_hour">8 hours</string>
|
||||
<string name="interval_24_hour">24 hours</string>
|
||||
<string name="interval_always_ask">Always ask</string>
|
||||
<string name="interval_custom">Custom...</string>
|
||||
<string name="pref_toggle_title">Toggle with short press</string>
|
||||
<string name="pref_toggle_description">Put checkmarks with a single tap instead of press-and-hold. More convenient, but might cause accidental toggles.</string>
|
||||
<string name="pref_snooze_interval_title">Snooze interval on reminders</string>
|
||||
@@ -95,6 +97,7 @@
|
||||
<string name="name">Name</string>
|
||||
<string name="settings">Settings</string>
|
||||
<string name="snooze_interval">Snooze interval</string>
|
||||
<string name="select_snooze_delay">Select snooze delay</string>
|
||||
|
||||
<string name="hint_title">Did you know?</string>
|
||||
<string name="hint_drag">To rearrange the entries, press-and-hold on the name of the habit, then drag it to the correct place.</string>
|
||||
|
||||
@@ -70,7 +70,7 @@ public class ReminderControllerTest extends BaseAndroidJVMTest
|
||||
DateUtils.setFixedLocalTime(now);
|
||||
when(preferences.getSnoozeInterval()).thenReturn(15L);
|
||||
|
||||
controller.onSnooze(habit);
|
||||
controller.onSnooze(habit,null);
|
||||
|
||||
verify(reminderScheduler).schedule(habit, nowTz + 900000);
|
||||
verify(notificationTray).cancel(habit);
|
||||
|
||||
Reference in New Issue
Block a user