Refactor broadcast receivers and add a public receiver

Fixes #6
pull/151/head
Alinson S. Xavier 9 years ago
parent 61b0b1fdea
commit 77f406dcee

@ -29,6 +29,7 @@ import com.getpebble.android.kit.util.*;
import org.isoron.uhabits.*; import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*; import org.isoron.uhabits.models.*;
import org.isoron.uhabits.receivers.*;
import org.json.*; import org.json.*;
import org.junit.*; import org.junit.*;
import org.junit.runner.*; import org.junit.runner.*;

@ -42,8 +42,8 @@
android:backupAgent=".HabitsBackupAgent" android:backupAgent=".HabitsBackupAgent"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/main_activity_title" android:label="@string/main_activity_title"
android:theme="@style/AppBaseTheme" android:supportsRtl="true"
android:supportsRtl="true"> android:theme="@style/AppBaseTheme">
<meta-data <meta-data
android:name="com.google.android.backup.api_key" android:name="com.google.android.backup.api_key"
@ -156,18 +156,37 @@
android:resource="@xml/widget_frequency_info"/> android:resource="@xml/widget_frequency_info"/>
</receiver> </receiver>
<receiver android:name=".HabitBroadcastReceiver"> <receiver android:name=".receivers.ReminderReceiver">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/> <action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter> </intent-filter>
</receiver> </receiver>
<receiver android:name=".pebble.PebbleReceiver"> <receiver android:name=".receivers.WidgetReceiver">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="org.isoron.uhabits.ACTION_TOGGLE_REPETITION"/>
<data android:host="org.isoron.uhabits" android:scheme="content"/>
</intent-filter>
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="org.isoron.uhabits.ACTION_ADD_REPETITION"/>
<data android:host="org.isoron.uhabits" android:scheme="content"/>
</intent-filter>
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="org.isoron.uhabits.ACTION_REMOVE_REPETITION"/>
<data android:host="org.isoron.uhabits" android:scheme="content"/>
</intent-filter>
</receiver>
<receiver android:name=".receivers.PebbleReceiver">
<intent-filter> <intent-filter>
<action android:name="com.getpebble.action.app.RECEIVE"/> <action android:name="com.getpebble.action.app.RECEIVE"/>
</intent-filter> </intent-filter>
</receiver> </receiver>
</application> </application>
</manifest> </manifest>

@ -22,7 +22,7 @@ package org.isoron.uhabits;
import org.isoron.uhabits.commands.*; import org.isoron.uhabits.commands.*;
import org.isoron.uhabits.io.*; import org.isoron.uhabits.io.*;
import org.isoron.uhabits.models.*; import org.isoron.uhabits.models.*;
import org.isoron.uhabits.pebble.*; import org.isoron.uhabits.receivers.*;
import org.isoron.uhabits.tasks.*; import org.isoron.uhabits.tasks.*;
import org.isoron.uhabits.ui.*; import org.isoron.uhabits.ui.*;
import org.isoron.uhabits.ui.habits.edit.*; import org.isoron.uhabits.ui.habits.edit.*;
@ -49,7 +49,7 @@ public interface BaseComponent
void inject(HabitCardListCache habitCardListCache); void inject(HabitCardListCache habitCardListCache);
void inject(HabitBroadcastReceiver habitBroadcastReceiver); void inject(WidgetReceiver widgetReceiver);
void inject(ListHabitsSelectionMenu listHabitsSelectionMenu); void inject(ListHabitsSelectionMenu listHabitsSelectionMenu);
@ -102,4 +102,8 @@ public interface BaseComponent
void inject(PebbleReceiver receiver); void inject(PebbleReceiver receiver);
void inject(HeaderView headerView); void inject(HeaderView headerView);
void inject(ReceiverActions receiverActions);
void inject(ReminderReceiver reminderReceiver);
} }

