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/impl/sysactivity | |
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/impl/sysactivity')
5 files changed, 1264 insertions, 1262 deletions
diff --git a/src/net/java/sip/communicator/impl/sysactivity/DBusNetworkManager.java b/src/net/java/sip/communicator/impl/sysactivity/DBusNetworkManager.java index 4c148df..c75e941 100644 --- a/src/net/java/sip/communicator/impl/sysactivity/DBusNetworkManager.java +++ b/src/net/java/sip/communicator/impl/sysactivity/DBusNetworkManager.java @@ -1,4 +1,4 @@ -/*
+/* * Jitsi, the OpenSource Java VoIP and Instant Messaging client. * * Copyright @ 2015 Atlassian Pty Ltd @@ -15,133 +15,133 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package net.java.sip.communicator.impl.sysactivity;
-
-import org.freedesktop.dbus.*;
-import org.freedesktop.dbus.exceptions.*;
-
-/**
- * NetworkManager D-Bus Interface
- *
- * @author Damian Minkov
- * @author Ingo Bauersachs
- */
-@DBusInterfaceName("org.freedesktop.NetworkManager")
-public interface DBusNetworkManager
- extends DBusInterface
-{
- /*
- * Types of NetworkManager states for versions < 0.9
- */
- public static final int NM_STATE_UNKNOWN = 0;
- public static final int NM_STATE_ASLEEP = 1;
- public static final int NM_STATE_CONNECTING = 2;
- public static final int NM_STATE_CONNECTED = 3;
- public static final int NM_STATE_DISCONNECTED = 4;
-
- /*
- * Types of NetworkManager states for versions >= 0.9
- */
- public static final int NM9_STATE_UNKNOWN = 0;
- public static final int NM9_STATE_ASLEEP = 10;
- public static final int NM9_STATE_DISCONNECTED = 20;
- public static final int NM9_STATE_DISCONNECTING = 30;
- public static final int NM9_STATE_CONNECTING = 40;
- public static final int NM9_STATE_CONNECTED_LOCAL = 50;
- public static final int NM9_STATE_CONNECTED_SITE = 60;
- public static final int NM9_STATE_CONNECTED_GLOBAL = 70;
-
- /**
- * State change signal.
- */
- public class StateChange extends DBusSignal
- {
- /**
- * The name of the signal.
- */
- public final String name;
-
- /**
- * The current status it holds.
- */
- public final UInt32 status;
-
- /**
- * Creates status change.
- * @param path the path
- * @param status the status
- * @throws DBusException
- */
- public StateChange(String path, UInt32 status)
- throws DBusException
- {
- super(path, status);
- name = path;
- this.status = status;
- }
-
- /**
- * The current status.
- * @return the current status
- */
- public int getStatus()
- {
- return status.intValue();
- }
-
- /**
- * Returns status description
- * @return the status description
- */
- public String getStatusName()
- {
- switch(status.intValue())
- {
- case NM_STATE_ASLEEP : return "Asleep";
- case NM_STATE_CONNECTING : return "Connecting";
- case NM_STATE_CONNECTED : return "Connected";
- case NM_STATE_DISCONNECTED : return "Disconnected";
- default : return "Unknown";
- }
- }
- }
-
- /**
- * State changed signal.
- */
- public static class StateChanged extends StateChange
- {
- /**
- * Creates status changed.
- * @param path the path
- * @param status the status
- * @throws DBusException
- */
- public StateChanged(String path, UInt32 status)
- throws DBusException
- {
- super(path, status);
- }
-
- /**
- * Returns status description
- * @return the status name
- */
- @Override
- public String getStatusName()
- {
- switch(status.intValue())
- {
- case NM9_STATE_UNKNOWN: return "Unknown";
- case NM9_STATE_ASLEEP: return "Asleep";
- case NM9_STATE_DISCONNECTED: return "Disconnected";
- case NM9_STATE_DISCONNECTING: return "Disconnecting";
- case NM9_STATE_CONNECTING: return "Connecting";
- case NM9_STATE_CONNECTED_LOCAL: return "LocalConnectivity";
- case NM9_STATE_CONNECTED_SITE: return "SiteConnectivity";
- case NM9_STATE_CONNECTED_GLOBAL: return "GlobalConnectivity";
- default : return "Unknown";
- }
- }
- }
-}
+package net.java.sip.communicator.impl.sysactivity; + +import org.freedesktop.dbus.*; +import org.freedesktop.dbus.exceptions.*; + +/** + * NetworkManager D-Bus Interface + * + * @author Damian Minkov + * @author Ingo Bauersachs + */ +@DBusInterfaceName("org.freedesktop.NetworkManager") +public interface DBusNetworkManager + extends DBusInterface +{ + /* + * Types of NetworkManager states for versions < 0.9 + */ + public static final int NM_STATE_UNKNOWN = 0; + public static final int NM_STATE_ASLEEP = 1; + public static final int NM_STATE_CONNECTING = 2; + public static final int NM_STATE_CONNECTED = 3; + public static final int NM_STATE_DISCONNECTED = 4; + + /* + * Types of NetworkManager states for versions >= 0.9 + */ + public static final int NM9_STATE_UNKNOWN = 0; + public static final int NM9_STATE_ASLEEP = 10; + public static final int NM9_STATE_DISCONNECTED = 20; + public static final int NM9_STATE_DISCONNECTING = 30; + public static final int NM9_STATE_CONNECTING = 40; + public static final int NM9_STATE_CONNECTED_LOCAL = 50; + public static final int NM9_STATE_CONNECTED_SITE = 60; + public static final int NM9_STATE_CONNECTED_GLOBAL = 70; + + /** + * State change signal. + */ + public class StateChange extends DBusSignal + { + /** + * The name of the signal. + */ + public final String name; + + /** + * The current status it holds. + */ + public final UInt32 status; + + /** + * Creates status change. + * @param path the path + * @param status the status + * @throws DBusException + */ + public StateChange(String path, UInt32 status) + throws DBusException + { + super(path, status); + name = path; + this.status = status; + } + + /** + * The current status. + * @return the current status + */ + public int getStatus() + { + return status.intValue(); + } + + /** + * Returns status description + * @return the status description + */ + public String getStatusName() + { + switch(status.intValue()) + { + case NM_STATE_ASLEEP : return "Asleep"; + case NM_STATE_CONNECTING : return "Connecting"; + case NM_STATE_CONNECTED : return "Connected"; + case NM_STATE_DISCONNECTED : return "Disconnected"; + default : return "Unknown"; + } + } + } + + /** + * State changed signal. + */ + public static class StateChanged extends StateChange + { + /** + * Creates status changed. + * @param path the path + * @param status the status + * @throws DBusException + */ + public StateChanged(String path, UInt32 status) + throws DBusException + { + super(path, status); + } + + /** + * Returns status description + * @return the status name + */ + @Override + public String getStatusName() + { + switch(status.intValue()) + { + case NM9_STATE_UNKNOWN: return "Unknown"; + case NM9_STATE_ASLEEP: return "Asleep"; + case NM9_STATE_DISCONNECTED: return "Disconnected"; + case NM9_STATE_DISCONNECTING: return "Disconnecting"; + case NM9_STATE_CONNECTING: return "Connecting"; + case NM9_STATE_CONNECTED_LOCAL: return "LocalConnectivity"; + case NM9_STATE_CONNECTED_SITE: return "SiteConnectivity"; + case NM9_STATE_CONNECTED_GLOBAL: return "GlobalConnectivity"; + default : return "Unknown"; + } + } + } +} diff --git a/src/net/java/sip/communicator/impl/sysactivity/NetworkManagerListenerImpl.java b/src/net/java/sip/communicator/impl/sysactivity/NetworkManagerListenerImpl.java index 952645a..94f8748 100644 --- a/src/net/java/sip/communicator/impl/sysactivity/NetworkManagerListenerImpl.java +++ b/src/net/java/sip/communicator/impl/sysactivity/NetworkManagerListenerImpl.java @@ -1,4 +1,4 @@ -/*
+/* * Jitsi, the OpenSource Java VoIP and Instant Messaging client. * * Copyright @ 2015 Atlassian Pty Ltd @@ -15,164 +15,164 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package net.java.sip.communicator.impl.sysactivity;
-
-import net.java.sip.communicator.service.sysactivity.event.*;
-import net.java.sip.communicator.util.*;
-
-import org.freedesktop.*;
-import org.freedesktop.dbus.*;
-import org.freedesktop.dbus.exceptions.*;
-
-/**
- * Responsible for subscribe and dispatch signals from NetworkManager.
- * Uses dbus to connect.
- *
- * @author Damian Minkov
- */
-@SuppressWarnings("rawtypes")
-public class NetworkManagerListenerImpl
- implements DBusSigHandler,
- SystemActivityManager
-{
- /**
- * The logger.
- */
- private Logger logger = Logger.getLogger(
- NetworkManagerListenerImpl.class.getName());
-
- /**
- * Dbus connection we use.
- */
- private DBusConnection dbusConn;
-
- /**
- * Make only one instance.
- */
- public NetworkManagerListenerImpl()
- {
- try
- {
- dbusConn = DBusConnection.getConnection(DBusConnection.SYSTEM);
- }
- catch(DBusException e)
- {
- logger.error("Cannot obtain dbus connection", e);
- }
- }
-
- /**
- * Starts
- */
- @SuppressWarnings("unchecked")
- public void start()
- {
- // on error connecting to dbus do nothing
- if(dbusConn == null)
- return;
-
- try
- {
- dbusConn.addSigHandler(DBus.NameOwnerChanged.class, this);
- dbusConn.addSigHandler(DBusNetworkManager.StateChange.class, this);
- dbusConn.addSigHandler(DBusNetworkManager.StateChanged.class, this);
- }
- catch(DBusException e)
- {
- logger.error("Error adding dbus signal handlers", e);
- }
- }
-
- /**
- * Stops.
- */
- @SuppressWarnings("unchecked")
- public void stop()
- {
- // on error connecting to dbus do nothing
- if(dbusConn == null)
- return;
-
- try
- {
- dbusConn.removeSigHandler(DBus.NameOwnerChanged.class, this);
- dbusConn.removeSigHandler(
- DBusNetworkManager.StateChange.class, this);
- dbusConn.removeSigHandler(
- DBusNetworkManager.StateChanged.class, this);
- }
- catch(DBusException e)
- {
- logger.error("Error removing dbus signal handlers", e);
- }
- }
-
- /**
- * Receives signals and dispatch them.
- * @param dBusSignal signal to handle.
- */
- public void handle(DBusSignal dBusSignal)
- {
- if(dBusSignal instanceof DBus.NameOwnerChanged)
- {
- DBus.NameOwnerChanged nameOwnerChanged =
- (DBus.NameOwnerChanged)dBusSignal;
-
- if(nameOwnerChanged.name.equals("org.freedesktop.NetworkManager"))
- {
- boolean b1 = nameOwnerChanged.old_owner != null
- && nameOwnerChanged.old_owner.length() > 0;
- boolean b2 = nameOwnerChanged.new_owner != null
- && nameOwnerChanged.new_owner.length() > 0;
-
- if(b1 && !b2)
- {
- SystemActivityEvent evt = new SystemActivityEvent(
- SysActivityActivator.getSystemActivityService(),
- SystemActivityEvent.EVENT_NETWORK_CHANGE);
- SysActivityActivator.getSystemActivityService()
- .fireSystemActivityEvent(evt);
- }
- }
- }
- else if(dBusSignal instanceof DBusNetworkManager.StateChange)
- {
- DBusNetworkManager.StateChange stateChange =
- (DBusNetworkManager.StateChange)dBusSignal;
-
- SystemActivityEvent evt = null;
- switch(stateChange.getStatus())
- {
- case DBusNetworkManager.NM_STATE_CONNECTED:
- case DBusNetworkManager.NM_STATE_DISCONNECTED:
- case DBusNetworkManager.NM9_STATE_DISCONNECTED:
- case DBusNetworkManager.NM9_STATE_CONNECTED_LOCAL:
- case DBusNetworkManager.NM9_STATE_CONNECTED_SITE:
- case DBusNetworkManager.NM9_STATE_CONNECTED_GLOBAL:
- evt = new SystemActivityEvent(
- SysActivityActivator.getSystemActivityService(),
- SystemActivityEvent.EVENT_NETWORK_CHANGE);
- break;
- case DBusNetworkManager.NM_STATE_ASLEEP:
- case DBusNetworkManager.NM9_STATE_ASLEEP:
- evt = new SystemActivityEvent(
- SysActivityActivator.getSystemActivityService(),
- SystemActivityEvent.EVENT_SLEEP);
- break;
- }
-
- if(evt != null)
- SysActivityActivator.getSystemActivityService()
- .fireSystemActivityEvent(evt);
- }
- }
-
- /**
- * Whether we are connected to the network manager through dbus.
- * @return whether we are connected to the network manager.
- */
- public boolean isConnected()
- {
- return dbusConn != null;
- }
-}
+package net.java.sip.communicator.impl.sysactivity; + +import net.java.sip.communicator.service.sysactivity.event.*; +import net.java.sip.communicator.util.*; + +import org.freedesktop.*; +import org.freedesktop.dbus.*; +import org.freedesktop.dbus.exceptions.*; + +/** + * Responsible for subscribe and dispatch signals from NetworkManager. + * Uses dbus to connect. + * + * @author Damian Minkov + */ +@SuppressWarnings("rawtypes") +public class NetworkManagerListenerImpl + implements DBusSigHandler, + SystemActivityManager +{ + /** + * The logger. + */ + private Logger logger = Logger.getLogger( + NetworkManagerListenerImpl.class.getName()); + + /** + * Dbus connection we use. + */ + private DBusConnection dbusConn; + + /** + * Make only one instance. + */ + public NetworkManagerListenerImpl() + { + try + { + dbusConn = DBusConnection.getConnection(DBusConnection.SYSTEM); + } + catch(DBusException e) + { + logger.error("Cannot obtain dbus connection", e); + } + } + + /** + * Starts + */ + @SuppressWarnings("unchecked") + public void start() + { + // on error connecting to dbus do nothing + if(dbusConn == null) + return; + + try + { + dbusConn.addSigHandler(DBus.NameOwnerChanged.class, this); + dbusConn.addSigHandler(DBusNetworkManager.StateChange.class, this); + dbusConn.addSigHandler(DBusNetworkManager.StateChanged.class, this); + } + catch(DBusException e) + { + logger.error("Error adding dbus signal handlers", e); + } + } + + /** + * Stops. + */ + @SuppressWarnings("unchecked") + public void stop() + { + // on error connecting to dbus do nothing + if(dbusConn == null) + return; + + try + { + dbusConn.removeSigHandler(DBus.NameOwnerChanged.class, this); + dbusConn.removeSigHandler( + DBusNetworkManager.StateChange.class, this); + dbusConn.removeSigHandler( + DBusNetworkManager.StateChanged.class, this); + } + catch(DBusException e) + { + logger.error("Error removing dbus signal handlers", e); + } + } + + /** + * Receives signals and dispatch them. + * @param dBusSignal signal to handle. + */ + public void handle(DBusSignal dBusSignal) + { + if(dBusSignal instanceof DBus.NameOwnerChanged) + { + DBus.NameOwnerChanged nameOwnerChanged = + (DBus.NameOwnerChanged)dBusSignal; + + if(nameOwnerChanged.name.equals("org.freedesktop.NetworkManager")) + { + boolean b1 = nameOwnerChanged.old_owner != null + && nameOwnerChanged.old_owner.length() > 0; + boolean b2 = nameOwnerChanged.new_owner != null + && nameOwnerChanged.new_owner.length() > 0; + + if(b1 && !b2) + { + SystemActivityEvent evt = new SystemActivityEvent( + SysActivityActivator.getSystemActivityService(), + SystemActivityEvent.EVENT_NETWORK_CHANGE); + SysActivityActivator.getSystemActivityService() + .fireSystemActivityEvent(evt); + } + } + } + else if(dBusSignal instanceof DBusNetworkManager.StateChange) + { + DBusNetworkManager.StateChange stateChange = + (DBusNetworkManager.StateChange)dBusSignal; + + SystemActivityEvent evt = null; + switch(stateChange.getStatus()) + { + case DBusNetworkManager.NM_STATE_CONNECTED: + case DBusNetworkManager.NM_STATE_DISCONNECTED: + case DBusNetworkManager.NM9_STATE_DISCONNECTED: + case DBusNetworkManager.NM9_STATE_CONNECTED_LOCAL: + case DBusNetworkManager.NM9_STATE_CONNECTED_SITE: + case DBusNetworkManager.NM9_STATE_CONNECTED_GLOBAL: + evt = new SystemActivityEvent( + SysActivityActivator.getSystemActivityService(), + SystemActivityEvent.EVENT_NETWORK_CHANGE); + break; + case DBusNetworkManager.NM_STATE_ASLEEP: + case DBusNetworkManager.NM9_STATE_ASLEEP: + evt = new SystemActivityEvent( + SysActivityActivator.getSystemActivityService(), + SystemActivityEvent.EVENT_SLEEP); + break; + } + + if(evt != null) + SysActivityActivator.getSystemActivityService() + .fireSystemActivityEvent(evt); + } + } + + /** + * Whether we are connected to the network manager through dbus. + * @return whether we are connected to the network manager. + */ + public boolean isConnected() + { + return dbusConn != null; + } +} diff --git a/src/net/java/sip/communicator/impl/sysactivity/SysActivityActivator.java b/src/net/java/sip/communicator/impl/sysactivity/SysActivityActivator.java index 6a46e18..96cea29 100644 --- a/src/net/java/sip/communicator/impl/sysactivity/SysActivityActivator.java +++ b/src/net/java/sip/communicator/impl/sysactivity/SysActivityActivator.java @@ -1,4 +1,4 @@ -/*
+/* * Jitsi, the OpenSource Java VoIP and Instant Messaging client. * * Copyright @ 2015 Atlassian Pty Ltd @@ -15,101 +15,101 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package net.java.sip.communicator.impl.sysactivity;
-
-import net.java.sip.communicator.service.sysactivity.*;
-import net.java.sip.communicator.util.*;
-
-import org.osgi.framework.*;
-
-/**
- * Listens for system activity changes like sleep, network change, inactivity
- * and informs all its listeners.
- *
- * @author Damian Minkov
- */
-public class SysActivityActivator
- implements BundleActivator
-{
- /**
- * The <tt>Logger</tt> used by this <tt>SysActivityActivator</tt> for
- * logging output.
- */
- private final Logger logger = Logger.getLogger(SysActivityActivator.class);
-
- /**
- * The OSGi <tt>BundleContext</tt>.
- */
- private static BundleContext bundleContext = null;
-
- /**
- * The system activity service impl.
- */
- private static SystemActivityNotificationsServiceImpl
- sysActivitiesServiceImpl;
-
- /**
- * Called when this bundle is started so the Framework can perform the
- * bundle-specific activities necessary to start this bundle.
- *
- * @param bundleContext The execution context of the bundle being started.
- * @throws Exception If this method throws an exception, this bundle is
- * marked as stopped and the Framework will remove this bundle's listeners,
- * unregister all services registered by this bundle, and release all
- * services used by this bundle.
- */
- public void start(BundleContext bundleContext)
- throws Exception
- {
- SysActivityActivator.bundleContext = bundleContext;
-
- if (logger.isDebugEnabled())
- logger.debug("Started.");
-
- sysActivitiesServiceImpl = new SystemActivityNotificationsServiceImpl();
- sysActivitiesServiceImpl.start();
-
- bundleContext.registerService(
- SystemActivityNotificationsService.class.getName(),
- sysActivitiesServiceImpl,
- null);
- }
-
- /**
- * Returns a reference to the bundle context that we were started with.
- * @return a reference to the BundleContext instance that we were started
- * with.
- */
- public static SystemActivityNotificationsServiceImpl
- getSystemActivityService()
- {
- return sysActivitiesServiceImpl;
- }
-
- /**
- * Called when this bundle is stopped so the Framework can perform the
- * bundle-specific activities necessary to stop the bundle.
- *
- * @param bundleContext The execution context of the bundle being stopped.
- * @throws Exception If this method throws an exception, the bundle is still
- * marked as stopped, and the Framework will remove the bundle's listeners,
- * unregister all services registered by the bundle, and release all
- * services used by the bundle.
- */
- public void stop(BundleContext bundleContext)
- throws Exception
- {
- if (sysActivitiesServiceImpl != null)
- sysActivitiesServiceImpl.stop();
- }
-
- /**
- * Returns a reference to the bundle context that we were started with.
- * @return a reference to the BundleContext instance that we were started
- * with.
- */
- public static BundleContext getBundleContext()
- {
- return bundleContext;
- }
-}
+package net.java.sip.communicator.impl.sysactivity; + +import net.java.sip.communicator.service.sysactivity.*; +import net.java.sip.communicator.util.*; + +import org.osgi.framework.*; + +/** + * Listens for system activity changes like sleep, network change, inactivity + * and informs all its listeners. + * + * @author Damian Minkov + */ +public class SysActivityActivator + implements BundleActivator +{ + /** + * The <tt>Logger</tt> used by this <tt>SysActivityActivator</tt> for + * logging output. + */ + private final Logger logger = Logger.getLogger(SysActivityActivator.class); + + /** + * The OSGi <tt>BundleContext</tt>. + */ + private static BundleContext bundleContext = null; + + /** + * The system activity service impl. + */ + private static SystemActivityNotificationsServiceImpl + sysActivitiesServiceImpl; + + /** + * Called when this bundle is started so the Framework can perform the + * bundle-specific activities necessary to start this bundle. + * + * @param bundleContext The execution context of the bundle being started. + * @throws Exception If this method throws an exception, this bundle is + * marked as stopped and the Framework will remove this bundle's listeners, + * unregister all services registered by this bundle, and release all + * services used by this bundle. + */ + public void start(BundleContext bundleContext) + throws Exception + { + SysActivityActivator.bundleContext = bundleContext; + + if (logger.isDebugEnabled()) + logger.debug("Started."); + + sysActivitiesServiceImpl = new SystemActivityNotificationsServiceImpl(); + sysActivitiesServiceImpl.start(); + + bundleContext.registerService( + SystemActivityNotificationsService.class.getName(), + sysActivitiesServiceImpl, + null); + } + + /** + * Returns a reference to the bundle context that we were started with. + * @return a reference to the BundleContext instance that we were started + * with. + */ + public static SystemActivityNotificationsServiceImpl + getSystemActivityService() + { + return sysActivitiesServiceImpl; + } + + /** + * Called when this bundle is stopped so the Framework can perform the + * bundle-specific activities necessary to stop the bundle. + * + * @param bundleContext The execution context of the bundle being stopped. + * @throws Exception If this method throws an exception, the bundle is still + * marked as stopped, and the Framework will remove the bundle's listeners, + * unregister all services registered by the bundle, and release all + * services used by the bundle. + */ + public void stop(BundleContext bundleContext) + throws Exception + { + if (sysActivitiesServiceImpl != null) + sysActivitiesServiceImpl.stop(); + } + + /** + * Returns a reference to the bundle context that we were started with. + * @return a reference to the BundleContext instance that we were started + * with. + */ + public static BundleContext getBundleContext() + { + return bundleContext; + } +} diff --git a/src/net/java/sip/communicator/impl/sysactivity/SystemActivityNotifications.java b/src/net/java/sip/communicator/impl/sysactivity/SystemActivityNotifications.java index a6ff936..e2fb17a 100644 --- a/src/net/java/sip/communicator/impl/sysactivity/SystemActivityNotifications.java +++ b/src/net/java/sip/communicator/impl/sysactivity/SystemActivityNotifications.java @@ -1,4 +1,4 @@ -/*
+/* * Jitsi, the OpenSource Java VoIP and Instant Messaging client. * * Copyright @ 2015 Atlassian Pty Ltd @@ -15,235 +15,237 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package net.java.sip.communicator.impl.sysactivity;
-
-import net.java.sip.communicator.util.*;
-
-/**
- * @author Damian Minkov
- */
-public class SystemActivityNotifications
-{
- /**
- * The <tt>Logger</tt> used by the <tt>SystemActivityNotifications</tt>
- * class to log debugging information.
- */
- private static final Logger logger
- = Logger.getLogger(SystemActivityNotifications.class);
-
- /**
- * Computer display has stand by.
- */
- public static final int NOTIFY_DISPLAY_SLEEP = 2;
-
- /**
- * Computer display wakes up after stand by.
- */
- public static final int NOTIFY_DISPLAY_WAKE = 3;
-
- /**
- * A change in dns configuration has occurred.
- */
- public static final int NOTIFY_DNS_CHANGE = 10;
-
- /**
- * All processes have been informed about ending session, now notify for
- * the actual end session.
- */
- public static final int NOTIFY_ENDSESSION = 12;
-
- /**
- * A change in network configuration has occurred.
- */
- public static final int NOTIFY_NETWORK_CHANGE = 9;
-
- /**
- * Notifies for start of process of ending desktop session,
- * logoff or shutdown.
- */
- public static final int NOTIFY_QUERY_ENDSESSION = 11;
-
- /**
- * Screen has been locked.
- */
- public static final int NOTIFY_SCREEN_LOCKED = 7;
-
- /**
- * Screen has been unlocked.
- */
- public static final int NOTIFY_SCREEN_UNLOCKED = 8;
-
- /**
- * Screensaver has been started.
- */
- public static final int NOTIFY_SCREENSAVER_START = 4;
-
- /**
- * Screensaver has been stopped.
- */
- public static final int NOTIFY_SCREENSAVER_STOP = 6;
-
- /**
- * Screensaver will stop.
- */
- public static final int NOTIFY_SCREENSAVER_WILL_STOP = 5;
-
- /**
- * Notify that computers is going to sleep.
- */
- public static final int NOTIFY_SLEEP = 0;
-
- /**
- * Notify that computer is wakeing up after stand by.
- */
- public static final int NOTIFY_WAKE = 1;
-
- /**
- * The native instance.
- */
- private static long ptr;
-
- /**
- * Init native library.
- */
- static
- {
- try
- {
- // Don't load native library on Android to prevent the exception
- if(!org.jitsi.util.OSUtils.IS_ANDROID)
- {
- System.loadLibrary("sysactivitynotifications");
-
- ptr = allocAndInit();
- if (ptr == -1)
- ptr = 0;
- }
- }
- catch (Throwable t)
- {
- if (t instanceof ThreadDeath)
- throw (ThreadDeath) t;
- else
- logger.warn("Failed to initialize native counterpart", t);
- }
- }
-
- /**
- * Allocate native resources and gets a pointer.
- *
- * @return
- */
- private static native long allocAndInit();
-
- /**
- * Returns the when was last input in milliseconds. The time when there was
- * any activity on the computer.
- *
- * @return the last input in milliseconds
- */
- public static native long getLastInput();
-
- /**
- * Whether native library is loaded.
- *
- * @return whether native library is loaded.
- */
- public static boolean isLoaded()
- {
- return (ptr != 0);
- }
-
- /**
- * Release native resources.
- *
- * @param ptr
- */
- private static native void release(long ptr);
-
- /**
- * Sets notifier delegate.
- *
- * @param ptr
- * @param delegate
- */
- public static native void setDelegate(
- long ptr,
- NotificationsDelegate delegate);
-
- /**
- * Sets delegate.
- *
- * @param delegate
- */
- public static void setDelegate(NotificationsDelegate delegate)
- {
- if (ptr != 0)
- setDelegate(ptr, delegate);
- }
-
- /**
- * Start.
- */
- public static void start()
- {
- if (ptr != 0)
- start(ptr);
- }
-
- /**
- * Start processing.
- *
- * @param ptr
- */
- private static native void start(long ptr);
-
- /**
- * Stop.
- */
- public static void stop()
- {
- if (ptr != 0)
- {
- stop(ptr);
- release(ptr);
- ptr = 0;
- }
- }
-
- /**
- * Stop processing.
- *
- * @param ptr
- */
- private static native void stop(long ptr);
-
- /**
- * Delegate class to be notified about changes.
- */
- public interface NotificationsDelegate
- {
- /**
- * Callback method when receiving notifications.
- *
- * @param type
- */
- public void notify(int type);
-
- /**
- * Callback method when receiving special network notifications.
- *
- * @param family family of network change (ipv6, ipv4)
- * @param luidIndex unique index of interface
- * @param name name of the interface
- * @param type of the interface
- * @param connected whether interface is connected or not.
- */
- public void notifyNetworkChange(
- int family,
- long luidIndex,
- String name,
- long type,
- boolean connected);
- }
-}
+package net.java.sip.communicator.impl.sysactivity; + +import net.java.sip.communicator.util.Logger; +import org.jitsi.util.*; + +/** + * @author Damian Minkov + */ +public class SystemActivityNotifications +{ + /** + * The <tt>Logger</tt> used by the <tt>SystemActivityNotifications</tt> + * class to log debugging information. + */ + private static final Logger logger + = Logger.getLogger(SystemActivityNotifications.class); + + /** + * Computer display has stand by. + */ + public static final int NOTIFY_DISPLAY_SLEEP = 2; + + /** + * Computer display wakes up after stand by. + */ + public static final int NOTIFY_DISPLAY_WAKE = 3; + + /** + * A change in dns configuration has occurred. + */ + public static final int NOTIFY_DNS_CHANGE = 10; + + /** + * All processes have been informed about ending session, now notify for + * the actual end session. + */ + public static final int NOTIFY_ENDSESSION = 12; + + /** + * A change in network configuration has occurred. + */ + public static final int NOTIFY_NETWORK_CHANGE = 9; + + /** + * Notifies for start of process of ending desktop session, + * logoff or shutdown. + */ + public static final int NOTIFY_QUERY_ENDSESSION = 11; + + /** + * Screen has been locked. + */ + public static final int NOTIFY_SCREEN_LOCKED = 7; + + /** + * Screen has been unlocked. + */ + public static final int NOTIFY_SCREEN_UNLOCKED = 8; + + /** + * Screensaver has been started. + */ + public static final int NOTIFY_SCREENSAVER_START = 4; + + /** + * Screensaver has been stopped. + */ + public static final int NOTIFY_SCREENSAVER_STOP = 6; + + /** + * Screensaver will stop. + */ + public static final int NOTIFY_SCREENSAVER_WILL_STOP = 5; + + /** + * Notify that computers is going to sleep. + */ + public static final int NOTIFY_SLEEP = 0; + + /** + * Notify that computer is wakeing up after stand by. + */ + public static final int NOTIFY_WAKE = 1; + + /** + * The native instance. + */ + private static long ptr; + + /** + * Init native library. + */ + static + { + try + { + // Don't load native library on Android to prevent the exception + if(!org.jitsi.util.OSUtils.IS_ANDROID) + { + JNIUtils.loadLibrary("sysactivitynotifications", + SystemActivityNotifications.class); + + ptr = allocAndInit(); + if (ptr == -1) + ptr = 0; + } + } + catch (Throwable t) + { + if (t instanceof ThreadDeath) + throw (ThreadDeath) t; + else + logger.warn("Failed to initialize native counterpart", t); + } + } + + /** + * Allocate native resources and gets a pointer. + * + * @return + */ + private static native long allocAndInit(); + + /** + * Returns the when was last input in milliseconds. The time when there was + * any activity on the computer. + * + * @return the last input in milliseconds + */ + public static native long getLastInput(); + + /** + * Whether native library is loaded. + * + * @return whether native library is loaded. + */ + public static boolean isLoaded() + { + return (ptr != 0); + } + + /** + * Release native resources. + * + * @param ptr + */ + private static native void release(long ptr); + + /** + * Sets notifier delegate. + * + * @param ptr + * @param delegate + */ + public static native void setDelegate( + long ptr, + NotificationsDelegate delegate); + + /** + * Sets delegate. + * + * @param delegate + */ + public static void setDelegate(NotificationsDelegate delegate) + { + if (ptr != 0) + setDelegate(ptr, delegate); + } + + /** + * Start. + */ + public static void start() + { + if (ptr != 0) + start(ptr); + } + + /** + * Start processing. + * + * @param ptr + */ + private static native void start(long ptr); + + /** + * Stop. + */ + public static void stop() + { + if (ptr != 0) + { + stop(ptr); + release(ptr); + ptr = 0; + } + } + + /** + * Stop processing. + * + * @param ptr + */ + private static native void stop(long ptr); + + /** + * Delegate class to be notified about changes. + */ + public interface NotificationsDelegate + { + /** + * Callback method when receiving notifications. + * + * @param type + */ + public void notify(int type); + + /** + * Callback method when receiving special network notifications. + * + * @param family family of network change (ipv6, ipv4) + * @param luidIndex unique index of interface + * @param name name of the interface + * @param type of the interface + * @param connected whether interface is connected or not. + */ + public void notifyNetworkChange( + int family, + long luidIndex, + String name, + long type, + boolean connected); + } +} diff --git a/src/net/java/sip/communicator/impl/sysactivity/SystemActivityNotificationsServiceImpl.java b/src/net/java/sip/communicator/impl/sysactivity/SystemActivityNotificationsServiceImpl.java index c25504f..c58805b 100644 --- a/src/net/java/sip/communicator/impl/sysactivity/SystemActivityNotificationsServiceImpl.java +++ b/src/net/java/sip/communicator/impl/sysactivity/SystemActivityNotificationsServiceImpl.java @@ -1,4 +1,4 @@ -/*
+/* * Jitsi, the OpenSource Java VoIP and Instant Messaging client. * * Copyright @ 2015 Atlassian Pty Ltd @@ -15,639 +15,639 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package net.java.sip.communicator.impl.sysactivity;
-
-import java.util.*;
-
-import net.java.sip.communicator.service.sysactivity.*;
-import net.java.sip.communicator.service.sysactivity.event.*;
-import net.java.sip.communicator.util.Logger;
-
-import org.jitsi.util.*;
-
-/**
- * Service implementation listens for computer changes as sleeping, network
- * change, inactivity.
- *
- * @author Damian Minkov
- */
-public class SystemActivityNotificationsServiceImpl
- implements SystemActivityNotifications.NotificationsDelegate,
- SystemActivityNotificationsService,
- Runnable
-{
- /**
- * The <tt>Logger</tt> used by this
- * <tt>SystemActivityNotificationsServiceImpl</tt> for logging output.
- */
- private final Logger logger
- = Logger.getLogger(SystemActivityNotificationsServiceImpl.class);
-
- /**
- * The thread dispatcher of network change events.
- */
- private final SystemActivityEventDispatcher eventDispatcher
- = new SystemActivityEventDispatcher();
-
- /**
- * A list of listeners registered for idle events.
- */
- private final Map<SystemActivityChangeListener,Long> idleChangeListeners
- = new HashMap<SystemActivityChangeListener, Long>();
-
- /**
- * Listeners which are fired for idle state and which will be fired
- * with idle end when needed.
- */
- private final List<SystemActivityChangeListener> listenersInIdleState
- = new ArrayList<SystemActivityChangeListener>();
-
- /**
- * The interval between checks when not idle.
- */
- private static final int CHECK_FOR_IDLE_DEFAULT = 30 * 1000;
-
- /**
- * The interval between checks when idle. The interval is shorter
- * so we can react almost immediately when we are active again.
- */
- private static final int CHECK_FOR_IDLE_WHEN_IDLE = 1000;
-
- /**
- * The time in milliseconds between two checks for system idle.
- */
- private static int idleStateCheckDelay = CHECK_FOR_IDLE_DEFAULT;
-
- /**
- * Whether current service is started or stopped.
- */
- private boolean running = false;
-
- /**
- * The time when we received latest network change event.
- */
- private long lastNetworkChange = -1;
-
- /**
- * Sometimes (on windows) we got several network change events
- * this is the time after which latest event we will skip next events.
- */
- private static final long NETWORK_EVENT_SILENT_TIME = 10*1000;
-
- /**
- * Whether network is currently connected.
- */
- private Boolean networkIsConnected = null;
-
- /**
- * The linux impl class name.
- */
- private static final String SYSTEM_ACTIVITY_MANAGER_LINUX_CLASS
- = "net.java.sip.communicator.impl.sysactivity.NetworkManagerListenerImpl";
-
- /**
- * The android impl class name.
- */
- private static final String SYSTEM_ACTIVITY_MANAGER_ANDROID_CLASS
- = "net.java.sip.communicator.impl.sysactivity.ConnectivityManagerListenerImpl";
-
- /**
- * The currently instantiated and working manager.
- */
- private SystemActivityManager currentRunningManager = null;
-
- /**
- * Init and start notifications.
- */
- public void start()
- {
- running = true;
-
- // set the delegate and start notification in new thread
- // make sure we don't block startup process
- Thread notifystartThread
- = new Thread(
- new Runnable()
- {
- public void run()
- {
- SystemActivityNotifications.setDelegate(
- SystemActivityNotificationsServiceImpl.this);
- SystemActivityNotifications.start();
- }
- },
- "SystemActivityNotificationsServiceImpl");
- notifystartThread.setDaemon(true);
- notifystartThread.start();
-
- if(isSupported(SystemActivityEvent.EVENT_SYSTEM_IDLE))
- {
- // a thread periodically checks system idle state and if it pass the
- // idle time for a particular listener, will inform it.
- Thread idleNotifyThread = new Thread(
- this,
- "SystemActivityNotificationsServiceImpl.IdleNotifyThread");
- idleNotifyThread.setDaemon(true);
- idleNotifyThread.start();
- }
-
- if (getCurrentRunningManager() != null)
- getCurrentRunningManager().start();
- }
-
- /**
- * Stop notifications.
- */
- public void stop()
- {
- SystemActivityNotifications.stop();
-
- if (getCurrentRunningManager() != null)
- getCurrentRunningManager().stop();
-
- eventDispatcher.stop();
-
- running = false;
-
- synchronized(this)
- {
- this.notifyAll();
- }
- }
-
- /**
- * Registers a listener that would be notified of changes that have occurred
- * in the underlying system.
- *
- * @param listener the listener that we'd like to register for changes in
- * the underlying system.
- */
- public void addSystemActivityChangeListener(
- SystemActivityChangeListener listener)
- {
- eventDispatcher.addSystemActivityChangeListener(listener);
- }
-
- /**
- * Remove the specified listener so that it won't receive further
- * notifications of changes that occur in the underlying system
- *
- * @param listener the listener to remove.
- */
- public void removeSystemActivityChangeListener(
- SystemActivityChangeListener listener)
- {
- eventDispatcher.removeSystemActivityChangeListener(listener);
- }
-
- /**
- * Registers a listener that would be notified for idle of the system
- * for <tt>idleTime</tt>.
- *
- * @param idleTime the time in milliseconds after which we will consider
- * system to be idle. This doesn't count when system seems idle as
- * monitor is off or screensaver is on, or desktop is locked.
- * @param listener the listener that we'd like to register for changes in
- * the underlying system.
- */
- public void addIdleSystemChangeListener(
- long idleTime,
- SystemActivityChangeListener listener)
- {
- synchronized (idleChangeListeners)
- {
- if (idleTime > 0
- && !idleChangeListeners.containsKey(listener))
- idleChangeListeners.put(listener, idleTime);
- }
- }
-
- /**
- * Remove the specified listener so that it won't receive further
- * notifications for idle system.
- *
- * @param listener the listener to remove.
- */
- public void removeIdleSystemChangeListener(
- SystemActivityChangeListener listener)
- {
- synchronized (idleChangeListeners)
- {
- idleChangeListeners.remove(listener);
- }
- }
-
- /**
- * The time since last user input. The time the system has been idle.
- * @return time the system has been idle.
- */
- public long getTimeSinceLastInput()
- {
- if(SystemActivityNotifications.isLoaded())
- return SystemActivityNotifications.getLastInput();
- else
- return -1;
- }
-
- /**
- * Callback method when receiving notifications.
- *
- * @param type type of the notification.
- */
- public void notify(int type)
- {
- SystemActivityEvent evt = null;
- switch(type)
- {
- case SystemActivityNotifications.NOTIFY_SLEEP :
- evt = new SystemActivityEvent(this,
- SystemActivityEvent.EVENT_SLEEP);
- break;
- case SystemActivityNotifications.NOTIFY_WAKE :
- evt = new SystemActivityEvent(this,
- SystemActivityEvent.EVENT_WAKE);
- break;
- case SystemActivityNotifications.NOTIFY_DISPLAY_SLEEP :
- evt = new SystemActivityEvent(this,
- SystemActivityEvent.EVENT_DISPLAY_SLEEP);
- break;
- case SystemActivityNotifications.NOTIFY_DISPLAY_WAKE :
- evt = new SystemActivityEvent(this,
- SystemActivityEvent.EVENT_DISPLAY_WAKE);
- break;
- case SystemActivityNotifications.NOTIFY_SCREENSAVER_START :
- evt = new SystemActivityEvent(this,
- SystemActivityEvent.EVENT_SCREENSAVER_START);
- break;
- case SystemActivityNotifications.NOTIFY_SCREENSAVER_WILL_STOP :
- evt = new SystemActivityEvent(this,
- SystemActivityEvent.EVENT_SCREENSAVER_WILL_STOP);
- break;
- case SystemActivityNotifications.NOTIFY_SCREENSAVER_STOP :
- evt = new SystemActivityEvent(this,
- SystemActivityEvent.EVENT_SCREENSAVER_STOP);
- break;
- case SystemActivityNotifications.NOTIFY_SCREEN_LOCKED :
- evt = new SystemActivityEvent(this,
- SystemActivityEvent.EVENT_SCREEN_LOCKED);
- break;
- case SystemActivityNotifications.NOTIFY_SCREEN_UNLOCKED :
- evt = new SystemActivityEvent(this,
- SystemActivityEvent.EVENT_SCREEN_UNLOCKED);
- break;
- case SystemActivityNotifications.NOTIFY_NETWORK_CHANGE :
- {
- evt = new SystemActivityEvent(this,
- SystemActivityEvent.EVENT_NETWORK_CHANGE);
- break;
- }
- case SystemActivityNotifications.NOTIFY_DNS_CHANGE :
- {
- evt = new SystemActivityEvent(this,
- SystemActivityEvent.EVENT_DNS_CHANGE);
- break;
- }
- case SystemActivityNotifications.NOTIFY_QUERY_ENDSESSION :
- {
- // both events QUERY_ENDSESSION and ENDSESSION
- // depend on the result one after another
- // we don't put them in new thread in order to give control
- // in the bundles using this events.
- evt = new SystemActivityEvent(this,
- SystemActivityEvent.EVENT_QUERY_ENDSESSION);
- eventDispatcher.fireSystemActivityEventCurrentThread(evt);
-
- return;
- }
- case SystemActivityNotifications.NOTIFY_ENDSESSION :
- {
- // both events QUERY_ENDSESSION and ENDSESSION
- // depend on the result one after another
- // we don't put them in new thread in order to give control
- // in the bundles using this events.
- evt = new SystemActivityEvent(this,
- SystemActivityEvent.EVENT_ENDSESSION);
- eventDispatcher.fireSystemActivityEventCurrentThread(evt);
-
- return;
- }
- }
-
- if (evt != null)
- fireSystemActivityEvent(evt);
- }
-
- /**
- * Callback method when receiving special network notifications.
- *
- * @param family family of network change (ipv6, ipv4)
- * AF_UNSPEC = 0 (The address family is unspecified.)
- * AF_INET = 2 (The Internet Protocol version 4 (IPv4) address family)
- * AF_INET6 = 23 (The Internet Protocol version 6 (IPv6) address family)
- * @param luidIndex unique index of interface
- * @param name name of the interface
- * @param type of the interface
- * Possible values for the interface type are listed in the Ipifcons.h file.
- * common values:
- * IF_TYPE_OTHER = 1 (Some other type of network interface.)
- * IF_TYPE_ETHERNET_CSMACD = 6 (An Ethernet network interface.)
- * IF_TYPE_ISO88025_TOKENRING = 9 (A token ring network interface.)
- * IF_TYPE_PPP = 23 (A PPP network interface.)
- * IF_TYPE_SOFTWARE_LOOPBACK = 24 (A software loopback network interface.)
- * IF_TYPE_IEEE80211 = 71 (An IEEE 802.11 wireless network interface.)
- * IF_TYPE_TUNNEL = 131 (A tunnel type encapsulation network interface.)
- * IF_TYPE_IEEE1394 = 144 (An IEEE 1394 (Firewire) high performance
- * serial bus network interface.)
- * @param connected whether interface is connected or not.
- */
- public void notifyNetworkChange(
- int family,
- long luidIndex,
- String name,
- long type,
- boolean connected)
- {
- long current = System.currentTimeMillis();
- if(current - lastNetworkChange <= NETWORK_EVENT_SILENT_TIME
- && (networkIsConnected != null && networkIsConnected.equals(connected)))
- {
- networkIsConnected = connected;
- return;
- }
-
- lastNetworkChange = current;
- networkIsConnected = connected;
-
- SystemActivityEvent evt = new SystemActivityEvent(this,
- SystemActivityEvent.EVENT_NETWORK_CHANGE);
- fireSystemActivityEvent(evt);
- }
-
- /**
- * The thread run method that handles idle notifies.
- *
- * @see Thread#run()
- */
- public void run()
- {
- while(running)
- {
- try
- {
- long idleTime = 0;
- if(idleChangeListeners.size() > 0)
- {
- // check
- idleTime = SystemActivityNotifications.getLastInput();
-
- if((idleTime < idleStateCheckDelay)
- && (listenersInIdleState.size() > 0))
- {
- for(SystemActivityChangeListener l
- : listenersInIdleState)
- {
- fireSystemIdleEndEvent(l);
- }
- listenersInIdleState.clear();
- }
-
- for(Map.Entry<SystemActivityChangeListener, Long> entry
- : idleChangeListeners.entrySet())
- {
- SystemActivityChangeListener listener =
- entry.getKey();
-
- if(!listenersInIdleState.contains(listener)
- && (entry.getValue() <= idleTime))
- {
- fireSystemIdleEvent(listener);
-
- listenersInIdleState.add(listener);
- }
- }
- }
-
- // if the minimum check for idle is X minutes
- // we will wait before checking (X - Y + 1sec)
- // where Y is the last idle time returned by OS
- if(listenersInIdleState.size() > 0)
- {
- idleStateCheckDelay = CHECK_FOR_IDLE_WHEN_IDLE;
- }
- else if(idleTime != 0)
- {
- long minIdleSetting = CHECK_FOR_IDLE_DEFAULT;
-
- if(!idleChangeListeners.isEmpty())
- minIdleSetting =
- Collections.min(idleChangeListeners.values());
-
- int newSetting = (int)(minIdleSetting - idleTime) + 1000;
-
- if(newSetting > 0)
- idleStateCheckDelay = newSetting;
- else
- idleStateCheckDelay = CHECK_FOR_IDLE_DEFAULT;
- }
- else
- {
- idleStateCheckDelay = CHECK_FOR_IDLE_DEFAULT;
- }
-
- // wait for the specified time
- synchronized(this)
- {
- this.wait(idleStateCheckDelay);
- }
- }
- catch(UnsatisfiedLinkError t)
- {
- logger.error("Missing native impl", t);
- return;
- }
- catch(Throwable t)
- {
- logger.error("Error checking for idle", t);
- }
- }
- }
-
- /**
- * Delivers the specified event to all registered listeners.
- *
- * @param evt the <tt>SystemActivityEvent</tt> that we'd like delivered to
- * all registered message listeners.
- */
- protected void fireSystemActivityEvent(SystemActivityEvent evt)
- {
- int eventID = evt.getEventID();
-
- // Add network activity info to track wake up problems.
- if (logger.isInfoEnabled()
- && ((eventID == SystemActivityEvent.EVENT_NETWORK_CHANGE)
- || (eventID == SystemActivityEvent.EVENT_DNS_CHANGE)))
- {
- logger.info("Received system activity event: " + evt);
- }
-
- if (eventID == SystemActivityEvent.EVENT_NETWORK_CHANGE)
- {
- // Give time to Java to dispatch same event and populate its network
- // interfaces.
- eventDispatcher.fireSystemActivityEvent(evt, 500);
- }
- else
- eventDispatcher.fireSystemActivityEvent(evt);
- }
-
- /**
- * Delivers the specified event to all registered listeners.
- *
- * @param listener listener to inform
- */
- protected void fireSystemIdleEvent(SystemActivityChangeListener listener)
- {
- SystemActivityEvent evt
- = new SystemActivityEvent(
- this,
- SystemActivityEvent.EVENT_SYSTEM_IDLE);
-
- if (logger.isDebugEnabled())
- logger.debug("Dispatching SystemActivityEvent evt=" + evt);
-
- try
- {
- listener.activityChanged(evt);
- }
- catch (Throwable t)
- {
- if (t instanceof ThreadDeath)
- throw (ThreadDeath) t;
- else
- logger.error("Error delivering event", t);
- }
- }
-
- /**
- * Delivers the specified event to listener.
- *
- * @param listener listener to inform
- */
- protected void fireSystemIdleEndEvent(
- SystemActivityChangeListener listener)
- {
- SystemActivityEvent evt
- = new SystemActivityEvent(
- this,
- SystemActivityEvent.EVENT_SYSTEM_IDLE_END);
-
- if (logger.isDebugEnabled())
- logger.debug("Dispatching SystemActivityEvent evt=" + evt);
-
- try
- {
- listener.activityChanged(evt);
- }
- catch (Throwable t)
- {
- if (t instanceof ThreadDeath)
- throw (ThreadDeath) t;
- else
- logger.error("Error delivering event", t);
- }
- }
-
- /**
- * Can check whether an event id is supported on
- * current operation system.
- * Simple return what is implemented in native, and checks
- * are made when possible, for example linux cannot connect
- * to NM through dbus.
- * @param eventID the event to check.
- * @return whether the supplied event id is supported.
- */
- public boolean isSupported(int eventID)
- {
- if(OSUtils.IS_WINDOWS)
- {
- switch(eventID)
- {
- case SystemActivityEvent.EVENT_SLEEP:
- case SystemActivityEvent.EVENT_WAKE:
- case SystemActivityEvent.EVENT_NETWORK_CHANGE:
- case SystemActivityEvent.EVENT_SYSTEM_IDLE:
- case SystemActivityEvent.EVENT_SYSTEM_IDLE_END:
- return SystemActivityNotifications.isLoaded();
- default:
- return false;
- }
- }
- else if(OSUtils.IS_MAC)
- {
- return SystemActivityNotifications.isLoaded();
- }
- else if(OSUtils.IS_LINUX)
- {
- switch(eventID)
- {
- case SystemActivityEvent.EVENT_SLEEP:
- case SystemActivityEvent.EVENT_NETWORK_CHANGE:
- {
- SystemActivityManager currentRunningManager
- = getCurrentRunningManager();
-
- return
- (currentRunningManager == null)
- ? false
- : currentRunningManager.isConnected();
- }
- case SystemActivityEvent.EVENT_SYSTEM_IDLE:
- case SystemActivityEvent.EVENT_SYSTEM_IDLE_END:
- return SystemActivityNotifications.isLoaded();
- default:
- return false;
- }
- }
- else if(OSUtils.IS_ANDROID)
- {
- return (eventID == SystemActivityEvent.EVENT_NETWORK_CHANGE);
- }
- else
- {
- return false;
- }
- }
-
- /**
- * Returns or instantiate the manager.
- * @return
- */
- private SystemActivityManager getCurrentRunningManager()
- {
- if(currentRunningManager == null)
- {
- try
- {
- String className = null;
- if(OSUtils.IS_LINUX)
- {
- className = SYSTEM_ACTIVITY_MANAGER_LINUX_CLASS;
- }
- else if(OSUtils.IS_ANDROID)
- {
- className = SYSTEM_ACTIVITY_MANAGER_ANDROID_CLASS;
- }
-
- if(className != null)
- currentRunningManager = (SystemActivityManager)
- Class.forName(className).newInstance();
- }
- catch(Throwable t)
- {
- logger.error("Error creating manager", t);
- }
- }
-
- return currentRunningManager;
- }
-}
+package net.java.sip.communicator.impl.sysactivity; + +import java.util.*; + +import net.java.sip.communicator.service.sysactivity.*; +import net.java.sip.communicator.service.sysactivity.event.*; +import net.java.sip.communicator.util.Logger; + +import org.jitsi.util.*; + +/** + * Service implementation listens for computer changes as sleeping, network + * change, inactivity. + * + * @author Damian Minkov + */ +public class SystemActivityNotificationsServiceImpl + implements SystemActivityNotifications.NotificationsDelegate, + SystemActivityNotificationsService, + Runnable +{ + /** + * The <tt>Logger</tt> used by this + * <tt>SystemActivityNotificationsServiceImpl</tt> for logging output. + */ + private final Logger logger + = Logger.getLogger(SystemActivityNotificationsServiceImpl.class); + + /** + * The thread dispatcher of network change events. + */ + private final SystemActivityEventDispatcher eventDispatcher + = new SystemActivityEventDispatcher(); + + /** + * A list of listeners registered for idle events. + */ + private final Map<SystemActivityChangeListener,Long> idleChangeListeners + = new HashMap<SystemActivityChangeListener, Long>(); + + /** + * Listeners which are fired for idle state and which will be fired + * with idle end when needed. + */ + private final List<SystemActivityChangeListener> listenersInIdleState + = new ArrayList<SystemActivityChangeListener>(); + + /** + * The interval between checks when not idle. + */ + private static final int CHECK_FOR_IDLE_DEFAULT = 30 * 1000; + + /** + * The interval between checks when idle. The interval is shorter + * so we can react almost immediately when we are active again. + */ + private static final int CHECK_FOR_IDLE_WHEN_IDLE = 1000; + + /** + * The time in milliseconds between two checks for system idle. + */ + private static int idleStateCheckDelay = CHECK_FOR_IDLE_DEFAULT; + + /** + * Whether current service is started or stopped. + */ + private boolean running = false; + + /** + * The time when we received latest network change event. + */ + private long lastNetworkChange = -1; + + /** + * Sometimes (on windows) we got several network change events + * this is the time after which latest event we will skip next events. + */ + private static final long NETWORK_EVENT_SILENT_TIME = 10*1000; + + /** + * Whether network is currently connected. + */ + private Boolean networkIsConnected = null; + + /** + * The linux impl class name. + */ + private static final String SYSTEM_ACTIVITY_MANAGER_LINUX_CLASS + = "net.java.sip.communicator.impl.sysactivity.NetworkManagerListenerImpl"; + + /** + * The android impl class name. + */ + private static final String SYSTEM_ACTIVITY_MANAGER_ANDROID_CLASS + = "net.java.sip.communicator.impl.sysactivity.ConnectivityManagerListenerImpl"; + + /** + * The currently instantiated and working manager. + */ + private SystemActivityManager currentRunningManager = null; + + /** + * Init and start notifications. + */ + public void start() + { + running = true; + + // set the delegate and start notification in new thread + // make sure we don't block startup process + Thread notifystartThread + = new Thread( + new Runnable() + { + public void run() + { + SystemActivityNotifications.setDelegate( + SystemActivityNotificationsServiceImpl.this); + SystemActivityNotifications.start(); + } + }, + "SystemActivityNotificationsServiceImpl"); + notifystartThread.setDaemon(true); + notifystartThread.start(); + + if(isSupported(SystemActivityEvent.EVENT_SYSTEM_IDLE)) + { + // a thread periodically checks system idle state and if it pass the + // idle time for a particular listener, will inform it. + Thread idleNotifyThread = new Thread( + this, + "SystemActivityNotificationsServiceImpl.IdleNotifyThread"); + idleNotifyThread.setDaemon(true); + idleNotifyThread.start(); + } + + if (getCurrentRunningManager() != null) + getCurrentRunningManager().start(); + } + + /** + * Stop notifications. + */ + public void stop() + { + SystemActivityNotifications.stop(); + + if (getCurrentRunningManager() != null) + getCurrentRunningManager().stop(); + + eventDispatcher.stop(); + + running = false; + + synchronized(this) + { + this.notifyAll(); + } + } + + /** + * Registers a listener that would be notified of changes that have occurred + * in the underlying system. + * + * @param listener the listener that we'd like to register for changes in + * the underlying system. + */ + public void addSystemActivityChangeListener( + SystemActivityChangeListener listener) + { + eventDispatcher.addSystemActivityChangeListener(listener); + } + + /** + * Remove the specified listener so that it won't receive further + * notifications of changes that occur in the underlying system + * + * @param listener the listener to remove. + */ + public void removeSystemActivityChangeListener( + SystemActivityChangeListener listener) + { + eventDispatcher.removeSystemActivityChangeListener(listener); + } + + /** + * Registers a listener that would be notified for idle of the system + * for <tt>idleTime</tt>. + * + * @param idleTime the time in milliseconds after which we will consider + * system to be idle. This doesn't count when system seems idle as + * monitor is off or screensaver is on, or desktop is locked. + * @param listener the listener that we'd like to register for changes in + * the underlying system. + */ + public void addIdleSystemChangeListener( + long idleTime, + SystemActivityChangeListener listener) + { + synchronized (idleChangeListeners) + { + if (idleTime > 0 + && !idleChangeListeners.containsKey(listener)) + idleChangeListeners.put(listener, idleTime); + } + } + + /** + * Remove the specified listener so that it won't receive further + * notifications for idle system. + * + * @param listener the listener to remove. + */ + public void removeIdleSystemChangeListener( + SystemActivityChangeListener listener) + { + synchronized (idleChangeListeners) + { + idleChangeListeners.remove(listener); + } + } + + /** + * The time since last user input. The time the system has been idle. + * @return time the system has been idle. + */ + public long getTimeSinceLastInput() + { + if(SystemActivityNotifications.isLoaded()) + return SystemActivityNotifications.getLastInput(); + else + return -1; + } + + /** + * Callback method when receiving notifications. + * + * @param type type of the notification. + */ + public void notify(int type) + { + SystemActivityEvent evt = null; + switch(type) + { + case SystemActivityNotifications.NOTIFY_SLEEP : + evt = new SystemActivityEvent(this, + SystemActivityEvent.EVENT_SLEEP); + break; + case SystemActivityNotifications.NOTIFY_WAKE : + evt = new SystemActivityEvent(this, + SystemActivityEvent.EVENT_WAKE); + break; + case SystemActivityNotifications.NOTIFY_DISPLAY_SLEEP : + evt = new SystemActivityEvent(this, + SystemActivityEvent.EVENT_DISPLAY_SLEEP); + break; + case SystemActivityNotifications.NOTIFY_DISPLAY_WAKE : + evt = new SystemActivityEvent(this, + SystemActivityEvent.EVENT_DISPLAY_WAKE); + break; + case SystemActivityNotifications.NOTIFY_SCREENSAVER_START : + evt = new SystemActivityEvent(this, + SystemActivityEvent.EVENT_SCREENSAVER_START); + break; + case SystemActivityNotifications.NOTIFY_SCREENSAVER_WILL_STOP : + evt = new SystemActivityEvent(this, + SystemActivityEvent.EVENT_SCREENSAVER_WILL_STOP); + break; + case SystemActivityNotifications.NOTIFY_SCREENSAVER_STOP : + evt = new SystemActivityEvent(this, + SystemActivityEvent.EVENT_SCREENSAVER_STOP); + break; + case SystemActivityNotifications.NOTIFY_SCREEN_LOCKED : + evt = new SystemActivityEvent(this, + SystemActivityEvent.EVENT_SCREEN_LOCKED); + break; + case SystemActivityNotifications.NOTIFY_SCREEN_UNLOCKED : + evt = new SystemActivityEvent(this, + SystemActivityEvent.EVENT_SCREEN_UNLOCKED); + break; + case SystemActivityNotifications.NOTIFY_NETWORK_CHANGE : + { + evt = new SystemActivityEvent(this, + SystemActivityEvent.EVENT_NETWORK_CHANGE); + break; + } + case SystemActivityNotifications.NOTIFY_DNS_CHANGE : + { + evt = new SystemActivityEvent(this, + SystemActivityEvent.EVENT_DNS_CHANGE); + break; + } + case SystemActivityNotifications.NOTIFY_QUERY_ENDSESSION : + { + // both events QUERY_ENDSESSION and ENDSESSION + // depend on the result one after another + // we don't put them in new thread in order to give control + // in the bundles using this events. + evt = new SystemActivityEvent(this, + SystemActivityEvent.EVENT_QUERY_ENDSESSION); + eventDispatcher.fireSystemActivityEventCurrentThread(evt); + + return; + } + case SystemActivityNotifications.NOTIFY_ENDSESSION : + { + // both events QUERY_ENDSESSION and ENDSESSION + // depend on the result one after another + // we don't put them in new thread in order to give control + // in the bundles using this events. + evt = new SystemActivityEvent(this, + SystemActivityEvent.EVENT_ENDSESSION); + eventDispatcher.fireSystemActivityEventCurrentThread(evt); + + return; + } + } + + if (evt != null) + fireSystemActivityEvent(evt); + } + + /** + * Callback method when receiving special network notifications. + * + * @param family family of network change (ipv6, ipv4) + * AF_UNSPEC = 0 (The address family is unspecified.) + * AF_INET = 2 (The Internet Protocol version 4 (IPv4) address family) + * AF_INET6 = 23 (The Internet Protocol version 6 (IPv6) address family) + * @param luidIndex unique index of interface + * @param name name of the interface + * @param type of the interface + * Possible values for the interface type are listed in the Ipifcons.h file. + * common values: + * IF_TYPE_OTHER = 1 (Some other type of network interface.) + * IF_TYPE_ETHERNET_CSMACD = 6 (An Ethernet network interface.) + * IF_TYPE_ISO88025_TOKENRING = 9 (A token ring network interface.) + * IF_TYPE_PPP = 23 (A PPP network interface.) + * IF_TYPE_SOFTWARE_LOOPBACK = 24 (A software loopback network interface.) + * IF_TYPE_IEEE80211 = 71 (An IEEE 802.11 wireless network interface.) + * IF_TYPE_TUNNEL = 131 (A tunnel type encapsulation network interface.) + * IF_TYPE_IEEE1394 = 144 (An IEEE 1394 (Firewire) high performance + * serial bus network interface.) + * @param connected whether interface is connected or not. + */ + public void notifyNetworkChange( + int family, + long luidIndex, + String name, + long type, + boolean connected) + { + long current = System.currentTimeMillis(); + if(current - lastNetworkChange <= NETWORK_EVENT_SILENT_TIME + && (networkIsConnected != null && networkIsConnected.equals(connected))) + { + networkIsConnected = connected; + return; + } + + lastNetworkChange = current; + networkIsConnected = connected; + + SystemActivityEvent evt = new SystemActivityEvent(this, + SystemActivityEvent.EVENT_NETWORK_CHANGE); + fireSystemActivityEvent(evt); + } + + /** + * The thread run method that handles idle notifies. + * + * @see Thread#run() + */ + public void run() + { + while(running) + { + try + { + long idleTime = 0; + if(idleChangeListeners.size() > 0) + { + // check + idleTime = SystemActivityNotifications.getLastInput(); + + if((idleTime < idleStateCheckDelay) + && (listenersInIdleState.size() > 0)) + { + for(SystemActivityChangeListener l + : listenersInIdleState) + { + fireSystemIdleEndEvent(l); + } + listenersInIdleState.clear(); + } + + for(Map.Entry<SystemActivityChangeListener, Long> entry + : idleChangeListeners.entrySet()) + { + SystemActivityChangeListener listener = + entry.getKey(); + + if(!listenersInIdleState.contains(listener) + && (entry.getValue() <= idleTime)) + { + fireSystemIdleEvent(listener); + + listenersInIdleState.add(listener); + } + } + } + + // if the minimum check for idle is X minutes + // we will wait before checking (X - Y + 1sec) + // where Y is the last idle time returned by OS + if(listenersInIdleState.size() > 0) + { + idleStateCheckDelay = CHECK_FOR_IDLE_WHEN_IDLE; + } + else if(idleTime != 0) + { + long minIdleSetting = CHECK_FOR_IDLE_DEFAULT; + + if(!idleChangeListeners.isEmpty()) + minIdleSetting = + Collections.min(idleChangeListeners.values()); + + int newSetting = (int)(minIdleSetting - idleTime) + 1000; + + if(newSetting > 0) + idleStateCheckDelay = newSetting; + else + idleStateCheckDelay = CHECK_FOR_IDLE_DEFAULT; + } + else + { + idleStateCheckDelay = CHECK_FOR_IDLE_DEFAULT; + } + + // wait for the specified time + synchronized(this) + { + this.wait(idleStateCheckDelay); + } + } + catch(UnsatisfiedLinkError t) + { + logger.error("Missing native impl", t); + return; + } + catch(Throwable t) + { + logger.error("Error checking for idle", t); + } + } + } + + /** + * Delivers the specified event to all registered listeners. + * + * @param evt the <tt>SystemActivityEvent</tt> that we'd like delivered to + * all registered message listeners. + */ + protected void fireSystemActivityEvent(SystemActivityEvent evt) + { + int eventID = evt.getEventID(); + + // Add network activity info to track wake up problems. + if (logger.isInfoEnabled() + && ((eventID == SystemActivityEvent.EVENT_NETWORK_CHANGE) + || (eventID == SystemActivityEvent.EVENT_DNS_CHANGE))) + { + logger.info("Received system activity event: " + evt); + } + + if (eventID == SystemActivityEvent.EVENT_NETWORK_CHANGE) + { + // Give time to Java to dispatch same event and populate its network + // interfaces. + eventDispatcher.fireSystemActivityEvent(evt, 500); + } + else + eventDispatcher.fireSystemActivityEvent(evt); + } + + /** + * Delivers the specified event to all registered listeners. + * + * @param listener listener to inform + */ + protected void fireSystemIdleEvent(SystemActivityChangeListener listener) + { + SystemActivityEvent evt + = new SystemActivityEvent( + this, + SystemActivityEvent.EVENT_SYSTEM_IDLE); + + if (logger.isDebugEnabled()) + logger.debug("Dispatching SystemActivityEvent evt=" + evt); + + try + { + listener.activityChanged(evt); + } + catch (Throwable t) + { + if (t instanceof ThreadDeath) + throw (ThreadDeath) t; + else + logger.error("Error delivering event", t); + } + } + + /** + * Delivers the specified event to listener. + * + * @param listener listener to inform + */ + protected void fireSystemIdleEndEvent( + SystemActivityChangeListener listener) + { + SystemActivityEvent evt + = new SystemActivityEvent( + this, + SystemActivityEvent.EVENT_SYSTEM_IDLE_END); + + if (logger.isDebugEnabled()) + logger.debug("Dispatching SystemActivityEvent evt=" + evt); + + try + { + listener.activityChanged(evt); + } + catch (Throwable t) + { + if (t instanceof ThreadDeath) + throw (ThreadDeath) t; + else + logger.error("Error delivering event", t); + } + } + + /** + * Can check whether an event id is supported on + * current operation system. + * Simple return what is implemented in native, and checks + * are made when possible, for example linux cannot connect + * to NM through dbus. + * @param eventID the event to check. + * @return whether the supplied event id is supported. + */ + public boolean isSupported(int eventID) + { + if(OSUtils.IS_WINDOWS) + { + switch(eventID) + { + case SystemActivityEvent.EVENT_SLEEP: + case SystemActivityEvent.EVENT_WAKE: + case SystemActivityEvent.EVENT_NETWORK_CHANGE: + case SystemActivityEvent.EVENT_SYSTEM_IDLE: + case SystemActivityEvent.EVENT_SYSTEM_IDLE_END: + return SystemActivityNotifications.isLoaded(); + default: + return false; + } + } + else if(OSUtils.IS_MAC) + { + return SystemActivityNotifications.isLoaded(); + } + else if(OSUtils.IS_LINUX) + { + switch(eventID) + { + case SystemActivityEvent.EVENT_SLEEP: + case SystemActivityEvent.EVENT_NETWORK_CHANGE: + { + SystemActivityManager currentRunningManager + = getCurrentRunningManager(); + + return + (currentRunningManager == null) + ? false + : currentRunningManager.isConnected(); + } + case SystemActivityEvent.EVENT_SYSTEM_IDLE: + case SystemActivityEvent.EVENT_SYSTEM_IDLE_END: + return SystemActivityNotifications.isLoaded(); + default: + return false; + } + } + else if(OSUtils.IS_ANDROID) + { + return (eventID == SystemActivityEvent.EVENT_NETWORK_CHANGE); + } + else + { + return false; + } + } + + /** + * Returns or instantiate the manager. + * @return + */ + private SystemActivityManager getCurrentRunningManager() + { + if(currentRunningManager == null) + { + try + { + String className = null; + if(OSUtils.IS_LINUX) + { + className = SYSTEM_ACTIVITY_MANAGER_LINUX_CLASS; + } + else if(OSUtils.IS_ANDROID) + { + className = SYSTEM_ACTIVITY_MANAGER_ANDROID_CLASS; + } + + if(className != null) + currentRunningManager = (SystemActivityManager) + Class.forName(className).newInstance(); + } + catch(Throwable t) + { + logger.error("Error creating manager", t); + } + } + + return currentRunningManager; + } +} |