mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Merge branch 'feature/custom-snooze' into dev
This commit is contained in:
@@ -91,6 +91,11 @@
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value=".activities.habits.list.ListHabitsActivity"/>
|
||||
</activity>
|
||||
<activity android:name=".notifications.SnoozeDelayPickerActivity"
|
||||
android:excludeFromRecents="true"
|
||||
android:launchMode="singleInstance"
|
||||
android:theme="@android:style/Theme.Translucent.NoTitleBar">
|
||||
</activity>
|
||||
|
||||
<receiver
|
||||
android:name=".widgets.CheckmarkWidgetProvider"
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -25,7 +25,10 @@ import org.isoron.androidbase.activities.*
|
||||
import org.isoron.uhabits.*
|
||||
import org.isoron.uhabits.activities.habits.list.views.*
|
||||
import org.isoron.uhabits.core.commands.*
|
||||
import org.isoron.uhabits.core.preferences.*
|
||||
import org.isoron.uhabits.core.ui.*
|
||||
import org.isoron.uhabits.core.ui.screens.habits.list.*
|
||||
import org.isoron.uhabits.core.utils.*
|
||||
import javax.inject.*
|
||||
|
||||
@ActivityScope
|
||||
@@ -33,8 +36,10 @@ class ListHabitsSelectionMenu @Inject constructor(
|
||||
private val screen: ListHabitsScreen,
|
||||
private val listAdapter: HabitCardListAdapter,
|
||||
var commandRunner: CommandRunner,
|
||||
private val prefs: Preferences,
|
||||
private val behavior: ListHabitsSelectionMenuBehavior,
|
||||
private val listController: Lazy<HabitCardListController>
|
||||
private val listController: Lazy<HabitCardListController>,
|
||||
private val notificationTray: NotificationTray
|
||||
) : BaseSelectionMenu() {
|
||||
|
||||
override fun onFinish() {
|
||||
@@ -69,6 +74,12 @@ class ListHabitsSelectionMenu @Inject constructor(
|
||||
return true
|
||||
}
|
||||
|
||||
R.id.action_notify -> {
|
||||
for(h in listAdapter.selected)
|
||||
notificationTray.show(h, DateUtils.getToday(), 0)
|
||||
return true
|
||||
}
|
||||
|
||||
else -> return false
|
||||
}
|
||||
}
|
||||
@@ -78,12 +89,14 @@ class ListHabitsSelectionMenu @Inject constructor(
|
||||
val itemColor = menu.findItem(R.id.action_color)
|
||||
val itemArchive = menu.findItem(R.id.action_archive_habit)
|
||||
val itemUnarchive = menu.findItem(R.id.action_unarchive_habit)
|
||||
val itemNotify = menu.findItem(R.id.action_notify)
|
||||
|
||||
itemColor.isVisible = true
|
||||
itemEdit.isVisible = behavior.canEdit()
|
||||
itemArchive.isVisible = behavior.canArchive()
|
||||
itemUnarchive.isVisible = behavior.canUnarchive()
|
||||
setTitle(Integer.toString(listAdapter.selected.size))
|
||||
itemNotify.isVisible = prefs.isDeveloper
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -44,8 +44,15 @@ class AndroidNotificationTray
|
||||
private val ringtoneManager: RingtoneManager
|
||||
) : NotificationTray.SystemTray {
|
||||
|
||||
private var active = HashSet<Int>()
|
||||
|
||||
override fun removeNotification(id: Int) {
|
||||
NotificationManagerCompat.from(context).cancel(id)
|
||||
val manager = NotificationManagerCompat.from(context)
|
||||
manager.cancel(id)
|
||||
active.remove(id)
|
||||
|
||||
// Clear the group summary notification
|
||||
if(active.isEmpty()) manager.cancelAll()
|
||||
}
|
||||
|
||||
override fun showNotification(habit: Habit,
|
||||
@@ -58,6 +65,7 @@ class AndroidNotificationTray
|
||||
notificationManager.notify(Int.MAX_VALUE, summary)
|
||||
val notification = buildNotification(habit, reminderTime, timestamp)
|
||||
notificationManager.notify(notificationId, notification)
|
||||
active.add(notificationId)
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@@ -79,8 +87,7 @@ class AndroidNotificationTray
|
||||
val removeRepetitionAction = Action(
|
||||
R.drawable.ic_action_cancel,
|
||||
context.getString(R.string.no),
|
||||
pendingIntents.removeRepetition(habit)
|
||||
)
|
||||
pendingIntents.removeRepetition(habit))
|
||||
|
||||
val wearableBg = decodeResource(context.resources, R.drawable.stripe)
|
||||
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package org.isoron.uhabits.notifications;
|
||||
|
||||
|
||||
import android.app.*;
|
||||
import android.os.*;
|
||||
import android.support.annotation.*;
|
||||
import android.support.v4.app.*;
|
||||
import android.text.format.*;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
|
||||
import com.android.datetimepicker.time.TimePickerDialog;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.receivers.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static android.content.ContentUris.parseId;
|
||||
|
||||
public class SnoozeDelayPickerActivity extends FragmentActivity
|
||||
implements AdapterView.OnItemClickListener
|
||||
{
|
||||
private Habit habit;
|
||||
|
||||
private ReminderController reminderController;
|
||||
|
||||
@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();
|
||||
|
||||
int theme = R.style.Theme_AppCompat_Light_Dialog_Alert;
|
||||
AlertDialog dialog = new AlertDialog.Builder(new ContextThemeWrapper(this, theme))
|
||||
.setTitle(R.string.select_snooze_delay)
|
||||
.setItems(R.array.snooze_picker_names, null)
|
||||
.create();
|
||||
|
||||
dialog.getListView().setOnItemClickListener(this);
|
||||
dialog.setOnDismissListener(d -> finish());
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private void showTimePicker()
|
||||
{
|
||||
final Calendar calendar = Calendar.getInstance();
|
||||
TimePickerDialog dialog = TimePickerDialog.newInstance(
|
||||
(view, hour, minute) -> {
|
||||
reminderController.onSnoozeTimePicked(habit, hour, minute);
|
||||
finish();
|
||||
},
|
||||
calendar.get(Calendar.HOUR_OF_DAY),
|
||||
calendar.get(Calendar.MINUTE),
|
||||
DateFormat.is24HourFormat(this));
|
||||
dialog.show(getSupportFragmentManager(), "timePicker");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
|
||||
{
|
||||
int[] snoozeValues = getResources().getIntArray(R.array.snooze_picker_values);
|
||||
if (snoozeValues[position] >= 0)
|
||||
{
|
||||
reminderController.onSnoozeDelayPicked(habit, snoozeValues[position]);
|
||||
finish();
|
||||
}
|
||||
else showTimePicker();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish()
|
||||
{
|
||||
super.finish();
|
||||
overridePendingTransition(0, 0);
|
||||
}
|
||||
}
|
||||
@@ -19,19 +19,21 @@
|
||||
|
||||
package org.isoron.uhabits.receivers;
|
||||
|
||||
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
|
||||
@@ -40,6 +42,7 @@ public class ReminderController
|
||||
@NonNull
|
||||
private final NotificationTray notificationTray;
|
||||
|
||||
@NonNull
|
||||
private Preferences preferences;
|
||||
|
||||
@Inject
|
||||
@@ -65,14 +68,25 @@ public class ReminderController
|
||||
reminderScheduler.scheduleAll();
|
||||
}
|
||||
|
||||
public void onSnooze(@NonNull Habit habit)
|
||||
public void onSnoozePressed(@NonNull Habit habit, final Context context)
|
||||
{
|
||||
long snoozeInterval = preferences.getSnoozeInterval();
|
||||
long delay = preferences.getSnoozeInterval();
|
||||
|
||||
long now = applyTimezone(getLocalTime());
|
||||
long reminderTime = now + snoozeInterval * 60 * 1000;
|
||||
if (delay < 0)
|
||||
showSnoozeDelayPicker(habit, context);
|
||||
else
|
||||
scheduleReminderMinutesFromNow(habit, delay);
|
||||
}
|
||||
|
||||
reminderScheduler.schedule(habit, reminderTime);
|
||||
public void onSnoozeDelayPicked(Habit habit, int delay)
|
||||
{
|
||||
scheduleReminderMinutesFromNow(habit, delay);
|
||||
}
|
||||
|
||||
public void onSnoozeTimePicked(Habit habit, int hour, int minute)
|
||||
{
|
||||
Long time = DateUtils.getUpcomingTimeInMillis(hour, minute);
|
||||
reminderScheduler.scheduleAtTime(habit, time);
|
||||
notificationTray.cancel(habit);
|
||||
}
|
||||
|
||||
@@ -80,4 +94,19 @@ public class ReminderController
|
||||
{
|
||||
notificationTray.cancel(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(context, SnoozeDelayPickerActivity.class);
|
||||
intent.setData(Uri.parse(habit.getUriString()));
|
||||
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.*;
|
||||
|
||||
/**
|
||||
@@ -38,30 +37,26 @@ import static android.content.ContentUris.*;
|
||||
public class ReminderReceiver extends BroadcastReceiver
|
||||
{
|
||||
public static final String ACTION_DISMISS_REMINDER =
|
||||
"org.isoron.uhabits.ACTION_DISMISS_REMINDER";
|
||||
"org.isoron.uhabits.ACTION_DISMISS_REMINDER";
|
||||
|
||||
public static final String ACTION_SHOW_REMINDER =
|
||||
"org.isoron.uhabits.ACTION_SHOW_REMINDER";
|
||||
"org.isoron.uhabits.ACTION_SHOW_REMINDER";
|
||||
|
||||
public static final String ACTION_SNOOZE_REMINDER =
|
||||
"org.isoron.uhabits.ACTION_SNOOZE_REMINDER";
|
||||
"org.isoron.uhabits.ACTION_SNOOZE_REMINDER";
|
||||
|
||||
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();
|
||||
if (context == null || intent == null) return;
|
||||
if (intent.getAction() == null) return;
|
||||
|
||||
ReminderComponent component = DaggerReminderReceiver_ReminderComponent
|
||||
.builder()
|
||||
.habitsApplicationComponent(app.getComponent())
|
||||
.build();
|
||||
|
||||
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()));
|
||||
|
||||
@@ -80,7 +75,7 @@ public class ReminderReceiver extends BroadcastReceiver
|
||||
case ACTION_SHOW_REMINDER:
|
||||
if (habit == null) return;
|
||||
reminderController.onShowReminder(habit,
|
||||
new Timestamp(timestamp), reminderTime);
|
||||
new Timestamp(timestamp), reminderTime);
|
||||
break;
|
||||
|
||||
case ACTION_DISMISS_REMINDER:
|
||||
@@ -90,7 +85,7 @@ public class ReminderReceiver extends BroadcastReceiver
|
||||
|
||||
case ACTION_SNOOZE_REMINDER:
|
||||
if (habit == null) return;
|
||||
reminderController.onSnooze(habit);
|
||||
reminderController.onSnoozePressed(habit, context);
|
||||
break;
|
||||
|
||||
case Intent.ACTION_BOOT_COMPLETED:
|
||||
@@ -103,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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,4 +46,9 @@
|
||||
android:title="@string/delete"
|
||||
app:showAsAction="never"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/action_notify"
|
||||
android:title="@string/reminder"
|
||||
app:showAsAction="never"/>
|
||||
|
||||
</menu>
|
||||
@@ -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_picker_names">
|
||||
<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_picker_values" 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,9 +70,9 @@ public class ReminderControllerTest extends BaseAndroidJVMTest
|
||||
DateUtils.setFixedLocalTime(now);
|
||||
when(preferences.getSnoozeInterval()).thenReturn(15L);
|
||||
|
||||
controller.onSnooze(habit);
|
||||
controller.onSnoozePressed(habit,null);
|
||||
|
||||
verify(reminderScheduler).schedule(habit, nowTz + 900000);
|
||||
verify(reminderScheduler).scheduleAtTime(habit, nowTz + 900000);
|
||||
verify(notificationTray).cancel(habit);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user