mirror of https://github.com/iSoron/uhabits.git
parent
61b0b1fdea
commit
77f406dcee
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue