Automation/app/src/main/java/com/jens/automation2/receivers/TimeZoneListener.java

114 lines
3.2 KiB
Java
Raw Normal View History

2021-02-16 13:42:49 +01:00
package com.jens.automation2.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import com.jens.automation2.AutomationService;
import com.jens.automation2.Miscellaneous;
import com.jens.automation2.Rule;
import com.jens.automation2.Trigger.Trigger_Enum;
public class TimeZoneListener extends BroadcastReceiver implements AutomationListenerInterface
{
private static TimeZoneListener timeZoneListenerInstance = null;
2022-05-26 18:26:49 +02:00
protected static boolean timezoneListenerActive = false;
2021-02-16 13:42:49 +01:00
protected static AutomationService automationServiceRef = null;
2022-05-26 18:26:49 +02:00
protected static IntentFilter timezoneListenerIntentFilter = null;
2021-02-16 13:42:49 +01:00
2022-05-26 18:26:49 +02:00
public static boolean isTimezoneListenerActive()
2021-02-16 13:42:49 +01:00
{
2022-05-26 18:26:49 +02:00
return timezoneListenerActive;
2021-02-16 13:42:49 +01:00
}
public static void startTimeZoneListener(AutomationService automationService)
{
if(timeZoneListenerInstance == null)
timeZoneListenerInstance = new TimeZoneListener();
automationServiceRef = automationService;
try
{
2022-05-26 18:26:49 +02:00
if(!timezoneListenerActive && Rule.isAnyRuleUsing(Trigger_Enum.timeFrame))
2021-02-16 13:42:49 +01:00
{
Miscellaneous.logEvent("i", "TimeZoneListener", "Starting TimeZoneListener", 4);
2022-05-26 18:26:49 +02:00
timezoneListenerActive = true;
2021-02-16 13:42:49 +01:00
2022-05-26 18:26:49 +02:00
if(timezoneListenerIntentFilter == null)
2021-02-16 13:42:49 +01:00
{
2022-05-26 18:26:49 +02:00
timezoneListenerIntentFilter = new IntentFilter();
timezoneListenerIntentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
timezoneListenerIntentFilter.addAction(Intent.ACTION_TIME_CHANGED);
2021-02-16 13:42:49 +01:00
}
2022-05-26 18:26:49 +02:00
automationService.registerReceiver(timeZoneListenerInstance, timezoneListenerIntentFilter);
2021-02-16 13:42:49 +01:00
}
}
catch(Exception ex)
{
Miscellaneous.logEvent("e", "TimeZoneListener", "Error starting TimeZoneListener: " + Log.getStackTraceString(ex), 3);
}
}
public static void stopTimeZoneListener()
{
try
{
2022-05-26 18:26:49 +02:00
if(timezoneListenerActive)
2021-02-16 13:42:49 +01:00
{
Miscellaneous.logEvent("i", "TimeZoneListener", "Stopping TimeZoneListener", 4);
automationServiceRef.unregisterReceiver(timeZoneListenerInstance);
2022-05-26 18:26:49 +02:00
timezoneListenerActive = false;
2021-02-16 13:42:49 +01:00
}
}
catch(Exception ex)
{
Miscellaneous.logEvent("e", "TimeZoneListener", "Error stopping TimeZoneListener: " + Log.getStackTraceString(ex), 3);
}
}
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if(action.equals(Intent.ACTION_TIMEZONE_CHANGED))
{
Miscellaneous.logEvent("i", "TimeZoneListener", "Device timezone changed. Reloading alarms.", 3);
2021-11-07 17:29:00 +01:00
DateTimeListener.reloadAlarms();
2021-02-16 13:42:49 +01:00
}
else if(action.equals(Intent.ACTION_TIME_CHANGED))
{
2022-05-26 18:26:49 +02:00
Miscellaneous.logEvent("i", "TimeZoneListener", "Device time changed. Reloading alarms.", 3);
2021-11-07 17:29:00 +01:00
DateTimeListener.reloadAlarms();
2021-02-16 13:42:49 +01:00
}
}
@Override
public void startListener(AutomationService automationService)
{
TimeZoneListener.startTimeZoneListener(automationService);
}
@Override
public void stopListener(AutomationService automationService)
{
TimeZoneListener.stopTimeZoneListener();
}
public static boolean haveAllPermission()
{
return true;
}
@Override
public boolean isListenerRunning()
{
2022-05-26 18:26:49 +02:00
return TimeZoneListener.isTimezoneListenerActive();
2021-02-16 13:42:49 +01:00
}
@Override
public Trigger_Enum[] getMonitoredTrigger()
{
return null;
}
}