Skip to content

Commit

Permalink
Refactor broadcast receivers and add a public receiver
Browse files Browse the repository at this point in the history
Fixes iSoron#6
  • Loading branch information
iSoron committed Jul 17, 2016
1 parent 61b0b1f commit 77f406d
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.receivers.*;
import org.json.*;
import org.junit.*;
import org.junit.runner.*;
Expand Down
41 changes: 30 additions & 11 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@

<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
android:maxSdkVersion="18"/>

<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
android:maxSdkVersion="18"/>

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
android:name="HabitsApplication"
android:allowBackup="true"
android:backupAgent=".HabitsBackupAgent"
android:icon="@mipmap/ic_launcher"
android:label="@string/main_activity_title"
android:theme="@style/AppBaseTheme"
android:supportsRtl="true">
android:supportsRtl="true"
android:theme="@style/AppBaseTheme">

<meta-data
android:name="com.google.android.backup.api_key"
Expand Down Expand Up @@ -93,7 +93,7 @@
android:label="@string/about">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
android:value=".MainActivity"/>
</activity>

<receiver
Expand Down Expand Up @@ -156,18 +156,37 @@
android:resource="@xml/widget_frequency_info"/>
</receiver>

<receiver android:name=".HabitBroadcastReceiver">
<receiver android:name=".receivers.ReminderReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

<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>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<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=".pebble.PebbleReceiver">
<receiver android:name=".receivers.PebbleReceiver">
<intent-filter>
<action android:name="com.getpebble.action.app.RECEIVE" />
<action android:name="com.getpebble.action.app.RECEIVE"/>
</intent-filter>
</receiver>

</application>

</manifest>
8 changes: 6 additions & 2 deletions app/src/main/java/org/isoron/uhabits/BaseComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.isoron.uhabits.commands.*;
import org.isoron.uhabits.io.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.pebble.*;
import org.isoron.uhabits.receivers.*;
import org.isoron.uhabits.tasks.*;
import org.isoron.uhabits.ui.*;
import org.isoron.uhabits.ui.habits.edit.*;
Expand All @@ -49,7 +49,7 @@ public interface BaseComponent

void inject(HabitCardListCache habitCardListCache);

void inject(HabitBroadcastReceiver habitBroadcastReceiver);
void inject(WidgetReceiver widgetReceiver);

void inject(ListHabitsSelectionMenu listHabitsSelectionMenu);

Expand Down Expand Up @@ -102,4 +102,8 @@ public interface BaseComponent
void inject(PebbleReceiver receiver);

void inject(HeaderView headerView);

void inject(ReceiverActions receiverActions);

void inject(ReminderReceiver reminderReceiver);
}
20 changes: 10 additions & 10 deletions app/src/main/java/org/isoron/uhabits/HabitPendingIntents.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,41 @@
import android.net.*;

import org.isoron.uhabits.models.*;
import org.isoron.uhabits.receivers.*;
import org.isoron.uhabits.ui.habits.show.*;

public abstract class HabitPendingIntents
{

private static final String BASE_URL =
public static final String BASE_URL =
"content://org.isoron.uhabits/habit/";

public static PendingIntent addCheckmark(Context context,
Habit habit,
Long timestamp)
{
Uri data = habit.getUri();
Intent checkIntent = new Intent(context, HabitBroadcastReceiver.class);
Intent checkIntent = new Intent(context, WidgetReceiver.class);
checkIntent.setData(data);
checkIntent.setAction(HabitBroadcastReceiver.ACTION_CHECK);
checkIntent.setAction(WidgetReceiver.ACTION_ADD_REPETITION);
if (timestamp != null) checkIntent.putExtra("timestamp", timestamp);
return PendingIntent.getBroadcast(context, 1, checkIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
}

public static PendingIntent dismissNotification(Context context)
{
Intent deleteIntent = new Intent(context, HabitBroadcastReceiver.class);
deleteIntent.setAction(HabitBroadcastReceiver.ACTION_DISMISS);
Intent deleteIntent = new Intent(context, ReminderReceiver.class);
deleteIntent.setAction(WidgetReceiver.ACTION_DISMISS_REMINDER);
return PendingIntent.getBroadcast(context, 0, deleteIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
}

public static PendingIntent snoozeNotification(Context context, Habit habit)
{
Uri data = habit.getUri();
Intent snoozeIntent = new Intent(context, HabitBroadcastReceiver.class);
Intent snoozeIntent = new Intent(context, ReminderReceiver.class);
snoozeIntent.setData(data);
snoozeIntent.setAction(HabitBroadcastReceiver.ACTION_SNOOZE);
snoozeIntent.setAction(ReminderReceiver.ACTION_SNOOZE_REMINDER);
return PendingIntent.getBroadcast(context, 0, snoozeIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
Expand All @@ -68,9 +68,9 @@ public static PendingIntent toggleCheckmark(Context context,
Long timestamp)
{
Uri data = habit.getUri();
Intent checkIntent = new Intent(context, HabitBroadcastReceiver.class);
Intent checkIntent = new Intent(context, WidgetReceiver.class);
checkIntent.setData(data);
checkIntent.setAction(HabitBroadcastReceiver.ACTION_TOGGLE);
checkIntent.setAction(WidgetReceiver.ACTION_TOGGLE_REPETITION);
if (timestamp != null) checkIntent.putExtra("timestamp", timestamp);
return PendingIntent.getBroadcast(context, 2, checkIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Expand Down
79 changes: 79 additions & 0 deletions app/src/main/java/org/isoron/uhabits/receivers/IntentParser.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* 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.support.annotation.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading

0 comments on commit 77f406d

Please sign in to comment.