@ -24,12 +24,12 @@ import android.content.*;
import android.net.*; import android.net.*;
import org.isoron.uhabits.models.*; import org.isoron.uhabits.models.*;
import org.isoron.uhabits.receivers.*;
import org.isoron.uhabits.ui.habits.show.*; import org.isoron.uhabits.ui.habits.show.*;
public abstract class HabitPendingIntents public abstract class HabitPendingIntents
{ {
public static final String BASE_URL =
private static final String BASE_URL =
"content://org.isoron.uhabits/habit/"; "content://org.isoron.uhabits/habit/";
public static PendingIntent addCheckmark(Context context, public static PendingIntent addCheckmark(Context context,
@ -37,9 +37,9 @@ public abstract class HabitPendingIntents
Long timestamp) Long timestamp)
{ {
Uri data = habit.getUri(); Uri data = habit.getUri();
Intent checkIntent = new Intent(context, HabitBroadcastReceiver.class); Intent checkIntent = new Intent(context, WidgetReceiver.class);
checkIntent.setData(data); checkIntent.setData(data);
checkIntent.setAction(HabitBroadcastReceiver.ACTION_CHECK); checkIntent.setAction(WidgetReceiver.ACTION_ADD_REPETITION);
if (timestamp != null) checkIntent.putExtra("timestamp", timestamp); if (timestamp != null) checkIntent.putExtra("timestamp", timestamp);
return PendingIntent.getBroadcast(context, 1, checkIntent, return PendingIntent.getBroadcast(context, 1, checkIntent,
PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT);
@ -47,8 +47,8 @@ public abstract class HabitPendingIntents
public static PendingIntent dismissNotification(Context context) public static PendingIntent dismissNotification(Context context)
{ {
Intent deleteIntent = new Intent(context, HabitBroadcastReceiver.class); Intent deleteIntent = new Intent(context, ReminderReceiver.class);
deleteIntent.setAction(HabitBroadcastReceiver.ACTION_DISMISS); deleteIntent.setAction(WidgetReceiver.ACTION_DISMISS_REMINDER);
return PendingIntent.getBroadcast(context, 0, deleteIntent, return PendingIntent.getBroadcast(context, 0, deleteIntent,
PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT);
} }
@ -56,9 +56,9 @@ public abstract class HabitPendingIntents
public static PendingIntent snoozeNotification(Context context, Habit habit) public static PendingIntent snoozeNotification(Context context, Habit habit)
{ {
Uri data = habit.getUri(); Uri data = habit.getUri();
Intent snoozeIntent = new Intent(context, HabitBroadcastReceiver.class); Intent snoozeIntent = new Intent(context, ReminderReceiver.class);
snoozeIntent.setData(data); snoozeIntent.setData(data);
snoozeIntent.setAction(HabitBroadcastReceiver.ACTION_SNOOZE); snoozeIntent.setAction(ReminderReceiver.ACTION_SNOOZE_REMINDER);
return PendingIntent.getBroadcast(context, 0, snoozeIntent, return PendingIntent.getBroadcast(context, 0, snoozeIntent,
PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT);
} }
@ -68,9 +68,9 @@ public abstract class HabitPendingIntents
Long timestamp) Long timestamp)
{ {
Uri data = habit.getUri(); Uri data = habit.getUri();
Intent checkIntent = new Intent(context, HabitBroadcastReceiver.class); Intent checkIntent = new Intent(context, WidgetReceiver.class);
checkIntent.setData(data); checkIntent.setData(data);
checkIntent.setAction(HabitBroadcastReceiver.ACTION_TOGGLE); checkIntent.setAction(WidgetReceiver.ACTION_TOGGLE_REPETITION);
if (timestamp != null) checkIntent.putExtra("timestamp", timestamp); if (timestamp != null) checkIntent.putExtra("timestamp", timestamp);
return PendingIntent.getBroadcast(context, 2, checkIntent, return PendingIntent.getBroadcast(context, 2, checkIntent,
PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT);

@ -0,0 +1,79 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.receivers;
import android.content.*;
import android.net.*;
import android.support.annotation.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.utils.*;
public class IntentParser
{
private HabitList habits;
public IntentParser(@NonNull HabitList habits)
{
this.habits = habits;
}
public CheckmarkIntentData parseCheckmarkIntent(@NonNull Intent intent)
{
Uri uri = intent.getData();
CheckmarkIntentData data = new CheckmarkIntentData();
data.habit = parseHabit(uri);
data.timestamp = parseTimestamp(intent);
return data;
}
@NonNull
protected Habit parseHabit(@NonNull Uri uri)
{
long habitId = ContentUris.parseId(uri);
Habit habit = habits.getById(habitId);
if (habit == null)
throw new IllegalArgumentException("habit not found");
return habit;
}
@NonNull
protected Long parseTimestamp(@NonNull Intent intent)
{
long today = DateUtils.getStartOfToday();
Long timestamp = intent.getLongExtra("timestamp", today);
timestamp = DateUtils.getStartOfDay(timestamp);
if (timestamp < 0 || timestamp > today)
throw new IllegalArgumentException("timestamp is not valid");
return timestamp;
}
class CheckmarkIntentData
{
public Habit habit;
public Long timestamp;
}
}

@ -17,7 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.isoron.uhabits.pebble; package org.isoron.uhabits.receivers;
import android.content.*; import android.content.*;
import android.support.annotation.*; import android.support.annotation.*;

@ -0,0 +1,62 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.receivers;
import android.support.annotation.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.commands.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.tasks.*;
import javax.inject.*;
public class ReceiverActions
{
@Inject
CommandRunner commandRunner;
public ReceiverActions()
{
HabitsApplication.getComponent().inject(this);
}
public void add_repetition(@NonNull Habit habit, long timestamp)
{
Repetition rep = habit.getRepetitions().getByTimestamp(timestamp);
if (rep != null) return;
toggle_repetition(habit, timestamp);
}
public void remove_repetition(@NonNull Habit habit, long timestamp)
{
Repetition rep = habit.getRepetitions().getByTimestamp(timestamp);
if (rep == null) return;
toggle_repetition(habit, timestamp);
}
public void toggle_repetition(@NonNull Habit habit, long timestamp)
{
new SimpleTask(() -> {
commandRunner.execute(new ToggleRepetitionCommand(habit, timestamp),
habit.getId());
}).execute();
}
}

@ -17,7 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.isoron.uhabits; package org.isoron.uhabits.receivers;
import android.app.*; import android.app.*;
import android.content.*; import android.content.*;
@ -26,8 +26,9 @@ import android.net.*;
import android.os.*; import android.os.*;
import android.preference.*; import android.preference.*;
import android.support.v4.app.*; import android.support.v4.app.*;
import android.util.*;
import org.isoron.uhabits.commands.*; import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*; import org.isoron.uhabits.models.*;
import org.isoron.uhabits.tasks.*; import org.isoron.uhabits.tasks.*;
import org.isoron.uhabits.utils.*; import org.isoron.uhabits.utils.*;
@ -41,29 +42,23 @@ import javax.inject.*;
* <p> * <p>
* All broadcast messages are received and processed by this class. * All broadcast messages are received and processed by this class.
*/ */
public class HabitBroadcastReceiver extends BroadcastReceiver public class ReminderReceiver extends BroadcastReceiver
{ {
public static final String ACTION_CHECK = "org.isoron.uhabits.ACTION_CHECK"; public static final String ACTION_DISMISS_REMINDER =
"org.isoron.uhabits.ACTION_DISMISS_REMINDER";
public static final String ACTION_DISMISS =
"org.isoron.uhabits.ACTION_DISMISS";
public static final String ACTION_SHOW_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 = public static final String ACTION_SNOOZE_REMINDER =
"org.isoron.uhabits.ACTION_SNOOZE"; "org.isoron.uhabits.ACTION_SNOOZE_REMINDER";
public static final String ACTION_TOGGLE = private static final String TAG = "ReminderReceiver";
"org.isoron.uhabits.ACTION_TOGGLE";
@Inject @Inject
HabitList habits; HabitList habits;
@Inject public ReminderReceiver()
CommandRunner commandRunner;
public HabitBroadcastReceiver()
{ {
super(); super();
HabitsApplication.getComponent().inject(this); HabitsApplication.getComponent().inject(this);
@ -71,80 +66,45 @@ public class HabitBroadcastReceiver extends BroadcastReceiver
@Override @Override
public void onReceive(final Context context, Intent intent) public void onReceive(final Context context, Intent intent)
{
Log.i(TAG, String.format("Received intent: %s", intent.toString()));
try
{ {
switch (intent.getAction()) switch (intent.getAction())
{ {
case ACTION_SHOW_REMINDER: case ACTION_SHOW_REMINDER:
createNotification(context, intent); onActionShowReminder(context, intent);
createReminderAlarmsDelayed(context);
break; break;
case ACTION_DISMISS: case ACTION_DISMISS_REMINDER:
// NOP // NOP
break; break;
case ACTION_CHECK: case ACTION_SNOOZE_REMINDER:
addRepetition(context, intent); onActionSnoozeReminder(context, intent);
break;
case ACTION_TOGGLE:
toggleRepetition(context, intent);
break;
case ACTION_SNOOZE:
snoozeHabit(context, intent);
break; break;
case Intent.ACTION_BOOT_COMPLETED: case Intent.ACTION_BOOT_COMPLETED:
ReminderUtils.createReminderAlarms(context, habits); onActionBootCompleted(context);
break; break;
} }
} }
catch (RuntimeException e)
private void addOrRemoveRepetition(Context context,
Intent intent,
boolean abortIfExists)
{
Uri data = intent.getData();
long today = DateUtils.getStartOfToday();
Long timestamp = intent.getLongExtra("timestamp", today);
long habitId = ContentUris.parseId(data);
Habit habit = habits.getById(habitId);
try
{ {
if (habit == null) return; Log.e(TAG, "could not process intent", e);
Repetition rep = habit.getRepetitions().getByTimestamp(timestamp);
if (abortIfExists && rep != null) return;
commandRunner.execute(new ToggleRepetitionCommand(habit, timestamp),
habitId);
}
finally
{
dismissNotification(context, habitId);
} }
} }
private void addRepetition(Context context, Intent intent) protected void onActionBootCompleted(Context context)
{ {
addOrRemoveRepetition(context, intent, true); ReminderUtils.createReminderAlarms(context, habits);
} }
private boolean checkWeekday(Intent intent, Habit habit) protected void onActionShowReminder(Context context, Intent intent)
{ {
if (!habit.hasReminder()) return false; createNotification(context, intent);
Reminder reminder = habit.getReminder(); createReminderAlarmsDelayed(context);
Long timestamp =
intent.getLongExtra("timestamp", DateUtils.getStartOfToday());
boolean reminderDays[] =
DateUtils.unpackWeekdayList(reminder.getDays());
int weekday = DateUtils.getWeekday(timestamp);
return reminderDays[weekday];
} }
private void createNotification(final Context context, final Intent intent) private void createNotification(final Context context, final Intent intent)
@ -172,7 +132,7 @@ public class HabitBroadcastReceiver extends BroadcastReceiver
protected void onPostExecute(Void aVoid) protected void onPostExecute(Void aVoid)
{ {
if (todayValue != Checkmark.UNCHECKED) return; if (todayValue != Checkmark.UNCHECKED) return;
if (!checkWeekday(intent, habit)) return; if (!shouldShowReminderToday(intent, habit)) return;
if (!habit.hasReminder()) return; if (!habit.hasReminder()) return;
Intent contentIntent = new Intent(context, MainActivity.class); Intent contentIntent = new Intent(context, MainActivity.class);
@ -185,8 +145,7 @@ public class HabitBroadcastReceiver extends BroadcastReceiver
dismissPendingIntent = dismissPendingIntent =
HabitPendingIntents.dismissNotification(context); HabitPendingIntents.dismissNotification(context);
PendingIntent checkIntentPending = PendingIntent checkIntentPending =
HabitPendingIntents.addCheckmark(context, habit, HabitPendingIntents.addCheckmark(context, habit, timestamp);
timestamp);
PendingIntent snoozeIntentPending = PendingIntent snoozeIntentPending =
HabitPendingIntents.snoozeNotification(context, habit); HabitPendingIntents.snoozeNotification(context, habit);
@ -246,7 +205,7 @@ public class HabitBroadcastReceiver extends BroadcastReceiver
notificationManager.cancel(notificationId); notificationManager.cancel(notificationId);
} }
private void snoozeHabit(Context context, Intent intent) private void onActionSnoozeReminder(Context context, Intent intent)
{ {
Uri data = intent.getData(); Uri data = intent.getData();
SharedPreferences prefs = SharedPreferences prefs =
@ -261,8 +220,18 @@ public class HabitBroadcastReceiver extends BroadcastReceiver
dismissNotification(context, habitId); dismissNotification(context, habitId);
} }
private void toggleRepetition(Context context, Intent intent) private boolean shouldShowReminderToday(Intent intent, Habit habit)
{ {
addOrRemoveRepetition(context, intent, false); if (!habit.hasReminder()) return false;
Reminder reminder = habit.getReminder();
Long timestamp =
intent.getLongExtra("timestamp", DateUtils.getStartOfToday());
boolean reminderDays[] =
DateUtils.unpackWeekdayList(reminder.getDays());
int weekday = DateUtils.getWeekday(timestamp);
return reminderDays[weekday];
} }
} }

@ -0,0 +1,115 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.receivers;
import android.content.*;
import android.support.annotation.*;
import android.util.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*;
import javax.inject.*;
/**
* The Android BroadcastReceiver for Loop Habit Tracker.
* <p>
* All broadcast messages are received and processed by this class.
*/
public class WidgetReceiver extends BroadcastReceiver
{
public static final String ACTION_ADD_REPETITION =
"org.isoron.uhabits.ACTION_ADD_REPETITION";
public static final String ACTION_DISMISS_REMINDER =
"org.isoron.uhabits.ACTION_DISMISS_REMINDER";
public static final String ACTION_REMOVE_REPETITION =
"org.isoron.uhabits.ACTION_REMOVE_REPETITION";
public static final String ACTION_TOGGLE_REPETITION =
"org.isoron.uhabits.ACTION_TOGGLE_REPETITION";
@Inject
HabitList habits;
@NonNull
private final IntentParser parser;
@NonNull
private final ReceiverActions actions;
public WidgetReceiver()
{
super();
HabitsApplication.getComponent().inject(this);
parser = new IntentParser(habits);
actions = new ReceiverActions();
}
@Override
public void onReceive(final Context context, Intent intent)
{
Log.d("WidgetReceiver",
String.format("Received intent: %s", intent.toString()));
try
{
switch (intent.getAction())
{
case ACTION_ADD_REPETITION:
onActionAddRepetition(intent);
break;
case ACTION_TOGGLE_REPETITION:
onActionToggleRepetition(intent);
break;
case ACTION_REMOVE_REPETITION:
onActionRemoveRepetition(intent);
break;
}
}
catch (RuntimeException e)
{
Log.e("WidgetReceiver", "could not process intent", e);
}
}
private void onActionAddRepetition(Intent intent)
{
IntentParser.CheckmarkIntentData data;
data = parser.parseCheckmarkIntent(intent);
actions.add_repetition(data.habit, data.timestamp);
}
private void onActionRemoveRepetition(Intent intent)
{
IntentParser.CheckmarkIntentData data;
data = parser.parseCheckmarkIntent(intent);
actions.remove_repetition(data.habit, data.timestamp);
}
private void onActionToggleRepetition(Intent intent)
{
IntentParser.CheckmarkIntentData data;
data = parser.parseCheckmarkIntent(intent);
actions.toggle_repetition(data.habit, data.timestamp);
}
}

@ -32,6 +32,7 @@ import android.util.*;
import org.isoron.uhabits.*; import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*; import org.isoron.uhabits.models.*;
import org.isoron.uhabits.receivers.*;
import java.text.*; import java.text.*;
import java.util.*; import java.util.*;
@ -64,8 +65,8 @@ public abstract class ReminderUtils
Uri uri = habit.getUri(); Uri uri = habit.getUri();
Intent alarmIntent = new Intent(context, HabitBroadcastReceiver.class); Intent alarmIntent = new Intent(context, ReminderReceiver.class);
alarmIntent.setAction(HabitBroadcastReceiver.ACTION_SHOW_REMINDER); alarmIntent.setAction(ReminderReceiver.ACTION_SHOW_REMINDER);
alarmIntent.setData(uri); alarmIntent.setData(uri);
alarmIntent.putExtra("timestamp", timestamp); alarmIntent.putExtra("timestamp", timestamp);
alarmIntent.putExtra("reminderTime", reminderTime); alarmIntent.putExtra("reminderTime", reminderTime);

Loading…
Cancel
Save