Skip to content

Commit

Permalink
Choose state of filter at boot based on time
Browse files Browse the repository at this point in the history
See #34

When automatic filter is enabled and the device is booted, it doesn't
restore the state from before the reboot, but determines if it is
currently 'on-time' or 'off-time'. This is the simple logic as described
in smichel17's post in #34.
  • Loading branch information
raatmarien committed Apr 9, 2016
1 parent ca575a5 commit 7158a3f
Showing 1 changed file with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import android.preference.PreferenceManager;
import android.util.Log;

import java.util.Calendar;

import com.jmstudios.redmoon.helper.FilterCommandFactory;
import com.jmstudios.redmoon.helper.FilterCommandSender;
import com.jmstudios.redmoon.model.SettingsModel;
Expand Down Expand Up @@ -90,7 +92,8 @@ public void onReceive(Context context, Intent intent) {
AutomaticFilterChangeReceiver.scheduleNextOnCommand(context);
AutomaticFilterChangeReceiver.scheduleNextPauseCommand(context);

commandSender.send(pausedBeforeReboot ? pauseCommand : onCommand);
commandSender.send(getPredictedPauseState(pausedBeforeReboot, settingsModel) ?
pauseCommand : onCommand);
} else {
if (DEBUG) Log.i(TAG, "Shades was off before reboot; no state to resume from.");
}
Expand All @@ -101,4 +104,41 @@ public void onReceive(Context context, Intent intent) {
Intent offCommand = commandFactory.createCommand(ScreenFilterService.COMMAND_OFF);
commandSender.send(offCommand);
}

private static boolean getPredictedPauseState(boolean pausedBeforeReboot,
SettingsModel model) {
if (model.getAutomaticFilterMode() == "never") {
return pausedBeforeReboot;
} else {
Calendar now = Calendar.getInstance();

String onTime = model.getAutomaticTurnOnTime();
int onHour = Integer.parseInt(onTime.split(":")[0]);
int onMinute = Integer.parseInt(onTime.split(":")[1]);
Calendar on = Calendar.getInstance();
on.set(Calendar.HOUR_OF_DAY, onHour);
on.set(Calendar.MINUTE, onMinute);

if (on.after(now))
on.add(Calendar.DATE, -1);

String offTime = model.getAutomaticTurnOffTime();
int offHour = Integer.parseInt(offTime.split(":")[0]);
int offMinute = Integer.parseInt(offTime.split(":")[1]);
Calendar off = Calendar.getInstance();
off.set(Calendar.HOUR_OF_DAY, offHour);
off.set(Calendar.MINUTE, offMinute);

while (off.before(on))
off.add(Calendar.DATE, 1);

if (DEBUG) {
Log.d(TAG, "On: " + onTime + ", off: " + offTime);
Log.d(TAG, "On DAY_OF_MONTH: " + Integer.toString(on.get(Calendar.DAY_OF_MONTH)));
Log.d(TAG, "Off DAY_OF_MONTH: " + Integer.toString(off.get(Calendar.DAY_OF_MONTH)));
}

return !(now.after(on) && now.before(off));
}
}
}

0 comments on commit 7158a3f

Please sign in to comment.