diff options
author | Wolfgang Wiedmeyer <wolfgit@wiedmeyer.de> | 2017-03-11 22:15:03 +0100 |
---|---|---|
committer | Wolfgang Wiedmeyer <wolfgit@wiedmeyer.de> | 2017-03-11 22:15:03 +0100 |
commit | 85901329b0794b136b96bf745f4ab1572806fc89 (patch) | |
tree | f23da7e97cae727f39d825f0fef8348cffb238e4 /src/net/java/sip/communicator/plugin/generalconfig/autoaway/AutoAwayWatcher.java | |
parent | 3db2e44f186c59429901b2c899e139ea60117a55 (diff) | |
parent | cf5da997da8820b4050f5b87ee9440a0ede36d1f (diff) | |
download | jitsi-85901329b0794b136b96bf745f4ab1572806fc89.zip jitsi-85901329b0794b136b96bf745f4ab1572806fc89.tar.gz jitsi-85901329b0794b136b96bf745f4ab1572806fc89.tar.bz2 |
Signed-off-by: Wolfgang Wiedmeyer <wolfgit@wiedmeyer.de>
Diffstat (limited to 'src/net/java/sip/communicator/plugin/generalconfig/autoaway/AutoAwayWatcher.java')
-rw-r--r-- | src/net/java/sip/communicator/plugin/generalconfig/autoaway/AutoAwayWatcher.java | 920 |
1 files changed, 460 insertions, 460 deletions
diff --git a/src/net/java/sip/communicator/plugin/generalconfig/autoaway/AutoAwayWatcher.java b/src/net/java/sip/communicator/plugin/generalconfig/autoaway/AutoAwayWatcher.java index 38a51c6..37bc48f 100644 --- a/src/net/java/sip/communicator/plugin/generalconfig/autoaway/AutoAwayWatcher.java +++ b/src/net/java/sip/communicator/plugin/generalconfig/autoaway/AutoAwayWatcher.java @@ -1,4 +1,4 @@ -/*
+/* * Jitsi, the OpenSource Java VoIP and Instant Messaging client. * * Copyright @ 2015 Atlassian Pty Ltd @@ -15,462 +15,462 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package net.java.sip.communicator.plugin.generalconfig.autoaway;
-
-import java.beans.*;
-import java.util.*;
-
-import net.java.sip.communicator.plugin.generalconfig.*;
-import net.java.sip.communicator.service.protocol.*;
-import net.java.sip.communicator.service.protocol.event.*;
-import net.java.sip.communicator.service.sysactivity.*;
-import net.java.sip.communicator.service.sysactivity.event.*;
-import net.java.sip.communicator.util.*;
-
-import org.osgi.framework.*;
-
-/**
- * Listens for idle events from SystemActivityNotifications Service.
- *
- * @author Damian Minkov
- */
-public class AutoAwayWatcher
- implements ServiceListener,
- RegistrationStateChangeListener
-{
- /**
- * The logger.
- */
- private static final Logger logger
- = Logger.getLogger(AutoAwayWatcher.class);
-
- /**
- * The states of providers before going to away.
- */
- private final Map<ProtocolProviderService, PresenceStatus> lastStates
- = new HashMap<ProtocolProviderService, PresenceStatus>();
-
- /**
- * Listens for idle events.
- */
- private IdleListener idleListener = null;
-
- /**
- * Creates AutoAway handler.
- */
- public AutoAwayWatcher()
- {
- if (Preferences.isEnabled())
- {
- start();
- }
-
- Preferences.addEnableChangeListener(
- new PropertyChangeListener()
- {
- public void propertyChange(PropertyChangeEvent evt)
- {
- if(Boolean.parseBoolean((String) evt.getNewValue()))
- start();
- else
- stopInner();
- }
- }
- );
-
- // listens for changes in configured value.
- Preferences.addTimerChangeListener(
- new PropertyChangeListener()
- {
- public void propertyChange(PropertyChangeEvent evt)
- {
- stopInner();
- start();
- }
- }
- );
- }
-
- /**
- * Starts and add needed listeners.
- */
- private void start()
- {
- if(idleListener == null)
- {
-
- idleListener = new IdleListener();
-
- SystemActivityNotificationsService
- systemActivityNotificationsService
- = getSystemActivityNotificationsService();
-
- systemActivityNotificationsService.addIdleSystemChangeListener(
- Preferences.getTimer() * 60 * 1000,
- idleListener);
- systemActivityNotificationsService
- .addSystemActivityChangeListener(idleListener);
-
- startListeningForNewProviders();
- }
- }
-
- /**
- * Start listening for new providers and their registration states.
- */
- private void startListeningForNewProviders()
- {
- // listen for new providers
- GeneralConfigPluginActivator.bundleContext.addServiceListener(this);
-
- // lets check current providers
- ServiceReference[] protocolProviderRefs = null;
- try
- {
- protocolProviderRefs = GeneralConfigPluginActivator.bundleContext
- .getServiceReferences(ProtocolProviderService.class.getName(),
- null);
- }
- catch (InvalidSyntaxException ex)
- {
- // this shouldn't happen since we're providing no parameter string
- // but let's log just in case.
- logger.error(
- "Error while retrieving service refs", ex);
- return;
- }
-
- // in case we found any
- if (protocolProviderRefs != null)
- {
- for (int i = 0; i < protocolProviderRefs.length; i++)
- {
- ProtocolProviderService provider = (ProtocolProviderService)
- GeneralConfigPluginActivator.bundleContext
- .getService(protocolProviderRefs[i]);
-
- this.handleProviderAdded(provider);
- }
- }
- }
-
- /**
- * Stop listening for new providers and their registration states.
- */
- private void stopListeningForNewProviders()
- {
- // stop listen for new providers
- GeneralConfigPluginActivator.bundleContext.removeServiceListener(this);
-
- // lets check current providers and remove registration state listener
- ServiceReference[] protocolProviderRefs = null;
- try
- {
- protocolProviderRefs = GeneralConfigPluginActivator.bundleContext
- .getServiceReferences(ProtocolProviderService.class.getName(),
- null);
- }
- catch (InvalidSyntaxException ex)
- {
- // this shouldn't happen since we're providing no parameter string
- // but let's log just in case.
- logger.error(
- "Error while retrieving service refs", ex);
- return;
- }
-
- // in case we found any
- if (protocolProviderRefs != null)
- {
- for (int i = 0; i < protocolProviderRefs.length; i++)
- {
- ProtocolProviderService provider = (ProtocolProviderService)
- GeneralConfigPluginActivator.bundleContext
- .getService(protocolProviderRefs[i]);
-
- this.handleProviderRemoved(provider);
- }
- }
- }
-
- /**
- * Stops and removes the listeners.
- */
- public void stop()
- {
- GeneralConfigPluginActivator.bundleContext.removeServiceListener(this);
- stopInner();
- }
-
- /**
- * Stops and removes the listeners.
- */
- private void stopInner()
- {
- if(idleListener != null)
- {
- SystemActivityNotificationsService
- systemActivityNotificationsService
- = getSystemActivityNotificationsService();
-
- systemActivityNotificationsService.removeIdleSystemChangeListener(
- idleListener);
- systemActivityNotificationsService
- .removeSystemActivityChangeListener(idleListener);
-
- stopListeningForNewProviders();
-
- idleListener = null;
- }
- }
-
- /**
- * Change protocol to away saving status so it can be set again when
- * out of idle state.
- */
- private void changeProtocolsToAway()
- {
- for (ProtocolProviderService protocolProvider
- : GeneralConfigPluginActivator.getProtocolProviders())
- {
- OperationSetPresence presence
- = protocolProvider.getOperationSet(
- OperationSetPresence.class);
-
- if(presence == null)
- continue;
-
- PresenceStatus status = presence.getPresenceStatus();
-
- if (status.getStatus() < PresenceStatus.AVAILABLE_THRESHOLD)
- {
- // already (manually) set to away or lower
- continue;
- }
-
- PresenceStatus newStatus
- = StatusUpdateThread.findAwayStatus(presence);
-
- try
- {
- if (newStatus != null && !status.equals(newStatus))
- {
- addProviderToLastStates(protocolProvider, status);
-
- presence.publishPresenceStatus(
- newStatus,
- newStatus.getStatusName());
- }
- }
- catch (IllegalArgumentException e)
- {
- }
- catch (IllegalStateException e)
- {
- }
- catch (OperationFailedException e)
- {
- }
- }
- }
-
- /**
- * Back to status which was already saved before going to idle.
- */
- private void changeProtocolsToPreviousState()
- {
- for (ProtocolProviderService protocolProvider
- : GeneralConfigPluginActivator.getProtocolProviders())
- {
- PresenceStatus lastState = lastStates.get(protocolProvider);
-
- if (lastState != null)
- {
- OperationSetPresence presence
- = protocolProvider.getOperationSet(
- OperationSetPresence.class);
- try
- {
- presence.publishPresenceStatus(lastState, "");
- }
- catch (IllegalArgumentException e)
- {
- }
- catch (IllegalStateException e)
- {
- }
- catch (OperationFailedException e)
- {
- }
- removeProviderFromLastStates(protocolProvider);
- }
- }
- }
-
- /**
- * The SystemActivityNotifications Service.
- * @return the SystemActivityNotifications Service.
- */
- private SystemActivityNotificationsService
- getSystemActivityNotificationsService()
- {
- return
- ServiceUtils.getService(
- GeneralConfigPluginActivator.bundleContext,
- SystemActivityNotificationsService.class);
- }
-
- /**
- * When new protocol provider is registered we add our
- * registration change listener.
- * If unregistered remove reference to the provider and the
- * registration change listener.
- *
- * @param serviceEvent ServiceEvent
- */
- public void serviceChanged(ServiceEvent serviceEvent)
- {
- Object service
- = GeneralConfigPluginActivator.bundleContext.getService(
- serviceEvent.getServiceReference());
-
- // we don't care if the source service is not a protocol provider
- if (service instanceof ProtocolProviderService)
- {
- int serviceEventType = serviceEvent.getType();
-
- if (serviceEventType == ServiceEvent.REGISTERED)
- handleProviderAdded((ProtocolProviderService) service);
- else if (serviceEventType == ServiceEvent.UNREGISTERING)
- handleProviderRemoved((ProtocolProviderService) service);
- }
- }
-
- /**
- * Used to set registration state change listener.
- *
- * @param provider ProtocolProviderService
- */
- private synchronized void handleProviderAdded(
- ProtocolProviderService provider)
- {
- provider.addRegistrationStateChangeListener(this);
- }
-
- /**
- * Removes the registration change listener.
- *
- * @param provider the ProtocolProviderService that has been unregistered.
- */
- private void handleProviderRemoved(ProtocolProviderService provider)
- {
- provider.removeRegistrationStateChangeListener(this);
- }
-
- /**
- * Remove provider from list with last statuses.
- * If this is the last provider stop listening for idle events.
- * @param provider
- */
- private synchronized void removeProviderFromLastStates(
- ProtocolProviderService provider)
- {
- lastStates.remove(provider);
- }
-
- /**
- * Remember provider's last status, normally before setting it to away.
- * If needed start listening for idle events.
- * @param provider the provider.
- * @param status the status to save.
- */
- private synchronized void addProviderToLastStates(
- ProtocolProviderService provider,
- PresenceStatus status)
- {
- if(lastStates.size() == 0)
- start();
-
- lastStates.put(provider, status);
- }
-
- /**
- * Listens for provider states.
- * @param evt
- */
- public void registrationStateChanged(RegistrationStateChangeEvent evt)
- {
- if(evt.getSource() instanceof ProtocolProviderService)
- {
- if(evt.getNewState().equals(RegistrationState.UNREGISTERED)
- || evt.getNewState().equals(RegistrationState.CONNECTION_FAILED))
- {
- removeProviderFromLastStates(evt.getProvider());
- }
- else if(evt.getNewState().equals(
- RegistrationState.REGISTERED))
- {
- // we have at least one provider, so lets start listening
- if(lastStates.size() == 0)
- {
- start();
- }
- else
- {
- // or check are we away
- if(getSystemActivityNotificationsService()
- .getTimeSinceLastInput()
- > Preferences.getTimer()*60*1000)
- {
- // we are away, so update the newly registered provider
- // do it in new thread to give the provider
- // time dispatch his status
- new Thread(new Runnable()
- {
- public void run()
- {
- try{
- Thread.sleep(1000);
- }
- catch(Throwable t){}
-
- changeProtocolsToAway();
- }
- }).start();
- }
- }
- }
- }
- }
-
- /**
- * Listener waiting for idle state change.
- */
- private class IdleListener
- implements SystemActivityChangeListener
- {
- /**
- * Listens for activities and set corresponding statuses.
- *
- * @param event the <tt>NotificationActionTypeEvent</tt>, which is
- */
- public void activityChanged(SystemActivityEvent event)
- {
- switch(event.getEventID())
- {
- case SystemActivityEvent.EVENT_DISPLAY_SLEEP:
- case SystemActivityEvent.EVENT_SCREEN_LOCKED:
- case SystemActivityEvent.EVENT_SCREENSAVER_START:
- case SystemActivityEvent.EVENT_SYSTEM_IDLE:
- changeProtocolsToAway();
- break;
- case SystemActivityEvent.EVENT_DISPLAY_WAKE:
- case SystemActivityEvent.EVENT_SCREEN_UNLOCKED:
- case SystemActivityEvent.EVENT_SCREENSAVER_STOP:
- case SystemActivityEvent.EVENT_SYSTEM_IDLE_END:
- changeProtocolsToPreviousState();
- break;
- }
- }
- }
-}
+package net.java.sip.communicator.plugin.generalconfig.autoaway; + +import java.beans.*; +import java.util.*; + +import net.java.sip.communicator.plugin.generalconfig.*; +import net.java.sip.communicator.service.protocol.*; +import net.java.sip.communicator.service.protocol.event.*; +import net.java.sip.communicator.service.sysactivity.*; +import net.java.sip.communicator.service.sysactivity.event.*; +import net.java.sip.communicator.util.*; + +import org.osgi.framework.*; + +/** + * Listens for idle events from SystemActivityNotifications Service. + * + * @author Damian Minkov + */ +public class AutoAwayWatcher + implements ServiceListener, + RegistrationStateChangeListener +{ + /** + * The logger. + */ + private static final Logger logger + = Logger.getLogger(AutoAwayWatcher.class); + + /** + * The states of providers before going to away. + */ + private final Map<ProtocolProviderService, PresenceStatus> lastStates + = new HashMap<ProtocolProviderService, PresenceStatus>(); + + /** + * Listens for idle events. + */ + private IdleListener idleListener = null; + + /** + * Creates AutoAway handler. + */ + public AutoAwayWatcher() + { + if (Preferences.isEnabled()) + { + start(); + } + + Preferences.addEnableChangeListener( + new PropertyChangeListener() + { + public void propertyChange(PropertyChangeEvent evt) + { + if(Boolean.parseBoolean((String) evt.getNewValue())) + start(); + else + stopInner(); + } + } + ); + + // listens for changes in configured value. + Preferences.addTimerChangeListener( + new PropertyChangeListener() + { + public void propertyChange(PropertyChangeEvent evt) + { + stopInner(); + start(); + } + } + ); + } + + /** + * Starts and add needed listeners. + */ + private void start() + { + if(idleListener == null) + { + + idleListener = new IdleListener(); + + SystemActivityNotificationsService + systemActivityNotificationsService + = getSystemActivityNotificationsService(); + + systemActivityNotificationsService.addIdleSystemChangeListener( + Preferences.getTimer() * 60 * 1000, + idleListener); + systemActivityNotificationsService + .addSystemActivityChangeListener(idleListener); + + startListeningForNewProviders(); + } + } + + /** + * Start listening for new providers and their registration states. + */ + private void startListeningForNewProviders() + { + // listen for new providers + GeneralConfigPluginActivator.bundleContext.addServiceListener(this); + + // lets check current providers + ServiceReference[] protocolProviderRefs = null; + try + { + protocolProviderRefs = GeneralConfigPluginActivator.bundleContext + .getServiceReferences(ProtocolProviderService.class.getName(), + null); + } + catch (InvalidSyntaxException ex) + { + // this shouldn't happen since we're providing no parameter string + // but let's log just in case. + logger.error( + "Error while retrieving service refs", ex); + return; + } + + // in case we found any + if (protocolProviderRefs != null) + { + for (int i = 0; i < protocolProviderRefs.length; i++) + { + ProtocolProviderService provider = (ProtocolProviderService) + GeneralConfigPluginActivator.bundleContext + .getService(protocolProviderRefs[i]); + + this.handleProviderAdded(provider); + } + } + } + + /** + * Stop listening for new providers and their registration states. + */ + private void stopListeningForNewProviders() + { + // stop listen for new providers + GeneralConfigPluginActivator.bundleContext.removeServiceListener(this); + + // lets check current providers and remove registration state listener + ServiceReference[] protocolProviderRefs = null; + try + { + protocolProviderRefs = GeneralConfigPluginActivator.bundleContext + .getServiceReferences(ProtocolProviderService.class.getName(), + null); + } + catch (InvalidSyntaxException ex) + { + // this shouldn't happen since we're providing no parameter string + // but let's log just in case. + logger.error( + "Error while retrieving service refs", ex); + return; + } + + // in case we found any + if (protocolProviderRefs != null) + { + for (int i = 0; i < protocolProviderRefs.length; i++) + { + ProtocolProviderService provider = (ProtocolProviderService) + GeneralConfigPluginActivator.bundleContext + .getService(protocolProviderRefs[i]); + + this.handleProviderRemoved(provider); + } + } + } + + /** + * Stops and removes the listeners. + */ + public void stop() + { + GeneralConfigPluginActivator.bundleContext.removeServiceListener(this); + stopInner(); + } + + /** + * Stops and removes the listeners. + */ + private void stopInner() + { + if(idleListener != null) + { + SystemActivityNotificationsService + systemActivityNotificationsService + = getSystemActivityNotificationsService(); + + systemActivityNotificationsService.removeIdleSystemChangeListener( + idleListener); + systemActivityNotificationsService + .removeSystemActivityChangeListener(idleListener); + + stopListeningForNewProviders(); + + idleListener = null; + } + } + + /** + * Change protocol to away saving status so it can be set again when + * out of idle state. + */ + private void changeProtocolsToAway() + { + for (ProtocolProviderService protocolProvider + : GeneralConfigPluginActivator.getProtocolProviders()) + { + OperationSetPresence presence + = protocolProvider.getOperationSet( + OperationSetPresence.class); + + if(presence == null) + continue; + + PresenceStatus status = presence.getPresenceStatus(); + + if (status.getStatus() < PresenceStatus.AVAILABLE_THRESHOLD) + { + // already (manually) set to away or lower + continue; + } + + PresenceStatus newStatus + = StatusUpdateThread.findAwayStatus(presence); + + try + { + if (newStatus != null && !status.equals(newStatus)) + { + addProviderToLastStates(protocolProvider, status); + + presence.publishPresenceStatus( + newStatus, + newStatus.getStatusName()); + } + } + catch (IllegalArgumentException e) + { + } + catch (IllegalStateException e) + { + } + catch (OperationFailedException e) + { + } + } + } + + /** + * Back to status which was already saved before going to idle. + */ + private void changeProtocolsToPreviousState() + { + for (ProtocolProviderService protocolProvider + : GeneralConfigPluginActivator.getProtocolProviders()) + { + PresenceStatus lastState = lastStates.get(protocolProvider); + + if (lastState != null) + { + OperationSetPresence presence + = protocolProvider.getOperationSet( + OperationSetPresence.class); + try + { + presence.publishPresenceStatus(lastState, ""); + } + catch (IllegalArgumentException e) + { + } + catch (IllegalStateException e) + { + } + catch (OperationFailedException e) + { + } + removeProviderFromLastStates(protocolProvider); + } + } + } + + /** + * The SystemActivityNotifications Service. + * @return the SystemActivityNotifications Service. + */ + private SystemActivityNotificationsService + getSystemActivityNotificationsService() + { + return + ServiceUtils.getService( + GeneralConfigPluginActivator.bundleContext, + SystemActivityNotificationsService.class); + } + + /** + * When new protocol provider is registered we add our + * registration change listener. + * If unregistered remove reference to the provider and the + * registration change listener. + * + * @param serviceEvent ServiceEvent + */ + public void serviceChanged(ServiceEvent serviceEvent) + { + Object service + = GeneralConfigPluginActivator.bundleContext.getService( + serviceEvent.getServiceReference()); + + // we don't care if the source service is not a protocol provider + if (service instanceof ProtocolProviderService) + { + int serviceEventType = serviceEvent.getType(); + + if (serviceEventType == ServiceEvent.REGISTERED) + handleProviderAdded((ProtocolProviderService) service); + else if (serviceEventType == ServiceEvent.UNREGISTERING) + handleProviderRemoved((ProtocolProviderService) service); + } + } + + /** + * Used to set registration state change listener. + * + * @param provider ProtocolProviderService + */ + private synchronized void handleProviderAdded( + ProtocolProviderService provider) + { + provider.addRegistrationStateChangeListener(this); + } + + /** + * Removes the registration change listener. + * + * @param provider the ProtocolProviderService that has been unregistered. + */ + private void handleProviderRemoved(ProtocolProviderService provider) + { + provider.removeRegistrationStateChangeListener(this); + } + + /** + * Remove provider from list with last statuses. + * If this is the last provider stop listening for idle events. + * @param provider + */ + private synchronized void removeProviderFromLastStates( + ProtocolProviderService provider) + { + lastStates.remove(provider); + } + + /** + * Remember provider's last status, normally before setting it to away. + * If needed start listening for idle events. + * @param provider the provider. + * @param status the status to save. + */ + private synchronized void addProviderToLastStates( + ProtocolProviderService provider, + PresenceStatus status) + { + if(lastStates.size() == 0) + start(); + + lastStates.put(provider, status); + } + + /** + * Listens for provider states. + * @param evt + */ + public void registrationStateChanged(RegistrationStateChangeEvent evt) + { + if(evt.getSource() instanceof ProtocolProviderService) + { + if(evt.getNewState().equals(RegistrationState.UNREGISTERED) + || evt.getNewState().equals(RegistrationState.CONNECTION_FAILED)) + { + removeProviderFromLastStates(evt.getProvider()); + } + else if(evt.getNewState().equals( + RegistrationState.REGISTERED)) + { + // we have at least one provider, so lets start listening + if(lastStates.size() == 0) + { + start(); + } + else + { + // or check are we away + if(getSystemActivityNotificationsService() + .getTimeSinceLastInput() + > Preferences.getTimer()*60*1000) + { + // we are away, so update the newly registered provider + // do it in new thread to give the provider + // time dispatch his status + new Thread(new Runnable() + { + public void run() + { + try{ + Thread.sleep(1000); + } + catch(Throwable t){} + + changeProtocolsToAway(); + } + }).start(); + } + } + } + } + } + + /** + * Listener waiting for idle state change. + */ + private class IdleListener + implements SystemActivityChangeListener + { + /** + * Listens for activities and set corresponding statuses. + * + * @param event the <tt>NotificationActionTypeEvent</tt>, which is + */ + public void activityChanged(SystemActivityEvent event) + { + switch(event.getEventID()) + { + case SystemActivityEvent.EVENT_DISPLAY_SLEEP: + case SystemActivityEvent.EVENT_SCREEN_LOCKED: + case SystemActivityEvent.EVENT_SCREENSAVER_START: + case SystemActivityEvent.EVENT_SYSTEM_IDLE: + changeProtocolsToAway(); + break; + case SystemActivityEvent.EVENT_DISPLAY_WAKE: + case SystemActivityEvent.EVENT_SCREEN_UNLOCKED: + case SystemActivityEvent.EVENT_SCREENSAVER_STOP: + case SystemActivityEvent.EVENT_SYSTEM_IDLE_END: + changeProtocolsToPreviousState(); + break; + } + } + } +} |