Added day from/to to rule time condition

pull/161/head
M66B 5 years ago
parent d8e5bf532f
commit 9e5280317f

@ -177,21 +177,19 @@ public class EntityRule {
// Schedule // Schedule
JSONObject jschedule = jcondition.optJSONObject("schedule"); JSONObject jschedule = jcondition.optJSONObject("schedule");
if (jschedule != null) { if (jschedule != null) {
int day = jschedule.optInt("day", -1) + 1;
int start = jschedule.optInt("start", 0); int start = jschedule.optInt("start", 0);
int end = jschedule.optInt("end", 0); int end = jschedule.optInt("end", 0);
if (end <= start) if (end <= start)
end += 24 * 60; end += 7 * 24 * 60;
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
cal.setTime(new Date(message.received)); cal.setTime(new Date(message.received));
int mday = cal.get(Calendar.DAY_OF_WEEK); int mday = cal.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY;
int mhour = cal.get(Calendar.HOUR_OF_DAY); int mhour = cal.get(Calendar.HOUR_OF_DAY);
int mminute = cal.get(Calendar.MINUTE); int mminute = cal.get(Calendar.MINUTE);
int minutes = mhour * 60 + mminute; int minutes = mday * 24 * 60 + mhour * 60 + mminute;
if (day > 0 && mday != day)
return false;
if (minutes < start || minutes > end) if (minutes < start || minutes > end)
return false; return false;
} }
@ -359,14 +357,13 @@ public class EntityRule {
if (jschedule == null) if (jschedule == null)
throw new IllegalArgumentException("Rule snooze schedule not found"); throw new IllegalArgumentException("Rule snooze schedule not found");
int start = jschedule.optInt("start", 0);
int end = jschedule.optInt("end", 0); int end = jschedule.optInt("end", 0);
if (end <= start) int day = end / (24 * 60);
end += 24 * 60; int hour = end / 60 % 24;
int hour = end / 60;
int minute = end % 60; int minute = end % 60;
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY + day);
cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute); cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0); cal.set(Calendar.SECOND, 0);

