From 658844345955e6574b0c7f5cb3b0d6a2f9cc5a76 Mon Sep 17 00:00:00 2001 From: jens Date: Sun, 7 Nov 2021 17:29:00 +0100 Subject: [PATCH 1/5] reoccuring time --- app/src/apkFlavor/AndroidManifest.xml | 2 +- app/src/fdroidFlavor/AndroidManifest.xml | 2 +- app/src/googlePlayFlavor/AndroidManifest.xml | 2 +- .../jens/automation2/ActivityMainRules.java | 4 +- .../ActivityManageTriggerTimeFrame.java | 68 ++++++++- .../jens/automation2/ReceiverCoordinator.java | 10 +- .../java/com/jens/automation2/TimeFrame.java | 143 ++++++++++-------- .../java/com/jens/automation2/Trigger.java | 6 +- ...armListener.java => DateTimeListener.java} | 20 +-- .../receivers/TimeZoneListener.java | 4 +- .../activity_manage_trigger_timeframe.xml | 28 ++++ app/src/main/res/values/strings.xml | 5 +- 12 files changed, 204 insertions(+), 90 deletions(-) rename app/src/main/java/com/jens/automation2/receivers/{AlarmListener.java => DateTimeListener.java} (94%) diff --git a/app/src/apkFlavor/AndroidManifest.xml b/app/src/apkFlavor/AndroidManifest.xml index acac67e7..73643316 100644 --- a/app/src/apkFlavor/AndroidManifest.xml +++ b/app/src/apkFlavor/AndroidManifest.xml @@ -132,7 +132,7 @@ android:scheme="package" />--> - + diff --git a/app/src/fdroidFlavor/AndroidManifest.xml b/app/src/fdroidFlavor/AndroidManifest.xml index b43e4ff8..3037baed 100644 --- a/app/src/fdroidFlavor/AndroidManifest.xml +++ b/app/src/fdroidFlavor/AndroidManifest.xml @@ -129,7 +129,7 @@ android:scheme="package" />--> - + diff --git a/app/src/googlePlayFlavor/AndroidManifest.xml b/app/src/googlePlayFlavor/AndroidManifest.xml index 71eee3e7..1c457831 100644 --- a/app/src/googlePlayFlavor/AndroidManifest.xml +++ b/app/src/googlePlayFlavor/AndroidManifest.xml @@ -123,7 +123,7 @@ android:scheme="package" />--> - + diff --git a/app/src/main/java/com/jens/automation2/ActivityMainRules.java b/app/src/main/java/com/jens/automation2/ActivityMainRules.java index fa037e65..afcacbc8 100644 --- a/app/src/main/java/com/jens/automation2/ActivityMainRules.java +++ b/app/src/main/java/com/jens/automation2/ActivityMainRules.java @@ -19,7 +19,7 @@ import android.widget.TextView; import android.widget.Toast; import com.jens.automation2.AutomationService.serviceCommands; -import com.jens.automation2.receivers.AlarmListener; +import com.jens.automation2.receivers.DateTimeListener; import java.util.ArrayList; @@ -254,7 +254,7 @@ public class ActivityMainRules extends ActivityGeneric try { if(AutomationService.isMyServiceRunning(this)) - AlarmListener.reloadAlarms(); + DateTimeListener.reloadAlarms(); } catch(NullPointerException e) { diff --git a/app/src/main/java/com/jens/automation2/ActivityManageTriggerTimeFrame.java b/app/src/main/java/com/jens/automation2/ActivityManageTriggerTimeFrame.java index c67edf7f..9bbf6066 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageTriggerTimeFrame.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageTriggerTimeFrame.java @@ -6,11 +6,16 @@ import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.EditText; import android.widget.RadioButton; import android.widget.TimePicker; import android.widget.Toast; +import org.apache.commons.lang3.StringUtils; + import java.sql.Time; +import java.text.ParseException; import java.util.ArrayList; import java.util.Calendar; @@ -18,8 +23,9 @@ public class ActivityManageTriggerTimeFrame extends Activity { Button bSaveTimeFrame; TimePicker startPicker, stopPicker; - CheckBox checkMonday, checkTuesday, checkWednesday, checkThursday, checkFriday, checkSaturday, checkSunday; + CheckBox checkMonday, checkTuesday, checkWednesday, checkThursday, checkFriday, checkSaturday, checkSunday, chkRepeat; RadioButton radioTimeFrameEntering, radioTimeFrameLeaving; + EditText etRepeatEvery; public static Trigger editedTimeFrameTrigger = null; @@ -44,7 +50,9 @@ public class ActivityManageTriggerTimeFrame extends Activity checkSunday = (CheckBox)findViewById(R.id.checkSunday); radioTimeFrameEntering = (RadioButton)findViewById(R.id.radioTimeFrameEntering); radioTimeFrameLeaving = (RadioButton)findViewById(R.id.radioTimeFrameLeaving); - + chkRepeat = (CheckBox)findViewById(R.id.chkRepeat); + etRepeatEvery = (EditText)findViewById(R.id.etRepeatEvery); + bSaveTimeFrame.setOnClickListener(new OnClickListener() { @Override @@ -92,11 +100,43 @@ public class ActivityManageTriggerTimeFrame extends Activity { Toast.makeText(getBaseContext(), getResources().getString(R.string.selectOneDay), Toast.LENGTH_LONG).show(); return; - } + } + + boolean goOn = false; + if(chkRepeat.isChecked()) + { + if(!StringUtils.isEmpty(etRepeatEvery.getText().toString())) + { + try + { + long value = Long.parseLong(etRepeatEvery.getText().toString()); + if(value > 0) + { + goOn = true; + } + } + catch(Exception e) + { + } + } + } + else + goOn = true; + + if(!goOn) + { + Toast.makeText(getBaseContext(), getResources().getString(R.string.enterRepetitionTime), Toast.LENGTH_LONG).show(); + return; + } if(editedTimeFrameTrigger.getTimeFrame() == null) + { // add new one - editedTimeFrameTrigger.setTimeFrame(new TimeFrame(startTime, stopTime, dayList)); + if(chkRepeat.isChecked()) + editedTimeFrameTrigger.setTimeFrame(new TimeFrame(startTime, stopTime, dayList, Long.parseLong(etRepeatEvery.getText().toString()))); + else + editedTimeFrameTrigger.setTimeFrame(new TimeFrame(startTime, stopTime, dayList, 0)); + } else { // edit one @@ -104,6 +144,11 @@ public class ActivityManageTriggerTimeFrame extends Activity editedTimeFrameTrigger.getTimeFrame().setTriggerTimeStop(stopTime); editedTimeFrameTrigger.getTimeFrame().getDayList().clear(); editedTimeFrameTrigger.getTimeFrame().setDayList(dayList); + + if(chkRepeat.isChecked()) + editedTimeFrameTrigger.getTimeFrame().setRepetition(Long.parseLong(etRepeatEvery.getText().toString())); + else + editedTimeFrameTrigger.getTimeFrame().setRepetition(0); } editedTimeFrameTrigger.setTriggerParameter(radioTimeFrameEntering.isChecked()); @@ -112,6 +157,15 @@ public class ActivityManageTriggerTimeFrame extends Activity finish(); } }); + + chkRepeat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() + { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) + { + etRepeatEvery.setEnabled(isChecked); + } + }); if(editedTimeFrameTrigger.getTimeFrame() != null) loadVariableIntoGui(); @@ -158,6 +212,12 @@ public class ActivityManageTriggerTimeFrame extends Activity break; } } + + if(editedTimeFrameTrigger.getTimeFrame().getRepetition() > 0) + { + chkRepeat.setChecked(true); + etRepeatEvery.setText(String.valueOf(editedTimeFrameTrigger.getTimeFrame().getRepetition())); + } } } diff --git a/app/src/main/java/com/jens/automation2/ReceiverCoordinator.java b/app/src/main/java/com/jens/automation2/ReceiverCoordinator.java index 9921ba13..c36aefe9 100644 --- a/app/src/main/java/com/jens/automation2/ReceiverCoordinator.java +++ b/app/src/main/java/com/jens/automation2/ReceiverCoordinator.java @@ -5,7 +5,7 @@ import android.util.Log; import com.jens.automation2.location.CellLocationChangedReceiver; import com.jens.automation2.location.WifiBroadcastReceiver; -import com.jens.automation2.receivers.AlarmListener; +import com.jens.automation2.receivers.DateTimeListener; import com.jens.automation2.receivers.AutomationListenerInterface; import com.jens.automation2.receivers.BatteryReceiver; import com.jens.automation2.receivers.BluetoothReceiver; @@ -42,7 +42,7 @@ public class ReceiverCoordinator Class adClass = Class.forName("ActivityDetectionReceiver"); allImplementers = new Class[] { adClass, - AlarmListener.class, + DateTimeListener.class, BatteryReceiver.class, BluetoothReceiver.class, ConnectivityReceiver.class, @@ -59,7 +59,7 @@ public class ReceiverCoordinator // e.printStackTrace(); allImplementers = new Class[] { - AlarmListener.class, + DateTimeListener.class, BatteryReceiver.class, BluetoothReceiver.class, ConnectivityReceiver.class, @@ -155,7 +155,7 @@ public class ReceiverCoordinator BatteryReceiver.startBatteryReceiver(AutomationService.getInstance()); // startAlarmListener - AlarmListener.startAlarmListener(AutomationService.getInstance()); + DateTimeListener.startAlarmListener(AutomationService.getInstance()); TimeZoneListener.startTimeZoneListener(AutomationService.getInstance()); // startNoiseListener @@ -199,7 +199,7 @@ public class ReceiverCoordinator WifiBroadcastReceiver.stopWifiReceiver(); BatteryReceiver.stopBatteryReceiver(); TimeZoneListener.stopTimeZoneListener(); - AlarmListener.stopAlarmListener(AutomationService.getInstance()); + DateTimeListener.stopAlarmListener(AutomationService.getInstance()); NoiseListener.stopNoiseListener(); ProcessListener.stopProcessListener(AutomationService.getInstance()); diff --git a/app/src/main/java/com/jens/automation2/TimeFrame.java b/app/src/main/java/com/jens/automation2/TimeFrame.java index 757d85b8..bde282df 100644 --- a/app/src/main/java/com/jens/automation2/TimeFrame.java +++ b/app/src/main/java/com/jens/automation2/TimeFrame.java @@ -6,70 +6,89 @@ import java.util.ArrayList; public class TimeFrame { // Defines a timeframe - private Time triggerTimeStart; - private Time triggerTimeStop; + protected Time triggerTimeStart; + protected Time triggerTimeStop; + protected long repetition; - private ArrayList dayList = new ArrayList(); - public ArrayList getDayList() + protected final static String separator = "/"; + + private ArrayList dayList = new ArrayList(); + public ArrayList getDayList() + { + return dayList; + } + public void setDayList(ArrayList dayList) + { + this.dayList = dayList; + } + public void setDayListFromString(String dayListString) + { +// Log.i("Parsing", "Full string: " + dayListString); + char[] dayListCharArray = dayListString.toCharArray(); + + dayList = new ArrayList(); + for(char item : dayListCharArray) { - return dayList; - } - public void setDayList(ArrayList dayList) - { - this.dayList = dayList; - } - public void setDayListFromString(String dayListString) - { -// Log.i("Parsing", "Full string: " + dayListString); - char[] dayListCharArray = dayListString.toCharArray(); - - dayList = new ArrayList(); - for(char item : dayListCharArray) - { // Log.i("Parsing", String.valueOf(item)); - dayList.add(Integer.parseInt(String.valueOf(item))); - } + dayList.add(Integer.parseInt(String.valueOf(item))); } + } - - public Time getTriggerTimeStart() - { - return triggerTimeStart; - } - public void setTriggerTimeStart(Time triggerTimeStart) - { - this.triggerTimeStart = triggerTimeStart; - } - public Time getTriggerTimeStop() - { - return triggerTimeStop; - } - public void setTriggerTimeStop(Time triggerTimeStop) - { - this.triggerTimeStop = triggerTimeStop; - } - - public TimeFrame (Time timeStart, Time timeEnd, ArrayList dayList2) - { - this.setTriggerTimeStart(timeStart); - this.setTriggerTimeStop(timeEnd); - this.setDayList(dayList2); - } - TimeFrame (String fileContent) - { - String[] dateArray = fileContent.split("/"); // example: timestart/timestop/days[int] - this.setTriggerTimeStart(Time.valueOf(dateArray[0])); - this.setTriggerTimeStop(Time.valueOf(dateArray[1])); - this.setDayListFromString(dateArray[2]); - } - @Override - public String toString() - { - String returnString = this.getTriggerTimeStart().toString() + "/" + this.getTriggerTimeStop().toString() + "/"; - - for(Integer oneDay : this.getDayList()) - returnString += String.valueOf(oneDay); - - return returnString; - } -} + public Time getTriggerTimeStart() + { + return triggerTimeStart; + } + public void setTriggerTimeStart(Time triggerTimeStart) + { + this.triggerTimeStart = triggerTimeStart; + } + + public Time getTriggerTimeStop() + { + return triggerTimeStop; + } + public void setTriggerTimeStop(Time triggerTimeStop) + { + this.triggerTimeStop = triggerTimeStop; + } + + public long getRepetition() + { + return repetition; + } + + public void setRepetition(long repetition) + { + this.repetition = repetition; + } + + public TimeFrame (Time timeStart, Time timeEnd, ArrayList dayList2, long repetition) + { + this.setTriggerTimeStart(timeStart); + this.setTriggerTimeStop(timeEnd); + this.setDayList(dayList2); + this.setRepetition(repetition); + } + + TimeFrame (String fileContent) + { + String[] dateArray = fileContent.split(separator); // example: timestart/timestop/days[int]/repetition + this.setTriggerTimeStart(Time.valueOf(dateArray[0])); + this.setTriggerTimeStop(Time.valueOf(dateArray[1])); + this.setDayListFromString(dateArray[2]); + this.setRepetition(Long.parseLong(dateArray[3])); + } + + @Override + public String toString() + { + String returnString = this.getTriggerTimeStart().toString() + separator + this.getTriggerTimeStop().toString() + separator; + + for(Integer oneDay : this.getDayList()) + returnString += String.valueOf(oneDay); + + returnString += separator + String.valueOf(repetition); + + return returnString; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/jens/automation2/Trigger.java b/app/src/main/java/com/jens/automation2/Trigger.java index 04e41983..7f02e2a2 100644 --- a/app/src/main/java/com/jens/automation2/Trigger.java +++ b/app/src/main/java/com/jens/automation2/Trigger.java @@ -294,7 +294,11 @@ public class Trigger else returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.leaving) + " "); - returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.triggerTimeFrame) + ": " + this.getTimeFrame().getTriggerTimeStart().toString() + " " + Miscellaneous.getAnyContext().getResources().getString(R.string.until) + " " + this.getTimeFrame().getTriggerTimeStop().toString() + " on days " + this.getTimeFrame().getDayList().toString()); + String repeat = ", no repetition"; + if(this.getTimeFrame().getRepetition() > 0) + repeat = ", " + String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.repeatEveryXsecondsWithVariable), this.getTimeFrame().getRepetition()); + + returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.triggerTimeFrame) + ": " + this.getTimeFrame().getTriggerTimeStart().toString() + " " + Miscellaneous.getAnyContext().getResources().getString(R.string.until) + " " + this.getTimeFrame().getTriggerTimeStop().toString() + " on days " + this.getTimeFrame().getDayList().toString() + repeat); break; case speed: if(getTriggerParameter()) diff --git a/app/src/main/java/com/jens/automation2/receivers/AlarmListener.java b/app/src/main/java/com/jens/automation2/receivers/DateTimeListener.java similarity index 94% rename from app/src/main/java/com/jens/automation2/receivers/AlarmListener.java rename to app/src/main/java/com/jens/automation2/receivers/DateTimeListener.java index 4c7bf102..86813e4b 100644 --- a/app/src/main/java/com/jens/automation2/receivers/AlarmListener.java +++ b/app/src/main/java/com/jens/automation2/receivers/DateTimeListener.java @@ -18,7 +18,7 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.Date; -public class AlarmListener extends BroadcastReceiver implements AutomationListenerInterface +public class DateTimeListener extends BroadcastReceiver implements AutomationListenerInterface { private static AutomationService automationServiceRef; private static AlarmManager centralAlarmManagerInstance; @@ -31,11 +31,11 @@ public class AlarmListener extends BroadcastReceiver implements AutomationListen public static void startAlarmListener(final AutomationService automationServiceRef) { - AlarmListener.startAlarmListenerInternal(automationServiceRef); + DateTimeListener.startAlarmListenerInternal(automationServiceRef); } public static void stopAlarmListener(Context context) { - AlarmListener.stopAlarmListenerInternal(); + DateTimeListener.stopAlarmListenerInternal(); } public static boolean isAlarmListenerActive() @@ -207,7 +207,7 @@ public class AlarmListener extends BroadcastReceiver implements AutomationListen } } - Intent alarmIntent = new Intent(automationServiceRef, AlarmListener.class); + Intent alarmIntent = new Intent(automationServiceRef, DateTimeListener.class); PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(automationServiceRef, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); centralAlarmManagerInstance.set(AlarmManager.RTC_WAKEUP, scheduleCandidate, alarmPendingIntent); @@ -224,7 +224,7 @@ public class AlarmListener extends BroadcastReceiver implements AutomationListen Miscellaneous.logEvent("i", "AlarmManager", "Clearing possibly standing alarms.", 4); for(int requestCode : requestCodeList) { - Intent alarmIntent = new Intent(automationServiceRef, AlarmListener.class); + Intent alarmIntent = new Intent(automationServiceRef, DateTimeListener.class); PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(automationServiceRef, requestCode, alarmIntent, 0); // Miscellaneous.logEvent("i", "AlarmManager", "Clearing alarm with request code: " + String.valueOf(requestCode)); centralAlarmManagerInstance.cancel(alarmPendingIntent); @@ -237,13 +237,13 @@ public class AlarmListener extends BroadcastReceiver implements AutomationListen if(!alarmListenerActive) { Miscellaneous.logEvent("i", "AlarmListener", "Starting alarm listener.", 4); - AlarmListener.automationServiceRef = givenAutomationServiceRef; + DateTimeListener.automationServiceRef = givenAutomationServiceRef; centralAlarmManagerInstance = (AlarmManager)automationServiceRef.getSystemService(automationServiceRef.ALARM_SERVICE); // alarmIntent = new Intent(automationServiceRef, AlarmListener.class); // alarmPendingIntent = PendingIntent.getBroadcast(automationServiceRef, 0, alarmIntent, 0); alarmListenerActive = true; Miscellaneous.logEvent("i", "AlarmListener", "Alarm listener started.", 4); - AlarmListener.setAlarms(); + DateTimeListener.setAlarms(); // // get a Calendar object with current time // Calendar cal = Calendar.getInstance(); @@ -269,17 +269,17 @@ public class AlarmListener extends BroadcastReceiver implements AutomationListen } public static void reloadAlarms() { - AlarmListener.setAlarms(); + DateTimeListener.setAlarms(); } @Override public void startListener(AutomationService automationService) { - AlarmListener.startAlarmListener(automationService); + DateTimeListener.startAlarmListener(automationService); } @Override public void stopListener(AutomationService automationService) { - AlarmListener.stopAlarmListener(automationService); + DateTimeListener.stopAlarmListener(automationService); } public static boolean haveAllPermission() diff --git a/app/src/main/java/com/jens/automation2/receivers/TimeZoneListener.java b/app/src/main/java/com/jens/automation2/receivers/TimeZoneListener.java index c2856f2f..5961a98c 100644 --- a/app/src/main/java/com/jens/automation2/receivers/TimeZoneListener.java +++ b/app/src/main/java/com/jens/automation2/receivers/TimeZoneListener.java @@ -77,12 +77,12 @@ public class TimeZoneListener extends BroadcastReceiver implements AutomationLis if(action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { Miscellaneous.logEvent("i", "TimeZoneListener", "Device timezone changed. Reloading alarms.", 3); - AlarmListener.reloadAlarms(); + DateTimeListener.reloadAlarms(); } else if(action.equals(Intent.ACTION_TIME_CHANGED)) { Miscellaneous.logEvent("i", "TimeZoneListener", "Device time changed. Reloading alarms.", 4); - AlarmListener.reloadAlarms(); + DateTimeListener.reloadAlarms(); } } @Override diff --git a/app/src/main/res/layout/activity_manage_trigger_timeframe.xml b/app/src/main/res/layout/activity_manage_trigger_timeframe.xml index a85659ac..0553e16f 100644 --- a/app/src/main/res/layout/activity_manage_trigger_timeframe.xml +++ b/app/src/main/res/layout/activity_manage_trigger_timeframe.xml @@ -119,6 +119,34 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sunday" /> + + + + + + + + + +