pull/212/head
M66B 3 years ago
parent 4ff914e6a8
commit fdaa270b80

@ -433,14 +433,18 @@ public class EntityRule {
// Schedule // Schedule
JSONObject jschedule = jcondition.optJSONObject("schedule"); JSONObject jschedule = jcondition.optJSONObject("schedule");
if (jschedule != null) { if (jschedule != null) {
boolean all = jschedule.optBoolean("all", false);
int start = jschedule.optInt("start", 0); int start = jschedule.optInt("start", 0);
int end = jschedule.optInt("end", 0); int end = jschedule.optInt("end", 0);
Calendar cal_start = getRelativeCalendar(start, message.received); Calendar cal_start = getRelativeCalendar(all, start, message.received);
Calendar cal_end = getRelativeCalendar(end, message.received); Calendar cal_end = getRelativeCalendar(all, end, message.received);
if (cal_start.getTimeInMillis() > cal_end.getTimeInMillis()) if (cal_start.getTimeInMillis() > cal_end.getTimeInMillis())
cal_start.add(Calendar.HOUR_OF_DAY, -7 * 24); if (all)
cal_end.add(Calendar.DATE, 1);
else
cal_start.add(Calendar.HOUR_OF_DAY, -7 * 24);
if (message.received < cal_start.getTimeInMillis() || if (message.received < cal_start.getTimeInMillis() ||
message.received > cal_end.getTimeInMillis()) message.received > cal_end.getTimeInMillis())
@ -1141,7 +1145,7 @@ public class EntityRule {
JSONObject jschedule = jcondition.optJSONObject("schedule"); JSONObject jschedule = jcondition.optJSONObject("schedule");
int end = (jschedule == null ? 0 : jschedule.optInt("end", 0)); int end = (jschedule == null ? 0 : jschedule.optInt("end", 0));
Calendar cal = getRelativeCalendar(end, message.received); Calendar cal = getRelativeCalendar(false, end, message.received);
wakeup = cal.getTimeInMillis() + duration * 3600 * 1000L; wakeup = cal.getTimeInMillis() + duration * 3600 * 1000L;
} else } else
wakeup = message.received + duration * 3600 * 1000L; wakeup = message.received + duration * 3600 * 1000L;
@ -1240,19 +1244,24 @@ public class EntityRule {
return true; return true;
} }
private static Calendar getRelativeCalendar(int minutes, long reference) { private static Calendar getRelativeCalendar(boolean all, int minutes, long reference) {
int d = minutes / (24 * 60); int d = minutes / (24 * 60);
int h = minutes / 60 % 24; int h = minutes / 60 % 24;
int m = minutes % 60; int m = minutes % 60;
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
if (reference > cal.getTimeInMillis() - 7 * 24 * 3600 * 1000L)
if (all)
cal.setTimeInMillis(reference); cal.setTimeInMillis(reference);
long time = cal.getTimeInMillis(); else {
if (reference > cal.getTimeInMillis() - 7 * 24 * 3600 * 1000L)
cal.setTimeInMillis(reference);
long time = cal.getTimeInMillis();
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY + d); cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY + d);
if (cal.getTimeInMillis() < time) if (cal.getTimeInMillis() < time)
cal.add(Calendar.HOUR_OF_DAY, 7 * 24); cal.add(Calendar.HOUR_OF_DAY, 7 * 24);
}
cal.set(Calendar.HOUR_OF_DAY, h); cal.set(Calendar.HOUR_OF_DAY, h);
cal.set(Calendar.MINUTE, m); cal.set(Calendar.MINUTE, m);

@ -126,6 +126,7 @@ public class FragmentRule extends FragmentBase {
private Spinner spScheduleDayEnd; private Spinner spScheduleDayEnd;
private TextView tvScheduleHourStart; private TextView tvScheduleHourStart;
private TextView tvScheduleHourEnd; private TextView tvScheduleHourEnd;
private CheckBox cbEveryDay;
private Spinner spAction; private Spinner spAction;
private TextView tvActionRemark; private TextView tvActionRemark;
@ -307,6 +308,7 @@ public class FragmentRule extends FragmentBase {
spScheduleDayEnd = view.findViewById(R.id.spScheduleDayEnd); spScheduleDayEnd = view.findViewById(R.id.spScheduleDayEnd);
tvScheduleHourStart = view.findViewById(R.id.tvScheduleHourStart); tvScheduleHourStart = view.findViewById(R.id.tvScheduleHourStart);
tvScheduleHourEnd = view.findViewById(R.id.tvScheduleHourEnd); tvScheduleHourEnd = view.findViewById(R.id.tvScheduleHourEnd);
cbEveryDay = view.findViewById(R.id.cbEveryDay);
spAction = view.findViewById(R.id.spAction); spAction = view.findViewById(R.id.spAction);
tvActionRemark = view.findViewById(R.id.tvActionRemark); tvActionRemark = view.findViewById(R.id.tvActionRemark);
@ -471,6 +473,14 @@ public class FragmentRule extends FragmentBase {
spScheduleDayStart.setAdapter(adapterDay); spScheduleDayStart.setAdapter(adapterDay);
spScheduleDayEnd.setAdapter(adapterDay); spScheduleDayEnd.setAdapter(adapterDay);
cbEveryDay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
spScheduleDayStart.setEnabled(!isChecked);
spScheduleDayEnd.setEnabled(!isChecked);
}
});
adapterAction = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, new ArrayList<Action>()); adapterAction = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, new ArrayList<Action>());
adapterAction.setDropDownViewResource(R.layout.spinner_item1_dropdown); adapterAction.setDropDownViewResource(R.layout.spinner_item1_dropdown);
spAction.setAdapter(adapterAction); spAction.setAdapter(adapterAction);
@ -1198,6 +1208,8 @@ public class FragmentRule extends FragmentBase {
int start = (jschedule != null && jschedule.has("start") ? jschedule.getInt("start") : 0); int start = (jschedule != null && jschedule.has("start") ? jschedule.getInt("start") : 0);
int end = (jschedule != null && jschedule.has("end") ? jschedule.getInt("end") : 0); int end = (jschedule != null && jschedule.has("end") ? jschedule.getInt("end") : 0);
cbEveryDay.setChecked(jschedule != null && jschedule.optBoolean("all"));
spScheduleDayStart.setSelection(start / (24 * 60)); spScheduleDayStart.setSelection(start / (24 * 60));
spScheduleDayEnd.setSelection(end / (24 * 60)); spScheduleDayEnd.setSelection(end / (24 * 60));
@ -1564,6 +1576,7 @@ public class FragmentRule extends FragmentBase {
int dstart = spScheduleDayStart.getSelectedItemPosition(); int dstart = spScheduleDayStart.getSelectedItemPosition();
int dend = spScheduleDayEnd.getSelectedItemPosition(); int dend = spScheduleDayEnd.getSelectedItemPosition();
Object hstart = tvScheduleHourStart.getTag(); Object hstart = tvScheduleHourStart.getTag();
Object hend = tvScheduleHourEnd.getTag(); Object hend = tvScheduleHourEnd.getTag();
if (hstart == null) if (hstart == null)
@ -1571,13 +1584,15 @@ public class FragmentRule extends FragmentBase {
if (hend == null) if (hend == null)
hend = 0; hend = 0;
int start = dstart * 24 * 60 + (int) hstart; boolean all = cbEveryDay.isChecked();
int end = dend * 24 * 60 + (int) hend; int start = (all ? 0 : dstart) * 24 * 60 + (int) hstart;
int end = (all ? 0 : dend) * 24 * 60 + (int) hend;
if (start != end) { if (start != end) {
JSONObject jschedule = new JSONObject(); JSONObject jschedule = new JSONObject();
jschedule.put("start", start); jschedule.put("start", start);
jschedule.put("end", end); jschedule.put("end", end);
jschedule.put("all", all);
jcondition.put("schedule", jschedule); jcondition.put("schedule", jschedule);
} }

@ -675,6 +675,15 @@
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvScheduleHourStart" /> app:layout_constraintTop_toBottomOf="@id/tvScheduleHourStart" />
<CheckBox
android:id="@+id/cbEveryDay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:text="@string/title_rule_time_every_day"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvScheduleHourEnd" />
<TextView <TextView
android:id="@+id/tvAction" android:id="@+id/tvAction"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -685,7 +694,7 @@
android:textColor="?android:attr/textColorPrimary" android:textColor="?android:attr/textColorPrimary"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvScheduleHourEnd" /> app:layout_constraintTop_toBottomOf="@+id/cbEveryDay" />
<View <View
android:id="@+id/vSeparatorAction" android:id="@+id/vSeparatorAction"

@ -1831,6 +1831,7 @@
<string name="title_rule_time_after">Received after</string> <string name="title_rule_time_after">Received after</string>
<string name="title_rule_time_before">Received before</string> <string name="title_rule_time_before">Received before</string>
<string name="title_rule_time_rel">Relative time (received) between</string> <string name="title_rule_time_rel">Relative time (received) between</string>
<string name="title_rule_time_every_day">Every day</string>
<string name="title_rule_regex">Regex</string> <string name="title_rule_regex">Regex</string>
<string name="title_rule_and">AND</string> <string name="title_rule_and">AND</string>
<string name="title_rule_action">Action</string> <string name="title_rule_action">Action</string>

Loading…
Cancel
Save