1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Copyright @ 2015 Atlassian Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* 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;
}
}
|