@ -99,9 +99,10 @@ public class FragmentRule extends FragmentBase {
private EditText etHeader; private EditText etHeader;
private CheckBox cbHeader; private CheckBox cbHeader;
private Spinner spScheduleDay; private Spinner spScheduleDayStart;
private TextView tvScheduleStart; private Spinner spScheduleDayEnd;
private TextView tvScheduleEnd; private TextView tvScheduleHourStart;
private TextView tvScheduleHourEnd;
private Spinner spAction; private Spinner spAction;
private TextView tvActionRemark; private TextView tvActionRemark;
@ -201,9 +202,10 @@ public class FragmentRule extends FragmentBase {
etHeader = view.findViewById(R.id.etHeader); etHeader = view.findViewById(R.id.etHeader);
cbHeader = view.findViewById(R.id.cbHeader); cbHeader = view.findViewById(R.id.cbHeader);
spScheduleDay = view.findViewById(R.id.spScheduleDay); spScheduleDayStart = view.findViewById(R.id.spScheduleDayStart);
tvScheduleStart = view.findViewById(R.id.tvScheduleStart); spScheduleDayEnd = view.findViewById(R.id.spScheduleDayEnd);
tvScheduleEnd = view.findViewById(R.id.tvScheduleEnd); tvScheduleHourStart = view.findViewById(R.id.tvScheduleHourStart);
tvScheduleHourEnd = view.findViewById(R.id.tvScheduleHourEnd);
spAction = view.findViewById(R.id.spAction); spAction = view.findViewById(R.id.spAction);
tvActionRemark = view.findViewById(R.id.tvActionRemark); tvActionRemark = view.findViewById(R.id.tvActionRemark);
@ -262,7 +264,8 @@ public class FragmentRule extends FragmentBase {
adapterDay = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, new ArrayList<String>()); adapterDay = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, new ArrayList<String>());
adapterDay.setDropDownViewResource(R.layout.spinner_item1_dropdown); adapterDay.setDropDownViewResource(R.layout.spinner_item1_dropdown);
spScheduleDay.setAdapter(adapterDay); spScheduleDayStart.setAdapter(adapterDay);
spScheduleDayEnd.setAdapter(adapterDay);
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);
@ -280,12 +283,11 @@ public class FragmentRule extends FragmentBase {
adapterAnswer.setDropDownViewResource(R.layout.spinner_item1_dropdown); adapterAnswer.setDropDownViewResource(R.layout.spinner_item1_dropdown);
spAnswer.setAdapter(adapterAnswer); spAnswer.setAdapter(adapterAnswer);
adapterDay.add(getString(R.string.title_any));
String[] dayNames = DateFormatSymbols.getInstance().getWeekdays(); String[] dayNames = DateFormatSymbols.getInstance().getWeekdays();
for (int day = Calendar.SUNDAY; day <= Calendar.SATURDAY; day++) for (int day = Calendar.SUNDAY; day <= Calendar.SATURDAY; day++)
adapterDay.add(dayNames[day]); adapterDay.add(dayNames[day]);
tvScheduleStart.setOnClickListener(new View.OnClickListener() { tvScheduleHourStart.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
Object time = v.getTag(); Object time = v.getTag();
@ -298,7 +300,7 @@ public class FragmentRule extends FragmentBase {
} }
}); });
tvScheduleEnd.setOnClickListener(new View.OnClickListener() { tvScheduleHourEnd.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
Object time = v.getTag(); Object time = v.getTag();
@ -580,15 +582,15 @@ public class FragmentRule extends FragmentBase {
private void onScheduleStart(Intent data) { private void onScheduleStart(Intent data) {
int minutes = data.getIntExtra("minutes", 0); int minutes = data.getIntExtra("minutes", 0);
tvScheduleStart.setTag(minutes); tvScheduleHourStart.setTag(minutes);
tvScheduleStart.setText(formatHour(getContext(), minutes)); tvScheduleHourStart.setText(formatHour(getContext(), minutes));
cbScheduleEnd.setChecked(true); cbScheduleEnd.setChecked(true);
} }
private void onScheduleEnd(Intent data) { private void onScheduleEnd(Intent data) {
int minutes = data.getIntExtra("minutes", 0); int minutes = data.getIntExtra("minutes", 0);
tvScheduleEnd.setTag(minutes); tvScheduleHourEnd.setTag(minutes);
tvScheduleEnd.setText(formatHour(getContext(), minutes)); tvScheduleHourEnd.setText(formatHour(getContext(), minutes));
cbScheduleEnd.setChecked(true); cbScheduleEnd.setChecked(true);
} }
@ -635,14 +637,17 @@ public class FragmentRule extends FragmentBase {
etHeader.setText(jheader == null ? null : jheader.getString("value")); etHeader.setText(jheader == null ? null : jheader.getString("value"));
cbHeader.setChecked(jheader != null && jheader.getBoolean("regex")); cbHeader.setChecked(jheader != null && jheader.getBoolean("regex"));
if (jschedule != null && jschedule.has("day"))
spScheduleDay.setSelection(jschedule.getInt("day") + 1);
int start = (jschedule != null && jschedule.has("start") ? jschedule.getInt("start") : 0); int start = (jschedule != null && jschedule.has("start") ? jschedule.getInt("start") : 0);
tvScheduleStart.setTag(start);
tvScheduleStart.setText(formatHour(getContext(), start));
int end = (jschedule != null && jschedule.has("end") ? jschedule.getInt("end") : 0); int end = (jschedule != null && jschedule.has("end") ? jschedule.getInt("end") : 0);
tvScheduleEnd.setTag(end);
tvScheduleEnd.setText(formatHour(getContext(), end)); spScheduleDayStart.setSelection(start / (24 * 60));
spScheduleDayEnd.setSelection(end / (24 * 60));
tvScheduleHourStart.setTag(start % (24 * 60));
tvScheduleHourStart.setText(formatHour(getContext(), start % (24 * 60)));
tvScheduleHourEnd.setTag(end % (24 * 60));
tvScheduleHourEnd.setText(formatHour(getContext(), end % (24 * 60)));
if (rule == null) { if (rule == null) {
for (int pos = 0; pos < adapterIdentity.getCount(); pos++) for (int pos = 0; pos < adapterIdentity.getCount(); pos++)
@ -899,18 +904,22 @@ public class FragmentRule extends FragmentBase {
jcondition.put("header", jheader); jcondition.put("header", jheader);
} }
int day = spScheduleDay.getSelectedItemPosition(); int dstart = spScheduleDayStart.getSelectedItemPosition();
Object start = tvScheduleStart.getTag(); int dend = spScheduleDayEnd.getSelectedItemPosition();
Object end = tvScheduleEnd.getTag(); Object hstart = tvScheduleHourStart.getTag();
if (start == null) Object hend = tvScheduleHourEnd.getTag();
start = 0; if (hstart == null)
if (end == null) hstart = 0;
end = 0; if (hend == null)
if (!(day == 0 && start.equals(end))) { hend = 0;
int start = dstart * 24 * 60 + (int) hstart;
int end = dend * 24 * 60 + (int) hend;
if (start != end) {
JSONObject jschedule = new JSONObject(); JSONObject jschedule = new JSONObject();
jschedule.put("day", spScheduleDay.getSelectedItemPosition() - 1); jschedule.put("start", start);
jschedule.put("start", (int) start); jschedule.put("end", end);
jschedule.put("end", (int) end);
jcondition.put("schedule", jschedule); jcondition.put("schedule", jschedule);
} }

@ -360,45 +360,44 @@
app:layout_constraintTop_toBottomOf="@id/vSeparatorSchedule" /> app:layout_constraintTop_toBottomOf="@id/vSeparatorSchedule" />
<Spinner <Spinner
android:id="@+id/spScheduleDay" android:id="@+id/spScheduleDayStart"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="@+id/tvScheduleHourStart"
app:layout_constraintEnd_toStartOf="@+id/tvScheduleHourStart"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvSchedule" /> app:layout_constraintTop_toTopOf="@+id/tvScheduleHourStart" />
<TextView <Spinner
android:id="@+id/tvScheduleStart" android:id="@+id/spScheduleDayEnd"
android:layout_width="wrap_content" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="12dp" app:layout_constraintBottom_toBottomOf="@+id/tvScheduleHourEnd"
android:text="00:00" app:layout_constraintEnd_toStartOf="@+id/tvScheduleHourEnd"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="?android:attr/textColorPrimary"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/spScheduleDay" /> app:layout_constraintTop_toTopOf="@+id/tvScheduleHourEnd" />
<TextView <TextView
android:id="@+id/tvScheduleSeparator" android:id="@+id/tvScheduleHourStart"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="—" android:padding="12dp"
android:text="00:00"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="?android:attr/textColorPrimary" android:textColor="?android:attr/textColorPrimary"
app:layout_constraintBottom_toBottomOf="@+id/tvScheduleStart" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/tvScheduleStart" app:layout_constraintTop_toBottomOf="@id/tvSchedule" />
app:layout_constraintTop_toTopOf="@id/tvScheduleStart" />
<TextView <TextView
android:id="@+id/tvScheduleEnd" android:id="@+id/tvScheduleHourEnd"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="12dp" android:padding="12dp"
android:text="00:00" android:text="00:00"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="?android:attr/textColorPrimary" android:textColor="?android:attr/textColorPrimary"
app:layout_constraintStart_toEndOf="@id/tvScheduleSeparator" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/tvScheduleStart" /> app:layout_constraintTop_toBottomOf="@id/tvScheduleHourStart" />
<View <View
android:id="@+id/vSeparatorAction" android:id="@+id/vSeparatorAction"
@ -407,7 +406,7 @@
android:layout_marginTop="24dp" android:layout_marginTop="24dp"
android:background="?attr/colorSeparator" android:background="?attr/colorSeparator"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvScheduleStart" /> app:layout_constraintTop_toBottomOf="@id/tvScheduleHourEnd" />
<TextView <TextView
android:id="@+id/tvAction" android:id="@+id/tvAction"

@ -782,7 +782,6 @@
<string name="title_report">Report</string> <string name="title_report">Report</string>
<string name="title_fix">Fix</string> <string name="title_fix">Fix</string>
<string name="title_enable">Enable</string> <string name="title_enable">Enable</string>
<string name="title_any">Any</string>
<string name="title_executing">Executing</string> <string name="title_executing">Executing</string>
<string name="title_completed">Completed</string> <string name="title_completed">Completed</string>
<string name="title_ask_what">Ask what to do</string> <string name="title_ask_what">Ask what to do</string>

Loading…
Cancel
Save